osdata.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /* Routines for handling XML generic OS data provided by target.
  2. Copyright (C) 2008-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "target.h"
  16. #include "xml-support.h"
  17. #include "osdata.h"
  18. #include "ui-out.h"
  19. #include "gdbcmd.h"
  20. #if !defined(HAVE_LIBEXPAT)
  21. std::unique_ptr<osdata>
  22. osdata_parse (const char *xml)
  23. {
  24. static int have_warned;
  25. if (!have_warned)
  26. {
  27. have_warned = 1;
  28. warning (_("Can not parse XML OS data; XML support was disabled "
  29. "at compile time"));
  30. }
  31. return NULL;
  32. }
  33. #else /* HAVE_LIBEXPAT */
  34. /* Internal parsing data passed to all XML callbacks. */
  35. struct osdata_parsing_data
  36. {
  37. std::unique_ptr<struct osdata> osdata;
  38. std::string property_name;
  39. };
  40. /* Handle the start of a <osdata> element. */
  41. static void
  42. osdata_start_osdata (struct gdb_xml_parser *parser,
  43. const struct gdb_xml_element *element,
  44. void *user_data,
  45. std::vector<gdb_xml_value> &attributes)
  46. {
  47. struct osdata_parsing_data *data = (struct osdata_parsing_data *) user_data;
  48. if (data->osdata != NULL)
  49. gdb_xml_error (parser, _("Seen more than on osdata element"));
  50. char *type = (char *) xml_find_attribute (attributes, "type")->value.get ();
  51. data->osdata.reset (new struct osdata (std::string (type)));
  52. }
  53. /* Handle the start of a <item> element. */
  54. static void
  55. osdata_start_item (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. data->osdata->items.emplace_back ();
  62. }
  63. /* Handle the start of a <column> element. */
  64. static void
  65. osdata_start_column (struct gdb_xml_parser *parser,
  66. const struct gdb_xml_element *element,
  67. void *user_data,
  68. std::vector<gdb_xml_value> &attributes)
  69. {
  70. struct osdata_parsing_data *data = (struct osdata_parsing_data *) user_data;
  71. const char *name
  72. = (const char *) xml_find_attribute (attributes, "name")->value.get ();
  73. data->property_name.assign (name);
  74. }
  75. /* Handle the end of a <column> element. */
  76. static void
  77. osdata_end_column (struct gdb_xml_parser *parser,
  78. const struct gdb_xml_element *element,
  79. void *user_data, const char *body_text)
  80. {
  81. osdata_parsing_data *data = (struct osdata_parsing_data *) user_data;
  82. struct osdata *osdata = data->osdata.get ();
  83. osdata_item &item = osdata->items.back ();
  84. item.columns.emplace_back (std::move (data->property_name),
  85. std::string (body_text));
  86. }
  87. /* The allowed elements and attributes for OS data object.
  88. The root element is a <osdata>. */
  89. const struct gdb_xml_attribute column_attributes[] = {
  90. { "name", GDB_XML_AF_NONE, NULL, NULL },
  91. { NULL, GDB_XML_AF_NONE, NULL, NULL }
  92. };
  93. const struct gdb_xml_element item_children[] = {
  94. { "column", column_attributes, NULL,
  95. GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
  96. osdata_start_column, osdata_end_column },
  97. { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
  98. };
  99. const struct gdb_xml_attribute osdata_attributes[] = {
  100. { "type", GDB_XML_AF_NONE, NULL, NULL },
  101. { NULL, GDB_XML_AF_NONE, NULL, NULL }
  102. };
  103. const struct gdb_xml_element osdata_children[] = {
  104. { "item", NULL, item_children,
  105. GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
  106. osdata_start_item, NULL },
  107. { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
  108. };
  109. const struct gdb_xml_element osdata_elements[] = {
  110. { "osdata", osdata_attributes, osdata_children,
  111. GDB_XML_EF_NONE, osdata_start_osdata, NULL },
  112. { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
  113. };
  114. std::unique_ptr<osdata>
  115. osdata_parse (const char *xml)
  116. {
  117. osdata_parsing_data data;
  118. if (gdb_xml_parse_quick (_("osdata"), "osdata.dtd",
  119. osdata_elements, xml, &data) == 0)
  120. {
  121. /* Parsed successfully, don't need to delete the result. */
  122. return std::move (data.osdata);
  123. }
  124. return NULL;
  125. }
  126. #endif
  127. std::unique_ptr<osdata>
  128. get_osdata (const char *type)
  129. {
  130. std::unique_ptr<osdata> osdata;
  131. gdb::optional<gdb::char_vector> xml = target_get_osdata (type);
  132. if (xml)
  133. {
  134. if ((*xml)[0] == '\0')
  135. {
  136. if (type)
  137. warning (_("Empty data returned by target. Wrong osdata type?"));
  138. else
  139. warning (_("Empty type list returned by target. No type data?"));
  140. }
  141. else
  142. osdata = osdata_parse (xml->data ());
  143. }
  144. if (osdata == NULL)
  145. error (_("Can not fetch data now."));
  146. return osdata;
  147. }
  148. const std::string *
  149. get_osdata_column (const osdata_item &item, const char *name)
  150. {
  151. for (const osdata_column &col : item.columns)
  152. if (col.name == name)
  153. return &col.value;
  154. return NULL;
  155. }
  156. void
  157. info_osdata (const char *type)
  158. {
  159. struct ui_out *uiout = current_uiout;
  160. struct osdata_item *last = NULL;
  161. int ncols = 0;
  162. int col_to_skip = -1;
  163. if (type == NULL)
  164. type = "";
  165. std::unique_ptr<osdata> osdata = get_osdata (type);
  166. int nrows = osdata->items.size ();
  167. if (*type == '\0' && nrows == 0)
  168. error (_("Available types of OS data not reported."));
  169. if (!osdata->items.empty ())
  170. {
  171. last = &osdata->items.back ();
  172. ncols = last->columns.size ();
  173. /* As a special case, scan the listing of available data types
  174. for a column named "Title", and only include it with MI
  175. output; this column's normal use is for titles for interface
  176. elements like menus, and it clutters up CLI output. */
  177. if (*type == '\0' && !uiout->is_mi_like_p ())
  178. {
  179. for (int ix = 0; ix < last->columns.size (); ix++)
  180. {
  181. if (last->columns[ix].name == "Title")
  182. col_to_skip = ix;
  183. }
  184. /* Be sure to reduce the total column count, otherwise
  185. internal errors ensue. */
  186. if (col_to_skip >= 0)
  187. --ncols;
  188. }
  189. }
  190. ui_out_emit_table table_emitter (uiout, ncols, nrows, "OSDataTable");
  191. /* With no columns/items, we just output an empty table, but we
  192. still output the table. This matters for MI. */
  193. if (ncols == 0)
  194. return;
  195. if (last != NULL && !last->columns.empty ())
  196. {
  197. for (int ix = 0; ix < last->columns.size (); ix++)
  198. {
  199. char col_name[32];
  200. if (ix == col_to_skip)
  201. continue;
  202. snprintf (col_name, 32, "col%d", ix);
  203. uiout->table_header (10, ui_left,
  204. col_name, last->columns[ix].name.c_str ());
  205. }
  206. }
  207. uiout->table_body ();
  208. if (nrows != 0)
  209. {
  210. for (const osdata_item &item : osdata->items)
  211. {
  212. {
  213. ui_out_emit_tuple tuple_emitter (uiout, "item");
  214. for (int ix_cols = 0; ix_cols < item.columns.size (); ix_cols++)
  215. {
  216. char col_name[32];
  217. if (ix_cols == col_to_skip)
  218. continue;
  219. snprintf (col_name, 32, "col%d", ix_cols);
  220. uiout->field_string (col_name, item.columns[ix_cols].value);
  221. }
  222. }
  223. uiout->text ("\n");
  224. }
  225. }
  226. }
  227. static void
  228. info_osdata_command (const char *arg, int from_tty)
  229. {
  230. info_osdata (arg);
  231. }
  232. void _initialize_osdata ();
  233. void
  234. _initialize_osdata ()
  235. {
  236. add_info ("os", info_osdata_command,
  237. _("Show OS data ARG."));
  238. }