OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [gcc-4.5.1/] [gcc/] [ada/] [tracebak.c] - Blame information for rev 816

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 281 jeremybenn
/****************************************************************************
2
 *                                                                          *
3
 *                         GNAT COMPILER COMPONENTS                         *
4
 *                                                                          *
5
 *                            T R A C E B A C K                             *
6
 *                                                                          *
7
 *                          C Implementation File                           *
8
 *                                                                          *
9
 *                     Copyright (C) 2000-2009, AdaCore                     *
10
 *                                                                          *
11
 * GNAT is free software;  you can  redistribute it  and/or modify it under *
12
 * terms of the  GNU General Public License as published  by the Free Soft- *
13
 * ware  Foundation;  either version 2,  or (at your option) any later ver- *
14
 * sion.  GNAT is distributed in the hope that it will be useful, but WITH- *
15
 * OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY *
16
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License *
17
 * for  more details.  You should have  received  a copy of the GNU General *
18
 * Public License  distributed with GNAT;  see file COPYING.  If not, write *
19
 * to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, *
20
 * Boston, MA 02110-1301, USA.                                              *
21
 *                                                                          *
22
 * As a  special  exception,  if you  link  this file  with other  files to *
23
 * produce an executable,  this file does not by itself cause the resulting *
24
 * executable to be covered by the GNU General Public License. This except- *
25
 * ion does not  however invalidate  any other reasons  why the  executable *
26
 * file might be covered by the  GNU Public License.                        *
27
 *                                                                          *
28
 * GNAT was originally developed  by the GNAT team at  New York University. *
29
 * Extensive contributions were provided by Ada Core Technologies Inc.      *
30
 *                                                                          *
31
 ****************************************************************************/
32
 
33
/* This file contains low level support for stack unwinding using GCC intrinsic
34
   functions.
35
   It has been tested on the following configurations:
36
   PowerPC/AiX
37
   PowerPC/Darwin
38
   PowerPC/VxWorks
39
   SPARC/Solaris
40
   i386/GNU/Linux
41
   i386/Solaris
42
   i386/NT
43
   i386/OS2
44
   i386/LynxOS
45
   Alpha/VxWorks
46
   Alpha/VMS
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
/* We don't want fancy_abort here.  */
61
#undef abort
62
#endif
63
 
64
extern int __gnat_backtrace (void **, int, void *, void *, int);
65
 
66
/* The point is to provide an implementation of the __gnat_backtrace function
67
   above, called by the default implementation of the System.Traceback package.
68
 
69
   We first have a series of target specific implementations, each included
70
   from a separate C file for readability purposes.
71
 
72
   Then come two flavors of a generic implementation: one relying on static
73
   assumptions about the frame layout, and the other one using the GCC EH
74
   infrastructure.  The former uses a whole set of macros and structures which
75
   may be tailored on a per target basis, and is activated as soon as
76
   USE_GENERIC_UNWINDER is defined.  The latter uses a small subset of the
77
   macro definitions and is activated when USE_GCC_UNWINDER is defined. It is
78
   only available post GCC 3.3.
79
 
80
   Finally, there is a default dummy implementation, necessary to make the
81
   linker happy on platforms where the feature is not supported, but where the
82
   function is still referenced by the default System.Traceback.  */
83
 
84
#define Lock_Task system__soft_links__lock_task
85
extern void (*Lock_Task) (void);
86
 
87
#define Unlock_Task system__soft_links__unlock_task
88
extern void (*Unlock_Task) (void);
89
 
90
/*-------------------------------------*
91
 *-- Target specific implementations --*
92
 *-------------------------------------*/
93
 
94
#if defined (__alpha_vxworks)
95
 
96
#include "tb-alvxw.c"
97
 
98
#elif defined (__ALPHA) && defined (__VMS__)
99
 
100
#include "tb-alvms.c"
101
 
102
#elif defined (__ia64__) && defined (__VMS__)
103
 
104
#include "tb-ivms.c"
105
 
