]>
Commit | Line | Data |
---|---|---|
bd5635a1 RP |
1 | /* Get info from stack frames; |
2 | convert between frames, blocks, functions and pc values. | |
0e2c2c1e | 3 | Copyright 1986, 1987, 1988, 1989, 1991, 1994 Free Software Foundation, Inc. |
bd5635a1 RP |
4 | |
5 | This file is part of GDB. | |
6 | ||
5259796b | 7 | This program is free software; you can redistribute it and/or modify |
bd5635a1 | 8 | it under the terms of the GNU General Public License as published by |
5259796b JG |
9 | the Free Software Foundation; either version 2 of the License, or |
10 | (at your option) any later version. | |
bd5635a1 | 11 | |
5259796b | 12 | This program is distributed in the hope that it will be useful, |
bd5635a1 RP |
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. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
5259796b | 18 | along with this program; if not, write to the Free Software |
0e2c2c1e | 19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
bd5635a1 RP |
20 | |
21 | #include "defs.h" | |
bd5635a1 | 22 | #include "symtab.h" |
23a8e291 JK |
23 | #include "bfd.h" |
24 | #include "symfile.h" | |
25 | #include "objfiles.h" | |
bd5635a1 RP |
26 | #include "frame.h" |
27 | #include "gdbcore.h" | |
28 | #include "value.h" /* for read_register */ | |
29 | #include "target.h" /* for target_has_stack */ | |
23a8e291 | 30 | #include "inferior.h" /* for read_pc */ |
16726dd1 | 31 | #include "annotate.h" |
bd5635a1 | 32 | |
23a8e291 | 33 | /* Is ADDR inside the startup file? Note that if your machine |
bd5635a1 RP |
34 | has a way to detect the bottom of the stack, there is no need |
35 | to call this function from FRAME_CHAIN_VALID; the reason for | |
36 | doing so is that some machines have no way of detecting bottom | |
23a8e291 JK |
37 | of stack. |
38 | ||
39 | A PC of zero is always considered to be the bottom of the stack. */ | |
40 | ||
bd5635a1 | 41 | int |
23a8e291 | 42 | inside_entry_file (addr) |
bd5635a1 RP |
43 | CORE_ADDR addr; |
44 | { | |
23a8e291 JK |
45 | if (addr == 0) |
46 | return 1; | |
47 | if (symfile_objfile == 0) | |
48 | return 0; | |
cef4c2e7 PS |
49 | #if CALL_DUMMY_LOCATION == AT_ENTRY_POINT |
50 | /* Do not stop backtracing if the pc is in the call dummy | |
51 | at the entry point. */ | |
52 | if (PC_IN_CALL_DUMMY (addr, 0, 0)) | |
53 | return 0; | |
54 | #endif | |
23a8e291 JK |
55 | return (addr >= symfile_objfile -> ei.entry_file_lowpc && |
56 | addr < symfile_objfile -> ei.entry_file_highpc); | |
bd5635a1 RP |
57 | } |
58 | ||
e140f1da JG |
59 | /* Test a specified PC value to see if it is in the range of addresses |
60 | that correspond to the main() function. See comments above for why | |
61 | we might want to do this. | |
62 | ||
23a8e291 JK |
63 | Typically called from FRAME_CHAIN_VALID. |
64 | ||
65 | A PC of zero is always considered to be the bottom of the stack. */ | |
e140f1da JG |
66 | |
67 | int | |
23a8e291 | 68 | inside_main_func (pc) |
e140f1da JG |
69 | CORE_ADDR pc; |
70 | { | |
0e2c2c1e | 71 | struct symbol *mainsym; |
23a8e291 JK |
72 | if (pc == 0) |
73 | return 1; | |
74 | if (symfile_objfile == 0) | |
75 | return 0; | |
0e2c2c1e KH |
76 | |
77 | if (symfile_objfile -> ei.main_func_lowpc == 0 && | |
56c423fe | 78 | symfile_objfile -> ei.main_func_highpc == 0) |
0e2c2c1e KH |
79 | { |
80 | mainsym = lookup_symbol ("main", NULL, VAR_NAMESPACE, NULL, NULL); | |
81 | if (mainsym && SYMBOL_CLASS(mainsym) == LOC_BLOCK) | |
82 | { | |
83 | symfile_objfile->ei.main_func_lowpc = BLOCK_START (SYMBOL_BLOCK_VALUE (mainsym)); | |
84 | symfile_objfile->ei.main_func_highpc = BLOCK_END (SYMBOL_BLOCK_VALUE (mainsym)); | |
85 | } | |
86 | ||
87 | } | |
23a8e291 JK |
88 | return (symfile_objfile -> ei.main_func_lowpc <= pc && |
89 | symfile_objfile -> ei.main_func_highpc > pc); | |
e140f1da JG |
90 | } |
91 | ||
92 | /* Test a specified PC value to see if it is in the range of addresses | |
23a8e291 JK |
93 | that correspond to the process entry point function. See comments |
94 | in objfiles.h for why we might want to do this. | |
95 | ||
96 | Typically called from FRAME_CHAIN_VALID. | |
e140f1da | 97 | |
23a8e291 | 98 | A PC of zero is always considered to be the bottom of the stack. */ |
e140f1da JG |
99 | |
100 | int | |
23a8e291 | 101 | inside_entry_func (pc) |
e140f1da JG |
102 | CORE_ADDR pc; |
103 | { | |
23a8e291 JK |
104 | if (pc == 0) |
105 | return 1; | |
106 | if (symfile_objfile == 0) | |
107 | return 0; | |
cef4c2e7 PS |
108 | #if CALL_DUMMY_LOCATION == AT_ENTRY_POINT |
109 | /* Do not stop backtracing if the pc is in the call dummy | |
110 | at the entry point. */ | |
111 | if (PC_IN_CALL_DUMMY (pc, 0, 0)) | |
112 | return 0; | |
113 | #endif | |
23a8e291 JK |
114 | return (symfile_objfile -> ei.entry_func_lowpc <= pc && |
115 | symfile_objfile -> ei.entry_func_highpc > pc); | |
e140f1da JG |
116 | } |
117 | ||
0e2c2c1e KH |
118 | /* Info about the innermost stack frame (contents of FP register) */ |
119 | ||
120 | static struct frame_info *current_frame; | |
bd5635a1 | 121 | |
0e2c2c1e KH |
122 | /* Cache for frame addresses already read by gdb. Valid only while |
123 | inferior is stopped. Control variables for the frame cache should | |
124 | be local to this module. */ | |
bd5635a1 | 125 | |
bd5635a1 RP |
126 | struct obstack frame_cache_obstack; |
127 | ||
128 | /* Return the innermost (currently executing) stack frame. */ | |
129 | ||
0e2c2c1e | 130 | struct frame_info * |
bd5635a1 RP |
131 | get_current_frame () |
132 | { | |
16726dd1 JK |
133 | if (current_frame == NULL) |
134 | { | |
135 | if (target_has_stack) | |
136 | current_frame = create_new_frame (read_fp (), read_pc ()); | |
137 | else | |
138 | error ("No stack."); | |
139 | } | |
bd5635a1 RP |
140 | return current_frame; |
141 | } | |
142 | ||
143 | void | |
144 | set_current_frame (frame) | |
0e2c2c1e | 145 | struct frame_info *frame; |
bd5635a1 RP |
146 | { |
147 | current_frame = frame; | |
148 | } | |
149 | ||
16726dd1 JK |
150 | /* Create an arbitrary (i.e. address specified by user) or innermost frame. |
151 | Always returns a non-NULL value. */ | |
152 | ||
0e2c2c1e | 153 | struct frame_info * |
bd5635a1 | 154 | create_new_frame (addr, pc) |
0e2c2c1e | 155 | CORE_ADDR addr; |
bd5635a1 RP |
156 | CORE_ADDR pc; |
157 | { | |
0e2c2c1e | 158 | struct frame_info *fi; |
d541211d | 159 | char *name; |
bd5635a1 | 160 | |
0e2c2c1e | 161 | fi = (struct frame_info *) |
bd5635a1 RP |
162 | obstack_alloc (&frame_cache_obstack, |
163 | sizeof (struct frame_info)); | |
164 | ||
165 | /* Arbitrary frame */ | |
0e2c2c1e KH |
166 | fi->next = NULL; |
167 | fi->prev = NULL; | |
168 | fi->frame = addr; | |
169 | fi->pc = pc; | |
d541211d | 170 | find_pc_partial_function (pc, &name, (CORE_ADDR *)NULL,(CORE_ADDR *)NULL); |
0e2c2c1e | 171 | fi->signal_handler_caller = IN_SIGTRAMP (fi->pc, name); |
bd5635a1 RP |
172 | |
173 | #ifdef INIT_EXTRA_FRAME_INFO | |
0e2c2c1e | 174 | INIT_EXTRA_FRAME_INFO (0, fi); |
bd5635a1 RP |
175 | #endif |
176 | ||
0e2c2c1e | 177 | return fi; |
bd5635a1 RP |
178 | } |
179 | ||
0e2c2c1e KH |
180 | /* Return the frame that called FI. |
181 | If FI is the original frame (it has no caller), return 0. */ | |
bd5635a1 | 182 | |
0e2c2c1e | 183 | struct frame_info * |
bd5635a1 | 184 | get_prev_frame (frame) |
0e2c2c1e | 185 | struct frame_info *frame; |
bd5635a1 | 186 | { |
bd5635a1 RP |
187 | return get_prev_frame_info (frame); |
188 | } | |
189 | ||
0e2c2c1e | 190 | /* Return the frame that FRAME calls (NULL if FRAME is the innermost |
bd5635a1 RP |
191 | frame). */ |
192 | ||
0e2c2c1e | 193 | struct frame_info * |
bd5635a1 | 194 | get_next_frame (frame) |
0e2c2c1e | 195 | struct frame_info *frame; |
bd5635a1 | 196 | { |
bd5635a1 RP |
197 | return frame->next; |
198 | } | |
199 | ||
0e2c2c1e KH |
200 | /* Flush the entire frame cache. */ |
201 | ||
bd5635a1 RP |
202 | void |
203 | flush_cached_frames () | |
204 | { | |
205 | /* Since we can't really be sure what the first object allocated was */ | |
206 | obstack_free (&frame_cache_obstack, 0); | |
207 | obstack_init (&frame_cache_obstack); | |
208 | ||
0e2c2c1e KH |
209 | current_frame = NULL; /* Invalidate cache */ |
210 | select_frame (NULL, -1); | |
16726dd1 | 211 | annotate_frames_invalid (); |
bd5635a1 RP |
212 | } |
213 | ||
2403f49b | 214 | /* Flush the frame cache, and start a new one if necessary. */ |
16726dd1 | 215 | |
2403f49b JK |
216 | void |
217 | reinit_frame_cache () | |
218 | { | |
2403f49b | 219 | flush_cached_frames (); |
0e2c2c1e KH |
220 | |
221 | /* FIXME: The inferior_pid test is wrong if there is a corefile. */ | |
16726dd1 | 222 | if (inferior_pid != 0) |
2289e1c3 | 223 | { |
2289e1c3 JK |
224 | select_frame (get_current_frame (), 0); |
225 | } | |
bd5635a1 RP |
226 | } |
227 | ||
228 | /* If a machine allows frameless functions, it should define a macro | |
229 | FRAMELESS_FUNCTION_INVOCATION(FI, FRAMELESS) in param.h. FI is the struct | |
230 | frame_info for the frame, and FRAMELESS should be set to nonzero | |
231 | if it represents a frameless function invocation. */ | |
232 | ||
23a8e291 | 233 | /* Return nonzero if the function for this frame lacks a prologue. Many |
bd5635a1 RP |
234 | machines can define FRAMELESS_FUNCTION_INVOCATION to just call this |
235 | function. */ | |
236 | ||
237 | int | |
238 | frameless_look_for_prologue (frame) | |
0e2c2c1e | 239 | struct frame_info *frame; |
bd5635a1 RP |
240 | { |
241 | CORE_ADDR func_start, after_prologue; | |
0e2c2c1e | 242 | func_start = (get_pc_function_start (frame->pc) + FUNCTION_START_OFFSET); |
bd5635a1 RP |
243 | if (func_start) |
244 | { | |
245 | after_prologue = func_start; | |
5259796b JG |
246 | #ifdef SKIP_PROLOGUE_FRAMELESS_P |
247 | /* This is faster, since only care whether there *is* a prologue, | |
248 | not how long it is. */ | |
249 | SKIP_PROLOGUE_FRAMELESS_P (after_prologue); | |
250 | #else | |
bd5635a1 | 251 | SKIP_PROLOGUE (after_prologue); |
5259796b | 252 | #endif |
bd5635a1 RP |
253 | return after_prologue == func_start; |
254 | } | |
255 | else | |
256 | /* If we can't find the start of the function, we don't really | |
257 | know whether the function is frameless, but we should be able | |
258 | to get a reasonable (i.e. best we can do under the | |
259 | circumstances) backtrace by saying that it isn't. */ | |
260 | return 0; | |
261 | } | |
262 | ||
e140f1da JG |
263 | /* Default a few macros that people seldom redefine. */ |
264 | ||
bd5635a1 RP |
265 | #if !defined (INIT_FRAME_PC) |
266 | #define INIT_FRAME_PC(fromleaf, prev) \ | |
267 | prev->pc = (fromleaf ? SAVED_PC_AFTER_CALL (prev->next) : \ | |
268 | prev->next ? FRAME_SAVED_PC (prev->next) : read_pc ()); | |
269 | #endif | |
270 | ||
e140f1da JG |
271 | #ifndef FRAME_CHAIN_COMBINE |
272 | #define FRAME_CHAIN_COMBINE(chain, thisframe) (chain) | |
273 | #endif | |
274 | ||
bd5635a1 RP |
275 | /* Return a structure containing various interesting information |
276 | about the frame that called NEXT_FRAME. Returns NULL | |
277 | if there is no such frame. */ | |
278 | ||
279 | struct frame_info * | |
280 | get_prev_frame_info (next_frame) | |
0e2c2c1e | 281 | struct frame_info *next_frame; |
bd5635a1 | 282 | { |
0e2c2c1e | 283 | CORE_ADDR address = 0; |
bd5635a1 RP |
284 | struct frame_info *prev; |
285 | int fromleaf = 0; | |
d541211d | 286 | char *name; |
bd5635a1 RP |
287 | |
288 | /* If the requested entry is in the cache, return it. | |
289 | Otherwise, figure out what the address should be for the entry | |
290 | we're about to add to the cache. */ | |
291 | ||
292 | if (!next_frame) | |
293 | { | |
9e837b37 PS |
294 | #if 0 |
295 | /* This screws value_of_variable, which just wants a nice clean | |
296 | NULL return from block_innermost_frame if there are no frames. | |
297 | I don't think I've ever seen this message happen otherwise. | |
298 | And returning NULL here is a perfectly legitimate thing to do. */ | |
bd5635a1 RP |
299 | if (!current_frame) |
300 | { | |
301 | error ("You haven't set up a process's stack to examine."); | |
302 | } | |
9e837b37 | 303 | #endif |
bd5635a1 RP |
304 | |
305 | return current_frame; | |
306 | } | |
307 | ||
308 | /* If we have the prev one, return it */ | |
309 | if (next_frame->prev) | |
310 | return next_frame->prev; | |
311 | ||
312 | /* On some machines it is possible to call a function without | |
313 | setting up a stack frame for it. On these machines, we | |
314 | define this macro to take two args; a frameinfo pointer | |
315 | identifying a frame and a variable to set or clear if it is | |
316 | or isn't leafless. */ | |
317 | #ifdef FRAMELESS_FUNCTION_INVOCATION | |
318 | /* Still don't want to worry about this except on the innermost | |
319 | frame. This macro will set FROMLEAF if NEXT_FRAME is a | |
320 | frameless function invocation. */ | |
321 | if (!(next_frame->next)) | |
322 | { | |
323 | FRAMELESS_FUNCTION_INVOCATION (next_frame, fromleaf); | |
324 | if (fromleaf) | |
0e2c2c1e | 325 | address = FRAME_FP (next_frame); |
bd5635a1 RP |
326 | } |
327 | #endif | |
328 | ||
329 | if (!fromleaf) | |
330 | { | |
331 | /* Two macros defined in tm.h specify the machine-dependent | |
332 | actions to be performed here. | |
333 | First, get the frame's chain-pointer. | |
334 | If that is zero, the frame is the outermost frame or a leaf | |
335 | called by the outermost frame. This means that if start | |
336 | calls main without a frame, we'll return 0 (which is fine | |
337 | anyway). | |
338 | ||
339 | Nope; there's a problem. This also returns when the current | |
340 | routine is a leaf of main. This is unacceptable. We move | |
341 | this to after the ffi test; I'd rather have backtraces from | |
342 | start go curfluy than have an abort called from main not show | |
343 | main. */ | |
344 | address = FRAME_CHAIN (next_frame); | |
345 | if (!FRAME_CHAIN_VALID (address, next_frame)) | |
346 | return 0; | |
347 | address = FRAME_CHAIN_COMBINE (address, next_frame); | |
348 | } | |
e140f1da JG |
349 | if (address == 0) |
350 | return 0; | |
bd5635a1 RP |
351 | |
352 | prev = (struct frame_info *) | |
353 | obstack_alloc (&frame_cache_obstack, | |
354 | sizeof (struct frame_info)); | |
355 | ||
356 | if (next_frame) | |
357 | next_frame->prev = prev; | |
358 | prev->next = next_frame; | |
359 | prev->prev = (struct frame_info *) 0; | |
360 | prev->frame = address; | |
23a8e291 JK |
361 | prev->signal_handler_caller = 0; |
362 | ||
363 | /* This change should not be needed, FIXME! We should | |
364 | determine whether any targets *need* INIT_FRAME_PC to happen | |
365 | after INIT_EXTRA_FRAME_INFO and come up with a simple way to | |
366 | express what goes on here. | |
367 | ||
368 | INIT_EXTRA_FRAME_INFO is called from two places: create_new_frame | |
369 | (where the PC is already set up) and here (where it isn't). | |
370 | INIT_FRAME_PC is only called from here, always after | |
371 | INIT_EXTRA_FRAME_INFO. | |
372 | ||
373 | The catch is the MIPS, where INIT_EXTRA_FRAME_INFO requires the PC | |
374 | value (which hasn't been set yet). Some other machines appear to | |
375 | require INIT_EXTRA_FRAME_INFO before they can do INIT_FRAME_PC. Phoo. | |
376 | ||
377 | We shouldn't need INIT_FRAME_PC_FIRST to add more complication to | |
378 | an already overcomplicated part of GDB. [email protected], 15Sep92. | |
379 | ||
0e2c2c1e KH |
380 | Assuming that some machines need INIT_FRAME_PC after |
381 | INIT_EXTRA_FRAME_INFO, one possible scheme: | |
23a8e291 JK |
382 | |
383 | SETUP_INNERMOST_FRAME() | |
384 | Default version is just create_new_frame (read_fp ()), | |
385 | read_pc ()). Machines with extra frame info would do that (or the | |
386 | local equivalent) and then set the extra fields. | |
387 | SETUP_ARBITRARY_FRAME(argc, argv) | |
388 | Only change here is that create_new_frame would no longer init extra | |
389 | frame info; SETUP_ARBITRARY_FRAME would have to do that. | |
390 | INIT_PREV_FRAME(fromleaf, prev) | |
9e837b37 PS |
391 | Replace INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC. This should |
392 | also return a flag saying whether to keep the new frame, or | |
393 | whether to discard it, because on some machines (e.g. mips) it | |
394 | is really awkward to have FRAME_CHAIN_VALID called *before* | |
395 | INIT_EXTRA_FRAME_INFO (there is no good way to get information | |
396 | deduced in FRAME_CHAIN_VALID into the extra fields of the new frame). | |
23a8e291 JK |
397 | std_frame_pc(fromleaf, prev) |
398 | This is the default setting for INIT_PREV_FRAME. It just does what | |
399 | the default INIT_FRAME_PC does. Some machines will call it from | |
400 | INIT_PREV_FRAME (either at the beginning, the end, or in the middle). | |
401 | Some machines won't use it. | |
0e2c2c1e | 402 | [email protected], 13Apr93, 31Jan94, 14Dec94. */ |
23a8e291 JK |
403 | |
404 | #ifdef INIT_FRAME_PC_FIRST | |
405 | INIT_FRAME_PC_FIRST (fromleaf, prev); | |
406 | #endif | |
bd5635a1 RP |
407 | |
408 | #ifdef INIT_EXTRA_FRAME_INFO | |
e140f1da | 409 | INIT_EXTRA_FRAME_INFO(fromleaf, prev); |
bd5635a1 RP |
410 | #endif |
411 | ||
412 | /* This entry is in the frame queue now, which is good since | |
9e837b37 | 413 | FRAME_SAVED_PC may use that queue to figure out its value |
e140f1da | 414 | (see tm-sparc.h). We want the pc saved in the inferior frame. */ |
bd5635a1 RP |
415 | INIT_FRAME_PC(fromleaf, prev); |
416 | ||
9e837b37 PS |
417 | /* If ->frame and ->pc are unchanged, we are in the process of getting |
418 | ourselves into an infinite backtrace. Some architectures check this | |
419 | in FRAME_CHAIN or thereabouts, but it seems like there is no reason | |
420 | this can't be an architecture-independent check. */ | |
421 | if (next_frame != NULL) | |
422 | { | |
423 | if (prev->frame == next_frame->frame | |
424 | && prev->pc == next_frame->pc) | |
425 | { | |
426 | next_frame->prev = NULL; | |
427 | obstack_free (&frame_cache_obstack, prev); | |
428 | return NULL; | |
429 | } | |
430 | } | |
431 | ||
d541211d PS |
432 | find_pc_partial_function (prev->pc, &name, |
433 | (CORE_ADDR *)NULL,(CORE_ADDR *)NULL); | |
434 | if (IN_SIGTRAMP (prev->pc, name)) | |
23a8e291 JK |
435 | prev->signal_handler_caller = 1; |
436 | ||
bd5635a1 RP |
437 | return prev; |
438 | } | |
439 | ||
440 | CORE_ADDR | |
441 | get_frame_pc (frame) | |
0e2c2c1e | 442 | struct frame_info *frame; |
bd5635a1 | 443 | { |
0e2c2c1e | 444 | return frame->pc; |
bd5635a1 RP |
445 | } |
446 | ||
447 | #if defined (FRAME_FIND_SAVED_REGS) | |
448 | /* Find the addresses in which registers are saved in FRAME. */ | |
449 | ||
450 | void | |
0e2c2c1e KH |
451 | get_frame_saved_regs (frame, saved_regs_addr) |
452 | struct frame_info *frame; | |
bd5635a1 RP |
453 | struct frame_saved_regs *saved_regs_addr; |
454 | { | |
0e2c2c1e | 455 | FRAME_FIND_SAVED_REGS (frame, *saved_regs_addr); |
bd5635a1 RP |
456 | } |
457 | #endif | |
458 | ||
459 | /* Return the innermost lexical block in execution | |
460 | in a specified stack frame. The frame address is assumed valid. */ | |
461 | ||
462 | struct block * | |
463 | get_frame_block (frame) | |
0e2c2c1e | 464 | struct frame_info *frame; |
bd5635a1 | 465 | { |
bd5635a1 RP |
466 | CORE_ADDR pc; |
467 | ||
0e2c2c1e KH |
468 | pc = frame->pc; |
469 | if (frame->next != 0 && frame->next->signal_handler_caller == 0) | |
747a6329 PS |
470 | /* We are not in the innermost frame and we were not interrupted |
471 | by a signal. We need to subtract one to get the correct block, | |
472 | in case the call instruction was the last instruction of the block. | |
473 | If there are any machines on which the saved pc does not point to | |
0e2c2c1e | 474 | after the call insn, we probably want to make frame->pc point after |
747a6329 | 475 | the call insn anyway. */ |
bd5635a1 RP |
476 | --pc; |
477 | return block_for_pc (pc); | |
478 | } | |
479 | ||
480 | struct block * | |
481 | get_current_block () | |
482 | { | |
483 | return block_for_pc (read_pc ()); | |
484 | } | |
485 | ||
486 | CORE_ADDR | |
487 | get_pc_function_start (pc) | |
488 | CORE_ADDR pc; | |
489 | { | |
23a8e291 | 490 | register struct block *bl; |
bd5635a1 | 491 | register struct symbol *symbol; |
23a8e291 JK |
492 | register struct minimal_symbol *msymbol; |
493 | CORE_ADDR fstart; | |
494 | ||
495 | if ((bl = block_for_pc (pc)) != NULL && | |
496 | (symbol = block_function (bl)) != NULL) | |
bd5635a1 | 497 | { |
23a8e291 JK |
498 | bl = SYMBOL_BLOCK_VALUE (symbol); |
499 | fstart = BLOCK_START (bl); | |
bd5635a1 | 500 | } |
23a8e291 JK |
501 | else if ((msymbol = lookup_minimal_symbol_by_pc (pc)) != NULL) |
502 | { | |
503 | fstart = SYMBOL_VALUE_ADDRESS (msymbol); | |
504 | } | |
505 | else | |
506 | { | |
507 | fstart = 0; | |
508 | } | |
509 | return (fstart); | |
bd5635a1 RP |
510 | } |
511 | ||
512 | /* Return the symbol for the function executing in frame FRAME. */ | |
513 | ||
514 | struct symbol * | |
515 | get_frame_function (frame) | |
0e2c2c1e | 516 | struct frame_info *frame; |
bd5635a1 RP |
517 | { |
518 | register struct block *bl = get_frame_block (frame); | |
519 | if (bl == 0) | |
520 | return 0; | |
521 | return block_function (bl); | |
522 | } | |
523 | \f | |
524 | /* Return the blockvector immediately containing the innermost lexical block | |
525 | containing the specified pc value, or 0 if there is none. | |
526 | PINDEX is a pointer to the index value of the block. If PINDEX | |
527 | is NULL, we don't pass this information back to the caller. */ | |
528 | ||
529 | struct blockvector * | |
530 | blockvector_for_pc (pc, pindex) | |
531 | register CORE_ADDR pc; | |
532 | int *pindex; | |
533 | { | |
534 | register struct block *b; | |
535 | register int bot, top, half; | |
536 | register struct symtab *s; | |
537 | struct blockvector *bl; | |
538 | ||
539 | /* First search all symtabs for one whose file contains our pc */ | |
540 | s = find_pc_symtab (pc); | |
541 | if (s == 0) | |
542 | return 0; | |
543 | ||
544 | bl = BLOCKVECTOR (s); | |
545 | b = BLOCKVECTOR_BLOCK (bl, 0); | |
546 | ||
547 | /* Then search that symtab for the smallest block that wins. */ | |
548 | /* Use binary search to find the last block that starts before PC. */ | |
549 | ||
550 | bot = 0; | |
551 | top = BLOCKVECTOR_NBLOCKS (bl); | |
552 | ||
553 | while (top - bot > 1) | |
554 | { | |
555 | half = (top - bot + 1) >> 1; | |
556 | b = BLOCKVECTOR_BLOCK (bl, bot + half); | |
557 | if (BLOCK_START (b) <= pc) | |
558 | bot += half; | |
559 | else | |
560 | top = bot + half; | |
561 | } | |
562 | ||
563 | /* Now search backward for a block that ends after PC. */ | |
564 | ||
565 | while (bot >= 0) | |
566 | { | |
567 | b = BLOCKVECTOR_BLOCK (bl, bot); | |
568 | if (BLOCK_END (b) > pc) | |
569 | { | |
570 | if (pindex) | |
571 | *pindex = bot; | |
572 | return bl; | |
573 | } | |
574 | bot--; | |
575 | } | |
576 | ||
577 | return 0; | |
578 | } | |
579 | ||
580 | /* Return the innermost lexical block containing the specified pc value, | |
581 | or 0 if there is none. */ | |
582 | ||
583 | struct block * | |
584 | block_for_pc (pc) | |
585 | register CORE_ADDR pc; | |
586 | { | |
587 | register struct blockvector *bl; | |
588 | int index; | |
589 | ||
590 | bl = blockvector_for_pc (pc, &index); | |
591 | if (bl) | |
592 | return BLOCKVECTOR_BLOCK (bl, index); | |
593 | return 0; | |
594 | } | |
595 | ||
596 | /* Return the function containing pc value PC. | |
597 | Returns 0 if function is not known. */ | |
598 | ||
599 | struct symbol * | |
600 | find_pc_function (pc) | |
601 | CORE_ADDR pc; | |
602 | { | |
603 | register struct block *b = block_for_pc (pc); | |
604 | if (b == 0) | |
605 | return 0; | |
606 | return block_function (b); | |
607 | } | |
608 | ||
609 | /* These variables are used to cache the most recent result | |
610 | * of find_pc_partial_function. */ | |
611 | ||
612 | static CORE_ADDR cache_pc_function_low = 0; | |
613 | static CORE_ADDR cache_pc_function_high = 0; | |
614 | static char *cache_pc_function_name = 0; | |
615 | ||
616 | /* Clear cache, e.g. when symbol table is discarded. */ | |
617 | ||
618 | void | |
619 | clear_pc_function_cache() | |
620 | { | |
621 | cache_pc_function_low = 0; | |
622 | cache_pc_function_high = 0; | |
623 | cache_pc_function_name = (char *)0; | |
624 | } | |
625 | ||
d541211d PS |
626 | /* Finds the "function" (text symbol) that is smaller than PC but |
627 | greatest of all of the potential text symbols. Sets *NAME and/or | |
628 | *ADDRESS conditionally if that pointer is non-null. If ENDADDR is | |
629 | non-null, then set *ENDADDR to be the end of the function | |
630 | (exclusive), but passing ENDADDR as non-null means that the | |
631 | function might cause symbols to be read. This function either | |
632 | succeeds or fails (not halfway succeeds). If it succeeds, it sets | |
633 | *NAME, *ADDRESS, and *ENDADDR to real information and returns 1. | |
634 | If it fails, it sets *NAME, *ADDRESS, and *ENDADDR to zero | |
635 | and returns 0. */ | |
bd5635a1 RP |
636 | |
637 | int | |
d541211d | 638 | find_pc_partial_function (pc, name, address, endaddr) |
bd5635a1 RP |
639 | CORE_ADDR pc; |
640 | char **name; | |
641 | CORE_ADDR *address; | |
d541211d | 642 | CORE_ADDR *endaddr; |
bd5635a1 RP |
643 | { |
644 | struct partial_symtab *pst; | |
645 | struct symbol *f; | |
23a8e291 | 646 | struct minimal_symbol *msymbol; |
bd5635a1 | 647 | struct partial_symbol *psb; |
981a3309 | 648 | struct obj_section *sec; |
bd5635a1 RP |
649 | |
650 | if (pc >= cache_pc_function_low && pc < cache_pc_function_high) | |
d541211d PS |
651 | goto return_cached_value; |
652 | ||
653 | /* If sigtramp is in the u area, it counts as a function (especially | |
654 | important for step_1). */ | |
655 | #if defined SIGTRAMP_START | |
656 | if (IN_SIGTRAMP (pc, (char *)NULL)) | |
bd5635a1 | 657 | { |
d541211d PS |
658 | cache_pc_function_low = SIGTRAMP_START; |
659 | cache_pc_function_high = SIGTRAMP_END; | |
660 | cache_pc_function_name = "<sigtramp>"; | |
661 | ||
662 | goto return_cached_value; | |
bd5635a1 | 663 | } |
d541211d | 664 | #endif |
bd5635a1 | 665 | |
d541211d | 666 | msymbol = lookup_minimal_symbol_by_pc (pc); |
bd5635a1 RP |
667 | pst = find_pc_psymtab (pc); |
668 | if (pst) | |
669 | { | |
d541211d PS |
670 | /* Need to read the symbols to get a good value for the end address. */ |
671 | if (endaddr != NULL && !pst->readin) | |
2f1c7c3f JK |
672 | { |
673 | /* Need to get the terminal in case symbol-reading produces | |
674 | output. */ | |
675 | target_terminal_ours_for_output (); | |
676 | PSYMTAB_TO_SYMTAB (pst); | |
677 | } | |
d541211d | 678 | |
bd5635a1 RP |
679 | if (pst->readin) |
680 | { | |
d541211d PS |
681 | /* Checking whether the msymbol has a larger value is for the |
682 | "pathological" case mentioned in print_frame_info. */ | |
bd5635a1 | 683 | f = find_pc_function (pc); |
d541211d PS |
684 | if (f != NULL |
685 | && (msymbol == NULL | |
686 | || (BLOCK_START (SYMBOL_BLOCK_VALUE (f)) | |
687 | >= SYMBOL_VALUE_ADDRESS (msymbol)))) | |
bd5635a1 | 688 | { |
d541211d PS |
689 | cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f)); |
690 | cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f)); | |
691 | cache_pc_function_name = SYMBOL_NAME (f); | |
692 | goto return_cached_value; | |
bd5635a1 | 693 | } |
bd5635a1 | 694 | } |
cef4c2e7 | 695 | else |
bd5635a1 | 696 | { |
cef4c2e7 PS |
697 | /* Now that static symbols go in the minimal symbol table, perhaps |
698 | we could just ignore the partial symbols. But at least for now | |
699 | we use the partial or minimal symbol, whichever is larger. */ | |
700 | psb = find_pc_psymbol (pst, pc); | |
701 | ||
702 | if (psb | |
703 | && (msymbol == NULL || | |
704 | (SYMBOL_VALUE_ADDRESS (psb) | |
705 | >= SYMBOL_VALUE_ADDRESS (msymbol)))) | |
706 | { | |
707 | /* This case isn't being cached currently. */ | |
708 | if (address) | |
709 | *address = SYMBOL_VALUE_ADDRESS (psb); | |
710 | if (name) | |
711 | *name = SYMBOL_NAME (psb); | |
712 | /* endaddr non-NULL can't happen here. */ | |
713 | return 1; | |
714 | } | |
bd5635a1 RP |
715 | } |
716 | } | |
d541211d | 717 | |
981a3309 SG |
718 | /* Not in the normal symbol tables, see if the pc is in a known section. |
719 | If it's not, then give up. This ensures that anything beyond the end | |
720 | of the text seg doesn't appear to be part of the last function in the | |
721 | text segment. */ | |
722 | ||
723 | sec = find_pc_section (pc); | |
724 | ||
725 | if (!sec) | |
726 | msymbol = NULL; | |
727 | ||
d541211d PS |
728 | /* Must be in the minimal symbol table. */ |
729 | if (msymbol == NULL) | |
bd5635a1 | 730 | { |
d541211d PS |
731 | /* No available symbol. */ |
732 | if (name != NULL) | |
733 | *name = 0; | |
734 | if (address != NULL) | |
735 | *address = 0; | |
736 | if (endaddr != NULL) | |
737 | *endaddr = 0; | |
738 | return 0; | |
bd5635a1 RP |
739 | } |
740 | ||
0e2c2c1e KH |
741 | /* See if we're in a transfer table for Sun shared libs. |
742 | ||
743 | Note the hack for Sun shared library transfer tables creates | |
744 | problems for single stepping through the return path from a shared | |
745 | library call if the return path includes trampoline code. | |
981a3309 | 746 | |
0e2c2c1e KH |
747 | I don't really understand the reasoning behind the magic handling |
748 | for mst_trampoline symbols. */ | |
749 | ||
750 | #ifdef INHIBIT_SUNSOLIB_TRANSFER_TABLE_HACK | |
751 | cache_pc_function_low = SYMBOL_VALUE_ADDRESS (msymbol); | |
752 | #else | |
9e837b37 | 753 | if (msymbol -> type == mst_text || msymbol -> type == mst_file_text) |
d541211d PS |
754 | cache_pc_function_low = SYMBOL_VALUE_ADDRESS (msymbol); |
755 | else | |
756 | /* It is a transfer table for Sun shared libraries. */ | |
757 | cache_pc_function_low = pc - FUNCTION_START_OFFSET; | |
0e2c2c1e | 758 | #endif |
981a3309 | 759 | |
23a8e291 | 760 | cache_pc_function_name = SYMBOL_NAME (msymbol); |
d541211d | 761 | |
981a3309 SG |
762 | /* Use the lesser of the next minimal symbol, or the end of the section, as |
763 | the end of the function. */ | |
764 | ||
765 | if (SYMBOL_NAME (msymbol + 1) != NULL | |
766 | && SYMBOL_VALUE_ADDRESS (msymbol + 1) < sec->endaddr) | |
23a8e291 | 767 | cache_pc_function_high = SYMBOL_VALUE_ADDRESS (msymbol + 1); |
bd5635a1 | 768 | else |
981a3309 SG |
769 | /* We got the start address from the last msymbol in the objfile. |
770 | So the end address is the end of the section. */ | |
771 | cache_pc_function_high = sec->endaddr; | |
d541211d PS |
772 | |
773 | return_cached_value: | |
bd5635a1 RP |
774 | if (address) |
775 | *address = cache_pc_function_low; | |
776 | if (name) | |
777 | *name = cache_pc_function_name; | |
d541211d PS |
778 | if (endaddr) |
779 | *endaddr = cache_pc_function_high; | |
bd5635a1 RP |
780 | return 1; |
781 | } | |
782 | ||
479fdd26 | 783 | /* Return the innermost stack frame executing inside of BLOCK, |
2289e1c3 | 784 | or NULL if there is no such frame. If BLOCK is NULL, just return NULL. */ |
23a8e291 | 785 | |
0e2c2c1e | 786 | struct frame_info * |
bd5635a1 RP |
787 | block_innermost_frame (block) |
788 | struct block *block; | |
789 | { | |
0e2c2c1e | 790 | struct frame_info *frame; |
2289e1c3 JK |
791 | register CORE_ADDR start; |
792 | register CORE_ADDR end; | |
bd5635a1 | 793 | |
479fdd26 JK |
794 | if (block == NULL) |
795 | return NULL; | |
796 | ||
2289e1c3 JK |
797 | start = BLOCK_START (block); |
798 | end = BLOCK_END (block); | |
799 | ||
0e2c2c1e | 800 | frame = NULL; |
bd5635a1 RP |
801 | while (1) |
802 | { | |
803 | frame = get_prev_frame (frame); | |
0e2c2c1e KH |
804 | if (frame == NULL) |
805 | return NULL; | |
806 | if (frame->pc >= start && frame->pc < end) | |
bd5635a1 RP |
807 | return frame; |
808 | } | |
809 | } | |
810 | ||
0e2c2c1e KH |
811 | /* Return the full FRAME which corresponds to the given CORE_ADDR |
812 | or NULL if no FRAME on the chain corresponds to CORE_ADDR. */ | |
999dd04b | 813 | |
0e2c2c1e | 814 | struct frame_info * |
999dd04b | 815 | find_frame_addr_in_frame_chain (frame_addr) |
0e2c2c1e | 816 | CORE_ADDR frame_addr; |
999dd04b | 817 | { |
0e2c2c1e | 818 | struct frame_info *frame = NULL; |
999dd04b | 819 | |
16726dd1 | 820 | if (frame_addr == (CORE_ADDR)0) |
999dd04b JL |
821 | return NULL; |
822 | ||
823 | while (1) | |
824 | { | |
825 | frame = get_prev_frame (frame); | |
826 | if (frame == NULL) | |
827 | return NULL; | |
999dd04b JL |
828 | if (FRAME_FP (frame) == frame_addr) |
829 | return frame; | |
830 | } | |
831 | } | |
832 | ||
d541211d PS |
833 | #ifdef SIGCONTEXT_PC_OFFSET |
834 | /* Get saved user PC for sigtramp from sigcontext for BSD style sigtramp. */ | |
835 | ||
836 | CORE_ADDR | |
837 | sigtramp_saved_pc (frame) | |
0e2c2c1e | 838 | struct frame_info *frame; |
d541211d PS |
839 | { |
840 | CORE_ADDR sigcontext_addr; | |
841 | char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT]; | |
842 | int ptrbytes = TARGET_PTR_BIT / TARGET_CHAR_BIT; | |
843 | int sigcontext_offs = (2 * TARGET_INT_BIT) / TARGET_CHAR_BIT; | |
844 | ||
845 | /* Get sigcontext address, it is the third parameter on the stack. */ | |
846 | if (frame->next) | |
847 | sigcontext_addr = read_memory_integer (FRAME_ARGS_ADDRESS (frame->next) | |
0e2c2c1e KH |
848 | + FRAME_ARGS_SKIP |
849 | + sigcontext_offs, | |
d541211d PS |
850 | ptrbytes); |
851 | else | |
852 | sigcontext_addr = read_memory_integer (read_register (SP_REGNUM) | |
853 | + sigcontext_offs, | |
854 | ptrbytes); | |
855 | ||
856 | /* Don't cause a memory_error when accessing sigcontext in case the stack | |
857 | layout has changed or the stack is corrupt. */ | |
858 | target_read_memory (sigcontext_addr + SIGCONTEXT_PC_OFFSET, buf, ptrbytes); | |
859 | return extract_unsigned_integer (buf, ptrbytes); | |
860 | } | |
861 | #endif /* SIGCONTEXT_PC_OFFSET */ | |
862 | ||
bd5635a1 RP |
863 | void |
864 | _initialize_blockframe () | |
865 | { | |
866 | obstack_init (&frame_cache_obstack); | |
867 | } |