2010-05-15 Michael Snyder <msnyder@vmware.com>
[deliverable/binutils-gdb.git] / gdb / osdata.c
CommitLineData
07e059b5
VP
1/* Routines for handling XML generic OS data provided by target.
2
4c38e0a4 3 Copyright (C) 2008, 2009, 2010 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"
25#include "gdb_string.h"
26#include "ui-out.h"
27#include "gdbcmd.h"
28
29#if !defined(HAVE_LIBEXPAT)
30
31struct osdata *
32osdata_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. */
51struct osdata_parsing_data
52 {
53 struct osdata *osdata;
54 char *property_name;
55 };
56
07e059b5
VP
57/* Handle the start of a <osdata> element. */
58
59static void
60osdata_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
79static void
80osdata_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 };
5cc80db3 86
07e059b5
VP
87 VEC_safe_push (osdata_item_s, data->osdata->items, &item);
88}
89
90/* Handle the start of a <column> element. */
91
92static void
93osdata_start_column (struct gdb_xml_parser *parser,
94 const struct gdb_xml_element *element,
95 void *user_data, VEC(gdb_xml_value_s) *attributes)
96{
97 struct osdata_parsing_data *data = user_data;
98 const char *name = VEC_index (gdb_xml_value_s, attributes, 0)->value;
5cc80db3 99
07e059b5
VP
100 data->property_name = xstrdup (name);
101}
102
103/* Handle the end of a <column> element. */
104
105static void
106osdata_end_column (struct gdb_xml_parser *parser,
107 const struct gdb_xml_element *element,
108 void *user_data, const char *body_text)
109{
110 struct osdata_parsing_data *data = user_data;
111 struct osdata *osdata = data->osdata;
112 struct osdata_item *item = VEC_last (osdata_item_s, osdata->items);
113 struct osdata_column *col = VEC_safe_push (osdata_column_s,
114 item->columns, NULL);
115
116 /* Transfer memory ownership. NAME was already strdup'ed. */
117 col->name = data->property_name;
118 col->value = xstrdup (body_text);
119 data->property_name = NULL;
120}
121
122/* Discard the constructed osdata (if an error occurs). */
123
124static void
125clear_parsing_data (void *p)
126{
127 struct osdata_parsing_data *data = p;
5cc80db3 128
07e059b5
VP
129 osdata_free (data->osdata);
130 data->osdata = NULL;
131 xfree (data->property_name);
132 data->property_name = NULL;
133}
134
135/* The allowed elements and attributes for OS data object.
136 The root element is a <osdata>. */
137
138const struct gdb_xml_attribute column_attributes[] = {
139 { "name", GDB_XML_AF_NONE, NULL, NULL },
140 { NULL, GDB_XML_AF_NONE, NULL, NULL }
141};
142
143const struct gdb_xml_element item_children[] = {
144 { "column", column_attributes, NULL,
145 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
146 osdata_start_column, osdata_end_column },
147 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
148};
149
150const struct gdb_xml_attribute osdata_attributes[] = {
151 { "type", GDB_XML_AF_NONE, NULL, NULL },
152 { NULL, GDB_XML_AF_NONE, NULL, NULL }
153};
154
155const struct gdb_xml_element osdata_children[] = {
156 { "item", NULL, item_children,
157 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
158 osdata_start_item, NULL },
159 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
160};
161
162const struct gdb_xml_element osdata_elements[] = {
163 { "osdata", osdata_attributes, osdata_children,
164 GDB_XML_EF_NONE, osdata_start_osdata, NULL },
165 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
166};
167
168struct osdata *
169osdata_parse (const char *xml)
170{
171 struct gdb_xml_parser *parser;
172 struct cleanup *before_deleting_result, *back_to;
173 struct osdata_parsing_data data = { NULL };
174
175 back_to = make_cleanup (null_cleanup, NULL);
176 parser = gdb_xml_create_parser_and_cleanup (_("osdata"),
177 osdata_elements, &data);
178 gdb_xml_use_dtd (parser, "osdata.dtd");
179
180 before_deleting_result = make_cleanup (clear_parsing_data, &data);
181
182 if (gdb_xml_parse (parser, xml) == 0)
183 /* Parsed successfully, don't need to delete the result. */
184 discard_cleanups (before_deleting_result);
185
186 do_cleanups (back_to);
187 return data.osdata;
188}
189#endif
190
e0665bc8
PA
191static void
192osdata_item_clear (struct osdata_item *item)
193{
194 if (item->columns != NULL)
195 {
196 struct osdata_column *col;
197 int ix;
5cc80db3 198
e0665bc8
PA
199 for (ix = 0;
200 VEC_iterate (osdata_column_s, item->columns,
201 ix, col);
202 ix++)
203 {
204 xfree (col->name);
205 xfree (col->value);
206 }
207 VEC_free (osdata_column_s, item->columns);
208 item->columns = NULL;
209 }
210}
211
07e059b5
VP
212void
213osdata_free (struct osdata *osdata)
214{
215 if (osdata == NULL)
216 return;
217
218 if (osdata->items != NULL)
219 {
220 struct osdata_item *item;
221 int ix;
5cc80db3 222
07e059b5
VP
223 for (ix = 0;
224 VEC_iterate (osdata_item_s, osdata->items,
225 ix, item);
226 ix++)
227 osdata_item_clear (item);
228 VEC_free (osdata_item_s, osdata->items);
229 }
230
231 xfree (osdata);
232}
233
e0665bc8
PA
234static void
235osdata_free_cleanup (void *arg)
236{
237 struct osdata *osdata = arg;
5cc80db3 238
e0665bc8
PA
239 osdata_free (osdata);
240}
241
242struct cleanup *
243make_cleanup_osdata_free (struct osdata *data)
244{
245 return make_cleanup (osdata_free_cleanup, data);
246}
247
248struct osdata *
249get_osdata (const char *type)
07e059b5 250{
e0665bc8 251 struct osdata *osdata = NULL;
07e059b5 252 char *xml = target_get_osdata (type);
5cc80db3 253
07e059b5
VP
254 if (xml)
255 {
e0665bc8
PA
256 struct cleanup *old_chain = make_cleanup (xfree, xml);
257
07e059b5
VP
258 if (xml[0] == '\0')
259 warning (_("Empty data returned by target. Wrong osdata type?"));
e0665bc8
PA
260 else
261 osdata = osdata_parse (xml);
262
263 do_cleanups (old_chain);
07e059b5 264 }
e0665bc8 265
07e059b5
VP
266 if (!osdata)
267 error (_("Can not fetch data now.\n"));
268
269 return osdata;
270}
271
272const char *
273get_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
2c0b251b 288static void
07e059b5
VP
289info_osdata_command (char *type, int from_tty)
290{
e0665bc8 291 struct osdata *osdata = NULL;
07e059b5 292 struct osdata_item *last;
e0665bc8 293 struct cleanup *old_chain;
07e059b5
VP
294 int ncols;
295 int nprocs;
296
297 if (type == 0)
298 /* TODO: No type could mean "list availables types". */
299 error (_("Argument required."));
300
301 osdata = get_osdata (type);
e0665bc8 302 old_chain = make_cleanup_osdata_free (osdata);
07e059b5
VP
303
304 nprocs = VEC_length (osdata_item_s, osdata->items);
305
306 last = VEC_last (osdata_item_s, osdata->items);
307 if (last && last->columns)
308 ncols = VEC_length (osdata_column_s, last->columns);
309 else
310 ncols = 0;
311
e0665bc8
PA
312 make_cleanup_ui_out_table_begin_end (uiout, ncols, nprocs,
313 "OSDataTable");
07e059b5
VP
314
315 if (last && last->columns)
316 {
317 struct osdata_column *col;
318 int ix;
5cc80db3 319
07e059b5
VP
320 for (ix = 0;
321 VEC_iterate (osdata_column_s, last->columns,
322 ix, col);
323 ix++)
324 ui_out_table_header (uiout, 10, ui_left,
325 col->name, col->name);
326 }
327
328 ui_out_table_body (uiout);
329
330 if (nprocs != 0)
331 {
332 struct osdata_item *item;
333 int ix_items;
5cc80db3 334
07e059b5
VP
335 for (ix_items = 0;
336 VEC_iterate (osdata_item_s, osdata->items,
337 ix_items, item);
338 ix_items++)
339 {
e0665bc8 340 struct cleanup *old_chain;
07e059b5
VP
341 struct ui_stream *stb;
342 int ix_cols;
343 struct osdata_column *col;
344
345 stb = ui_out_stream_new (uiout);
346 old_chain = make_cleanup_ui_out_stream_delete (stb);
e0665bc8 347 make_cleanup_ui_out_tuple_begin_end (uiout, "item");
07e059b5
VP
348
349 for (ix_cols = 0;
350 VEC_iterate (osdata_column_s, item->columns,
351 ix_cols, col);
352 ix_cols++)
353 ui_out_field_string (uiout, col->name, col->value);
354
07e059b5
VP
355 do_cleanups (old_chain);
356
357 ui_out_text (uiout, "\n");
358 }
359 }
360
e0665bc8 361 do_cleanups (old_chain);
07e059b5
VP
362}
363
364extern initialize_file_ftype _initialize_osdata; /* -Wmissing-prototypes */
365
366void
367_initialize_osdata (void)
368{
369 add_info ("os", info_osdata_command,
370 _("Show OS data ARG."));
07e059b5 371}
This page took 0.175154 seconds and 4 git commands to generate.