OSDN Git Service

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