gdb/osdata.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Routines for handling XML generic OS data provided by target.

  2.    Copyright (C) 2008-2015 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 "vec.h"
  17. #include "xml-support.h"
  18. #include "osdata.h"
  19. #include "ui-out.h"
  20. #include "gdbcmd.h"

  21. #if !defined(HAVE_LIBEXPAT)

  22. struct osdata *
  23. osdata_parse (const char *xml)
  24. {
  25.   static int have_warned;

  26.   if (!have_warned)
  27.     {
  28.       have_warned = 1;
  29.       warning (_("Can not parse XML OS data; XML support was disabled "
  30.                 "at compile time"));
  31.     }

  32.   return NULL;
  33. }

  34. #else /* HAVE_LIBEXPAT */

  35. /* Internal parsing data passed to all XML callbacks.  */
  36. struct osdata_parsing_data
  37.   {
  38.     struct osdata *osdata;
  39.     char *property_name;
  40.   };

  41. /* Handle the start of a <osdata> element.  */

  42. static void
  43. osdata_start_osdata (struct gdb_xml_parser *parser,
  44.                         const struct gdb_xml_element *element,
  45.                         void *user_data, VEC(gdb_xml_value_s) *attributes)
  46. {
  47.   struct osdata_parsing_data *data = user_data;
  48.   char *type;
  49.   struct osdata *osdata;

  50.   if (data->osdata)
  51.     gdb_xml_error (parser, _("Seen more than on osdata element"));

  52.   type = xml_find_attribute (attributes, "type")->value;
  53.   osdata = XCNEW (struct osdata);
  54.   osdata->type = xstrdup (type);
  55.   data->osdata = osdata;
  56. }

  57. /* Handle the start of a <item> element.  */

  58. static void
  59. osdata_start_item (struct gdb_xml_parser *parser,
  60.                   const struct gdb_xml_element *element,
  61.                   void *user_data, VEC(gdb_xml_value_s) *attributes)
  62. {
  63.   struct osdata_parsing_data *data = user_data;
  64.   struct osdata_item item = { NULL };

  65.   VEC_safe_push (osdata_item_s, data->osdata->items, &item);
  66. }

  67. /* Handle the start of a <column> element.  */

  68. static void
  69. osdata_start_column (struct gdb_xml_parser *parser,
  70.                     const struct gdb_xml_element *element,
  71.                     void *user_data, VEC(gdb_xml_value_s) *attributes)
  72. {
  73.   struct osdata_parsing_data *data = user_data;
  74.   const char *name = xml_find_attribute (attributes, "name")->value;

  75.   data->property_name = xstrdup (name);
  76. }

  77. /* Handle the end of a <column> element.  */

  78. static void
  79. osdata_end_column (struct gdb_xml_parser *parser,
  80.                   const struct gdb_xml_element *element,
  81.                   void *user_data, const char *body_text)
  82. {
  83.   struct osdata_parsing_data *data = user_data;
  84.   struct osdata *osdata = data->osdata;
  85.   struct osdata_item *item = VEC_last (osdata_item_s, osdata->items);
  86.   struct osdata_column *col = VEC_safe_push (osdata_column_s,
  87.                                             item->columns, NULL);

  88.   /* Transfer memory ownership.  NAME was already strdup'ed.  */
  89.   col->name = data->property_name;
  90.   col->value = xstrdup (body_text);
  91.   data->property_name = NULL;
  92. }

  93. /* Discard the constructed osdata (if an error occurs).  */

  94. static void
  95. clear_parsing_data (void *p)
  96. {
  97.   struct osdata_parsing_data *data = p;

  98.   osdata_free (data->osdata);
  99.   data->osdata = NULL;
  100.   xfree (data->property_name);
  101.   data->property_name = NULL;
  102. }

  103. /* The allowed elements and attributes for OS data object.
  104.    The root element is a <osdata>.  */

  105. const struct gdb_xml_attribute column_attributes[] = {
  106.   { "name", GDB_XML_AF_NONE, NULL, NULL },
  107.   { NULL, GDB_XML_AF_NONE, NULL, NULL }
  108. };

  109. const struct gdb_xml_element item_children[] = {
  110.   { "column", column_attributes, NULL,
  111.     GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
  112.     osdata_start_column, osdata_end_column },
  113.   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
  114. };

  115. const struct gdb_xml_attribute osdata_attributes[] = {
  116.   { "type", GDB_XML_AF_NONE, NULL, NULL },
  117.   { NULL, GDB_XML_AF_NONE, NULL, NULL }
  118. };

  119. const struct gdb_xml_element osdata_children[] = {
  120.   { "item", NULL, item_children,
  121.     GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
  122.     osdata_start_item, NULL },
  123.   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
  124. };

  125. const struct gdb_xml_element osdata_elements[] = {
  126.   { "osdata", osdata_attributes, osdata_children,
  127.     GDB_XML_EF_NONE, osdata_start_osdata, NULL },
  128.   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
  129. };

  130. struct osdata *
  131. osdata_parse (const char *xml)
  132. {
  133.   struct cleanup *back_to;
  134.   struct osdata_parsing_data data = { NULL };

  135.   back_to = make_cleanup (clear_parsing_data, &data);

  136.   if (gdb_xml_parse_quick (_("osdata"), "osdata.dtd",
  137.                            osdata_elements, xml, &data) == 0)
  138.     {
  139.       /* Parsed successfully, don't need to delete the result.  */
  140.       discard_cleanups (back_to);
  141.       return data.osdata;
  142.     }

  143.   do_cleanups (back_to);
  144.   return NULL;
  145. }
  146. #endif

  147. static void
  148. osdata_item_clear (struct osdata_item *item)
  149. {
  150.   if (item->columns != NULL)
  151.     {
  152.       struct osdata_column *col;
  153.       int ix;

  154.       for (ix = 0;
  155.            VEC_iterate (osdata_column_s, item->columns,
  156.                         ix, col);
  157.            ix++)
  158.        {
  159.          xfree (col->name);
  160.          xfree (col->value);
  161.        }
  162.       VEC_free (osdata_column_s, item->columns);
  163.       item->columns = NULL;
  164.     }
  165. }

  166. void
  167. osdata_free (struct osdata *osdata)
  168. {
  169.   if (osdata == NULL)
  170.     return;

  171.   if (osdata->items != NULL)
  172.     {
  173.       struct osdata_item *item;
  174.       int ix;

  175.       for (ix = 0;
  176.           VEC_iterate (osdata_item_s, osdata->items,
  177.                        ix, item);
  178.           ix++)
  179.        osdata_item_clear (item);
  180.       VEC_free (osdata_item_s, osdata->items);
  181.     }

  182.   xfree (osdata);
  183. }

  184. static void
  185. osdata_free_cleanup (void *arg)
  186. {
  187.   struct osdata *osdata = arg;

  188.   osdata_free (osdata);
  189. }

  190. struct cleanup *
  191. make_cleanup_osdata_free (struct osdata *data)
  192. {
  193.   return make_cleanup (osdata_free_cleanup, data);
  194. }

  195. struct osdata *
  196. get_osdata (const char *type)
  197. {
  198.   struct osdata *osdata = NULL;
  199.   char *xml = target_get_osdata (type);

  200.   if (xml)
  201.     {
  202.       struct cleanup *old_chain = make_cleanup (xfree, xml);

  203.       if (xml[0] == '\0')
  204.         {
  205.           if (type)
  206.             warning (_("Empty data returned by target.  Wrong osdata type?"));
  207.           else
  208.             warning (_("Empty type list returned by target.  No type data?"));
  209.         }
  210.       else
  211.         osdata = osdata_parse (xml);

  212.       do_cleanups (old_chain);
  213.     }

  214.   if (!osdata)
  215.     error (_("Can not fetch data now."));

  216.   return osdata;
  217. }

  218. const char *
  219. get_osdata_column (struct osdata_item *item, const char *name)
  220. {
  221.   struct osdata_column *col;
  222.   int ix_cols;

  223.   for (ix_cols = 0;
  224.        VEC_iterate (osdata_column_s, item->columns,
  225.                     ix_cols, col);
  226.        ix_cols++)
  227.     if (strcmp (col->name, name) == 0)
  228.       return col->value;

  229.   return NULL;
  230. }

  231. void
  232. info_osdata_command (char *type, int from_tty)
  233. {
  234.   struct ui_out *uiout = current_uiout;
  235.   struct osdata *osdata = NULL;
  236.   struct osdata_item *last = NULL;
  237.   struct cleanup *old_chain;
  238.   int ncols = 0;
  239.   int nrows;
  240.   int col_to_skip = -1;

  241.   osdata = get_osdata (type);
  242.   old_chain = make_cleanup_osdata_free (osdata);

  243.   nrows = VEC_length (osdata_item_s, osdata->items);

  244.   if (!type && nrows == 0)
  245.     error (_("Available types of OS data not reported."));

  246.   if (!VEC_empty (osdata_item_s, osdata->items))
  247.     {
  248.       last = VEC_last (osdata_item_s, osdata->items);
  249.       if (last->columns)
  250.         ncols = VEC_length (osdata_column_s, last->columns);

  251.       /* As a special case, scan the listing of available data types
  252.          for a column named "Title", and only include it with MI
  253.          output; this column's normal use is for titles for interface
  254.          elements like menus, and it clutters up CLI output.  */
  255.       if (!type && !ui_out_is_mi_like_p (uiout))
  256.         {
  257.           struct osdata_column *col;
  258.           int ix;

  259.           for (ix = 0;
  260.                VEC_iterate (osdata_column_s, last->columns, ix, col);
  261.                ix++)
  262.             {
  263.               if (strcmp (col->name, "Title") == 0)
  264.                 col_to_skip = ix;
  265.             }
  266.           /* Be sure to reduce the total column count, otherwise
  267.              internal errors ensue.  */
  268.           if (col_to_skip >= 0)
  269.             --ncols;
  270.         }
  271.     }

  272.   make_cleanup_ui_out_table_begin_end (uiout, ncols, nrows,
  273.                                        "OSDataTable");

  274.   /* With no columns/items, we just output an empty table, but we
  275.      still output the table.  This matters for MI.  */
  276.   if (ncols == 0)
  277.     {
  278.       do_cleanups (old_chain);
  279.       return;
  280.     }

  281.   if (last && last->columns)
  282.     {
  283.       struct osdata_column *col;
  284.       int ix;

  285.       for (ix = 0;
  286.           VEC_iterate (osdata_column_s, last->columns,
  287.                        ix, col);
  288.           ix++)
  289.         {
  290.           char col_name[32];

  291.           if (ix == col_to_skip)
  292.             continue;

  293.           snprintf (col_name, 32, "col%d", ix);
  294.           ui_out_table_header (uiout, 10, ui_left,
  295.                                col_name, col->name);
  296.         }
  297.     }

  298.   ui_out_table_body (uiout);

  299.   if (nrows != 0)
  300.     {
  301.       struct osdata_item *item;
  302.       int ix_items;

  303.       for (ix_items = 0;
  304.           VEC_iterate (osdata_item_s, osdata->items,
  305.                        ix_items, item);
  306.           ix_items++)
  307.        {
  308.          struct cleanup *old_chain;
  309.          int ix_cols;
  310.          struct osdata_column *col;

  311.          old_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "item");

  312.          for (ix_cols = 0;
  313.               VEC_iterate (osdata_column_s, item->columns,
  314.                            ix_cols, col);
  315.               ix_cols++)
  316.            {
  317.              char col_name[32];

  318.              if (ix_cols == col_to_skip)
  319.                continue;

  320.              snprintf (col_name, 32, "col%d", ix_cols);
  321.              ui_out_field_string (uiout, col_name, col->value);
  322.            }

  323.          do_cleanups (old_chain);

  324.          ui_out_text (uiout, "\n");
  325.        }
  326.     }

  327.   do_cleanups (old_chain);
  328. }

  329. extern initialize_file_ftype _initialize_osdata; /* -Wmissing-prototypes */

  330. void
  331. _initialize_osdata (void)
  332. {
  333.   add_info ("os", info_osdata_command,
  334.            _("Show OS data ARG."));
  335. }