1 /* Routines for handling XML generic OS data provided by target. 2 3 Copyright (C) 2008, 2009 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 "defs.h" 21 #include "target.h" 22 #include "vec.h" 23 #include "xml-support.h" 24 #include "osdata.h" 25 #include "gdb_string.h" 26 #include "ui-out.h" 27 #include "gdbcmd.h" 28 29 #if !defined(HAVE_LIBEXPAT) 30 31 struct osdata * 32 osdata_parse (const char *xml) 33 { 34 static int have_warned; 35 36 if (!have_warned) 37 { 38 have_warned = 1; 39 warning (_("Can not parse XML OS data; XML support was disabled " 40 "at compile time")); 41 } 42 43 return NULL; 44 } 45 46 #else /* HAVE_LIBEXPAT */ 47 48 #include "xml-support.h" 49 50 /* Internal parsing data passed to all XML callbacks. */ 51 struct osdata_parsing_data 52 { 53 struct osdata *osdata; 54 char *property_name; 55 }; 56 57 /* Handle the start of a <osdata> element. */ 58 59 static void 60 osdata_start_osdata (struct gdb_xml_parser *parser, 61 const struct gdb_xml_element *element, 62 void *user_data, VEC(gdb_xml_value_s) *attributes) 63 { 64 struct osdata_parsing_data *data = user_data; 65 char *type; 66 struct osdata *osdata; 67 68 if (data->osdata) 69 gdb_xml_error (parser, _("Seen more than on osdata element")); 70 71 type = VEC_index (gdb_xml_value_s, attributes, 0)->value; 72 osdata = XZALLOC (struct osdata); 73 osdata->type = xstrdup (type); 74 data->osdata = osdata; 75 } 76 77 /* Handle the start of a <item> element. */ 78 79 static void 80 osdata_start_item (struct gdb_xml_parser *parser, 81 const struct gdb_xml_element *element, 82 void *user_data, VEC(gdb_xml_value_s) *attributes) 83 { 84 struct osdata_parsing_data *data = user_data; 85 struct osdata_item item = { NULL }; 86 VEC_safe_push (osdata_item_s, data->osdata->items, &item); 87 } 88 89 /* Handle the start of a <column> element. */ 90 91 static void 92 osdata_start_column (struct gdb_xml_parser *parser, 93 const struct gdb_xml_element *element, 94 void *user_data, VEC(gdb_xml_value_s) *attributes) 95 { 96 struct osdata_parsing_data *data = user_data; 97 const char *name = VEC_index (gdb_xml_value_s, attributes, 0)->value; 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 { 108 struct osdata_parsing_data *data = user_data; 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 { 125 struct osdata_parsing_data *data = p; 126 osdata_free (data->osdata); 127 data->osdata = NULL; 128 xfree (data->property_name); 129 data->property_name = NULL; 130 } 131 132 /* The allowed elements and attributes for OS data object. 133 The root element is a <osdata>. */ 134 135 const struct gdb_xml_attribute column_attributes[] = { 136 { "name", GDB_XML_AF_NONE, NULL, NULL }, 137 { NULL, GDB_XML_AF_NONE, NULL, NULL } 138 }; 139 140 const struct gdb_xml_element item_children[] = { 141 { "column", column_attributes, NULL, 142 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL, 143 osdata_start_column, osdata_end_column }, 144 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } 145 }; 146 147 const struct gdb_xml_attribute osdata_attributes[] = { 148 { "type", GDB_XML_AF_NONE, NULL, NULL }, 149 { NULL, GDB_XML_AF_NONE, NULL, NULL } 150 }; 151 152 const struct gdb_xml_element osdata_children[] = { 153 { "item", NULL, item_children, 154 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL, 155 osdata_start_item, NULL }, 156 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } 157 }; 158 159 const struct gdb_xml_element osdata_elements[] = { 160 { "osdata", osdata_attributes, osdata_children, 161 GDB_XML_EF_NONE, osdata_start_osdata, NULL }, 162 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } 163 }; 164 165 struct osdata * 166 osdata_parse (const char *xml) 167 { 168 struct gdb_xml_parser *parser; 169 struct cleanup *before_deleting_result, *back_to; 170 struct osdata_parsing_data data = { NULL }; 171 172 back_to = make_cleanup (null_cleanup, NULL); 173 parser = gdb_xml_create_parser_and_cleanup (_("osdata"), 174 osdata_elements, &data); 175 gdb_xml_use_dtd (parser, "osdata.dtd"); 176 177 before_deleting_result = make_cleanup (clear_parsing_data, &data); 178 179 if (gdb_xml_parse (parser, xml) == 0) 180 /* Parsed successfully, don't need to delete the result. */ 181 discard_cleanups (before_deleting_result); 182 183 do_cleanups (back_to); 184 return data.osdata; 185 } 186 #endif 187 188 static void 189 osdata_item_clear (struct osdata_item *item) 190 { 191 if (item->columns != NULL) 192 { 193 struct osdata_column *col; 194 int ix; 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 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; 218 for (ix = 0; 219 VEC_iterate (osdata_item_s, osdata->items, 220 ix, item); 221 ix++) 222 osdata_item_clear (item); 223 VEC_free (osdata_item_s, osdata->items); 224 } 225 226 xfree (osdata); 227 } 228 229 static void 230 osdata_free_cleanup (void *arg) 231 { 232 struct osdata *osdata = arg; 233 osdata_free (osdata); 234 } 235 236 struct cleanup * 237 make_cleanup_osdata_free (struct osdata *data) 238 { 239 return make_cleanup (osdata_free_cleanup, data); 240 } 241 242 struct osdata * 243 get_osdata (const char *type) 244 { 245 struct osdata *osdata = NULL; 246 char *xml = target_get_osdata (type); 247 if (xml) 248 { 249 struct cleanup *old_chain = make_cleanup (xfree, xml); 250 251 if (xml[0] == '\0') 252 warning (_("Empty data returned by target. Wrong osdata type?")); 253 else 254 osdata = osdata_parse (xml); 255 256 do_cleanups (old_chain); 257 } 258 259 if (!osdata) 260 error (_("Can not fetch data now.\n")); 261 262 return osdata; 263 } 264 265 const char * 266 get_osdata_column (struct osdata_item *item, const char *name) 267 { 268 struct osdata_column *col; 269 int ix_cols; 270 271 for (ix_cols = 0; 272 VEC_iterate (osdata_column_s, item->columns, 273 ix_cols, col); 274 ix_cols++) 275 if (strcmp (col->name, name) == 0) 276 return col->value; 277 278 return NULL; 279 } 280 281 static void 282 info_osdata_command (char *type, int from_tty) 283 { 284 struct osdata *osdata = NULL; 285 struct osdata_item *last; 286 struct cleanup *old_chain; 287 int ncols; 288 int nprocs; 289 290 if (type == 0) 291 /* TODO: No type could mean "list availables types". */ 292 error (_("Argument required.")); 293 294 osdata = get_osdata (type); 295 old_chain = make_cleanup_osdata_free (osdata); 296 297 nprocs = VEC_length (osdata_item_s, osdata->items); 298 299 last = VEC_last (osdata_item_s, osdata->items); 300 if (last && last->columns) 301 ncols = VEC_length (osdata_column_s, last->columns); 302 else 303 ncols = 0; 304 305 make_cleanup_ui_out_table_begin_end (uiout, ncols, nprocs, 306 "OSDataTable"); 307 308 if (last && last->columns) 309 { 310 struct osdata_column *col; 311 int ix; 312 for (ix = 0; 313 VEC_iterate (osdata_column_s, last->columns, 314 ix, col); 315 ix++) 316 ui_out_table_header (uiout, 10, ui_left, 317 col->name, col->name); 318 } 319 320 ui_out_table_body (uiout); 321 322 if (nprocs != 0) 323 { 324 struct osdata_item *item; 325 int ix_items; 326 for (ix_items = 0; 327 VEC_iterate (osdata_item_s, osdata->items, 328 ix_items, item); 329 ix_items++) 330 { 331 struct cleanup *old_chain; 332 struct ui_stream *stb; 333 int ix_cols; 334 struct osdata_column *col; 335 336 stb = ui_out_stream_new (uiout); 337 old_chain = make_cleanup_ui_out_stream_delete (stb); 338 make_cleanup_ui_out_tuple_begin_end (uiout, "item"); 339 340 for (ix_cols = 0; 341 VEC_iterate (osdata_column_s, item->columns, 342 ix_cols, col); 343 ix_cols++) 344 ui_out_field_string (uiout, col->name, col->value); 345 346 do_cleanups (old_chain); 347 348 ui_out_text (uiout, "\n"); 349 } 350 } 351 352 do_cleanups (old_chain); 353 } 354 355 extern initialize_file_ftype _initialize_osdata; /* -Wmissing-prototypes */ 356 357 void 358 _initialize_osdata (void) 359 { 360 add_info ("os", info_osdata_command, 361 _("Show OS data ARG.")); 362 } 363