introduce minimal_symbol_upper_bound
[deliverable/binutils-gdb.git] / gdb / blockframe.c
1 /* Get info from stack frames; convert between frames, blocks,
2 functions and pc values.
3
4 Copyright (C) 1986-2014 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include "defs.h"
22 #include "symtab.h"
23 #include "bfd.h"
24 #include "objfiles.h"
25 #include "frame.h"
26 #include "gdbcore.h"
27 #include "value.h"
28 #include "target.h"
29 #include "inferior.h"
30 #include "annotate.h"
31 #include "regcache.h"
32 #include "gdb_assert.h"
33 #include "dummy-frame.h"
34 #include "command.h"
35 #include "gdbcmd.h"
36 #include "block.h"
37 #include "inline-frame.h"
38
39 /* Return the innermost lexical block in execution in a specified
40 stack frame. The frame address is assumed valid.
41
42 If ADDR_IN_BLOCK is non-zero, set *ADDR_IN_BLOCK to the exact code
43 address we used to choose the block. We use this to find a source
44 line, to decide which macro definitions are in scope.
45
46 The value returned in *ADDR_IN_BLOCK isn't necessarily the frame's
47 PC, and may not really be a valid PC at all. For example, in the
48 caller of a function declared to never return, the code at the
49 return address will never be reached, so the call instruction may
50 be the very last instruction in the block. So the address we use
51 to choose the block is actually one byte before the return address
52 --- hopefully pointing us at the call instruction, or its delay
53 slot instruction. */
54
55 struct block *
56 get_frame_block (struct frame_info *frame, CORE_ADDR *addr_in_block)
57 {
58 CORE_ADDR pc;
59 struct block *bl;
60 int inline_count;
61
62 if (!get_frame_address_in_block_if_available (frame, &pc))
63 return NULL;
64
65 if (addr_in_block)
66 *addr_in_block = pc;
67
68 bl = block_for_pc (pc);
69 if (bl == NULL)
70 return NULL;
71
72 inline_count = frame_inlined_callees (frame);
73
74 while (inline_count > 0)
75 {
76 if (block_inlined_p (bl))
77 inline_count--;
78
79 bl = BLOCK_SUPERBLOCK (bl);
80 gdb_assert (bl != NULL);
81 }
82
83 return bl;
84 }
85
86 CORE_ADDR
87 get_pc_function_start (CORE_ADDR pc)
88 {
89 struct block *bl;
90 struct bound_minimal_symbol msymbol;
91
92 bl = block_for_pc (pc);
93 if (bl)
94 {
95 struct symbol *symbol = block_linkage_function (bl);
96
97 if (symbol)
98 {
99 bl = SYMBOL_BLOCK_VALUE (symbol);
100 return BLOCK_START (bl);
101 }
102 }
103
104 msymbol = lookup_minimal_symbol_by_pc (pc);
105 if (msymbol.minsym)
106 {
107 CORE_ADDR fstart = SYMBOL_VALUE_ADDRESS (msymbol.minsym);
108
109 if (find_pc_section (fstart))
110 return fstart;
111 }
112
113 return 0;
114 }
115
116 /* Return the symbol for the function executing in frame FRAME. */
117
118 struct symbol *
119 get_frame_function (struct frame_info *frame)
120 {
121 struct block *bl = get_frame_block (frame, 0);
122
123 if (bl == NULL)
124 return NULL;
125
126 while (BLOCK_FUNCTION (bl) == NULL && BLOCK_SUPERBLOCK (bl) != NULL)
127 bl = BLOCK_SUPERBLOCK (bl);
128
129 return BLOCK_FUNCTION (bl);
130 }
131 \f
132
133 /* Return the function containing pc value PC in section SECTION.
134 Returns 0 if function is not known. */
135
136 struct symbol *
137 find_pc_sect_function (CORE_ADDR pc, struct obj_section *section)
138 {
139 struct block *b = block_for_pc_sect (pc, section);
140
141 if (b == 0)
142 return 0;
143 return block_linkage_function (b);
144 }
145
146 /* Return the function containing pc value PC.
147 Returns 0 if function is not known.
148 Backward compatibility, no section */
149
150 struct symbol *
151 find_pc_function (CORE_ADDR pc)
152 {
153 return find_pc_sect_function (pc, find_pc_mapped_section (pc));
154 }
155
156 /* These variables are used to cache the most recent result
157 of find_pc_partial_function. */
158
159 static CORE_ADDR cache_pc_function_low = 0;
160 static CORE_ADDR cache_pc_function_high = 0;
161 static const char *cache_pc_function_name = 0;
162 static struct obj_section *cache_pc_function_section = NULL;
163 static int cache_pc_function_is_gnu_ifunc = 0;
164
165 /* Clear cache, e.g. when symbol table is discarded. */
166
167 void
168 clear_pc_function_cache (void)
169 {
170 cache_pc_function_low = 0;
171 cache_pc_function_high = 0;
172 cache_pc_function_name = (char *) 0;
173 cache_pc_function_section = NULL;
174 cache_pc_function_is_gnu_ifunc = 0;
175 }
176
177 /* Finds the "function" (text symbol) that is smaller than PC but
178 greatest of all of the potential text symbols in SECTION. Sets
179 *NAME and/or *ADDRESS conditionally if that pointer is non-null.
180 If ENDADDR is non-null, then set *ENDADDR to be the end of the
181 function (exclusive), but passing ENDADDR as non-null means that
182 the function might cause symbols to be read. If IS_GNU_IFUNC_P is provided
183 *IS_GNU_IFUNC_P is set to 1 on return if the function is STT_GNU_IFUNC.
184 This function either succeeds or fails (not halfway succeeds). If it
185 succeeds, it sets *NAME, *ADDRESS, and *ENDADDR to real information and
186 returns 1. If it fails, it sets *NAME, *ADDRESS, *ENDADDR and
187 *IS_GNU_IFUNC_P to zero and returns 0. */
188
189 /* Backward compatibility, no section argument. */
190
191 int
192 find_pc_partial_function_gnu_ifunc (CORE_ADDR pc, const char **name,
193 CORE_ADDR *address, CORE_ADDR *endaddr,
194 int *is_gnu_ifunc_p)
195 {
196 struct obj_section *section;
197 struct symbol *f;
198 struct bound_minimal_symbol msymbol;
199 struct symtab *symtab = NULL;
200 struct objfile *objfile;
201 int i;
202 CORE_ADDR mapped_pc;
203
204 /* To ensure that the symbol returned belongs to the correct setion
205 (and that the last [random] symbol from the previous section
206 isn't returned) try to find the section containing PC. First try
207 the overlay code (which by default returns NULL); and second try
208 the normal section code (which almost always succeeds). */
209 section = find_pc_overlay (pc);
210 if (section == NULL)
211 section = find_pc_section (pc);
212
213 mapped_pc = overlay_mapped_address (pc, section);
214
215 if (mapped_pc >= cache_pc_function_low
216 && mapped_pc < cache_pc_function_high
217 && section == cache_pc_function_section)
218 goto return_cached_value;
219
220 msymbol = lookup_minimal_symbol_by_pc_section (mapped_pc, section);
221 ALL_OBJFILES (objfile)
222 {
223 if (objfile->sf)
224 symtab = objfile->sf->qf->find_pc_sect_symtab (objfile, msymbol.minsym,
225 mapped_pc, section, 0);
226 if (symtab)
227 break;
228 }
229
230 if (symtab)
231 {
232 /* Checking whether the msymbol has a larger value is for the
233 "pathological" case mentioned in print_frame_info. */
234 f = find_pc_sect_function (mapped_pc, section);
235 if (f != NULL
236 && (msymbol.minsym == NULL
237 || (BLOCK_START (SYMBOL_BLOCK_VALUE (f))
238 >= SYMBOL_VALUE_ADDRESS (msymbol.minsym))))
239 {
240 cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f));
241 cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f));
242 cache_pc_function_name = SYMBOL_LINKAGE_NAME (f);
243 cache_pc_function_section = section;
244 cache_pc_function_is_gnu_ifunc = TYPE_GNU_IFUNC (SYMBOL_TYPE (f));
245 goto return_cached_value;
246 }
247 }
248
249 /* Not in the normal symbol tables, see if the pc is in a known
250 section. If it's not, then give up. This ensures that anything
251 beyond the end of the text seg doesn't appear to be part of the
252 last function in the text segment. */
253
254 if (!section)
255 msymbol.minsym = NULL;
256
257 /* Must be in the minimal symbol table. */
258 if (msymbol.minsym == NULL)
259 {
260 /* No available symbol. */
261 if (name != NULL)
262 *name = 0;
263 if (address != NULL)
264 *address = 0;
265 if (endaddr != NULL)
266 *endaddr = 0;
267 if (is_gnu_ifunc_p != NULL)
268 *is_gnu_ifunc_p = 0;
269 return 0;
270 }
271
272 cache_pc_function_low = SYMBOL_VALUE_ADDRESS (msymbol.minsym);
273 cache_pc_function_name = SYMBOL_LINKAGE_NAME (msymbol.minsym);
274 cache_pc_function_section = section;
275 cache_pc_function_is_gnu_ifunc = (MSYMBOL_TYPE (msymbol.minsym)
276 == mst_text_gnu_ifunc);
277 cache_pc_function_high = minimal_symbol_upper_bound (msymbol);
278
279 return_cached_value:
280
281 if (address)
282 {
283 if (pc_in_unmapped_range (pc, section))
284 *address = overlay_unmapped_address (cache_pc_function_low, section);
285 else
286 *address = cache_pc_function_low;
287 }
288
289 if (name)
290 *name = cache_pc_function_name;
291
292 if (endaddr)
293 {
294 if (pc_in_unmapped_range (pc, section))
295 {
296 /* Because the high address is actually beyond the end of
297 the function (and therefore possibly beyond the end of
298 the overlay), we must actually convert (high - 1) and
299 then add one to that. */
300
301 *endaddr = 1 + overlay_unmapped_address (cache_pc_function_high - 1,
302 section);
303 }
304 else
305 *endaddr = cache_pc_function_high;
306 }
307
308 if (is_gnu_ifunc_p)
309 *is_gnu_ifunc_p = cache_pc_function_is_gnu_ifunc;
310
311 return 1;
312 }
313
314 /* See find_pc_partial_function_gnu_ifunc, only the IS_GNU_IFUNC_P parameter
315 is omitted here for backward API compatibility. */
316
317 int
318 find_pc_partial_function (CORE_ADDR pc, const char **name, CORE_ADDR *address,
319 CORE_ADDR *endaddr)
320 {
321 return find_pc_partial_function_gnu_ifunc (pc, name, address, endaddr, NULL);
322 }
323
324 /* Return the innermost stack frame that is executing inside of BLOCK and is
325 at least as old as the selected frame. Return NULL if there is no
326 such frame. If BLOCK is NULL, just return NULL. */
327
328 struct frame_info *
329 block_innermost_frame (const struct block *block)
330 {
331 struct frame_info *frame;
332
333 if (block == NULL)
334 return NULL;
335
336 frame = get_selected_frame_if_set ();
337 if (frame == NULL)
338 frame = get_current_frame ();
339 while (frame != NULL)
340 {
341 struct block *frame_block = get_frame_block (frame, NULL);
342 if (frame_block != NULL && contained_in (frame_block, block))
343 return frame;
344
345 frame = get_prev_frame (frame);
346 }
347
348 return NULL;
349 }
This page took 0.03734 seconds and 5 git commands to generate.