1 /* Routines for handling XML generic OS data provided by target.
3 Copyright (C) 2008-2019 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/>. */
22 #include "gdbsupport/vec.h"
23 #include "xml-support.h"
28 #if !defined(HAVE_LIBEXPAT)
30 std::unique_ptr<osdata>
31 osdata_parse (const char *xml)
33 static int have_warned;
38 warning (_("Can not parse XML OS data; XML support was disabled "
45 #else /* HAVE_LIBEXPAT */
47 /* Internal parsing data passed to all XML callbacks. */
48 struct osdata_parsing_data
50 std::unique_ptr<struct osdata> osdata;
51 std::string property_name;
54 /* Handle the start of a <osdata> element. */
57 osdata_start_osdata (struct gdb_xml_parser *parser,
58 const struct gdb_xml_element *element,
60 std::vector<gdb_xml_value> &attributes)
62 struct osdata_parsing_data *data = (struct osdata_parsing_data *) user_data;
64 if (data->osdata != NULL)
65 gdb_xml_error (parser, _("Seen more than on osdata element"));
67 char *type = (char *) xml_find_attribute (attributes, "type")->value.get ();
68 data->osdata.reset (new struct osdata (std::string (type)));
71 /* Handle the start of a <item> element. */
74 osdata_start_item (struct gdb_xml_parser *parser,
75 const struct gdb_xml_element *element,
77 std::vector<gdb_xml_value> &attributes)
79 struct osdata_parsing_data *data = (struct osdata_parsing_data *) user_data;
80 data->osdata->items.emplace_back ();
83 /* Handle the start of a <column> element. */
86 osdata_start_column (struct gdb_xml_parser *parser,
87 const struct gdb_xml_element *element,
89 std::vector<gdb_xml_value> &attributes)
91 struct osdata_parsing_data *data = (struct osdata_parsing_data *) user_data;
93 = (const char *) xml_find_attribute (attributes, "name")->value.get ();
95 data->property_name.assign (name);
98 /* Handle the end of a <column> element. */
101 osdata_end_column (struct gdb_xml_parser *parser,
102 const struct gdb_xml_element *element,
103 void *user_data, const char *body_text)
105 osdata_parsing_data *data = (struct osdata_parsing_data *) user_data;
106 struct osdata *osdata = data->osdata.get ();
107 osdata_item &item = osdata->items.back ();
109 item.columns.emplace_back (std::move (data->property_name),
110 std::string (body_text));
113 /* The allowed elements and attributes for OS data object.
114 The root element is a <osdata>. */
116 const struct gdb_xml_attribute column_attributes[] = {
117 { "name", GDB_XML_AF_NONE, NULL, NULL },
118 { NULL, GDB_XML_AF_NONE, NULL, NULL }
121 const struct gdb_xml_element item_children[] = {
122 { "column", column_attributes, NULL,
123 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
124 osdata_start_column, osdata_end_column },
125 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
128 const struct gdb_xml_attribute osdata_attributes[] = {
129 { "type", GDB_XML_AF_NONE, NULL, NULL },
130 { NULL, GDB_XML_AF_NONE, NULL, NULL }
133 const struct gdb_xml_element osdata_children[] = {
134 { "item", NULL, item_children,
135 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
136 osdata_start_item, NULL },
137 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
140 const struct gdb_xml_element osdata_elements[] = {
141 { "osdata", osdata_attributes, osdata_children,
142 GDB_XML_EF_NONE, osdata_start_osdata, NULL },
143 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
146 std::unique_ptr<osdata>
147 osdata_parse (const char *xml)
149 osdata_parsing_data data;
151 if (gdb_xml_parse_quick (_("osdata"), "osdata.dtd",
152 osdata_elements, xml, &data) == 0)
154 /* Parsed successfully, don't need to delete the result. */
155 return std::move (data.osdata);
162 std::unique_ptr<osdata>
163 get_osdata (const char *type)
165 std::unique_ptr<osdata> osdata;
166 gdb::optional<gdb::char_vector> xml = target_get_osdata (type);
170 if ((*xml)[0] == '\0')
173 warning (_("Empty data returned by target. Wrong osdata type?"));
175 warning (_("Empty type list returned by target. No type data?"));
178 osdata = osdata_parse (xml->data ());
182 error (_("Can not fetch data now."));
188 get_osdata_column (const osdata_item &item, const char *name)
190 for (const osdata_column &col : item.columns)
191 if (col.name == name)
198 info_osdata (const char *type)
200 struct ui_out *uiout = current_uiout;
201 struct osdata_item *last = NULL;
203 int col_to_skip = -1;
208 std::unique_ptr<osdata> osdata = get_osdata (type);
210 int nrows = osdata->items.size ();
212 if (*type == '\0' && nrows == 0)
213 error (_("Available types of OS data not reported."));
215 if (!osdata->items.empty ())
217 last = &osdata->items.back ();
218 ncols = last->columns.size ();
220 /* As a special case, scan the listing of available data types
221 for a column named "Title", and only include it with MI
222 output; this column's normal use is for titles for interface
223 elements like menus, and it clutters up CLI output. */
224 if (*type == '\0' && !uiout->is_mi_like_p ())
226 for (int ix = 0; ix < last->columns.size (); ix++)
228 if (last->columns[ix].name == "Title")
231 /* Be sure to reduce the total column count, otherwise
232 internal errors ensue. */
233 if (col_to_skip >= 0)
238 ui_out_emit_table table_emitter (uiout, ncols, nrows, "OSDataTable");
240 /* With no columns/items, we just output an empty table, but we
241 still output the table. This matters for MI. */
245 if (last != NULL && !last->columns.empty ())
247 for (int ix = 0; ix < last->columns.size (); ix++)
251 if (ix == col_to_skip)
254 snprintf (col_name, 32, "col%d", ix);
255 uiout->table_header (10, ui_left,
256 col_name, last->columns[ix].name.c_str ());
260 uiout->table_body ();
264 for (const osdata_item &item : osdata->items)
267 ui_out_emit_tuple tuple_emitter (uiout, "item");
269 for (int ix_cols = 0; ix_cols < item.columns.size (); ix_cols++)
273 if (ix_cols == col_to_skip)
276 snprintf (col_name, 32, "col%d", ix_cols);
277 uiout->field_string (col_name,
278 item.columns[ix_cols].value.c_str ());
288 info_osdata_command (const char *arg, int from_tty)
294 _initialize_osdata (void)
296 add_info ("os", info_osdata_command,
297 _("Show OS data ARG."));