OSDN Git Service

* 41intnam.ads, 42intnam.ads, 4aintnam.ads, 4cintnam.ads,
[pf3gnuchains/gcc-fork.git] / gcc / ada / tracebak.c
1 /****************************************************************************
2  *                                                                          *
3  *                         GNAT COMPILER COMPONENTS                         *
4  *                                                                          *
5  *                            T R A C E B A C K                             *
6  *                                                                          *
7  *                          C Implementation File                           *
8  *                                                                          *
9  *                            $Revision$
10  *                                                                          *
11  *           Copyright (C) 2000-2001 Ada Core Technologies, Inc.            *
12  *                                                                          *
13  * GNAT is free software;  you can  redistribute it  and/or modify it under *
14  * terms of the  GNU General Public License as published  by the Free Soft- *
15  * ware  Foundation;  either version 2,  or (at your option) any later ver- *
16  * sion.  GNAT is distributed in the hope that it will be useful, but WITH- *
17  * OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY *
18  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License *
19  * for  more details.  You should have  received  a copy of the GNU General *
20  * Public License  distributed with GNAT;  see file COPYING.  If not, write *
21  * to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, *
22  * MA 02111-1307, USA.                                                      *
23  *                                                                          *
24  * As a  special  exception,  if you  link  this file  with other  files to *
25  * produce an executable,  this file does not by itself cause the resulting *
26  * executable to be covered by the GNU General Public License. This except- *
27  * ion does not  however invalidate  any other reasons  why the  executable *
28  * file might be covered by the  GNU Public License.                        *
29  *                                                                          *
30  * GNAT was originally developed  by the GNAT team at  New York University. *
31  * It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). *
32  *                                                                          *
33  ****************************************************************************/
34
35 /* This file contains low level support for stack unwinding using GCC intrinsic
36    functions.
37    It has been tested on the following configurations:
38    HPPA/HP-UX
39    PowerPC/AiX
40    PowerPC/VxWorks
41    Sparc/Solaris
42    i386/GNU/Linux
43    i386/Solaris
44    i386/NT
45    i386/OS2
46    i386/LynxOS
47    Alpha/VxWorks
48 */
49
50 #ifdef __alpha_vxworks
51 #include "vxWorks.h"
52 #endif
53
54 #ifdef IN_RTS
55 #define POSIX
56 #include "tconfig.h"
57 #include "tsystem.h"
58 #else
59 #include "config.h"
60 #include "system.h"
61 #endif
62
63 #define Lock_Task system__soft_links__lock_task
64 extern void (*Lock_Task) PARAMS ((void));
65
66 #define Unlock_Task system__soft_links__unlock_task
67 extern void (*Unlock_Task) PARAMS ((void));
68
69 #ifndef CURRENT_STACK_FRAME
70 # define CURRENT_STACK_FRAME  ({ char __csf; &__csf; })
71 #endif
72
73 extern int __gnat_backtrace     PARAMS ((void **, int, void *, void *));
74
75 #if defined (__hppa)
76 struct layout
77 {
78   void *return_address;
79   void *pad[4];
80   struct layout *next;
81 };
82
83 #define FRAME_LEVEL 1
84 #define FRAME_OFFSET -20
85 #define SKIP_FRAME 1
86 #define PC_ADJUST -4
87
88 /* If CURRENT is unaligned, it means that CURRENT is not a valid frame
89    pointer and we should stop popping frames. */
90
91 #define STOP_FRAME(CURRENT, TOP_STACK) \
92   (((int) (CURRENT) & 0x3) != 0 && (CURRENT)->return_address == 0)
93
94 /* Current implementation need to be protected against invalid memory
95    accesses */
96 #define PROTECT_SEGV
97
98 #elif defined (_AIX)
99 struct layout
100 {
101   struct layout *next;
102   void *pad;
103   void *return_address;
104 };
105
106 #define FRAME_LEVEL 1
107 #define FRAME_OFFSET 0
108 #define SKIP_FRAME 2
109 #define PC_ADJUST -4
110 #define STOP_FRAME(CURRENT, TOP_STACK) ((void *) (CURRENT) < (TOP_STACK))
111
112 #elif defined (_ARCH_PPC) && defined (__vxworks)
113 struct layout
114 {
115   struct layout *next;
116   void *return_address;
117 };
118
119 #define FRAME_LEVEL 1
120 #define FRAME_OFFSET 0
121 #define SKIP_FRAME 2
122 #define PC_ADJUST 0
123 #define STOP_FRAME(CURRENT, TOP_STACK) ((CURRENT)->return_address == 0)
124
125 #elif defined (sun) && defined (sparc)
126 struct layout
127 {
128   struct layout *next;
129   void *return_address;
130 };
131
132 #define FRAME_LEVEL 1
133 #define FRAME_OFFSET (14*4)
134 #define SKIP_FRAME 1
135 #define PC_ADJUST 0
136 #define STOP_FRAME(CURRENT, TOP_STACK) \
137   ((CURRENT)->return_address == 0|| (CURRENT)->next == 0 \
138    || (void *) (CURRENT) < (TOP_STACK))
139
140 #elif defined (i386)
141 struct layout
142 {
143   struct layout *next;
144   void *return_address;
145 };
146
147 #ifdef _WIN32
148 /* _image_base__ is the image starting address, no stack addresses should be
149    under this value */
150 extern unsigned int _image_base__;
151 #define LOWEST_ADDR ((unsigned int) (&_image_base__))
152 #else
153 #define LOWEST_ADDR 0
154 #endif
155
156 #define FRAME_LEVEL 0
157 #define FRAME_OFFSET 0
158 #define SKIP_FRAME 1
159 #define PC_ADJUST -2
160 #define STOP_FRAME(CURRENT, TOP_STACK) \
161   ((unsigned int)(CURRENT)->return_address < LOWEST_ADDR \
162    || (CURRENT)->return_address == 0|| (CURRENT)->next == 0  \
163    || (void *) (CURRENT) < (TOP_STACK))
164
165 /* On i386 architecture we check that at the call point we really have a call
166    insn. Possible call instructions are:
167
168    call  addr16        E8 xx xx xx xx
169    call  reg           FF Dx
170    call  off(reg)      FF xx xx
171    lcall addr seg      9A xx xx xx xx xx xx
172
173    This check will not catch all cases but it will increase the backtrace
174    reliability on this architecture.
175 */
176
177 #define VALID_STACK_FRAME(ptr) \
178    (((*((ptr) - 3) & 0xff) == 0xe8) \
179     || ((*((ptr) - 4) & 0xff) == 0x9a) \
180     || ((*((ptr) - 2) & 0xff) == 0xff) \
181     || (((*((ptr) - 1) & 0xff00) == 0xff00) \
182         && ((*((ptr) - 1) & 0xf0) == 0xd0)))
183
184 #elif defined (__alpha_vxworks)
185
186 #define SKIP_FRAME 1
187 #define PC_ADJUST -4
188
189 extern void kerTaskEntry();
190
191 #define STOP_FRAME \
192    (current == NULL \
193     || ((CORE_ADDR) &kerTaskEntry >= PROC_LOW_ADDR (current->proc_desc) \
194         && current->pc >= (CORE_ADDR) &kerTaskEntry))
195 #endif
196
197 #if !defined (PC_ADJUST)
198 int
199 __gnat_backtrace (array, size, exclude_min, exclude_max)
200      void **array ATTRIBUTE_UNUSED;
201      int size ATTRIBUTE_UNUSED;
202      void *exclude_min ATTRIBUTE_UNUSED;
203      void *exclude_max ATTRIBUTE_UNUSED;
204 {
205   return 0;
206 }
207
208 #elif !defined (__alpha_vxworks)
209
210 #ifdef PROTECT_SEGV
211 #include <setjmp.h>
212 #include <signal.h>
213
214 static jmp_buf sigsegv_excp;
215
216 static void
217 segv_handler (ignored)
218      int ignored;
219 {
220   longjmp (sigsegv_excp, 1);
221 }
222 #endif
223
224 #ifndef VALID_STACK_FRAME
225 #define VALID_STACK_FRAME(ptr) 1
226 #endif
227
228 int
229 __gnat_backtrace (array, size, exclude_min, exclude_max)
230      void **array;
231      int size;
232      void *exclude_min;
233      void *exclude_max;
234 {
235   struct layout *current;
236   void *top_frame;
237   void *top_stack;
238   int cnt = 0;
239
240 #ifdef PROTECT_SEGV
241   struct sigaction this_act, old_act;
242
243   /* This function is not thread safe if PROTECT_SEGV is defined, so
244      protect it */
245   (*Lock_Task) ();
246 #endif
247
248   top_frame = __builtin_frame_address (FRAME_LEVEL);
249   top_stack = CURRENT_STACK_FRAME;
250   current = (struct layout *) ((size_t) top_frame + FRAME_OFFSET);
251
252 #ifdef PROTECT_SEGV
253   this_act.sa_handler = segv_handler;
254   sigemptyset (&this_act.sa_mask);
255   this_act.sa_flags = 0;
256   sigaction (SIGSEGV, &this_act, &old_act);
257
258   if (setjmp (sigsegv_excp))
259     goto Done;
260 #endif
261
262   /* We skip the call to this function, it makes no sense to record it.  */
263   while (cnt < SKIP_FRAME)
264     {
265       current = (struct layout *) ((size_t) current->next + FRAME_OFFSET);
266       cnt++;
267     }
268
269   cnt = 0;
270   while (cnt < size)
271     {
272       if (STOP_FRAME (current, top_stack) || 
273           !VALID_STACK_FRAME((char *)(current->return_address + PC_ADJUST)))
274         break;
275
276       if (current->return_address < exclude_min
277           || current->return_address > exclude_max)
278         array[cnt++] = current->return_address + PC_ADJUST;
279
280       current = (struct layout *) ((size_t) current->next + FRAME_OFFSET);
281     }
282
283 #ifdef PROTECT_SEGV
284  Done:
285   sigaction (SIGSEGV, &old_act, NULL);
286   (*Unlock_Task) ();
287 #endif
288   return cnt;
289 }
290
291 #else
292 /* Alpha vxWorks requires a special, complex treatment that is extracted
293    from GDB */
294
295 #include <string.h>
296
297 /* Register numbers of various important registers.
298    Note that most of these values are "real" register numbers,
299    and correspond to the general registers of the machine,
300    and FP_REGNUM is a "phony" register number which is too large
301    to be an actual register number as far as the user is concerned
302    but serves to get the desired value when passed to read_register.  */
303
304 #define T7_REGNUM 8             /* Return address register for OSF/1 __add* */
305 #define GCC_FP_REGNUM 15        /* Used by gcc as frame register */
306 #define T9_REGNUM 23            /* Return address register for OSF/1 __div* */
307 #define SP_REGNUM 30            /* Contains address of top of stack */
308 #define RA_REGNUM 26            /* Contains return address value */
309 #define FP0_REGNUM 32           /* Floating point register 0 */
310 #define PC_REGNUM 64            /* Contains program counter */
311 #define NUM_REGS 66
312
313 #define VM_MIN_ADDRESS (CORE_ADDR)0x120000000
314
315 #define SIZEOF_FRAME_SAVED_REGS (sizeof (CORE_ADDR) * (NUM_REGS))
316 #define INIT_EXTRA_FRAME_INFO(fromleaf, fci) init_extra_frame_info(fci)
317
318 #define FRAME_CHAIN(thisframe) (CORE_ADDR) alpha_frame_chain (thisframe)
319
320 #define FRAME_CHAIN_VALID(CHAIN, THISFRAME)     \
321   ((CHAIN) != 0                                 \
322    && !inside_entry_file (FRAME_SAVED_PC (THISFRAME)))
323
324 #define FRAME_SAVED_PC(FRAME)   (alpha_frame_saved_pc (FRAME))
325
326 #define FRAME_CHAIN_COMBINE(CHAIN, THISFRAME) (CHAIN)
327
328 #define INIT_FRAME_PC(FROMLEAF, PREV)
329
330 #define INIT_FRAME_PC_FIRST(FROMLEAF, PREV) \
331   (PREV)->pc = ((FROMLEAF) ? SAVED_PC_AFTER_CALL ((PREV)->next) \
332                 : (PREV)->next ? FRAME_SAVED_PC ((prev)->NEXT) : read_pc ());
333
334 #define SAVED_PC_AFTER_CALL(FRAME)      alpha_saved_pc_after_call (FRAME)
335
336 typedef unsigned long long int bfd_vma;
337
338 typedef bfd_vma CORE_ADDR;
339
340 typedef struct pdr
341 {
342   bfd_vma adr;          /* memory address of start of procedure */
343   long  isym;           /* start of local symbol entries */
344   long  iline;          /* start of line number entries*/
345   long  regmask;        /* save register mask */
346   long  regoffset;      /* save register offset */
347   long  iopt;           /* start of optimization symbol entries*/
348   long  fregmask;       /* save floating point register mask */
349   long  fregoffset;     /* save floating point register offset */
350   long  frameoffset;    /* frame size */
351   short framereg;       /* frame pointer register */
352   short pcreg;          /* offset or reg of return pc */
353   long  lnLow;          /* lowest line in the procedure */
354   long  lnHigh;         /* highest line in the procedure */
355   bfd_vma cbLineOffset; /* byte offset for this procedure from the fd base */
356   /* These fields are new for 64 bit ECOFF.  */
357   unsigned gp_prologue : 8; /* byte size of GP prologue */
358   unsigned gp_used : 1; /* true if the procedure uses GP */
359   unsigned reg_frame : 1; /* true if register frame procedure */
360   unsigned prof : 1;    /* true if compiled with -pg */
361   unsigned reserved : 13; /* reserved: must be zero */
362   unsigned localoff : 8; /* offset of local variables from vfp */
363 } PDR;
364
365 typedef struct alpha_extra_func_info
366 {
367   long numargs;         /* number of args to procedure (was iopt) */
368   PDR pdr;                      /* Procedure descriptor record */
369 }
370 *alpha_extra_func_info_t;
371
372 struct frame_info
373 {
374   /* Nominal address of the frame described.  See comments at FRAME_FP
375      about what this means outside the *FRAME* macros; in the *FRAME*
376      macros, it can mean whatever makes most sense for this machine.  */
377   CORE_ADDR frame;
378
379   /* Address at which execution is occurring in this frame.  For the
380      innermost frame, it's the current pc.  For other frames, it is a
381      pc saved in the next frame.  */
382   CORE_ADDR pc;
383
384   /* For each register, address of where it was saved on entry to the
385      frame, or zero if it was not saved on entry to this frame.  This
386      includes special registers such as pc and fp saved in special
387      ways in the stack frame.  The SP_REGNUM is even more special, the
388      address here is the sp for the next frame, not the address where
389      the sp was saved.  Allocated by frame_saved_regs_zalloc () which
390      is called and initialized by FRAME_INIT_SAVED_REGS. */
391   CORE_ADDR *saved_regs;        /*NUM_REGS */
392
393   int localoff;
394   int pc_reg;
395   alpha_extra_func_info_t proc_desc;
396
397   /* Pointers to the next and previous frame_info's in the frame cache.  */
398   struct frame_info *next, *prev;
399 };
400
401 struct frame_saved_regs
402 {
403   /* For each register R (except the SP), regs[R] is the address at
404      which it was saved on entry to the frame, or zero if it was not
405      saved on entry to this frame.  This includes special registers
406      such as pc and fp saved in special ways in the stack frame.
407
408      regs[SP_REGNUM] is different.  It holds the actual SP, not the
409      address at which it was saved.  */
410
411   CORE_ADDR regs[NUM_REGS];
412 };
413
414 static CORE_ADDR theRegisters[32];
415
416 /* Prototypes for local functions. */
417
418 static CORE_ADDR read_next_frame_reg PARAMS ((struct frame_info *, int));
419 static CORE_ADDR heuristic_proc_start PARAMS ((CORE_ADDR));
420 static int alpha_about_to_return PARAMS ((CORE_ADDR pc));
421 static void init_extra_frame_info PARAMS ((struct frame_info *));
422 static CORE_ADDR alpha_frame_chain PARAMS ((struct frame_info *));
423 static CORE_ADDR alpha_frame_saved_pc PARAMS ((struct frame_info *frame))
424 static void *trace_alloc PARAMS ((unsigned int));
425 static struct frame_info *create_new_frame PARAMS ((CORE_ADDR, CORE_ADDR));
426
427 static alpha_extra_func_info_t
428 heuristic_proc_desc PARAMS ((CORE_ADDR, CORE_ADDR, struct frame_info *,
429                              struct frame_saved_regs *));
430
431 static alpha_extra_func_info_t
432 find_proc_desc PARAMS ((CORE_ADDR, struct frame_info *,
433                         struct frame_saved_regs *));
434
435 /* Heuristic_proc_start may hunt through the text section for a long
436    time across a 2400 baud serial line.  Allows the user to limit this
437    search.  */
438 static unsigned int heuristic_fence_post = 1<<16;
439
440 /* Layout of a stack frame on the alpha:
441
442                 |                               |
443  pdr members:   |  7th ... nth arg,             |
444                 |  `pushed' by caller.          |
445                 |                               |
446 ----------------|-------------------------------|<--  old_sp == vfp
447    ^  ^  ^  ^   |                               |
448    |  |  |  |   |                               |
449    |  |localoff |  Copies of 1st .. 6th         |
450    |  |  |  |   |  argument if necessary.       |
451    |  |  |  v   |                               |
452    |  |  |  --- |-------------------------------|<-- FRAME_LOCALS_ADDRESS
453    |  |  |      |                               |
454    |  |  |      |  Locals and temporaries.      |
455    |  |  |      |                               |
456    |  |  |      |-------------------------------|
457    |  |  |      |                               |
458    |-fregoffset |  Saved float registers.       |
459    |  |  |      |  F9                           |
460    |  |  |      |   .                           |
461    |  |  |      |   .                           |
462    |  |  |      |  F2                           |
463    |  |  v      |                               |
464    |  |  -------|-------------------------------|
465    |  |         |                               |
466    |  |         |  Saved registers.             |
467    |  |         |  S6                           |
468    |-regoffset  |   .                           |
469    |  |         |   .                           |
470    |  |         |  S0                           |
471    |  |         |  pdr.pcreg                    |
472    |  v         |                               |
473    |  ----------|-------------------------------|
474    |            |                               |
475  frameoffset    |  Argument build area, gets    |
476    |            |  7th ... nth arg for any      |
477    |            |  called procedure.            |
478    v            |                               |
479    -------------|-------------------------------|<-- sp
480                 |                               |            */
481
482 #define PROC_LOW_ADDR(PROC) ((PROC)->pdr.adr)               /* least address */
483 #define PROC_HIGH_ADDR(PROC) ((PROC)->pdr.iline)      /* upper address bound */
484 #define PROC_DUMMY_FRAME(PROC) ((PROC)->pdr.cbLineOffset) /*CALL_DUMMY frame */
485 #define PROC_FRAME_OFFSET(PROC) ((PROC)->pdr.frameoffset)
486 #define PROC_FRAME_REG(PROC) ((PROC)->pdr.framereg)
487 #define PROC_REG_MASK(PROC) ((PROC)->pdr.regmask)
488 #define PROC_FREG_MASK(PROC) ((PROC)->pdr.fregmask)
489 #define PROC_REG_OFFSET(PROC) ((PROC)->pdr.regoffset)
490 #define PROC_FREG_OFFSET(PROC) ((PROC)->pdr.fregoffset)
491 #define PROC_PC_REG(PROC) ((PROC)->pdr.pcreg)
492 #define PROC_LOCALOFF(PROC) ((PROC)->pdr.localoff)
493
494 /* Local storage allocation/deallocation functions.  trace_alloc does
495    a malloc, but also chains allocated blocks on trace_alloc_chain, so
496    they may all be freed on exit from __gnat_backtrace. */
497
498 struct alloc_chain
499 {
500   struct alloc_chain *next;
501   double x[0];
502 };
503 struct alloc_chain *trace_alloc_chain;
504
505 static void * 
506 trace_alloc (n)
507      unsigned int n;
508 {
509   struct alloc_chain * result = malloc (n + sizeof(struct alloc_chain));
510
511   result->next = trace_alloc_chain;
512   trace_alloc_chain = result;
513   return (void*) result->x;
514 }
515
516 static void
517 free_trace_alloc ()
518 {
519   while (trace_alloc_chain != 0)
520     {
521       struct alloc_chain *old = trace_alloc_chain;
522
523       trace_alloc_chain = trace_alloc_chain->next;
524       free (old);
525     }
526 }
527
528 /* Read value at ADDR into *DEST, returning 0 if this is valid, != 0
529    otherwise. */
530
531 static int
532 read_memory_safe4 (addr, dest)
533      CORE_ADDR addr;
534      unsigned int *dest;
535 {
536   *dest = *((unsigned int*) addr);
537   return 0;
538 }
539
540 /* Read value at ADDR into *DEST, returning 0 if this is valid, != 0
541    otherwise. */
542
543 static int
544 read_memory_safe8 (addr, dest)
545      CORE_ADDR addr;
546      CORE_ADDR *dest;
547 {
548   *dest = *((CORE_ADDR*) addr);
549   return 0;
550 }
551
552 static CORE_ADDR
553 read_register (regno)
554      int regno;
555 {
556   if (regno >= 0 && regno < 31)
557     return theRegisters[regno];
558
559   return (CORE_ADDR) 0;
560 }
561
562 static void
563 frame_saved_regs_zalloc (fi)
564      struct frame_info *fi;
565 {
566   fi->saved_regs = (CORE_ADDR *) trace_alloc (SIZEOF_FRAME_SAVED_REGS);
567   memset (fi->saved_regs, 0, SIZEOF_FRAME_SAVED_REGS);
568 }
569
570 static void *
571 frame_obstack_alloc (size)
572      unsigned long size;
573 {
574   return (void *) trace_alloc (size);
575 }
576
577 static int
578 inside_entry_file (addr)
579      CORE_ADDR addr;
580 {
581   if (addr == 0)
582     return 1;
583   else
584     return 0;
585 }
586
587 static CORE_ADDR
588 alpha_saved_pc_after_call (frame)
589      struct frame_info *frame;
590 {
591   CORE_ADDR pc = frame->pc;
592   alpha_extra_func_info_t proc_desc;
593   int pcreg;
594
595   proc_desc = find_proc_desc (pc, frame->next, NULL);
596   pcreg = proc_desc ? PROC_PC_REG (proc_desc) : RA_REGNUM;
597
598   return read_register (pcreg);
599 }
600
601 /* Guaranteed to set frame->saved_regs to some values (it never leaves it
602    NULL).  */
603
604 static void
605 alpha_find_saved_regs (frame)
606      struct frame_info *frame;
607 {
608   int ireg;
609   CORE_ADDR reg_position;
610   unsigned long mask;
611   alpha_extra_func_info_t proc_desc;
612   int returnreg;
613
614   frame_saved_regs_zalloc (frame);
615
616   /* If it is the frame for __sigtramp, the saved registers are located in a
617      sigcontext structure somewhere on the stack. __sigtramp passes a pointer
618      to the sigcontext structure on the stack.  If the stack layout for
619      __sigtramp changes, or if sigcontext offsets change, we might have to
620      update this code.  */
621
622 #ifndef SIGFRAME_PC_OFF
623 #define SIGFRAME_PC_OFF         (2 * 8)
624 #define SIGFRAME_REGSAVE_OFF    (4 * 8)
625 #define SIGFRAME_FPREGSAVE_OFF  (SIGFRAME_REGSAVE_OFF + 32 * 8 + 8)
626 #endif
627
628   proc_desc = frame->proc_desc;
629   if (proc_desc == NULL)
630     /* I'm not sure how/whether this can happen.  Normally when we can't
631        find a proc_desc, we "synthesize" one using heuristic_proc_desc
632        and set the saved_regs right away.  */
633     return;
634
635   /* Fill in the offsets for the registers which gen_mask says
636      were saved.  */
637
638   reg_position = frame->frame + PROC_REG_OFFSET (proc_desc);
639   mask = PROC_REG_MASK (proc_desc);
640
641   returnreg = PROC_PC_REG (proc_desc);
642
643   /* Note that RA is always saved first, regardless of its actual
644      register number.  */
645   if (mask & (1 << returnreg))
646     {
647       frame->saved_regs[returnreg] = reg_position;
648       reg_position += 8;
649       mask &= ~(1 << returnreg);        /* Clear bit for RA so we
650                                            don't save again later. */
651     }
652
653   for (ireg = 0; ireg <= 31; ireg++)
654     if (mask & (1 << ireg))
655       {
656         frame->saved_regs[ireg] = reg_position;
657         reg_position += 8;
658       }
659
660   /* Fill in the offsets for the registers which float_mask says
661      were saved.  */
662
663   reg_position = frame->frame + PROC_FREG_OFFSET (proc_desc);
664   mask = PROC_FREG_MASK (proc_desc);
665
666   for (ireg = 0; ireg <= 31; ireg++)
667     if (mask & (1 << ireg))
668       {
669         frame->saved_regs[FP0_REGNUM + ireg] = reg_position;
670         reg_position += 8;
671       }
672
673   frame->saved_regs[PC_REGNUM] = frame->saved_regs[returnreg];
674 }
675
676 static CORE_ADDR
677 read_next_frame_reg (fi, regno)
678      struct frame_info *fi;
679      int regno;
680 {
681   CORE_ADDR result;
682   for (; fi; fi = fi->next)
683     {
684       /* We have to get the saved sp from the sigcontext
685          if it is a signal handler frame.  */
686       if (regno == SP_REGNUM)
687         return fi->frame;
688       else
689         {
690           if (fi->saved_regs == 0)
691             alpha_find_saved_regs (fi);
692
693           if (fi->saved_regs[regno])
694             {
695               if (read_memory_safe8 (fi->saved_regs[regno], &result) == 0)
696                 return result;
697               else
698                 return 0;
699             }
700         }
701     }
702
703   return read_register (regno);
704 }
705
706 static CORE_ADDR
707 alpha_frame_saved_pc (frame)
708      struct frame_info *frame;
709 {
710   return read_next_frame_reg (frame, frame->pc_reg);
711 }
712
713 static struct alpha_extra_func_info temp_proc_desc;
714
715 /* Nonzero if instruction at PC is a return instruction.  "ret
716    $zero,($ra),1" on alpha. */
717
718 static int
719 alpha_about_to_return (pc)
720      CORE_ADDR pc;
721 {
722   int inst;
723
724   read_memory_safe4 (pc, &inst);
725   return inst == 0x6bfa8001;
726 }
727
728 /* A heuristically computed start address for the subprogram
729    containing address PC.   Returns 0 if none detected. */
730
731 static CORE_ADDR
732 heuristic_proc_start (pc)
733      CORE_ADDR pc;
734 {
735   CORE_ADDR start_pc = pc;
736   CORE_ADDR fence = start_pc - heuristic_fence_post;
737
738   if (start_pc == 0)
739     return 0;
740
741   if (heuristic_fence_post == UINT_MAX
742       || fence < VM_MIN_ADDRESS)
743     fence = VM_MIN_ADDRESS;
744
745   /* search back for previous return */
746   for (start_pc -= 4; ; start_pc -= 4)
747     {
748       if (start_pc < fence)
749         return 0;
750       else if (alpha_about_to_return (start_pc))
751         break;
752     }
753
754   start_pc += 4;                /* skip return */
755   return start_pc;
756 }
757
758 static alpha_extra_func_info_t
759 heuristic_proc_desc (start_pc, limit_pc, next_frame, saved_regs_p)
760      CORE_ADDR start_pc;
761      CORE_ADDR limit_pc;
762      struct frame_info *next_frame;
763      struct frame_saved_regs *saved_regs_p;
764 {
765   CORE_ADDR sp = read_next_frame_reg (next_frame, SP_REGNUM);
766   CORE_ADDR cur_pc;
767   int frame_size;
768   int has_frame_reg = 0;
769   unsigned long reg_mask = 0;
770   int pcreg = -1;
771
772   if (start_pc == 0)
773     return 0;
774
775   memset (&temp_proc_desc, '\0', sizeof (temp_proc_desc));
776   if (saved_regs_p != 0)
777     memset (saved_regs_p, '\0', sizeof (struct frame_saved_regs));
778
779   PROC_LOW_ADDR (&temp_proc_desc) = start_pc;
780
781   if (start_pc + 200 < limit_pc)
782     limit_pc = start_pc + 200;
783
784   frame_size = 0;
785   for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += 4)
786     {
787       unsigned int word;
788       int status;
789
790       status = read_memory_safe4 (cur_pc, &word);
791       if (status)
792         return 0;
793
794       if ((word & 0xffff0000) == 0x23de0000)    /* lda $sp,n($sp) */
795         {
796           if (word & 0x8000)
797             frame_size += (-word) & 0xffff;
798           else
799             /* Exit loop if a positive stack adjustment is found, which
800                usually means that the stack cleanup code in the function
801                epilogue is reached.  */
802             break;
803         }
804       else if ((word & 0xfc1f0000) == 0xb41e0000        /* stq reg,n($sp) */
805                && (word & 0xffff0000) != 0xb7fe0000)    /* reg != $zero */
806         {
807           int reg = (word & 0x03e00000) >> 21;
808
809           reg_mask |= 1 << reg;
810           if (saved_regs_p != 0)
811             saved_regs_p->regs[reg] = sp + (short) word;
812
813           /* Starting with OSF/1-3.2C, the system libraries are shipped
814              without local symbols, but they still contain procedure
815              descriptors without a symbol reference. GDB is currently
816              unable to find these procedure descriptors and uses
817              heuristic_proc_desc instead.
818              As some low level compiler support routines (__div*, __add*)
819              use a non-standard return address register, we have to
820              add some heuristics to determine the return address register,
821              or stepping over these routines will fail.
822              Usually the return address register is the first register
823              saved on the stack, but assembler optimization might
824              rearrange the register saves.
825              So we recognize only a few registers (t7, t9, ra) within
826              the procedure prologue as valid return address registers.
827              If we encounter a return instruction, we extract the
828              the return address register from it.
829
830              FIXME: Rewriting GDB to access the procedure descriptors,
831              e.g. via the minimal symbol table, might obviate this hack.  */
832           if (pcreg == -1
833               && cur_pc < (start_pc + 80)
834               && (reg == T7_REGNUM || reg == T9_REGNUM || reg == RA_REGNUM))
835             pcreg = reg;
836         }
837       else if ((word & 0xffe0ffff) == 0x6be08001)       /* ret zero,reg,1 */
838         pcreg = (word >> 16) & 0x1f;
839       else if (word == 0x47de040f)      /* bis sp,sp fp */
840         has_frame_reg = 1;
841     }
842
843   if (pcreg == -1)
844     {
845       /* If we haven't found a valid return address register yet,
846          keep searching in the procedure prologue.  */
847       while (cur_pc < (limit_pc + 80) && cur_pc < (start_pc + 80))
848         {
849           unsigned int word;
850
851           if (read_memory_safe4 (cur_pc, &word))
852             break;
853           cur_pc += 4;
854
855           if ((word & 0xfc1f0000) == 0xb41e0000         /* stq reg,n($sp) */
856               && (word & 0xffff0000) != 0xb7fe0000)     /* reg != $zero */
857             {
858               int reg = (word & 0x03e00000) >> 21;
859
860               if (reg == T7_REGNUM || reg == T9_REGNUM || reg == RA_REGNUM)
861                 {
862                   pcreg = reg;
863                   break;
864                 }
865             }
866           else if ((word & 0xffe0ffff) == 0x6be08001)   /* ret zero,reg,1 */
867             {
868               pcreg = (word >> 16) & 0x1f;
869               break;
870             }
871         }
872     }
873
874   if (has_frame_reg)
875     PROC_FRAME_REG (&temp_proc_desc) = GCC_FP_REGNUM;
876   else
877     PROC_FRAME_REG (&temp_proc_desc) = SP_REGNUM;
878
879   PROC_FRAME_OFFSET (&temp_proc_desc) = frame_size;
880   PROC_REG_MASK (&temp_proc_desc) = reg_mask;
881   PROC_PC_REG (&temp_proc_desc) = (pcreg == -1) ? RA_REGNUM : pcreg;
882   PROC_LOCALOFF (&temp_proc_desc) = 0;  /* XXX - bogus */
883
884   return &temp_proc_desc;
885 }
886
887 static alpha_extra_func_info_t
888 find_proc_desc (pc, next_frame, saved_regs)
889      CORE_ADDR pc;
890      struct frame_info *next_frame;
891      struct frame_saved_regs *saved_regs;
892 {
893   CORE_ADDR startaddr;
894
895   /* If heuristic_fence_post is non-zero, determine the procedure
896      start address by examining the instructions.
897      This allows us to find the start address of static functions which
898      have no symbolic information, as startaddr would have been set to
899      the preceding global function start address by the
900      find_pc_partial_function call above.  */
901   startaddr = heuristic_proc_start (pc);
902
903   return heuristic_proc_desc (startaddr, pc, next_frame, saved_regs);
904 }
905
906 static CORE_ADDR
907 alpha_frame_chain (frame)
908      struct frame_info *frame;
909 {
910   alpha_extra_func_info_t proc_desc;
911   CORE_ADDR saved_pc = FRAME_SAVED_PC (frame);
912
913   if (saved_pc == 0 || inside_entry_file (saved_pc))
914     return 0;
915
916   proc_desc = find_proc_desc (saved_pc, frame, NULL);
917   if (!proc_desc)
918     return 0;
919
920   /* If no frame pointer and frame size is zero, we must be at end
921      of stack (or otherwise hosed).  If we don't check frame size,
922      we loop forever if we see a zero size frame.  */
923   if (PROC_FRAME_REG (proc_desc) == SP_REGNUM
924       && PROC_FRAME_OFFSET (proc_desc) == 0)
925     return 0;
926   else
927     return read_next_frame_reg (frame, PROC_FRAME_REG (proc_desc))
928       + PROC_FRAME_OFFSET (proc_desc);
929 }
930
931 static void
932 init_extra_frame_info (frame)
933      struct frame_info *frame;
934 {
935   struct frame_saved_regs temp_saved_regs;
936   alpha_extra_func_info_t proc_desc = 
937     find_proc_desc (frame->pc, frame->next, &temp_saved_regs);
938
939   frame->saved_regs = NULL;
940   frame->localoff = 0;
941   frame->pc_reg = RA_REGNUM;
942   frame->proc_desc = proc_desc;
943
944   if (proc_desc)
945     {
946       /* Get the locals offset and the saved pc register from the
947          procedure descriptor, they are valid even if we are in the
948          middle of the prologue.  */
949       frame->localoff = PROC_LOCALOFF (proc_desc);
950       frame->pc_reg = PROC_PC_REG (proc_desc);
951
952       /* Fixup frame-pointer - only needed for top frame */
953
954       /* This may not be quite right, if proc has a real frame register.
955          Get the value of the frame relative sp, procedure might have been
956          interrupted by a signal at it's very start.  */
957       if (frame->pc == PROC_LOW_ADDR (proc_desc))
958         frame->frame = read_next_frame_reg (frame->next, SP_REGNUM);
959       else
960         frame->frame
961           = (read_next_frame_reg (frame->next, PROC_FRAME_REG (proc_desc))
962              + PROC_FRAME_OFFSET (proc_desc));
963
964       frame->saved_regs
965         = (CORE_ADDR *) frame_obstack_alloc (SIZEOF_FRAME_SAVED_REGS);
966       memcpy
967         (frame->saved_regs, temp_saved_regs.regs, SIZEOF_FRAME_SAVED_REGS);
968       frame->saved_regs[PC_REGNUM] = frame->saved_regs[RA_REGNUM];
969     }
970 }
971
972 /* Create an arbitrary (i.e. address specified by user) or innermost frame.
973    Always returns a non-NULL value.  */
974
975 static struct frame_info *
976 create_new_frame (addr, pc)
977      CORE_ADDR addr;
978      CORE_ADDR pc;
979 {
980   struct frame_info *fi;
981
982   fi = (struct frame_info *)
983     trace_alloc (sizeof (struct frame_info));
984
985   /* Arbitrary frame */
986   fi->next = NULL;
987   fi->prev = NULL;
988   fi->frame = addr;
989   fi->pc = pc;
990
991 #ifdef INIT_EXTRA_FRAME_INFO
992   INIT_EXTRA_FRAME_INFO (0, fi);
993 #endif
994
995   return fi;
996 }
997
998 static CORE_ADDR current_pc;
999
1000 static void
1001 set_current_pc ()
1002 {
1003   current_pc = (CORE_ADDR) __builtin_return_address (0);
1004 }
1005
1006 static CORE_ADDR
1007 read_pc ()
1008 {
1009   return current_pc;
1010 }
1011
1012 static struct frame_info *
1013 get_current_frame ()
1014 {
1015   return create_new_frame (0, read_pc ());
1016 }
1017
1018 /* Return the frame that called FI.
1019    If FI is the original frame (it has no caller), return 0.  */
1020
1021 static struct frame_info *
1022 get_prev_frame (next_frame)
1023      struct frame_info *next_frame;
1024 {
1025   CORE_ADDR address = 0;
1026   struct frame_info *prev;
1027   int fromleaf = 0;
1028
1029   /* If we have the prev one, return it */
1030   if (next_frame->prev)
1031     return next_frame->prev;
1032
1033   /* On some machines it is possible to call a function without
1034      setting up a stack frame for it.  On these machines, we
1035      define this macro to take two args; a frameinfo pointer
1036      identifying a frame and a variable to set or clear if it is
1037      or isn't leafless.  */
1038
1039   /* Two macros defined in tm.h specify the machine-dependent
1040      actions to be performed here.
1041
1042      First, get the frame's chain-pointer.  If that is zero, the frame
1043      is the outermost frame or a leaf called by the outermost frame.
1044      This means that if start calls main without a frame, we'll return
1045      0 (which is fine anyway).
1046
1047      Nope; there's a problem.  This also returns when the current
1048      routine is a leaf of main.  This is unacceptable.  We move
1049      this to after the ffi test; I'd rather have backtraces from
1050      start go curfluy than have an abort called from main not show
1051      main.  */
1052
1053   address = FRAME_CHAIN (next_frame);
1054   if (!FRAME_CHAIN_VALID (address, next_frame))
1055     return 0;
1056   address = FRAME_CHAIN_COMBINE (address, next_frame);
1057
1058   if (address == 0)
1059     return 0;
1060
1061   prev = (struct frame_info *) trace_alloc (sizeof (struct frame_info));
1062
1063   prev->saved_regs = NULL;
1064   if (next_frame)
1065     next_frame->prev = prev;
1066
1067   prev->next = next_frame;
1068   prev->prev = (struct frame_info *) 0;
1069   prev->frame = address;
1070
1071   /* This change should not be needed, FIXME!  We should
1072      determine whether any targets *need* INIT_FRAME_PC to happen
1073      after INIT_EXTRA_FRAME_INFO and come up with a simple way to
1074      express what goes on here.
1075
1076      INIT_EXTRA_FRAME_INFO is called from two places: create_new_frame
1077      (where the PC is already set up) and here (where it isn't).
1078      INIT_FRAME_PC is only called from here, always after
1079      INIT_EXTRA_FRAME_INFO.
1080
1081      The catch is the MIPS, where INIT_EXTRA_FRAME_INFO requires the PC
1082      value (which hasn't been set yet).  Some other machines appear to
1083      require INIT_EXTRA_FRAME_INFO before they can do INIT_FRAME_PC.  Phoo.
1084
1085      We shouldn't need INIT_FRAME_PC_FIRST to add more complication to
1086      an already overcomplicated part of GDB.   gnu@cygnus.com, 15Sep92.
1087
1088      Assuming that some machines need INIT_FRAME_PC after
1089      INIT_EXTRA_FRAME_INFO, one possible scheme:
1090
1091      SETUP_INNERMOST_FRAME()
1092      Default version is just create_new_frame (read_fp ()),
1093      read_pc ()).  Machines with extra frame info would do that (or the
1094      local equivalent) and then set the extra fields.
1095      INIT_PREV_FRAME(fromleaf, prev)
1096      Replace INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC.  This should
1097      also return a flag saying whether to keep the new frame, or
1098      whether to discard it, because on some machines (e.g.  mips) it
1099      is really awkward to have FRAME_CHAIN_VALID called *before*
1100      INIT_EXTRA_FRAME_INFO (there is no good way to get information
1101      deduced in FRAME_CHAIN_VALID into the extra fields of the new frame).
1102      std_frame_pc(fromleaf, prev)
1103      This is the default setting for INIT_PREV_FRAME.  It just does what
1104      the default INIT_FRAME_PC does.  Some machines will call it from
1105      INIT_PREV_FRAME (either at the beginning, the end, or in the middle).
1106      Some machines won't use it.
1107      kingdon@cygnus.com, 13Apr93, 31Jan94, 14Dec94.  */
1108
1109 #ifdef INIT_FRAME_PC_FIRST
1110   INIT_FRAME_PC_FIRST (fromleaf, prev);
1111 #endif
1112
1113 #ifdef INIT_EXTRA_FRAME_INFO
1114   INIT_EXTRA_FRAME_INFO (fromleaf, prev);
1115 #endif
1116
1117   /* This entry is in the frame queue now, which is good since
1118      FRAME_SAVED_PC may use that queue to figure out its value
1119      (see tm-sparc.h).  We want the pc saved in the inferior frame. */
1120   INIT_FRAME_PC (fromleaf, prev);
1121
1122   /* If ->frame and ->pc are unchanged, we are in the process of getting
1123      ourselves into an infinite backtrace.  Some architectures check this
1124      in FRAME_CHAIN or thereabouts, but it seems like there is no reason
1125      this can't be an architecture-independent check.  */
1126   if (next_frame != NULL)
1127     {
1128       if (prev->frame == next_frame->frame
1129           && prev->pc == next_frame->pc)
1130         {
1131           next_frame->prev = NULL;
1132           free (prev);
1133           return NULL;
1134         }
1135     }
1136
1137   return prev;
1138 }
1139
1140 #define SAVE(regno,disp) \
1141     "stq $" #regno ", " #disp "(%0)\n" 
1142
1143 int
1144 __gnat_backtrace (array, size, exclude_min, exclude_max)
1145      void **array;
1146      int size;
1147      void *exclude_min;
1148      void *exclude_max;
1149 {
1150   struct frame_info* top;
1151   struct frame_info* current;
1152   int cnt;
1153
1154   /* This function is not thread safe, protect it */
1155   (*Lock_Task) ();
1156   asm volatile (
1157       SAVE (9,72)
1158       SAVE (10,80)
1159       SAVE (11,88)
1160       SAVE (12,96)
1161       SAVE (13,104)
1162       SAVE (14,112)
1163       SAVE (15,120)
1164       SAVE (16,128)
1165       SAVE (17,136)
1166       SAVE (18,144)
1167       SAVE (19,152)
1168       SAVE (20,160)
1169       SAVE (21,168)
1170       SAVE (22,176)
1171       SAVE (23,184)
1172       SAVE (24,192)
1173       SAVE (25,200)
1174       SAVE (26,208)
1175       SAVE (27,216)
1176       SAVE (28,224)
1177       SAVE (29,232)
1178       SAVE (30,240)
1179       : : "r" (&theRegisters));
1180
1181   trace_alloc_chain = NULL;
1182   set_current_pc ();
1183
1184   top = current = get_current_frame ();
1185   cnt = 0;
1186
1187   /* We skip the call to this function, it makes no sense to record it.  */
1188   for (cnt = 0; cnt < SKIP_FRAME; cnt += 1) {
1189     current = get_prev_frame (current);
1190   }
1191
1192   cnt = 0;
1193   while (cnt < size)
1194     {
1195       if (STOP_FRAME)
1196         break;
1197
1198       if (current->pc < (CORE_ADDR) exclude_min
1199           || current->pc > (CORE_ADDR) exclude_max)
1200         array[cnt++] = (void*) (current->pc + PC_ADJUST);
1201
1202       current = get_prev_frame (current);
1203     }
1204
1205   free_trace_alloc ();
1206   (*Unlock_Task) ();
1207
1208   return cnt;
1209 }
1210 #endif