6345279381a21ac1143e832f5b785e5631557ceb
[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, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "defs.h"
23 #include "frame.h"
24 #include "inferior.h"
25 #include "symtab.h"
26 #include "value.h"
27 #include "gdbcmd.h"
28 #include "gdbcore.h"
29 #include "dis-asm.h"
30 #include "symfile.h"
31 #include "objfiles.h"
32 #include "gdb_string.h"
33 #include "linespec.h"
34 #include "regcache.h"
35 #include "doublest.h"
36
37 struct frame_extra_info
38 {
39 alpha_extra_func_info_t proc_desc;
40 int localoff;
41 int pc_reg;
42 };
43
44 /* FIXME: Some of this code should perhaps be merged with mips-tdep.c. */
45
46 /* Prototypes for local functions. */
47
48 static void alpha_find_saved_regs (struct frame_info *);
49
50 static alpha_extra_func_info_t push_sigtramp_desc (CORE_ADDR low_addr);
51
52 static CORE_ADDR read_next_frame_reg (struct frame_info *, int);
53
54 static CORE_ADDR heuristic_proc_start (CORE_ADDR);
55
56 static alpha_extra_func_info_t heuristic_proc_desc (CORE_ADDR,
57 CORE_ADDR,
58 struct frame_info *);
59
60 static alpha_extra_func_info_t find_proc_desc (CORE_ADDR,
61 struct frame_info *);
62
63 #if 0
64 static int alpha_in_lenient_prologue (CORE_ADDR, CORE_ADDR);
65 #endif
66
67 static void reinit_frame_cache_sfunc (char *, int, struct cmd_list_element *);
68
69 static CORE_ADDR after_prologue (CORE_ADDR pc,
70 alpha_extra_func_info_t proc_desc);
71
72 static int alpha_in_prologue (CORE_ADDR pc,
73 alpha_extra_func_info_t proc_desc);
74
75 static int alpha_about_to_return (CORE_ADDR pc);
76
77 void _initialize_alpha_tdep (void);
78
79 /* Heuristic_proc_start may hunt through the text section for a long
80 time across a 2400 baud serial line. Allows the user to limit this
81 search. */
82 static unsigned int heuristic_fence_post = 0;
83 /* *INDENT-OFF* */
84 /* Layout of a stack frame on the alpha:
85
86 | |
87 pdr members: | 7th ... nth arg, |
88 | `pushed' by caller. |
89 | |
90 ----------------|-------------------------------|<-- old_sp == vfp
91 ^ ^ ^ ^ | |
92 | | | | | |
93 | |localoff | Copies of 1st .. 6th |
94 | | | | | argument if necessary. |
95 | | | v | |
96 | | | --- |-------------------------------|<-- FRAME_LOCALS_ADDRESS
97 | | | | |
98 | | | | Locals and temporaries. |
99 | | | | |
100 | | | |-------------------------------|
101 | | | | |
102 |-fregoffset | Saved float registers. |
103 | | | | F9 |
104 | | | | . |
105 | | | | . |
106 | | | | F2 |
107 | | v | |
108 | | -------|-------------------------------|
109 | | | |
110 | | | Saved registers. |
111 | | | S6 |
112 |-regoffset | . |
113 | | | . |
114 | | | S0 |
115 | | | pdr.pcreg |
116 | v | |
117 | ----------|-------------------------------|
118 | | |
119 frameoffset | Argument build area, gets |
120 | | 7th ... nth arg for any |
121 | | called procedure. |
122 v | |
123 -------------|-------------------------------|<-- sp
124 | |
125 */
126 /* *INDENT-ON* */
127
128
129
130 #define PROC_LOW_ADDR(proc) ((proc)->pdr.adr) /* least address */
131 /* These next two fields are kind of being hijacked. I wonder if
132 iline is too small for the values it needs to hold, if GDB is
133 running on a 32-bit host. */
134 #define PROC_HIGH_ADDR(proc) ((proc)->pdr.iline) /* upper address bound */
135 #define PROC_DUMMY_FRAME(proc) ((proc)->pdr.cbLineOffset) /*CALL_DUMMY frame */
136 #define PROC_FRAME_OFFSET(proc) ((proc)->pdr.frameoffset)
137 #define PROC_FRAME_REG(proc) ((proc)->pdr.framereg)
138 #define PROC_REG_MASK(proc) ((proc)->pdr.regmask)
139 #define PROC_FREG_MASK(proc) ((proc)->pdr.fregmask)
140 #define PROC_REG_OFFSET(proc) ((proc)->pdr.regoffset)
141 #define PROC_FREG_OFFSET(proc) ((proc)->pdr.fregoffset)
142 #define PROC_PC_REG(proc) ((proc)->pdr.pcreg)
143 #define PROC_LOCALOFF(proc) ((proc)->pdr.localoff)
144 #define PROC_SYMBOL(proc) (*(struct symbol**)&(proc)->pdr.isym)
145 #define _PROC_MAGIC_ 0x0F0F0F0F
146 #define PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym == _PROC_MAGIC_)
147 #define SET_PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym = _PROC_MAGIC_)
148
149 struct linked_proc_info
150 {
151 struct alpha_extra_func_info info;
152 struct linked_proc_info *next;
153 }
154 *linked_proc_desc_table = NULL;
155 \f
156 int
157 alpha_osf_in_sigtramp (CORE_ADDR pc, char *func_name)
158 {
159 return (func_name != NULL && STREQ ("__sigtramp", func_name));
160 }
161
162 /* Under GNU/Linux, signal handler invocations can be identified by the
163 designated code sequence that is used to return from a signal
164 handler. In particular, the return address of a signal handler
165 points to the following sequence (the first instruction is quadword
166 aligned):
167
168 bis $30,$30,$16
169 addq $31,0x67,$0
170 call_pal callsys
171
172 Each instruction has a unique encoding, so we simply attempt to
173 match the instruction the pc is pointing to with any of the above
174 instructions. If there is a hit, we know the offset to the start
175 of the designated sequence and can then check whether we really are
176 executing in a designated sequence. If not, -1 is returned,
177 otherwise the offset from the start of the desingated sequence is
178 returned.
179
180 There is a slight chance of false hits: code could jump into the
181 middle of the designated sequence, in which case there is no
182 guarantee that we are in the middle of a sigreturn syscall. Don't
183 think this will be a problem in praxis, though.
184 */
185
186 #ifndef TM_LINUXALPHA_H
187 /* HACK: Provide a prototype when compiling this file for non
188 linuxalpha targets. */
189 long alpha_linux_sigtramp_offset (CORE_ADDR pc);
190 #endif
191 long
192 alpha_linux_sigtramp_offset (CORE_ADDR pc)
193 {
194 unsigned int i[3], w;
195 long off;
196
197 if (read_memory_nobpt (pc, (char *) &w, 4) != 0)
198 return -1;
199
200 off = -1;
201 switch (w)
202 {
203 case 0x47de0410:
204 off = 0;
205 break; /* bis $30,$30,$16 */
206 case 0x43ecf400:
207 off = 4;
208 break; /* addq $31,0x67,$0 */
209 case 0x00000083:
210 off = 8;
211 break; /* call_pal callsys */
212 default:
213 return -1;
214 }
215 pc -= off;
216 if (pc & 0x7)
217 {
218 /* designated sequence is not quadword aligned */
219 return -1;
220 }
221
222 if (read_memory_nobpt (pc, (char *) i, sizeof (i)) != 0)
223 return -1;
224
225 if (i[0] == 0x47de0410 && i[1] == 0x43ecf400 && i[2] == 0x00000083)
226 return off;
227
228 return -1;
229 }
230 \f
231
232 /* Under OSF/1, the __sigtramp routine is frameless and has a frame
233 size of zero, but we are able to backtrace through it. */
234 CORE_ADDR
235 alpha_osf_skip_sigtramp_frame (struct frame_info *frame, CORE_ADDR pc)
236 {
237 char *name;
238 find_pc_partial_function (pc, &name, (CORE_ADDR *) NULL, (CORE_ADDR *) NULL);
239 if (IN_SIGTRAMP (pc, name))
240 return frame->frame;
241 else
242 return 0;
243 }
244 \f
245
246 /* Dynamically create a signal-handler caller procedure descriptor for
247 the signal-handler return code starting at address LOW_ADDR. The
248 descriptor is added to the linked_proc_desc_table. */
249
250 static alpha_extra_func_info_t
251 push_sigtramp_desc (CORE_ADDR low_addr)
252 {
253 struct linked_proc_info *link;
254 alpha_extra_func_info_t proc_desc;
255
256 link = (struct linked_proc_info *)
257 xmalloc (sizeof (struct linked_proc_info));
258 link->next = linked_proc_desc_table;
259 linked_proc_desc_table = link;
260
261 proc_desc = &link->info;
262
263 proc_desc->numargs = 0;
264 PROC_LOW_ADDR (proc_desc) = low_addr;
265 PROC_HIGH_ADDR (proc_desc) = low_addr + 3 * 4;
266 PROC_DUMMY_FRAME (proc_desc) = 0;
267 PROC_FRAME_OFFSET (proc_desc) = 0x298; /* sizeof(struct sigcontext_struct) */
268 PROC_FRAME_REG (proc_desc) = SP_REGNUM;
269 PROC_REG_MASK (proc_desc) = 0xffff;
270 PROC_FREG_MASK (proc_desc) = 0xffff;
271 PROC_PC_REG (proc_desc) = 26;
272 PROC_LOCALOFF (proc_desc) = 0;
273 SET_PROC_DESC_IS_DYN_SIGTRAMP (proc_desc);
274 return (proc_desc);
275 }
276 \f
277
278 char *
279 alpha_register_name (int regno)
280 {
281 static char *register_names[] =
282 {
283 "v0", "t0", "t1", "t2", "t3", "t4", "t5", "t6",
284 "t7", "s0", "s1", "s2", "s3", "s4", "s5", "fp",
285 "a0", "a1", "a2", "a3", "a4", "a5", "t8", "t9",
286 "t10", "t11", "ra", "t12", "at", "gp", "sp", "zero",
287 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
288 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
289 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
290 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "fpcr",
291 "pc", "vfp",
292 };
293
294 if (regno < 0)
295 return (NULL);
296 if (regno >= (sizeof(register_names) / sizeof(*register_names)))
297 return (NULL);
298 return (register_names[regno]);
299 }
300
301 int
302 alpha_cannot_fetch_register (int regno)
303 {
304 return (regno == FP_REGNUM || regno == ZERO_REGNUM);
305 }
306
307 int
308 alpha_cannot_store_register (int regno)
309 {
310 return (regno == FP_REGNUM || regno == ZERO_REGNUM);
311 }
312
313 int
314 alpha_register_convertible (int regno)
315 {
316 return (regno >= FP0_REGNUM && regno <= FP0_REGNUM + 31);
317 }
318 \f
319
320 /* Guaranteed to set frame->saved_regs to some values (it never leaves it
321 NULL). */
322
323 static void
324 alpha_find_saved_regs (struct frame_info *frame)
325 {
326 int ireg;
327 CORE_ADDR reg_position;
328 unsigned long mask;
329 alpha_extra_func_info_t proc_desc;
330 int returnreg;
331
332 frame_saved_regs_zalloc (frame);
333
334 /* If it is the frame for __sigtramp, the saved registers are located
335 in a sigcontext structure somewhere on the stack. __sigtramp
336 passes a pointer to the sigcontext structure on the stack.
337 If the stack layout for __sigtramp changes, or if sigcontext offsets
338 change, we might have to update this code. */
339 #ifndef SIGFRAME_PC_OFF
340 #define SIGFRAME_PC_OFF (2 * 8)
341 #define SIGFRAME_REGSAVE_OFF (4 * 8)
342 #define SIGFRAME_FPREGSAVE_OFF (SIGFRAME_REGSAVE_OFF + 32 * 8 + 8)
343 #endif
344 if (frame->signal_handler_caller)
345 {
346 CORE_ADDR sigcontext_addr;
347
348 sigcontext_addr = SIGCONTEXT_ADDR (frame);
349 for (ireg = 0; ireg < 32; ireg++)
350 {
351 reg_position = sigcontext_addr + SIGFRAME_REGSAVE_OFF + ireg * 8;
352 frame->saved_regs[ireg] = reg_position;
353 }
354 for (ireg = 0; ireg < 32; ireg++)
355 {
356 reg_position = sigcontext_addr + SIGFRAME_FPREGSAVE_OFF + ireg * 8;
357 frame->saved_regs[FP0_REGNUM + ireg] = reg_position;
358 }
359 frame->saved_regs[PC_REGNUM] = sigcontext_addr + SIGFRAME_PC_OFF;
360 return;
361 }
362
363 proc_desc = frame->extra_info->proc_desc;
364 if (proc_desc == NULL)
365 /* I'm not sure how/whether this can happen. Normally when we can't
366 find a proc_desc, we "synthesize" one using heuristic_proc_desc
367 and set the saved_regs right away. */
368 return;
369
370 /* Fill in the offsets for the registers which gen_mask says
371 were saved. */
372
373 reg_position = frame->frame + PROC_REG_OFFSET (proc_desc);
374 mask = PROC_REG_MASK (proc_desc);
375
376 returnreg = PROC_PC_REG (proc_desc);
377
378 /* Note that RA is always saved first, regardless of its actual
379 register number. */
380 if (mask & (1 << returnreg))
381 {
382 frame->saved_regs[returnreg] = reg_position;
383 reg_position += 8;
384 mask &= ~(1 << returnreg); /* Clear bit for RA so we
385 don't save again later. */
386 }
387
388 for (ireg = 0; ireg <= 31; ++ireg)
389 if (mask & (1 << ireg))
390 {
391 frame->saved_regs[ireg] = reg_position;
392 reg_position += 8;
393 }
394
395 /* Fill in the offsets for the registers which float_mask says
396 were saved. */
397
398 reg_position = frame->frame + PROC_FREG_OFFSET (proc_desc);
399 mask = PROC_FREG_MASK (proc_desc);
400
401 for (ireg = 0; ireg <= 31; ++ireg)
402 if (mask & (1 << ireg))
403 {
404 frame->saved_regs[FP0_REGNUM + ireg] = reg_position;
405 reg_position += 8;
406 }
407
408 frame->saved_regs[PC_REGNUM] = frame->saved_regs[returnreg];
409 }
410
411 void
412 alpha_frame_init_saved_regs (struct frame_info *fi)
413 {
414 if (fi->saved_regs == NULL)
415 alpha_find_saved_regs (fi);
416 fi->saved_regs[SP_REGNUM] = fi->frame;
417 }
418
419 static CORE_ADDR
420 read_next_frame_reg (struct frame_info *fi, int regno)
421 {
422 for (; fi; fi = fi->next)
423 {
424 /* We have to get the saved sp from the sigcontext
425 if it is a signal handler frame. */
426 if (regno == SP_REGNUM && !fi->signal_handler_caller)
427 return fi->frame;
428 else
429 {
430 if (fi->saved_regs == NULL)
431 alpha_find_saved_regs (fi);
432 if (fi->saved_regs[regno])
433 return read_memory_integer (fi->saved_regs[regno], 8);
434 }
435 }
436 return read_register (regno);
437 }
438
439 CORE_ADDR
440 alpha_frame_saved_pc (struct frame_info *frame)
441 {
442 alpha_extra_func_info_t proc_desc = frame->extra_info->proc_desc;
443 /* We have to get the saved pc from the sigcontext
444 if it is a signal handler frame. */
445 int pcreg = frame->signal_handler_caller ? PC_REGNUM
446 : frame->extra_info->pc_reg;
447
448 if (proc_desc && PROC_DESC_IS_DUMMY (proc_desc))
449 return read_memory_integer (frame->frame - 8, 8);
450
451 return read_next_frame_reg (frame, pcreg);
452 }
453
454 CORE_ADDR
455 alpha_saved_pc_after_call (struct frame_info *frame)
456 {
457 CORE_ADDR pc = frame->pc;
458 CORE_ADDR tmp;
459 alpha_extra_func_info_t proc_desc;
460 int pcreg;
461
462 /* Skip over shared library trampoline if necessary. */
463 tmp = SKIP_TRAMPOLINE_CODE (pc);
464 if (tmp != 0)
465 pc = tmp;
466
467 proc_desc = find_proc_desc (pc, frame->next);
468 pcreg = proc_desc ? PROC_PC_REG (proc_desc) : RA_REGNUM;
469
470 if (frame->signal_handler_caller)
471 return alpha_frame_saved_pc (frame);
472 else
473 return read_register (pcreg);
474 }
475
476
477 static struct alpha_extra_func_info temp_proc_desc;
478 static CORE_ADDR temp_saved_regs[NUM_REGS];
479
480 /* Nonzero if instruction at PC is a return instruction. "ret
481 $zero,($ra),1" on alpha. */
482
483 static int
484 alpha_about_to_return (CORE_ADDR pc)
485 {
486 return read_memory_integer (pc, 4) == 0x6bfa8001;
487 }
488
489
490
491 /* This fencepost looks highly suspicious to me. Removing it also
492 seems suspicious as it could affect remote debugging across serial
493 lines. */
494
495 static CORE_ADDR
496 heuristic_proc_start (CORE_ADDR pc)
497 {
498 CORE_ADDR start_pc = pc;
499 CORE_ADDR fence = start_pc - heuristic_fence_post;
500
501 if (start_pc == 0)
502 return 0;
503
504 if (heuristic_fence_post == UINT_MAX
505 || fence < VM_MIN_ADDRESS)
506 fence = VM_MIN_ADDRESS;
507
508 /* search back for previous return */
509 for (start_pc -= 4;; start_pc -= 4)
510 if (start_pc < fence)
511 {
512 /* It's not clear to me why we reach this point when
513 stop_soon_quietly, but with this test, at least we
514 don't print out warnings for every child forked (eg, on
515 decstation). 22apr93 rich@cygnus.com. */
516 if (!stop_soon_quietly)
517 {
518 static int blurb_printed = 0;
519
520 if (fence == VM_MIN_ADDRESS)
521 warning ("Hit beginning of text section without finding");
522 else
523 warning ("Hit heuristic-fence-post without finding");
524
525 warning ("enclosing function for address 0x%s", paddr_nz (pc));
526 if (!blurb_printed)
527 {
528 printf_filtered ("\
529 This warning occurs if you are debugging a function without any symbols\n\
530 (for example, in a stripped executable). In that case, you may wish to\n\
531 increase the size of the search with the `set heuristic-fence-post' command.\n\
532 \n\
533 Otherwise, you told GDB there was a function where there isn't one, or\n\
534 (more likely) you have encountered a bug in GDB.\n");
535 blurb_printed = 1;
536 }
537 }
538
539 return 0;
540 }
541 else if (alpha_about_to_return (start_pc))
542 break;
543
544 start_pc += 4; /* skip return */
545 return start_pc;
546 }
547
548 static alpha_extra_func_info_t
549 heuristic_proc_desc (CORE_ADDR start_pc, CORE_ADDR limit_pc,
550 struct frame_info *next_frame)
551 {
552 CORE_ADDR sp = read_next_frame_reg (next_frame, SP_REGNUM);
553 CORE_ADDR cur_pc;
554 int frame_size;
555 int has_frame_reg = 0;
556 unsigned long reg_mask = 0;
557 int pcreg = -1;
558
559 if (start_pc == 0)
560 return NULL;
561 memset (&temp_proc_desc, '\0', sizeof (temp_proc_desc));
562 memset (&temp_saved_regs, '\0', SIZEOF_FRAME_SAVED_REGS);
563 PROC_LOW_ADDR (&temp_proc_desc) = start_pc;
564
565 if (start_pc + 200 < limit_pc)
566 limit_pc = start_pc + 200;
567 frame_size = 0;
568 for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += 4)
569 {
570 char buf[4];
571 unsigned long word;
572 int status;
573
574 status = read_memory_nobpt (cur_pc, buf, 4);
575 if (status)
576 memory_error (status, cur_pc);
577 word = extract_unsigned_integer (buf, 4);
578
579 if ((word & 0xffff0000) == 0x23de0000) /* lda $sp,n($sp) */
580 {
581 if (word & 0x8000)
582 frame_size += (-word) & 0xffff;
583 else
584 /* Exit loop if a positive stack adjustment is found, which
585 usually means that the stack cleanup code in the function
586 epilogue is reached. */
587 break;
588 }
589 else if ((word & 0xfc1f0000) == 0xb41e0000 /* stq reg,n($sp) */
590 && (word & 0xffff0000) != 0xb7fe0000) /* reg != $zero */
591 {
592 int reg = (word & 0x03e00000) >> 21;
593 reg_mask |= 1 << reg;
594 temp_saved_regs[reg] = sp + (short) word;
595
596 /* Starting with OSF/1-3.2C, the system libraries are shipped
597 without local symbols, but they still contain procedure
598 descriptors without a symbol reference. GDB is currently
599 unable to find these procedure descriptors and uses
600 heuristic_proc_desc instead.
601 As some low level compiler support routines (__div*, __add*)
602 use a non-standard return address register, we have to
603 add some heuristics to determine the return address register,
604 or stepping over these routines will fail.
605 Usually the return address register is the first register
606 saved on the stack, but assembler optimization might
607 rearrange the register saves.
608 So we recognize only a few registers (t7, t9, ra) within
609 the procedure prologue as valid return address registers.
610 If we encounter a return instruction, we extract the
611 the return address register from it.
612
613 FIXME: Rewriting GDB to access the procedure descriptors,
614 e.g. via the minimal symbol table, might obviate this hack. */
615 if (pcreg == -1
616 && cur_pc < (start_pc + 80)
617 && (reg == T7_REGNUM || reg == T9_REGNUM || reg == RA_REGNUM))
618 pcreg = reg;
619 }
620 else if ((word & 0xffe0ffff) == 0x6be08001) /* ret zero,reg,1 */
621 pcreg = (word >> 16) & 0x1f;
622 else if (word == 0x47de040f) /* bis sp,sp fp */
623 has_frame_reg = 1;
624 }
625 if (pcreg == -1)
626 {
627 /* If we haven't found a valid return address register yet,
628 keep searching in the procedure prologue. */
629 while (cur_pc < (limit_pc + 80) && cur_pc < (start_pc + 80))
630 {
631 char buf[4];
632 unsigned long word;
633
634 if (read_memory_nobpt (cur_pc, buf, 4))
635 break;
636 cur_pc += 4;
637 word = extract_unsigned_integer (buf, 4);
638
639 if ((word & 0xfc1f0000) == 0xb41e0000 /* stq reg,n($sp) */
640 && (word & 0xffff0000) != 0xb7fe0000) /* reg != $zero */
641 {
642 int reg = (word & 0x03e00000) >> 21;
643 if (reg == T7_REGNUM || reg == T9_REGNUM || reg == RA_REGNUM)
644 {
645 pcreg = reg;
646 break;
647 }
648 }
649 else if ((word & 0xffe0ffff) == 0x6be08001) /* ret zero,reg,1 */
650 {
651 pcreg = (word >> 16) & 0x1f;
652 break;
653 }
654 }
655 }
656
657 if (has_frame_reg)
658 PROC_FRAME_REG (&temp_proc_desc) = GCC_FP_REGNUM;
659 else
660 PROC_FRAME_REG (&temp_proc_desc) = SP_REGNUM;
661 PROC_FRAME_OFFSET (&temp_proc_desc) = frame_size;
662 PROC_REG_MASK (&temp_proc_desc) = reg_mask;
663 PROC_PC_REG (&temp_proc_desc) = (pcreg == -1) ? RA_REGNUM : pcreg;
664 PROC_LOCALOFF (&temp_proc_desc) = 0; /* XXX - bogus */
665 return &temp_proc_desc;
666 }
667
668 /* This returns the PC of the first inst after the prologue. If we can't
669 find the prologue, then return 0. */
670
671 static CORE_ADDR
672 after_prologue (CORE_ADDR pc, alpha_extra_func_info_t proc_desc)
673 {
674 struct symtab_and_line sal;
675 CORE_ADDR func_addr, func_end;
676
677 if (!proc_desc)
678 proc_desc = find_proc_desc (pc, NULL);
679
680 if (proc_desc)
681 {
682 if (PROC_DESC_IS_DYN_SIGTRAMP (proc_desc))
683 return PROC_LOW_ADDR (proc_desc); /* "prologue" is in kernel */
684
685 /* If function is frameless, then we need to do it the hard way. I
686 strongly suspect that frameless always means prologueless... */
687 if (PROC_FRAME_REG (proc_desc) == SP_REGNUM
688 && PROC_FRAME_OFFSET (proc_desc) == 0)
689 return 0;
690 }
691
692 if (!find_pc_partial_function (pc, NULL, &func_addr, &func_end))
693 return 0; /* Unknown */
694
695 sal = find_pc_line (func_addr, 0);
696
697 if (sal.end < func_end)
698 return sal.end;
699
700 /* The line after the prologue is after the end of the function. In this
701 case, tell the caller to find the prologue the hard way. */
702
703 return 0;
704 }
705
706 /* Return non-zero if we *might* be in a function prologue. Return zero if we
707 are definitively *not* in a function prologue. */
708
709 static int
710 alpha_in_prologue (CORE_ADDR pc, alpha_extra_func_info_t proc_desc)
711 {
712 CORE_ADDR after_prologue_pc;
713
714 after_prologue_pc = after_prologue (pc, proc_desc);
715
716 if (after_prologue_pc == 0
717 || pc < after_prologue_pc)
718 return 1;
719 else
720 return 0;
721 }
722
723 static alpha_extra_func_info_t
724 find_proc_desc (CORE_ADDR pc, struct frame_info *next_frame)
725 {
726 alpha_extra_func_info_t proc_desc;
727 struct block *b;
728 struct symbol *sym;
729 CORE_ADDR startaddr;
730
731 /* Try to get the proc_desc from the linked call dummy proc_descs
732 if the pc is in the call dummy.
733 This is hairy. In the case of nested dummy calls we have to find the
734 right proc_desc, but we might not yet know the frame for the dummy
735 as it will be contained in the proc_desc we are searching for.
736 So we have to find the proc_desc whose frame is closest to the current
737 stack pointer. */
738
739 if (PC_IN_CALL_DUMMY (pc, 0, 0))
740 {
741 struct linked_proc_info *link;
742 CORE_ADDR sp = read_next_frame_reg (next_frame, SP_REGNUM);
743 alpha_extra_func_info_t found_proc_desc = NULL;
744 long min_distance = LONG_MAX;
745
746 for (link = linked_proc_desc_table; link; link = link->next)
747 {
748 long distance = (CORE_ADDR) PROC_DUMMY_FRAME (&link->info) - sp;
749 if (distance > 0 && distance < min_distance)
750 {
751 min_distance = distance;
752 found_proc_desc = &link->info;
753 }
754 }
755 if (found_proc_desc != NULL)
756 return found_proc_desc;
757 }
758
759 b = block_for_pc (pc);
760
761 find_pc_partial_function (pc, NULL, &startaddr, NULL);
762 if (b == NULL)
763 sym = NULL;
764 else
765 {
766 if (startaddr > BLOCK_START (b))
767 /* This is the "pathological" case referred to in a comment in
768 print_frame_info. It might be better to move this check into
769 symbol reading. */
770 sym = NULL;
771 else
772 sym = lookup_symbol (MIPS_EFI_SYMBOL_NAME, b, LABEL_NAMESPACE,
773 0, NULL);
774 }
775
776 /* If we never found a PDR for this function in symbol reading, then
777 examine prologues to find the information. */
778 if (sym && ((mips_extra_func_info_t) SYMBOL_VALUE (sym))->pdr.framereg == -1)
779 sym = NULL;
780
781 if (sym)
782 {
783 /* IF this is the topmost frame AND
784 * (this proc does not have debugging information OR
785 * the PC is in the procedure prologue)
786 * THEN create a "heuristic" proc_desc (by analyzing
787 * the actual code) to replace the "official" proc_desc.
788 */
789 proc_desc = (alpha_extra_func_info_t) SYMBOL_VALUE (sym);
790 if (next_frame == NULL)
791 {
792 if (PROC_DESC_IS_DUMMY (proc_desc) || alpha_in_prologue (pc, proc_desc))
793 {
794 alpha_extra_func_info_t found_heuristic =
795 heuristic_proc_desc (PROC_LOW_ADDR (proc_desc),
796 pc, next_frame);
797 if (found_heuristic)
798 {
799 PROC_LOCALOFF (found_heuristic) =
800 PROC_LOCALOFF (proc_desc);
801 PROC_PC_REG (found_heuristic) = PROC_PC_REG (proc_desc);
802 proc_desc = found_heuristic;
803 }
804 }
805 }
806 }
807 else
808 {
809 long offset;
810
811 /* Is linked_proc_desc_table really necessary? It only seems to be used
812 by procedure call dummys. However, the procedures being called ought
813 to have their own proc_descs, and even if they don't,
814 heuristic_proc_desc knows how to create them! */
815
816 register struct linked_proc_info *link;
817 for (link = linked_proc_desc_table; link; link = link->next)
818 if (PROC_LOW_ADDR (&link->info) <= pc
819 && PROC_HIGH_ADDR (&link->info) > pc)
820 return &link->info;
821
822 /* If PC is inside a dynamically generated sigtramp handler,
823 create and push a procedure descriptor for that code: */
824 offset = DYNAMIC_SIGTRAMP_OFFSET (pc);
825 if (offset >= 0)
826 return push_sigtramp_desc (pc - offset);
827
828 /* If heuristic_fence_post is non-zero, determine the procedure
829 start address by examining the instructions.
830 This allows us to find the start address of static functions which
831 have no symbolic information, as startaddr would have been set to
832 the preceding global function start address by the
833 find_pc_partial_function call above. */
834 if (startaddr == 0 || heuristic_fence_post != 0)
835 startaddr = heuristic_proc_start (pc);
836
837 proc_desc =
838 heuristic_proc_desc (startaddr, pc, next_frame);
839 }
840 return proc_desc;
841 }
842
843 alpha_extra_func_info_t cached_proc_desc;
844
845 CORE_ADDR
846 alpha_frame_chain (struct frame_info *frame)
847 {
848 alpha_extra_func_info_t proc_desc;
849 CORE_ADDR saved_pc = FRAME_SAVED_PC (frame);
850
851 if (saved_pc == 0 || inside_entry_file (saved_pc))
852 return 0;
853
854 proc_desc = find_proc_desc (saved_pc, frame);
855 if (!proc_desc)
856 return 0;
857
858 cached_proc_desc = proc_desc;
859
860 /* Fetch the frame pointer for a dummy frame from the procedure
861 descriptor. */
862 if (PROC_DESC_IS_DUMMY (proc_desc))
863 return (CORE_ADDR) PROC_DUMMY_FRAME (proc_desc);
864
865 /* If no frame pointer and frame size is zero, we must be at end
866 of stack (or otherwise hosed). If we don't check frame size,
867 we loop forever if we see a zero size frame. */
868 if (PROC_FRAME_REG (proc_desc) == SP_REGNUM
869 && PROC_FRAME_OFFSET (proc_desc) == 0
870 /* The previous frame from a sigtramp frame might be frameless
871 and have frame size zero. */
872 && !frame->signal_handler_caller)
873 return FRAME_PAST_SIGTRAMP_FRAME (frame, saved_pc);
874 else
875 return read_next_frame_reg (frame, PROC_FRAME_REG (proc_desc))
876 + PROC_FRAME_OFFSET (proc_desc);
877 }
878
879 void
880 alpha_print_extra_frame_info (struct frame_info *fi)
881 {
882 if (fi
883 && fi->extra_info
884 && fi->extra_info->proc_desc
885 && fi->extra_info->proc_desc->pdr.framereg < NUM_REGS)
886 printf_filtered (" frame pointer is at %s+%s\n",
887 REGISTER_NAME (fi->extra_info->proc_desc->pdr.framereg),
888 paddr_d (fi->extra_info->proc_desc->pdr.frameoffset));
889 }
890
891 void
892 alpha_init_extra_frame_info (int fromleaf, struct frame_info *frame)
893 {
894 /* Use proc_desc calculated in frame_chain */
895 alpha_extra_func_info_t proc_desc =
896 frame->next ? cached_proc_desc : find_proc_desc (frame->pc, frame->next);
897
898 frame->extra_info = (struct frame_extra_info *)
899 frame_obstack_alloc (sizeof (struct frame_extra_info));
900
901 frame->saved_regs = NULL;
902 frame->extra_info->localoff = 0;
903 frame->extra_info->pc_reg = RA_REGNUM;
904 frame->extra_info->proc_desc = proc_desc == &temp_proc_desc ? 0 : proc_desc;
905 if (proc_desc)
906 {
907 /* Get the locals offset and the saved pc register from the
908 procedure descriptor, they are valid even if we are in the
909 middle of the prologue. */
910 frame->extra_info->localoff = PROC_LOCALOFF (proc_desc);
911 frame->extra_info->pc_reg = PROC_PC_REG (proc_desc);
912
913 /* Fixup frame-pointer - only needed for top frame */
914
915 /* Fetch the frame pointer for a dummy frame from the procedure
916 descriptor. */
917 if (PROC_DESC_IS_DUMMY (proc_desc))
918 frame->frame = (CORE_ADDR) PROC_DUMMY_FRAME (proc_desc);
919
920 /* This may not be quite right, if proc has a real frame register.
921 Get the value of the frame relative sp, procedure might have been
922 interrupted by a signal at it's very start. */
923 else if (frame->pc == PROC_LOW_ADDR (proc_desc)
924 && !PROC_DESC_IS_DYN_SIGTRAMP (proc_desc))
925 frame->frame = read_next_frame_reg (frame->next, SP_REGNUM);
926 else
927 frame->frame = read_next_frame_reg (frame->next, PROC_FRAME_REG (proc_desc))
928 + PROC_FRAME_OFFSET (proc_desc);
929
930 if (proc_desc == &temp_proc_desc)
931 {
932 char *name;
933
934 /* Do not set the saved registers for a sigtramp frame,
935 alpha_find_saved_registers will do that for us.
936 We can't use frame->signal_handler_caller, it is not yet set. */
937 find_pc_partial_function (frame->pc, &name,
938 (CORE_ADDR *) NULL, (CORE_ADDR *) NULL);
939 if (!IN_SIGTRAMP (frame->pc, name))
940 {
941 frame->saved_regs = (CORE_ADDR *)
942 frame_obstack_alloc (SIZEOF_FRAME_SAVED_REGS);
943 memcpy (frame->saved_regs, temp_saved_regs,
944 SIZEOF_FRAME_SAVED_REGS);
945 frame->saved_regs[PC_REGNUM]
946 = frame->saved_regs[RA_REGNUM];
947 }
948 }
949 }
950 }
951
952 CORE_ADDR
953 alpha_frame_locals_address (struct frame_info *fi)
954 {
955 return (fi->frame - fi->extra_info->localoff);
956 }
957
958 CORE_ADDR
959 alpha_frame_args_address (struct frame_info *fi)
960 {
961 return (fi->frame - (ALPHA_NUM_ARG_REGS * 8));
962 }
963
964 /* ALPHA stack frames are almost impenetrable. When execution stops,
965 we basically have to look at symbol information for the function
966 that we stopped in, which tells us *which* register (if any) is
967 the base of the frame pointer, and what offset from that register
968 the frame itself is at.
969
970 This presents a problem when trying to examine a stack in memory
971 (that isn't executing at the moment), using the "frame" command. We
972 don't have a PC, nor do we have any registers except SP.
973
974 This routine takes two arguments, SP and PC, and tries to make the
975 cached frames look as if these two arguments defined a frame on the
976 cache. This allows the rest of info frame to extract the important
977 arguments without difficulty. */
978
979 struct frame_info *
980 setup_arbitrary_frame (int argc, CORE_ADDR *argv)
981 {
982 if (argc != 2)
983 error ("ALPHA frame specifications require two arguments: sp and pc");
984
985 return create_new_frame (argv[0], argv[1]);
986 }
987
988 /* The alpha passes the first six arguments in the registers, the rest on
989 the stack. The register arguments are eventually transferred to the
990 argument transfer area immediately below the stack by the called function
991 anyway. So we `push' at least six arguments on the stack, `reload' the
992 argument registers and then adjust the stack pointer to point past the
993 sixth argument. This algorithm simplifies the passing of a large struct
994 which extends from the registers to the stack.
995 If the called function is returning a structure, the address of the
996 structure to be returned is passed as a hidden first argument. */
997
998 CORE_ADDR
999 alpha_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
1000 int struct_return, CORE_ADDR struct_addr)
1001 {
1002 int i;
1003 int accumulate_size = struct_return ? 8 : 0;
1004 int arg_regs_size = ALPHA_NUM_ARG_REGS * 8;
1005 struct alpha_arg
1006 {
1007 char *contents;
1008 int len;
1009 int offset;
1010 };
1011 struct alpha_arg *alpha_args =
1012 (struct alpha_arg *) alloca (nargs * sizeof (struct alpha_arg));
1013 register struct alpha_arg *m_arg;
1014 char raw_buffer[sizeof (CORE_ADDR)];
1015 int required_arg_regs;
1016
1017 for (i = 0, m_arg = alpha_args; i < nargs; i++, m_arg++)
1018 {
1019 struct value *arg = args[i];
1020 struct type *arg_type = check_typedef (VALUE_TYPE (arg));
1021 /* Cast argument to long if necessary as the compiler does it too. */
1022 switch (TYPE_CODE (arg_type))
1023 {
1024 case TYPE_CODE_INT:
1025 case TYPE_CODE_BOOL:
1026 case TYPE_CODE_CHAR:
1027 case TYPE_CODE_RANGE:
1028 case TYPE_CODE_ENUM:
1029 if (TYPE_LENGTH (arg_type) < TYPE_LENGTH (builtin_type_long))
1030 {
1031 arg_type = builtin_type_long;
1032 arg = value_cast (arg_type, arg);
1033 }
1034 break;
1035 default:
1036 break;
1037 }
1038 m_arg->len = TYPE_LENGTH (arg_type);
1039 m_arg->offset = accumulate_size;
1040 accumulate_size = (accumulate_size + m_arg->len + 7) & ~7;
1041 m_arg->contents = VALUE_CONTENTS (arg);
1042 }
1043
1044 /* Determine required argument register loads, loading an argument register
1045 is expensive as it uses three ptrace calls. */
1046 required_arg_regs = accumulate_size / 8;
1047 if (required_arg_regs > ALPHA_NUM_ARG_REGS)
1048 required_arg_regs = ALPHA_NUM_ARG_REGS;
1049
1050 /* Make room for the arguments on the stack. */
1051 if (accumulate_size < arg_regs_size)
1052 accumulate_size = arg_regs_size;
1053 sp -= accumulate_size;
1054
1055 /* Keep sp aligned to a multiple of 16 as the compiler does it too. */
1056 sp &= ~15;
1057
1058 /* `Push' arguments on the stack. */
1059 for (i = nargs; m_arg--, --i >= 0;)
1060 write_memory (sp + m_arg->offset, m_arg->contents, m_arg->len);
1061 if (struct_return)
1062 {
1063 store_address (raw_buffer, sizeof (CORE_ADDR), struct_addr);
1064 write_memory (sp, raw_buffer, sizeof (CORE_ADDR));
1065 }
1066
1067 /* Load the argument registers. */
1068 for (i = 0; i < required_arg_regs; i++)
1069 {
1070 LONGEST val;
1071
1072 val = read_memory_integer (sp + i * 8, 8);
1073 write_register (A0_REGNUM + i, val);
1074 write_register (FPA0_REGNUM + i, val);
1075 }
1076
1077 return sp + arg_regs_size;
1078 }
1079
1080 void
1081 alpha_push_dummy_frame (void)
1082 {
1083 int ireg;
1084 struct linked_proc_info *link;
1085 alpha_extra_func_info_t proc_desc;
1086 CORE_ADDR sp = read_register (SP_REGNUM);
1087 CORE_ADDR save_address;
1088 char raw_buffer[MAX_REGISTER_RAW_SIZE];
1089 unsigned long mask;
1090
1091 link = (struct linked_proc_info *) xmalloc (sizeof (struct linked_proc_info));
1092 link->next = linked_proc_desc_table;
1093 linked_proc_desc_table = link;
1094
1095 proc_desc = &link->info;
1096
1097 /*
1098 * The registers we must save are all those not preserved across
1099 * procedure calls.
1100 * In addition, we must save the PC and RA.
1101 *
1102 * Dummy frame layout:
1103 * (high memory)
1104 * Saved PC
1105 * Saved F30
1106 * ...
1107 * Saved F0
1108 * Saved R29
1109 * ...
1110 * Saved R0
1111 * Saved R26 (RA)
1112 * Parameter build area
1113 * (low memory)
1114 */
1115
1116 /* MASK(i,j) == (1<<i) + (1<<(i+1)) + ... + (1<<j)). Assume i<=j<31. */
1117 #define MASK(i,j) ((((LONGEST)1 << ((j)+1)) - 1) ^ (((LONGEST)1 << (i)) - 1))
1118 #define GEN_REG_SAVE_MASK (MASK(0,8) | MASK(16,29))
1119 #define GEN_REG_SAVE_COUNT 24
1120 #define FLOAT_REG_SAVE_MASK (MASK(0,1) | MASK(10,30))
1121 #define FLOAT_REG_SAVE_COUNT 23
1122 /* The special register is the PC as we have no bit for it in the save masks.
1123 alpha_frame_saved_pc knows where the pc is saved in a dummy frame. */
1124 #define SPECIAL_REG_SAVE_COUNT 1
1125
1126 PROC_REG_MASK (proc_desc) = GEN_REG_SAVE_MASK;
1127 PROC_FREG_MASK (proc_desc) = FLOAT_REG_SAVE_MASK;
1128 /* PROC_REG_OFFSET is the offset from the dummy frame to the saved RA,
1129 but keep SP aligned to a multiple of 16. */
1130 PROC_REG_OFFSET (proc_desc) =
1131 -((8 * (SPECIAL_REG_SAVE_COUNT
1132 + GEN_REG_SAVE_COUNT
1133 + FLOAT_REG_SAVE_COUNT)
1134 + 15) & ~15);
1135 PROC_FREG_OFFSET (proc_desc) =
1136 PROC_REG_OFFSET (proc_desc) + 8 * GEN_REG_SAVE_COUNT;
1137
1138 /* Save general registers.
1139 The return address register is the first saved register, all other
1140 registers follow in ascending order.
1141 The PC is saved immediately below the SP. */
1142 save_address = sp + PROC_REG_OFFSET (proc_desc);
1143 store_address (raw_buffer, 8, read_register (RA_REGNUM));
1144 write_memory (save_address, raw_buffer, 8);
1145 save_address += 8;
1146 mask = PROC_REG_MASK (proc_desc) & 0xffffffffL;
1147 for (ireg = 0; mask; ireg++, mask >>= 1)
1148 if (mask & 1)
1149 {
1150 if (ireg == RA_REGNUM)
1151 continue;
1152 store_address (raw_buffer, 8, read_register (ireg));
1153 write_memory (save_address, raw_buffer, 8);
1154 save_address += 8;
1155 }
1156
1157 store_address (raw_buffer, 8, read_register (PC_REGNUM));
1158 write_memory (sp - 8, raw_buffer, 8);
1159
1160 /* Save floating point registers. */
1161 save_address = sp + PROC_FREG_OFFSET (proc_desc);
1162 mask = PROC_FREG_MASK (proc_desc) & 0xffffffffL;
1163 for (ireg = 0; mask; ireg++, mask >>= 1)
1164 if (mask & 1)
1165 {
1166 store_address (raw_buffer, 8, read_register (ireg + FP0_REGNUM));
1167 write_memory (save_address, raw_buffer, 8);
1168 save_address += 8;
1169 }
1170
1171 /* Set and save the frame address for the dummy.
1172 This is tricky. The only registers that are suitable for a frame save
1173 are those that are preserved across procedure calls (s0-s6). But if
1174 a read system call is interrupted and then a dummy call is made
1175 (see testsuite/gdb.t17/interrupt.exp) the dummy call hangs till the read
1176 is satisfied. Then it returns with the s0-s6 registers set to the values
1177 on entry to the read system call and our dummy frame pointer would be
1178 destroyed. So we save the dummy frame in the proc_desc and handle the
1179 retrieval of the frame pointer of a dummy specifically. The frame register
1180 is set to the virtual frame (pseudo) register, it's value will always
1181 be read as zero and will help us to catch any errors in the dummy frame
1182 retrieval code. */
1183 PROC_DUMMY_FRAME (proc_desc) = sp;
1184 PROC_FRAME_REG (proc_desc) = FP_REGNUM;
1185 PROC_FRAME_OFFSET (proc_desc) = 0;
1186 sp += PROC_REG_OFFSET (proc_desc);
1187 write_register (SP_REGNUM, sp);
1188
1189 PROC_LOW_ADDR (proc_desc) = CALL_DUMMY_ADDRESS ();
1190 PROC_HIGH_ADDR (proc_desc) = PROC_LOW_ADDR (proc_desc) + 4;
1191
1192 SET_PROC_DESC_IS_DUMMY (proc_desc);
1193 PROC_PC_REG (proc_desc) = RA_REGNUM;
1194 }
1195
1196 void
1197 alpha_pop_frame (void)
1198 {
1199 register int regnum;
1200 struct frame_info *frame = get_current_frame ();
1201 CORE_ADDR new_sp = frame->frame;
1202
1203 alpha_extra_func_info_t proc_desc = frame->extra_info->proc_desc;
1204
1205 /* we need proc_desc to know how to restore the registers;
1206 if it is NULL, construct (a temporary) one */
1207 if (proc_desc == NULL)
1208 proc_desc = find_proc_desc (frame->pc, frame->next);
1209
1210 /* Question: should we copy this proc_desc and save it in
1211 frame->proc_desc? If we do, who will free it?
1212 For now, we don't save a copy... */
1213
1214 write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
1215 if (frame->saved_regs == NULL)
1216 alpha_find_saved_regs (frame);
1217 if (proc_desc)
1218 {
1219 for (regnum = 32; --regnum >= 0;)
1220 if (PROC_REG_MASK (proc_desc) & (1 << regnum))
1221 write_register (regnum,
1222 read_memory_integer (frame->saved_regs[regnum],
1223 8));
1224 for (regnum = 32; --regnum >= 0;)
1225 if (PROC_FREG_MASK (proc_desc) & (1 << regnum))
1226 write_register (regnum + FP0_REGNUM,
1227 read_memory_integer (frame->saved_regs[regnum + FP0_REGNUM], 8));
1228 }
1229 write_register (SP_REGNUM, new_sp);
1230 flush_cached_frames ();
1231
1232 if (proc_desc && (PROC_DESC_IS_DUMMY (proc_desc)
1233 || PROC_DESC_IS_DYN_SIGTRAMP (proc_desc)))
1234 {
1235 struct linked_proc_info *pi_ptr, *prev_ptr;
1236
1237 for (pi_ptr = linked_proc_desc_table, prev_ptr = NULL;
1238 pi_ptr != NULL;
1239 prev_ptr = pi_ptr, pi_ptr = pi_ptr->next)
1240 {
1241 if (&pi_ptr->info == proc_desc)
1242 break;
1243 }
1244
1245 if (pi_ptr == NULL)
1246 error ("Can't locate dummy extra frame info\n");
1247
1248 if (prev_ptr != NULL)
1249 prev_ptr->next = pi_ptr->next;
1250 else
1251 linked_proc_desc_table = pi_ptr->next;
1252
1253 xfree (pi_ptr);
1254 }
1255 }
1256 \f
1257 /* To skip prologues, I use this predicate. Returns either PC itself
1258 if the code at PC does not look like a function prologue; otherwise
1259 returns an address that (if we're lucky) follows the prologue. If
1260 LENIENT, then we must skip everything which is involved in setting
1261 up the frame (it's OK to skip more, just so long as we don't skip
1262 anything which might clobber the registers which are being saved.
1263 Currently we must not skip more on the alpha, but we might need the
1264 lenient stuff some day. */
1265
1266 CORE_ADDR
1267 alpha_skip_prologue (CORE_ADDR pc, int lenient)
1268 {
1269 unsigned long inst;
1270 int offset;
1271 CORE_ADDR post_prologue_pc;
1272 char buf[4];
1273
1274 #ifdef GDB_TARGET_HAS_SHARED_LIBS
1275 /* Silently return the unaltered pc upon memory errors.
1276 This could happen on OSF/1 if decode_line_1 tries to skip the
1277 prologue for quickstarted shared library functions when the
1278 shared library is not yet mapped in.
1279 Reading target memory is slow over serial lines, so we perform
1280 this check only if the target has shared libraries. */
1281 if (target_read_memory (pc, buf, 4))
1282 return pc;
1283 #endif
1284
1285 /* See if we can determine the end of the prologue via the symbol table.
1286 If so, then return either PC, or the PC after the prologue, whichever
1287 is greater. */
1288
1289 post_prologue_pc = after_prologue (pc, NULL);
1290
1291 if (post_prologue_pc != 0)
1292 return max (pc, post_prologue_pc);
1293
1294 /* Can't determine prologue from the symbol table, need to examine
1295 instructions. */
1296
1297 /* Skip the typical prologue instructions. These are the stack adjustment
1298 instruction and the instructions that save registers on the stack
1299 or in the gcc frame. */
1300 for (offset = 0; offset < 100; offset += 4)
1301 {
1302 int status;
1303
1304 status = read_memory_nobpt (pc + offset, buf, 4);
1305 if (status)
1306 memory_error (status, pc + offset);
1307 inst = extract_unsigned_integer (buf, 4);
1308
1309 /* The alpha has no delay slots. But let's keep the lenient stuff,
1310 we might need it for something else in the future. */
1311 if (lenient && 0)
1312 continue;
1313
1314 if ((inst & 0xffff0000) == 0x27bb0000) /* ldah $gp,n($t12) */
1315 continue;
1316 if ((inst & 0xffff0000) == 0x23bd0000) /* lda $gp,n($gp) */
1317 continue;
1318 if ((inst & 0xffff0000) == 0x23de0000) /* lda $sp,n($sp) */
1319 continue;
1320 if ((inst & 0xffe01fff) == 0x43c0153e) /* subq $sp,n,$sp */
1321 continue;
1322
1323 if ((inst & 0xfc1f0000) == 0xb41e0000
1324 && (inst & 0xffff0000) != 0xb7fe0000)
1325 continue; /* stq reg,n($sp) */
1326 /* reg != $zero */
1327 if ((inst & 0xfc1f0000) == 0x9c1e0000
1328 && (inst & 0xffff0000) != 0x9ffe0000)
1329 continue; /* stt reg,n($sp) */
1330 /* reg != $zero */
1331 if (inst == 0x47de040f) /* bis sp,sp,fp */
1332 continue;
1333
1334 break;
1335 }
1336 return pc + offset;
1337 }
1338
1339 #if 0
1340 /* Is address PC in the prologue (loosely defined) for function at
1341 STARTADDR? */
1342
1343 static int
1344 alpha_in_lenient_prologue (CORE_ADDR startaddr, CORE_ADDR pc)
1345 {
1346 CORE_ADDR end_prologue = alpha_skip_prologue (startaddr, 1);
1347 return pc >= startaddr && pc < end_prologue;
1348 }
1349 #endif
1350
1351 /* The alpha needs a conversion between register and memory format if
1352 the register is a floating point register and
1353 memory format is float, as the register format must be double
1354 or
1355 memory format is an integer with 4 bytes or less, as the representation
1356 of integers in floating point registers is different. */
1357 void
1358 alpha_register_convert_to_virtual (int regnum, struct type *valtype,
1359 char *raw_buffer, char *virtual_buffer)
1360 {
1361 if (TYPE_LENGTH (valtype) >= REGISTER_RAW_SIZE (regnum))
1362 {
1363 memcpy (virtual_buffer, raw_buffer, REGISTER_VIRTUAL_SIZE (regnum));
1364 return;
1365 }
1366
1367 if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1368 {
1369 double d = extract_floating (raw_buffer, REGISTER_RAW_SIZE (regnum));
1370 store_floating (virtual_buffer, TYPE_LENGTH (valtype), d);
1371 }
1372 else if (TYPE_CODE (valtype) == TYPE_CODE_INT && TYPE_LENGTH (valtype) <= 4)
1373 {
1374 ULONGEST l;
1375 l = extract_unsigned_integer (raw_buffer, REGISTER_RAW_SIZE (regnum));
1376 l = ((l >> 32) & 0xc0000000) | ((l >> 29) & 0x3fffffff);
1377 store_unsigned_integer (virtual_buffer, TYPE_LENGTH (valtype), l);
1378 }
1379 else
1380 error ("Cannot retrieve value from floating point register");
1381 }
1382
1383 void
1384 alpha_register_convert_to_raw (struct type *valtype, int regnum,
1385 char *virtual_buffer, char *raw_buffer)
1386 {
1387 if (TYPE_LENGTH (valtype) >= REGISTER_RAW_SIZE (regnum))
1388 {
1389 memcpy (raw_buffer, virtual_buffer, REGISTER_RAW_SIZE (regnum));
1390 return;
1391 }
1392
1393 if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1394 {
1395 double d = extract_floating (virtual_buffer, TYPE_LENGTH (valtype));
1396 store_floating (raw_buffer, REGISTER_RAW_SIZE (regnum), d);
1397 }
1398 else if (TYPE_CODE (valtype) == TYPE_CODE_INT && TYPE_LENGTH (valtype) <= 4)
1399 {
1400 ULONGEST l;
1401 if (TYPE_UNSIGNED (valtype))
1402 l = extract_unsigned_integer (virtual_buffer, TYPE_LENGTH (valtype));
1403 else
1404 l = extract_signed_integer (virtual_buffer, TYPE_LENGTH (valtype));
1405 l = ((l & 0xc0000000) << 32) | ((l & 0x3fffffff) << 29);
1406 store_unsigned_integer (raw_buffer, REGISTER_RAW_SIZE (regnum), l);
1407 }
1408 else
1409 error ("Cannot store value in floating point register");
1410 }
1411
1412 /* Given a return value in `regbuf' with a type `valtype',
1413 extract and copy its value into `valbuf'. */
1414
1415 void
1416 alpha_extract_return_value (struct type *valtype,
1417 char regbuf[REGISTER_BYTES], char *valbuf)
1418 {
1419 if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1420 alpha_register_convert_to_virtual (FP0_REGNUM, valtype,
1421 regbuf + REGISTER_BYTE (FP0_REGNUM),
1422 valbuf);
1423 else
1424 memcpy (valbuf, regbuf + REGISTER_BYTE (V0_REGNUM), TYPE_LENGTH (valtype));
1425 }
1426
1427 /* Given a return value in `regbuf' with a type `valtype',
1428 write its value into the appropriate register. */
1429
1430 void
1431 alpha_store_return_value (struct type *valtype, char *valbuf)
1432 {
1433 char raw_buffer[MAX_REGISTER_RAW_SIZE];
1434 int regnum = V0_REGNUM;
1435 int length = TYPE_LENGTH (valtype);
1436
1437 if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1438 {
1439 regnum = FP0_REGNUM;
1440 length = REGISTER_RAW_SIZE (regnum);
1441 alpha_register_convert_to_raw (valtype, regnum, valbuf, raw_buffer);
1442 }
1443 else
1444 memcpy (raw_buffer, valbuf, length);
1445
1446 write_register_bytes (REGISTER_BYTE (regnum), raw_buffer, length);
1447 }
1448
1449 /* Just like reinit_frame_cache, but with the right arguments to be
1450 callable as an sfunc. */
1451
1452 static void
1453 reinit_frame_cache_sfunc (char *args, int from_tty, struct cmd_list_element *c)
1454 {
1455 reinit_frame_cache ();
1456 }
1457
1458 /* This is the definition of CALL_DUMMY_ADDRESS. It's a heuristic that is used
1459 to find a convenient place in the text segment to stick a breakpoint to
1460 detect the completion of a target function call (ala call_function_by_hand).
1461 */
1462
1463 CORE_ADDR
1464 alpha_call_dummy_address (void)
1465 {
1466 CORE_ADDR entry;
1467 struct minimal_symbol *sym;
1468
1469 entry = entry_point_address ();
1470
1471 if (entry != 0)
1472 return entry;
1473
1474 sym = lookup_minimal_symbol ("_Prelude", NULL, symfile_objfile);
1475
1476 if (!sym || MSYMBOL_TYPE (sym) != mst_text)
1477 return 0;
1478 else
1479 return SYMBOL_VALUE_ADDRESS (sym) + 4;
1480 }
1481
1482 int
1483 alpha_use_struct_convention (int gcc_p, struct type *type)
1484 {
1485 /* Structures are returned by ref in extra arg0. */
1486 return 1;
1487 }
1488
1489 /* alpha_software_single_step() is called just before we want to resume
1490 the inferior, if we want to single-step it but there is no hardware
1491 or kernel single-step support (NetBSD on Alpha, for example). We find
1492 the target of the coming instruction and breakpoint it.
1493
1494 single_step is also called just after the inferior stops. If we had
1495 set up a simulated single-step, we undo our damage. */
1496
1497 static CORE_ADDR
1498 alpha_next_pc (CORE_ADDR pc)
1499 {
1500 unsigned int insn;
1501 unsigned int op;
1502 int offset;
1503 LONGEST rav;
1504
1505 insn = read_memory_unsigned_integer (pc, sizeof (insn));
1506
1507 /* Opcode is top 6 bits. */
1508 op = (insn >> 26) & 0x3f;
1509
1510 if (op == 0x1a)
1511 {
1512 /* Jump format: target PC is:
1513 RB & ~3 */
1514 return (read_register ((insn >> 16) & 0x1f) & ~3);
1515 }
1516
1517 if ((op & 0x30) == 0x30)
1518 {
1519 /* Branch format: target PC is:
1520 (new PC) + (4 * sext(displacement)) */
1521 if (op == 0x30 || /* BR */
1522 op == 0x34) /* BSR */
1523 {
1524 branch_taken:
1525 offset = (insn & 0x001fffff);
1526 if (offset & 0x00100000)
1527 offset |= 0xffe00000;
1528 offset *= 4;
1529 return (pc + 4 + offset);
1530 }
1531
1532 /* Need to determine if branch is taken; read RA. */
1533 rav = (LONGEST) read_register ((insn >> 21) & 0x1f);
1534 switch (op)
1535 {
1536 case 0x38: /* BLBC */
1537 if ((rav & 1) == 0)
1538 goto branch_taken;
1539 break;
1540 case 0x3c: /* BLBS */
1541 if (rav & 1)
1542 goto branch_taken;
1543 break;
1544 case 0x39: /* BEQ */
1545 if (rav == 0)
1546 goto branch_taken;
1547 break;
1548 case 0x3d: /* BNE */
1549 if (rav != 0)
1550 goto branch_taken;
1551 break;
1552 case 0x3a: /* BLT */
1553 if (rav < 0)
1554 goto branch_taken;
1555 break;
1556 case 0x3b: /* BLE */
1557 if (rav <= 0)
1558 goto branch_taken;
1559 break;
1560 case 0x3f: /* BGT */
1561 if (rav > 0)
1562 goto branch_taken;
1563 break;
1564 case 0x3e: /* BGE */
1565 if (rav >= 0)
1566 goto branch_taken;
1567 break;
1568 }
1569 }
1570
1571 /* Not a branch or branch not taken; target PC is:
1572 pc + 4 */
1573 return (pc + 4);
1574 }
1575
1576 void
1577 alpha_software_single_step (enum target_signal sig, int insert_breakpoints_p)
1578 {
1579 static CORE_ADDR next_pc;
1580 typedef char binsn_quantum[BREAKPOINT_MAX];
1581 static binsn_quantum break_mem;
1582 CORE_ADDR pc;
1583
1584 if (insert_breakpoints_p)
1585 {
1586 pc = read_pc ();
1587 next_pc = alpha_next_pc (pc);
1588
1589 target_insert_breakpoint (next_pc, break_mem);
1590 }
1591 else
1592 {
1593 target_remove_breakpoint (next_pc, break_mem);
1594 write_pc (next_pc);
1595 }
1596 }
1597
1598 void
1599 _initialize_alpha_tdep (void)
1600 {
1601 struct cmd_list_element *c;
1602
1603 tm_print_insn = print_insn_alpha;
1604
1605 /* Let the user set the fence post for heuristic_proc_start. */
1606
1607 /* We really would like to have both "0" and "unlimited" work, but
1608 command.c doesn't deal with that. So make it a var_zinteger
1609 because the user can always use "999999" or some such for unlimited. */
1610 c = add_set_cmd ("heuristic-fence-post", class_support, var_zinteger,
1611 (char *) &heuristic_fence_post,
1612 "\
1613 Set the distance searched for the start of a function.\n\
1614 If you are debugging a stripped executable, GDB needs to search through the\n\
1615 program for the start of a function. This command sets the distance of the\n\
1616 search. The only need to set it is when debugging a stripped executable.",
1617 &setlist);
1618 /* We need to throw away the frame cache when we set this, since it
1619 might change our ability to get backtraces. */
1620 c->function.sfunc = reinit_frame_cache_sfunc;
1621 add_show_from_set (c, &showlist);
1622 }
This page took 0.062576 seconds and 4 git commands to generate.