1 /* Routines for handling XML memory maps provided by target.
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
24 #include "memory-map.h"
25 #include "gdb_assert.h"
26 #include "exceptions.h"
28 #include "gdb_string.h"
30 #if !defined(HAVE_LIBEXPAT)
33 parse_memory_map (const char *memory_map
)
35 static int have_warned
;
40 warning (_("Can not parse XML memory map; XML support was disabled "
47 #else /* HAVE_LIBEXPAT */
49 #include "xml-support.h"
51 /* Internal parsing data passed to all XML callbacks. */
52 struct memory_map_parsing_data
54 VEC(mem_region_s
) **memory_map
;
55 char property_name
[32];
58 /* Handle the start of a <memory> element. */
61 memory_map_start_memory (struct gdb_xml_parser
*parser
,
62 const struct gdb_xml_element
*element
,
63 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
65 struct memory_map_parsing_data
*data
= user_data
;
66 struct mem_region
*r
= VEC_safe_push (mem_region_s
, *data
->memory_map
, NULL
);
67 ULONGEST
*start_p
, *length_p
, *type_p
;
69 start_p
= VEC_index (gdb_xml_value_s
, attributes
, 0)->value
;
70 length_p
= VEC_index (gdb_xml_value_s
, attributes
, 1)->value
;
71 type_p
= VEC_index (gdb_xml_value_s
, attributes
, 2)->value
;
75 r
->hi
= r
->lo
+ *length_p
;
76 r
->attrib
.mode
= *type_p
;
77 r
->attrib
.blocksize
= -1;
80 /* Handle the end of a <memory> element. Verify that any necessary
81 children were present. */
84 memory_map_end_memory (struct gdb_xml_parser
*parser
,
85 const struct gdb_xml_element
*element
,
86 void *user_data
, const char *body_text
)
88 struct memory_map_parsing_data
*data
= user_data
;
89 struct mem_region
*r
= VEC_last (mem_region_s
, *data
->memory_map
);
91 if (r
->attrib
.mode
== MEM_FLASH
&& r
->attrib
.blocksize
== -1)
92 gdb_xml_error (parser
, _("Flash block size is not set"));
95 /* Handle the start of a <property> element by saving the name
96 attribute for later. */
99 memory_map_start_property (struct gdb_xml_parser
*parser
,
100 const struct gdb_xml_element
*element
,
101 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
103 struct memory_map_parsing_data
*data
= user_data
;
106 name
= VEC_index (gdb_xml_value_s
, attributes
, 0)->value
;
107 snprintf (data
->property_name
, sizeof (data
->property_name
), "%s", name
);
110 /* Handle the end of a <property> element and its value. */
113 memory_map_end_property (struct gdb_xml_parser
*parser
,
114 const struct gdb_xml_element
*element
,
115 void *user_data
, const char *body_text
)
117 struct memory_map_parsing_data
*data
= user_data
;
118 char *name
= data
->property_name
;
120 if (strcmp (name
, "blocksize") == 0)
122 struct mem_region
*r
= VEC_last (mem_region_s
, *data
->memory_map
);
124 r
->attrib
.blocksize
= gdb_xml_parse_ulongest (parser
, body_text
);
127 gdb_xml_debug (parser
, _("Unknown property \"%s\""), name
);
130 /* Discard the constructed memory map (if an error occurs). */
133 clear_result (void *p
)
135 VEC(mem_region_s
) **result
= p
;
136 VEC_free (mem_region_s
, *result
);
140 /* The allowed elements and attributes for an XML memory map. */
142 const struct gdb_xml_attribute property_attributes
[] = {
143 { "name", GDB_XML_AF_NONE
, NULL
, NULL
},
144 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
147 const struct gdb_xml_element memory_children
[] = {
148 { "property", property_attributes
, NULL
,
149 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
,
150 memory_map_start_property
, memory_map_end_property
},
151 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
154 const struct gdb_xml_enum memory_type_enum
[] = {
157 { "flash", MEM_FLASH
},
161 const struct gdb_xml_attribute memory_attributes
[] = {
162 { "start", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
163 { "length", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
164 { "type", GDB_XML_AF_NONE
, gdb_xml_parse_attr_enum
, &memory_type_enum
},
165 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
168 const struct gdb_xml_element memory_map_children
[] = {
169 { "memory", memory_attributes
, memory_children
, GDB_XML_EF_REPEATABLE
,
170 memory_map_start_memory
, memory_map_end_memory
},
171 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
174 const struct gdb_xml_element memory_map_elements
[] = {
175 { "memory-map", NULL
, memory_map_children
, GDB_XML_EF_NONE
,
177 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
181 parse_memory_map (const char *memory_map
)
183 struct gdb_xml_parser
*parser
;
184 VEC(mem_region_s
) *result
= NULL
;
185 struct cleanup
*before_deleting_result
, *back_to
;
186 struct memory_map_parsing_data data
= {};
188 back_to
= make_cleanup (null_cleanup
, NULL
);
189 parser
= gdb_xml_create_parser_and_cleanup (_("target memory map"),
190 memory_map_elements
, &data
);
192 /* Note: 'clear_result' will zero 'result'. */
193 before_deleting_result
= make_cleanup (clear_result
, &result
);
194 data
.memory_map
= &result
;
196 if (gdb_xml_parse (parser
, memory_map
) == 0)
197 /* Parsed successfully, don't need to delete the result. */
198 discard_cleanups (before_deleting_result
);
200 do_cleanups (back_to
);
204 #endif /* HAVE_LIBEXPAT */