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