tramp-frame.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* Signal trampoline unwinder, for GDB the GNU Debugger.
  2. Copyright (C) 2004-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 "tramp-frame.h"
  16. #include "frame-unwind.h"
  17. #include "gdbcore.h"
  18. #include "symtab.h"
  19. #include "objfiles.h"
  20. #include "target.h"
  21. #include "trad-frame.h"
  22. #include "frame-base.h"
  23. struct frame_data
  24. {
  25. const struct tramp_frame *tramp_frame;
  26. };
  27. struct tramp_frame_cache
  28. {
  29. CORE_ADDR func;
  30. const struct tramp_frame *tramp_frame;
  31. struct trad_frame_cache *trad_cache;
  32. };
  33. static struct trad_frame_cache *
  34. tramp_frame_cache (struct frame_info *this_frame,
  35. void **this_cache)
  36. {
  37. struct tramp_frame_cache *tramp_cache
  38. = (struct tramp_frame_cache *) *this_cache;
  39. if (tramp_cache->trad_cache == NULL)
  40. {
  41. tramp_cache->trad_cache = trad_frame_cache_zalloc (this_frame);
  42. tramp_cache->tramp_frame->init (tramp_cache->tramp_frame,
  43. this_frame,
  44. tramp_cache->trad_cache,
  45. tramp_cache->func);
  46. }
  47. return tramp_cache->trad_cache;
  48. }
  49. static void
  50. tramp_frame_this_id (struct frame_info *this_frame,
  51. void **this_cache,
  52. struct frame_id *this_id)
  53. {
  54. struct trad_frame_cache *trad_cache
  55. = tramp_frame_cache (this_frame, this_cache);
  56. trad_frame_get_id (trad_cache, this_id);
  57. }
  58. static struct value *
  59. tramp_frame_prev_register (struct frame_info *this_frame,
  60. void **this_cache,
  61. int prev_regnum)
  62. {
  63. struct trad_frame_cache *trad_cache
  64. = tramp_frame_cache (this_frame, this_cache);
  65. return trad_frame_get_register (trad_cache, this_frame, prev_regnum);
  66. }
  67. static CORE_ADDR
  68. tramp_frame_start (const struct tramp_frame *tramp,
  69. struct frame_info *this_frame, CORE_ADDR pc)
  70. {
  71. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  72. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  73. int ti;
  74. /* Check if we can use this trampoline. */
  75. if (tramp->validate && !tramp->validate (tramp, this_frame, &pc))
  76. return 0;
  77. /* Search through the trampoline for one that matches the
  78. instruction sequence around PC. */
  79. for (ti = 0; tramp->insn[ti].bytes != TRAMP_SENTINEL_INSN; ti++)
  80. {
  81. CORE_ADDR func = pc - tramp->insn_size * ti;
  82. int i;
  83. for (i = 0; 1; i++)
  84. {
  85. gdb_byte buf[sizeof (tramp->insn[0])];
  86. ULONGEST insn;
  87. size_t insn_size = tramp->insn_size;
  88. if (tramp->insn[i].bytes == TRAMP_SENTINEL_INSN)
  89. return func;
  90. if (!safe_frame_unwind_memory (this_frame,
  91. func + i * insn_size,
  92. {buf, insn_size}))
  93. break;
  94. insn = extract_unsigned_integer (buf, insn_size, byte_order);
  95. if (tramp->insn[i].bytes != (insn & tramp->insn[i].mask))
  96. break;
  97. }
  98. }
  99. /* Trampoline doesn't match. */
  100. return 0;
  101. }
  102. static int
  103. tramp_frame_sniffer (const struct frame_unwind *self,
  104. struct frame_info *this_frame,
  105. void **this_cache)
  106. {
  107. const struct tramp_frame *tramp = self->unwind_data->tramp_frame;
  108. CORE_ADDR pc = get_frame_pc (this_frame);
  109. CORE_ADDR func;
  110. struct tramp_frame_cache *tramp_cache;
  111. /* tausq/2004-12-12: We used to assume if pc has a name or is in a valid
  112. section, then this is not a trampoline. However, this assumption is
  113. false on HPUX which has a signal trampoline that has a name; it can
  114. also be false when using an alternative signal stack. */
  115. func = tramp_frame_start (tramp, this_frame, pc);
  116. if (func == 0)
  117. return 0;
  118. tramp_cache = FRAME_OBSTACK_ZALLOC (struct tramp_frame_cache);
  119. tramp_cache->func = func;
  120. tramp_cache->tramp_frame = tramp;
  121. (*this_cache) = tramp_cache;
  122. return 1;
  123. }
  124. void
  125. tramp_frame_prepend_unwinder (struct gdbarch *gdbarch,
  126. const struct tramp_frame *tramp_frame)
  127. {
  128. struct frame_data *data;
  129. struct frame_unwind *unwinder;
  130. int i;
  131. /* Check that the instruction sequence contains a sentinel. */
  132. for (i = 0; i < ARRAY_SIZE (tramp_frame->insn); i++)
  133. {
  134. if (tramp_frame->insn[i].bytes == TRAMP_SENTINEL_INSN)
  135. break;
  136. }
  137. gdb_assert (i < ARRAY_SIZE (tramp_frame->insn));
  138. gdb_assert (tramp_frame->insn_size <= sizeof (tramp_frame->insn[0].bytes));
  139. data = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct frame_data);
  140. unwinder = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct frame_unwind);
  141. data->tramp_frame = tramp_frame;
  142. unwinder->type = tramp_frame->frame_type;
  143. unwinder->unwind_data = data;
  144. unwinder->sniffer = tramp_frame_sniffer;
  145. unwinder->stop_reason = default_frame_unwind_stop_reason;
  146. unwinder->this_id = tramp_frame_this_id;
  147. unwinder->prev_register = tramp_frame_prev_register;
  148. frame_unwind_prepend_unwinder (gdbarch, unwinder);
  149. }