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