106
#else
107
 
108
/* No target specific implementation.  */
109
 
110
/*----------------------------------------------------------------*
111
 *-- Target specific definitions for the generic implementation --*
112
 *----------------------------------------------------------------*/
113
 
114
/* The stack layout is specified by the target ABI. The "generic" scheme is
115
   based on the following assumption:
116
 
117
     The stack layout from some frame pointer is such that the information
118
     required to compute the backtrace is available at static offsets.
119
 
120
   For a given frame, the information we are interested in is the saved return
121
   address (somewhere after the call instruction in the caller) and a pointer
122
   to the caller's frame. The former is the base of the call chain information
123
   we store in the tracebacks array. The latter allows us to loop over the
124
   successive frames in the chain.
125
 
126
   To initiate the process, we retrieve an initial frame address using the
127
   appropriate GCC builtin (__builtin_frame_address).
128
 
129
   This scheme is unfortunately not applicable on every target because the
130
   stack layout is not necessarily regular (static) enough. On targets where
131
   this scheme applies, the implementation relies on the following items:
132
 
133
   o struct layout, describing the expected stack data layout relevant to the
134
     information we are interested in,
135
 
136
   o FRAME_OFFSET, the offset, from a given frame address or frame pointer
137
     value, at which this layout will be found,
138
 
139
   o FRAME_LEVEL, controls how many frames up we get at to start with,
140
     from the initial frame pointer we compute by way of the GCC builtin,
141
 
142
 
143
     where return addresses are saved by a function in it's caller's frame
144
     (e.g. PPC).
145
 
146
   o PC_ADJUST, to account for the difference between a call point (address
147
     of a call instruction), which is what we want in the output array, and
148
     the associated return address, which is what we retrieve from the stack.
149
 
150
   o STOP_FRAME, to decide whether we reached the top of the call chain, and
151
     thus if the process shall stop.
152
 
153
           :
154
           :                   stack
155
           |             +----------------+
156
           |   +-------->|       :        |
157
           |   |         | (FRAME_OFFSET) |
158
           |   |         |       :        |  (PC_ADJUST)
159
           |   |  layout:| return_address ----------------+
160
           |   |         |     ....       |               |
161
           +---------------  next_frame   |               |
162
               |         |     ....       |               |
163
               |         |                |               |
164
               |         +----------------+               |  +-----+
165
               |         |       :        |<- Base fp     |  |  :  |
166
               |         | (FRAME_OFFSET) | (FRAME_LEVEL) |  |  :  |
167
               |         |       :        |               +--->    | [1]
168
               |  layout:| return_address -------------------->    | [0]
169
               |         |       ...      |  (PC_ADJUST)     +-----+
170
               +----------   next_frame   |                 traceback[]
171
                         |       ...      |
172
                         |                |
173
                         +----------------+
174
 
175
   o BASE_SKIP,
176
 
177
   Since we inherently deal with return addresses, there is an implicit shift
178
   by at least one for the initial point we are able to observe in the chain.
179
 
180
   On some targets (e.g. sparc-solaris), the first return address we can
181
   easily get without special code is even our caller's return address, so
182
   there is a initial shift of two.
183
 
184
   BASE_SKIP represents this initial shift, which is the minimal "skip_frames"
185
   value we support. We could add special code for the skip_frames < BASE_SKIP
186
   cases. This is not done currently because there is virtually no situation
187
   in which this would be useful.
188
 
189
   Finally, to account for some ABI specificities, a target may (but does
190
   not have to) define:
191
 
192
   o FORCE_CALL, to force a call to a dummy function at the very beginning
193
     of the computation. See the PPC AIX target for an example where this
194
     is useful.
195
 
196
   o FETCH_UP_FRAME, to force an invocation of __builtin_frame_address with a
197
     positive argument right after a possibly forced call even if FRAME_LEVEL
198
     is 0. See the SPARC Solaris case for an example where this is useful.
199
 
200
  */
201
 
