osdata.c revision 1.11 1 /* Routines for handling XML generic OS data provided by target.
2
3 Copyright (C) 2008-2024 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 #include "target.h"
21 #include "xml-support.h"
22 #include "osdata.h"
23 #include "ui-out.h"
24 #include "cli/cli-cmds.h"
25
26 #if !defined(HAVE_LIBEXPAT)
27
28 std::unique_ptr<osdata>
29 osdata_parse (const char *xml)
30 {
31 static int have_warned;
32
33 if (!have_warned)
34 {
35 have_warned = 1;
36 warning (_("Can not parse XML OS data; XML support was disabled "
37 "at compile time"));
38 }
39
40 return NULL;
41 }
42
43 #else /* HAVE_LIBEXPAT */
44
45 /* Internal parsing data passed to all XML callbacks. */
46 struct osdata_parsing_data
47 {
48 std::unique_ptr<struct osdata> osdata;
49 std::string property_name;
50 };
51
52 /* Handle the start of a <osdata> element. */
53
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)
59 {
60 struct osdata_parsing_data *data = (struct osdata_parsing_data *) user_data;
61
62 if (data->osdata != NULL)
63 gdb_xml_error (parser, _("Seen more than on osdata element"));
64
65 char *type = (char *) xml_find_attribute (attributes, "type")->value.get ();
66 data->osdata.reset (new struct osdata (std::string (type)));
67 }
68
69 /* Handle the start of a <item> element. */
70
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)
76 {
77 struct osdata_parsing_data *data = (struct osdata_parsing_data *) user_data;
78 data->osdata->items.emplace_back ();
79 }
80
81 /* Handle the start of a <column> element. */
82
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)
88 {
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 ();
92
93 data->property_name.assign (name);
94 }
95
96 /* Handle the end of a <column> element. */
97
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)
102 {
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 ();
106
107 item.columns.emplace_back (std::move (data->property_name),
108 std::string (body_text));
109 }
110
111 /* The allowed elements and attributes for OS data object.
112 The root element is a <osdata>. */
113
114 const struct gdb_xml_attribute column_attributes[] = {
115 { "name", GDB_XML_AF_NONE, NULL, NULL },
116 { NULL, GDB_XML_AF_NONE, NULL, NULL }
117 };
118
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 }
124 };
125
126 const struct gdb_xml_attribute osdata_attributes[] = {
127 { "type", GDB_XML_AF_NONE, NULL, NULL },
128 { NULL, GDB_XML_AF_NONE, NULL, NULL }
129 };
130
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 }
136 };
137
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 }
142 };
143
144 std::unique_ptr<osdata>
145 osdata_parse (const char *xml)
146 {
147 osdata_parsing_data data;
148
149 if (gdb_xml_parse_quick (_("osdata"), "osdata.dtd",
150 osdata_elements, xml, &data) == 0)
151 {
152 /* Parsed successfully, don't need to delete the result. */
153 return std::move (data.osdata);
154 }
155
156 return NULL;
157 }
158 #endif
159
160 std::unique_ptr<osdata>
161 get_osdata (const char *type)
162 {
163 std::unique_ptr<osdata> osdata;
164 std::optional<gdb::char_vector> xml = target_get_osdata (type);
165
166 if (xml)
167 {
168 if ((*xml)[0] == '\0')
169 {
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?"));
174 }
175 else
176 osdata = osdata_parse (xml->data ());
177 }
178
179 if (osdata == NULL)
180 error (_("Can not fetch data now."));
181
182 return osdata;
183 }
184
185 const std::string *
186 get_osdata_column (const osdata_item &item, const char *name)
187 {
188 for (const osdata_column &col : item.columns)
189 if (col.name == name)
190 return &col.value;
191
192 return NULL;
193 }
194
195 void
196 info_osdata (const char *type)
197 {
198 struct ui_out *uiout = current_uiout;
199 struct osdata_item *last = NULL;
200 int ncols = 0;
201 int col_to_skip = -1;
202
203 if (type == NULL)
204 type = "";
205
206 std::unique_ptr<osdata> osdata = get_osdata (type);
207
208 int nrows = osdata->items.size ();
209
210 if (*type == '\0' && nrows == 0)
211 error (_("Available types of OS data not reported."));
212
213 if (!osdata->items.empty ())
214 {
215 last = &osdata->items.back ();
216 ncols = last->columns.size ();
217
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 ())
223 {
224 for (int ix = 0; ix < last->columns.size (); ix++)
225 {
226 if (last->columns[ix].name == "Title")
227 col_to_skip = ix;
228 }
229 /* Be sure to reduce the total column count, otherwise
230 internal errors ensue. */
231 if (col_to_skip >= 0)
232 --ncols;
233 }
234 }
235
236 ui_out_emit_table table_emitter (uiout, ncols, nrows, "OSDataTable");
237
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;
242
243 if (last != NULL && !last->columns.empty ())
244 {
245 for (int ix = 0; ix < last->columns.size (); ix++)
246 {
247 char col_name[32];
248
249 if (ix == col_to_skip)
250 continue;
251
252 snprintf (col_name, 32, "col%d", ix);
253 uiout->table_header (10, ui_left,
254 col_name, last->columns[ix].name.c_str ());
255 }
256 }
257
258 uiout->table_body ();
259
260 if (nrows != 0)
261 {
262 for (const osdata_item &item : osdata->items)
263 {
264 {
265 ui_out_emit_tuple tuple_emitter (uiout, "item");
266
267 for (int ix_cols = 0; ix_cols < item.columns.size (); ix_cols++)
268 {
269 char col_name[32];
270
271 if (ix_cols == col_to_skip)
272 continue;
273
274 snprintf (col_name, 32, "col%d", ix_cols);
275 uiout->field_string (col_name, item.columns[ix_cols].value);
276 }
277 }
278
279 uiout->text ("\n");
280 }
281 }
282 }
283
284 static void
285 info_osdata_command (const char *arg, int from_tty)
286 {
287 info_osdata (arg);
288 }
289
290 void _initialize_osdata ();
291 void
292 _initialize_osdata ()
293 {
294 add_info ("os", info_osdata_command,
295 _("Show OS data ARG."));
296 }
297