(PCINDEX): New macro.
[deliverable/binutils-gdb.git] / gdb / alpha-tdep.c
CommitLineData
cef4c2e7 1/* Target-dependent code for the ALPHA architecture, for GDB, the GNU Debugger.
a1a0d974 2 Copyright 1993, 1994, 1995 Free Software Foundation, Inc.
cef4c2e7
PS
3
4This file is part of GDB.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20#include "defs.h"
21#include "frame.h"
22#include "inferior.h"
23#include "symtab.h"
24#include "value.h"
25#include "gdbcmd.h"
26#include "gdbcore.h"
27#include "dis-asm.h"
72bba93b
SG
28#include "symfile.h"
29#include "objfiles.h"
a1a0d974 30#include <string.h>
cef4c2e7
PS
31
32/* FIXME: Some of this code should perhaps be merged with mips-tdep.c. */
33
72bba93b
SG
34/* FIXME: Put this declaration in frame.h. */
35extern struct obstack frame_cache_obstack;
cef4c2e7
PS
36\f
37
38/* Forward declarations. */
39
669caa9c 40static CORE_ADDR read_next_frame_reg PARAMS ((struct frame_info *, int));
cef4c2e7 41
669caa9c 42static CORE_ADDR heuristic_proc_start PARAMS ((CORE_ADDR));
cef4c2e7 43
669caa9c
SS
44static alpha_extra_func_info_t heuristic_proc_desc PARAMS ((CORE_ADDR,
45 CORE_ADDR,
46 struct frame_info *));
cef4c2e7 47
e3be225e
SS
48static alpha_extra_func_info_t find_proc_desc PARAMS ((CORE_ADDR,
49 struct frame_info *));
cef4c2e7 50
e3be225e 51#if 0
669caa9c 52static int alpha_in_lenient_prologue PARAMS ((CORE_ADDR, CORE_ADDR));
e3be225e 53#endif
cef4c2e7 54
e3be225e
SS
55static void reinit_frame_cache_sfunc PARAMS ((char *, int,
56 struct cmd_list_element *));
cef4c2e7 57
72bba93b
SG
58static CORE_ADDR after_prologue PARAMS ((CORE_ADDR pc,
59 alpha_extra_func_info_t proc_desc));
60
61static int in_prologue PARAMS ((CORE_ADDR pc,
62 alpha_extra_func_info_t proc_desc));
63
cef4c2e7
PS
64/* Heuristic_proc_start may hunt through the text section for a long
65 time across a 2400 baud serial line. Allows the user to limit this
66 search. */
67static unsigned int heuristic_fence_post = 0;
68
69/* Layout of a stack frame on the alpha:
70
71 | |
72 pdr members: | 7th ... nth arg, |
73 | `pushed' by caller. |
74 | |
75----------------|-------------------------------|<-- old_sp == vfp
76 ^ ^ ^ ^ | |
77 | | | | | |
78 | |localoff | Copies of 1st .. 6th |
79 | | | | | argument if necessary. |
80 | | | v | |
3e6b0674
PS
81 | | | --- |-------------------------------|<-- FRAME_LOCALS_ADDRESS
82 | | | | |
cef4c2e7
PS
83 | | | | Locals and temporaries. |
84 | | | | |
85 | | | |-------------------------------|
86 | | | | |
87 |-fregoffset | Saved float registers. |
88 | | | | F9 |
89 | | | | . |
90 | | | | . |
91 | | | | F2 |
92 | | v | |
93 | | -------|-------------------------------|
94 | | | |
95 | | | Saved registers. |
96 | | | S6 |
97 |-regoffset | . |
98 | | | . |
99 | | | S0 |
100 | | | pdr.pcreg |
101 | v | |
102 | ----------|-------------------------------|
103 | | |
104 frameoffset | Argument build area, gets |
105 | | 7th ... nth arg for any |
106 | | called procedure. |
107 v | |
108 -------------|-------------------------------|<-- sp
109 | |
110*/
111
112#define PROC_LOW_ADDR(proc) ((proc)->pdr.adr) /* least address */
113#define PROC_HIGH_ADDR(proc) ((proc)->pdr.iline) /* upper address bound */
114#define PROC_DUMMY_FRAME(proc) ((proc)->pdr.iopt) /* frame for CALL_DUMMY */
115#define PROC_FRAME_OFFSET(proc) ((proc)->pdr.frameoffset)
116#define PROC_FRAME_REG(proc) ((proc)->pdr.framereg)
117#define PROC_REG_MASK(proc) ((proc)->pdr.regmask)
118#define PROC_FREG_MASK(proc) ((proc)->pdr.fregmask)
119#define PROC_REG_OFFSET(proc) ((proc)->pdr.regoffset)
120#define PROC_FREG_OFFSET(proc) ((proc)->pdr.fregoffset)
121#define PROC_PC_REG(proc) ((proc)->pdr.pcreg)
122#define PROC_LOCALOFF(proc) ((proc)->pdr.localoff)
123#define PROC_SYMBOL(proc) (*(struct symbol**)&(proc)->pdr.isym)
124#define _PROC_MAGIC_ 0x0F0F0F0F
125#define PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym == _PROC_MAGIC_)
126#define SET_PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym = _PROC_MAGIC_)
127
128struct linked_proc_info
129{
130 struct alpha_extra_func_info info;
131 struct linked_proc_info *next;
132} *linked_proc_desc_table = NULL;
133
134\f
72bba93b
SG
135/* Guaranteed to set fci->saved_regs to some values (it never leaves it
136 NULL). */
137
138void
669caa9c
SS
139alpha_find_saved_regs (frame)
140 struct frame_info *frame;
72bba93b
SG
141{
142 int ireg;
143 CORE_ADDR reg_position;
144 unsigned long mask;
145 alpha_extra_func_info_t proc_desc;
146 int returnreg;
147
669caa9c 148 frame->saved_regs = (struct frame_saved_regs *)
72bba93b 149 obstack_alloc (&frame_cache_obstack, sizeof(struct frame_saved_regs));
669caa9c 150 memset (frame->saved_regs, 0, sizeof (struct frame_saved_regs));
72bba93b 151
669caa9c 152 proc_desc = frame->proc_desc;
72bba93b
SG
153 if (proc_desc == NULL)
154 /* I'm not sure how/whether this can happen. Normally when we can't
155 find a proc_desc, we "synthesize" one using heuristic_proc_desc
156 and set the saved_regs right away. */
157 return;
158
159 /* Fill in the offsets for the registers which gen_mask says
160 were saved. */
161
669caa9c 162 reg_position = frame->frame + PROC_REG_OFFSET (proc_desc);
72bba93b
SG
163 mask = PROC_REG_MASK (proc_desc);
164
165 returnreg = PROC_PC_REG (proc_desc);
166
167 /* Note that RA is always saved first, regardless of it's actual
168 register number. */
169 if (mask & (1 << returnreg))
170 {
669caa9c 171 frame->saved_regs->regs[returnreg] = reg_position;
72bba93b
SG
172 reg_position += 8;
173 mask &= ~(1 << returnreg); /* Clear bit for RA so we
174 don't save again later. */
175 }
176
177 for (ireg = 0; ireg <= 31 ; ++ireg)
178 if (mask & (1 << ireg))
179 {
669caa9c 180 frame->saved_regs->regs[ireg] = reg_position;
72bba93b
SG
181 reg_position += 8;
182 }
183
184 /* Fill in the offsets for the registers which float_mask says
185 were saved. */
186
669caa9c 187 reg_position = frame->frame + PROC_FREG_OFFSET (proc_desc);
72bba93b
SG
188 mask = PROC_FREG_MASK (proc_desc);
189
190 for (ireg = 0; ireg <= 31 ; ++ireg)
191 if (mask & (1 << ireg))
192 {
669caa9c 193 frame->saved_regs->regs[FP0_REGNUM+ireg] = reg_position;
72bba93b
SG
194 reg_position += 8;
195 }
196
669caa9c 197 frame->saved_regs->regs[PC_REGNUM] = frame->saved_regs->regs[returnreg];
72bba93b 198}
cef4c2e7
PS
199
200static CORE_ADDR
201read_next_frame_reg(fi, regno)
669caa9c 202 struct frame_info *fi;
cef4c2e7
PS
203 int regno;
204{
205 /* If it is the frame for sigtramp we have a pointer to the sigcontext
206 on the stack.
207 If the stack layout for __sigtramp changes or if sigcontext offsets
208 change we might have to update this code. */
209#ifndef SIGFRAME_PC_OFF
210#define SIGFRAME_PC_OFF (2 * 8)
211#define SIGFRAME_REGSAVE_OFF (4 * 8)
212#endif
213 for (; fi; fi = fi->next)
214 {
215 if (fi->signal_handler_caller)
216 {
217 int offset;
218 CORE_ADDR sigcontext_addr = read_memory_integer(fi->frame, 8);
219
220 if (regno == PC_REGNUM)
221 offset = SIGFRAME_PC_OFF;
222 else if (regno < 32)
223 offset = SIGFRAME_REGSAVE_OFF + regno * 8;
224 else
225 return 0;
226 return read_memory_integer(sigcontext_addr + offset, 8);
227 }
228 else if (regno == SP_REGNUM)
229 return fi->frame;
72bba93b
SG
230 else
231 {
232 if (fi->saved_regs == NULL)
233 alpha_find_saved_regs (fi);
234 if (fi->saved_regs->regs[regno])
235 return read_memory_integer(fi->saved_regs->regs[regno], 8);
236 }
cef4c2e7
PS
237 }
238 return read_register(regno);
239}
240
241CORE_ADDR
242alpha_frame_saved_pc(frame)
669caa9c 243 struct frame_info *frame;
cef4c2e7
PS
244{
245 alpha_extra_func_info_t proc_desc = frame->proc_desc;
0434c1a0
PS
246 /* We have to get the saved pc from the sigcontext
247 if it is a signal handler frame. */
248 int pcreg = frame->signal_handler_caller ? PC_REGNUM
249 : (proc_desc ? PROC_PC_REG(proc_desc) : RA_REGNUM);
cef4c2e7
PS
250
251 if (proc_desc && PROC_DESC_IS_DUMMY(proc_desc))
252 return read_memory_integer(frame->frame - 8, 8);
253
254 return read_next_frame_reg(frame, pcreg);
255}
256
257CORE_ADDR
258alpha_saved_pc_after_call (frame)
669caa9c 259 struct frame_info *frame;
cef4c2e7
PS
260{
261 alpha_extra_func_info_t proc_desc = find_proc_desc (frame->pc, frame->next);
262 int pcreg = proc_desc ? PROC_PC_REG (proc_desc) : RA_REGNUM;
263
264 return read_register (pcreg);
265}
266
267
268static struct alpha_extra_func_info temp_proc_desc;
269static struct frame_saved_regs temp_saved_regs;
270
271/* This fencepost looks highly suspicious to me. Removing it also
272 seems suspicious as it could affect remote debugging across serial
273 lines. */
274
275static CORE_ADDR
276heuristic_proc_start(pc)
277 CORE_ADDR pc;
278{
279 CORE_ADDR start_pc = pc;
280 CORE_ADDR fence = start_pc - heuristic_fence_post;
281
282 if (start_pc == 0) return 0;
283
284 if (heuristic_fence_post == UINT_MAX
285 || fence < VM_MIN_ADDRESS)
286 fence = VM_MIN_ADDRESS;
287
288 /* search back for previous return */
289 for (start_pc -= 4; ; start_pc -= 4)
290 if (start_pc < fence)
291 {
292 /* It's not clear to me why we reach this point when
293 stop_soon_quietly, but with this test, at least we
294 don't print out warnings for every child forked (eg, on
295 decstation). 22apr93 rich@cygnus.com. */
296 if (!stop_soon_quietly)
297 {
298 static int blurb_printed = 0;
299
300 if (fence == VM_MIN_ADDRESS)
301 warning("Hit beginning of text section without finding");
302 else
303 warning("Hit heuristic-fence-post without finding");
304
305 warning("enclosing function for address 0x%lx", pc);
306 if (!blurb_printed)
307 {
308 printf_filtered ("\
309This warning occurs if you are debugging a function without any symbols\n\
310(for example, in a stripped executable). In that case, you may wish to\n\
311increase the size of the search with the `set heuristic-fence-post' command.\n\
312\n\
313Otherwise, you told GDB there was a function where there isn't one, or\n\
314(more likely) you have encountered a bug in GDB.\n");
315 blurb_printed = 1;
316 }
317 }
318
319 return 0;
320 }
321 else if (ABOUT_TO_RETURN(start_pc))
322 break;
323
324 start_pc += 4; /* skip return */
325 return start_pc;
326}
327
328static alpha_extra_func_info_t
329heuristic_proc_desc(start_pc, limit_pc, next_frame)
330 CORE_ADDR start_pc, limit_pc;
669caa9c 331 struct frame_info *next_frame;
cef4c2e7 332{
2fe3b329 333 CORE_ADDR sp = read_next_frame_reg (next_frame, SP_REGNUM);
cef4c2e7
PS
334 CORE_ADDR cur_pc;
335 int frame_size;
336 int has_frame_reg = 0;
337 unsigned long reg_mask = 0;
338
339 if (start_pc == 0)
340 return NULL;
669caa9c
SS
341 memset (&temp_proc_desc, '\0', sizeof(temp_proc_desc));
342 memset (&temp_saved_regs, '\0', sizeof(struct frame_saved_regs));
343 PROC_LOW_ADDR (&temp_proc_desc) = start_pc;
cef4c2e7
PS
344
345 if (start_pc + 200 < limit_pc)
346 limit_pc = start_pc + 200;
347 frame_size = 0;
348 for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += 4)
349 {
350 char buf[4];
351 unsigned long word;
352 int status;
353
354 status = read_memory_nobpt (cur_pc, buf, 4);
355 if (status)
356 memory_error (status, cur_pc);
357 word = extract_unsigned_integer (buf, 4);
358
359 if ((word & 0xffff0000) == 0x23de0000) /* lda $sp,n($sp) */
360 frame_size += (-word) & 0xffff;
361 else if ((word & 0xfc1f0000) == 0xb41e0000 /* stq reg,n($sp) */
362 && (word & 0xffff0000) != 0xb7fe0000) /* reg != $zero */
363 {
364 int reg = (word & 0x03e00000) >> 21;
365 reg_mask |= 1 << reg;
366 temp_saved_regs.regs[reg] = sp + (short)word;
367 }
368 else if (word == 0x47de040f) /* bis sp,sp fp */
369 has_frame_reg = 1;
370 }
371 if (has_frame_reg)
372 PROC_FRAME_REG(&temp_proc_desc) = GCC_FP_REGNUM;
373 else
374 PROC_FRAME_REG(&temp_proc_desc) = SP_REGNUM;
375 PROC_FRAME_OFFSET(&temp_proc_desc) = frame_size;
376 PROC_REG_MASK(&temp_proc_desc) = reg_mask;
377 PROC_PC_REG(&temp_proc_desc) = RA_REGNUM;
72bba93b 378 PROC_LOCALOFF(&temp_proc_desc) = 0; /* XXX - bogus */
cef4c2e7
PS
379 return &temp_proc_desc;
380}
381
72bba93b
SG
382/* This returns the PC of the first inst after the prologue. If we can't
383 find the prologue, then return 0. */
384
385static CORE_ADDR
386after_prologue (pc, proc_desc)
387 CORE_ADDR pc;
388 alpha_extra_func_info_t proc_desc;
389{
72bba93b
SG
390 struct symtab_and_line sal;
391 CORE_ADDR func_addr, func_end;
392
393 if (!proc_desc)
394 proc_desc = find_proc_desc (pc, NULL);
395
396 if (proc_desc)
397 {
398 /* If function is frameless, then we need to do it the hard way. I
399 strongly suspect that frameless always means prologueless... */
400 if (PROC_FRAME_REG (proc_desc) == SP_REGNUM
401 && PROC_FRAME_OFFSET (proc_desc) == 0)
402 return 0;
403 }
404
405 if (!find_pc_partial_function (pc, NULL, &func_addr, &func_end))
406 return 0; /* Unknown */
407
408 sal = find_pc_line (func_addr, 0);
409
410 if (sal.end < func_end)
411 return sal.end;
412
413 /* The line after the prologue is after the end of the function. In this
414 case, tell the caller to find the prologue the hard way. */
415
416 return 0;
417}
418
419/* Return non-zero if we *might* be in a function prologue. Return zero if we
420 are definatly *not* in a function prologue. */
421
422static int
423in_prologue (pc, proc_desc)
424 CORE_ADDR pc;
425 alpha_extra_func_info_t proc_desc;
426{
427 CORE_ADDR after_prologue_pc;
428
429 after_prologue_pc = after_prologue (pc, proc_desc);
430
431 if (after_prologue_pc == 0
432 || pc < after_prologue_pc)
433 return 1;
434 else
435 return 0;
436}
437
cef4c2e7 438static alpha_extra_func_info_t
669caa9c 439find_proc_desc (pc, next_frame)
cef4c2e7 440 CORE_ADDR pc;
669caa9c 441 struct frame_info *next_frame;
cef4c2e7
PS
442{
443 alpha_extra_func_info_t proc_desc;
444 struct block *b;
445 struct symbol *sym;
446 CORE_ADDR startaddr;
447
448 /* Try to get the proc_desc from the linked call dummy proc_descs
449 if the pc is in the call dummy.
450 This is hairy. In the case of nested dummy calls we have to find the
451 right proc_desc, but we might not yet know the frame for the dummy
452 as it will be contained in the proc_desc we are searching for.
453 So we have to find the proc_desc whose frame is closest to the current
454 stack pointer. */
72bba93b 455
cef4c2e7
PS
456 if (PC_IN_CALL_DUMMY (pc, 0, 0))
457 {
458 struct linked_proc_info *link;
2fe3b329 459 CORE_ADDR sp = read_next_frame_reg (next_frame, SP_REGNUM);
cef4c2e7
PS
460 alpha_extra_func_info_t found_proc_desc = NULL;
461 long min_distance = LONG_MAX;
462
463 for (link = linked_proc_desc_table; link; link = link->next)
464 {
465 long distance = (CORE_ADDR) PROC_DUMMY_FRAME (&link->info) - sp;
466 if (distance > 0 && distance < min_distance)
467 {
468 min_distance = distance;
469 found_proc_desc = &link->info;
470 }
471 }
472 if (found_proc_desc != NULL)
473 return found_proc_desc;
474 }
475
476 b = block_for_pc(pc);
72bba93b 477
cef4c2e7
PS
478 find_pc_partial_function (pc, NULL, &startaddr, NULL);
479 if (b == NULL)
480 sym = NULL;
481 else
482 {
483 if (startaddr > BLOCK_START (b))
484 /* This is the "pathological" case referred to in a comment in
485 print_frame_info. It might be better to move this check into
486 symbol reading. */
487 sym = NULL;
488 else
489 sym = lookup_symbol (MIPS_EFI_SYMBOL_NAME, b, LABEL_NAMESPACE,
490 0, NULL);
491 }
492
4f69fe46
JK
493 /* If we never found a PDR for this function in symbol reading, then
494 examine prologues to find the information. */
495 if (sym && ((mips_extra_func_info_t) SYMBOL_VALUE (sym))->pdr.framereg == -1)
496 sym = NULL;
497
cef4c2e7
PS
498 if (sym)
499 {
72bba93b
SG
500 /* IF this is the topmost frame AND
501 * (this proc does not have debugging information OR
cef4c2e7
PS
502 * the PC is in the procedure prologue)
503 * THEN create a "heuristic" proc_desc (by analyzing
504 * the actual code) to replace the "official" proc_desc.
505 */
506 proc_desc = (alpha_extra_func_info_t)SYMBOL_VALUE(sym);
72bba93b
SG
507 if (next_frame == NULL)
508 {
509 if (PROC_DESC_IS_DUMMY (proc_desc) || in_prologue (pc, proc_desc))
510 {
cef4c2e7 511 alpha_extra_func_info_t found_heuristic =
72bba93b
SG
512 heuristic_proc_desc (PROC_LOW_ADDR (proc_desc),
513 pc, next_frame);
cef4c2e7 514 if (found_heuristic)
de7ad6d8
JK
515 {
516 PROC_LOCALOFF (found_heuristic) =
517 PROC_LOCALOFF (proc_desc);
518 proc_desc = found_heuristic;
519 }
72bba93b
SG
520 }
521 }
cef4c2e7
PS
522 }
523 else
524 {
72bba93b
SG
525 /* Is linked_proc_desc_table really necessary? It only seems to be used
526 by procedure call dummys. However, the procedures being called ought
527 to have their own proc_descs, and even if they don't,
528 heuristic_proc_desc knows how to create them! */
529
530 register struct linked_proc_info *link;
531 for (link = linked_proc_desc_table; link; link = link->next)
532 if (PROC_LOW_ADDR(&link->info) <= pc
533 && PROC_HIGH_ADDR(&link->info) > pc)
534 return &link->info;
535
cef4c2e7
PS
536 if (startaddr == 0)
537 startaddr = heuristic_proc_start (pc);
538
539 proc_desc =
540 heuristic_proc_desc (startaddr, pc, next_frame);
541 }
542 return proc_desc;
543}
544
545alpha_extra_func_info_t cached_proc_desc;
546
669caa9c 547CORE_ADDR
cef4c2e7 548alpha_frame_chain(frame)
669caa9c 549 struct frame_info *frame;
cef4c2e7
PS
550{
551 alpha_extra_func_info_t proc_desc;
552 CORE_ADDR saved_pc = FRAME_SAVED_PC(frame);
553
554 if (saved_pc == 0 || inside_entry_file (saved_pc))
555 return 0;
556
557 proc_desc = find_proc_desc(saved_pc, frame);
558 if (!proc_desc)
559 return 0;
560
561 cached_proc_desc = proc_desc;
562
563 /* Fetch the frame pointer for a dummy frame from the procedure
564 descriptor. */
565 if (PROC_DESC_IS_DUMMY(proc_desc))
669caa9c 566 return (CORE_ADDR) PROC_DUMMY_FRAME(proc_desc);
cef4c2e7
PS
567
568 /* If no frame pointer and frame size is zero, we must be at end
569 of stack (or otherwise hosed). If we don't check frame size,
570 we loop forever if we see a zero size frame. */
571 if (PROC_FRAME_REG (proc_desc) == SP_REGNUM
572 && PROC_FRAME_OFFSET (proc_desc) == 0
cef4c2e7
PS
573 /* The previous frame from a sigtramp frame might be frameless
574 and have frame size zero. */
575 && !frame->signal_handler_caller)
0434c1a0
PS
576 {
577 /* The alpha __sigtramp routine is frameless and has a frame size
578 of zero, but we are able to backtrace through it. */
579 char *name;
580 find_pc_partial_function (saved_pc, &name,
581 (CORE_ADDR *)NULL, (CORE_ADDR *)NULL);
582 if (IN_SIGTRAMP (saved_pc, name))
583 return frame->frame;
584 else
585 return 0;
586 }
cef4c2e7
PS
587 else
588 return read_next_frame_reg(frame, PROC_FRAME_REG(proc_desc))
0434c1a0 589 + PROC_FRAME_OFFSET(proc_desc);
cef4c2e7
PS
590}
591
592void
669caa9c
SS
593init_extra_frame_info (frame)
594 struct frame_info *frame;
cef4c2e7 595{
cef4c2e7
PS
596 /* Use proc_desc calculated in frame_chain */
597 alpha_extra_func_info_t proc_desc =
669caa9c 598 frame->next ? cached_proc_desc : find_proc_desc(frame->pc, frame->next);
cef4c2e7 599
669caa9c
SS
600 frame->saved_regs = NULL;
601 frame->proc_desc =
cef4c2e7
PS
602 proc_desc == &temp_proc_desc ? 0 : proc_desc;
603 if (proc_desc)
604 {
cef4c2e7
PS
605 /* Get the locals offset from the procedure descriptor, it is valid
606 even if we are in the middle of the prologue. */
669caa9c 607 frame->localoff = PROC_LOCALOFF(proc_desc);
cef4c2e7 608
cef4c2e7 609 /* Fixup frame-pointer - only needed for top frame */
72bba93b 610
cef4c2e7
PS
611 /* Fetch the frame pointer for a dummy frame from the procedure
612 descriptor. */
613 if (PROC_DESC_IS_DUMMY(proc_desc))
669caa9c 614 frame->frame = (CORE_ADDR) PROC_DUMMY_FRAME(proc_desc);
72bba93b 615
cef4c2e7
PS
616 /* This may not be quite right, if proc has a real frame register.
617 Get the value of the frame relative sp, procedure might have been
618 interrupted by a signal at it's very start. */
669caa9c
SS
619 else if (frame->pc == PROC_LOW_ADDR (proc_desc) && !PROC_DESC_IS_DUMMY (proc_desc))
620 frame->frame = read_next_frame_reg (frame->next, SP_REGNUM);
cef4c2e7 621 else
669caa9c
SS
622 frame->frame = read_next_frame_reg (frame->next, PROC_FRAME_REG (proc_desc))
623 + PROC_FRAME_OFFSET (proc_desc);
cef4c2e7
PS
624
625 if (proc_desc == &temp_proc_desc)
cef4c2e7 626 {
669caa9c 627 frame->saved_regs = (struct frame_saved_regs*)
72bba93b
SG
628 obstack_alloc (&frame_cache_obstack,
629 sizeof (struct frame_saved_regs));
669caa9c
SS
630 *frame->saved_regs = temp_saved_regs;
631 frame->saved_regs->regs[PC_REGNUM] = frame->saved_regs->regs[RA_REGNUM];
cef4c2e7 632 }
cef4c2e7
PS
633 }
634}
635
636/* ALPHA stack frames are almost impenetrable. When execution stops,
637 we basically have to look at symbol information for the function
638 that we stopped in, which tells us *which* register (if any) is
639 the base of the frame pointer, and what offset from that register
640 the frame itself is at.
641
642 This presents a problem when trying to examine a stack in memory
643 (that isn't executing at the moment), using the "frame" command. We
644 don't have a PC, nor do we have any registers except SP.
645
646 This routine takes two arguments, SP and PC, and tries to make the
647 cached frames look as if these two arguments defined a frame on the
648 cache. This allows the rest of info frame to extract the important
649 arguments without difficulty. */
650
669caa9c 651struct frame_info *
cef4c2e7
PS
652setup_arbitrary_frame (argc, argv)
653 int argc;
669caa9c 654 CORE_ADDR *argv;
cef4c2e7
PS
655{
656 if (argc != 2)
657 error ("ALPHA frame specifications require two arguments: sp and pc");
658
659 return create_new_frame (argv[0], argv[1]);
660}
661
662/* The alpha passes the first six arguments in the registers, the rest on
663 the stack. The register arguments are eventually transferred to the
664 argument transfer area immediately below the stack by the called function
665 anyway. So we `push' at least six arguments on the stack, `reload' the
666 argument registers and then adjust the stack pointer to point past the
667 sixth argument. This algorithm simplifies the passing of a large struct
668 which extends from the registers to the stack.
669 If the called function is returning a structure, the address of the
670 structure to be returned is passed as a hidden first argument. */
671
cef4c2e7
PS
672CORE_ADDR
673alpha_push_arguments (nargs, args, sp, struct_return, struct_addr)
7810d333
JK
674 int nargs;
675 value_ptr *args;
676 CORE_ADDR sp;
677 int struct_return;
678 CORE_ADDR struct_addr;
cef4c2e7
PS
679{
680 register i;
681 int accumulate_size = struct_return ? 8 : 0;
3e6b0674 682 int arg_regs_size = ALPHA_NUM_ARG_REGS * 8;
cef4c2e7
PS
683 struct alpha_arg { char *contents; int len; int offset; };
684 struct alpha_arg *alpha_args =
685 (struct alpha_arg*)alloca (nargs * sizeof (struct alpha_arg));
686 register struct alpha_arg *m_arg;
687 char raw_buffer[sizeof (CORE_ADDR)];
688 int required_arg_regs;
689
690 for (i = 0, m_arg = alpha_args; i < nargs; i++, m_arg++)
691 {
5222ca60 692 value_ptr arg = args[i];
cef4c2e7
PS
693 /* Cast argument to long if necessary as the compiler does it too. */
694 if (TYPE_LENGTH (VALUE_TYPE (arg)) < TYPE_LENGTH (builtin_type_long))
695 arg = value_cast (builtin_type_long, arg);
696 m_arg->len = TYPE_LENGTH (VALUE_TYPE (arg));
697 m_arg->offset = accumulate_size;
698 accumulate_size = (accumulate_size + m_arg->len + 7) & ~7;
699 m_arg->contents = VALUE_CONTENTS(arg);
700 }
701
702 /* Determine required argument register loads, loading an argument register
703 is expensive as it uses three ptrace calls. */
704 required_arg_regs = accumulate_size / 8;
3e6b0674
PS
705 if (required_arg_regs > ALPHA_NUM_ARG_REGS)
706 required_arg_regs = ALPHA_NUM_ARG_REGS;
cef4c2e7
PS
707
708 /* Make room for the arguments on the stack. */
709 if (accumulate_size < arg_regs_size)
710 accumulate_size = arg_regs_size;
711 sp -= accumulate_size;
712
713 /* Keep sp aligned to a multiple of 16 as the compiler does it too. */
714 sp &= ~15;
715
716 /* `Push' arguments on the stack. */
717 for (i = nargs; m_arg--, --i >= 0; )
718 write_memory(sp + m_arg->offset, m_arg->contents, m_arg->len);
719 if (struct_return)
720 {
721 store_address (raw_buffer, sizeof (CORE_ADDR), struct_addr);
722 write_memory (sp, raw_buffer, sizeof (CORE_ADDR));
723 }
724
725 /* Load the argument registers. */
726 for (i = 0; i < required_arg_regs; i++)
727 {
728 LONGEST val;
729
730 val = read_memory_integer (sp + i * 8, 8);
731 write_register (A0_REGNUM + i, val);
732 write_register (FPA0_REGNUM + i, val);
733 }
734
735 return sp + arg_regs_size;
736}
737
738void
739alpha_push_dummy_frame()
740{
741 int ireg;
72bba93b
SG
742 struct linked_proc_info *link;
743 alpha_extra_func_info_t proc_desc;
cef4c2e7
PS
744 CORE_ADDR sp = read_register (SP_REGNUM);
745 CORE_ADDR save_address;
746 char raw_buffer[MAX_REGISTER_RAW_SIZE];
747 unsigned long mask;
748
72bba93b 749 link = (struct linked_proc_info *) xmalloc(sizeof (struct linked_proc_info));
cef4c2e7
PS
750 link->next = linked_proc_desc_table;
751 linked_proc_desc_table = link;
72bba93b
SG
752
753 proc_desc = &link->info;
cef4c2e7
PS
754
755 /*
756 * The registers we must save are all those not preserved across
757 * procedure calls.
758 * In addition, we must save the PC and RA.
759 *
760 * Dummy frame layout:
761 * (high memory)
762 * Saved PC
763 * Saved F30
764 * ...
765 * Saved F0
766 * Saved R29
767 * ...
768 * Saved R0
769 * Saved R26 (RA)
770 * Parameter build area
771 * (low memory)
772 */
773
774/* MASK(i,j) == (1<<i) + (1<<(i+1)) + ... + (1<<j)). Assume i<=j<31. */
775#define MASK(i,j) (((1L << ((j)+1)) - 1) ^ ((1L << (i)) - 1))
776#define GEN_REG_SAVE_MASK (MASK(0,8) | MASK(16,29))
777#define GEN_REG_SAVE_COUNT 24
778#define FLOAT_REG_SAVE_MASK (MASK(0,1) | MASK(10,30))
779#define FLOAT_REG_SAVE_COUNT 23
780 /* The special register is the PC as we have no bit for it in the save masks.
781 alpha_frame_saved_pc knows where the pc is saved in a dummy frame. */
782#define SPECIAL_REG_SAVE_COUNT 1
783
784 PROC_REG_MASK(proc_desc) = GEN_REG_SAVE_MASK;
785 PROC_FREG_MASK(proc_desc) = FLOAT_REG_SAVE_MASK;
786 /* PROC_REG_OFFSET is the offset from the dummy frame to the saved RA,
787 but keep SP aligned to a multiple of 16. */
788 PROC_REG_OFFSET(proc_desc) =
789 - ((8 * (SPECIAL_REG_SAVE_COUNT
790 + GEN_REG_SAVE_COUNT
791 + FLOAT_REG_SAVE_COUNT)
792 + 15) & ~15);
793 PROC_FREG_OFFSET(proc_desc) =
794 PROC_REG_OFFSET(proc_desc) + 8 * GEN_REG_SAVE_COUNT;
795
796 /* Save general registers.
797 The return address register is the first saved register, all other
798 registers follow in ascending order.
799 The PC is saved immediately below the SP. */
800 save_address = sp + PROC_REG_OFFSET(proc_desc);
801 store_address (raw_buffer, 8, read_register (RA_REGNUM));
802 write_memory (save_address, raw_buffer, 8);
803 save_address += 8;
804 mask = PROC_REG_MASK(proc_desc) & 0xffffffffL;
805 for (ireg = 0; mask; ireg++, mask >>= 1)
806 if (mask & 1)
807 {
808 if (ireg == RA_REGNUM)
809 continue;
810 store_address (raw_buffer, 8, read_register (ireg));
811 write_memory (save_address, raw_buffer, 8);
812 save_address += 8;
813 }
814
815 store_address (raw_buffer, 8, read_register (PC_REGNUM));
816 write_memory (sp - 8, raw_buffer, 8);
817
818 /* Save floating point registers. */
819 save_address = sp + PROC_FREG_OFFSET(proc_desc);
820 mask = PROC_FREG_MASK(proc_desc) & 0xffffffffL;
821 for (ireg = 0; mask; ireg++, mask >>= 1)
822 if (mask & 1)
823 {
824 store_address (raw_buffer, 8, read_register (ireg + FP0_REGNUM));
825 write_memory (save_address, raw_buffer, 8);
826 save_address += 8;
827 }
828
829 /* Set and save the frame address for the dummy.
830 This is tricky. The only registers that are suitable for a frame save
831 are those that are preserved across procedure calls (s0-s6). But if
832 a read system call is interrupted and then a dummy call is made
833 (see testsuite/gdb.t17/interrupt.exp) the dummy call hangs till the read
834 is satisfied. Then it returns with the s0-s6 registers set to the values
835 on entry to the read system call and our dummy frame pointer would be
836 destroyed. So we save the dummy frame in the proc_desc and handle the
837 retrieval of the frame pointer of a dummy specifically. The frame register
838 is set to the virtual frame (pseudo) register, it's value will always
839 be read as zero and will help us to catch any errors in the dummy frame
840 retrieval code. */
841 PROC_DUMMY_FRAME(proc_desc) = sp;
842 PROC_FRAME_REG(proc_desc) = FP_REGNUM;
843 PROC_FRAME_OFFSET(proc_desc) = 0;
844 sp += PROC_REG_OFFSET(proc_desc);
845 write_register (SP_REGNUM, sp);
846
72bba93b 847 PROC_LOW_ADDR(proc_desc) = CALL_DUMMY_ADDRESS ();
cef4c2e7
PS
848 PROC_HIGH_ADDR(proc_desc) = PROC_LOW_ADDR(proc_desc) + 4;
849
850 SET_PROC_DESC_IS_DUMMY(proc_desc);
851 PROC_PC_REG(proc_desc) = RA_REGNUM;
852}
853
854void
855alpha_pop_frame()
856{
857 register int regnum;
669caa9c 858 struct frame_info *frame = get_current_frame ();
cef4c2e7
PS
859 CORE_ADDR new_sp = frame->frame;
860
861 alpha_extra_func_info_t proc_desc = frame->proc_desc;
862
863 write_register (PC_REGNUM, FRAME_SAVED_PC(frame));
72bba93b
SG
864 if (frame->saved_regs == NULL)
865 alpha_find_saved_regs (frame);
cef4c2e7
PS
866 if (proc_desc)
867 {
868 for (regnum = 32; --regnum >= 0; )
869 if (PROC_REG_MASK(proc_desc) & (1 << regnum))
870 write_register (regnum,
871 read_memory_integer (frame->saved_regs->regs[regnum],
872 8));
873 for (regnum = 32; --regnum >= 0; )
874 if (PROC_FREG_MASK(proc_desc) & (1 << regnum))
875 write_register (regnum + FP0_REGNUM,
876 read_memory_integer (frame->saved_regs->regs[regnum + FP0_REGNUM], 8));
877 }
878 write_register (SP_REGNUM, new_sp);
879 flush_cached_frames ();
cef4c2e7
PS
880
881 if (proc_desc && PROC_DESC_IS_DUMMY(proc_desc))
882 {
883 struct linked_proc_info *pi_ptr, *prev_ptr;
884
885 for (pi_ptr = linked_proc_desc_table, prev_ptr = NULL;
886 pi_ptr != NULL;
887 prev_ptr = pi_ptr, pi_ptr = pi_ptr->next)
888 {
889 if (&pi_ptr->info == proc_desc)
890 break;
891 }
892
893 if (pi_ptr == NULL)
894 error ("Can't locate dummy extra frame info\n");
895
896 if (prev_ptr != NULL)
897 prev_ptr->next = pi_ptr->next;
898 else
899 linked_proc_desc_table = pi_ptr->next;
900
901 free (pi_ptr);
902 }
903}
904\f
905/* To skip prologues, I use this predicate. Returns either PC itself
906 if the code at PC does not look like a function prologue; otherwise
907 returns an address that (if we're lucky) follows the prologue. If
908 LENIENT, then we must skip everything which is involved in setting
909 up the frame (it's OK to skip more, just so long as we don't skip
910 anything which might clobber the registers which are being saved.
911 Currently we must not skip more on the alpha, but we might the lenient
912 stuff some day. */
913
914CORE_ADDR
915alpha_skip_prologue (pc, lenient)
916 CORE_ADDR pc;
917 int lenient;
918{
919 unsigned long inst;
920 int offset;
72bba93b 921 CORE_ADDR post_prologue_pc;
2fe3b329
PS
922 char buf[4];
923
924#ifdef GDB_TARGET_HAS_SHARED_LIBS
925 /* Silently return the unaltered pc upon memory errors.
926 This could happen on OSF/1 if decode_line_1 tries to skip the
927 prologue for quickstarted shared library functions when the
928 shared library is not yet mapped in.
929 Reading target memory is slow over serial lines, so we perform
930 this check only if the target has shared libraries. */
931 if (target_read_memory (pc, buf, 4))
932 return pc;
933#endif
72bba93b
SG
934
935 /* See if we can determine the end of the prologue via the symbol table.
936 If so, then return either PC, or the PC after the prologue, whichever
937 is greater. */
938
939 post_prologue_pc = after_prologue (pc, NULL);
940
941 if (post_prologue_pc != 0)
942 return max (pc, post_prologue_pc);
943
944 /* Can't determine prologue from the symbol table, need to examine
945 instructions. */
cef4c2e7
PS
946
947 /* Skip the typical prologue instructions. These are the stack adjustment
948 instruction and the instructions that save registers on the stack
949 or in the gcc frame. */
950 for (offset = 0; offset < 100; offset += 4)
951 {
cef4c2e7
PS
952 int status;
953
954 status = read_memory_nobpt (pc + offset, buf, 4);
955 if (status)
956 memory_error (status, pc + offset);
957 inst = extract_unsigned_integer (buf, 4);
958
959 /* The alpha has no delay slots. But let's keep the lenient stuff,
960 we might need it for something else in the future. */
961 if (lenient && 0)
962 continue;
963
964 if ((inst & 0xffff0000) == 0x27bb0000) /* ldah $gp,n($t12) */
965 continue;
966 if ((inst & 0xffff0000) == 0x23bd0000) /* lda $gp,n($gp) */
967 continue;
968 if ((inst & 0xffff0000) == 0x23de0000) /* lda $sp,n($sp) */
969 continue;
970 else if ((inst & 0xfc1f0000) == 0xb41e0000
971 && (inst & 0xffff0000) != 0xb7fe0000)
972 continue; /* stq reg,n($sp) */
973 /* reg != $zero */
974 else if ((inst & 0xfc1f0000) == 0x9c1e0000
975 && (inst & 0xffff0000) != 0x9ffe0000)
976 continue; /* stt reg,n($sp) */
977 /* reg != $zero */
978 else if (inst == 0x47de040f) /* bis sp,sp,fp */
979 continue;
980 else
981 break;
982 }
983 return pc + offset;
984}
985
e3be225e 986#if 0
cef4c2e7
PS
987/* Is address PC in the prologue (loosely defined) for function at
988 STARTADDR? */
989
990static int
991alpha_in_lenient_prologue (startaddr, pc)
992 CORE_ADDR startaddr;
993 CORE_ADDR pc;
994{
995 CORE_ADDR end_prologue = alpha_skip_prologue (startaddr, 1);
996 return pc >= startaddr && pc < end_prologue;
997}
e3be225e 998#endif
cef4c2e7 999
ad09cb2b
PS
1000/* The alpha needs a conversion between register and memory format if
1001 the register is a floating point register and
1002 memory format is float, as the register format must be double
1003 or
1004 memory format is an integer with 4 bytes or less, as the representation
1005 of integers in floating point registers is different. */
1006void
1007alpha_register_convert_to_virtual (regnum, valtype, raw_buffer, virtual_buffer)
1008 int regnum;
1009 struct type *valtype;
1010 char *raw_buffer;
1011 char *virtual_buffer;
1012{
1013 if (TYPE_LENGTH (valtype) >= REGISTER_RAW_SIZE (regnum))
1014 {
1015 memcpy (virtual_buffer, raw_buffer, REGISTER_VIRTUAL_SIZE (regnum));
1016 return;
1017 }
1018
1019 if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1020 {
1021 double d = extract_floating (raw_buffer, REGISTER_RAW_SIZE (regnum));
1022 store_floating (virtual_buffer, TYPE_LENGTH (valtype), d);
1023 }
1024 else if (TYPE_CODE (valtype) == TYPE_CODE_INT && TYPE_LENGTH (valtype) <= 4)
1025 {
1026 unsigned LONGEST l;
1027 l = extract_unsigned_integer (raw_buffer, REGISTER_RAW_SIZE (regnum));
1028 l = ((l >> 32) & 0xc0000000) | ((l >> 29) & 0x3fffffff);
1029 store_unsigned_integer (virtual_buffer, TYPE_LENGTH (valtype), l);
1030 }
1031 else
1032 error ("Cannot retrieve value from floating point register");
1033}
1034
1035void
1036alpha_register_convert_to_raw (valtype, regnum, virtual_buffer, raw_buffer)
1037 struct type *valtype;
1038 int regnum;
1039 char *virtual_buffer;
1040 char *raw_buffer;
1041{
1042 if (TYPE_LENGTH (valtype) >= REGISTER_RAW_SIZE (regnum))
1043 {
1044 memcpy (raw_buffer, virtual_buffer, REGISTER_RAW_SIZE (regnum));
1045 return;
1046 }
1047
1048 if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1049 {
1050 double d = extract_floating (virtual_buffer, TYPE_LENGTH (valtype));
1051 store_floating (raw_buffer, REGISTER_RAW_SIZE (regnum), d);
1052 }
1053 else if (TYPE_CODE (valtype) == TYPE_CODE_INT && TYPE_LENGTH (valtype) <= 4)
1054 {
1055 unsigned LONGEST l;
1056 if (TYPE_UNSIGNED (valtype))
1057 l = extract_unsigned_integer (virtual_buffer, TYPE_LENGTH (valtype));
1058 else
1059 l = extract_signed_integer (virtual_buffer, TYPE_LENGTH (valtype));
1060 l = ((l & 0xc0000000) << 32) | ((l & 0x3fffffff) << 29);
1061 store_unsigned_integer (raw_buffer, REGISTER_RAW_SIZE (regnum), l);
1062 }
1063 else
1064 error ("Cannot store value in floating point register");
1065}
1066
cef4c2e7
PS
1067/* Given a return value in `regbuf' with a type `valtype',
1068 extract and copy its value into `valbuf'. */
669caa9c 1069
cef4c2e7
PS
1070void
1071alpha_extract_return_value (valtype, regbuf, valbuf)
1072 struct type *valtype;
1073 char regbuf[REGISTER_BYTES];
1074 char *valbuf;
1075{
1076 int regnum;
1077
1078 regnum = TYPE_CODE (valtype) == TYPE_CODE_FLT ? FP0_REGNUM : V0_REGNUM;
1079
1080 memcpy (valbuf, regbuf + REGISTER_BYTE (regnum), TYPE_LENGTH (valtype));
1081}
1082
1083/* Given a return value in `regbuf' with a type `valtype',
7810d333 1084 write its value into the appropriate register. */
669caa9c 1085
cef4c2e7
PS
1086void
1087alpha_store_return_value (valtype, valbuf)
1088 struct type *valtype;
1089 char *valbuf;
1090{
1091 int regnum;
1092 char raw_buffer[MAX_REGISTER_RAW_SIZE];
1093
1094 regnum = TYPE_CODE (valtype) == TYPE_CODE_FLT ? FP0_REGNUM : V0_REGNUM;
1095 memcpy(raw_buffer, valbuf, TYPE_LENGTH (valtype));
1096
1097 write_register_bytes(REGISTER_BYTE (regnum), raw_buffer, TYPE_LENGTH (valtype));
1098}
1099
cef4c2e7
PS
1100/* Just like reinit_frame_cache, but with the right arguments to be
1101 callable as an sfunc. */
669caa9c 1102
cef4c2e7
PS
1103static void
1104reinit_frame_cache_sfunc (args, from_tty, c)
1105 char *args;
1106 int from_tty;
1107 struct cmd_list_element *c;
1108{
1109 reinit_frame_cache ();
1110}
1111
72bba93b
SG
1112/* This is the definition of CALL_DUMMY_ADDRESS. It's a heuristic that is used
1113 to find a convenient place in the text segment to stick a breakpoint to
1114 detect the completion of a target function call (ala call_function_by_hand).
1115 */
1116
1117CORE_ADDR
1118alpha_call_dummy_address ()
1119{
1120 CORE_ADDR entry;
1121 struct minimal_symbol *sym;
1122
1123 entry = entry_point_address ();
1124
1125 if (entry != 0)
1126 return entry;
1127
2d336b1b 1128 sym = lookup_minimal_symbol ("_Prelude", NULL, symfile_objfile);
72bba93b
SG
1129
1130 if (!sym || MSYMBOL_TYPE (sym) != mst_text)
1131 return 0;
1132 else
1133 return SYMBOL_VALUE_ADDRESS (sym) + 4;
1134}
1135
cef4c2e7
PS
1136void
1137_initialize_alpha_tdep ()
1138{
1139 struct cmd_list_element *c;
1140
18b46e7c
SS
1141 tm_print_insn = print_insn_alpha;
1142
cef4c2e7
PS
1143 /* Let the user set the fence post for heuristic_proc_start. */
1144
1145 /* We really would like to have both "0" and "unlimited" work, but
1146 command.c doesn't deal with that. So make it a var_zinteger
1147 because the user can always use "999999" or some such for unlimited. */
1148 c = add_set_cmd ("heuristic-fence-post", class_support, var_zinteger,
1149 (char *) &heuristic_fence_post,
1150 "\
1151Set the distance searched for the start of a function.\n\
1152If you are debugging a stripped executable, GDB needs to search through the\n\
1153program for the start of a function. This command sets the distance of the\n\
1154search. The only need to set it is when debugging a stripped executable.",
1155 &setlist);
1156 /* We need to throw away the frame cache when we set this, since it
1157 might change our ability to get backtraces. */
1158 c->function.sfunc = reinit_frame_cache_sfunc;
1159 add_show_from_set (c, &showlist);
1160}
This page took 0.133894 seconds and 4 git commands to generate.