202
/*--------------------------- PPC AIX/Darwin ----------------------------*/
203
 
204
#if ((defined (_POWER) && defined (_AIX)) || \
205
(defined (__ppc__) && defined (__APPLE__)))
206
 
207
#define USE_GENERIC_UNWINDER
208
 
209
struct layout
210
{
211
  struct layout *next;
212
  void *pad;
213
  void *return_address;
214
};
215
 
216
#define FRAME_OFFSET(FP) 0
217
#define PC_ADJUST -4
218
#define STOP_FRAME(CURRENT, TOP_STACK) ((void *) (CURRENT) < (TOP_STACK))
219
 
220
/* The PPC ABI has an interesting specificity: the return address saved by a
221
   function is located in it's caller's frame, and the save operation only
222
   takes place if the function performs a call.
223
 
224
   To have __gnat_backtrace retrieve its own return address, we then
225
   define ... */
226
 
227
#define FORCE_CALL 1
228
#define FRAME_LEVEL 1
229
 
230
#define BASE_SKIP 1
231
 
232
/*-------------------- PPC ELF (GNU/Linux & VxWorks) ---------------------*/
233
 
234
#elif (defined (_ARCH_PPC) && defined (__vxworks)) ||  \
235
  (defined (linux) && defined (__powerpc__))
236
 
237
#define USE_GENERIC_UNWINDER
238
 
239
struct layout
240
{
241
  struct layout *next;
242
  void *return_address;
243
};
244
 
245
#define FORCE_CALL 1
246
#define FRAME_LEVEL 1
247
/* See the PPC AIX case for an explanation of these values.  */
248
 
249
#define FRAME_OFFSET(FP) 0
250
#define PC_ADJUST -4
251
#define STOP_FRAME(CURRENT, TOP_STACK) ((CURRENT)->next == 0)
252
 
253
#define BASE_SKIP 1
254
 
255
/*-------------------------- SPARC Solaris -----------------------------*/
256
 
257
#elif defined (sun) && defined (sparc)
258
 
259
#define USE_GENERIC_UNWINDER
260
 
261
/* These definitions are inspired from the Appendix D (Software
262
   Considerations) of the SPARC V8 architecture manual.  */
263
 
264
struct layout
265
{
266
  struct layout *next;
267
  void *return_address;
268
};
269
 
270
#ifdef __arch64__
271
#define STACK_BIAS 2047 /* V9 ABI */
272
#else
273
#define STACK_BIAS 0    /* V8 ABI */
274
#endif
275
 
276
#define FRAME_LEVEL 0
277
#define FRAME_OFFSET(FP) (14 * sizeof (void*) + (FP ? STACK_BIAS : 0))
278
#define PC_ADJUST 0
279
#define STOP_FRAME(CURRENT, TOP_STACK) \
280
  ((CURRENT)->return_address == 0|| (CURRENT)->next == 0 \
281
   || (void *) (CURRENT) < (TOP_STACK))
282
 
283
/* The SPARC register windows need to be flushed before we may access them
284
   from the stack. This is achieved by way of builtin_frame_address only
285
   when the "count" argument is positive, so force at least one such call.  */
286
#define FETCH_UP_FRAME_ADDRESS
287
 
288
#define BASE_SKIP 2
289
/* From the frame pointer of frame N, we are accessing the flushed register
290
   window of frame N-1 (positive offset from fp), in which we retrieve the
291
   saved return address. We then end up with our caller's return address.  */
292
 
293
/*------------------------------- x86 ----------------------------------*/
294
 
295
#elif defined (i386)
296
 
297
#if defined (__WIN32)
298
#include <windows.h>
299
#define IS_BAD_PTR(ptr) (IsBadCodePtr((void *)ptr))
300
#elif defined (sun)
301
#define IS_BAD_PTR(ptr) ((unsigned long)ptr == -1UL)
302
#else
303
#define IS_BAD_PTR(ptr) 0
304
#endif
305
 
306
#define USE_GENERIC_UNWINDER
307
 
