Automatic date update in version.in
[binutils-gdb.git] / gdb / tramp-frame.c
blob4b397cbf046dae0f9b7e69291b606242ca680a96
1 /* Signal trampoline unwinder, for GDB the GNU Debugger.
3 Copyright (C) 2004-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "tramp-frame.h"
21 #include "frame-unwind.h"
22 #include "gdbcore.h"
23 #include "symtab.h"
24 #include "objfiles.h"
25 #include "target.h"
26 #include "trad-frame.h"
27 #include "frame-base.h"
29 struct frame_data
31 const struct tramp_frame *tramp_frame;
34 struct tramp_frame_cache
36 CORE_ADDR func;
37 const struct tramp_frame *tramp_frame;
38 struct trad_frame_cache *trad_cache;
41 static struct trad_frame_cache *
42 tramp_frame_cache (const frame_info_ptr &this_frame,
43 void **this_cache)
45 struct tramp_frame_cache *tramp_cache
46 = (struct tramp_frame_cache *) *this_cache;
48 if (tramp_cache->trad_cache == NULL)
50 tramp_cache->trad_cache = trad_frame_cache_zalloc (this_frame);
51 tramp_cache->tramp_frame->init (tramp_cache->tramp_frame,
52 this_frame,
53 tramp_cache->trad_cache,
54 tramp_cache->func);
56 return tramp_cache->trad_cache;
59 static void
60 tramp_frame_this_id (const frame_info_ptr &this_frame,
61 void **this_cache,
62 struct frame_id *this_id)
64 struct trad_frame_cache *trad_cache
65 = tramp_frame_cache (this_frame, this_cache);
67 trad_frame_get_id (trad_cache, this_id);
70 static struct value *
71 tramp_frame_prev_register (const frame_info_ptr &this_frame,
72 void **this_cache,
73 int prev_regnum)
75 struct trad_frame_cache *trad_cache
76 = tramp_frame_cache (this_frame, this_cache);
78 return trad_frame_get_register (trad_cache, this_frame, prev_regnum);
81 static CORE_ADDR
82 tramp_frame_start (const struct tramp_frame *tramp,
83 const frame_info_ptr &this_frame, CORE_ADDR pc)
85 struct gdbarch *gdbarch = get_frame_arch (this_frame);
86 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
87 int ti;
89 /* Check if we can use this trampoline. */
90 if (tramp->validate && !tramp->validate (tramp, this_frame, &pc))
91 return 0;
93 /* Search through the trampoline for one that matches the
94 instruction sequence around PC. */
95 for (ti = 0; tramp->insn[ti].bytes != TRAMP_SENTINEL_INSN; ti++)
97 CORE_ADDR func = pc - tramp->insn_size * ti;
98 int i;
100 for (i = 0; 1; i++)
102 gdb_byte buf[sizeof (tramp->insn[0])];
103 ULONGEST insn;
104 size_t insn_size = tramp->insn_size;
106 if (tramp->insn[i].bytes == TRAMP_SENTINEL_INSN)
107 return func;
108 if (!safe_frame_unwind_memory (this_frame,
109 func + i * insn_size,
110 {buf, insn_size}))
111 break;
112 insn = extract_unsigned_integer (buf, insn_size, byte_order);
113 if (tramp->insn[i].bytes != (insn & tramp->insn[i].mask))
114 break;
117 /* Trampoline doesn't match. */
118 return 0;
121 static int
122 tramp_frame_sniffer (const struct frame_unwind *self,
123 const frame_info_ptr &this_frame,
124 void **this_cache)
126 const struct tramp_frame *tramp = self->unwind_data->tramp_frame;
127 CORE_ADDR pc = get_frame_pc (this_frame);
128 CORE_ADDR func;
129 struct tramp_frame_cache *tramp_cache;
131 /* tausq/2004-12-12: We used to assume if pc has a name or is in a valid
132 section, then this is not a trampoline. However, this assumption is
133 false on HPUX which has a signal trampoline that has a name; it can
134 also be false when using an alternative signal stack. */
135 func = tramp_frame_start (tramp, this_frame, pc);
136 if (func == 0)
137 return 0;
138 tramp_cache = FRAME_OBSTACK_ZALLOC (struct tramp_frame_cache);
139 tramp_cache->func = func;
140 tramp_cache->tramp_frame = tramp;
141 (*this_cache) = tramp_cache;
142 return 1;
145 void
146 tramp_frame_prepend_unwinder (struct gdbarch *gdbarch,
147 const struct tramp_frame *tramp_frame)
149 struct frame_data *data;
150 struct frame_unwind *unwinder;
151 int i;
153 /* Check that the instruction sequence contains a sentinel. */
154 for (i = 0; i < ARRAY_SIZE (tramp_frame->insn); i++)
156 if (tramp_frame->insn[i].bytes == TRAMP_SENTINEL_INSN)
157 break;
159 gdb_assert (i < ARRAY_SIZE (tramp_frame->insn));
160 gdb_assert (tramp_frame->insn_size <= sizeof (tramp_frame->insn[0].bytes));
162 data = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct frame_data);
163 unwinder = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct frame_unwind);
165 data->tramp_frame = tramp_frame;
166 unwinder->type = tramp_frame->frame_type;
167 unwinder->unwind_data = data;
168 unwinder->sniffer = tramp_frame_sniffer;
169 unwinder->stop_reason = default_frame_unwind_stop_reason;
170 unwinder->this_id = tramp_frame_this_id;
171 unwinder->prev_register = tramp_frame_prev_register;
172 unwinder->prev_arch = tramp_frame->prev_arch;
173 frame_unwind_prepend_unwinder (gdbarch, unwinder);