Copyright updates for 2007.
[external/binutils.git] / gdb / memory-map.c
1 /* Routines for handling XML memory maps provided by target.
2
3    Copyright (C) 2006, 2007 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 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"
49
50 /* Internal parsing data passed to all XML callbacks.  */
51 struct memory_map_parsing_data
52   {
53     VEC(mem_region_s) **memory_map;
54     char property_name[32];
55   };
56
57 /* Handle the start of a <memory> element.  */
58
59 static void
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)
63 {
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;
77 }
78
79 /* Handle the end of a <memory> element.  Verify that any necessary
80    children were present.  */
81
82 static void
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)
86 {
87   struct memory_map_parsing_data *data = user_data;
88   struct mem_region *r = VEC_last (mem_region_s, *data->memory_map);
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
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)
101 {
102   struct memory_map_parsing_data *data = user_data;
103   char *name;
104
105   name = VEC_index (gdb_xml_value_s, attributes, 0)->value;
106   snprintf (data->property_name, sizeof (data->property_name), "%s", name);
107 }
108
109 /* Handle the end of a <property> element and its value.  */
110
111 static void
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)
115 {
116   struct memory_map_parsing_data *data = user_data;
117   char *name = data->property_name;
118
119   if (strcmp (name, "blocksize") == 0)
120     {
121       struct mem_region *r = VEC_last (mem_region_s, *data->memory_map);
122
123       r->attrib.blocksize = gdb_xml_parse_ulongest (parser, body_text);
124     }
125   else
126     gdb_xml_debug (parser, _("Unknown property \"%s\""), name);
127 }
128
129 /* Discard the constructed memory map (if an error occurs).  */
130
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
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
179 VEC(mem_region_s) *
180 parse_memory_map (const char *memory_map)
181 {
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 = {};
186
187   back_to = make_cleanup (null_cleanup, NULL);
188   parser = gdb_xml_create_parser_and_cleanup (_("target memory map"),
189                                               memory_map_elements, &data);
190
191   /* Note: 'clear_result' will zero 'result'.  */
192   before_deleting_result = make_cleanup (clear_result, &result);
193   data.memory_map = &result;
194
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);
198
199   do_cleanups (back_to);
200   return result;
201 }
202
203 #endif /* HAVE_LIBEXPAT */