alpha-mdebug-tdep.c revision 1.9 1 /* Target-dependent mdebug code for the ALPHA architecture.
2 Copyright (C) 1993-2020 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19 #include "defs.h"
20 #include "frame.h"
21 #include "frame-unwind.h"
22 #include "frame-base.h"
23 #include "symtab.h"
24 #include "gdbcore.h"
25 #include "block.h"
26 #include "trad-frame.h"
27
28 #include "alpha-tdep.h"
29 #include "mdebugread.h"
30 #include "gdbarch.h"
31
32 /* FIXME: Some of this code should perhaps be merged with mips. */
33
34 /* *INDENT-OFF* */
35 /* Layout of a stack frame on the alpha:
36
37 | |
38 pdr members: | 7th ... nth arg, |
39 | `pushed' by caller. |
40 | |
41 ----------------|-------------------------------|<-- old_sp == vfp
42 ^ ^ ^ ^ | |
43 | | | | | |
44 | |localoff | Copies of 1st .. 6th |
45 | | | | | argument if necessary. |
46 | | | v | |
47 | | | --- |-------------------------------|<-- LOCALS_ADDRESS
48 | | | | |
49 | | | | Locals and temporaries. |
50 | | | | |
51 | | | |-------------------------------|
52 | | | | |
53 |-fregoffset | Saved float registers. |
54 | | | | F9 |
55 | | | | . |
56 | | | | . |
57 | | | | F2 |
58 | | v | |
59 | | -------|-------------------------------|
60 | | | |
61 | | | Saved registers. |
62 | | | S6 |
63 |-regoffset | . |
64 | | | . |
65 | | | S0 |
66 | | | pdr.pcreg |
67 | v | |
68 | ----------|-------------------------------|
69 | | |
70 frameoffset | Argument build area, gets |
71 | | 7th ... nth arg for any |
72 | | called procedure. |
73 v | |
74 -------------|-------------------------------|<-- sp
75 | |
76 */
77 /* *INDENT-ON* */
78
79 #define PROC_LOW_ADDR(proc) ((proc)->pdr.adr)
80 #define PROC_FRAME_OFFSET(proc) ((proc)->pdr.frameoffset)
81 #define PROC_FRAME_REG(proc) ((proc)->pdr.framereg)
82 #define PROC_REG_MASK(proc) ((proc)->pdr.regmask)
83 #define PROC_FREG_MASK(proc) ((proc)->pdr.fregmask)
84 #define PROC_REG_OFFSET(proc) ((proc)->pdr.regoffset)
85 #define PROC_FREG_OFFSET(proc) ((proc)->pdr.fregoffset)
86 #define PROC_PC_REG(proc) ((proc)->pdr.pcreg)
87 #define PROC_LOCALOFF(proc) ((proc)->pdr.localoff)
88
89 /* Locate the mdebug PDR for the given PC. Return null if one can't
91 be found; you'll have to fall back to other methods in that case. */
92
93 static struct mdebug_extra_func_info *
94 find_proc_desc (CORE_ADDR pc)
95 {
96 const struct block *b = block_for_pc (pc);
97 struct mdebug_extra_func_info *proc_desc = NULL;
98 struct symbol *sym = NULL;
99 const char *sh_name = NULL;
100
101 if (b)
102 {
103 CORE_ADDR startaddr;
104 find_pc_partial_function (pc, &sh_name, &startaddr, NULL);
105
106 if (startaddr > BLOCK_START (b))
107 /* This is the "pathological" case referred to in a comment in
108 print_frame_info. It might be better to move this check into
109 symbol reading. */
110 sym = NULL;
111 else
112 sym = lookup_symbol (MDEBUG_EFI_SYMBOL_NAME, b, LABEL_DOMAIN,
113 0).symbol;
114 }
115
116 if (sym)
117 {
118 proc_desc = (struct mdebug_extra_func_info *) SYMBOL_VALUE_BYTES (sym);
119
120 /* Correct incorrect setjmp procedure descriptor from the library
121 to make backtrace through setjmp work. */
122 if (proc_desc->pdr.pcreg == 0
123 && strcmp (sh_name, "setjmp") == 0)
124 {
125 proc_desc->pdr.pcreg = ALPHA_RA_REGNUM;
126 proc_desc->pdr.regmask = 0x80000000;
127 proc_desc->pdr.regoffset = -4;
128 }
129
130 /* If we never found a PDR for this function in symbol reading,
131 then examine prologues to find the information. */
132 if (proc_desc->pdr.framereg == -1)
133 proc_desc = NULL;
134 }
135
136 return proc_desc;
137 }
138
139 /* Return a non-zero result if the function is frameless; zero otherwise. */
140
141 static int
142 alpha_mdebug_frameless (struct mdebug_extra_func_info *proc_desc)
143 {
144 return (PROC_FRAME_REG (proc_desc) == ALPHA_SP_REGNUM
145 && PROC_FRAME_OFFSET (proc_desc) == 0);
146 }
147
148 /* This returns the PC of the first inst after the prologue. If we can't
149 find the prologue, then return 0. */
150
151 static CORE_ADDR
152 alpha_mdebug_after_prologue (CORE_ADDR pc,
153 struct mdebug_extra_func_info *proc_desc)
154 {
155 if (proc_desc)
156 {
157 /* If function is frameless, then we need to do it the hard way. I
158 strongly suspect that frameless always means prologueless... */
159 if (alpha_mdebug_frameless (proc_desc))
160 return 0;
161 }
162
163 return alpha_after_prologue (pc);
164 }
165
166 /* Return non-zero if we *might* be in a function prologue. Return zero
167 if we are definitively *not* in a function prologue. */
168
169 static int
170 alpha_mdebug_in_prologue (CORE_ADDR pc,
171 struct mdebug_extra_func_info *proc_desc)
172 {
173 CORE_ADDR after_prologue_pc = alpha_mdebug_after_prologue (pc, proc_desc);
174 return (after_prologue_pc == 0 || pc < after_prologue_pc);
175 }
176
177
178 /* Frame unwinder that reads mdebug PDRs. */
180
181 struct alpha_mdebug_unwind_cache
182 {
183 struct mdebug_extra_func_info *proc_desc;
184 CORE_ADDR vfp;
185 struct trad_frame_saved_reg *saved_regs;
186 };
187
188 /* Extract all of the information about the frame from PROC_DESC
189 and store the resulting register save locations in the structure. */
190
191 static struct alpha_mdebug_unwind_cache *
192 alpha_mdebug_frame_unwind_cache (struct frame_info *this_frame,
193 void **this_prologue_cache)
194 {
195 struct alpha_mdebug_unwind_cache *info;
196 struct mdebug_extra_func_info *proc_desc;
197 ULONGEST vfp;
198 CORE_ADDR pc, reg_position;
199 unsigned long mask;
200 int ireg, returnreg;
201
202 if (*this_prologue_cache)
203 return (struct alpha_mdebug_unwind_cache *) *this_prologue_cache;
204
205 info = FRAME_OBSTACK_ZALLOC (struct alpha_mdebug_unwind_cache);
206 *this_prologue_cache = info;
207 pc = get_frame_address_in_block (this_frame);
208
209 /* ??? We don't seem to be able to cache the lookup of the PDR
210 from alpha_mdebug_frame_p. It'd be nice if we could change
211 the arguments to that function. Oh well. */
212 proc_desc = find_proc_desc (pc);
213 info->proc_desc = proc_desc;
214 gdb_assert (proc_desc != NULL);
215
216 info->saved_regs = trad_frame_alloc_saved_regs (this_frame);
217
218 /* The VFP of the frame is at FRAME_REG+FRAME_OFFSET. */
219 vfp = get_frame_register_unsigned (this_frame, PROC_FRAME_REG (proc_desc));
220 vfp += PROC_FRAME_OFFSET (info->proc_desc);
221 info->vfp = vfp;
222
223 /* Fill in the offsets for the registers which gen_mask says were saved. */
224
225 reg_position = vfp + PROC_REG_OFFSET (proc_desc);
226 mask = PROC_REG_MASK (proc_desc);
227 returnreg = PROC_PC_REG (proc_desc);
228
229 /* Note that RA is always saved first, regardless of its actual
230 register number. */
231 if (mask & (1 << returnreg))
232 {
233 /* Clear bit for RA so we don't save it again later. */
234 mask &= ~(1 << returnreg);
235
236 info->saved_regs[returnreg].addr = reg_position;
237 reg_position += 8;
238 }
239
240 for (ireg = 0; ireg <= 31; ++ireg)
241 if (mask & (1 << ireg))
242 {
243 info->saved_regs[ireg].addr = reg_position;
244 reg_position += 8;
245 }
246
247 reg_position = vfp + PROC_FREG_OFFSET (proc_desc);
248 mask = PROC_FREG_MASK (proc_desc);
249
250 for (ireg = 0; ireg <= 31; ++ireg)
251 if (mask & (1 << ireg))
252 {
253 info->saved_regs[ALPHA_FP0_REGNUM + ireg].addr = reg_position;
254 reg_position += 8;
255 }
256
257 /* The stack pointer of the previous frame is computed by popping
258 the current stack frame. */
259 if (!trad_frame_addr_p (info->saved_regs, ALPHA_SP_REGNUM))
260 trad_frame_set_value (info->saved_regs, ALPHA_SP_REGNUM, vfp);
261
262 return info;
263 }
264
265 /* Given a GDB frame, determine the address of the calling function's
266 frame. This will be used to create a new GDB frame struct. */
267
268 static void
269 alpha_mdebug_frame_this_id (struct frame_info *this_frame,
270 void **this_prologue_cache,
271 struct frame_id *this_id)
272 {
273 struct alpha_mdebug_unwind_cache *info
274 = alpha_mdebug_frame_unwind_cache (this_frame, this_prologue_cache);
275
276 *this_id = frame_id_build (info->vfp, get_frame_func (this_frame));
277 }
278
279 /* Retrieve the value of REGNUM in FRAME. Don't give up! */
280
281 static struct value *
282 alpha_mdebug_frame_prev_register (struct frame_info *this_frame,
283 void **this_prologue_cache, int regnum)
284 {
285 struct alpha_mdebug_unwind_cache *info
286 = alpha_mdebug_frame_unwind_cache (this_frame, this_prologue_cache);
287
288 /* The PC of the previous frame is stored in the link register of
289 the current frame. Frob regnum so that we pull the value from
290 the correct place. */
291 if (regnum == ALPHA_PC_REGNUM)
292 regnum = PROC_PC_REG (info->proc_desc);
293
294 return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
295 }
296
297 /* Return a non-zero result if the size of the stack frame exceeds the
298 maximum debuggable frame size (512 Kbytes); zero otherwise. */
299
300 static int
301 alpha_mdebug_max_frame_size_exceeded (struct mdebug_extra_func_info *proc_desc)
302 {
303 /* If frame offset is null, we can be in two cases: either the
304 function is frameless (the stack frame is null) or its
305 frame exceeds the maximum debuggable frame size (512 Kbytes). */
306
307 return (PROC_FRAME_OFFSET (proc_desc) == 0
308 && !alpha_mdebug_frameless (proc_desc));
309 }
310
311 static int
312 alpha_mdebug_frame_sniffer (const struct frame_unwind *self,
313 struct frame_info *this_frame,
314 void **this_cache)
315 {
316 CORE_ADDR pc = get_frame_address_in_block (this_frame);
317 struct mdebug_extra_func_info *proc_desc;
318
319 /* If this PC does not map to a PDR, then clearly this isn't an
320 mdebug frame. */
321 proc_desc = find_proc_desc (pc);
322 if (proc_desc == NULL)
323 return 0;
324
325 /* If we're in the prologue, the PDR for this frame is not yet valid.
326 Say no here and we'll fall back on the heuristic unwinder. */
327 if (alpha_mdebug_in_prologue (pc, proc_desc))
328 return 0;
329
330 /* If the maximum debuggable frame size has been exceeded, the
331 proc desc is bogus. Fall back on the heuristic unwinder. */
332 if (alpha_mdebug_max_frame_size_exceeded (proc_desc))
333 return 0;
334
335 return 1;
336 }
337
338 static const struct frame_unwind alpha_mdebug_frame_unwind = {
339 NORMAL_FRAME,
340 default_frame_unwind_stop_reason,
341 alpha_mdebug_frame_this_id,
342 alpha_mdebug_frame_prev_register,
343 NULL,
344 alpha_mdebug_frame_sniffer
345 };
346
347 static CORE_ADDR
348 alpha_mdebug_frame_base_address (struct frame_info *this_frame,
349 void **this_prologue_cache)
350 {
351 struct alpha_mdebug_unwind_cache *info
352 = alpha_mdebug_frame_unwind_cache (this_frame, this_prologue_cache);
353
354 return info->vfp;
355 }
356
357 static CORE_ADDR
358 alpha_mdebug_frame_locals_address (struct frame_info *this_frame,
359 void **this_prologue_cache)
360 {
361 struct alpha_mdebug_unwind_cache *info
362 = alpha_mdebug_frame_unwind_cache (this_frame, this_prologue_cache);
363
364 return info->vfp - PROC_LOCALOFF (info->proc_desc);
365 }
366
367 static CORE_ADDR
368 alpha_mdebug_frame_args_address (struct frame_info *this_frame,
369 void **this_prologue_cache)
370 {
371 struct alpha_mdebug_unwind_cache *info
372 = alpha_mdebug_frame_unwind_cache (this_frame, this_prologue_cache);
373
374 return info->vfp - ALPHA_NUM_ARG_REGS * 8;
375 }
376
377 static const struct frame_base alpha_mdebug_frame_base = {
378 &alpha_mdebug_frame_unwind,
379 alpha_mdebug_frame_base_address,
380 alpha_mdebug_frame_locals_address,
381 alpha_mdebug_frame_args_address
382 };
383
384 static const struct frame_base *
385 alpha_mdebug_frame_base_sniffer (struct frame_info *this_frame)
386 {
387 CORE_ADDR pc = get_frame_address_in_block (this_frame);
388 struct mdebug_extra_func_info *proc_desc;
389
390 /* If this PC does not map to a PDR, then clearly this isn't an
391 mdebug frame. */
392 proc_desc = find_proc_desc (pc);
393 if (proc_desc == NULL)
394 return NULL;
395
396 /* If the maximum debuggable frame size has been exceeded, the
397 proc desc is bogus. Fall back on the heuristic unwinder. */
398 if (alpha_mdebug_max_frame_size_exceeded (proc_desc))
399 return 0;
400
401 return &alpha_mdebug_frame_base;
402 }
403
404
405 void
407 alpha_mdebug_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
408 {
409 frame_unwind_append_unwinder (gdbarch, &alpha_mdebug_frame_unwind);
410 frame_base_append_sniffer (gdbarch, alpha_mdebug_frame_base_sniffer);
411 }
412