1 /* Routines for handling XML generic OS data provided by target.
3 Copyright (C) 2008-2018 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/>. */
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,
59 void *user_data, VEC(gdb_xml_value_s) *attributes)
61 struct osdata_parsing_data *data = (struct osdata_parsing_data *) user_data;
63 if (data->osdata != NULL)
64 gdb_xml_error (parser, _("Seen more than on osdata element"));
66 char *type = (char *) xml_find_attribute (attributes, "type")->value;
67 data->osdata.reset (new struct osdata (std::string (type)));
70 /* Handle the start of a <item> element. */
73 osdata_start_item (struct gdb_xml_parser *parser,
74 const struct gdb_xml_element *element,
75 void *user_data, VEC(gdb_xml_value_s) *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. */
84 osdata_start_column (struct gdb_xml_parser *parser,
85 const struct gdb_xml_element *element,
86 void *user_data, VEC(gdb_xml_value_s) *attributes)
88 struct osdata_parsing_data *data = (struct osdata_parsing_data *) user_data;
90 = (const char *) xml_find_attribute (attributes, "name")->value;
92 data->property_name.assign (name);
95 /* Handle the end of a <column> element. */
98 osdata_end_column (struct gdb_xml_parser *parser,
99 const struct gdb_xml_element *element,
100 void *user_data, const char *body_text)
102 osdata_parsing_data *data = (struct osdata_parsing_data *) user_data;
103 struct osdata *osdata = data->osdata.get ();
104 osdata_item &item = osdata->items.back ();
106 item.columns.emplace_back (std::move (data->property_name),
107 std::string (body_text));
110 /* The allowed elements and attributes for OS data object.
111 The root element is a <osdata>. */
113 const struct gdb_xml_attribute column_attributes[] = {
114 { "name", GDB_XML_AF_NONE, NULL, NULL },
115 { NULL, GDB_XML_AF_NONE, NULL, NULL }
118 const struct gdb_xml_element item_children[] = {
119 { "column", column_attributes, NULL,
120 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
121 osdata_start_column, osdata_end_column },
122 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
125 const struct gdb_xml_attribute osdata_attributes[] = {
126 { "type", GDB_XML_AF_NONE, NULL, NULL },
127 { NULL, GDB_XML_AF_NONE, NULL, NULL }
130 const struct gdb_xml_element osdata_children[] = {
131 { "item", NULL, item_children,
132 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
133 osdata_start_item, NULL },
134 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
137 const struct gdb_xml_element osdata_elements[] = {
138 { "osdata", osdata_attributes, osdata_children,
139 GDB_XML_EF_NONE, osdata_start_osdata, NULL },
140 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
143 std::unique_ptr<osdata>
144 osdata_parse (const char *xml)
146 osdata_parsing_data data;
148 if (gdb_xml_parse_quick (_("osdata"), "osdata.dtd",
149 osdata_elements, xml, &data) == 0)
151 /* Parsed successfully, don't need to delete the result. */
152 return std::move (data.osdata);
159 std::unique_ptr<osdata>
160 get_osdata (const char *type)
162 std::unique_ptr<osdata> osdata;
163 gdb::unique_xmalloc_ptr<char> xml = target_get_osdata (type);
167 if (xml.get ()[0] == '\0')
170 warning (_("Empty data returned by target. Wrong osdata type?"));
172 warning (_("Empty type list returned by target. No type data?"));
175 osdata = osdata_parse (xml.get ());
179 error (_("Can not fetch data now."));
185 get_osdata_column (const osdata_item &item, const char *name)
187 for (const osdata_column &col : item.columns)
188 if (col.name == name)
195 info_osdata (const char *type)
197 struct ui_out *uiout = current_uiout;
198 struct osdata_item *last = NULL;
200 int col_to_skip = -1;
205 std::unique_ptr<osdata> osdata = get_osdata (type);
207 int nrows = osdata->items.size ();
209 if (*type == '\0' && nrows == 0)
210 error (_("Available types of OS data not reported."));
212 if (!osdata->items.empty ())
214 last = &osdata->items.back ();
215 ncols = last->columns.size ();
217 /* As a special case, scan the listing of available data types
218 for a column named "Title", and only include it with MI
219 output; this column's normal use is for titles for interface
220 elements like menus, and it clutters up CLI output. */
221 if (*type == '\0' && !uiout->is_mi_like_p ())
223 for (int ix = 0; ix < last->columns.size (); ix++)
225 if (last->columns[ix].name == "Title")
228 /* Be sure to reduce the total column count, otherwise
229 internal errors ensue. */
230 if (col_to_skip >= 0)
235 ui_out_emit_table table_emitter (uiout, ncols, nrows, "OSDataTable");
237 /* With no columns/items, we just output an empty table, but we
238 still output the table. This matters for MI. */
242 if (last != NULL && !last->columns.empty ())
244 for (int ix = 0; ix < last->columns.size (); ix++)
248 if (ix == col_to_skip)
251 snprintf (col_name, 32, "col%d", ix);
252 uiout->table_header (10, ui_left,
253 col_name, last->columns[ix].name.c_str ());
257 uiout->table_body ();
261 for (const osdata_item &item : osdata->items)
264 ui_out_emit_tuple tuple_emitter (uiout, "item");
266 for (int ix_cols = 0; ix_cols < item.columns.size (); ix_cols++)
270 if (ix_cols == col_to_skip)
273 snprintf (col_name, 32, "col%d", ix_cols);
274 uiout->field_string (col_name,
275 item.columns[ix_cols].value.c_str ());
285 info_osdata_command (const char *arg, int from_tty)
291 _initialize_osdata (void)
293 add_info ("os", info_osdata_command,
294 _("Show OS data ARG."));