gdb, testsuite: Fix return value in gdb.base/foll-fork.exp
[binutils-gdb.git] / gdb / osdata.c
blobe4d9b0bef97870c83f7ae928fc36e5f65666bb74
1 /* Routines for handling XML generic OS data provided by target.
3 Copyright (C) 2008-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "target.h"
21 #include "xml-support.h"
22 #include "osdata.h"
23 #include "ui-out.h"
24 #include "cli/cli-cmds.h"
26 #if !defined(HAVE_LIBEXPAT)
28 std::unique_ptr<osdata>
29 osdata_parse (const char *xml)
31 static int have_warned;
33 if (!have_warned)
35 have_warned = 1;
36 warning (_("Can not parse XML OS data; XML support was disabled "
37 "at compile time"));
40 return NULL;
43 #else /* HAVE_LIBEXPAT */
45 /* Internal parsing data passed to all XML callbacks. */
46 struct osdata_parsing_data
48 std::unique_ptr<struct osdata> osdata;
49 std::string property_name;
52 /* Handle the start of a <osdata> element. */
54 static void
55 osdata_start_osdata (struct gdb_xml_parser *parser,
56 const struct gdb_xml_element *element,
57 void *user_data,
58 std::vector<gdb_xml_value> &attributes)
60 struct osdata_parsing_data *data = (struct osdata_parsing_data *) user_data;
62 if (data->osdata != NULL)
63 gdb_xml_error (parser, _("Seen more than on osdata element"));
65 char *type = (char *) xml_find_attribute (attributes, "type")->value.get ();
66 data->osdata.reset (new struct osdata (std::string (type)));
69 /* Handle the start of a <item> element. */
71 static void
72 osdata_start_item (struct gdb_xml_parser *parser,
73 const struct gdb_xml_element *element,
74 void *user_data,
75 std::vector<gdb_xml_value> &attributes)
77 struct osdata_parsing_data *data = (struct osdata_parsing_data *) user_data;
78 data->osdata->items.emplace_back ();
81 /* Handle the start of a <column> element. */
83 static void
84 osdata_start_column (struct gdb_xml_parser *parser,
85 const struct gdb_xml_element *element,
86 void *user_data,
87 std::vector<gdb_xml_value> &attributes)
89 struct osdata_parsing_data *data = (struct osdata_parsing_data *) user_data;
90 const char *name
91 = (const char *) xml_find_attribute (attributes, "name")->value.get ();
93 data->property_name.assign (name);
96 /* Handle the end of a <column> element. */
98 static void
99 osdata_end_column (struct gdb_xml_parser *parser,
100 const struct gdb_xml_element *element,
101 void *user_data, const char *body_text)
103 osdata_parsing_data *data = (struct osdata_parsing_data *) user_data;
104 struct osdata *osdata = data->osdata.get ();
105 osdata_item &item = osdata->items.back ();
107 item.columns.emplace_back (std::move (data->property_name),
108 std::string (body_text));
111 /* The allowed elements and attributes for OS data object.
112 The root element is a <osdata>. */
114 const struct gdb_xml_attribute column_attributes[] = {
115 { "name", GDB_XML_AF_NONE, NULL, NULL },
116 { NULL, GDB_XML_AF_NONE, NULL, NULL }
119 const struct gdb_xml_element item_children[] = {
120 { "column", column_attributes, NULL,
121 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
122 osdata_start_column, osdata_end_column },
123 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
126 const struct gdb_xml_attribute osdata_attributes[] = {
127 { "type", GDB_XML_AF_NONE, NULL, NULL },
128 { NULL, GDB_XML_AF_NONE, NULL, NULL }
131 const struct gdb_xml_element osdata_children[] = {
132 { "item", NULL, item_children,
133 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
134 osdata_start_item, NULL },
135 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
138 const struct gdb_xml_element osdata_elements[] = {
139 { "osdata", osdata_attributes, osdata_children,
140 GDB_XML_EF_NONE, osdata_start_osdata, NULL },
141 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
144 std::unique_ptr<osdata>
145 osdata_parse (const char *xml)
147 osdata_parsing_data data;
149 if (gdb_xml_parse_quick (_("osdata"), "osdata.dtd",
150 osdata_elements, xml, &data) == 0)
152 /* Parsed successfully, don't need to delete the result. */
153 return std::move (data.osdata);
156 return NULL;
158 #endif
160 std::unique_ptr<osdata>
161 get_osdata (const char *type)
163 std::unique_ptr<osdata> osdata;
164 std::optional<gdb::char_vector> xml = target_get_osdata (type);
166 if (xml)
168 if ((*xml)[0] == '\0')
170 if (type)
171 warning (_("Empty data returned by target. Wrong osdata type?"));
172 else
173 warning (_("Empty type list returned by target. No type data?"));
175 else
176 osdata = osdata_parse (xml->data ());
179 if (osdata == NULL)
180 error (_("Can not fetch data now."));
182 return osdata;
185 const std::string *
186 get_osdata_column (const osdata_item &item, const char *name)
188 for (const osdata_column &col : item.columns)
189 if (col.name == name)
190 return &col.value;
192 return NULL;
195 void
196 info_osdata (const char *type)
198 struct ui_out *uiout = current_uiout;
199 struct osdata_item *last = NULL;
200 int ncols = 0;
201 int col_to_skip = -1;
203 if (type == NULL)
204 type = "";
206 std::unique_ptr<osdata> osdata = get_osdata (type);
208 int nrows = osdata->items.size ();
210 if (*type == '\0' && nrows == 0)
211 error (_("Available types of OS data not reported."));
213 if (!osdata->items.empty ())
215 last = &osdata->items.back ();
216 ncols = last->columns.size ();
218 /* As a special case, scan the listing of available data types
219 for a column named "Title", and only include it with MI
220 output; this column's normal use is for titles for interface
221 elements like menus, and it clutters up CLI output. */
222 if (*type == '\0' && !uiout->is_mi_like_p ())
224 for (int ix = 0; ix < last->columns.size (); ix++)
226 if (last->columns[ix].name == "Title")
227 col_to_skip = ix;
229 /* Be sure to reduce the total column count, otherwise
230 internal errors ensue. */
231 if (col_to_skip >= 0)
232 --ncols;
236 ui_out_emit_table table_emitter (uiout, ncols, nrows, "OSDataTable");
238 /* With no columns/items, we just output an empty table, but we
239 still output the table. This matters for MI. */
240 if (ncols == 0)
241 return;
243 if (last != NULL && !last->columns.empty ())
245 for (int ix = 0; ix < last->columns.size (); ix++)
247 char col_name[32];
249 if (ix == col_to_skip)
250 continue;
252 snprintf (col_name, 32, "col%d", ix);
253 uiout->table_header (10, ui_left,
254 col_name, last->columns[ix].name.c_str ());
258 uiout->table_body ();
260 if (nrows != 0)
262 for (const osdata_item &item : osdata->items)
265 ui_out_emit_tuple tuple_emitter (uiout, "item");
267 for (int ix_cols = 0; ix_cols < item.columns.size (); ix_cols++)
269 char col_name[32];
271 if (ix_cols == col_to_skip)
272 continue;
274 snprintf (col_name, 32, "col%d", ix_cols);
275 uiout->field_string (col_name, item.columns[ix_cols].value);
279 uiout->text ("\n");
284 static void
285 info_osdata_command (const char *arg, int from_tty)
287 info_osdata (arg);
290 void _initialize_osdata ();
291 void
292 _initialize_osdata ()
294 add_info ("os", info_osdata_command,
295 _("Show OS data ARG."));