308
struct layout
309
{
310
  struct layout *next;
311
  void *return_address;
312
};
313
 
314
#define FRAME_LEVEL 1
315
/* builtin_frame_address (1) is expected to work on this target, and (0) might
316
   return the soft stack pointer, which does not designate a location where a
317
   backchain and a return address might be found.  */
318
 
319
#define FRAME_OFFSET(FP) 0
320
#define PC_ADJUST -2
321
#define STOP_FRAME(CURRENT, TOP_STACK) \
322
  (IS_BAD_PTR((long)(CURRENT)) \
323
   || IS_BAD_PTR((long)(CURRENT)->return_address) \
324
   || (CURRENT)->return_address == 0|| (CURRENT)->next == 0  \
325
   || (void *) (CURRENT) < (TOP_STACK))
326
 
327
#define BASE_SKIP (1+FRAME_LEVEL)
328
 
329
/* On i386 architecture we check that at the call point we really have a call
330
   insn. Possible call instructions are:
331
 
332
   call  addr16        E8 xx xx xx xx
333
   call  reg           FF Dx
334
   call  off(reg)      FF xx xx
335
   lcall addr seg      9A xx xx xx xx xx xx
336
 
337
   This check will not catch all cases but it will increase the backtrace
338
   reliability on this architecture.
339
*/
340
 
341
#define VALID_STACK_FRAME(ptr) \
342
   (!IS_BAD_PTR(ptr) \
343
    && (((*((ptr) - 3) & 0xff) == 0xe8) \
344
        || ((*((ptr) - 5) & 0xff) == 0x9a) \
345
        || ((*((ptr) - 1) & 0xff) == 0xff) \
346
        || (((*(ptr) & 0xd0ff) == 0xd0ff))))
347
 
348
/*----------------------------- x86_64 ---------------------------------*/
349
 
350
#elif defined (__x86_64__)
351
 
352
#define USE_GCC_UNWINDER
353
/* The generic unwinder is not used for this target because it is based
354
   on frame layout assumptions that are not reliable on this target (the
355
   rbp register is very likely used for something else than storing the
356
   frame pointer in optimized code). Hence, we use the GCC unwinder
357
   based on DWARF 2 call frame information, although it has the drawback
358
   of not being able to unwind through frames compiled without DWARF 2
359
   information.
360
*/
361
 
362
#define PC_ADJUST -2
363
/* The minimum size of call instructions on this architecture is 2 bytes */
364
 
365
/*----------------------------- ia64 ---------------------------------*/
366
 
367
#elif defined (__ia64__) && (defined (linux) || defined (__hpux__))
368
 
369
#define USE_GCC_UNWINDER
370
/* Use _Unwind_Backtrace driven exceptions on ia64 HP-UX and ia64
371
   GNU/Linux, where _Unwind_Backtrace is provided by the system unwind
372
   library. On HP-UX 11.23 this requires patch PHSS_33352, which adds
373
   _Unwind_Backtrace to the system unwind library. */
374
 
375
#define PC_ADJUST -4
376
 
377
 
378
#endif
379
 
380
/*---------------------------------------------------------------------*
381
 *--      The post GCC 3.3 infrastructure based implementation       --*
382
 *---------------------------------------------------------------------*/
383
 
384
#if defined (USE_GCC_UNWINDER) && (__GNUC__ * 10 + __GNUC_MINOR__ > 33)
385
 
386
/* Conditioning the inclusion on the GCC version is useful to avoid bootstrap
387
   path problems, since the included file refers to post 3.3 functions in
388
   libgcc, and the stage1 compiler is unlikely to be linked against a post 3.3
389
   library.  It actually disables the support for backtraces in this compiler
390
   for targets defining USE_GCC_UNWINDER, which is OK since we don't use the
391
   traceback capability in the compiler anyway.
392
 
393
   The condition is expressed the way above because we cannot reliably rely on
394
   any other macro from the base compiler when compiling stage1.  */
395
 
