Update
[gdb.git] / gdb / memory-map.c
blob4bb022bfdbb2f50f935fff985b9cc3ba9eca75a2
1 /* Routines for handling XML memory maps provided by target.
3 Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #include "defs.h"
21 #include "memory-map.h"
22 #include "gdb_assert.h"
23 #include "exceptions.h"
25 #include "gdb_string.h"
27 #if !defined(HAVE_LIBEXPAT)
29 VEC(mem_region_s) *
30 parse_memory_map (const char *memory_map)
32 static int have_warned;
34 if (!have_warned)
36 have_warned = 1;
37 warning (_("Can not parse XML memory map; XML support was disabled "
38 "at compile time"));
41 return NULL;
44 #else /* HAVE_LIBEXPAT */
46 #include "xml-support.h"
48 /* Internal parsing data passed to all XML callbacks. */
49 struct memory_map_parsing_data
51 VEC(mem_region_s) **memory_map;
52 char property_name[32];
55 /* Handle the start of a <memory> element. */
57 static void
58 memory_map_start_memory (struct gdb_xml_parser *parser,
59 const struct gdb_xml_element *element,
60 void *user_data, VEC(gdb_xml_value_s) *attributes)
62 struct memory_map_parsing_data *data = user_data;
63 struct mem_region *r = VEC_safe_push (mem_region_s, *data->memory_map, NULL);
64 ULONGEST *start_p, *length_p, *type_p;
66 start_p = VEC_index (gdb_xml_value_s, attributes, 0)->value;
67 length_p = VEC_index (gdb_xml_value_s, attributes, 1)->value;
68 type_p = VEC_index (gdb_xml_value_s, attributes, 2)->value;
70 mem_region_init (r);
71 r->lo = *start_p;
72 r->hi = r->lo + *length_p;
73 r->attrib.mode = *type_p;
74 r->attrib.blocksize = -1;
77 /* Handle the end of a <memory> element. Verify that any necessary
78 children were present. */
80 static void
81 memory_map_end_memory (struct gdb_xml_parser *parser,
82 const struct gdb_xml_element *element,
83 void *user_data, const char *body_text)
85 struct memory_map_parsing_data *data = user_data;
86 struct mem_region *r = VEC_last (mem_region_s, *data->memory_map);
88 if (r->attrib.mode == MEM_FLASH && r->attrib.blocksize == -1)
89 gdb_xml_error (parser, _("Flash block size is not set"));
92 /* Handle the start of a <property> element by saving the name
93 attribute for later. */
95 static void
96 memory_map_start_property (struct gdb_xml_parser *parser,
97 const struct gdb_xml_element *element,
98 void *user_data, VEC(gdb_xml_value_s) *attributes)
100 struct memory_map_parsing_data *data = user_data;
101 char *name;
103 name = VEC_index (gdb_xml_value_s, attributes, 0)->value;
104 snprintf (data->property_name, sizeof (data->property_name), "%s", name);
107 /* Handle the end of a <property> element and its value. */
109 static void
110 memory_map_end_property (struct gdb_xml_parser *parser,
111 const struct gdb_xml_element *element,
112 void *user_data, const char *body_text)
114 struct memory_map_parsing_data *data = user_data;
115 char *name = data->property_name;
117 if (strcmp (name, "blocksize") == 0)
119 struct mem_region *r = VEC_last (mem_region_s, *data->memory_map);
121 r->attrib.blocksize = gdb_xml_parse_ulongest (parser, body_text);
123 else
124 gdb_xml_debug (parser, _("Unknown property \"%s\""), name);
127 /* Discard the constructed memory map (if an error occurs). */
129 static void
130 clear_result (void *p)
132 VEC(mem_region_s) **result = p;
133 VEC_free (mem_region_s, *result);
134 *result = NULL;
137 /* The allowed elements and attributes for an XML memory map. */
139 const struct gdb_xml_attribute property_attributes[] = {
140 { "name", GDB_XML_AF_NONE, NULL, NULL },
141 { NULL, GDB_XML_AF_NONE, NULL, NULL }
144 const struct gdb_xml_element memory_children[] = {
145 { "property", property_attributes, NULL,
146 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
147 memory_map_start_property, memory_map_end_property },
148 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
151 const struct gdb_xml_enum memory_type_enum[] = {
152 { "ram", MEM_RW },
153 { "rom", MEM_RO },
154 { "flash", MEM_FLASH },
155 { NULL, 0 }
158 const struct gdb_xml_attribute memory_attributes[] = {
159 { "start", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
160 { "length", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
161 { "type", GDB_XML_AF_NONE, gdb_xml_parse_attr_enum, &memory_type_enum },
162 { NULL, GDB_XML_AF_NONE, NULL, NULL }
165 const struct gdb_xml_element memory_map_children[] = {
166 { "memory", memory_attributes, memory_children, GDB_XML_EF_REPEATABLE,
167 memory_map_start_memory, memory_map_end_memory },
168 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
171 const struct gdb_xml_element memory_map_elements[] = {
172 { "memory-map", NULL, memory_map_children, GDB_XML_EF_NONE,
173 NULL, NULL },
174 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
177 VEC(mem_region_s) *
178 parse_memory_map (const char *memory_map)
180 struct gdb_xml_parser *parser;
181 VEC(mem_region_s) *result = NULL;
182 struct cleanup *before_deleting_result, *back_to;
183 struct memory_map_parsing_data data = { NULL };
185 back_to = make_cleanup (null_cleanup, NULL);
186 parser = gdb_xml_create_parser_and_cleanup (_("target memory map"),
187 memory_map_elements, &data);
189 /* Note: 'clear_result' will zero 'result'. */
190 before_deleting_result = make_cleanup (clear_result, &result);
191 data.memory_map = &result;
193 if (gdb_xml_parse (parser, memory_map) == 0)
194 /* Parsed successfully, don't need to delete the result. */
195 discard_cleanups (before_deleting_result);
197 do_cleanups (back_to);
198 return result;
201 #endif /* HAVE_LIBEXPAT */