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