1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT COMPILER COMPONENTS                         --
4--                                                                          --
5--                     S Y S T E M . T R A C E B A C K                      --
6--                                                                          --
7--                                 B o d y                                  --
8--                                                                          --
9--                     Copyright (C) 1999-2010, 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--  This version uses System.Machine_State_Operations routines
33
34with System.Machine_State_Operations;
35
36package body System.Traceback is
37
38   use System.Machine_State_Operations;
39
40   ----------------
41   -- Call_Chain --
42   ----------------
43
44   procedure Call_Chain
45     (Traceback : System.Address;
46      Max_Len   : Natural;
47      Len       : out Natural;
48      Exclude_Min : System.Address := System.Null_Address;
49      Exclude_Max : System.Address := System.Null_Address;
50      Skip_Frames : Natural := 1)
51   is
52      type Tracebacks_Array is array (1 .. Max_Len) of Code_Loc;
53      pragma Suppress_Initialization (Tracebacks_Array);
54
55      M     : Machine_State;
56      Code  : Code_Loc;
57
58      Trace : Tracebacks_Array;
59      for Trace'Address use Traceback;
60
61      N_Skips  : Natural := 0;
62
63   begin
64      M := Allocate_Machine_State;
65      Set_Machine_State (M);
66
67      --  Skip the requested number of frames
68
69      loop
70         Code := Get_Code_Loc (M);
71         exit when Code = Null_Address or else N_Skips = Skip_Frames;
72
73         Pop_Frame (M);
74         N_Skips := N_Skips + 1;
75      end loop;
76
77      --  Now, record the frames outside the exclusion bounds, updating
78      --  the Len output value along the way.
79
80      Len := 0;
81      loop
82         Code := Get_Code_Loc (M);
83         exit when Code = Null_Address or else Len = Max_Len;
84
85         if Code < Exclude_Min or else Code > Exclude_Max then
86            Len := Len + 1;
87            Trace (Len) := Code;
88         end if;
89
90         Pop_Frame (M);
91      end loop;
92
93      Free_Machine_State (M);
94   end Call_Chain;
95
96   ------------------
97   -- C_Call_Chain --
98   ------------------
99
100   function C_Call_Chain
101     (Traceback : System.Address;
102      Max_Len   : Natural) return Natural
103   is
104      Val : Natural;
105   begin
106      Call_Chain (Traceback, Max_Len, Val);
107      return Val;
108   end C_Call_Chain;
109
110end System.Traceback;
111