OSDN Git Service

2007-08-14 Tristan Gingold <gingold@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-tasdeb.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                  GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS                --
4 --                                                                          --
5 --                  S Y S T E M . T A S K I N G . D E B U G                 --
6 --                                                                          --
7 --                                  B o d y                                 --
8 --                                                                          --
9 --          Copyright (C) 1997-2007, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNARL 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. GNARL 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 GNARL; 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 other files  instantiate  generics from this --
23 -- unit, or you link  this unit with other files  to produce an executable, --
24 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
25 -- covered  by the  GNU  General  Public  License.  This exception does not --
26 -- however invalidate  any other reasons why  the executable file  might be --
27 -- covered by the  GNU Public License.                                      --
28 --                                                                          --
29 -- GNARL was developed by the GNARL team at Florida State University.       --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc.     --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 --  This package encapsulates all direct interfaces to task debugging services
35 --  that are needed by gdb with gnat mode.
36
37 --  Note : This file *must* be compiled with debugging information
38
39 --  Do not add any dependency to GNARL packages since this package is used
40 --  in both normal and restricted (ravenscar) environments.
41
42 with System.CRTL;
43 with System.Task_Primitives.Operations;
44 with Ada.Unchecked_Conversion;
45
46 package body System.Tasking.Debug is
47
48    package STPO renames System.Task_Primitives.Operations;
49
50    function To_Integer is new
51      Ada.Unchecked_Conversion (Task_Id, System.Address);
52
53    type Trace_Flag_Set is array (Character) of Boolean;
54
55    Trace_On : Trace_Flag_Set := ('A' .. 'Z' => False, others => True);
56
57    -----------------------
58    -- Local Subprograms --
59    -----------------------
60
61    procedure Write (Fd : Integer; S : String; Count : Integer);
62
63    procedure Put (S : String);
64    --  Display S on standard output
65
66    procedure Put_Line (S : String := "");
67    --  Display S on standard output with an additional line terminator
68
69    ------------------------
70    -- Continue_All_Tasks --
71    ------------------------
72
73    procedure Continue_All_Tasks is
74       C : Task_Id;
75
76       Dummy : Boolean;
77       pragma Unreferenced (Dummy);
78
79    begin
80       STPO.Lock_RTS;
81
82       C := All_Tasks_List;
83       while C /= null loop
84          Dummy := STPO.Continue_Task (C);
85          C := C.Common.All_Tasks_Link;
86       end loop;
87
88       STPO.Unlock_RTS;
89    end Continue_All_Tasks;
90
91    --------------------
92    -- Get_User_State --
93    --------------------
94
95    function Get_User_State return Long_Integer is
96    begin
97       return STPO.Self.User_State;
98    end Get_User_State;
99
100    ----------------
101    -- List_Tasks --
102    ----------------
103
104    procedure List_Tasks is
105       C : Task_Id;
106    begin
107       C := All_Tasks_List;
108
109       while C /= null loop
110          Print_Task_Info (C);
111          C := C.Common.All_Tasks_Link;
112       end loop;
113    end List_Tasks;
114
115    ------------------------
116    -- Print_Current_Task --
117    ------------------------
118
119    procedure Print_Current_Task is
120    begin
121       Print_Task_Info (STPO.Self);
122    end Print_Current_Task;
123
124    ---------------------
125    -- Print_Task_Info --
126    ---------------------
127
128    procedure Print_Task_Info (T : Task_Id) is
129       Entry_Call : Entry_Call_Link;
130       Parent     : Task_Id;
131
132    begin
133       if T = null then
134          Put_Line ("null task");
135          return;
136       end if;
137
138       Put (T.Common.Task_Image (1 .. T.Common.Task_Image_Len) & ": " &
139            Task_States'Image (T.Common.State));
140
141       Parent := T.Common.Parent;
142
143       if Parent = null then
144          Put (", parent: <none>");
145       else
146          Put (", parent: " &
147               Parent.Common.Task_Image (1 .. Parent.Common.Task_Image_Len));
148       end if;
149
150       Put (", prio:" & T.Common.Current_Priority'Img);
151
152       if not T.Callable then
153          Put (", not callable");
154       end if;
155
156       if T.Aborting then
157          Put (", aborting");
158       end if;
159
160       if T.Deferral_Level /= 0 then
161          Put (", abort deferred");
162       end if;
163
164       if T.Common.Call /= null then
165          Entry_Call := T.Common.Call;
166          Put (", serving:");
167
168          while Entry_Call /= null loop
169             Put (To_Integer (Entry_Call.Self)'Img);
170             Entry_Call := Entry_Call.Acceptor_Prev_Call;
171          end loop;
172       end if;
173
174       if T.Open_Accepts /= null then
175          Put (", accepting:");
176
177          for J in T.Open_Accepts'Range loop
178             Put (T.Open_Accepts (J).S'Img);
179          end loop;
180
181          if T.Terminate_Alternative then
182             Put (" or terminate");
183          end if;
184       end if;
185
186       if T.User_State /= 0 then
187          Put (", state:" & T.User_State'Img);
188       end if;
189
190       Put_Line;
191    end Print_Task_Info;
192
193    ---------
194    -- Put --
195    ---------
196
197    procedure Put (S : String) is
198    begin
199       Write (2, S, S'Length);
200    end Put;
201
202    --------------
203    -- Put_Line --
204    --------------
205
206    procedure Put_Line (S : String := "") is
207    begin
208       Write (2, S & ASCII.LF, S'Length + 1);
209    end Put_Line;
210
211    ----------------------
212    -- Resume_All_Tasks --
213    ----------------------
214
215    procedure Resume_All_Tasks (Thread_Self : OS_Interface.Thread_Id) is
216       C     : Task_Id;
217       Dummy : Boolean;
218       pragma Unreferenced (Dummy);
219
220    begin
221       STPO.Lock_RTS;
222       C := All_Tasks_List;
223
224       while C /= null loop
225          Dummy := STPO.Resume_Task (C, Thread_Self);
226          C := C.Common.All_Tasks_Link;
227       end loop;
228
229       STPO.Unlock_RTS;
230    end Resume_All_Tasks;
231
232    ---------------
233    -- Set_Trace --
234    ---------------
235
236    procedure Set_Trace (Flag  : Character; Value : Boolean := True) is
237    begin
238       Trace_On (Flag) := Value;
239    end Set_Trace;
240
241    --------------------
242    -- Set_User_State --
243    --------------------
244
245    procedure Set_User_State (Value : Long_Integer) is
246    begin
247       STPO.Self.User_State := Value;
248    end Set_User_State;
249
250    --------------------
251    -- Stop_All_Tasks --
252    --------------------
253
254    procedure Stop_All_Tasks is
255    begin
256       STPO.Stop_All_Tasks;
257    end Stop_All_Tasks;
258
259    -----------------------
260    -- Suspend_All_Tasks --
261    -----------------------
262
263    procedure Suspend_All_Tasks (Thread_Self : OS_Interface.Thread_Id) is
264       C     : Task_Id;
265       Dummy : Boolean;
266       pragma Unreferenced (Dummy);
267
268    begin
269       STPO.Lock_RTS;
270       C := All_Tasks_List;
271
272       while C /= null loop
273          Dummy := STPO.Suspend_Task (C, Thread_Self);
274          C := C.Common.All_Tasks_Link;
275       end loop;
276
277       STPO.Unlock_RTS;
278    end Suspend_All_Tasks;
279
280    ------------------------
281    -- Task_Creation_Hook --
282    ------------------------
283
284    procedure Task_Creation_Hook (Thread : OS_Interface.Thread_Id) is
285       pragma Inspection_Point (Thread);
286       --  gdb needs to access the thread parameter in order to implement
287       --  the multitask mode under VxWorks.
288
289    begin
290       null;
291    end Task_Creation_Hook;
292
293    ---------------------------
294    -- Task_Termination_Hook --
295    ---------------------------
296
297    procedure Task_Termination_Hook is
298    begin
299       null;
300    end Task_Termination_Hook;
301
302    -----------
303    -- Trace --
304    -----------
305
306    procedure Trace
307      (Self_Id  : Task_Id;
308       Msg      : String;
309       Flag     : Character;
310       Other_Id : Task_Id := null)
311    is
312    begin
313       if Trace_On (Flag) then
314          Put (To_Integer (Self_Id)'Img &
315               ':' & Flag & ':' &
316               Self_Id.Common.Task_Image (1 .. Self_Id.Common.Task_Image_Len) &
317               ':');
318
319          if Other_Id /= null then
320             Put (To_Integer (Other_Id)'Img & ':');
321          end if;
322
323          Put_Line (Msg);
324       end if;
325    end Trace;
326
327    -----------
328    -- Write --
329    -----------
330
331    procedure Write (Fd : Integer; S : String; Count : Integer) is
332       Discard : Integer;
333       pragma Unreferenced (Discard);
334    begin
335       Discard := System.CRTL.write (Fd, S (S'First)'Address, Count);
336       --  Is it really right to ignore write errors here ???
337    end Write;
338
339 end System.Tasking.Debug;