tdesc.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* Copyright (C) 2012-2022 Free Software Foundation, Inc.
  2. This file is part of GDB.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #include "server.h"
  14. #include "tdesc.h"
  15. #include "regdef.h"
  16. #ifndef IN_PROCESS_AGENT
  17. target_desc::~target_desc ()
  18. {
  19. xfree ((char *) arch);
  20. xfree ((char *) osabi);
  21. }
  22. bool target_desc::operator== (const target_desc &other) const
  23. {
  24. if (reg_defs != other.reg_defs)
  25. return false;
  26. /* Compare expedite_regs. */
  27. int i = 0;
  28. for (; expedite_regs[i] != NULL; i++)
  29. {
  30. if (strcmp (expedite_regs[i], other.expedite_regs[i]) != 0)
  31. return false;
  32. }
  33. if (other.expedite_regs[i] != NULL)
  34. return false;
  35. return true;
  36. }
  37. #endif
  38. void target_desc::accept (tdesc_element_visitor &v) const
  39. {
  40. #ifndef IN_PROCESS_AGENT
  41. v.visit_pre (this);
  42. for (const tdesc_feature_up &feature : features)
  43. feature->accept (v);
  44. v.visit_post (this);
  45. #endif
  46. }
  47. void
  48. init_target_desc (struct target_desc *tdesc,
  49. const char **expedite_regs)
  50. {
  51. int offset = 0;
  52. /* Go through all the features and populate reg_defs. */
  53. for (const tdesc_feature_up &feature : tdesc->features)
  54. for (const tdesc_reg_up &treg : feature->registers)
  55. {
  56. int regnum = treg->target_regnum;
  57. /* Register number will increase (possibly with gaps) or be zero. */
  58. gdb_assert (regnum == 0 || regnum >= tdesc->reg_defs.size ());
  59. if (regnum != 0)
  60. tdesc->reg_defs.resize (regnum, gdb::reg (offset));
  61. tdesc->reg_defs.emplace_back (treg->name.c_str (), offset,
  62. treg->bitsize);
  63. offset += treg->bitsize;
  64. }
  65. tdesc->registers_size = offset / 8;
  66. /* Make sure PBUFSIZ is large enough to hold a full register
  67. packet. */
  68. gdb_assert (2 * tdesc->registers_size + 32 <= PBUFSIZ);
  69. #ifndef IN_PROCESS_AGENT
  70. tdesc->expedite_regs = expedite_regs;
  71. #endif
  72. }
  73. /* See gdbsupport/tdesc.h. */
  74. target_desc_up
  75. allocate_target_description (void)
  76. {
  77. return target_desc_up (new target_desc ());
  78. }
  79. /* See gdbsupport/tdesc.h. */
  80. void
  81. target_desc_deleter::operator() (struct target_desc *target_desc) const
  82. {
  83. delete target_desc;
  84. }
  85. #ifndef IN_PROCESS_AGENT
  86. static const struct target_desc default_description {};
  87. void
  88. copy_target_description (struct target_desc *dest,
  89. const struct target_desc *src)
  90. {
  91. dest->reg_defs = src->reg_defs;
  92. dest->expedite_regs = src->expedite_regs;
  93. dest->registers_size = src->registers_size;
  94. dest->xmltarget = src->xmltarget;
  95. }
  96. const struct target_desc *
  97. current_target_desc (void)
  98. {
  99. if (current_thread == NULL)
  100. return &default_description;
  101. return current_process ()->tdesc;
  102. }
  103. /* An empty structure. */
  104. struct tdesc_compatible_info { };
  105. /* See gdbsupport/tdesc.h. */
  106. const std::vector<tdesc_compatible_info_up> &
  107. tdesc_compatible_info_list (const target_desc *target_desc)
  108. {
  109. static std::vector<tdesc_compatible_info_up> empty;
  110. return empty;
  111. }
  112. /* See gdbsupport/tdesc.h. */
  113. const char *
  114. tdesc_compatible_info_arch_name (const tdesc_compatible_info_up &c_info)
  115. {
  116. return nullptr;
  117. }
  118. /* See gdbsupport/tdesc.h. */
  119. const char *
  120. tdesc_architecture_name (const struct target_desc *target_desc)
  121. {
  122. return target_desc->arch;
  123. }
  124. /* See gdbsupport/tdesc.h. */
  125. void
  126. set_tdesc_architecture (struct target_desc *target_desc,
  127. const char *name)
  128. {
  129. target_desc->arch = xstrdup (name);
  130. }
  131. /* See gdbsupport/tdesc.h. */
  132. const char *
  133. tdesc_osabi_name (const struct target_desc *target_desc)
  134. {
  135. return target_desc->osabi;
  136. }
  137. /* See gdbsupport/tdesc.h. */
  138. void
  139. set_tdesc_osabi (struct target_desc *target_desc, const char *name)
  140. {
  141. target_desc->osabi = xstrdup (name);
  142. }
  143. /* See gdbsupport/tdesc.h. */
  144. const char *
  145. tdesc_get_features_xml (const target_desc *tdesc)
  146. {
  147. /* Either .xmltarget or .features is not NULL. */
  148. gdb_assert (tdesc->xmltarget != NULL
  149. || (!tdesc->features.empty ()
  150. && tdesc->arch != NULL));
  151. if (tdesc->xmltarget == NULL)
  152. {
  153. std::string buffer ("@");
  154. print_xml_feature v (&buffer);
  155. tdesc->accept (v);
  156. tdesc->xmltarget = xstrdup (buffer.c_str ());
  157. }
  158. return tdesc->xmltarget;
  159. }
  160. #endif
  161. /* See gdbsupport/tdesc.h. */
  162. struct tdesc_feature *
  163. tdesc_create_feature (struct target_desc *tdesc, const char *name)
  164. {
  165. struct tdesc_feature *new_feature = new tdesc_feature (name);
  166. tdesc->features.emplace_back (new_feature);
  167. return new_feature;
  168. }
  169. /* See gdbsupport/tdesc.h. */
  170. bool
  171. tdesc_contains_feature (const target_desc *tdesc, const std::string &feature)
  172. {
  173. gdb_assert (tdesc != nullptr);
  174. for (const tdesc_feature_up &f : tdesc->features)
  175. {
  176. if (f->name == feature)
  177. return true;
  178. }
  179. return false;
  180. }