gdb/python: implement Python find_exec_by_build_id hook
[binutils-gdb.git] / gdb / dwarf2 / frame-tailcall.c
blob6ecf8a0b15d5addfbdf9ec69f90df48c8094b95d
1 /* Virtual tail call frames unwinder for GDB.
3 Copyright (C) 2010-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 "exceptions.h"
21 #include "frame.h"
22 #include "dwarf2/frame-tailcall.h"
23 #include "dwarf2/loc.h"
24 #include "frame-unwind.h"
25 #include "hashtab.h"
26 #include "regcache.h"
27 #include "value.h"
28 #include "dwarf2/frame.h"
29 #include "gdbarch.h"
31 /* Contains struct tailcall_cache indexed by next_bottom_frame. */
32 static htab_t cache_htab;
34 /* Associate structure of the unwinder to call_site_chain. Lifetime of this
35 structure is maintained by REFC decremented by dealloc_cache, all of them
36 get deleted during reinit_frame_cache. */
37 struct tailcall_cache
39 /* It must be the first one of this struct. It is the furthest callee. */
40 frame_info *next_bottom_frame;
42 /* Reference count. The whole chain of virtual tail call frames shares one
43 tailcall_cache. */
44 int refc;
46 /* Associated found virtual tail call frames chain, it is never NULL. */
47 struct call_site_chain *chain;
49 /* Cached pretended_chain_levels result. */
50 int chain_levels;
52 /* Unwound PC from the top (caller) frame, as it is not contained
53 in CHAIN. */
54 CORE_ADDR prev_pc;
56 /* Compensate SP in caller frames appropriately. prev_sp and
57 entry_cfa_sp_offset are valid only if PREV_SP_P. PREV_SP is SP at the top
58 (caller) frame. ENTRY_CFA_SP_OFFSET is shift of SP in tail call frames
59 against next_bottom_frame SP. */
60 unsigned prev_sp_p : 1;
61 CORE_ADDR prev_sp;
62 LONGEST entry_cfa_sp_offset;
65 /* hash_f for htab_create_alloc of cache_htab. */
67 static hashval_t
68 cache_hash (const void *arg)
70 const struct tailcall_cache *cache = (const struct tailcall_cache *) arg;
72 return htab_hash_pointer (cache->next_bottom_frame);
75 /* eq_f for htab_create_alloc of cache_htab. */
77 static int
78 cache_eq (const void *arg1, const void *arg2)
80 const struct tailcall_cache *cache1 = (const struct tailcall_cache *) arg1;
81 const struct tailcall_cache *cache2 = (const struct tailcall_cache *) arg2;
83 return cache1->next_bottom_frame == cache2->next_bottom_frame;
86 /* Create new tailcall_cache for NEXT_BOTTOM_FRAME, NEXT_BOTTOM_FRAME must not
87 yet have been indexed by cache_htab. Caller holds one reference of the new
88 tailcall_cache. */
90 static struct tailcall_cache *
91 cache_new_ref1 (const frame_info_ptr &next_bottom_frame)
93 struct tailcall_cache *cache = XCNEW (struct tailcall_cache);
94 void **slot;
96 cache->next_bottom_frame = next_bottom_frame.get ();
97 cache->refc = 1;
99 slot = htab_find_slot (cache_htab, cache, INSERT);
100 gdb_assert (*slot == NULL);
101 *slot = cache;
103 return cache;
106 /* Create new reference to CACHE. */
108 static void
109 cache_ref (struct tailcall_cache *cache)
111 gdb_assert (cache->refc > 0);
113 cache->refc++;
116 /* Drop reference to CACHE, possibly fully freeing it and unregistering it from
117 cache_htab. */
119 static void
120 cache_unref (struct tailcall_cache *cache)
122 gdb_assert (cache->refc > 0);
124 if (!--cache->refc)
126 gdb_assert (htab_find_slot (cache_htab, cache, NO_INSERT) != NULL);
127 htab_remove_elt (cache_htab, cache);
129 xfree (cache->chain);
130 xfree (cache);
134 /* Return 1 if FI is a non-bottom (not the callee) tail call frame. Otherwise
135 return 0. */
137 static int
138 frame_is_tailcall (const frame_info_ptr &fi)
140 return frame_unwinder_is (fi, &dwarf2_tailcall_frame_unwind);
143 /* Try to find tailcall_cache in cache_htab if FI is a part of its virtual tail
144 call chain. Otherwise return NULL. No new reference is created. */
146 static struct tailcall_cache *
147 cache_find (const frame_info_ptr &initial_fi)
149 struct tailcall_cache *cache;
150 struct tailcall_cache search;
151 void **slot;
153 frame_info_ptr fi = initial_fi;
154 while (frame_is_tailcall (fi))
156 fi = get_next_frame (fi);
157 gdb_assert (fi != NULL);
160 search.next_bottom_frame = fi.get();
161 search.refc = 1;
162 slot = htab_find_slot (cache_htab, &search, NO_INSERT);
163 if (slot == NULL)
164 return NULL;
166 cache = (struct tailcall_cache *) *slot;
167 gdb_assert (cache != NULL);
168 return cache;
171 /* Number of virtual frames between THIS_FRAME and CACHE->NEXT_BOTTOM_FRAME.
172 If THIS_FRAME is CACHE-> NEXT_BOTTOM_FRAME return -1. */
174 static int
175 existing_next_levels (const frame_info_ptr &this_frame,
176 struct tailcall_cache *cache)
178 int retval = (frame_relative_level (this_frame)
179 - frame_relative_level (frame_info_ptr (cache->next_bottom_frame)) - 1);
181 gdb_assert (retval >= -1);
183 return retval;
186 /* The number of virtual tail call frames in CHAIN. With no virtual tail call
187 frames the function would return 0 (but CHAIN does not exist in such
188 case). */
190 static int
191 pretended_chain_levels (struct call_site_chain *chain)
193 int chain_levels;
195 gdb_assert (chain != NULL);
197 if (chain->callers == chain->length && chain->callees == chain->length)
198 return chain->length;
200 chain_levels = chain->callers + chain->callees;
201 gdb_assert (chain_levels <= chain->length);
203 return chain_levels;
206 /* Implementation of frame_this_id_ftype. THIS_CACHE must be already
207 initialized with tailcall_cache, THIS_FRAME must be a part of THIS_CACHE.
209 Specific virtual tail call frames are tracked by INLINE_DEPTH. */
211 static void
212 tailcall_frame_this_id (const frame_info_ptr &this_frame, void **this_cache,
213 struct frame_id *this_id)
215 struct tailcall_cache *cache = (struct tailcall_cache *) *this_cache;
216 frame_info_ptr next_frame;
218 /* Tail call does not make sense for a sentinel frame. */
219 next_frame = get_next_frame (this_frame);
220 gdb_assert (next_frame != NULL);
222 *this_id = get_frame_id (next_frame);
223 (*this_id).code_addr = get_frame_pc (this_frame);
224 (*this_id).code_addr_p = true;
225 (*this_id).artificial_depth = (cache->chain_levels
226 - existing_next_levels (this_frame, cache));
227 gdb_assert ((*this_id).artificial_depth > 0);
230 /* Find PC to be unwound from THIS_FRAME. THIS_FRAME must be a part of
231 CACHE. */
233 static CORE_ADDR
234 pretend_pc (const frame_info_ptr &this_frame, struct tailcall_cache *cache)
236 int next_levels = existing_next_levels (this_frame, cache);
237 struct call_site_chain *chain = cache->chain;
239 gdb_assert (chain != NULL);
241 next_levels++;
242 gdb_assert (next_levels >= 0);
244 if (next_levels < chain->callees)
245 return chain->call_site[chain->length - next_levels - 1]->pc ();
246 next_levels -= chain->callees;
248 /* Otherwise CHAIN->CALLEES are already covered by CHAIN->CALLERS. */
249 if (chain->callees != chain->length)
251 if (next_levels < chain->callers)
252 return chain->call_site[chain->callers - next_levels - 1]->pc ();
253 next_levels -= chain->callers;
256 gdb_assert (next_levels == 0);
257 return cache->prev_pc;
260 /* Implementation of frame_prev_register_ftype. If no specific register
261 override is supplied NULL is returned (this is incompatible with
262 frame_prev_register_ftype semantics). next_bottom_frame and tail call
263 frames unwind the NULL case differently. */
265 struct value *
266 dwarf2_tailcall_prev_register_first (const frame_info_ptr &this_frame,
267 void **tailcall_cachep, int regnum)
269 struct gdbarch *this_gdbarch = get_frame_arch (this_frame);
270 struct tailcall_cache *cache = (struct tailcall_cache *) *tailcall_cachep;
271 CORE_ADDR addr;
273 if (regnum == gdbarch_pc_regnum (this_gdbarch))
274 addr = pretend_pc (this_frame, cache);
275 else if (cache->prev_sp_p && regnum == gdbarch_sp_regnum (this_gdbarch))
277 int next_levels = existing_next_levels (this_frame, cache);
279 if (next_levels == cache->chain_levels - 1)
280 addr = cache->prev_sp;
281 else
282 addr = dwarf2_frame_cfa (this_frame) - cache->entry_cfa_sp_offset;
284 else
285 return NULL;
287 return frame_unwind_got_address (this_frame, regnum, addr);
290 /* Implementation of frame_prev_register_ftype for tail call frames. Register
291 set of virtual tail call frames is assumed to be the one of the top (caller)
292 frame - assume unchanged register value for NULL from
293 dwarf2_tailcall_prev_register_first. */
295 static struct value *
296 tailcall_frame_prev_register (const frame_info_ptr &this_frame,
297 void **this_cache, int regnum)
299 struct tailcall_cache *cache = (struct tailcall_cache *) *this_cache;
300 struct value *val;
302 gdb_assert (this_frame != cache->next_bottom_frame);
304 val = dwarf2_tailcall_prev_register_first (this_frame, this_cache, regnum);
305 if (val)
306 return val;
308 return frame_unwind_got_register (this_frame, regnum, regnum);
311 /* Implementation of frame_sniffer_ftype. It will never find a new chain, use
312 dwarf2_tailcall_sniffer_first for the bottom (callee) frame. It will find
313 all the predecessing virtual tail call frames, it will return false when
314 there exist no more tail call frames in this chain. */
316 static int
317 tailcall_frame_sniffer (const struct frame_unwind *self,
318 const frame_info_ptr &this_frame, void **this_cache)
320 frame_info_ptr next_frame;
321 int next_levels;
322 struct tailcall_cache *cache;
324 if (!dwarf2_frame_unwinders_enabled_p)
325 return 0;
327 /* Inner tail call element does not make sense for a sentinel frame. */
328 next_frame = get_next_frame (this_frame);
329 if (next_frame == NULL)
330 return 0;
332 cache = cache_find (next_frame);
333 if (cache == NULL)
334 return 0;
336 cache_ref (cache);
338 next_levels = existing_next_levels (this_frame, cache);
340 /* NEXT_LEVELS is -1 only in dwarf2_tailcall_sniffer_first. */
341 gdb_assert (next_levels >= 0);
342 gdb_assert (next_levels <= cache->chain_levels);
344 if (next_levels == cache->chain_levels)
346 cache_unref (cache);
347 return 0;
350 *this_cache = cache;
351 return 1;
354 /* The initial "sniffer" whether THIS_FRAME is a bottom (callee) frame of a new
355 chain to create. Keep TAILCALL_CACHEP NULL if it did not find any chain,
356 initialize it otherwise. No tail call chain is created if there are no
357 unambiguous virtual tail call frames to report.
359 ENTRY_CFA_SP_OFFSETP is NULL if no special SP handling is possible,
360 otherwise *ENTRY_CFA_SP_OFFSETP is the number of bytes to subtract from tail
361 call frames frame base to get the SP value there - to simulate return
362 address pushed on the stack. */
364 void
365 dwarf2_tailcall_sniffer_first (const frame_info_ptr &this_frame,
366 void **tailcall_cachep,
367 const LONGEST *entry_cfa_sp_offsetp)
369 CORE_ADDR prev_pc = 0, prev_sp = 0; /* GCC warning. */
370 int prev_sp_p = 0;
371 CORE_ADDR this_pc;
372 struct gdbarch *prev_gdbarch;
373 gdb::unique_xmalloc_ptr<call_site_chain> chain;
374 struct tailcall_cache *cache;
376 gdb_assert (*tailcall_cachep == NULL);
378 /* PC may be after the function if THIS_FRAME calls noreturn function,
379 get_frame_address_in_block will decrease it by 1 in such case. */
380 this_pc = get_frame_address_in_block (this_frame);
384 int sp_regnum;
386 prev_gdbarch = frame_unwind_arch (this_frame);
388 /* Simulate frame_unwind_pc without setting this_frame->prev_pc.p. */
389 prev_pc = gdbarch_unwind_pc (prev_gdbarch, this_frame);
391 /* call_site_find_chain can throw an exception. */
392 chain = call_site_find_chain (prev_gdbarch, prev_pc, this_pc);
394 if (entry_cfa_sp_offsetp != NULL)
396 sp_regnum = gdbarch_sp_regnum (prev_gdbarch);
397 if (sp_regnum != -1)
399 prev_sp = frame_unwind_register_unsigned (this_frame, sp_regnum);
400 prev_sp_p = 1;
404 catch (const gdb_exception_error &except)
406 if (entry_values_debug)
407 exception_print (gdb_stdout, except);
409 switch (except.error)
411 case NO_ENTRY_VALUE_ERROR:
412 /* Thrown by call_site_find_chain. */
413 case MEMORY_ERROR:
414 case OPTIMIZED_OUT_ERROR:
415 case NOT_AVAILABLE_ERROR:
416 /* These can normally happen when we try to access an
417 optimized out or unavailable register, either in a
418 physical register or spilled to memory. */
419 return;
422 /* Let unexpected errors propagate. */
423 throw;
426 /* Ambiguous unwind or unambiguous unwind verified as matching. */
427 if (chain == NULL || chain->length == 0)
428 return;
430 cache = cache_new_ref1 (this_frame);
431 *tailcall_cachep = cache;
432 cache->chain = chain.release ();
433 cache->prev_pc = prev_pc;
434 cache->chain_levels = pretended_chain_levels (cache->chain);
435 cache->prev_sp_p = prev_sp_p;
436 if (cache->prev_sp_p)
438 cache->prev_sp = prev_sp;
439 cache->entry_cfa_sp_offset = *entry_cfa_sp_offsetp;
441 gdb_assert (cache->chain_levels > 0);
444 /* Implementation of frame_dealloc_cache_ftype. It can be called even for the
445 bottom chain frame from dwarf2_frame_dealloc_cache which is not a real
446 TAILCALL_FRAME. */
448 static void
449 tailcall_frame_dealloc_cache (frame_info *self, void *this_cache)
451 struct tailcall_cache *cache = (struct tailcall_cache *) this_cache;
453 cache_unref (cache);
456 /* Implementation of frame_prev_arch_ftype. We assume all the virtual tail
457 call frames have gdbarch of the bottom (callee) frame. */
459 static struct gdbarch *
460 tailcall_frame_prev_arch (const frame_info_ptr &this_frame,
461 void **this_prologue_cache)
463 struct tailcall_cache *cache = (struct tailcall_cache *) *this_prologue_cache;
465 return get_frame_arch (frame_info_ptr (cache->next_bottom_frame));
468 /* Virtual tail call frame unwinder if dwarf2_tailcall_sniffer_first finds
469 a chain to create. */
471 const struct frame_unwind dwarf2_tailcall_frame_unwind =
473 "dwarf2 tailcall",
474 TAILCALL_FRAME,
475 default_frame_unwind_stop_reason,
476 tailcall_frame_this_id,
477 tailcall_frame_prev_register,
478 NULL,
479 tailcall_frame_sniffer,
480 tailcall_frame_dealloc_cache,
481 tailcall_frame_prev_arch
484 void _initialize_tailcall_frame ();
485 void
486 _initialize_tailcall_frame ()
488 cache_htab = htab_create_alloc (50, cache_hash, cache_eq, NULL, xcalloc,
489 xfree);