1 /* Routines for handling XML memory maps provided by target.
3 Copyright (C) 2006, 2007 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 2 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, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
23 #include "memory-map.h"
24 #include "gdb_assert.h"
25 #include "exceptions.h"
27 #include "gdb_string.h"
29 #if !defined(HAVE_LIBEXPAT)
32 parse_memory_map (const char *memory_map
)
34 static int have_warned
;
39 warning (_("Can not parse XML memory map; XML support was disabled "
46 #else /* HAVE_LIBEXPAT */
48 #include "xml-support.h"
50 /* Internal parsing data passed to all XML callbacks. */
51 struct memory_map_parsing_data
53 VEC(mem_region_s
) **memory_map
;
54 char property_name
[32];
57 /* Handle the start of a <memory> element. */
60 memory_map_start_memory (struct gdb_xml_parser
*parser
,
61 const struct gdb_xml_element
*element
,
62 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
64 struct memory_map_parsing_data
*data
= user_data
;
65 struct mem_region
*r
= VEC_safe_push (mem_region_s
, *data
->memory_map
, NULL
);
66 ULONGEST
*start_p
, *length_p
, *type_p
;
68 start_p
= VEC_index (gdb_xml_value_s
, attributes
, 0)->value
;
69 length_p
= VEC_index (gdb_xml_value_s
, attributes
, 1)->value
;
70 type_p
= VEC_index (gdb_xml_value_s
, attributes
, 2)->value
;
74 r
->hi
= r
->lo
+ *length_p
;
75 r
->attrib
.mode
= *type_p
;
76 r
->attrib
.blocksize
= -1;
79 /* Handle the end of a <memory> element. Verify that any necessary
80 children were present. */
83 memory_map_end_memory (struct gdb_xml_parser
*parser
,
84 const struct gdb_xml_element
*element
,
85 void *user_data
, const char *body_text
)
87 struct memory_map_parsing_data
*data
= user_data
;
88 struct mem_region
*r
= VEC_last (mem_region_s
, *data
->memory_map
);
90 if (r
->attrib
.mode
== MEM_FLASH
&& r
->attrib
.blocksize
== -1)
91 gdb_xml_error (parser
, _("Flash block size is not set"));
94 /* Handle the start of a <property> element by saving the name
95 attribute for later. */
98 memory_map_start_property (struct gdb_xml_parser
*parser
,
99 const struct gdb_xml_element
*element
,
100 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
102 struct memory_map_parsing_data
*data
= user_data
;
105 name
= VEC_index (gdb_xml_value_s
, attributes
, 0)->value
;
106 snprintf (data
->property_name
, sizeof (data
->property_name
), "%s", name
);
109 /* Handle the end of a <property> element and its value. */
112 memory_map_end_property (struct gdb_xml_parser
*parser
,
113 const struct gdb_xml_element
*element
,
114 void *user_data
, const char *body_text
)
116 struct memory_map_parsing_data
*data
= user_data
;
117 char *name
= data
->property_name
;
119 if (strcmp (name
, "blocksize") == 0)
121 struct mem_region
*r
= VEC_last (mem_region_s
, *data
->memory_map
);
123 r
->attrib
.blocksize
= gdb_xml_parse_ulongest (parser
, body_text
);
126 gdb_xml_debug (parser
, _("Unknown property \"%s\""), name
);
129 /* Discard the constructed memory map (if an error occurs). */
132 clear_result (void *p
)
134 VEC(mem_region_s
) **result
= p
;
135 VEC_free (mem_region_s
, *result
);
139 /* The allowed elements and attributes for an XML memory map. */
141 const struct gdb_xml_attribute property_attributes
[] = {
142 { "name", GDB_XML_AF_NONE
, NULL
, NULL
},
143 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
146 const struct gdb_xml_element memory_children
[] = {
147 { "property", property_attributes
, NULL
,
148 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
,
149 memory_map_start_property
, memory_map_end_property
},
150 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
153 const struct gdb_xml_enum memory_type_enum
[] = {
156 { "flash", MEM_FLASH
},
160 const struct gdb_xml_attribute memory_attributes
[] = {
161 { "start", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
162 { "length", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
163 { "type", GDB_XML_AF_NONE
, gdb_xml_parse_attr_enum
, &memory_type_enum
},
164 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
167 const struct gdb_xml_element memory_map_children
[] = {
168 { "memory", memory_attributes
, memory_children
, GDB_XML_EF_REPEATABLE
,
169 memory_map_start_memory
, memory_map_end_memory
},
170 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
173 const struct gdb_xml_element memory_map_elements
[] = {
174 { "memory-map", NULL
, memory_map_children
, GDB_XML_EF_NONE
,
176 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
180 parse_memory_map (const char *memory_map
)
182 struct gdb_xml_parser
*parser
;
183 VEC(mem_region_s
) *result
= NULL
;
184 struct cleanup
*before_deleting_result
, *back_to
;
185 struct memory_map_parsing_data data
= {};
187 back_to
= make_cleanup (null_cleanup
, NULL
);
188 parser
= gdb_xml_create_parser_and_cleanup (_("target memory map"),
189 memory_map_elements
, &data
);
191 /* Note: 'clear_result' will zero 'result'. */
192 before_deleting_result
= make_cleanup (clear_result
, &result
);
193 data
.memory_map
= &result
;
195 if (gdb_xml_parse (parser
, memory_map
) == 0)
196 /* Parsed successfully, don't need to delete the result. */
197 discard_cleanups (before_deleting_result
);
199 do_cleanups (back_to
);
203 #endif /* HAVE_LIBEXPAT */