* alphabsd-tdep.c (alphabsd_supply_fpreg): Fix typo last change.
[deliverable/binutils-gdb.git] / gdb / alpha-tdep.c
CommitLineData
c906108c 1/* Target-dependent code for the ALPHA architecture, for GDB, the GNU Debugger.
1e698235 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
b6ba6518 3 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
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.
c906108c 11
c5aa993b
JM
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.
c906108c 16
c5aa993b
JM
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. */
c906108c
SS
21
22#include "defs.h"
615967cb 23#include "doublest.h"
c906108c 24#include "frame.h"
d2427a71
RH
25#include "frame-unwind.h"
26#include "frame-base.h"
c906108c
SS
27#include "inferior.h"
28#include "symtab.h"
29#include "value.h"
30#include "gdbcmd.h"
31#include "gdbcore.h"
32#include "dis-asm.h"
33#include "symfile.h"
34#include "objfiles.h"
35#include "gdb_string.h"
c5f0f3d0 36#include "linespec.h"
4e052eda 37#include "regcache.h"
615967cb 38#include "reggroups.h"
dc129d82 39#include "arch-utils.h"
4be87837 40#include "osabi.h"
fe898f56 41#include "block.h"
dc129d82
JT
42
43#include "elf-bfd.h"
44
45#include "alpha-tdep.h"
46
c906108c 47\f
fa88f677 48static const char *
636a6dfc
JT
49alpha_register_name (int regno)
50{
5ab84872 51 static const char * const register_names[] =
636a6dfc
JT
52 {
53 "v0", "t0", "t1", "t2", "t3", "t4", "t5", "t6",
54 "t7", "s0", "s1", "s2", "s3", "s4", "s5", "fp",
55 "a0", "a1", "a2", "a3", "a4", "a5", "t8", "t9",
56 "t10", "t11", "ra", "t12", "at", "gp", "sp", "zero",
57 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
58 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
59 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
60 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "fpcr",
44d88583 61 "pc", "", "unique"
636a6dfc
JT
62 };
63
64 if (regno < 0)
5ab84872 65 return NULL;
636a6dfc 66 if (regno >= (sizeof(register_names) / sizeof(*register_names)))
5ab84872
RH
67 return NULL;
68 return register_names[regno];
636a6dfc 69}
d734c450 70
dc129d82 71static int
d734c450
JT
72alpha_cannot_fetch_register (int regno)
73{
44d88583 74 return regno == ALPHA_ZERO_REGNUM;
d734c450
JT
75}
76
dc129d82 77static int
d734c450
JT
78alpha_cannot_store_register (int regno)
79{
44d88583 80 return regno == ALPHA_ZERO_REGNUM;
d734c450
JT
81}
82
dc129d82 83static struct type *
c483c494 84alpha_register_type (struct gdbarch *gdbarch, int regno)
0d056799 85{
72667056
RH
86 if (regno == ALPHA_SP_REGNUM || regno == ALPHA_GP_REGNUM)
87 return builtin_type_void_data_ptr;
88 if (regno == ALPHA_PC_REGNUM)
89 return builtin_type_void_func_ptr;
90
91 /* Don't need to worry about little vs big endian until
92 some jerk tries to port to alpha-unicosmk. */
b38b6be2 93 if (regno >= ALPHA_FP0_REGNUM && regno < ALPHA_FP0_REGNUM + 31)
72667056
RH
94 return builtin_type_ieee_double_little;
95
96 return builtin_type_int64;
0d056799 97}
f8453e34 98
615967cb
RH
99/* Is REGNUM a member of REGGROUP? */
100
101static int
102alpha_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
103 struct reggroup *group)
104{
105 /* Filter out any registers eliminated, but whose regnum is
106 reserved for backward compatibility, e.g. the vfp. */
107 if (REGISTER_NAME (regnum) == NULL || *REGISTER_NAME (regnum) == '\0')
108 return 0;
109
df4a182b
RH
110 if (group == all_reggroup)
111 return 1;
112
113 /* Zero should not be saved or restored. Technically it is a general
114 register (just as $f31 would be a float if we represented it), but
115 there's no point displaying it during "info regs", so leave it out
116 of all groups except for "all". */
117 if (regnum == ALPHA_ZERO_REGNUM)
118 return 0;
119
120 /* All other registers are saved and restored. */
121 if (group == save_reggroup || group == restore_reggroup)
615967cb
RH
122 return 1;
123
124 /* All other groups are non-overlapping. */
125
126 /* Since this is really a PALcode memory slot... */
127 if (regnum == ALPHA_UNIQUE_REGNUM)
128 return group == system_reggroup;
129
130 /* Force the FPCR to be considered part of the floating point state. */
131 if (regnum == ALPHA_FPCR_REGNUM)
132 return group == float_reggroup;
133
134 if (regnum >= ALPHA_FP0_REGNUM && regnum < ALPHA_FP0_REGNUM + 31)
135 return group == float_reggroup;
136 else
137 return group == general_reggroup;
138}
139
dc129d82 140static int
f8453e34
JT
141alpha_register_byte (int regno)
142{
143 return (regno * 8);
144}
145
dc129d82 146static int
f8453e34
JT
147alpha_register_raw_size (int regno)
148{
149 return 8;
150}
151
dc129d82 152static int
f8453e34
JT
153alpha_register_virtual_size (int regno)
154{
155 return 8;
156}
636a6dfc 157
c483c494
RH
158/* The following represents exactly the conversion performed by
159 the LDS instruction. This applies to both single-precision
160 floating point and 32-bit integers. */
161
162static void
163alpha_lds (void *out, const void *in)
164{
165 ULONGEST mem = extract_unsigned_integer (in, 4);
166 ULONGEST frac = (mem >> 0) & 0x7fffff;
167 ULONGEST sign = (mem >> 31) & 1;
168 ULONGEST exp_msb = (mem >> 30) & 1;
169 ULONGEST exp_low = (mem >> 23) & 0x7f;
170 ULONGEST exp, reg;
171
172 exp = (exp_msb << 10) | exp_low;
173 if (exp_msb)
174 {
175 if (exp_low == 0x7f)
176 exp = 0x7ff;
177 }
178 else
179 {
180 if (exp_low != 0x00)
181 exp |= 0x380;
182 }
183
184 reg = (sign << 63) | (exp << 52) | (frac << 29);
185 store_unsigned_integer (out, 8, reg);
186}
187
188/* Similarly, this represents exactly the conversion performed by
189 the STS instruction. */
190
191static inline void
192alpha_sts (void *out, const void *in)
193{
194 ULONGEST reg, mem;
195
196 reg = extract_unsigned_integer (in, 8);
197 mem = ((reg >> 32) & 0xc0000000) | ((reg >> 29) & 0x3fffffff);
198 store_unsigned_integer (out, 4, mem);
199}
200
d2427a71
RH
201/* The alpha needs a conversion between register and memory format if the
202 register is a floating point register and memory format is float, as the
203 register format must be double or memory format is an integer with 4
204 bytes or less, as the representation of integers in floating point
205 registers is different. */
206
c483c494
RH
207static int
208alpha_convert_register_p (int regno)
14696584 209{
c483c494 210 return (regno >= ALPHA_FP0_REGNUM && regno < ALPHA_FP0_REGNUM + 31);
14696584
RH
211}
212
d2427a71 213static void
c483c494 214alpha_register_to_value (int regnum, struct type *valtype, char *in, char *out)
5868c862 215{
c483c494 216 switch (TYPE_LENGTH (valtype))
d2427a71 217 {
c483c494
RH
218 case 4:
219 alpha_sts (out, in);
220 break;
221 case 8:
222 memcpy (out, in, 8);
223 break;
224 default:
225 error ("Cannot retrieve value from floating point register");
d2427a71 226 }
d2427a71 227}
5868c862 228
d2427a71 229static void
c483c494 230alpha_value_to_register (struct type *valtype, int regnum, char *in, char *out)
d2427a71 231{
c483c494 232 switch (TYPE_LENGTH (valtype))
d2427a71 233 {
c483c494
RH
234 case 4:
235 alpha_lds (out, in);
236 break;
237 case 8:
238 memcpy (out, in, 8);
239 break;
240 default:
241 error ("Cannot store value in floating point register");
d2427a71 242 }
5868c862
JT
243}
244
d2427a71
RH
245\f
246/* The alpha passes the first six arguments in the registers, the rest on
c88e30c0
RH
247 the stack. The register arguments are stored in ARG_REG_BUFFER, and
248 then moved into the register file; this simplifies the passing of a
249 large struct which extends from the registers to the stack, plus avoids
250 three ptrace invocations per word.
251
252 We don't bother tracking which register values should go in integer
253 regs or fp regs; we load the same values into both.
254
d2427a71
RH
255 If the called function is returning a structure, the address of the
256 structure to be returned is passed as a hidden first argument. */
c906108c 257
d2427a71 258static CORE_ADDR
c88e30c0
RH
259alpha_push_dummy_call (struct gdbarch *gdbarch, CORE_ADDR func_addr,
260 struct regcache *regcache, CORE_ADDR bp_addr,
261 int nargs, struct value **args, CORE_ADDR sp,
262 int struct_return, CORE_ADDR struct_addr)
c906108c 263{
d2427a71
RH
264 int i;
265 int accumulate_size = struct_return ? 8 : 0;
d2427a71 266 struct alpha_arg
c906108c 267 {
d2427a71
RH
268 char *contents;
269 int len;
270 int offset;
271 };
c88e30c0
RH
272 struct alpha_arg *alpha_args
273 = (struct alpha_arg *) alloca (nargs * sizeof (struct alpha_arg));
d2427a71 274 register struct alpha_arg *m_arg;
c88e30c0 275 char arg_reg_buffer[ALPHA_REGISTER_SIZE * ALPHA_NUM_ARG_REGS];
d2427a71 276 int required_arg_regs;
c906108c 277
c88e30c0
RH
278 /* The ABI places the address of the called function in T12. */
279 regcache_cooked_write_signed (regcache, ALPHA_T12_REGNUM, func_addr);
280
281 /* Set the return address register to point to the entry point
282 of the program, where a breakpoint lies in wait. */
283 regcache_cooked_write_signed (regcache, ALPHA_RA_REGNUM, bp_addr);
284
285 /* Lay out the arguments in memory. */
d2427a71
RH
286 for (i = 0, m_arg = alpha_args; i < nargs; i++, m_arg++)
287 {
288 struct value *arg = args[i];
289 struct type *arg_type = check_typedef (VALUE_TYPE (arg));
c88e30c0 290
d2427a71
RH
291 /* Cast argument to long if necessary as the compiler does it too. */
292 switch (TYPE_CODE (arg_type))
c906108c 293 {
d2427a71
RH
294 case TYPE_CODE_INT:
295 case TYPE_CODE_BOOL:
296 case TYPE_CODE_CHAR:
297 case TYPE_CODE_RANGE:
298 case TYPE_CODE_ENUM:
0ede8eca 299 if (TYPE_LENGTH (arg_type) == 4)
d2427a71 300 {
0ede8eca
RH
301 /* 32-bit values must be sign-extended to 64 bits
302 even if the base data type is unsigned. */
303 arg_type = builtin_type_int32;
304 arg = value_cast (arg_type, arg);
305 }
306 if (TYPE_LENGTH (arg_type) < ALPHA_REGISTER_SIZE)
307 {
308 arg_type = builtin_type_int64;
d2427a71
RH
309 arg = value_cast (arg_type, arg);
310 }
311 break;
7b5e1cb3 312
c88e30c0
RH
313 case TYPE_CODE_FLT:
314 /* "float" arguments loaded in registers must be passed in
315 register format, aka "double". */
316 if (accumulate_size < sizeof (arg_reg_buffer)
317 && TYPE_LENGTH (arg_type) == 4)
318 {
eb4edb88 319 arg_type = builtin_type_ieee_double_little;
c88e30c0
RH
320 arg = value_cast (arg_type, arg);
321 }
322 /* Tru64 5.1 has a 128-bit long double, and passes this by
323 invisible reference. No one else uses this data type. */
324 else if (TYPE_LENGTH (arg_type) == 16)
325 {
326 /* Allocate aligned storage. */
327 sp = (sp & -16) - 16;
328
329 /* Write the real data into the stack. */
330 write_memory (sp, VALUE_CONTENTS (arg), 16);
331
332 /* Construct the indirection. */
333 arg_type = lookup_pointer_type (arg_type);
334 arg = value_from_pointer (arg_type, sp);
335 }
336 break;
7b5e1cb3
RH
337
338 case TYPE_CODE_COMPLEX:
339 /* ??? The ABI says that complex values are passed as two
340 separate scalar values. This distinction only matters
341 for complex float. However, GCC does not implement this. */
342
343 /* Tru64 5.1 has a 128-bit long double, and passes this by
344 invisible reference. */
345 if (TYPE_LENGTH (arg_type) == 32)
346 {
347 /* Allocate aligned storage. */
348 sp = (sp & -16) - 16;
349
350 /* Write the real data into the stack. */
351 write_memory (sp, VALUE_CONTENTS (arg), 32);
352
353 /* Construct the indirection. */
354 arg_type = lookup_pointer_type (arg_type);
355 arg = value_from_pointer (arg_type, sp);
356 }
357 break;
358
d2427a71
RH
359 default:
360 break;
c906108c 361 }
d2427a71
RH
362 m_arg->len = TYPE_LENGTH (arg_type);
363 m_arg->offset = accumulate_size;
364 accumulate_size = (accumulate_size + m_arg->len + 7) & ~7;
365 m_arg->contents = VALUE_CONTENTS (arg);
c906108c
SS
366 }
367
d2427a71
RH
368 /* Determine required argument register loads, loading an argument register
369 is expensive as it uses three ptrace calls. */
370 required_arg_regs = accumulate_size / 8;
371 if (required_arg_regs > ALPHA_NUM_ARG_REGS)
372 required_arg_regs = ALPHA_NUM_ARG_REGS;
c906108c 373
d2427a71 374 /* Make room for the arguments on the stack. */
c88e30c0
RH
375 if (accumulate_size < sizeof(arg_reg_buffer))
376 accumulate_size = 0;
377 else
378 accumulate_size -= sizeof(arg_reg_buffer);
d2427a71 379 sp -= accumulate_size;
c906108c 380
c88e30c0 381 /* Keep sp aligned to a multiple of 16 as the ABI requires. */
d2427a71 382 sp &= ~15;
c906108c 383
d2427a71
RH
384 /* `Push' arguments on the stack. */
385 for (i = nargs; m_arg--, --i >= 0;)
c906108c 386 {
c88e30c0
RH
387 char *contents = m_arg->contents;
388 int offset = m_arg->offset;
389 int len = m_arg->len;
390
391 /* Copy the bytes destined for registers into arg_reg_buffer. */
392 if (offset < sizeof(arg_reg_buffer))
393 {
394 if (offset + len <= sizeof(arg_reg_buffer))
395 {
396 memcpy (arg_reg_buffer + offset, contents, len);
397 continue;
398 }
399 else
400 {
401 int tlen = sizeof(arg_reg_buffer) - offset;
402 memcpy (arg_reg_buffer + offset, contents, tlen);
403 offset += tlen;
404 contents += tlen;
405 len -= tlen;
406 }
407 }
408
409 /* Everything else goes to the stack. */
410 write_memory (sp + offset - sizeof(arg_reg_buffer), contents, len);
c906108c 411 }
c88e30c0
RH
412 if (struct_return)
413 store_unsigned_integer (arg_reg_buffer, ALPHA_REGISTER_SIZE, struct_addr);
c906108c 414
d2427a71
RH
415 /* Load the argument registers. */
416 for (i = 0; i < required_arg_regs; i++)
417 {
09cc52fd
RH
418 regcache_cooked_write (regcache, ALPHA_A0_REGNUM + i,
419 arg_reg_buffer + i*ALPHA_REGISTER_SIZE);
420 regcache_cooked_write (regcache, ALPHA_FPA0_REGNUM + i,
421 arg_reg_buffer + i*ALPHA_REGISTER_SIZE);
d2427a71 422 }
c906108c 423
09cc52fd
RH
424 /* Finally, update the stack pointer. */
425 regcache_cooked_write_signed (regcache, ALPHA_SP_REGNUM, sp);
426
c88e30c0 427 return sp;
c906108c
SS
428}
429
5ec2bb99
RH
430/* Extract from REGCACHE the value about to be returned from a function
431 and copy it into VALBUF. */
d2427a71 432
dc129d82 433static void
5ec2bb99
RH
434alpha_extract_return_value (struct type *valtype, struct regcache *regcache,
435 void *valbuf)
140f9984 436{
7b5e1cb3 437 int length = TYPE_LENGTH (valtype);
5ec2bb99
RH
438 char raw_buffer[ALPHA_REGISTER_SIZE];
439 ULONGEST l;
440
441 switch (TYPE_CODE (valtype))
442 {
443 case TYPE_CODE_FLT:
7b5e1cb3 444 switch (length)
5ec2bb99
RH
445 {
446 case 4:
447 regcache_cooked_read (regcache, ALPHA_FP0_REGNUM, raw_buffer);
c483c494 448 alpha_sts (valbuf, raw_buffer);
5ec2bb99
RH
449 break;
450
451 case 8:
452 regcache_cooked_read (regcache, ALPHA_FP0_REGNUM, valbuf);
453 break;
454
24064b5c
RH
455 case 16:
456 regcache_cooked_read_unsigned (regcache, ALPHA_V0_REGNUM, &l);
457 read_memory (l, valbuf, 16);
458 break;
459
5ec2bb99 460 default:
67dfac52 461 internal_error (__FILE__, __LINE__, "unknown floating point width");
5ec2bb99
RH
462 }
463 break;
464
7b5e1cb3
RH
465 case TYPE_CODE_COMPLEX:
466 switch (length)
467 {
468 case 8:
469 /* ??? This isn't correct wrt the ABI, but it's what GCC does. */
470 regcache_cooked_read (regcache, ALPHA_FP0_REGNUM, valbuf);
471 break;
472
473 case 16:
474 regcache_cooked_read (regcache, ALPHA_FP0_REGNUM, valbuf);
475 regcache_cooked_read (regcache, ALPHA_FP0_REGNUM+1,
476 (char *)valbuf + 8);
477 break;
478
479 case 32:
480 regcache_cooked_read_signed (regcache, ALPHA_V0_REGNUM, &l);
481 read_memory (l, valbuf, 32);
482 break;
483
484 default:
67dfac52 485 internal_error (__FILE__, __LINE__, "unknown floating point width");
7b5e1cb3
RH
486 }
487 break;
488
5ec2bb99
RH
489 default:
490 /* Assume everything else degenerates to an integer. */
491 regcache_cooked_read_unsigned (regcache, ALPHA_V0_REGNUM, &l);
7b5e1cb3 492 store_unsigned_integer (valbuf, length, l);
5ec2bb99
RH
493 break;
494 }
140f9984
JT
495}
496
5ec2bb99
RH
497/* Extract from REGCACHE the address of a structure about to be returned
498 from a function. */
499
500static CORE_ADDR
501alpha_extract_struct_value_address (struct regcache *regcache)
502{
503 ULONGEST addr;
504 regcache_cooked_read_unsigned (regcache, ALPHA_V0_REGNUM, &addr);
505 return addr;
506}
507
508/* Insert the given value into REGCACHE as if it was being
509 returned by a function. */
0d056799 510
d2427a71 511static void
5ec2bb99
RH
512alpha_store_return_value (struct type *valtype, struct regcache *regcache,
513 const void *valbuf)
c906108c 514{
d2427a71 515 int length = TYPE_LENGTH (valtype);
5ec2bb99
RH
516 char raw_buffer[ALPHA_REGISTER_SIZE];
517 ULONGEST l;
d2427a71 518
5ec2bb99 519 switch (TYPE_CODE (valtype))
c906108c 520 {
5ec2bb99
RH
521 case TYPE_CODE_FLT:
522 switch (length)
523 {
524 case 4:
c483c494 525 alpha_lds (raw_buffer, valbuf);
f75d70cc
RH
526 regcache_cooked_write (regcache, ALPHA_FP0_REGNUM, raw_buffer);
527 break;
5ec2bb99
RH
528
529 case 8:
530 regcache_cooked_write (regcache, ALPHA_FP0_REGNUM, valbuf);
531 break;
532
24064b5c
RH
533 case 16:
534 /* FIXME: 128-bit long doubles are returned like structures:
535 by writing into indirect storage provided by the caller
536 as the first argument. */
537 error ("Cannot set a 128-bit long double return value.");
538
5ec2bb99 539 default:
67dfac52 540 internal_error (__FILE__, __LINE__, "unknown floating point width");
5ec2bb99
RH
541 }
542 break;
d2427a71 543
7b5e1cb3
RH
544 case TYPE_CODE_COMPLEX:
545 switch (length)
546 {
547 case 8:
548 /* ??? This isn't correct wrt the ABI, but it's what GCC does. */
549 regcache_cooked_write (regcache, ALPHA_FP0_REGNUM, valbuf);
550 break;
551
552 case 16:
553 regcache_cooked_write (regcache, ALPHA_FP0_REGNUM, valbuf);
554 regcache_cooked_write (regcache, ALPHA_FP0_REGNUM+1,
555 (const char *)valbuf + 8);
556 break;
557
558 case 32:
559 /* FIXME: 128-bit long doubles are returned like structures:
560 by writing into indirect storage provided by the caller
561 as the first argument. */
562 error ("Cannot set a 128-bit long double return value.");
563
564 default:
67dfac52 565 internal_error (__FILE__, __LINE__, "unknown floating point width");
7b5e1cb3
RH
566 }
567 break;
568
5ec2bb99
RH
569 default:
570 /* Assume everything else degenerates to an integer. */
0ede8eca
RH
571 /* 32-bit values must be sign-extended to 64 bits
572 even if the base data type is unsigned. */
573 if (length == 4)
574 valtype = builtin_type_int32;
5ec2bb99
RH
575 l = unpack_long (valtype, valbuf);
576 regcache_cooked_write_unsigned (regcache, ALPHA_V0_REGNUM, l);
577 break;
578 }
c906108c
SS
579}
580
d2427a71
RH
581static int
582alpha_use_struct_convention (int gcc_p, struct type *type)
c906108c 583{
d2427a71
RH
584 /* Structures are returned by ref in extra arg0. */
585 return 1;
586}
c906108c 587
d2427a71
RH
588\f
589static const unsigned char *
590alpha_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
c906108c 591{
d2427a71
RH
592 static const unsigned char alpha_breakpoint[] =
593 { 0x80, 0, 0, 0 }; /* call_pal bpt */
c906108c 594
d2427a71
RH
595 *lenptr = sizeof(alpha_breakpoint);
596 return (alpha_breakpoint);
597}
c906108c 598
d2427a71
RH
599\f
600/* This returns the PC of the first insn after the prologue.
601 If we can't find the prologue, then return 0. */
c906108c 602
d2427a71
RH
603CORE_ADDR
604alpha_after_prologue (CORE_ADDR pc)
c906108c 605{
d2427a71
RH
606 struct symtab_and_line sal;
607 CORE_ADDR func_addr, func_end;
c906108c 608
d2427a71 609 if (!find_pc_partial_function (pc, NULL, &func_addr, &func_end))
c5aa993b 610 return 0;
c906108c 611
d2427a71
RH
612 sal = find_pc_line (func_addr, 0);
613 if (sal.end < func_end)
614 return sal.end;
c5aa993b 615
d2427a71
RH
616 /* The line after the prologue is after the end of the function. In this
617 case, tell the caller to find the prologue the hard way. */
618 return 0;
c906108c
SS
619}
620
d2427a71
RH
621/* Read an instruction from memory at PC, looking through breakpoints. */
622
623unsigned int
624alpha_read_insn (CORE_ADDR pc)
c906108c 625{
d2427a71
RH
626 char buf[4];
627 int status;
c5aa993b 628
d2427a71
RH
629 status = read_memory_nobpt (pc, buf, 4);
630 if (status)
631 memory_error (status, pc);
632 return extract_unsigned_integer (buf, 4);
633}
c5aa993b 634
d2427a71
RH
635/* To skip prologues, I use this predicate. Returns either PC itself
636 if the code at PC does not look like a function prologue; otherwise
637 returns an address that (if we're lucky) follows the prologue. If
638 LENIENT, then we must skip everything which is involved in setting
639 up the frame (it's OK to skip more, just so long as we don't skip
640 anything which might clobber the registers which are being saved. */
c906108c 641
d2427a71
RH
642static CORE_ADDR
643alpha_skip_prologue (CORE_ADDR pc)
644{
645 unsigned long inst;
646 int offset;
647 CORE_ADDR post_prologue_pc;
648 char buf[4];
c906108c 649
d2427a71
RH
650 /* Silently return the unaltered pc upon memory errors.
651 This could happen on OSF/1 if decode_line_1 tries to skip the
652 prologue for quickstarted shared library functions when the
653 shared library is not yet mapped in.
654 Reading target memory is slow over serial lines, so we perform
655 this check only if the target has shared libraries (which all
656 Alpha targets do). */
657 if (target_read_memory (pc, buf, 4))
658 return pc;
c906108c 659
d2427a71
RH
660 /* See if we can determine the end of the prologue via the symbol table.
661 If so, then return either PC, or the PC after the prologue, whichever
662 is greater. */
c906108c 663
d2427a71
RH
664 post_prologue_pc = alpha_after_prologue (pc);
665 if (post_prologue_pc != 0)
666 return max (pc, post_prologue_pc);
c906108c 667
d2427a71
RH
668 /* Can't determine prologue from the symbol table, need to examine
669 instructions. */
dc1b0db2 670
d2427a71
RH
671 /* Skip the typical prologue instructions. These are the stack adjustment
672 instruction and the instructions that save registers on the stack
673 or in the gcc frame. */
674 for (offset = 0; offset < 100; offset += 4)
675 {
676 inst = alpha_read_insn (pc + offset);
c906108c 677
d2427a71
RH
678 if ((inst & 0xffff0000) == 0x27bb0000) /* ldah $gp,n($t12) */
679 continue;
680 if ((inst & 0xffff0000) == 0x23bd0000) /* lda $gp,n($gp) */
681 continue;
682 if ((inst & 0xffff0000) == 0x23de0000) /* lda $sp,n($sp) */
683 continue;
684 if ((inst & 0xffe01fff) == 0x43c0153e) /* subq $sp,n,$sp */
685 continue;
c906108c 686
d2427a71
RH
687 if (((inst & 0xfc1f0000) == 0xb41e0000 /* stq reg,n($sp) */
688 || (inst & 0xfc1f0000) == 0x9c1e0000) /* stt reg,n($sp) */
689 && (inst & 0x03e00000) != 0x03e00000) /* reg != $zero */
690 continue;
c906108c 691
d2427a71
RH
692 if (inst == 0x47de040f) /* bis sp,sp,fp */
693 continue;
694 if (inst == 0x47fe040f) /* bis zero,sp,fp */
695 continue;
c906108c 696
d2427a71 697 break;
c906108c 698 }
d2427a71
RH
699 return pc + offset;
700}
c906108c 701
d2427a71
RH
702\f
703/* Figure out where the longjmp will land.
704 We expect the first arg to be a pointer to the jmp_buf structure from
705 which we extract the PC (JB_PC) that we will land at. The PC is copied
706 into the "pc". This routine returns true on success. */
c906108c
SS
707
708static int
d2427a71 709alpha_get_longjmp_target (CORE_ADDR *pc)
c906108c 710{
d2427a71
RH
711 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
712 CORE_ADDR jb_addr;
5ab84872 713 char raw_buffer[ALPHA_REGISTER_SIZE];
c906108c 714
d2427a71 715 jb_addr = read_register (ALPHA_A0_REGNUM);
c906108c 716
d2427a71
RH
717 if (target_read_memory (jb_addr + (tdep->jb_pc * tdep->jb_elt_size),
718 raw_buffer, tdep->jb_elt_size))
c906108c 719 return 0;
d2427a71 720
7c0b4a20 721 *pc = extract_unsigned_integer (raw_buffer, tdep->jb_elt_size);
d2427a71 722 return 1;
c906108c
SS
723}
724
d2427a71
RH
725\f
726/* Frame unwinder for signal trampolines. We use alpha tdep bits that
727 describe the location and shape of the sigcontext structure. After
728 that, all registers are in memory, so it's easy. */
729/* ??? Shouldn't we be able to do this generically, rather than with
730 OSABI data specific to Alpha? */
731
732struct alpha_sigtramp_unwind_cache
c906108c 733{
d2427a71
RH
734 CORE_ADDR sigcontext_addr;
735};
c906108c 736
d2427a71
RH
737static struct alpha_sigtramp_unwind_cache *
738alpha_sigtramp_frame_unwind_cache (struct frame_info *next_frame,
739 void **this_prologue_cache)
740{
741 struct alpha_sigtramp_unwind_cache *info;
742 struct gdbarch_tdep *tdep;
c906108c 743
d2427a71
RH
744 if (*this_prologue_cache)
745 return *this_prologue_cache;
c906108c 746
d2427a71
RH
747 info = FRAME_OBSTACK_ZALLOC (struct alpha_sigtramp_unwind_cache);
748 *this_prologue_cache = info;
c906108c 749
d2427a71
RH
750 tdep = gdbarch_tdep (current_gdbarch);
751 info->sigcontext_addr = tdep->sigcontext_addr (next_frame);
c906108c 752
d2427a71 753 return info;
c906108c
SS
754}
755
d2427a71
RH
756/* Return the address of REGNO in a sigtramp frame. Since this is all
757 arithmetic, it doesn't seem worthwhile to cache it. */
c5aa993b 758
d2427a71
RH
759#ifndef SIGFRAME_PC_OFF
760#define SIGFRAME_PC_OFF (2 * 8)
761#define SIGFRAME_REGSAVE_OFF (4 * 8)
762#define SIGFRAME_FPREGSAVE_OFF (SIGFRAME_REGSAVE_OFF + 32 * 8 + 8)
763#endif
c5aa993b 764
d2427a71
RH
765static CORE_ADDR
766alpha_sigtramp_register_address (CORE_ADDR sigcontext_addr, unsigned int regno)
767{
768 if (regno < 32)
769 return sigcontext_addr + SIGFRAME_REGSAVE_OFF + regno * 8;
b38b6be2 770 if (regno >= ALPHA_FP0_REGNUM && regno < ALPHA_FP0_REGNUM + 32)
d2427a71 771 return sigcontext_addr + SIGFRAME_FPREGSAVE_OFF + regno * 8;
b38b6be2 772 if (regno == ALPHA_PC_REGNUM)
d2427a71 773 return sigcontext_addr + SIGFRAME_PC_OFF;
c5aa993b 774
d2427a71 775 return 0;
c906108c
SS
776}
777
d2427a71
RH
778/* Given a GDB frame, determine the address of the calling function's
779 frame. This will be used to create a new GDB frame struct. */
140f9984 780
dc129d82 781static void
d2427a71
RH
782alpha_sigtramp_frame_this_id (struct frame_info *next_frame,
783 void **this_prologue_cache,
784 struct frame_id *this_id)
c906108c 785{
d2427a71
RH
786 struct alpha_sigtramp_unwind_cache *info
787 = alpha_sigtramp_frame_unwind_cache (next_frame, this_prologue_cache);
788 struct gdbarch_tdep *tdep;
789 CORE_ADDR stack_addr, code_addr;
790
791 /* If the OSABI couldn't locate the sigcontext, give up. */
792 if (info->sigcontext_addr == 0)
793 return;
794
795 /* If we have dynamic signal trampolines, find their start.
796 If we do not, then we must assume there is a symbol record
797 that can provide the start address. */
798 tdep = gdbarch_tdep (current_gdbarch);
799 if (tdep->dynamic_sigtramp_offset)
c906108c 800 {
d2427a71
RH
801 int offset;
802 code_addr = frame_pc_unwind (next_frame);
803 offset = tdep->dynamic_sigtramp_offset (code_addr);
804 if (offset >= 0)
805 code_addr -= offset;
c906108c 806 else
d2427a71 807 code_addr = 0;
c906108c 808 }
d2427a71
RH
809 else
810 code_addr = frame_func_unwind (next_frame);
c906108c 811
d2427a71
RH
812 /* The stack address is trivially read from the sigcontext. */
813 stack_addr = alpha_sigtramp_register_address (info->sigcontext_addr,
814 ALPHA_SP_REGNUM);
815 stack_addr = read_memory_unsigned_integer (stack_addr, ALPHA_REGISTER_SIZE);
c906108c 816
d2427a71 817 *this_id = frame_id_build (stack_addr, code_addr);
c906108c
SS
818}
819
d2427a71 820/* Retrieve the value of REGNUM in FRAME. Don't give up! */
c906108c 821
d2427a71
RH
822static void
823alpha_sigtramp_frame_prev_register (struct frame_info *next_frame,
824 void **this_prologue_cache,
825 int regnum, int *optimizedp,
826 enum lval_type *lvalp, CORE_ADDR *addrp,
827 int *realnump, void *bufferp)
c906108c 828{
d2427a71
RH
829 struct alpha_sigtramp_unwind_cache *info
830 = alpha_sigtramp_frame_unwind_cache (next_frame, this_prologue_cache);
831 CORE_ADDR addr;
c906108c 832
d2427a71 833 if (info->sigcontext_addr != 0)
c906108c 834 {
d2427a71
RH
835 /* All integer and fp registers are stored in memory. */
836 addr = alpha_sigtramp_register_address (info->sigcontext_addr, regnum);
837 if (addr != 0)
c906108c 838 {
d2427a71
RH
839 *optimizedp = 0;
840 *lvalp = lval_memory;
841 *addrp = addr;
842 *realnump = -1;
843 if (bufferp != NULL)
844 read_memory (addr, bufferp, ALPHA_REGISTER_SIZE);
845 return;
c906108c 846 }
c906108c
SS
847 }
848
d2427a71
RH
849 /* This extra register may actually be in the sigcontext, but our
850 current description of it in alpha_sigtramp_frame_unwind_cache
851 doesn't include it. Too bad. Fall back on whatever's in the
852 outer frame. */
853 frame_register (next_frame, regnum, optimizedp, lvalp, addrp,
854 realnump, bufferp);
855}
c906108c 856
d2427a71
RH
857static const struct frame_unwind alpha_sigtramp_frame_unwind = {
858 SIGTRAMP_FRAME,
859 alpha_sigtramp_frame_this_id,
860 alpha_sigtramp_frame_prev_register
861};
c906108c 862
d2427a71
RH
863static const struct frame_unwind *
864alpha_sigtramp_frame_p (CORE_ADDR pc)
865{
866 char *name;
c906108c 867
d2427a71
RH
868 /* We shouldn't even bother to try if the OSABI didn't register
869 a sigcontext_addr handler. */
870 if (!gdbarch_tdep (current_gdbarch)->sigcontext_addr)
871 return NULL;
c906108c 872
d2427a71
RH
873 /* Otherwise we should be in a signal frame. */
874 find_pc_partial_function (pc, &name, NULL, NULL);
875 if (PC_IN_SIGTRAMP (pc, name))
876 return &alpha_sigtramp_frame_unwind;
c906108c 877
d2427a71 878 return NULL;
c906108c 879}
d2427a71
RH
880\f
881/* Fallback alpha frame unwinder. Uses instruction scanning and knows
882 something about the traditional layout of alpha stack frames. */
c906108c 883
d2427a71 884struct alpha_heuristic_unwind_cache
c906108c 885{
d2427a71
RH
886 CORE_ADDR *saved_regs;
887 CORE_ADDR vfp;
888 CORE_ADDR start_pc;
889 int return_reg;
890};
c906108c 891
d2427a71
RH
892/* Heuristic_proc_start may hunt through the text section for a long
893 time across a 2400 baud serial line. Allows the user to limit this
894 search. */
895static unsigned int heuristic_fence_post = 0;
c906108c 896
d2427a71
RH
897/* Attempt to locate the start of the function containing PC. We assume that
898 the previous function ends with an about_to_return insn. Not foolproof by
899 any means, since gcc is happy to put the epilogue in the middle of a
900 function. But we're guessing anyway... */
c906108c 901
d2427a71
RH
902static CORE_ADDR
903alpha_heuristic_proc_start (CORE_ADDR pc)
904{
905 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
906 CORE_ADDR last_non_nop = pc;
907 CORE_ADDR fence = pc - heuristic_fence_post;
908 CORE_ADDR orig_pc = pc;
fbe586ae 909 CORE_ADDR func;
9e0b60a8 910
d2427a71
RH
911 if (pc == 0)
912 return 0;
9e0b60a8 913
fbe586ae
RH
914 /* First see if we can find the start of the function from minimal
915 symbol information. This can succeed with a binary that doesn't
916 have debug info, but hasn't been stripped. */
917 func = get_pc_function_start (pc);
918 if (func)
919 return func;
920
d2427a71
RH
921 if (heuristic_fence_post == UINT_MAX
922 || fence < tdep->vm_min_address)
923 fence = tdep->vm_min_address;
c906108c 924
d2427a71
RH
925 /* Search back for previous return; also stop at a 0, which might be
926 seen for instance before the start of a code section. Don't include
927 nops, since this usually indicates padding between functions. */
928 for (pc -= 4; pc >= fence; pc -= 4)
c906108c 929 {
d2427a71
RH
930 unsigned int insn = alpha_read_insn (pc);
931 switch (insn)
c906108c 932 {
d2427a71
RH
933 case 0: /* invalid insn */
934 case 0x6bfa8001: /* ret $31,($26),1 */
935 return last_non_nop;
936
937 case 0x2ffe0000: /* unop: ldq_u $31,0($30) */
938 case 0x47ff041f: /* nop: bis $31,$31,$31 */
939 break;
940
941 default:
942 last_non_nop = pc;
943 break;
c906108c 944 }
d2427a71 945 }
c906108c 946
d2427a71
RH
947 /* It's not clear to me why we reach this point when stopping quietly,
948 but with this test, at least we don't print out warnings for every
949 child forked (eg, on decstation). 22apr93 rich@cygnus.com. */
950 if (stop_soon == NO_STOP_QUIETLY)
951 {
952 static int blurb_printed = 0;
c906108c 953
d2427a71
RH
954 if (fence == tdep->vm_min_address)
955 warning ("Hit beginning of text section without finding");
c906108c 956 else
d2427a71
RH
957 warning ("Hit heuristic-fence-post without finding");
958 warning ("enclosing function for address 0x%s", paddr_nz (orig_pc));
c906108c 959
d2427a71
RH
960 if (!blurb_printed)
961 {
962 printf_filtered ("\
963This warning occurs if you are debugging a function without any symbols\n\
964(for example, in a stripped executable). In that case, you may wish to\n\
965increase the size of the search with the `set heuristic-fence-post' command.\n\
966\n\
967Otherwise, you told GDB there was a function where there isn't one, or\n\
968(more likely) you have encountered a bug in GDB.\n");
969 blurb_printed = 1;
970 }
971 }
c906108c 972
d2427a71
RH
973 return 0;
974}
c906108c 975
fbe586ae 976static struct alpha_heuristic_unwind_cache *
d2427a71
RH
977alpha_heuristic_frame_unwind_cache (struct frame_info *next_frame,
978 void **this_prologue_cache,
979 CORE_ADDR start_pc)
980{
981 struct alpha_heuristic_unwind_cache *info;
982 ULONGEST val;
983 CORE_ADDR limit_pc, cur_pc;
984 int frame_reg, frame_size, return_reg, reg;
c906108c 985
d2427a71
RH
986 if (*this_prologue_cache)
987 return *this_prologue_cache;
c906108c 988
d2427a71
RH
989 info = FRAME_OBSTACK_ZALLOC (struct alpha_heuristic_unwind_cache);
990 *this_prologue_cache = info;
991 info->saved_regs = frame_obstack_zalloc (SIZEOF_FRAME_SAVED_REGS);
c906108c 992
d2427a71
RH
993 limit_pc = frame_pc_unwind (next_frame);
994 if (start_pc == 0)
995 start_pc = alpha_heuristic_proc_start (limit_pc);
996 info->start_pc = start_pc;
c906108c 997
d2427a71
RH
998 frame_reg = ALPHA_SP_REGNUM;
999 frame_size = 0;
1000 return_reg = -1;
c906108c 1001
d2427a71
RH
1002 /* If we've identified a likely place to start, do code scanning. */
1003 if (start_pc != 0)
c5aa993b 1004 {
d2427a71
RH
1005 /* Limit the forward search to 50 instructions. */
1006 if (start_pc + 200 < limit_pc)
1007 limit_pc = start_pc + 200;
c5aa993b 1008
d2427a71
RH
1009 for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += 4)
1010 {
1011 unsigned int word = alpha_read_insn (cur_pc);
c5aa993b 1012
d2427a71
RH
1013 if ((word & 0xffff0000) == 0x23de0000) /* lda $sp,n($sp) */
1014 {
1015 if (word & 0x8000)
1016 {
1017 /* Consider only the first stack allocation instruction
1018 to contain the static size of the frame. */
1019 if (frame_size == 0)
1020 frame_size = (-word) & 0xffff;
1021 }
1022 else
1023 {
1024 /* Exit loop if a positive stack adjustment is found, which
1025 usually means that the stack cleanup code in the function
1026 epilogue is reached. */
1027 break;
1028 }
1029 }
1030 else if ((word & 0xfc1f0000) == 0xb41e0000) /* stq reg,n($sp) */
1031 {
1032 reg = (word & 0x03e00000) >> 21;
1033
1034 if (reg == 31)
1035 continue;
1036
1037 /* Do not compute the address where the register was saved yet,
1038 because we don't know yet if the offset will need to be
1039 relative to $sp or $fp (we can not compute the address
1040 relative to $sp if $sp is updated during the execution of
1041 the current subroutine, for instance when doing some alloca).
1042 So just store the offset for the moment, and compute the
1043 address later when we know whether this frame has a frame
1044 pointer or not. */
1045 /* Hack: temporarily add one, so that the offset is non-zero
1046 and we can tell which registers have save offsets below. */
1047 info->saved_regs[reg] = (word & 0xffff) + 1;
1048
1049 /* Starting with OSF/1-3.2C, the system libraries are shipped
1050 without local symbols, but they still contain procedure
1051 descriptors without a symbol reference. GDB is currently
1052 unable to find these procedure descriptors and uses
1053 heuristic_proc_desc instead.
1054 As some low level compiler support routines (__div*, __add*)
1055 use a non-standard return address register, we have to
1056 add some heuristics to determine the return address register,
1057 or stepping over these routines will fail.
1058 Usually the return address register is the first register
1059 saved on the stack, but assembler optimization might
1060 rearrange the register saves.
1061 So we recognize only a few registers (t7, t9, ra) within
1062 the procedure prologue as valid return address registers.
1063 If we encounter a return instruction, we extract the
1064 the return address register from it.
1065
1066 FIXME: Rewriting GDB to access the procedure descriptors,
1067 e.g. via the minimal symbol table, might obviate this hack. */
1068 if (return_reg == -1
1069 && cur_pc < (start_pc + 80)
1070 && (reg == ALPHA_T7_REGNUM
1071 || reg == ALPHA_T9_REGNUM
1072 || reg == ALPHA_RA_REGNUM))
1073 return_reg = reg;
1074 }
1075 else if ((word & 0xffe0ffff) == 0x6be08001) /* ret zero,reg,1 */
1076 return_reg = (word >> 16) & 0x1f;
1077 else if (word == 0x47de040f) /* bis sp,sp,fp */
1078 frame_reg = ALPHA_GCC_FP_REGNUM;
1079 else if (word == 0x47fe040f) /* bis zero,sp,fp */
1080 frame_reg = ALPHA_GCC_FP_REGNUM;
1081 }
c5aa993b 1082
d2427a71
RH
1083 /* If we haven't found a valid return address register yet, keep
1084 searching in the procedure prologue. */
1085 if (return_reg == -1)
1086 {
1087 while (cur_pc < (limit_pc + 80) && cur_pc < (start_pc + 80))
1088 {
1089 unsigned int word = alpha_read_insn (cur_pc);
c5aa993b 1090
d2427a71
RH
1091 if ((word & 0xfc1f0000) == 0xb41e0000) /* stq reg,n($sp) */
1092 {
1093 reg = (word & 0x03e00000) >> 21;
1094 if (reg == ALPHA_T7_REGNUM
1095 || reg == ALPHA_T9_REGNUM
1096 || reg == ALPHA_RA_REGNUM)
1097 {
1098 return_reg = reg;
1099 break;
1100 }
1101 }
1102 else if ((word & 0xffe0ffff) == 0x6be08001) /* ret zero,reg,1 */
1103 {
1104 return_reg = (word >> 16) & 0x1f;
1105 break;
1106 }
85b32d22
RH
1107
1108 cur_pc += 4;
d2427a71
RH
1109 }
1110 }
c906108c 1111 }
c906108c 1112
d2427a71
RH
1113 /* Failing that, do default to the customary RA. */
1114 if (return_reg == -1)
1115 return_reg = ALPHA_RA_REGNUM;
1116 info->return_reg = return_reg;
f8453e34 1117
d2427a71
RH
1118 frame_unwind_unsigned_register (next_frame, frame_reg, &val);
1119 info->vfp = val + frame_size;
c906108c 1120
d2427a71
RH
1121 /* Convert offsets to absolute addresses. See above about adding
1122 one to the offsets to make all detected offsets non-zero. */
1123 for (reg = 0; reg < ALPHA_NUM_REGS; ++reg)
1124 if (info->saved_regs[reg])
1125 info->saved_regs[reg] += val - 1;
1126
1127 return info;
c906108c 1128}
c906108c 1129
d2427a71
RH
1130/* Given a GDB frame, determine the address of the calling function's
1131 frame. This will be used to create a new GDB frame struct. */
1132
fbe586ae 1133static void
d2427a71
RH
1134alpha_heuristic_frame_this_id (struct frame_info *next_frame,
1135 void **this_prologue_cache,
1136 struct frame_id *this_id)
c906108c 1137{
d2427a71
RH
1138 struct alpha_heuristic_unwind_cache *info
1139 = alpha_heuristic_frame_unwind_cache (next_frame, this_prologue_cache, 0);
c906108c 1140
fbe586ae
RH
1141 /* This is meant to halt the backtrace at "_start". Make sure we
1142 don't halt it at a generic dummy frame. */
1143 if (inside_entry_file (info->start_pc))
1144 return;
1145
d2427a71 1146 *this_id = frame_id_build (info->vfp, info->start_pc);
c906108c
SS
1147}
1148
d2427a71
RH
1149/* Retrieve the value of REGNUM in FRAME. Don't give up! */
1150
fbe586ae 1151static void
d2427a71
RH
1152alpha_heuristic_frame_prev_register (struct frame_info *next_frame,
1153 void **this_prologue_cache,
1154 int regnum, int *optimizedp,
1155 enum lval_type *lvalp, CORE_ADDR *addrp,
1156 int *realnump, void *bufferp)
c906108c 1157{
d2427a71
RH
1158 struct alpha_heuristic_unwind_cache *info
1159 = alpha_heuristic_frame_unwind_cache (next_frame, this_prologue_cache, 0);
1160
1161 /* The PC of the previous frame is stored in the link register of
1162 the current frame. Frob regnum so that we pull the value from
1163 the correct place. */
1164 if (regnum == ALPHA_PC_REGNUM)
1165 regnum = info->return_reg;
1166
1167 /* For all registers known to be saved in the current frame,
1168 do the obvious and pull the value out. */
1169 if (info->saved_regs[regnum])
c906108c 1170 {
d2427a71
RH
1171 *optimizedp = 0;
1172 *lvalp = lval_memory;
1173 *addrp = info->saved_regs[regnum];
1174 *realnump = -1;
1175 if (bufferp != NULL)
1176 read_memory (*addrp, bufferp, ALPHA_REGISTER_SIZE);
c906108c
SS
1177 return;
1178 }
1179
d2427a71
RH
1180 /* The stack pointer of the previous frame is computed by popping
1181 the current stack frame. */
1182 if (regnum == ALPHA_SP_REGNUM)
c906108c 1183 {
d2427a71
RH
1184 *optimizedp = 0;
1185 *lvalp = not_lval;
1186 *addrp = 0;
1187 *realnump = -1;
1188 if (bufferp != NULL)
1189 store_unsigned_integer (bufferp, ALPHA_REGISTER_SIZE, info->vfp);
1190 return;
c906108c 1191 }
95b80706 1192
d2427a71
RH
1193 /* Otherwise assume the next frame has the same register value. */
1194 frame_register (next_frame, regnum, optimizedp, lvalp, addrp,
1195 realnump, bufferp);
95b80706
JT
1196}
1197
d2427a71
RH
1198static const struct frame_unwind alpha_heuristic_frame_unwind = {
1199 NORMAL_FRAME,
1200 alpha_heuristic_frame_this_id,
1201 alpha_heuristic_frame_prev_register
1202};
c906108c 1203
d2427a71
RH
1204static const struct frame_unwind *
1205alpha_heuristic_frame_p (CORE_ADDR pc)
c906108c 1206{
d2427a71 1207 return &alpha_heuristic_frame_unwind;
c906108c
SS
1208}
1209
fbe586ae 1210static CORE_ADDR
d2427a71
RH
1211alpha_heuristic_frame_base_address (struct frame_info *next_frame,
1212 void **this_prologue_cache)
c906108c 1213{
d2427a71
RH
1214 struct alpha_heuristic_unwind_cache *info
1215 = alpha_heuristic_frame_unwind_cache (next_frame, this_prologue_cache, 0);
c906108c 1216
d2427a71 1217 return info->vfp;
c906108c
SS
1218}
1219
d2427a71
RH
1220static const struct frame_base alpha_heuristic_frame_base = {
1221 &alpha_heuristic_frame_unwind,
1222 alpha_heuristic_frame_base_address,
1223 alpha_heuristic_frame_base_address,
1224 alpha_heuristic_frame_base_address
1225};
1226
c906108c 1227/* Just like reinit_frame_cache, but with the right arguments to be
d2427a71 1228 callable as an sfunc. Used by the "set heuristic-fence-post" command. */
c906108c
SS
1229
1230static void
fba45db2 1231reinit_frame_cache_sfunc (char *args, int from_tty, struct cmd_list_element *c)
c906108c
SS
1232{
1233 reinit_frame_cache ();
1234}
1235
d2427a71
RH
1236\f
1237/* ALPHA stack frames are almost impenetrable. When execution stops,
1238 we basically have to look at symbol information for the function
1239 that we stopped in, which tells us *which* register (if any) is
1240 the base of the frame pointer, and what offset from that register
1241 the frame itself is at.
c906108c 1242
d2427a71
RH
1243 This presents a problem when trying to examine a stack in memory
1244 (that isn't executing at the moment), using the "frame" command. We
1245 don't have a PC, nor do we have any registers except SP.
c906108c 1246
d2427a71
RH
1247 This routine takes two arguments, SP and PC, and tries to make the
1248 cached frames look as if these two arguments defined a frame on the
1249 cache. This allows the rest of info frame to extract the important
1250 arguments without difficulty. */
ec32e4be 1251
d2427a71
RH
1252struct frame_info *
1253alpha_setup_arbitrary_frame (int argc, CORE_ADDR *argv)
0d056799 1254{
d2427a71
RH
1255 if (argc != 2)
1256 error ("ALPHA frame specifications require two arguments: sp and pc");
0d056799 1257
d2427a71 1258 return create_new_frame (argv[0], argv[1]);
0d056799
JT
1259}
1260
d2427a71
RH
1261/* Assuming NEXT_FRAME->prev is a dummy, return the frame ID of that
1262 dummy frame. The frame ID's base needs to match the TOS value
1263 saved by save_dummy_frame_tos(), and the PC match the dummy frame's
1264 breakpoint. */
d734c450 1265
d2427a71
RH
1266static struct frame_id
1267alpha_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
0d056799 1268{
d2427a71
RH
1269 ULONGEST base;
1270 frame_unwind_unsigned_register (next_frame, ALPHA_SP_REGNUM, &base);
1271 return frame_id_build (base, frame_pc_unwind (next_frame));
0d056799
JT
1272}
1273
dc129d82 1274static CORE_ADDR
d2427a71 1275alpha_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
accc6d1f 1276{
d2427a71
RH
1277 ULONGEST pc;
1278 frame_unwind_unsigned_register (next_frame, ALPHA_PC_REGNUM, &pc);
1279 return pc;
accc6d1f
JT
1280}
1281
98a8e1e5
RH
1282\f
1283/* Helper routines for alpha*-nat.c files to move register sets to and
1284 from core files. The UNIQUE pointer is allowed to be NULL, as most
1285 targets don't supply this value in their core files. */
1286
1287void
1288alpha_supply_int_regs (int regno, const void *r0_r30,
1289 const void *pc, const void *unique)
1290{
1291 int i;
1292
1293 for (i = 0; i < 31; ++i)
1294 if (regno == i || regno == -1)
1295 supply_register (i, (const char *)r0_r30 + i*8);
1296
1297 if (regno == ALPHA_ZERO_REGNUM || regno == -1)
1298 supply_register (ALPHA_ZERO_REGNUM, NULL);
1299
1300 if (regno == ALPHA_PC_REGNUM || regno == -1)
1301 supply_register (ALPHA_PC_REGNUM, pc);
1302
1303 if (regno == ALPHA_UNIQUE_REGNUM || regno == -1)
1304 supply_register (ALPHA_UNIQUE_REGNUM, unique);
1305}
1306
1307void
1308alpha_fill_int_regs (int regno, void *r0_r30, void *pc, void *unique)
1309{
1310 int i;
1311
1312 for (i = 0; i < 31; ++i)
1313 if (regno == i || regno == -1)
1314 regcache_collect (i, (char *)r0_r30 + i*8);
1315
1316 if (regno == ALPHA_PC_REGNUM || regno == -1)
1317 regcache_collect (ALPHA_PC_REGNUM, pc);
1318
1319 if (unique && (regno == ALPHA_UNIQUE_REGNUM || regno == -1))
1320 regcache_collect (ALPHA_UNIQUE_REGNUM, unique);
1321}
1322
1323void
1324alpha_supply_fp_regs (int regno, const void *f0_f30, const void *fpcr)
1325{
1326 int i;
1327
1328 for (i = ALPHA_FP0_REGNUM; i < ALPHA_FP0_REGNUM + 31; ++i)
1329 if (regno == i || regno == -1)
1330 supply_register (i, (const char *)f0_f30 + (i - ALPHA_FP0_REGNUM) * 8);
1331
1332 if (regno == ALPHA_FPCR_REGNUM || regno == -1)
1333 supply_register (ALPHA_FPCR_REGNUM, fpcr);
1334}
1335
1336void
1337alpha_fill_fp_regs (int regno, void *f0_f30, void *fpcr)
1338{
1339 int i;
1340
1341 for (i = ALPHA_FP0_REGNUM; i < ALPHA_FP0_REGNUM + 31; ++i)
1342 if (regno == i || regno == -1)
1343 regcache_collect (i, (char *)f0_f30 + (i - ALPHA_FP0_REGNUM) * 8);
1344
1345 if (regno == ALPHA_FPCR_REGNUM || regno == -1)
1346 regcache_collect (ALPHA_FPCR_REGNUM, fpcr);
1347}
1348
d2427a71 1349\f
ec32e4be
JT
1350/* alpha_software_single_step() is called just before we want to resume
1351 the inferior, if we want to single-step it but there is no hardware
1352 or kernel single-step support (NetBSD on Alpha, for example). We find
1353 the target of the coming instruction and breakpoint it.
1354
1355 single_step is also called just after the inferior stops. If we had
1356 set up a simulated single-step, we undo our damage. */
1357
1358static CORE_ADDR
1359alpha_next_pc (CORE_ADDR pc)
1360{
1361 unsigned int insn;
1362 unsigned int op;
1363 int offset;
1364 LONGEST rav;
1365
1366 insn = read_memory_unsigned_integer (pc, sizeof (insn));
1367
1368 /* Opcode is top 6 bits. */
1369 op = (insn >> 26) & 0x3f;
1370
1371 if (op == 0x1a)
1372 {
1373 /* Jump format: target PC is:
1374 RB & ~3 */
1375 return (read_register ((insn >> 16) & 0x1f) & ~3);
1376 }
1377
1378 if ((op & 0x30) == 0x30)
1379 {
1380 /* Branch format: target PC is:
1381 (new PC) + (4 * sext(displacement)) */
1382 if (op == 0x30 || /* BR */
1383 op == 0x34) /* BSR */
1384 {
1385 branch_taken:
1386 offset = (insn & 0x001fffff);
1387 if (offset & 0x00100000)
1388 offset |= 0xffe00000;
1389 offset *= 4;
1390 return (pc + 4 + offset);
1391 }
1392
1393 /* Need to determine if branch is taken; read RA. */
1394 rav = (LONGEST) read_register ((insn >> 21) & 0x1f);
1395 switch (op)
1396 {
1397 case 0x38: /* BLBC */
1398 if ((rav & 1) == 0)
1399 goto branch_taken;
1400 break;
1401 case 0x3c: /* BLBS */
1402 if (rav & 1)
1403 goto branch_taken;
1404 break;
1405 case 0x39: /* BEQ */
1406 if (rav == 0)
1407 goto branch_taken;
1408 break;
1409 case 0x3d: /* BNE */
1410 if (rav != 0)
1411 goto branch_taken;
1412 break;
1413 case 0x3a: /* BLT */
1414 if (rav < 0)
1415 goto branch_taken;
1416 break;
1417 case 0x3b: /* BLE */
1418 if (rav <= 0)
1419 goto branch_taken;
1420 break;
1421 case 0x3f: /* BGT */
1422 if (rav > 0)
1423 goto branch_taken;
1424 break;
1425 case 0x3e: /* BGE */
1426 if (rav >= 0)
1427 goto branch_taken;
1428 break;
d2427a71
RH
1429
1430 /* ??? Missing floating-point branches. */
ec32e4be
JT
1431 }
1432 }
1433
1434 /* Not a branch or branch not taken; target PC is:
1435 pc + 4 */
1436 return (pc + 4);
1437}
1438
1439void
1440alpha_software_single_step (enum target_signal sig, int insert_breakpoints_p)
1441{
1442 static CORE_ADDR next_pc;
1443 typedef char binsn_quantum[BREAKPOINT_MAX];
1444 static binsn_quantum break_mem;
1445 CORE_ADDR pc;
1446
1447 if (insert_breakpoints_p)
1448 {
1449 pc = read_pc ();
1450 next_pc = alpha_next_pc (pc);
1451
1452 target_insert_breakpoint (next_pc, break_mem);
1453 }
1454 else
1455 {
1456 target_remove_breakpoint (next_pc, break_mem);
1457 write_pc (next_pc);
1458 }
c906108c
SS
1459}
1460
dc129d82 1461\f
dc129d82
JT
1462/* Initialize the current architecture based on INFO. If possible, re-use an
1463 architecture from ARCHES, which is a list of architectures already created
1464 during this debugging session.
1465
1466 Called e.g. at program startup, when reading a core file, and when reading
1467 a binary file. */
1468
1469static struct gdbarch *
1470alpha_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1471{
1472 struct gdbarch_tdep *tdep;
1473 struct gdbarch *gdbarch;
dc129d82
JT
1474
1475 /* Try to determine the ABI of the object we are loading. */
4be87837 1476 if (info.abfd != NULL && info.osabi == GDB_OSABI_UNKNOWN)
dc129d82 1477 {
4be87837
DJ
1478 /* If it's an ECOFF file, assume it's OSF/1. */
1479 if (bfd_get_flavour (info.abfd) == bfd_target_ecoff_flavour)
aff87235 1480 info.osabi = GDB_OSABI_OSF1;
dc129d82
JT
1481 }
1482
1483 /* Find a candidate among extant architectures. */
4be87837
DJ
1484 arches = gdbarch_list_lookup_by_info (arches, &info);
1485 if (arches != NULL)
1486 return arches->gdbarch;
dc129d82
JT
1487
1488 tdep = xmalloc (sizeof (struct gdbarch_tdep));
1489 gdbarch = gdbarch_alloc (&info, tdep);
1490
d2427a71
RH
1491 /* Lowest text address. This is used by heuristic_proc_start()
1492 to decide when to stop looking. */
d9b023cc
JT
1493 tdep->vm_min_address = (CORE_ADDR) 0x120000000;
1494
36a6271d 1495 tdep->dynamic_sigtramp_offset = NULL;
5868c862 1496 tdep->sigcontext_addr = NULL;
36a6271d 1497
accc6d1f
JT
1498 tdep->jb_pc = -1; /* longjmp support not enabled by default */
1499
dc129d82
JT
1500 /* Type sizes */
1501 set_gdbarch_short_bit (gdbarch, 16);
1502 set_gdbarch_int_bit (gdbarch, 32);
1503 set_gdbarch_long_bit (gdbarch, 64);
1504 set_gdbarch_long_long_bit (gdbarch, 64);
1505 set_gdbarch_float_bit (gdbarch, 32);
1506 set_gdbarch_double_bit (gdbarch, 64);
1507 set_gdbarch_long_double_bit (gdbarch, 64);
1508 set_gdbarch_ptr_bit (gdbarch, 64);
1509
1510 /* Register info */
1511 set_gdbarch_num_regs (gdbarch, ALPHA_NUM_REGS);
1512 set_gdbarch_sp_regnum (gdbarch, ALPHA_SP_REGNUM);
dc129d82
JT
1513 set_gdbarch_pc_regnum (gdbarch, ALPHA_PC_REGNUM);
1514 set_gdbarch_fp0_regnum (gdbarch, ALPHA_FP0_REGNUM);
1515
1516 set_gdbarch_register_name (gdbarch, alpha_register_name);
dc129d82
JT
1517 set_gdbarch_register_byte (gdbarch, alpha_register_byte);
1518 set_gdbarch_register_raw_size (gdbarch, alpha_register_raw_size);
dc129d82 1519 set_gdbarch_register_virtual_size (gdbarch, alpha_register_virtual_size);
c483c494 1520 set_gdbarch_register_type (gdbarch, alpha_register_type);
dc129d82
JT
1521
1522 set_gdbarch_cannot_fetch_register (gdbarch, alpha_cannot_fetch_register);
1523 set_gdbarch_cannot_store_register (gdbarch, alpha_cannot_store_register);
1524
c483c494
RH
1525 set_gdbarch_convert_register_p (gdbarch, alpha_convert_register_p);
1526 set_gdbarch_register_to_value (gdbarch, alpha_register_to_value);
1527 set_gdbarch_value_to_register (gdbarch, alpha_value_to_register);
dc129d82 1528
615967cb
RH
1529 set_gdbarch_register_reggroup_p (gdbarch, alpha_register_reggroup_p);
1530
d2427a71 1531 /* Prologue heuristics. */
dc129d82
JT
1532 set_gdbarch_skip_prologue (gdbarch, alpha_skip_prologue);
1533
5ef165c2
RH
1534 /* Disassembler. */
1535 set_gdbarch_print_insn (gdbarch, print_insn_alpha);
1536
d2427a71 1537 /* Call info. */
dc129d82
JT
1538 set_gdbarch_frame_num_args (gdbarch, frame_num_args_unknown);
1539 set_gdbarch_frameless_function_invocation (gdbarch,
1540 generic_frameless_function_invocation_not);
1541
dc129d82 1542 set_gdbarch_use_struct_convention (gdbarch, alpha_use_struct_convention);
5ec2bb99
RH
1543 set_gdbarch_extract_return_value (gdbarch, alpha_extract_return_value);
1544 set_gdbarch_store_return_value (gdbarch, alpha_store_return_value);
1545 set_gdbarch_extract_struct_value_address (gdbarch,
dc129d82
JT
1546 alpha_extract_struct_value_address);
1547
1548 /* Settings for calling functions in the inferior. */
c88e30c0 1549 set_gdbarch_push_dummy_call (gdbarch, alpha_push_dummy_call);
d2427a71
RH
1550
1551 /* Methods for saving / extracting a dummy frame's ID. */
1552 set_gdbarch_unwind_dummy_id (gdbarch, alpha_unwind_dummy_id);
1553 set_gdbarch_save_dummy_frame_tos (gdbarch, generic_save_dummy_frame_tos);
1554
1555 /* Return the unwound PC value. */
1556 set_gdbarch_unwind_pc (gdbarch, alpha_unwind_pc);
dc129d82
JT
1557
1558 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
36a6271d 1559 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
dc129d82 1560
95b80706 1561 set_gdbarch_breakpoint_from_pc (gdbarch, alpha_breakpoint_from_pc);
dc129d82 1562 set_gdbarch_decr_pc_after_break (gdbarch, 4);
95b80706
JT
1563
1564 set_gdbarch_function_start_offset (gdbarch, 0);
dc129d82
JT
1565 set_gdbarch_frame_args_skip (gdbarch, 0);
1566
44dffaac 1567 /* Hook in ABI-specific overrides, if they have been registered. */
4be87837 1568 gdbarch_init_osabi (info, gdbarch);
44dffaac 1569
accc6d1f
JT
1570 /* Now that we have tuned the configuration, set a few final things
1571 based on what the OS ABI has told us. */
1572
1573 if (tdep->jb_pc >= 0)
1574 set_gdbarch_get_longjmp_target (gdbarch, alpha_get_longjmp_target);
1575
d2427a71
RH
1576 frame_unwind_append_predicate (gdbarch, alpha_sigtramp_frame_p);
1577 frame_unwind_append_predicate (gdbarch, alpha_heuristic_frame_p);
dc129d82 1578
d2427a71 1579 frame_base_set_default (gdbarch, &alpha_heuristic_frame_base);
accc6d1f 1580
d2427a71 1581 return gdbarch;
dc129d82
JT
1582}
1583
c906108c 1584void
fba45db2 1585_initialize_alpha_tdep (void)
c906108c
SS
1586{
1587 struct cmd_list_element *c;
1588
d2427a71 1589 gdbarch_register (bfd_arch_alpha, alpha_gdbarch_init, NULL);
c906108c
SS
1590
1591 /* Let the user set the fence post for heuristic_proc_start. */
1592
1593 /* We really would like to have both "0" and "unlimited" work, but
1594 command.c doesn't deal with that. So make it a var_zinteger
1595 because the user can always use "999999" or some such for unlimited. */
1596 c = add_set_cmd ("heuristic-fence-post", class_support, var_zinteger,
1597 (char *) &heuristic_fence_post,
1598 "\
1599Set the distance searched for the start of a function.\n\
1600If you are debugging a stripped executable, GDB needs to search through the\n\
1601program for the start of a function. This command sets the distance of the\n\
1602search. The only need to set it is when debugging a stripped executable.",
1603 &setlist);
1604 /* We need to throw away the frame cache when we set this, since it
1605 might change our ability to get backtraces. */
9f60d481 1606 set_cmd_sfunc (c, reinit_frame_cache_sfunc);
c906108c
SS
1607 add_show_from_set (c, &showlist);
1608}
This page took 0.464082 seconds and 4 git commands to generate.