Commit | Line | Data |
---|---|---|
07e059b5 VP |
1 | /* Routines for handling XML generic OS data provided by target. |
2 | ||
61baf725 | 3 | Copyright (C) 2008-2017 Free Software Foundation, Inc. |
07e059b5 VP |
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 "defs.h" | |
21 | #include "target.h" | |
22 | #include "vec.h" | |
23 | #include "xml-support.h" | |
24 | #include "osdata.h" | |
07e059b5 VP |
25 | #include "ui-out.h" |
26 | #include "gdbcmd.h" | |
27 | ||
28 | #if !defined(HAVE_LIBEXPAT) | |
29 | ||
30 | struct osdata * | |
31 | osdata_parse (const char *xml) | |
32 | { | |
33 | static int have_warned; | |
34 | ||
35 | if (!have_warned) | |
36 | { | |
37 | have_warned = 1; | |
38 | warning (_("Can not parse XML OS data; XML support was disabled " | |
39 | "at compile time")); | |
40 | } | |
41 | ||
42 | return NULL; | |
43 | } | |
44 | ||
45 | #else /* HAVE_LIBEXPAT */ | |
46 | ||
07e059b5 VP |
47 | /* Internal parsing data passed to all XML callbacks. */ |
48 | struct osdata_parsing_data | |
49 | { | |
50 | struct osdata *osdata; | |
51 | char *property_name; | |
52 | }; | |
53 | ||
07e059b5 VP |
54 | /* Handle the start of a <osdata> element. */ |
55 | ||
56 | static void | |
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) | |
60 | { | |
19ba03f4 | 61 | struct osdata_parsing_data *data = (struct osdata_parsing_data *) user_data; |
07e059b5 VP |
62 | char *type; |
63 | struct osdata *osdata; | |
64 | ||
65 | if (data->osdata) | |
66 | gdb_xml_error (parser, _("Seen more than on osdata element")); | |
67 | ||
19ba03f4 | 68 | type = (char *) xml_find_attribute (attributes, "type")->value; |
41bf6aca | 69 | osdata = XCNEW (struct osdata); |
07e059b5 VP |
70 | osdata->type = xstrdup (type); |
71 | data->osdata = osdata; | |
72 | } | |
73 | ||
74 | /* Handle the start of a <item> element. */ | |
75 | ||
76 | static void | |
77 | osdata_start_item (struct gdb_xml_parser *parser, | |
78 | const struct gdb_xml_element *element, | |
79 | void *user_data, VEC(gdb_xml_value_s) *attributes) | |
80 | { | |
19ba03f4 | 81 | struct osdata_parsing_data *data = (struct osdata_parsing_data *) user_data; |
07e059b5 | 82 | struct osdata_item item = { NULL }; |
5cc80db3 | 83 | |
07e059b5 VP |
84 | VEC_safe_push (osdata_item_s, data->osdata->items, &item); |
85 | } | |
86 | ||
87 | /* Handle the start of a <column> element. */ | |
88 | ||
89 | static void | |
90 | osdata_start_column (struct gdb_xml_parser *parser, | |
91 | const struct gdb_xml_element *element, | |
92 | void *user_data, VEC(gdb_xml_value_s) *attributes) | |
93 | { | |
19ba03f4 SM |
94 | struct osdata_parsing_data *data = (struct osdata_parsing_data *) user_data; |
95 | const char *name | |
96 | = (const char *) xml_find_attribute (attributes, "name")->value; | |
5cc80db3 | 97 | |
07e059b5 VP |
98 | data->property_name = xstrdup (name); |
99 | } | |
100 | ||
101 | /* Handle the end of a <column> element. */ | |
102 | ||
103 | static void | |
104 | osdata_end_column (struct gdb_xml_parser *parser, | |
105 | const struct gdb_xml_element *element, | |
106 | void *user_data, const char *body_text) | |
107 | { | |
19ba03f4 | 108 | struct osdata_parsing_data *data = (struct osdata_parsing_data *) user_data; |
07e059b5 VP |
109 | struct osdata *osdata = data->osdata; |
110 | struct osdata_item *item = VEC_last (osdata_item_s, osdata->items); | |
111 | struct osdata_column *col = VEC_safe_push (osdata_column_s, | |
112 | item->columns, NULL); | |
113 | ||
114 | /* Transfer memory ownership. NAME was already strdup'ed. */ | |
115 | col->name = data->property_name; | |
116 | col->value = xstrdup (body_text); | |
117 | data->property_name = NULL; | |
118 | } | |
119 | ||
120 | /* Discard the constructed osdata (if an error occurs). */ | |
121 | ||
122 | static void | |
123 | clear_parsing_data (void *p) | |
124 | { | |
19ba03f4 | 125 | struct osdata_parsing_data *data = (struct osdata_parsing_data *) p; |
5cc80db3 | 126 | |
07e059b5 VP |
127 | osdata_free (data->osdata); |
128 | data->osdata = NULL; | |
129 | xfree (data->property_name); | |
130 | data->property_name = NULL; | |
131 | } | |
132 | ||
133 | /* The allowed elements and attributes for OS data object. | |
134 | The root element is a <osdata>. */ | |
135 | ||
136 | const struct gdb_xml_attribute column_attributes[] = { | |
137 | { "name", GDB_XML_AF_NONE, NULL, NULL }, | |
138 | { NULL, GDB_XML_AF_NONE, NULL, NULL } | |
139 | }; | |
140 | ||
141 | const struct gdb_xml_element item_children[] = { | |
142 | { "column", column_attributes, NULL, | |
143 | GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL, | |
144 | osdata_start_column, osdata_end_column }, | |
145 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } | |
146 | }; | |
147 | ||
148 | const struct gdb_xml_attribute osdata_attributes[] = { | |
149 | { "type", GDB_XML_AF_NONE, NULL, NULL }, | |
150 | { NULL, GDB_XML_AF_NONE, NULL, NULL } | |
151 | }; | |
152 | ||
153 | const struct gdb_xml_element osdata_children[] = { | |
154 | { "item", NULL, item_children, | |
155 | GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL, | |
156 | osdata_start_item, NULL }, | |
157 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } | |
158 | }; | |
159 | ||
160 | const struct gdb_xml_element osdata_elements[] = { | |
161 | { "osdata", osdata_attributes, osdata_children, | |
162 | GDB_XML_EF_NONE, osdata_start_osdata, NULL }, | |
163 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } | |
164 | }; | |
165 | ||
166 | struct osdata * | |
167 | osdata_parse (const char *xml) | |
168 | { | |
efc0eabd | 169 | struct cleanup *back_to; |
07e059b5 VP |
170 | struct osdata_parsing_data data = { NULL }; |
171 | ||
efc0eabd | 172 | back_to = make_cleanup (clear_parsing_data, &data); |
07e059b5 | 173 | |
efc0eabd PA |
174 | if (gdb_xml_parse_quick (_("osdata"), "osdata.dtd", |
175 | osdata_elements, xml, &data) == 0) | |
176 | { | |
177 | /* Parsed successfully, don't need to delete the result. */ | |
178 | discard_cleanups (back_to); | |
179 | return data.osdata; | |
180 | } | |
07e059b5 VP |
181 | |
182 | do_cleanups (back_to); | |
efc0eabd | 183 | return NULL; |
07e059b5 VP |
184 | } |
185 | #endif | |
186 | ||
e0665bc8 PA |
187 | static void |
188 | osdata_item_clear (struct osdata_item *item) | |
189 | { | |
190 | if (item->columns != NULL) | |
191 | { | |
192 | struct osdata_column *col; | |
193 | int ix; | |
5cc80db3 | 194 | |
e0665bc8 PA |
195 | for (ix = 0; |
196 | VEC_iterate (osdata_column_s, item->columns, | |
197 | ix, col); | |
198 | ix++) | |
199 | { | |
200 | xfree (col->name); | |
201 | xfree (col->value); | |
202 | } | |
203 | VEC_free (osdata_column_s, item->columns); | |
204 | item->columns = NULL; | |
205 | } | |
206 | } | |
207 | ||
07e059b5 VP |
208 | void |
209 | osdata_free (struct osdata *osdata) | |
210 | { | |
211 | if (osdata == NULL) | |
212 | return; | |
213 | ||
214 | if (osdata->items != NULL) | |
215 | { | |
216 | struct osdata_item *item; | |
217 | int ix; | |
5cc80db3 | 218 | |
07e059b5 VP |
219 | for (ix = 0; |
220 | VEC_iterate (osdata_item_s, osdata->items, | |
221 | ix, item); | |
222 | ix++) | |
223 | osdata_item_clear (item); | |
224 | VEC_free (osdata_item_s, osdata->items); | |
225 | } | |
226 | ||
227 | xfree (osdata); | |
228 | } | |
229 | ||
e0665bc8 PA |
230 | static void |
231 | osdata_free_cleanup (void *arg) | |
232 | { | |
19ba03f4 | 233 | struct osdata *osdata = (struct osdata *) arg; |
5cc80db3 | 234 | |
e0665bc8 PA |
235 | osdata_free (osdata); |
236 | } | |
237 | ||
238 | struct cleanup * | |
239 | make_cleanup_osdata_free (struct osdata *data) | |
240 | { | |
241 | return make_cleanup (osdata_free_cleanup, data); | |
242 | } | |
243 | ||
244 | struct osdata * | |
245 | get_osdata (const char *type) | |
07e059b5 | 246 | { |
e0665bc8 | 247 | struct osdata *osdata = NULL; |
b7b030ad | 248 | gdb::unique_xmalloc_ptr<char> xml = target_get_osdata (type); |
5cc80db3 | 249 | |
07e059b5 VP |
250 | if (xml) |
251 | { | |
b7b030ad | 252 | if (xml.get ()[0] == '\0') |
a61408f8 SS |
253 | { |
254 | if (type) | |
255 | warning (_("Empty data returned by target. Wrong osdata type?")); | |
256 | else | |
257 | warning (_("Empty type list returned by target. No type data?")); | |
258 | } | |
e0665bc8 | 259 | else |
b7b030ad | 260 | osdata = osdata_parse (xml.get ()); |
07e059b5 | 261 | } |
e0665bc8 | 262 | |
07e059b5 | 263 | if (!osdata) |
b37520b6 | 264 | error (_("Can not fetch data now.")); |
07e059b5 VP |
265 | |
266 | return osdata; | |
267 | } | |
268 | ||
269 | const char * | |
270 | get_osdata_column (struct osdata_item *item, const char *name) | |
271 | { | |
272 | struct osdata_column *col; | |
273 | int ix_cols; | |
274 | ||
275 | for (ix_cols = 0; | |
276 | VEC_iterate (osdata_column_s, item->columns, | |
277 | ix_cols, col); | |
278 | ix_cols++) | |
279 | if (strcmp (col->name, name) == 0) | |
280 | return col->value; | |
281 | ||
282 | return NULL; | |
283 | } | |
284 | ||
f3e0e960 | 285 | void |
fdf9e36f | 286 | info_osdata (const char *type) |
07e059b5 | 287 | { |
79a45e25 | 288 | struct ui_out *uiout = current_uiout; |
e0665bc8 | 289 | struct osdata *osdata = NULL; |
8443c207 | 290 | struct osdata_item *last = NULL; |
e0665bc8 | 291 | struct cleanup *old_chain; |
8443c207 KY |
292 | int ncols = 0; |
293 | int nrows; | |
71caed83 | 294 | int col_to_skip = -1; |
07e059b5 | 295 | |
fdf9e36f PA |
296 | if (type == NULL) |
297 | type = ""; | |
298 | ||
07e059b5 | 299 | osdata = get_osdata (type); |
e0665bc8 | 300 | old_chain = make_cleanup_osdata_free (osdata); |
07e059b5 | 301 | |
8443c207 | 302 | nrows = VEC_length (osdata_item_s, osdata->items); |
07e059b5 | 303 | |
fdf9e36f | 304 | if (*type == '\0' && nrows == 0) |
a61408f8 | 305 | error (_("Available types of OS data not reported.")); |
8443c207 KY |
306 | |
307 | if (!VEC_empty (osdata_item_s, osdata->items)) | |
308 | { | |
309 | last = VEC_last (osdata_item_s, osdata->items); | |
310 | if (last->columns) | |
311 | ncols = VEC_length (osdata_column_s, last->columns); | |
71caed83 SS |
312 | |
313 | /* As a special case, scan the listing of available data types | |
314 | for a column named "Title", and only include it with MI | |
315 | output; this column's normal use is for titles for interface | |
316 | elements like menus, and it clutters up CLI output. */ | |
a5bef50f | 317 | if (*type == '\0' && !uiout->is_mi_like_p ()) |
71caed83 SS |
318 | { |
319 | struct osdata_column *col; | |
320 | int ix; | |
321 | ||
322 | for (ix = 0; | |
323 | VEC_iterate (osdata_column_s, last->columns, ix, col); | |
324 | ix++) | |
325 | { | |
326 | if (strcmp (col->name, "Title") == 0) | |
327 | col_to_skip = ix; | |
328 | } | |
329 | /* Be sure to reduce the total column count, otherwise | |
330 | internal errors ensue. */ | |
331 | if (col_to_skip >= 0) | |
332 | --ncols; | |
333 | } | |
8443c207 | 334 | } |
a61408f8 | 335 | |
4a2b031d | 336 | ui_out_emit_table table_emitter (uiout, ncols, nrows, "OSDataTable"); |
07e059b5 | 337 | |
8443c207 KY |
338 | /* With no columns/items, we just output an empty table, but we |
339 | still output the table. This matters for MI. */ | |
340 | if (ncols == 0) | |
341 | { | |
342 | do_cleanups (old_chain); | |
343 | return; | |
344 | } | |
345 | ||
07e059b5 VP |
346 | if (last && last->columns) |
347 | { | |
348 | struct osdata_column *col; | |
349 | int ix; | |
5cc80db3 | 350 | |
07e059b5 VP |
351 | for (ix = 0; |
352 | VEC_iterate (osdata_column_s, last->columns, | |
353 | ix, col); | |
354 | ix++) | |
8443c207 KY |
355 | { |
356 | char col_name[32]; | |
71caed83 SS |
357 | |
358 | if (ix == col_to_skip) | |
359 | continue; | |
360 | ||
8443c207 | 361 | snprintf (col_name, 32, "col%d", ix); |
112e8700 | 362 | uiout->table_header (10, ui_left, |
8443c207 KY |
363 | col_name, col->name); |
364 | } | |
07e059b5 VP |
365 | } |
366 | ||
112e8700 | 367 | uiout->table_body (); |
07e059b5 | 368 | |
8443c207 | 369 | if (nrows != 0) |
07e059b5 VP |
370 | { |
371 | struct osdata_item *item; | |
372 | int ix_items; | |
5cc80db3 | 373 | |
07e059b5 VP |
374 | for (ix_items = 0; |
375 | VEC_iterate (osdata_item_s, osdata->items, | |
376 | ix_items, item); | |
377 | ix_items++) | |
378 | { | |
07e059b5 VP |
379 | int ix_cols; |
380 | struct osdata_column *col; | |
381 | ||
2e783024 TT |
382 | { |
383 | ui_out_emit_tuple tuple_emitter (uiout, "item"); | |
07e059b5 | 384 | |
2e783024 TT |
385 | for (ix_cols = 0; |
386 | VEC_iterate (osdata_column_s, item->columns, | |
387 | ix_cols, col); | |
388 | ix_cols++) | |
389 | { | |
390 | char col_name[32]; | |
71caed83 | 391 | |
2e783024 TT |
392 | if (ix_cols == col_to_skip) |
393 | continue; | |
71caed83 | 394 | |
2e783024 TT |
395 | snprintf (col_name, 32, "col%d", ix_cols); |
396 | uiout->field_string (col_name, col->value); | |
397 | } | |
398 | } | |
07e059b5 | 399 | |
112e8700 | 400 | uiout->text ("\n"); |
07e059b5 VP |
401 | } |
402 | } | |
403 | ||
e0665bc8 | 404 | do_cleanups (old_chain); |
07e059b5 VP |
405 | } |
406 | ||
fdf9e36f | 407 | static void |
1d12d88f | 408 | info_osdata_command (const char *arg, int from_tty) |
fdf9e36f PA |
409 | { |
410 | info_osdata (arg); | |
411 | } | |
412 | ||
07e059b5 VP |
413 | void |
414 | _initialize_osdata (void) | |
415 | { | |
416 | add_info ("os", info_osdata_command, | |
417 | _("Show OS data ARG.")); | |
07e059b5 | 418 | } |