1 /* Routines for handling XML memory maps provided by target.
2
3 Copyright (C) 2006-2024 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 "memory-map.h"
21
22 #if !defined(HAVE_LIBEXPAT)
23
24 std::vector<mem_region>
parse_memory_map(const char * memory_map)25 parse_memory_map (const char *memory_map)
26 {
27 static int have_warned;
28
29 if (!have_warned)
30 {
31 have_warned = 1;
32 warning (_("Can not parse XML memory map; XML support was disabled "
33 "at compile time"));
34 }
35
36 return std::vector<mem_region> ();
37 }
38
39 #else /* HAVE_LIBEXPAT */
40
41 #include "xml-support.h"
42
43 /* Internal parsing data passed to all XML callbacks. */
44 struct memory_map_parsing_data
45 {
memory_map_parsing_datamemory_map_parsing_data46 memory_map_parsing_data (std::vector<mem_region> *memory_map_)
47 : memory_map (memory_map_)
48 {}
49
50 std::vector<mem_region> *memory_map;
51
52 std::string property_name;
53 };
54
55 /* Handle the start of a <memory> element. */
56
57 static void
memory_map_start_memory(struct gdb_xml_parser * parser,const struct gdb_xml_element * element,void * user_data,std::vector<gdb_xml_value> & attributes)58 memory_map_start_memory (struct gdb_xml_parser *parser,
59 const struct gdb_xml_element *element,
60 void *user_data,
61 std::vector<gdb_xml_value> &attributes)
62 {
63 struct memory_map_parsing_data *data
64 = (struct memory_map_parsing_data *) user_data;
65 ULONGEST *start_p, *length_p, *type_p;
66
67 start_p
68 = (ULONGEST *) xml_find_attribute (attributes, "start")->value.get ();
69 length_p
70 = (ULONGEST *) xml_find_attribute (attributes, "length")->value.get ();
71 type_p
72 = (ULONGEST *) xml_find_attribute (attributes, "type")->value.get ();
73
74 data->memory_map->emplace_back (*start_p, *start_p + *length_p,
75 (enum mem_access_mode) *type_p);
76 }
77
78 /* Handle the end of a <memory> element. Verify that any necessary
79 children were present. */
80
81 static void
memory_map_end_memory(struct gdb_xml_parser * parser,const struct gdb_xml_element * element,void * user_data,const char * body_text)82 memory_map_end_memory (struct gdb_xml_parser *parser,
83 const struct gdb_xml_element *element,
84 void *user_data, const char *body_text)
85 {
86 struct memory_map_parsing_data *data
87 = (struct memory_map_parsing_data *) user_data;
88 const mem_region &r = data->memory_map->back ();
89
90 if (r.attrib.mode == MEM_FLASH && r.attrib.blocksize == -1)
91 gdb_xml_error (parser, _("Flash block size is not set"));
92 }
93
94 /* Handle the start of a <property> element by saving the name
95 attribute for later. */
96
97 static void
memory_map_start_property(struct gdb_xml_parser * parser,const struct gdb_xml_element * element,void * user_data,std::vector<gdb_xml_value> & attributes)98 memory_map_start_property (struct gdb_xml_parser *parser,
99 const struct gdb_xml_element *element,
100 void *user_data,
101 std::vector<gdb_xml_value> &attributes)
102 {
103 struct memory_map_parsing_data *data
104 = (struct memory_map_parsing_data *) user_data;
105 char *name;
106
107 name = (char *) xml_find_attribute (attributes, "name")->value.get ();
108 data->property_name.assign (name);
109 }
110
111 /* Handle the end of a <property> element and its value. */
112
113 static void
memory_map_end_property(struct gdb_xml_parser * parser,const struct gdb_xml_element * element,void * user_data,const char * body_text)114 memory_map_end_property (struct gdb_xml_parser *parser,
115 const struct gdb_xml_element *element,
116 void *user_data, const char *body_text)
117 {
118 struct memory_map_parsing_data *data
119 = (struct memory_map_parsing_data *) user_data;
120
121 if (data->property_name == "blocksize")
122 {
123 mem_region &r = data->memory_map->back ();
124
125 r.attrib.blocksize = gdb_xml_parse_ulongest (parser, body_text);
126 }
127 else
128 gdb_xml_debug (parser, _("Unknown property \"%s\""),
129 data->property_name.c_str ());
130 }
131
132 /* The allowed elements and attributes for an XML memory map. */
133
134 const struct gdb_xml_attribute property_attributes[] = {
135 { "name", GDB_XML_AF_NONE, NULL, NULL },
136 { NULL, GDB_XML_AF_NONE, NULL, NULL }
137 };
138
139 const struct gdb_xml_element memory_children[] = {
140 { "property", property_attributes, NULL,
141 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
142 memory_map_start_property, memory_map_end_property },
143 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
144 };
145
146 const struct gdb_xml_enum memory_type_enum[] = {
147 { "ram", MEM_RW },
148 { "rom", MEM_RO },
149 { "flash", MEM_FLASH },
150 { NULL, 0 }
151 };
152
153 const struct gdb_xml_attribute memory_attributes[] = {
154 { "start", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
155 { "length", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
156 { "type", GDB_XML_AF_NONE, gdb_xml_parse_attr_enum, &memory_type_enum },
157 { NULL, GDB_XML_AF_NONE, NULL, NULL }
158 };
159
160 const struct gdb_xml_element memory_map_children[] = {
161 { "memory", memory_attributes, memory_children, GDB_XML_EF_REPEATABLE,
162 memory_map_start_memory, memory_map_end_memory },
163 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
164 };
165
166 const struct gdb_xml_element memory_map_elements[] = {
167 { "memory-map", NULL, memory_map_children, GDB_XML_EF_NONE,
168 NULL, NULL },
169 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
170 };
171
172 std::vector<mem_region>
parse_memory_map(const char * memory_map)173 parse_memory_map (const char *memory_map)
174 {
175 std::vector<mem_region> ret;
176 memory_map_parsing_data data (&ret);
177
178 if (gdb_xml_parse_quick (_("target memory map"), NULL, memory_map_elements,
179 memory_map, &data) == 0)
180 {
181 /* Parsed successfully, keep the result. */
182 return ret;
183 }
184
185 return std::vector<mem_region> ();
186 }
187
188 #endif /* HAVE_LIBEXPAT */
189