GDB (xrefs)
/tmp/gdb-7.10/gdb/osdata.c
Go to the documentation of this file.
1 /* Routines for handling XML generic OS data provided by target.
2 
3  Copyright (C) 2008-2015 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 "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 
47 /* Internal parsing data passed to all XML callbacks. */
49  {
50  struct osdata *osdata;
52  };
53 
54 /* Handle the start of a <osdata> element. */
55 
56 static void
58  const struct gdb_xml_element *element,
59  void *user_data, VEC(gdb_xml_value_s) *attributes)
60 {
61  struct osdata_parsing_data *data = user_data;
62  char *type;
63  struct osdata *osdata;
64 
65  if (data->osdata)
66  gdb_xml_error (parser, _("Seen more than on osdata element"));
67 
68  type = xml_find_attribute (attributes, "type")->value;
69  osdata = XCNEW (struct osdata);
70  osdata->type = xstrdup (type);
71  data->osdata = osdata;
72 }
73 
74 /* Handle the start of a <item> element. */
75 
76 static void
78  const struct gdb_xml_element *element,
79  void *user_data, VEC(gdb_xml_value_s) *attributes)
80 {
81  struct osdata_parsing_data *data = user_data;
82  struct osdata_item item = { NULL };
83 
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
91  const struct gdb_xml_element *element,
92  void *user_data, VEC(gdb_xml_value_s) *attributes)
93 {
94  struct osdata_parsing_data *data = user_data;
95  const char *name = xml_find_attribute (attributes, "name")->value;
96 
97  data->property_name = xstrdup (name);
98 }
99 
100 /* Handle the end of a <column> element. */
101 
102 static void
104  const struct gdb_xml_element *element,
105  void *user_data, const char *body_text)
106 {
107  struct osdata_parsing_data *data = user_data;
108  struct osdata *osdata = data->osdata;
109  struct osdata_item *item = VEC_last (osdata_item_s, osdata->items);
111  item->columns, NULL);
112 
113  /* Transfer memory ownership. NAME was already strdup'ed. */
114  col->name = data->property_name;
115  col->value = xstrdup (body_text);
116  data->property_name = NULL;
117 }
118 
119 /* Discard the constructed osdata (if an error occurs). */
120 
121 static void
123 {
124  struct osdata_parsing_data *data = p;
125 
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,
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,
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,
162  { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
163 };
164 
165 struct osdata *
166 osdata_parse (const char *xml)
167 {
168  struct cleanup *back_to;
169  struct osdata_parsing_data data = { NULL };
170 
171  back_to = make_cleanup (clear_parsing_data, &data);
172 
173  if (gdb_xml_parse_quick (_("osdata"), "osdata.dtd",
174  osdata_elements, xml, &data) == 0)
175  {
176  /* Parsed successfully, don't need to delete the result. */
177  discard_cleanups (back_to);
178  return data.osdata;
179  }
180 
181  do_cleanups (back_to);
182  return NULL;
183 }
184 #endif
185 
186 static void
188 {
189  if (item->columns != NULL)
190  {
191  struct osdata_column *col;
192  int ix;
193 
194  for (ix = 0;
195  VEC_iterate (osdata_column_s, item->columns,
196  ix, col);
197  ix++)
198  {
199  xfree (col->name);
200  xfree (col->value);
201  }
202  VEC_free (osdata_column_s, item->columns);
203  item->columns = NULL;
204  }
205 }
206 
207 void
209 {
210  if (osdata == NULL)
211  return;
212 
213  if (osdata->items != NULL)
214  {
215  struct osdata_item *item;
216  int ix;
217 
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
231 {
232  struct osdata *osdata = arg;
233 
234  osdata_free (osdata);
235 }
236 
237 struct cleanup *
239 {
240  return make_cleanup (osdata_free_cleanup, data);
241 }
242 
243 struct osdata *
244 get_osdata (const char *type)
245 {
246  struct osdata *osdata = NULL;
247  char *xml = target_get_osdata (type);
248 
249  if (xml)
250  {
251  struct cleanup *old_chain = make_cleanup (xfree, xml);
252 
253  if (xml[0] == '\0')
254  {
255  if (type)
256  warning (_("Empty data returned by target. Wrong osdata type?"));
257  else
258  warning (_("Empty type list returned by target. No type data?"));
259  }
260  else
261  osdata = osdata_parse (xml);
262 
263  do_cleanups (old_chain);
264  }
265 
266  if (!osdata)
267  error (_("Can not fetch data now."));
268 
269  return osdata;
270 }
271 
272 const char *
273 get_osdata_column (struct osdata_item *item, const char *name)
274 {
275  struct osdata_column *col;
276  int ix_cols;
277 
278  for (ix_cols = 0;
279  VEC_iterate (osdata_column_s, item->columns,
280  ix_cols, col);
281  ix_cols++)
282  if (strcmp (col->name, name) == 0)
283  return col->value;
284 
285  return NULL;
286 }
287 
288 void
289 info_osdata_command (char *type, int from_tty)
290 {
291  struct ui_out *uiout = current_uiout;
292  struct osdata *osdata = NULL;
293  struct osdata_item *last = NULL;
294  struct cleanup *old_chain;
295  int ncols = 0;
296  int nrows;
297  int col_to_skip = -1;
298 
299  osdata = get_osdata (type);
300  old_chain = make_cleanup_osdata_free (osdata);
301 
302  nrows = VEC_length (osdata_item_s, osdata->items);
303 
304  if (!type && nrows == 0)
305  error (_("Available types of OS data not reported."));
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);
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. */
317  if (!type && !ui_out_is_mi_like_p (uiout))
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  }
334  }
335 
336  make_cleanup_ui_out_table_begin_end (uiout, ncols, nrows,
337  "OSDataTable");
338 
339  /* With no columns/items, we just output an empty table, but we
340  still output the table. This matters for MI. */
341  if (ncols == 0)
342  {
343  do_cleanups (old_chain);
344  return;
345  }
346 
347  if (last && last->columns)
348  {
349  struct osdata_column *col;
350  int ix;
351 
352  for (ix = 0;
353  VEC_iterate (osdata_column_s, last->columns,
354  ix, col);
355  ix++)
356  {
357  char col_name[32];
358 
359  if (ix == col_to_skip)
360  continue;
361 
362  snprintf (col_name, 32, "col%d", ix);
363  ui_out_table_header (uiout, 10, ui_left,
364  col_name, col->name);
365  }
366  }
367 
368  ui_out_table_body (uiout);
369 
370  if (nrows != 0)
371  {
372  struct osdata_item *item;
373  int ix_items;
374 
375  for (ix_items = 0;
376  VEC_iterate (osdata_item_s, osdata->items,
377  ix_items, item);
378  ix_items++)
379  {
380  struct cleanup *old_chain;
381  int ix_cols;
382  struct osdata_column *col;
383 
384  old_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "item");
385 
386  for (ix_cols = 0;
387  VEC_iterate (osdata_column_s, item->columns,
388  ix_cols, col);
389  ix_cols++)
390  {
391  char col_name[32];
392 
393  if (ix_cols == col_to_skip)
394  continue;
395 
396  snprintf (col_name, 32, "col%d", ix_cols);
397  ui_out_field_string (uiout, col_name, col->value);
398  }
399 
400  do_cleanups (old_chain);
401 
402  ui_out_text (uiout, "\n");
403  }
404  }
405 
406  do_cleanups (old_chain);
407 }
408 
409 extern initialize_file_ftype _initialize_osdata; /* -Wmissing-prototypes */
410 
411 void
413 {
415  _("Show OS data ARG."));
416 }
static void osdata_end_column(struct gdb_xml_parser *parser, const struct gdb_xml_element *element, void *user_data, const char *body_text)
Definition: osdata.c:103
void * value
Definition: xml-support.h:76
void xfree(void *)
Definition: common-utils.c:97
void warning(const char *fmt,...)
Definition: errors.c:26
struct cleanup * make_cleanup_ui_out_table_begin_end(struct ui_out *ui_out, int nr_cols, int nr_rows, const char *tblid)
Definition: ui-out.c:369
struct osdata * get_osdata(const char *type)
Definition: osdata.c:244
int ui_out_is_mi_like_p(struct ui_out *uiout)
Definition: ui-out.c:655
initialize_file_ftype _initialize_osdata
#define VEC_safe_push(T, V, O)
Definition: vec.h:260
#define VEC(T)
Definition: vec.h:398
#define _(String)
Definition: gdb_locale.h:40
Definition: ui-out.h:40
char * value
Definition: osdata.h:28
static void osdata_start_column(struct gdb_xml_parser *parser, const struct gdb_xml_element *element, void *user_data, VEC(gdb_xml_value_s)*attributes)
Definition: osdata.c:90
Definition: ui-out.c:99
void ui_out_text(struct ui_out *uiout, const char *string)
Definition: ui-out.c:582
struct osdata * osdata
Definition: osdata.c:50
const char *const name
Definition: aarch64-tdep.c:68
#define VEC_iterate(T, V, I, P)
Definition: vec.h:165
const struct gdb_xml_attribute column_attributes[]
Definition: osdata.c:135
int gdb_xml_parse_quick(const char *name, const char *dtd_name, const struct gdb_xml_element *elements, const char *document, void *user_data)
Definition: xml-support.c:599
void initialize_file_ftype(void)
Definition: defs.h:281
struct cleanup * make_cleanup_ui_out_tuple_begin_end(struct ui_out *uiout, const char *id)
Definition: ui-out.c:451
char * target_get_osdata(const char *type)
Definition: target.c:2648
struct cleanup * make_cleanup_osdata_free(struct osdata *data)
Definition: osdata.c:238
#define VEC_length(T, V)
Definition: vec.h:124
struct cleanup * make_cleanup(make_cleanup_ftype *function, void *arg)
Definition: cleanups.c:117
void osdata_free(struct osdata *osdata)
Definition: osdata.c:208
Definition: gdbtypes.h:749
struct cmd_list_element * add_info(const char *name, cmd_cfunc_ftype *fun, const char *doc)
Definition: cli-decode.c:857
static const char * type
Definition: language.c:103
Definition: osdata.h:38
static void osdata_item_clear(struct osdata_item *item)
Definition: osdata.c:187
void gdb_xml_error(struct gdb_xml_parser *parser, const char *format,...)
Definition: xml-support.c:128
char * property_name
Definition: osdata.c:51
#define VEC_last(T, V)
Definition: vec.h:142
#define VEC_empty(T, V)
Definition: vec.h:132
static void osdata_start_item(struct gdb_xml_parser *parser, const struct gdb_xml_element *element, void *user_data, VEC(gdb_xml_value_s)*attributes)
Definition: osdata.c:77
static void osdata_start_osdata(struct gdb_xml_parser *parser, const struct gdb_xml_element *element, void *user_data, VEC(gdb_xml_value_s)*attributes)
Definition: osdata.c:57
static void osdata_free_cleanup(void *arg)
Definition: osdata.c:230
char * name
Definition: osdata.h:27
struct gdb_xml_value * xml_find_attribute(VEC(gdb_xml_value_s)*attributes, const char *name)
Definition: xml-support.c:142
void discard_cleanups(struct cleanup *old_chain)
Definition: cleanups.c:213
const struct gdb_xml_element osdata_children[]
Definition: osdata.c:152
#define VEC_free(T, V)
Definition: vec.h:180
void ui_out_table_header(struct ui_out *uiout, int width, enum ui_align alignment, const char *col_name, const char *colhdr)
Definition: ui-out.c:346
char * type
Definition: osdata.h:40
const char * get_osdata_column(struct osdata_item *item, const char *name)
Definition: osdata.c:273
void info_osdata_command(char *type, int from_tty)
Definition: osdata.c:289
void ui_out_field_string(struct ui_out *uiout, const char *fldname, const char *string)
Definition: ui-out.c:541
void ui_out_table_body(struct ui_out *uiout)
Definition: ui-out.c:309
struct ui_out * current_uiout
Definition: ui-out.c:233
const struct gdb_xml_element item_children[]
Definition: osdata.c:140
void error(const char *fmt,...)
Definition: errors.c:38
struct osdata * osdata_parse(const char *xml)
Definition: osdata.c:166
const struct gdb_xml_attribute osdata_attributes[]
Definition: osdata.c:147
static void clear_parsing_data(void *p)
Definition: osdata.c:122
void do_cleanups(struct cleanup *old_chain)
Definition: cleanups.c:175