ALPHA: Migrate from 'regset_from_core_section' to 'iterate_over_regset_sections'
[deliverable/binutils-gdb.git] / gdb / memory-map.c
CommitLineData
fd79ecee
DJ
1/* Routines for handling XML memory maps provided by target.
2
ecd75fc8 3 Copyright (C) 2006-2014 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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
fd79ecee
DJ
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
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
fd79ecee
DJ
19
20#include "defs.h"
21#include "memory-map.h"
fd79ecee
DJ
22#include "exceptions.h"
23
fd79ecee
DJ
24#if !defined(HAVE_LIBEXPAT)
25
26VEC(mem_region_s) *
27parse_memory_map (const char *memory_map)
28{
29 static int have_warned;
30
31 if (!have_warned)
32 {
33 have_warned = 1;
34 warning (_("Can not parse XML memory map; XML support was disabled "
35 "at compile time"));
36 }
37
38 return NULL;
39}
40
41#else /* HAVE_LIBEXPAT */
42
43#include "xml-support.h"
fd79ecee 44
e776119f 45/* Internal parsing data passed to all XML callbacks. */
fd79ecee
DJ
46struct memory_map_parsing_data
47 {
48 VEC(mem_region_s) **memory_map;
e776119f 49 char property_name[32];
fd79ecee
DJ
50 };
51
e776119f
DJ
52/* Handle the start of a <memory> element. */
53
fd79ecee 54static void
e776119f
DJ
55memory_map_start_memory (struct gdb_xml_parser *parser,
56 const struct gdb_xml_element *element,
57 void *user_data, VEC(gdb_xml_value_s) *attributes)
fd79ecee 58{
e776119f
DJ
59 struct memory_map_parsing_data *data = user_data;
60 struct mem_region *r = VEC_safe_push (mem_region_s, *data->memory_map, NULL);
61 ULONGEST *start_p, *length_p, *type_p;
62
3d2c1d41
PA
63 start_p = xml_find_attribute (attributes, "start")->value;
64 length_p = xml_find_attribute (attributes, "length")->value;
65 type_p = xml_find_attribute (attributes, "type")->value;
e776119f
DJ
66
67 mem_region_init (r);
68 r->lo = *start_p;
69 r->hi = r->lo + *length_p;
70 r->attrib.mode = *type_p;
71 r->attrib.blocksize = -1;
fd79ecee
DJ
72}
73
e776119f
DJ
74/* Handle the end of a <memory> element. Verify that any necessary
75 children were present. */
fd79ecee
DJ
76
77static void
e776119f
DJ
78memory_map_end_memory (struct gdb_xml_parser *parser,
79 const struct gdb_xml_element *element,
80 void *user_data, const char *body_text)
fd79ecee 81{
e776119f
DJ
82 struct memory_map_parsing_data *data = user_data;
83 struct mem_region *r = VEC_last (mem_region_s, *data->memory_map);
fd79ecee 84
e776119f
DJ
85 if (r->attrib.mode == MEM_FLASH && r->attrib.blocksize == -1)
86 gdb_xml_error (parser, _("Flash block size is not set"));
fd79ecee
DJ
87}
88
e776119f
DJ
89/* Handle the start of a <property> element by saving the name
90 attribute for later. */
fd79ecee
DJ
91
92static void
e776119f
DJ
93memory_map_start_property (struct gdb_xml_parser *parser,
94 const struct gdb_xml_element *element,
95 void *user_data, VEC(gdb_xml_value_s) *attributes)
fd79ecee 96{
e776119f
DJ
97 struct memory_map_parsing_data *data = user_data;
98 char *name;
fd79ecee 99
3d2c1d41 100 name = xml_find_attribute (attributes, "name")->value;
e776119f 101 snprintf (data->property_name, sizeof (data->property_name), "%s", name);
fd79ecee
DJ
102}
103
e776119f 104/* Handle the end of a <property> element and its value. */
fd79ecee 105
fd79ecee 106static void
e776119f
DJ
107memory_map_end_property (struct gdb_xml_parser *parser,
108 const struct gdb_xml_element *element,
109 void *user_data, const char *body_text)
fd79ecee 110{
e776119f
DJ
111 struct memory_map_parsing_data *data = user_data;
112 char *name = data->property_name;
fd79ecee 113
e776119f 114 if (strcmp (name, "blocksize") == 0)
fd79ecee 115 {
e776119f 116 struct mem_region *r = VEC_last (mem_region_s, *data->memory_map);
fd79ecee 117
e776119f
DJ
118 r->attrib.blocksize = gdb_xml_parse_ulongest (parser, body_text);
119 }
120 else
121 gdb_xml_debug (parser, _("Unknown property \"%s\""), name);
fd79ecee
DJ
122}
123
e776119f
DJ
124/* Discard the constructed memory map (if an error occurs). */
125
fd79ecee
DJ
126static void
127clear_result (void *p)
128{
129 VEC(mem_region_s) **result = p;
130 VEC_free (mem_region_s, *result);
131 *result = NULL;
132}
133
e776119f
DJ
134/* The allowed elements and attributes for an XML memory map. */
135
136const struct gdb_xml_attribute property_attributes[] = {
137 { "name", GDB_XML_AF_NONE, NULL, NULL },
138 { NULL, GDB_XML_AF_NONE, NULL, NULL }
139};
140
141const struct gdb_xml_element memory_children[] = {
142 { "property", property_attributes, NULL,
143 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
144 memory_map_start_property, memory_map_end_property },
145 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
146};
147
148const struct gdb_xml_enum memory_type_enum[] = {
149 { "ram", MEM_RW },
150 { "rom", MEM_RO },
151 { "flash", MEM_FLASH },
152 { NULL, 0 }
153};
154
155const struct gdb_xml_attribute memory_attributes[] = {
156 { "start", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
157 { "length", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
158 { "type", GDB_XML_AF_NONE, gdb_xml_parse_attr_enum, &memory_type_enum },
159 { NULL, GDB_XML_AF_NONE, NULL, NULL }
160};
161
162const struct gdb_xml_element memory_map_children[] = {
163 { "memory", memory_attributes, memory_children, GDB_XML_EF_REPEATABLE,
164 memory_map_start_memory, memory_map_end_memory },
165 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
166};
167
168const struct gdb_xml_element memory_map_elements[] = {
169 { "memory-map", NULL, memory_map_children, GDB_XML_EF_NONE,
170 NULL, NULL },
171 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
172};
173
fd79ecee
DJ
174VEC(mem_region_s) *
175parse_memory_map (const char *memory_map)
176{
177 VEC(mem_region_s) *result = NULL;
efc0eabd 178 struct cleanup *back_to;
f6071bfa 179 struct memory_map_parsing_data data = { NULL };
fd79ecee 180
fd79ecee 181 data.memory_map = &result;
efc0eabd
PA
182 back_to = make_cleanup (clear_result, &result);
183 if (gdb_xml_parse_quick (_("target memory map"), NULL, memory_map_elements,
184 memory_map, &data) == 0)
185 {
186 /* Parsed successfully, keep the result. */
187 discard_cleanups (back_to);
188 return result;
189 }
fd79ecee 190
fd79ecee 191 do_cleanups (back_to);
efc0eabd 192 return NULL;
fd79ecee
DJ
193}
194
195#endif /* HAVE_LIBEXPAT */
This page took 1.163589 seconds and 4 git commands to generate.