xref: /dflybsd-src/contrib/gdb-7/gdb/memory-map.c (revision de8e141f24382815c10a4012d209bbbf7abf1112)
15796c8dcSSimon Schubert /* Routines for handling XML memory maps provided by target.
25796c8dcSSimon Schubert 
3*ef5ccd6cSJohn Marino    Copyright (C) 2006-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    This file is part of GDB.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert    (at your option) any later version.
115796c8dcSSimon Schubert 
125796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155796c8dcSSimon Schubert    GNU General Public License for more details.
165796c8dcSSimon Schubert 
175796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert #include "memory-map.h"
225796c8dcSSimon Schubert #include "gdb_assert.h"
235796c8dcSSimon Schubert #include "exceptions.h"
245796c8dcSSimon Schubert 
255796c8dcSSimon Schubert #include "gdb_string.h"
265796c8dcSSimon Schubert 
275796c8dcSSimon Schubert #if !defined(HAVE_LIBEXPAT)
285796c8dcSSimon Schubert 
VEC(mem_region_s)295796c8dcSSimon Schubert VEC(mem_region_s) *
305796c8dcSSimon Schubert parse_memory_map (const char *memory_map)
315796c8dcSSimon Schubert {
325796c8dcSSimon Schubert   static int have_warned;
335796c8dcSSimon Schubert 
345796c8dcSSimon Schubert   if (!have_warned)
355796c8dcSSimon Schubert     {
365796c8dcSSimon Schubert       have_warned = 1;
375796c8dcSSimon Schubert       warning (_("Can not parse XML memory map; XML support was disabled "
385796c8dcSSimon Schubert 		 "at compile time"));
395796c8dcSSimon Schubert     }
405796c8dcSSimon Schubert 
415796c8dcSSimon Schubert   return NULL;
425796c8dcSSimon Schubert }
435796c8dcSSimon Schubert 
445796c8dcSSimon Schubert #else /* HAVE_LIBEXPAT */
455796c8dcSSimon Schubert 
465796c8dcSSimon Schubert #include "xml-support.h"
475796c8dcSSimon Schubert 
485796c8dcSSimon Schubert /* Internal parsing data passed to all XML callbacks.  */
495796c8dcSSimon Schubert struct memory_map_parsing_data
505796c8dcSSimon Schubert   {
515796c8dcSSimon Schubert     VEC(mem_region_s) **memory_map;
525796c8dcSSimon Schubert     char property_name[32];
535796c8dcSSimon Schubert   };
545796c8dcSSimon Schubert 
555796c8dcSSimon Schubert /* Handle the start of a <memory> element.  */
565796c8dcSSimon Schubert 
575796c8dcSSimon Schubert static void
memory_map_start_memory(struct gdb_xml_parser * parser,const struct gdb_xml_element * element,void * user_data,VEC (gdb_xml_value_s)* attributes)585796c8dcSSimon Schubert memory_map_start_memory (struct gdb_xml_parser *parser,
595796c8dcSSimon Schubert 			 const struct gdb_xml_element *element,
605796c8dcSSimon Schubert 			 void *user_data, VEC(gdb_xml_value_s) *attributes)
615796c8dcSSimon Schubert {
625796c8dcSSimon Schubert   struct memory_map_parsing_data *data = user_data;
635796c8dcSSimon Schubert   struct mem_region *r = VEC_safe_push (mem_region_s, *data->memory_map, NULL);
645796c8dcSSimon Schubert   ULONGEST *start_p, *length_p, *type_p;
655796c8dcSSimon Schubert 
66c50c785cSJohn Marino   start_p = xml_find_attribute (attributes, "start")->value;
67c50c785cSJohn Marino   length_p = xml_find_attribute (attributes, "length")->value;
68c50c785cSJohn Marino   type_p = xml_find_attribute (attributes, "type")->value;
695796c8dcSSimon Schubert 
705796c8dcSSimon Schubert   mem_region_init (r);
715796c8dcSSimon Schubert   r->lo = *start_p;
725796c8dcSSimon Schubert   r->hi = r->lo + *length_p;
735796c8dcSSimon Schubert   r->attrib.mode = *type_p;
745796c8dcSSimon Schubert   r->attrib.blocksize = -1;
755796c8dcSSimon Schubert }
765796c8dcSSimon Schubert 
775796c8dcSSimon Schubert /* Handle the end of a <memory> element.  Verify that any necessary
785796c8dcSSimon Schubert    children were present.  */
795796c8dcSSimon Schubert 
805796c8dcSSimon Schubert static void
memory_map_end_memory(struct gdb_xml_parser * parser,const struct gdb_xml_element * element,void * user_data,const char * body_text)815796c8dcSSimon Schubert memory_map_end_memory (struct gdb_xml_parser *parser,
825796c8dcSSimon Schubert 		       const struct gdb_xml_element *element,
835796c8dcSSimon Schubert 		       void *user_data, const char *body_text)
845796c8dcSSimon Schubert {
855796c8dcSSimon Schubert   struct memory_map_parsing_data *data = user_data;
865796c8dcSSimon Schubert   struct mem_region *r = VEC_last (mem_region_s, *data->memory_map);
875796c8dcSSimon Schubert 
885796c8dcSSimon Schubert   if (r->attrib.mode == MEM_FLASH && r->attrib.blocksize == -1)
895796c8dcSSimon Schubert     gdb_xml_error (parser, _("Flash block size is not set"));
905796c8dcSSimon Schubert }
915796c8dcSSimon Schubert 
925796c8dcSSimon Schubert /* Handle the start of a <property> element by saving the name
935796c8dcSSimon Schubert    attribute for later.  */
945796c8dcSSimon Schubert 
955796c8dcSSimon Schubert static void
memory_map_start_property(struct gdb_xml_parser * parser,const struct gdb_xml_element * element,void * user_data,VEC (gdb_xml_value_s)* attributes)965796c8dcSSimon Schubert memory_map_start_property (struct gdb_xml_parser *parser,
975796c8dcSSimon Schubert 			   const struct gdb_xml_element *element,
985796c8dcSSimon Schubert 			   void *user_data, VEC(gdb_xml_value_s) *attributes)
995796c8dcSSimon Schubert {
1005796c8dcSSimon Schubert   struct memory_map_parsing_data *data = user_data;
1015796c8dcSSimon Schubert   char *name;
1025796c8dcSSimon Schubert 
103c50c785cSJohn Marino   name = xml_find_attribute (attributes, "name")->value;
1045796c8dcSSimon Schubert   snprintf (data->property_name, sizeof (data->property_name), "%s", name);
1055796c8dcSSimon Schubert }
1065796c8dcSSimon Schubert 
1075796c8dcSSimon Schubert /* Handle the end of a <property> element and its value.  */
1085796c8dcSSimon Schubert 
1095796c8dcSSimon Schubert static void
memory_map_end_property(struct gdb_xml_parser * parser,const struct gdb_xml_element * element,void * user_data,const char * body_text)1105796c8dcSSimon Schubert memory_map_end_property (struct gdb_xml_parser *parser,
1115796c8dcSSimon Schubert 			 const struct gdb_xml_element *element,
1125796c8dcSSimon Schubert 			 void *user_data, const char *body_text)
1135796c8dcSSimon Schubert {
1145796c8dcSSimon Schubert   struct memory_map_parsing_data *data = user_data;
1155796c8dcSSimon Schubert   char *name = data->property_name;
1165796c8dcSSimon Schubert 
1175796c8dcSSimon Schubert   if (strcmp (name, "blocksize") == 0)
1185796c8dcSSimon Schubert     {
1195796c8dcSSimon Schubert       struct mem_region *r = VEC_last (mem_region_s, *data->memory_map);
1205796c8dcSSimon Schubert 
1215796c8dcSSimon Schubert       r->attrib.blocksize = gdb_xml_parse_ulongest (parser, body_text);
1225796c8dcSSimon Schubert     }
1235796c8dcSSimon Schubert   else
1245796c8dcSSimon Schubert     gdb_xml_debug (parser, _("Unknown property \"%s\""), name);
1255796c8dcSSimon Schubert }
1265796c8dcSSimon Schubert 
1275796c8dcSSimon Schubert /* Discard the constructed memory map (if an error occurs).  */
1285796c8dcSSimon Schubert 
1295796c8dcSSimon Schubert static void
clear_result(void * p)1305796c8dcSSimon Schubert clear_result (void *p)
1315796c8dcSSimon Schubert {
1325796c8dcSSimon Schubert   VEC(mem_region_s) **result = p;
1335796c8dcSSimon Schubert   VEC_free (mem_region_s, *result);
1345796c8dcSSimon Schubert   *result = NULL;
1355796c8dcSSimon Schubert }
1365796c8dcSSimon Schubert 
1375796c8dcSSimon Schubert /* The allowed elements and attributes for an XML memory map.  */
1385796c8dcSSimon Schubert 
1395796c8dcSSimon Schubert const struct gdb_xml_attribute property_attributes[] = {
1405796c8dcSSimon Schubert   { "name", GDB_XML_AF_NONE, NULL, NULL },
1415796c8dcSSimon Schubert   { NULL, GDB_XML_AF_NONE, NULL, NULL }
1425796c8dcSSimon Schubert };
1435796c8dcSSimon Schubert 
1445796c8dcSSimon Schubert const struct gdb_xml_element memory_children[] = {
1455796c8dcSSimon Schubert   { "property", property_attributes, NULL,
1465796c8dcSSimon Schubert     GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
1475796c8dcSSimon Schubert     memory_map_start_property, memory_map_end_property },
1485796c8dcSSimon Schubert   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
1495796c8dcSSimon Schubert };
1505796c8dcSSimon Schubert 
1515796c8dcSSimon Schubert const struct gdb_xml_enum memory_type_enum[] = {
1525796c8dcSSimon Schubert   { "ram", MEM_RW },
1535796c8dcSSimon Schubert   { "rom", MEM_RO },
1545796c8dcSSimon Schubert   { "flash", MEM_FLASH },
1555796c8dcSSimon Schubert   { NULL, 0 }
1565796c8dcSSimon Schubert };
1575796c8dcSSimon Schubert 
1585796c8dcSSimon Schubert const struct gdb_xml_attribute memory_attributes[] = {
1595796c8dcSSimon Schubert   { "start", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
1605796c8dcSSimon Schubert   { "length", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
1615796c8dcSSimon Schubert   { "type", GDB_XML_AF_NONE, gdb_xml_parse_attr_enum, &memory_type_enum },
1625796c8dcSSimon Schubert   { NULL, GDB_XML_AF_NONE, NULL, NULL }
1635796c8dcSSimon Schubert };
1645796c8dcSSimon Schubert 
1655796c8dcSSimon Schubert const struct gdb_xml_element memory_map_children[] = {
1665796c8dcSSimon Schubert   { "memory", memory_attributes, memory_children, GDB_XML_EF_REPEATABLE,
1675796c8dcSSimon Schubert     memory_map_start_memory, memory_map_end_memory },
1685796c8dcSSimon Schubert   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
1695796c8dcSSimon Schubert };
1705796c8dcSSimon Schubert 
1715796c8dcSSimon Schubert const struct gdb_xml_element memory_map_elements[] = {
1725796c8dcSSimon Schubert   { "memory-map", NULL, memory_map_children, GDB_XML_EF_NONE,
1735796c8dcSSimon Schubert     NULL, NULL },
1745796c8dcSSimon Schubert   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
1755796c8dcSSimon Schubert };
1765796c8dcSSimon Schubert 
VEC(mem_region_s)1775796c8dcSSimon Schubert VEC(mem_region_s) *
1785796c8dcSSimon Schubert parse_memory_map (const char *memory_map)
1795796c8dcSSimon Schubert {
1805796c8dcSSimon Schubert   VEC(mem_region_s) *result = NULL;
181c50c785cSJohn Marino   struct cleanup *back_to;
1825796c8dcSSimon Schubert   struct memory_map_parsing_data data = { NULL };
1835796c8dcSSimon Schubert 
1845796c8dcSSimon Schubert   data.memory_map = &result;
185c50c785cSJohn Marino   back_to = make_cleanup (clear_result, &result);
186c50c785cSJohn Marino   if (gdb_xml_parse_quick (_("target memory map"), NULL, memory_map_elements,
187c50c785cSJohn Marino 			   memory_map, &data) == 0)
188c50c785cSJohn Marino     {
189c50c785cSJohn Marino       /* Parsed successfully, keep the result.  */
190c50c785cSJohn Marino       discard_cleanups (back_to);
191c50c785cSJohn Marino       return result;
192c50c785cSJohn Marino     }
1935796c8dcSSimon Schubert 
1945796c8dcSSimon Schubert   do_cleanups (back_to);
195c50c785cSJohn Marino   return NULL;
1965796c8dcSSimon Schubert }
1975796c8dcSSimon Schubert 
1985796c8dcSSimon Schubert #endif /* HAVE_LIBEXPAT */
199