Commit | Line | Data |
---|---|---|
fd79ecee DJ |
1 | /* Routines for handling XML memory maps provided by target. |
2 | ||
6aba47ca | 3 | Copyright (C) 2006, 2007 Free Software Foundation, Inc. |
fd79ecee DJ |
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 2 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, write to the Free Software | |
19 | Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
20 | Boston, MA 02110-1301, USA. */ | |
21 | ||
22 | #include "defs.h" | |
23 | #include "memory-map.h" | |
24 | #include "gdb_assert.h" | |
25 | #include "exceptions.h" | |
26 | ||
27 | #include "gdb_string.h" | |
28 | ||
29 | #if !defined(HAVE_LIBEXPAT) | |
30 | ||
31 | VEC(mem_region_s) * | |
32 | parse_memory_map (const char *memory_map) | |
33 | { | |
34 | static int have_warned; | |
35 | ||
36 | if (!have_warned) | |
37 | { | |
38 | have_warned = 1; | |
39 | warning (_("Can not parse XML memory map; 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" | |
fd79ecee | 49 | |
e776119f | 50 | /* Internal parsing data passed to all XML callbacks. */ |
fd79ecee DJ |
51 | struct memory_map_parsing_data |
52 | { | |
53 | VEC(mem_region_s) **memory_map; | |
e776119f | 54 | char property_name[32]; |
fd79ecee DJ |
55 | }; |
56 | ||
e776119f DJ |
57 | /* Handle the start of a <memory> element. */ |
58 | ||
fd79ecee | 59 | static void |
e776119f DJ |
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) | |
fd79ecee | 63 | { |
e776119f DJ |
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; | |
67 | ||
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; | |
71 | ||
72 | mem_region_init (r); | |
73 | r->lo = *start_p; | |
74 | r->hi = r->lo + *length_p; | |
75 | r->attrib.mode = *type_p; | |
76 | r->attrib.blocksize = -1; | |
fd79ecee DJ |
77 | } |
78 | ||
e776119f DJ |
79 | /* Handle the end of a <memory> element. Verify that any necessary |
80 | children were present. */ | |
fd79ecee DJ |
81 | |
82 | static void | |
e776119f DJ |
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) | |
fd79ecee | 86 | { |
e776119f DJ |
87 | struct memory_map_parsing_data *data = user_data; |
88 | struct mem_region *r = VEC_last (mem_region_s, *data->memory_map); | |
fd79ecee | 89 | |
e776119f DJ |
90 | if (r->attrib.mode == MEM_FLASH && r->attrib.blocksize == -1) |
91 | gdb_xml_error (parser, _("Flash block size is not set")); | |
fd79ecee DJ |
92 | } |
93 | ||
e776119f DJ |
94 | /* Handle the start of a <property> element by saving the name |
95 | attribute for later. */ | |
fd79ecee DJ |
96 | |
97 | static void | |
e776119f DJ |
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) | |
fd79ecee | 101 | { |
e776119f DJ |
102 | struct memory_map_parsing_data *data = user_data; |
103 | char *name; | |
fd79ecee | 104 | |
e776119f DJ |
105 | name = VEC_index (gdb_xml_value_s, attributes, 0)->value; |
106 | snprintf (data->property_name, sizeof (data->property_name), "%s", name); | |
fd79ecee DJ |
107 | } |
108 | ||
e776119f | 109 | /* Handle the end of a <property> element and its value. */ |
fd79ecee | 110 | |
fd79ecee | 111 | static void |
e776119f DJ |
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) | |
fd79ecee | 115 | { |
e776119f DJ |
116 | struct memory_map_parsing_data *data = user_data; |
117 | char *name = data->property_name; | |
fd79ecee | 118 | |
e776119f | 119 | if (strcmp (name, "blocksize") == 0) |
fd79ecee | 120 | { |
e776119f | 121 | struct mem_region *r = VEC_last (mem_region_s, *data->memory_map); |
fd79ecee | 122 | |
e776119f DJ |
123 | r->attrib.blocksize = gdb_xml_parse_ulongest (parser, body_text); |
124 | } | |
125 | else | |
126 | gdb_xml_debug (parser, _("Unknown property \"%s\""), name); | |
fd79ecee DJ |
127 | } |
128 | ||
e776119f DJ |
129 | /* Discard the constructed memory map (if an error occurs). */ |
130 | ||
fd79ecee DJ |
131 | static void |
132 | clear_result (void *p) | |
133 | { | |
134 | VEC(mem_region_s) **result = p; | |
135 | VEC_free (mem_region_s, *result); | |
136 | *result = NULL; | |
137 | } | |
138 | ||
e776119f DJ |
139 | /* The allowed elements and attributes for an XML memory map. */ |
140 | ||
141 | const struct gdb_xml_attribute property_attributes[] = { | |
142 | { "name", GDB_XML_AF_NONE, NULL, NULL }, | |
143 | { NULL, GDB_XML_AF_NONE, NULL, NULL } | |
144 | }; | |
145 | ||
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 } | |
151 | }; | |
152 | ||
153 | const struct gdb_xml_enum memory_type_enum[] = { | |
154 | { "ram", MEM_RW }, | |
155 | { "rom", MEM_RO }, | |
156 | { "flash", MEM_FLASH }, | |
157 | { NULL, 0 } | |
158 | }; | |
159 | ||
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 } | |
165 | }; | |
166 | ||
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 } | |
171 | }; | |
172 | ||
173 | const struct gdb_xml_element memory_map_elements[] = { | |
174 | { "memory-map", NULL, memory_map_children, GDB_XML_EF_NONE, | |
175 | NULL, NULL }, | |
176 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } | |
177 | }; | |
178 | ||
fd79ecee DJ |
179 | VEC(mem_region_s) * |
180 | parse_memory_map (const char *memory_map) | |
181 | { | |
e776119f | 182 | struct gdb_xml_parser *parser; |
fd79ecee | 183 | VEC(mem_region_s) *result = NULL; |
e776119f | 184 | struct cleanup *before_deleting_result, *back_to; |
fd79ecee DJ |
185 | struct memory_map_parsing_data data = {}; |
186 | ||
e776119f DJ |
187 | back_to = make_cleanup (null_cleanup, NULL); |
188 | parser = gdb_xml_create_parser_and_cleanup (_("target memory map"), | |
189 | memory_map_elements, &data); | |
fd79ecee | 190 | |
fd79ecee DJ |
191 | /* Note: 'clear_result' will zero 'result'. */ |
192 | before_deleting_result = make_cleanup (clear_result, &result); | |
fd79ecee DJ |
193 | data.memory_map = &result; |
194 | ||
e776119f | 195 | if (gdb_xml_parse (parser, memory_map) == 0) |
fd79ecee DJ |
196 | /* Parsed successfully, don't need to delete the result. */ |
197 | discard_cleanups (before_deleting_result); | |
198 | ||
fd79ecee DJ |
199 | do_cleanups (back_to); |
200 | return result; | |
201 | } | |
202 | ||
203 | #endif /* HAVE_LIBEXPAT */ |