OSDN Git Service

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