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 "xml-support.h"
27 #if !defined(HAVE_LIBEXPAT)
29 std::unique_ptr
<osdata
>
30 osdata_parse (const char *xml
)
32 static int have_warned
;
37 warning (_("Can not parse XML OS data; XML support was disabled "
44 #else /* HAVE_LIBEXPAT */
46 /* Internal parsing data passed to all XML callbacks. */
47 struct osdata_parsing_data
49 std::unique_ptr
<struct osdata
> osdata
;
50 std::string property_name
;
53 /* Handle the start of a <osdata> element. */
56 osdata_start_osdata (struct gdb_xml_parser
*parser
,
57 const struct gdb_xml_element
*element
,
59 std::vector
<gdb_xml_value
> &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
.get ();
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
,
76 std::vector
<gdb_xml_value
> &attributes
)
78 struct osdata_parsing_data
*data
= (struct osdata_parsing_data
*) user_data
;
79 data
->osdata
->items
.emplace_back ();
82 /* Handle the start of a <column> element. */
85 osdata_start_column (struct gdb_xml_parser
*parser
,
86 const struct gdb_xml_element
*element
,
88 std::vector
<gdb_xml_value
> &attributes
)
90 struct osdata_parsing_data
*data
= (struct osdata_parsing_data
*) user_data
;
92 = (const char *) xml_find_attribute (attributes
, "name")->value
.get ();
94 data
->property_name
.assign (name
);
97 /* Handle the end of a <column> element. */
100 osdata_end_column (struct gdb_xml_parser
*parser
,
101 const struct gdb_xml_element
*element
,
102 void *user_data
, const char *body_text
)
104 osdata_parsing_data
*data
= (struct osdata_parsing_data
*) user_data
;
105 struct osdata
*osdata
= data
->osdata
.get ();
106 osdata_item
&item
= osdata
->items
.back ();
108 item
.columns
.emplace_back (std::move (data
->property_name
),
109 std::string (body_text
));
112 /* The allowed elements and attributes for OS data object.
113 The root element is a <osdata>. */
115 const struct gdb_xml_attribute column_attributes
[] = {
116 { "name", GDB_XML_AF_NONE
, NULL
, NULL
},
117 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
120 const struct gdb_xml_element item_children
[] = {
121 { "column", column_attributes
, NULL
,
122 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
,
123 osdata_start_column
, osdata_end_column
},
124 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
127 const struct gdb_xml_attribute osdata_attributes
[] = {
128 { "type", GDB_XML_AF_NONE
, NULL
, NULL
},
129 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
132 const struct gdb_xml_element osdata_children
[] = {
133 { "item", NULL
, item_children
,
134 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
,
135 osdata_start_item
, NULL
},
136 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
139 const struct gdb_xml_element osdata_elements
[] = {
140 { "osdata", osdata_attributes
, osdata_children
,
141 GDB_XML_EF_NONE
, osdata_start_osdata
, NULL
},
142 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
145 std::unique_ptr
<osdata
>
146 osdata_parse (const char *xml
)
148 osdata_parsing_data data
;
150 if (gdb_xml_parse_quick (_("osdata"), "osdata.dtd",
151 osdata_elements
, xml
, &data
) == 0)
153 /* Parsed successfully, don't need to delete the result. */
154 return std::move (data
.osdata
);
161 std::unique_ptr
<osdata
>
162 get_osdata (const char *type
)
164 std::unique_ptr
<osdata
> osdata
;
165 gdb::optional
<gdb::char_vector
> xml
= target_get_osdata (type
);
169 if ((*xml
)[0] == '\0')
172 warning (_("Empty data returned by target. Wrong osdata type?"));
174 warning (_("Empty type list returned by target. No type data?"));
177 osdata
= osdata_parse (xml
->data ());
181 error (_("Can not fetch data now."));
187 get_osdata_column (const osdata_item
&item
, const char *name
)
189 for (const osdata_column
&col
: item
.columns
)
190 if (col
.name
== name
)
197 info_osdata (const char *type
)
199 struct ui_out
*uiout
= current_uiout
;
200 struct osdata_item
*last
= NULL
;
202 int col_to_skip
= -1;
207 std::unique_ptr
<osdata
> osdata
= get_osdata (type
);
209 int nrows
= osdata
->items
.size ();
211 if (*type
== '\0' && nrows
== 0)
212 error (_("Available types of OS data not reported."));
214 if (!osdata
->items
.empty ())
216 last
= &osdata
->items
.back ();
217 ncols
= last
->columns
.size ();
219 /* As a special case, scan the listing of available data types
220 for a column named "Title", and only include it with MI
221 output; this column's normal use is for titles for interface
222 elements like menus, and it clutters up CLI output. */
223 if (*type
== '\0' && !uiout
->is_mi_like_p ())
225 for (int ix
= 0; ix
< last
->columns
.size (); ix
++)
227 if (last
->columns
[ix
].name
== "Title")
230 /* Be sure to reduce the total column count, otherwise
231 internal errors ensue. */
232 if (col_to_skip
>= 0)
237 ui_out_emit_table
table_emitter (uiout
, ncols
, nrows
, "OSDataTable");
239 /* With no columns/items, we just output an empty table, but we
240 still output the table. This matters for MI. */
244 if (last
!= NULL
&& !last
->columns
.empty ())
246 for (int ix
= 0; ix
< last
->columns
.size (); ix
++)
250 if (ix
== col_to_skip
)
253 snprintf (col_name
, 32, "col%d", ix
);
254 uiout
->table_header (10, ui_left
,
255 col_name
, last
->columns
[ix
].name
.c_str ());
259 uiout
->table_body ();
263 for (const osdata_item
&item
: osdata
->items
)
266 ui_out_emit_tuple
tuple_emitter (uiout
, "item");
268 for (int ix_cols
= 0; ix_cols
< item
.columns
.size (); ix_cols
++)
272 if (ix_cols
== col_to_skip
)
275 snprintf (col_name
, 32, "col%d", ix_cols
);
276 uiout
->field_string (col_name
,
277 item
.columns
[ix_cols
].value
.c_str ());
287 info_osdata_command (const char *arg
, int from_tty
)
293 _initialize_osdata (void)
295 add_info ("os", info_osdata_command
,
296 _("Show OS data ARG."));