396
#include "tb-gcc.c"
397
 
398
/*------------------------------------------------------------------*
399
 *-- The generic implementation based on frame layout assumptions --*
400
 *------------------------------------------------------------------*/
401
 
402
#elif defined (USE_GENERIC_UNWINDER)
403
 
404
#ifndef CURRENT_STACK_FRAME
405
# define CURRENT_STACK_FRAME  ({ char __csf; &__csf; })
406
#endif
407
 
408
#ifndef VALID_STACK_FRAME
409
#define VALID_STACK_FRAME(ptr) 1
410
#endif
411
 
412
#ifndef MAX
413
#define MAX(x,y) ((x) > (y) ? (x) : (y))
414
#endif
415
 
416
#ifndef FORCE_CALL
417
#define FORCE_CALL 0
418
#endif
419
 
420
/* Make sure the function is not inlined.  */
421
static void forced_callee (void) __attribute__ ((noinline));
422
 
423
static void forced_callee (void)
424
{
425
  /* Make sure the function is not pure.  */
426
  volatile int i __attribute__ ((unused)) = 0;
427
}
428
 
429
int
430
__gnat_backtrace (void **array,
431
                  int size,
432
                  void *exclude_min,
433
                  void *exclude_max,
434
                  int skip_frames)
435
{
436
  struct layout *current;
437
  void *top_frame;
438
  void *top_stack;
439
  int cnt = 0;
440
 
441
  if (FORCE_CALL)
442
    forced_callee ();
443
 
444
  /* Force a call to builtin_frame_address with a positive argument
445
     if required. This is necessary e.g. on SPARC to have the register
446
     windows flushed before we attempt to access them on the stack.  */
447
#if defined (FETCH_UP_FRAME_ADDRESS) && (FRAME_LEVEL == 0)
448
  __builtin_frame_address (1);
449
#endif
450
 
451
  top_frame = __builtin_frame_address (FRAME_LEVEL);
452
  top_stack = CURRENT_STACK_FRAME;
453
  current = (struct layout *) ((size_t) top_frame + FRAME_OFFSET (0));
454
 
455
  /* Skip the number of calls we have been requested to skip, accounting for
456
     the BASE_SKIP parameter.
457
 
458
     FRAME_LEVEL is meaningless for the count adjustment. It impacts where we
459
     start retrieving data from, but how many frames "up" we start at is in
460
     BASE_SKIP by definition.  */
461
 
462
  skip_frames = MAX (0, skip_frames - BASE_SKIP);
463
 
464
  while (cnt < skip_frames)
465
    {
466
      current = (struct layout *) ((size_t) current->next + FRAME_OFFSET (1));
467
      cnt++;
468
    }
469
 
470
  cnt = 0;
471
  while (cnt < size)
472
    {
473
      if (STOP_FRAME (current, top_stack) ||
474
          !VALID_STACK_FRAME((char *)(current->return_address + PC_ADJUST)))
475
        break;
476
 
477
      if (current->return_address < exclude_min
478
          || current->return_address > exclude_max)
479
        array[cnt++] = current->return_address + PC_ADJUST;
480
 
481
      current = (struct layout *) ((size_t) current->next + FRAME_OFFSET (1));
482
    }
483
 
484
  return cnt;
485
}
486
 
487
#else
488
 
489
/* No target specific implementation and neither USE_GCC_UNWINDER not
490
   USE_GCC_UNWINDER defined.  */
491
 
492
/*------------------------------*
493
 *-- The dummy implementation --*
494
 *------------------------------*/
495
 
496
int
497
__gnat_backtrace (void **array ATTRIBUTE_UNUSED,
498
                  int size ATTRIBUTE_UNUSED,
499
                  void *exclude_min ATTRIBUTE_UNUSED,
500
                  void *exclude_max ATTRIBUTE_UNUSED,
501
                  int skip_frames ATTRIBUTE_UNUSED)
502
{
503
  return 0;
504
}
505
 
506
#endif
507
 
508
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.