2014-10-31 Hristian Kirtchev <kirtchev@adacore.com>
[official-gcc.git] / gcc / ada / s-traceb-mastop.adb
blob0ce7c50f933a0f02383c76e45e98878a8e2302a3
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-2014, 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 ------------------------------------------------------------------------------
32 -- This version uses System.Machine_State_Operations routines
34 with System.Machine_State_Operations;
36 package body System.Traceback is
38 use System.Machine_State_Operations;
40 -- procedure Call_Chain
41 -- (Traceback : System.Address;
42 -- Max_Len : Natural;
43 -- Len : out Natural;
44 -- Exclude_Min : System.Address := System.Null_Address;
45 -- Exclude_Max : System.Address := System.Null_Address;
46 -- Skip_Frames : Natural := 1);
47 -- -- Same as the exported version, but takes Traceback as an Address
48 -- ???See declaration in the spec for why this is temporarily commented out.
50 ----------------
51 -- Call_Chain --
52 ----------------
54 procedure Call_Chain
55 (Traceback : System.Address;
56 Max_Len : Natural;
57 Len : out Natural;
58 Exclude_Min : System.Address := System.Null_Address;
59 Exclude_Max : System.Address := System.Null_Address;
60 Skip_Frames : Natural := 1)
62 type Tracebacks_Array is array (1 .. Max_Len) of Code_Loc;
63 pragma Suppress_Initialization (Tracebacks_Array);
65 M : Machine_State;
66 Code : Code_Loc;
68 Trace : Tracebacks_Array;
69 for Trace'Address use Traceback;
71 N_Skips : Natural := 0;
73 begin
74 M := Allocate_Machine_State;
75 Set_Machine_State (M);
77 -- Skip the requested number of frames
79 loop
80 Code := Get_Code_Loc (M);
81 exit when Code = Null_Address or else N_Skips = Skip_Frames;
83 Pop_Frame (M);
84 N_Skips := N_Skips + 1;
85 end loop;
87 -- Now, record the frames outside the exclusion bounds, updating
88 -- the Len output value along the way.
90 Len := 0;
91 loop
92 Code := Get_Code_Loc (M);
93 exit when Code = Null_Address or else Len = Max_Len;
95 if Code < Exclude_Min or else Code > Exclude_Max then
96 Len := Len + 1;
97 Trace (Len) := Code;
98 end if;
100 Pop_Frame (M);
101 end loop;
103 Free_Machine_State (M);
104 end Call_Chain;
106 procedure Call_Chain
107 (Traceback : in out System.Traceback_Entries.Tracebacks_Array;
108 Max_Len : Natural;
109 Len : out Natural;
110 Exclude_Min : System.Address := System.Null_Address;
111 Exclude_Max : System.Address := System.Null_Address;
112 Skip_Frames : Natural := 1)
114 begin
115 Call_Chain
116 (Traceback'Address, Max_Len, Len,
117 Exclude_Min, Exclude_Max,
119 -- Skip one extra frame to skip the other Call_Chain entry as well
121 Skip_Frames => Skip_Frames + 1);
122 end Call_Chain;
124 ------------------
125 -- C_Call_Chain --
126 ------------------
128 function C_Call_Chain
129 (Traceback : System.Address;
130 Max_Len : Natural) return Natural
132 Val : Natural;
133 begin
134 Call_Chain (Traceback, Max_Len, Val);
135 return Val;
136 end C_Call_Chain;
138 end System.Traceback;