gdb/
[deliverable/binutils-gdb.git] / gdb / osdata.c
CommitLineData
07e059b5
VP
1/* Routines for handling XML generic OS data provided by target.
2
3 Copyright (C) 2008 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
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
57static void
58osdata_item_clear (struct osdata_item *item)
59{
60 if (item->columns != NULL)
61 {
62 struct osdata_column *col;
63 int ix;
64 for (ix = 0;
65 VEC_iterate (osdata_column_s, item->columns,
66 ix, col);
67 ix++)
68 {
69 xfree (col->name);
70 xfree (col->value);
71 }
72 VEC_free (osdata_column_s, item->columns);
73 item->columns = NULL;
74 }
75}
76
77/* Handle the start of a <osdata> element. */
78
79static void
80osdata_start_osdata (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 char *type;
86 struct osdata *osdata;
87
88 if (data->osdata)
89 gdb_xml_error (parser, _("Seen more than on osdata element"));
90
91 type = VEC_index (gdb_xml_value_s, attributes, 0)->value;
92 osdata = XZALLOC (struct osdata);
93 osdata->type = xstrdup (type);
94 data->osdata = osdata;
95}
96
97/* Handle the start of a <item> element. */
98
99static void
100osdata_start_item (struct gdb_xml_parser *parser,
101 const struct gdb_xml_element *element,
102 void *user_data, VEC(gdb_xml_value_s) *attributes)
103{
104 struct osdata_parsing_data *data = user_data;
105 struct osdata_item item = { NULL };
106 VEC_safe_push (osdata_item_s, data->osdata->items, &item);
107}
108
109/* Handle the start of a <column> element. */
110
111static void
112osdata_start_column (struct gdb_xml_parser *parser,
113 const struct gdb_xml_element *element,
114 void *user_data, VEC(gdb_xml_value_s) *attributes)
115{
116 struct osdata_parsing_data *data = user_data;
117 const char *name = VEC_index (gdb_xml_value_s, attributes, 0)->value;
118 data->property_name = xstrdup (name);
119}
120
121/* Handle the end of a <column> element. */
122
123static void
124osdata_end_column (struct gdb_xml_parser *parser,
125 const struct gdb_xml_element *element,
126 void *user_data, const char *body_text)
127{
128 struct osdata_parsing_data *data = user_data;
129 struct osdata *osdata = data->osdata;
130 struct osdata_item *item = VEC_last (osdata_item_s, osdata->items);
131 struct osdata_column *col = VEC_safe_push (osdata_column_s,
132 item->columns, NULL);
133
134 /* Transfer memory ownership. NAME was already strdup'ed. */
135 col->name = data->property_name;
136 col->value = xstrdup (body_text);
137 data->property_name = NULL;
138}
139
140/* Discard the constructed osdata (if an error occurs). */
141
142static void
143clear_parsing_data (void *p)
144{
145 struct osdata_parsing_data *data = p;
146 osdata_free (data->osdata);
147 data->osdata = NULL;
148 xfree (data->property_name);
149 data->property_name = NULL;
150}
151
152/* The allowed elements and attributes for OS data object.
153 The root element is a <osdata>. */
154
155const struct gdb_xml_attribute column_attributes[] = {
156 { "name", GDB_XML_AF_NONE, NULL, NULL },
157 { NULL, GDB_XML_AF_NONE, NULL, NULL }
158};
159
160const struct gdb_xml_element item_children[] = {
161 { "column", column_attributes, NULL,
162 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
163 osdata_start_column, osdata_end_column },
164 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
165};
166
167const struct gdb_xml_attribute osdata_attributes[] = {
168 { "type", GDB_XML_AF_NONE, NULL, NULL },
169 { NULL, GDB_XML_AF_NONE, NULL, NULL }
170};
171
172const struct gdb_xml_element osdata_children[] = {
173 { "item", NULL, item_children,
174 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
175 osdata_start_item, NULL },
176 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
177};
178
179const struct gdb_xml_element osdata_elements[] = {
180 { "osdata", osdata_attributes, osdata_children,
181 GDB_XML_EF_NONE, osdata_start_osdata, NULL },
182 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
183};
184
185struct osdata *
186osdata_parse (const char *xml)
187{
188 struct gdb_xml_parser *parser;
189 struct cleanup *before_deleting_result, *back_to;
190 struct osdata_parsing_data data = { NULL };
191
192 back_to = make_cleanup (null_cleanup, NULL);
193 parser = gdb_xml_create_parser_and_cleanup (_("osdata"),
194 osdata_elements, &data);
195 gdb_xml_use_dtd (parser, "osdata.dtd");
196
197 before_deleting_result = make_cleanup (clear_parsing_data, &data);
198
199 if (gdb_xml_parse (parser, xml) == 0)
200 /* Parsed successfully, don't need to delete the result. */
201 discard_cleanups (before_deleting_result);
202
203 do_cleanups (back_to);
204 return data.osdata;
205}
206#endif
207
208void
209osdata_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
229struct osdata *get_osdata (const char *type)
230{
231 struct osdata * osdata = NULL;
232 char *xml = target_get_osdata (type);
233 if (xml)
234 {
235 if (xml[0] == '\0')
236 warning (_("Empty data returned by target. Wrong osdata type?"));
237
238 osdata = osdata_parse (xml);
239 }
240
241 if (!osdata)
242 error (_("Can not fetch data now.\n"));
243
244 return osdata;
245}
246
247const char *
248get_osdata_column (struct osdata_item *item, const char *name)
249{
250 struct osdata_column *col;
251 int ix_cols;
252
253 for (ix_cols = 0;
254 VEC_iterate (osdata_column_s, item->columns,
255 ix_cols, col);
256 ix_cols++)
257 if (strcmp (col->name, name) == 0)
258 return col->value;
259
260 return NULL;
261}
262
263void
264info_osdata_command (char *type, int from_tty)
265{
266 struct osdata * osdata = NULL;
267 struct cleanup *proc_tbl_chain;
268 struct osdata_item *last;
269 int ncols;
270 int nprocs;
271
272 if (type == 0)
273 /* TODO: No type could mean "list availables types". */
274 error (_("Argument required."));
275
276 osdata = get_osdata (type);
277
278 nprocs = VEC_length (osdata_item_s, osdata->items);
279
280 last = VEC_last (osdata_item_s, osdata->items);
281 if (last && last->columns)
282 ncols = VEC_length (osdata_column_s, last->columns);
283 else
284 ncols = 0;
285
286 proc_tbl_chain
287 = make_cleanup_ui_out_table_begin_end (uiout, ncols, nprocs,
288 "OSDataTable");
289
290 if (last && last->columns)
291 {
292 struct osdata_column *col;
293 int ix;
294 for (ix = 0;
295 VEC_iterate (osdata_column_s, last->columns,
296 ix, col);
297 ix++)
298 ui_out_table_header (uiout, 10, ui_left,
299 col->name, col->name);
300 }
301
302 ui_out_table_body (uiout);
303
304 if (nprocs != 0)
305 {
306 struct osdata_item *item;
307 int ix_items;
308 for (ix_items = 0;
309 VEC_iterate (osdata_item_s, osdata->items,
310 ix_items, item);
311 ix_items++)
312 {
313 struct cleanup *old_chain, *chain;
314 struct ui_stream *stb;
315 int ix_cols;
316 struct osdata_column *col;
317
318 stb = ui_out_stream_new (uiout);
319 old_chain = make_cleanup_ui_out_stream_delete (stb);
320 chain = make_cleanup_ui_out_tuple_begin_end (uiout, "item");
321
322 for (ix_cols = 0;
323 VEC_iterate (osdata_column_s, item->columns,
324 ix_cols, col);
325 ix_cols++)
326 ui_out_field_string (uiout, col->name, col->value);
327
328 do_cleanups (chain);
329 do_cleanups (old_chain);
330
331 ui_out_text (uiout, "\n");
332 }
333 }
334
335 do_cleanups (proc_tbl_chain);
336
337 osdata_free (osdata);
338}
339
340static void
341info_processes_command (char *args, int from_tty)
342{
343 info_osdata_command ("processes", from_tty);
344}
345
346extern initialize_file_ftype _initialize_osdata; /* -Wmissing-prototypes */
347
348void
349_initialize_osdata (void)
350{
351 add_info ("os", info_osdata_command,
352 _("Show OS data ARG."));
353
354 /* An alias for "info osdata processes". */
355 add_info ("processes", info_processes_command,
356 _("List running processes on the target."));
357}
This page took 0.043847 seconds and 4 git commands to generate.