gdb/memory-map.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Routines for handling XML memory maps provided by target.

  2.    Copyright (C) 2006-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "memory-map.h"

  16. #if !defined(HAVE_LIBEXPAT)

  17. VEC(mem_region_s) *
  18. parse_memory_map (const char *memory_map)
  19. {
  20.   static int have_warned;

  21.   if (!have_warned)
  22.     {
  23.       have_warned = 1;
  24.       warning (_("Can not parse XML memory map; XML support was disabled "
  25.                  "at compile time"));
  26.     }

  27.   return NULL;
  28. }

  29. #else /* HAVE_LIBEXPAT */

  30. #include "xml-support.h"

  31. /* Internal parsing data passed to all XML callbacks.  */
  32. struct memory_map_parsing_data
  33.   {
  34.     VEC(mem_region_s) **memory_map;
  35.     char property_name[32];
  36.   };

  37. /* Handle the start of a <memory> element.  */

  38. static void
  39. memory_map_start_memory (struct gdb_xml_parser *parser,
  40.                          const struct gdb_xml_element *element,
  41.                          void *user_data, VEC(gdb_xml_value_s) *attributes)
  42. {
  43.   struct memory_map_parsing_data *data = user_data;
  44.   struct mem_region *r = VEC_safe_push (mem_region_s, *data->memory_map, NULL);
  45.   ULONGEST *start_p, *length_p, *type_p;

  46.   start_p = xml_find_attribute (attributes, "start")->value;
  47.   length_p = xml_find_attribute (attributes, "length")->value;
  48.   type_p = xml_find_attribute (attributes, "type")->value;

  49.   mem_region_init (r);
  50.   r->lo = *start_p;
  51.   r->hi = r->lo + *length_p;
  52.   r->attrib.mode = *type_p;
  53.   r->attrib.blocksize = -1;
  54. }

  55. /* Handle the end of a <memory> element.  Verify that any necessary
  56.    children were present.  */

  57. static void
  58. memory_map_end_memory (struct gdb_xml_parser *parser,
  59.                        const struct gdb_xml_element *element,
  60.                        void *user_data, const char *body_text)
  61. {
  62.   struct memory_map_parsing_data *data = user_data;
  63.   struct mem_region *r = VEC_last (mem_region_s, *data->memory_map);

  64.   if (r->attrib.mode == MEM_FLASH && r->attrib.blocksize == -1)
  65.     gdb_xml_error (parser, _("Flash block size is not set"));
  66. }

  67. /* Handle the start of a <property> element by saving the name
  68.    attribute for later.  */

  69. static void
  70. memory_map_start_property (struct gdb_xml_parser *parser,
  71.                            const struct gdb_xml_element *element,
  72.                            void *user_data, VEC(gdb_xml_value_s) *attributes)
  73. {
  74.   struct memory_map_parsing_data *data = user_data;
  75.   char *name;

  76.   name = xml_find_attribute (attributes, "name")->value;
  77.   snprintf (data->property_name, sizeof (data->property_name), "%s", name);
  78. }

  79. /* Handle the end of a <property> element and its value.  */

  80. static void
  81. memory_map_end_property (struct gdb_xml_parser *parser,
  82.                          const struct gdb_xml_element *element,
  83.                          void *user_data, const char *body_text)
  84. {
  85.   struct memory_map_parsing_data *data = user_data;
  86.   char *name = data->property_name;

  87.   if (strcmp (name, "blocksize") == 0)
  88.     {
  89.       struct mem_region *r = VEC_last (mem_region_s, *data->memory_map);

  90.       r->attrib.blocksize = gdb_xml_parse_ulongest (parser, body_text);
  91.     }
  92.   else
  93.     gdb_xml_debug (parser, _("Unknown property \"%s\""), name);
  94. }

  95. /* Discard the constructed memory map (if an error occurs).  */

  96. static void
  97. clear_result (void *p)
  98. {
  99.   VEC(mem_region_s) **result = p;
  100.   VEC_free (mem_region_s, *result);
  101.   *result = NULL;
  102. }

  103. /* The allowed elements and attributes for an XML memory map.  */

  104. const struct gdb_xml_attribute property_attributes[] = {
  105.   { "name", GDB_XML_AF_NONE, NULL, NULL },
  106.   { NULL, GDB_XML_AF_NONE, NULL, NULL }
  107. };

  108. const struct gdb_xml_element memory_children[] = {
  109.   { "property", property_attributes, NULL,
  110.     GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
  111.     memory_map_start_property, memory_map_end_property },
  112.   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
  113. };

  114. const struct gdb_xml_enum memory_type_enum[] = {
  115.   { "ram", MEM_RW },
  116.   { "rom", MEM_RO },
  117.   { "flash", MEM_FLASH },
  118.   { NULL, 0 }
  119. };

  120. const struct gdb_xml_attribute memory_attributes[] = {
  121.   { "start", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
  122.   { "length", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
  123.   { "type", GDB_XML_AF_NONE, gdb_xml_parse_attr_enum, &memory_type_enum },
  124.   { NULL, GDB_XML_AF_NONE, NULL, NULL }
  125. };

  126. const struct gdb_xml_element memory_map_children[] = {
  127.   { "memory", memory_attributes, memory_children, GDB_XML_EF_REPEATABLE,
  128.     memory_map_start_memory, memory_map_end_memory },
  129.   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
  130. };

  131. const struct gdb_xml_element memory_map_elements[] = {
  132.   { "memory-map", NULL, memory_map_children, GDB_XML_EF_NONE,
  133.     NULL, NULL },
  134.   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
  135. };

  136. VEC(mem_region_s) *
  137. parse_memory_map (const char *memory_map)
  138. {
  139.   VEC(mem_region_s) *result = NULL;
  140.   struct cleanup *back_to;
  141.   struct memory_map_parsing_data data = { NULL };

  142.   data.memory_map = &result;
  143.   back_to = make_cleanup (clear_result, &result);
  144.   if (gdb_xml_parse_quick (_("target memory map"), NULL, memory_map_elements,
  145.                            memory_map, &data) == 0)
  146.     {
  147.       /* Parsed successfully, keep the result.  */
  148.       discard_cleanups (back_to);
  149.       return result;
  150.     }

  151.   do_cleanups (back_to);
  152.   return NULL;
  153. }

  154. #endif /* HAVE_LIBEXPAT */