trad-frame.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* Traditional frame unwind support, for GDB the GNU Debugger.
  2. Copyright (C) 2003-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 "frame.h"
  16. #include "trad-frame.h"
  17. #include "regcache.h"
  18. #include "frame-unwind.h"
  19. #include "target.h"
  20. #include "value.h"
  21. #include "gdbarch.h"
  22. #include "gdbsupport/traits.h"
  23. struct trad_frame_cache
  24. {
  25. struct frame_info *this_frame;
  26. CORE_ADDR this_base;
  27. trad_frame_saved_reg *prev_regs;
  28. struct frame_id this_id;
  29. };
  30. struct trad_frame_cache *
  31. trad_frame_cache_zalloc (struct frame_info *this_frame)
  32. {
  33. struct trad_frame_cache *this_trad_cache;
  34. this_trad_cache = FRAME_OBSTACK_ZALLOC (struct trad_frame_cache);
  35. this_trad_cache->prev_regs = trad_frame_alloc_saved_regs (this_frame);
  36. this_trad_cache->this_frame = this_frame;
  37. return this_trad_cache;
  38. }
  39. /* See trad-frame.h. */
  40. void
  41. trad_frame_reset_saved_regs (struct gdbarch *gdbarch,
  42. trad_frame_saved_reg *regs)
  43. {
  44. int numregs = gdbarch_num_cooked_regs (gdbarch);
  45. for (int regnum = 0; regnum < numregs; regnum++)
  46. regs[regnum].set_realreg (regnum);
  47. }
  48. trad_frame_saved_reg *
  49. trad_frame_alloc_saved_regs (struct gdbarch *gdbarch)
  50. {
  51. #ifdef HAVE_IS_TRIVIALLY_CONSTRUCTIBLE
  52. gdb_static_assert (std::is_trivially_constructible<trad_frame_saved_reg>::value);
  53. #endif
  54. int numregs = gdbarch_num_cooked_regs (gdbarch);
  55. trad_frame_saved_reg *this_saved_regs
  56. = FRAME_OBSTACK_CALLOC (numregs, trad_frame_saved_reg);
  57. /* For backwards compatibility, initialize all the register values to
  58. REALREG, with register 0 stored in 0, register 1 stored in 1 and so
  59. on. */
  60. trad_frame_reset_saved_regs (gdbarch, this_saved_regs);
  61. return this_saved_regs;
  62. }
  63. /* A traditional frame is unwound by analysing the function prologue
  64. and using the information gathered to track registers. For
  65. non-optimized frames, the technique is reliable (just need to check
  66. for all potential instruction sequences). */
  67. trad_frame_saved_reg *
  68. trad_frame_alloc_saved_regs (struct frame_info *this_frame)
  69. {
  70. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  71. return trad_frame_alloc_saved_regs (gdbarch);
  72. }
  73. void
  74. trad_frame_set_reg_value (struct trad_frame_cache *this_trad_cache,
  75. int regnum, LONGEST val)
  76. {
  77. /* External interface for users of trad_frame_cache
  78. (who cannot access the prev_regs object directly). */
  79. this_trad_cache->prev_regs[regnum].set_value (val);
  80. }
  81. void
  82. trad_frame_set_reg_realreg (struct trad_frame_cache *this_trad_cache,
  83. int regnum, int realreg)
  84. {
  85. this_trad_cache->prev_regs[regnum].set_realreg (realreg);
  86. }
  87. void
  88. trad_frame_set_reg_addr (struct trad_frame_cache *this_trad_cache,
  89. int regnum, CORE_ADDR addr)
  90. {
  91. this_trad_cache->prev_regs[regnum].set_addr (addr);
  92. }
  93. void
  94. trad_frame_set_reg_regmap (struct trad_frame_cache *this_trad_cache,
  95. const struct regcache_map_entry *regmap,
  96. CORE_ADDR addr, size_t size)
  97. {
  98. struct gdbarch *gdbarch = get_frame_arch (this_trad_cache->this_frame);
  99. int offs = 0, count;
  100. for (; (count = regmap->count) != 0; regmap++)
  101. {
  102. int regno = regmap->regno;
  103. int slot_size = regmap->size;
  104. if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
  105. slot_size = register_size (gdbarch, regno);
  106. if (offs + slot_size > size)
  107. break;
  108. if (regno == REGCACHE_MAP_SKIP)
  109. offs += count * slot_size;
  110. else
  111. for (; count--; regno++, offs += slot_size)
  112. {
  113. /* Mimic the semantics of regcache::transfer_regset if a
  114. register slot's size does not match the size of a
  115. register.
  116. If a register slot is larger than a register, assume
  117. the register's value is stored in the first N bytes of
  118. the slot and ignore the remaining bytes.
  119. If the register slot is smaller than the register,
  120. assume that the slot contains the low N bytes of the
  121. register's value. Since trad_frame assumes that
  122. registers stored by address are sized according to the
  123. register, read the low N bytes and zero-extend them to
  124. generate a register value. */
  125. if (slot_size >= register_size (gdbarch, regno))
  126. trad_frame_set_reg_addr (this_trad_cache, regno, addr + offs);
  127. else
  128. {
  129. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  130. gdb_byte buf[slot_size];
  131. if (target_read_memory (addr + offs, buf, sizeof buf) == 0)
  132. {
  133. LONGEST val
  134. = extract_unsigned_integer (buf, sizeof buf, byte_order);
  135. trad_frame_set_reg_value (this_trad_cache, regno, val);
  136. }
  137. }
  138. }
  139. }
  140. }
  141. /* See trad-frame.h. */
  142. void
  143. trad_frame_set_reg_value_bytes (struct trad_frame_cache *this_trad_cache,
  144. int regnum,
  145. gdb::array_view<const gdb_byte> bytes)
  146. {
  147. /* External interface for users of trad_frame_cache
  148. (who cannot access the prev_regs object directly). */
  149. this_trad_cache->prev_regs[regnum].set_value_bytes (bytes);
  150. }
  151. struct value *
  152. trad_frame_get_prev_register (struct frame_info *this_frame,
  153. trad_frame_saved_reg this_saved_regs[],
  154. int regnum)
  155. {
  156. if (this_saved_regs[regnum].is_addr ())
  157. /* The register was saved in memory. */
  158. return frame_unwind_got_memory (this_frame, regnum,
  159. this_saved_regs[regnum].addr ());
  160. else if (this_saved_regs[regnum].is_realreg ())
  161. return frame_unwind_got_register (this_frame, regnum,
  162. this_saved_regs[regnum].realreg ());
  163. else if (this_saved_regs[regnum].is_value ())
  164. /* The register's value is available. */
  165. return frame_unwind_got_constant (this_frame, regnum,
  166. this_saved_regs[regnum].value ());
  167. else if (this_saved_regs[regnum].is_value_bytes ())
  168. /* The register's value is available as a sequence of bytes. */
  169. return frame_unwind_got_bytes (this_frame, regnum,
  170. this_saved_regs[regnum].value_bytes ());
  171. else
  172. return frame_unwind_got_optimized (this_frame, regnum);
  173. }
  174. struct value *
  175. trad_frame_get_register (struct trad_frame_cache *this_trad_cache,
  176. struct frame_info *this_frame,
  177. int regnum)
  178. {
  179. return trad_frame_get_prev_register (this_frame, this_trad_cache->prev_regs,
  180. regnum);
  181. }
  182. void
  183. trad_frame_set_id (struct trad_frame_cache *this_trad_cache,
  184. struct frame_id this_id)
  185. {
  186. this_trad_cache->this_id = this_id;
  187. }
  188. void
  189. trad_frame_get_id (struct trad_frame_cache *this_trad_cache,
  190. struct frame_id *this_id)
  191. {
  192. (*this_id) = this_trad_cache->this_id;
  193. }
  194. void
  195. trad_frame_set_this_base (struct trad_frame_cache *this_trad_cache,
  196. CORE_ADDR this_base)
  197. {
  198. this_trad_cache->this_base = this_base;
  199. }
  200. CORE_ADDR
  201. trad_frame_get_this_base (struct trad_frame_cache *this_trad_cache)
  202. {
  203. return this_trad_cache->this_base;
  204. }