Fix problems with infinite recursion when printing a class
[deliverable/binutils-gdb.git] / gdb / alpha-tdep.c
1 /* Target-dependent code for the ALPHA architecture, for GDB, the GNU Debugger.
2 Copyright 1993, 1994, 1995 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 2 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, write to the Free Software
18 Foundation, 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"
28 #include "symfile.h"
29 #include "objfiles.h"
30 #include <string.h>
31
32 /* FIXME: Some of this code should perhaps be merged with mips-tdep.c. */
33
34 /* FIXME: Put this declaration in frame.h. */
35 extern struct obstack frame_cache_obstack;
36 \f
37
38 /* Forward declarations. */
39
40 static CORE_ADDR read_next_frame_reg PARAMS ((struct frame_info *, int));
41
42 static CORE_ADDR heuristic_proc_start PARAMS ((CORE_ADDR));
43
44 static alpha_extra_func_info_t heuristic_proc_desc PARAMS ((CORE_ADDR,
45 CORE_ADDR,
46 struct frame_info *));
47
48 static alpha_extra_func_info_t find_proc_desc PARAMS ((CORE_ADDR,
49 struct frame_info *));
50
51 #if 0
52 static int alpha_in_lenient_prologue PARAMS ((CORE_ADDR, CORE_ADDR));
53 #endif
54
55 static void reinit_frame_cache_sfunc PARAMS ((char *, int,
56 struct cmd_list_element *));
57
58 static CORE_ADDR after_prologue PARAMS ((CORE_ADDR pc,
59 alpha_extra_func_info_t proc_desc));
60
61 static int in_prologue PARAMS ((CORE_ADDR pc,
62 alpha_extra_func_info_t proc_desc));
63
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. */
67 static 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 | |
81 | | | --- |-------------------------------|<-- FRAME_LOCALS_ADDRESS
82 | | | | |
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
128 struct 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
135 /* Guaranteed to set fci->saved_regs to some values (it never leaves it
136 NULL). */
137
138 void
139 alpha_find_saved_regs (frame)
140 struct frame_info *frame;
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
148 frame->saved_regs = (struct frame_saved_regs *)
149 obstack_alloc (&frame_cache_obstack, sizeof(struct frame_saved_regs));
150 memset (frame->saved_regs, 0, sizeof (struct frame_saved_regs));
151
152 proc_desc = frame->proc_desc;
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
162 reg_position = frame->frame + PROC_REG_OFFSET (proc_desc);
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 {
171 frame->saved_regs->regs[returnreg] = reg_position;
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 {
180 frame->saved_regs->regs[ireg] = reg_position;
181 reg_position += 8;
182 }
183
184 /* Fill in the offsets for the registers which float_mask says
185 were saved. */
186
187 reg_position = frame->frame + PROC_FREG_OFFSET (proc_desc);
188 mask = PROC_FREG_MASK (proc_desc);
189
190 for (ireg = 0; ireg <= 31 ; ++ireg)
191 if (mask & (1 << ireg))
192 {
193 frame->saved_regs->regs[FP0_REGNUM+ireg] = reg_position;
194 reg_position += 8;
195 }
196
197 frame->saved_regs->regs[PC_REGNUM] = frame->saved_regs->regs[returnreg];
198 }
199
200 static CORE_ADDR
201 read_next_frame_reg(fi, regno)
202 struct frame_info *fi;
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;
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 }
237 }
238 return read_register(regno);
239 }
240
241 CORE_ADDR
242 alpha_frame_saved_pc(frame)
243 struct frame_info *frame;
244 {
245 alpha_extra_func_info_t proc_desc = frame->proc_desc;
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);
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
257 CORE_ADDR
258 alpha_saved_pc_after_call (frame)
259 struct frame_info *frame;
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
268 static struct alpha_extra_func_info temp_proc_desc;
269 static 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
275 static CORE_ADDR
276 heuristic_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 ("\
309 This 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\
311 increase the size of the search with the `set heuristic-fence-post' command.\n\
312 \n\
313 Otherwise, 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
328 static alpha_extra_func_info_t
329 heuristic_proc_desc(start_pc, limit_pc, next_frame)
330 CORE_ADDR start_pc, limit_pc;
331 struct frame_info *next_frame;
332 {
333 CORE_ADDR sp = read_next_frame_reg (next_frame, SP_REGNUM);
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;
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;
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;
378 PROC_LOCALOFF(&temp_proc_desc) = 0; /* XXX - bogus */
379 return &temp_proc_desc;
380 }
381
382 /* This returns the PC of the first inst after the prologue. If we can't
383 find the prologue, then return 0. */
384
385 static CORE_ADDR
386 after_prologue (pc, proc_desc)
387 CORE_ADDR pc;
388 alpha_extra_func_info_t proc_desc;
389 {
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
422 static int
423 in_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
438 static alpha_extra_func_info_t
439 find_proc_desc (pc, next_frame)
440 CORE_ADDR pc;
441 struct frame_info *next_frame;
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. */
455
456 if (PC_IN_CALL_DUMMY (pc, 0, 0))
457 {
458 struct linked_proc_info *link;
459 CORE_ADDR sp = read_next_frame_reg (next_frame, SP_REGNUM);
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);
477
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
493 if (sym)
494 {
495 /* IF this is the topmost frame AND
496 * (this proc does not have debugging information OR
497 * the PC is in the procedure prologue)
498 * THEN create a "heuristic" proc_desc (by analyzing
499 * the actual code) to replace the "official" proc_desc.
500 */
501 proc_desc = (alpha_extra_func_info_t)SYMBOL_VALUE(sym);
502 if (next_frame == NULL)
503 {
504 if (PROC_DESC_IS_DUMMY (proc_desc) || in_prologue (pc, proc_desc))
505 {
506 alpha_extra_func_info_t found_heuristic =
507 heuristic_proc_desc (PROC_LOW_ADDR (proc_desc),
508 pc, next_frame);
509 if (found_heuristic)
510 {
511 PROC_LOCALOFF (found_heuristic) =
512 PROC_LOCALOFF (proc_desc);
513 proc_desc = found_heuristic;
514 }
515 }
516 }
517 }
518 else
519 {
520 /* Is linked_proc_desc_table really necessary? It only seems to be used
521 by procedure call dummys. However, the procedures being called ought
522 to have their own proc_descs, and even if they don't,
523 heuristic_proc_desc knows how to create them! */
524
525 register struct linked_proc_info *link;
526 for (link = linked_proc_desc_table; link; link = link->next)
527 if (PROC_LOW_ADDR(&link->info) <= pc
528 && PROC_HIGH_ADDR(&link->info) > pc)
529 return &link->info;
530
531 if (startaddr == 0)
532 startaddr = heuristic_proc_start (pc);
533
534 proc_desc =
535 heuristic_proc_desc (startaddr, pc, next_frame);
536 }
537 return proc_desc;
538 }
539
540 alpha_extra_func_info_t cached_proc_desc;
541
542 CORE_ADDR
543 alpha_frame_chain(frame)
544 struct frame_info *frame;
545 {
546 alpha_extra_func_info_t proc_desc;
547 CORE_ADDR saved_pc = FRAME_SAVED_PC(frame);
548
549 if (saved_pc == 0 || inside_entry_file (saved_pc))
550 return 0;
551
552 proc_desc = find_proc_desc(saved_pc, frame);
553 if (!proc_desc)
554 return 0;
555
556 cached_proc_desc = proc_desc;
557
558 /* Fetch the frame pointer for a dummy frame from the procedure
559 descriptor. */
560 if (PROC_DESC_IS_DUMMY(proc_desc))
561 return (CORE_ADDR) PROC_DUMMY_FRAME(proc_desc);
562
563 /* If no frame pointer and frame size is zero, we must be at end
564 of stack (or otherwise hosed). If we don't check frame size,
565 we loop forever if we see a zero size frame. */
566 if (PROC_FRAME_REG (proc_desc) == SP_REGNUM
567 && PROC_FRAME_OFFSET (proc_desc) == 0
568 /* The previous frame from a sigtramp frame might be frameless
569 and have frame size zero. */
570 && !frame->signal_handler_caller)
571 {
572 /* The alpha __sigtramp routine is frameless and has a frame size
573 of zero, but we are able to backtrace through it. */
574 char *name;
575 find_pc_partial_function (saved_pc, &name,
576 (CORE_ADDR *)NULL, (CORE_ADDR *)NULL);
577 if (IN_SIGTRAMP (saved_pc, name))
578 return frame->frame;
579 else
580 return 0;
581 }
582 else
583 return read_next_frame_reg(frame, PROC_FRAME_REG(proc_desc))
584 + PROC_FRAME_OFFSET(proc_desc);
585 }
586
587 void
588 init_extra_frame_info (frame)
589 struct frame_info *frame;
590 {
591 /* Use proc_desc calculated in frame_chain */
592 alpha_extra_func_info_t proc_desc =
593 frame->next ? cached_proc_desc : find_proc_desc(frame->pc, frame->next);
594
595 frame->saved_regs = NULL;
596 frame->proc_desc =
597 proc_desc == &temp_proc_desc ? 0 : proc_desc;
598 if (proc_desc)
599 {
600 /* Get the locals offset from the procedure descriptor, it is valid
601 even if we are in the middle of the prologue. */
602 frame->localoff = PROC_LOCALOFF(proc_desc);
603
604 /* Fixup frame-pointer - only needed for top frame */
605
606 /* Fetch the frame pointer for a dummy frame from the procedure
607 descriptor. */
608 if (PROC_DESC_IS_DUMMY(proc_desc))
609 frame->frame = (CORE_ADDR) PROC_DUMMY_FRAME(proc_desc);
610
611 /* This may not be quite right, if proc has a real frame register.
612 Get the value of the frame relative sp, procedure might have been
613 interrupted by a signal at it's very start. */
614 else if (frame->pc == PROC_LOW_ADDR (proc_desc) && !PROC_DESC_IS_DUMMY (proc_desc))
615 frame->frame = read_next_frame_reg (frame->next, SP_REGNUM);
616 else
617 frame->frame = read_next_frame_reg (frame->next, PROC_FRAME_REG (proc_desc))
618 + PROC_FRAME_OFFSET (proc_desc);
619
620 if (proc_desc == &temp_proc_desc)
621 {
622 frame->saved_regs = (struct frame_saved_regs*)
623 obstack_alloc (&frame_cache_obstack,
624 sizeof (struct frame_saved_regs));
625 *frame->saved_regs = temp_saved_regs;
626 frame->saved_regs->regs[PC_REGNUM] = frame->saved_regs->regs[RA_REGNUM];
627 }
628 }
629 }
630
631 /* ALPHA stack frames are almost impenetrable. When execution stops,
632 we basically have to look at symbol information for the function
633 that we stopped in, which tells us *which* register (if any) is
634 the base of the frame pointer, and what offset from that register
635 the frame itself is at.
636
637 This presents a problem when trying to examine a stack in memory
638 (that isn't executing at the moment), using the "frame" command. We
639 don't have a PC, nor do we have any registers except SP.
640
641 This routine takes two arguments, SP and PC, and tries to make the
642 cached frames look as if these two arguments defined a frame on the
643 cache. This allows the rest of info frame to extract the important
644 arguments without difficulty. */
645
646 struct frame_info *
647 setup_arbitrary_frame (argc, argv)
648 int argc;
649 CORE_ADDR *argv;
650 {
651 if (argc != 2)
652 error ("ALPHA frame specifications require two arguments: sp and pc");
653
654 return create_new_frame (argv[0], argv[1]);
655 }
656
657 /* The alpha passes the first six arguments in the registers, the rest on
658 the stack. The register arguments are eventually transferred to the
659 argument transfer area immediately below the stack by the called function
660 anyway. So we `push' at least six arguments on the stack, `reload' the
661 argument registers and then adjust the stack pointer to point past the
662 sixth argument. This algorithm simplifies the passing of a large struct
663 which extends from the registers to the stack.
664 If the called function is returning a structure, the address of the
665 structure to be returned is passed as a hidden first argument. */
666
667 CORE_ADDR
668 alpha_push_arguments (nargs, args, sp, struct_return, struct_addr)
669 int nargs;
670 value_ptr *args;
671 CORE_ADDR sp;
672 int struct_return;
673 CORE_ADDR struct_addr;
674 {
675 register i;
676 int accumulate_size = struct_return ? 8 : 0;
677 int arg_regs_size = ALPHA_NUM_ARG_REGS * 8;
678 struct alpha_arg { char *contents; int len; int offset; };
679 struct alpha_arg *alpha_args =
680 (struct alpha_arg*)alloca (nargs * sizeof (struct alpha_arg));
681 register struct alpha_arg *m_arg;
682 char raw_buffer[sizeof (CORE_ADDR)];
683 int required_arg_regs;
684
685 for (i = 0, m_arg = alpha_args; i < nargs; i++, m_arg++)
686 {
687 value_ptr arg = args[i];
688 /* Cast argument to long if necessary as the compiler does it too. */
689 if (TYPE_LENGTH (VALUE_TYPE (arg)) < TYPE_LENGTH (builtin_type_long))
690 arg = value_cast (builtin_type_long, arg);
691 m_arg->len = TYPE_LENGTH (VALUE_TYPE (arg));
692 m_arg->offset = accumulate_size;
693 accumulate_size = (accumulate_size + m_arg->len + 7) & ~7;
694 m_arg->contents = VALUE_CONTENTS(arg);
695 }
696
697 /* Determine required argument register loads, loading an argument register
698 is expensive as it uses three ptrace calls. */
699 required_arg_regs = accumulate_size / 8;
700 if (required_arg_regs > ALPHA_NUM_ARG_REGS)
701 required_arg_regs = ALPHA_NUM_ARG_REGS;
702
703 /* Make room for the arguments on the stack. */
704 if (accumulate_size < arg_regs_size)
705 accumulate_size = arg_regs_size;
706 sp -= accumulate_size;
707
708 /* Keep sp aligned to a multiple of 16 as the compiler does it too. */
709 sp &= ~15;
710
711 /* `Push' arguments on the stack. */
712 for (i = nargs; m_arg--, --i >= 0; )
713 write_memory(sp + m_arg->offset, m_arg->contents, m_arg->len);
714 if (struct_return)
715 {
716 store_address (raw_buffer, sizeof (CORE_ADDR), struct_addr);
717 write_memory (sp, raw_buffer, sizeof (CORE_ADDR));
718 }
719
720 /* Load the argument registers. */
721 for (i = 0; i < required_arg_regs; i++)
722 {
723 LONGEST val;
724
725 val = read_memory_integer (sp + i * 8, 8);
726 write_register (A0_REGNUM + i, val);
727 write_register (FPA0_REGNUM + i, val);
728 }
729
730 return sp + arg_regs_size;
731 }
732
733 void
734 alpha_push_dummy_frame()
735 {
736 int ireg;
737 struct linked_proc_info *link;
738 alpha_extra_func_info_t proc_desc;
739 CORE_ADDR sp = read_register (SP_REGNUM);
740 CORE_ADDR save_address;
741 char raw_buffer[MAX_REGISTER_RAW_SIZE];
742 unsigned long mask;
743
744 link = (struct linked_proc_info *) xmalloc(sizeof (struct linked_proc_info));
745 link->next = linked_proc_desc_table;
746 linked_proc_desc_table = link;
747
748 proc_desc = &link->info;
749
750 /*
751 * The registers we must save are all those not preserved across
752 * procedure calls.
753 * In addition, we must save the PC and RA.
754 *
755 * Dummy frame layout:
756 * (high memory)
757 * Saved PC
758 * Saved F30
759 * ...
760 * Saved F0
761 * Saved R29
762 * ...
763 * Saved R0
764 * Saved R26 (RA)
765 * Parameter build area
766 * (low memory)
767 */
768
769 /* MASK(i,j) == (1<<i) + (1<<(i+1)) + ... + (1<<j)). Assume i<=j<31. */
770 #define MASK(i,j) (((1L << ((j)+1)) - 1) ^ ((1L << (i)) - 1))
771 #define GEN_REG_SAVE_MASK (MASK(0,8) | MASK(16,29))
772 #define GEN_REG_SAVE_COUNT 24
773 #define FLOAT_REG_SAVE_MASK (MASK(0,1) | MASK(10,30))
774 #define FLOAT_REG_SAVE_COUNT 23
775 /* The special register is the PC as we have no bit for it in the save masks.
776 alpha_frame_saved_pc knows where the pc is saved in a dummy frame. */
777 #define SPECIAL_REG_SAVE_COUNT 1
778
779 PROC_REG_MASK(proc_desc) = GEN_REG_SAVE_MASK;
780 PROC_FREG_MASK(proc_desc) = FLOAT_REG_SAVE_MASK;
781 /* PROC_REG_OFFSET is the offset from the dummy frame to the saved RA,
782 but keep SP aligned to a multiple of 16. */
783 PROC_REG_OFFSET(proc_desc) =
784 - ((8 * (SPECIAL_REG_SAVE_COUNT
785 + GEN_REG_SAVE_COUNT
786 + FLOAT_REG_SAVE_COUNT)
787 + 15) & ~15);
788 PROC_FREG_OFFSET(proc_desc) =
789 PROC_REG_OFFSET(proc_desc) + 8 * GEN_REG_SAVE_COUNT;
790
791 /* Save general registers.
792 The return address register is the first saved register, all other
793 registers follow in ascending order.
794 The PC is saved immediately below the SP. */
795 save_address = sp + PROC_REG_OFFSET(proc_desc);
796 store_address (raw_buffer, 8, read_register (RA_REGNUM));
797 write_memory (save_address, raw_buffer, 8);
798 save_address += 8;
799 mask = PROC_REG_MASK(proc_desc) & 0xffffffffL;
800 for (ireg = 0; mask; ireg++, mask >>= 1)
801 if (mask & 1)
802 {
803 if (ireg == RA_REGNUM)
804 continue;
805 store_address (raw_buffer, 8, read_register (ireg));
806 write_memory (save_address, raw_buffer, 8);
807 save_address += 8;
808 }
809
810 store_address (raw_buffer, 8, read_register (PC_REGNUM));
811 write_memory (sp - 8, raw_buffer, 8);
812
813 /* Save floating point registers. */
814 save_address = sp + PROC_FREG_OFFSET(proc_desc);
815 mask = PROC_FREG_MASK(proc_desc) & 0xffffffffL;
816 for (ireg = 0; mask; ireg++, mask >>= 1)
817 if (mask & 1)
818 {
819 store_address (raw_buffer, 8, read_register (ireg + FP0_REGNUM));
820 write_memory (save_address, raw_buffer, 8);
821 save_address += 8;
822 }
823
824 /* Set and save the frame address for the dummy.
825 This is tricky. The only registers that are suitable for a frame save
826 are those that are preserved across procedure calls (s0-s6). But if
827 a read system call is interrupted and then a dummy call is made
828 (see testsuite/gdb.t17/interrupt.exp) the dummy call hangs till the read
829 is satisfied. Then it returns with the s0-s6 registers set to the values
830 on entry to the read system call and our dummy frame pointer would be
831 destroyed. So we save the dummy frame in the proc_desc and handle the
832 retrieval of the frame pointer of a dummy specifically. The frame register
833 is set to the virtual frame (pseudo) register, it's value will always
834 be read as zero and will help us to catch any errors in the dummy frame
835 retrieval code. */
836 PROC_DUMMY_FRAME(proc_desc) = sp;
837 PROC_FRAME_REG(proc_desc) = FP_REGNUM;
838 PROC_FRAME_OFFSET(proc_desc) = 0;
839 sp += PROC_REG_OFFSET(proc_desc);
840 write_register (SP_REGNUM, sp);
841
842 PROC_LOW_ADDR(proc_desc) = CALL_DUMMY_ADDRESS ();
843 PROC_HIGH_ADDR(proc_desc) = PROC_LOW_ADDR(proc_desc) + 4;
844
845 SET_PROC_DESC_IS_DUMMY(proc_desc);
846 PROC_PC_REG(proc_desc) = RA_REGNUM;
847 }
848
849 void
850 alpha_pop_frame()
851 {
852 register int regnum;
853 struct frame_info *frame = get_current_frame ();
854 CORE_ADDR new_sp = frame->frame;
855
856 alpha_extra_func_info_t proc_desc = frame->proc_desc;
857
858 write_register (PC_REGNUM, FRAME_SAVED_PC(frame));
859 if (frame->saved_regs == NULL)
860 alpha_find_saved_regs (frame);
861 if (proc_desc)
862 {
863 for (regnum = 32; --regnum >= 0; )
864 if (PROC_REG_MASK(proc_desc) & (1 << regnum))
865 write_register (regnum,
866 read_memory_integer (frame->saved_regs->regs[regnum],
867 8));
868 for (regnum = 32; --regnum >= 0; )
869 if (PROC_FREG_MASK(proc_desc) & (1 << regnum))
870 write_register (regnum + FP0_REGNUM,
871 read_memory_integer (frame->saved_regs->regs[regnum + FP0_REGNUM], 8));
872 }
873 write_register (SP_REGNUM, new_sp);
874 flush_cached_frames ();
875
876 if (proc_desc && PROC_DESC_IS_DUMMY(proc_desc))
877 {
878 struct linked_proc_info *pi_ptr, *prev_ptr;
879
880 for (pi_ptr = linked_proc_desc_table, prev_ptr = NULL;
881 pi_ptr != NULL;
882 prev_ptr = pi_ptr, pi_ptr = pi_ptr->next)
883 {
884 if (&pi_ptr->info == proc_desc)
885 break;
886 }
887
888 if (pi_ptr == NULL)
889 error ("Can't locate dummy extra frame info\n");
890
891 if (prev_ptr != NULL)
892 prev_ptr->next = pi_ptr->next;
893 else
894 linked_proc_desc_table = pi_ptr->next;
895
896 free (pi_ptr);
897 }
898 }
899 \f
900 /* To skip prologues, I use this predicate. Returns either PC itself
901 if the code at PC does not look like a function prologue; otherwise
902 returns an address that (if we're lucky) follows the prologue. If
903 LENIENT, then we must skip everything which is involved in setting
904 up the frame (it's OK to skip more, just so long as we don't skip
905 anything which might clobber the registers which are being saved.
906 Currently we must not skip more on the alpha, but we might the lenient
907 stuff some day. */
908
909 CORE_ADDR
910 alpha_skip_prologue (pc, lenient)
911 CORE_ADDR pc;
912 int lenient;
913 {
914 unsigned long inst;
915 int offset;
916 CORE_ADDR post_prologue_pc;
917 char buf[4];
918
919 #ifdef GDB_TARGET_HAS_SHARED_LIBS
920 /* Silently return the unaltered pc upon memory errors.
921 This could happen on OSF/1 if decode_line_1 tries to skip the
922 prologue for quickstarted shared library functions when the
923 shared library is not yet mapped in.
924 Reading target memory is slow over serial lines, so we perform
925 this check only if the target has shared libraries. */
926 if (target_read_memory (pc, buf, 4))
927 return pc;
928 #endif
929
930 /* See if we can determine the end of the prologue via the symbol table.
931 If so, then return either PC, or the PC after the prologue, whichever
932 is greater. */
933
934 post_prologue_pc = after_prologue (pc, NULL);
935
936 if (post_prologue_pc != 0)
937 return max (pc, post_prologue_pc);
938
939 /* Can't determine prologue from the symbol table, need to examine
940 instructions. */
941
942 /* Skip the typical prologue instructions. These are the stack adjustment
943 instruction and the instructions that save registers on the stack
944 or in the gcc frame. */
945 for (offset = 0; offset < 100; offset += 4)
946 {
947 int status;
948
949 status = read_memory_nobpt (pc + offset, buf, 4);
950 if (status)
951 memory_error (status, pc + offset);
952 inst = extract_unsigned_integer (buf, 4);
953
954 /* The alpha has no delay slots. But let's keep the lenient stuff,
955 we might need it for something else in the future. */
956 if (lenient && 0)
957 continue;
958
959 if ((inst & 0xffff0000) == 0x27bb0000) /* ldah $gp,n($t12) */
960 continue;
961 if ((inst & 0xffff0000) == 0x23bd0000) /* lda $gp,n($gp) */
962 continue;
963 if ((inst & 0xffff0000) == 0x23de0000) /* lda $sp,n($sp) */
964 continue;
965 else if ((inst & 0xfc1f0000) == 0xb41e0000
966 && (inst & 0xffff0000) != 0xb7fe0000)
967 continue; /* stq reg,n($sp) */
968 /* reg != $zero */
969 else if ((inst & 0xfc1f0000) == 0x9c1e0000
970 && (inst & 0xffff0000) != 0x9ffe0000)
971 continue; /* stt reg,n($sp) */
972 /* reg != $zero */
973 else if (inst == 0x47de040f) /* bis sp,sp,fp */
974 continue;
975 else
976 break;
977 }
978 return pc + offset;
979 }
980
981 #if 0
982 /* Is address PC in the prologue (loosely defined) for function at
983 STARTADDR? */
984
985 static int
986 alpha_in_lenient_prologue (startaddr, pc)
987 CORE_ADDR startaddr;
988 CORE_ADDR pc;
989 {
990 CORE_ADDR end_prologue = alpha_skip_prologue (startaddr, 1);
991 return pc >= startaddr && pc < end_prologue;
992 }
993 #endif
994
995 /* The alpha needs a conversion between register and memory format if
996 the register is a floating point register and
997 memory format is float, as the register format must be double
998 or
999 memory format is an integer with 4 bytes or less, as the representation
1000 of integers in floating point registers is different. */
1001 void
1002 alpha_register_convert_to_virtual (regnum, valtype, raw_buffer, virtual_buffer)
1003 int regnum;
1004 struct type *valtype;
1005 char *raw_buffer;
1006 char *virtual_buffer;
1007 {
1008 if (TYPE_LENGTH (valtype) >= REGISTER_RAW_SIZE (regnum))
1009 {
1010 memcpy (virtual_buffer, raw_buffer, REGISTER_VIRTUAL_SIZE (regnum));
1011 return;
1012 }
1013
1014 if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1015 {
1016 double d = extract_floating (raw_buffer, REGISTER_RAW_SIZE (regnum));
1017 store_floating (virtual_buffer, TYPE_LENGTH (valtype), d);
1018 }
1019 else if (TYPE_CODE (valtype) == TYPE_CODE_INT && TYPE_LENGTH (valtype) <= 4)
1020 {
1021 unsigned LONGEST l;
1022 l = extract_unsigned_integer (raw_buffer, REGISTER_RAW_SIZE (regnum));
1023 l = ((l >> 32) & 0xc0000000) | ((l >> 29) & 0x3fffffff);
1024 store_unsigned_integer (virtual_buffer, TYPE_LENGTH (valtype), l);
1025 }
1026 else
1027 error ("Cannot retrieve value from floating point register");
1028 }
1029
1030 void
1031 alpha_register_convert_to_raw (valtype, regnum, virtual_buffer, raw_buffer)
1032 struct type *valtype;
1033 int regnum;
1034 char *virtual_buffer;
1035 char *raw_buffer;
1036 {
1037 if (TYPE_LENGTH (valtype) >= REGISTER_RAW_SIZE (regnum))
1038 {
1039 memcpy (raw_buffer, virtual_buffer, REGISTER_RAW_SIZE (regnum));
1040 return;
1041 }
1042
1043 if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1044 {
1045 double d = extract_floating (virtual_buffer, TYPE_LENGTH (valtype));
1046 store_floating (raw_buffer, REGISTER_RAW_SIZE (regnum), d);
1047 }
1048 else if (TYPE_CODE (valtype) == TYPE_CODE_INT && TYPE_LENGTH (valtype) <= 4)
1049 {
1050 unsigned LONGEST l;
1051 if (TYPE_UNSIGNED (valtype))
1052 l = extract_unsigned_integer (virtual_buffer, TYPE_LENGTH (valtype));
1053 else
1054 l = extract_signed_integer (virtual_buffer, TYPE_LENGTH (valtype));
1055 l = ((l & 0xc0000000) << 32) | ((l & 0x3fffffff) << 29);
1056 store_unsigned_integer (raw_buffer, REGISTER_RAW_SIZE (regnum), l);
1057 }
1058 else
1059 error ("Cannot store value in floating point register");
1060 }
1061
1062 /* Given a return value in `regbuf' with a type `valtype',
1063 extract and copy its value into `valbuf'. */
1064
1065 void
1066 alpha_extract_return_value (valtype, regbuf, valbuf)
1067 struct type *valtype;
1068 char regbuf[REGISTER_BYTES];
1069 char *valbuf;
1070 {
1071 int regnum;
1072
1073 regnum = TYPE_CODE (valtype) == TYPE_CODE_FLT ? FP0_REGNUM : V0_REGNUM;
1074
1075 memcpy (valbuf, regbuf + REGISTER_BYTE (regnum), TYPE_LENGTH (valtype));
1076 }
1077
1078 /* Given a return value in `regbuf' with a type `valtype',
1079 write its value into the appropriate register. */
1080
1081 void
1082 alpha_store_return_value (valtype, valbuf)
1083 struct type *valtype;
1084 char *valbuf;
1085 {
1086 int regnum;
1087 char raw_buffer[MAX_REGISTER_RAW_SIZE];
1088
1089 regnum = TYPE_CODE (valtype) == TYPE_CODE_FLT ? FP0_REGNUM : V0_REGNUM;
1090 memcpy(raw_buffer, valbuf, TYPE_LENGTH (valtype));
1091
1092 write_register_bytes(REGISTER_BYTE (regnum), raw_buffer, TYPE_LENGTH (valtype));
1093 }
1094
1095 /* Just like reinit_frame_cache, but with the right arguments to be
1096 callable as an sfunc. */
1097
1098 static void
1099 reinit_frame_cache_sfunc (args, from_tty, c)
1100 char *args;
1101 int from_tty;
1102 struct cmd_list_element *c;
1103 {
1104 reinit_frame_cache ();
1105 }
1106
1107 /* This is the definition of CALL_DUMMY_ADDRESS. It's a heuristic that is used
1108 to find a convenient place in the text segment to stick a breakpoint to
1109 detect the completion of a target function call (ala call_function_by_hand).
1110 */
1111
1112 CORE_ADDR
1113 alpha_call_dummy_address ()
1114 {
1115 CORE_ADDR entry;
1116 struct minimal_symbol *sym;
1117
1118 entry = entry_point_address ();
1119
1120 if (entry != 0)
1121 return entry;
1122
1123 sym = lookup_minimal_symbol ("_Prelude", NULL, symfile_objfile);
1124
1125 if (!sym || MSYMBOL_TYPE (sym) != mst_text)
1126 return 0;
1127 else
1128 return SYMBOL_VALUE_ADDRESS (sym) + 4;
1129 }
1130
1131 void
1132 _initialize_alpha_tdep ()
1133 {
1134 struct cmd_list_element *c;
1135
1136 tm_print_insn = print_insn_alpha;
1137
1138 /* Let the user set the fence post for heuristic_proc_start. */
1139
1140 /* We really would like to have both "0" and "unlimited" work, but
1141 command.c doesn't deal with that. So make it a var_zinteger
1142 because the user can always use "999999" or some such for unlimited. */
1143 c = add_set_cmd ("heuristic-fence-post", class_support, var_zinteger,
1144 (char *) &heuristic_fence_post,
1145 "\
1146 Set the distance searched for the start of a function.\n\
1147 If you are debugging a stripped executable, GDB needs to search through the\n\
1148 program for the start of a function. This command sets the distance of the\n\
1149 search. The only need to set it is when debugging a stripped executable.",
1150 &setlist);
1151 /* We need to throw away the frame cache when we set this, since it
1152 might change our ability to get backtraces. */
1153 c->function.sfunc = reinit_frame_cache_sfunc;
1154 add_show_from_set (c, &showlist);
1155 }
This page took 0.053679 seconds and 4 git commands to generate.