1 /****************************************************************************
2  *                                                                          *
3  *                         GNAT RUN-TIME COMPONENTS                         *
4  *                                                                          *
5  *                 T R A C E B A C K - I t a n i u m  / V M S               *
6  *                                                                          *
7  *                          C Implementation File                           *
8  *                                                                          *
9  *                     Copyright (C) 2007-2011, 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 3,  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.                                     *
17  *                                                                          *
18  * As a special exception under Section 7 of GPL version 3, you are granted *
19  * additional permissions described in the GCC Runtime Library Exception,   *
20  * version 3.1, as published by the Free Software Foundation.               *
21  *                                                                          *
22  * You should have received a copy of the GNU General Public License and    *
23  * a copy of the GCC Runtime Library Exception along with this program;     *
24  * see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    *
25  * <http://www.gnu.org/licenses/>.                                          *
26  *                                                                          *
27  * GNAT was originally developed  by the GNAT team at  New York University. *
28  * Extensive contributions were provided by Ada Core Technologies Inc.      *
29  *                                                                          *
30  ****************************************************************************/
31 
32 /* Itanium Open/VMS implementation of backtrace.  Use ICB (Invocation
33    Context Block) routines.  */
34 #include <stdlib.h>
35 #include <vms/libicb.h>
36 
37 /* Declare libicb routines.  */
38 extern INVO_CONTEXT_BLK *LIB$I64_CREATE_INVO_CONTEXT (void *(*)(size_t),
39 						      void (*)(void *),
40 						      int);
41 extern void LIB$I64_FREE_INVO_CONTEXT (INVO_CONTEXT_BLK *);
42 extern int LIB$I64_GET_CURR_INVO_CONTEXT(INVO_CONTEXT_BLK *);
43 extern int LIB$I64_GET_PREV_INVO_CONTEXT(INVO_CONTEXT_BLK *);
44 
45 /* Gcc internal headers poison malloc.  So use xmalloc() when building the
46    compiler.  */
47 #ifdef IN_RTS
48 #define BT_MALLOC malloc
49 #else
50 #define BT_MALLOC xmalloc
51 #endif
52 
53 int
__gnat_backtrace(void ** array,int size,void * exclude_min,void * exclude_max,int skip_frames)54 __gnat_backtrace (void **array, int size,
55                   void *exclude_min, void *exclude_max, int skip_frames)
56 {
57   INVO_CONTEXT_BLK *ctxt;
58   int res = 0;
59   int n = 0;
60 
61   /* Create the context.  */
62   ctxt = LIB$I64_CREATE_INVO_CONTEXT (BT_MALLOC, free, 0);
63   if (ctxt == NULL)
64     return 0;
65 
66   LIB$I64_GET_CURR_INVO_CONTEXT (ctxt);
67 
68   while (1)
69     {
70       void *pc = (void *)ctxt->libicb$ih_pc;
71       if (pc == (void *)0)
72 	break;
73       if (ctxt->libicb$v_bottom_of_stack)
74 	break;
75       if (n >= skip_frames && (pc < exclude_min || pc > exclude_max))
76 	{
77 	  array[res++] = (void *)(ctxt->libicb$ih_pc);
78 	  if (res == size)
79 	    break;
80 	}
81       n++;
82       LIB$I64_GET_PREV_INVO_CONTEXT (ctxt);
83     }
84 
85   /* Free the context.  */
86   LIB$I64_FREE_INVO_CONTEXT (ctxt);
87   return res;
88 }
89