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/>. */
21 #include "xml-support.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
;
36 warning (_("Can not parse XML OS data; XML support was disabled "
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. */
55 osdata_start_osdata (struct gdb_xml_parser
*parser
,
56 const struct gdb_xml_element
*element
,
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. */
72 osdata_start_item (struct gdb_xml_parser
*parser
,
73 const struct gdb_xml_element
*element
,
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. */
84 osdata_start_column (struct gdb_xml_parser
*parser
,
85 const struct gdb_xml_element
*element
,
87 std::vector
<gdb_xml_value
> &attributes
)
89 struct osdata_parsing_data
*data
= (struct osdata_parsing_data
*) user_data
;
91 = (const char *) xml_find_attribute (attributes
, "name")->value
.get ();
93 data
->property_name
.assign (name
);
96 /* Handle the end of a <column> element. */
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
);
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
);
168 if ((*xml
)[0] == '\0')
171 warning (_("Empty data returned by target. Wrong osdata type?"));
173 warning (_("Empty type list returned by target. No type data?"));
176 osdata
= osdata_parse (xml
->data ());
180 error (_("Can not fetch data now."));
186 get_osdata_column (const osdata_item
&item
, const char *name
)
188 for (const osdata_column
&col
: item
.columns
)
189 if (col
.name
== name
)
196 info_osdata (const char *type
)
198 struct ui_out
*uiout
= current_uiout
;
199 struct osdata_item
*last
= NULL
;
201 int col_to_skip
= -1;
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")
229 /* Be sure to reduce the total column count, otherwise
230 internal errors ensue. */
231 if (col_to_skip
>= 0)
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. */
243 if (last
!= NULL
&& !last
->columns
.empty ())
245 for (int ix
= 0; ix
< last
->columns
.size (); ix
++)
249 if (ix
== col_to_skip
)
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 ();
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
++)
271 if (ix_cols
== col_to_skip
)
274 snprintf (col_name
, 32, "col%d", ix_cols
);
275 uiout
->field_string (col_name
, item
.columns
[ix_cols
].value
);
285 info_osdata_command (const char *arg
, int from_tty
)
290 void _initialize_osdata ();
292 _initialize_osdata ()
294 add_info ("os", info_osdata_command
,
295 _("Show OS data ARG."));