memory-map.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* Routines for handling XML memory maps provided by target.
  2. Copyright (C) 2006-2022 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. std::vector<mem_region>
  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 std::vector<mem_region> ();
  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. memory_map_parsing_data (std::vector<mem_region> *memory_map_)
  35. : memory_map (memory_map_)
  36. {}
  37. std::vector<mem_region> *memory_map;
  38. std::string property_name;
  39. };
  40. /* Handle the start of a <memory> element. */
  41. static void
  42. memory_map_start_memory (struct gdb_xml_parser *parser,
  43. const struct gdb_xml_element *element,
  44. void *user_data,
  45. std::vector<gdb_xml_value> &attributes)
  46. {
  47. struct memory_map_parsing_data *data
  48. = (struct memory_map_parsing_data *) user_data;
  49. ULONGEST *start_p, *length_p, *type_p;
  50. start_p
  51. = (ULONGEST *) xml_find_attribute (attributes, "start")->value.get ();
  52. length_p
  53. = (ULONGEST *) xml_find_attribute (attributes, "length")->value.get ();
  54. type_p
  55. = (ULONGEST *) xml_find_attribute (attributes, "type")->value.get ();
  56. data->memory_map->emplace_back (*start_p, *start_p + *length_p,
  57. (enum mem_access_mode) *type_p);
  58. }
  59. /* Handle the end of a <memory> element. Verify that any necessary
  60. children were present. */
  61. static void
  62. memory_map_end_memory (struct gdb_xml_parser *parser,
  63. const struct gdb_xml_element *element,
  64. void *user_data, const char *body_text)
  65. {
  66. struct memory_map_parsing_data *data
  67. = (struct memory_map_parsing_data *) user_data;
  68. const mem_region &r = data->memory_map->back ();
  69. if (r.attrib.mode == MEM_FLASH && r.attrib.blocksize == -1)
  70. gdb_xml_error (parser, _("Flash block size is not set"));
  71. }
  72. /* Handle the start of a <property> element by saving the name
  73. attribute for later. */
  74. static void
  75. memory_map_start_property (struct gdb_xml_parser *parser,
  76. const struct gdb_xml_element *element,
  77. void *user_data,
  78. std::vector<gdb_xml_value> &attributes)
  79. {
  80. struct memory_map_parsing_data *data
  81. = (struct memory_map_parsing_data *) user_data;
  82. char *name;
  83. name = (char *) xml_find_attribute (attributes, "name")->value.get ();
  84. data->property_name.assign (name);
  85. }
  86. /* Handle the end of a <property> element and its value. */
  87. static void
  88. memory_map_end_property (struct gdb_xml_parser *parser,
  89. const struct gdb_xml_element *element,
  90. void *user_data, const char *body_text)
  91. {
  92. struct memory_map_parsing_data *data
  93. = (struct memory_map_parsing_data *) user_data;
  94. if (data->property_name == "blocksize")
  95. {
  96. mem_region &r = data->memory_map->back ();
  97. r.attrib.blocksize = gdb_xml_parse_ulongest (parser, body_text);
  98. }
  99. else
  100. gdb_xml_debug (parser, _("Unknown property \"%s\""),
  101. data->property_name.c_str ());
  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. std::vector<mem_region>
  137. parse_memory_map (const char *memory_map)
  138. {
  139. std::vector<mem_region> ret;
  140. memory_map_parsing_data data (&ret);
  141. if (gdb_xml_parse_quick (_("target memory map"), NULL, memory_map_elements,
  142. memory_map, &data) == 0)
  143. {
  144. /* Parsed successfully, keep the result. */
  145. return ret;
  146. }
  147. return std::vector<mem_region> ();
  148. }
  149. #endif /* HAVE_LIBEXPAT */