summaryrefslogtreecommitdiff
path: root/gcc/ada/s-tasdeb.adb
blob: c437117bc47c80d2a2eacfa707e5417edd27d471 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
------------------------------------------------------------------------------
--                                                                          --
--                 GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS              --
--                                                                          --
--                  S Y S T E M . T A S K I N G . D E B U G                 --
--                                                                          --
--                                  B o d y                                 --
--                                                                          --
--          Copyright (C) 1997-2002 Free Software Foundation, Inc.          --
--                                                                          --
-- GNARL is free software; you can  redistribute it  and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 2,  or (at your option) any later ver- --
-- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
-- for  more details.  You should have  received  a copy of the GNU General --
-- Public License  distributed with GNARL; see file COPYING.  If not, write --
-- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
-- MA 02111-1307, USA.                                                      --
--                                                                          --
-- As a special exception,  if other files  instantiate  generics from this --
-- unit, or you link  this unit with other files  to produce an executable, --
-- this  unit  does not  by itself cause  the resulting  executable  to  be --
-- covered  by the  GNU  General  Public  License.  This exception does not --
-- however invalidate  any other reasons why  the executable file  might be --
-- covered by the  GNU Public License.                                      --
--                                                                          --
-- GNARL was developed by the GNARL team at Florida State University.       --
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
--                                                                          --
------------------------------------------------------------------------------

--  This package encapsulates all direct interfaces to task debugging services
--  that are needed by gdb with gnat mode (1.13 and higher)

--  Note : This file *must* be compiled with debugging information

--  Do not add any dependency to GNARL packages since this package is used
--  in both normal and restricted (ravenscar) environments.

with System.Task_Info,
     System.Task_Primitives.Operations,
     Unchecked_Conversion;

package body System.Tasking.Debug is

   use Interfaces.C;

   package STPO renames System.Task_Primitives.Operations;

   type Integer_Address is mod 2 ** Standard'Address_Size;

   function "+" is new
     Unchecked_Conversion (Task_ID, Integer_Address);

   Hex_Address_Width : constant := (Standard'Address_Size / 4);

   Hex_Digits : constant array (0 .. Integer_Address'(15)) of Character :=
                  "0123456789abcdef";

   subtype Buf_Range is Integer range 1 .. 80;
   type Buf_Array is array (Buf_Range) of aliased Character;

   type Buffer is record
      Next  : Buf_Range := Buf_Range'First;
      Chars : Buf_Array := (Buf_Range => ' ');
   end record;

   type Buffer_Ptr is access all Buffer;

   type Trace_Flag_Set is array (Character) of Boolean;

   Trace_On : Trace_Flag_Set := ('A' .. 'Z' => False, others => True);

   -----------------------
   -- Local Subprograms --
   -----------------------

   procedure Put
     (T      : ST.Task_ID;
      Width  : Integer;
      Buffer : Buffer_Ptr);
   --  Put TCB pointer T, (coded in hexadecimal) into Buffer
   --  right-justified in Width characters.

   procedure Put
     (N      : Integer_Address;
      Width  : Integer;
      Buffer : Buffer_Ptr);
   --  Put N (coded in decimal) into Buf right-justified in Width
   --  characters starting at Buf (Next).

   procedure Put
     (S      : String;
      Width  : Integer;
      Buffer : Buffer_Ptr);
   --  Put string S into Buf left-justified in Width characters
   --  starting with space in Buf (Next), truncated as necessary.

   procedure Put
     (C      : Character;
      Buffer : Buffer_Ptr);
   --  Put character C into Buf, left-justified, starting at Buf (Next)

   procedure Space (Buffer : Buffer_Ptr);
   --  Increment Next, resulting in a space

   procedure Space
     (N      : Integer;
      Buffer : Buffer_Ptr);
   --  Increment Next by N, resulting in N spaces

   procedure Clear (Buffer : Buffer_Ptr);
   --  Clear Buf and reset Next to 1

   procedure Write_Buf (Buffer : Buffer_Ptr);
   --  Write contents of Buf (1 .. Next) to standard output

   -----------
   -- Clear --
   -----------

   procedure Clear (Buffer : Buffer_Ptr) is
      Next : Buf_Range renames Buffer.Next;
      Buf  : Buf_Array renames Buffer.Chars;

   begin
      Buf := (Buf_Range => ' ');
      Next := 1;
   end Clear;

   -----------
   -- Image --
   -----------

   function Image (T : ST.Task_ID) return String is
      Buf    : aliased Buffer;
      Result : String (1 .. Hex_Address_Width + 21);

      use type System.Task_Info.Task_Image_Type;

   begin
      Clear (Buf'Unchecked_Access);
      Put (T, Hex_Address_Width, Buf'Unchecked_Access);
      Put (':', Buf'Unchecked_Access);
      Put (Integer_Address (T.Serial_Number), 4, Buf'Unchecked_Access);
      Space (Buf'Unchecked_Access);

      if T.Common.Task_Image = null then
         Put ("", 15, Buf'Unchecked_Access);
      else
         Put (T.Common.Task_Image.all, 15, Buf'Unchecked_Access);
      end if;

      for J in Result'Range loop
         Result (J) := Buf.Chars (J);
      end loop;

      return Result;
   end Image;

   ----------------
   -- List_Tasks --
   ----------------

   procedure List_Tasks is
      C : ST.Task_ID;

   begin
      Print_Task_Info_Header;
      C := All_Tasks_List;

      while C /= null loop
         Print_Task_Info (C);
         C := C.Common.All_Tasks_Link;
      end loop;
   end List_Tasks;

   -----------------------
   -- Print_Accept_Info --
   -----------------------

   procedure Print_Accept_Info (T : ST.Task_ID) is
      Buf : aliased Buffer;

   begin
      if T.Open_Accepts = null then
         return;
      end if;

      Clear (Buf'Unchecked_Access);
      Space (10, Buf'Unchecked_Access);
      Put ("accepting:", 11, Buf'Unchecked_Access);

      for J in T.Open_Accepts.all'Range loop
         Put (Integer_Address (T.Open_Accepts (J).S), 3, Buf'Unchecked_Access);
      end loop;

      Write_Buf (Buf'Unchecked_Access);
   end Print_Accept_Info;

   ------------------------
   -- Print_Current_Task --
   ------------------------

   procedure Print_Current_Task is
   begin
      Print_Task_Info (STPO.Self);
   end Print_Current_Task;

   ---------------------
   -- Print_Task_Info --
   ---------------------

   procedure Print_Task_Info (T : ST.Task_ID) is
      Entry_Call : Entry_Call_Link;
      Buf        : aliased Buffer;

      use type System.Task_Info.Task_Image_Type;

   begin
      Clear (Buf'Unchecked_Access);
      Put (T, Hex_Address_Width, Buf'Unchecked_Access);
      Put (':', Buf'Unchecked_Access);
      Put (' ', Buf'Unchecked_Access);
      Put (':', Buf'Unchecked_Access);

      if T = null then
         Put (" null task", 10, Buf'Unchecked_Access);
         Write_Buf (Buf'Unchecked_Access);
         return;
      end if;

      Put (Integer_Address (T.Serial_Number), 4, Buf'Unchecked_Access);
      Space (Buf'Unchecked_Access);

      if T.Common.Task_Image = null then
         Put ("", 15, Buf'Unchecked_Access);
      else
         Put (T.Common.Task_Image.all, 15, Buf'Unchecked_Access);
      end if;

      Space (Buf'Unchecked_Access);
      Put (Task_States'Image (T.Common.State), 10, Buf'Unchecked_Access);
      Space (Buf'Unchecked_Access);

      if T.Callable then
         Put ('C', Buf'Unchecked_Access);
      else
         Space (Buf'Unchecked_Access);
      end if;

      if T.Open_Accepts /= null then
         Put ('A', Buf'Unchecked_Access);
      else
         Space (Buf'Unchecked_Access);
      end if;

      if T.Common.Call /= null then
         Put ('C', Buf'Unchecked_Access);
      else
         Space (Buf'Unchecked_Access);
      end if;

      if T.Terminate_Alternative then
         Put ('T', Buf'Unchecked_Access);
      else
         Space (Buf'Unchecked_Access);
      end if;

      if T.Aborting then
         Put ('A', Buf'Unchecked_Access);
      else
         Space (Buf'Unchecked_Access);
      end if;

      if T.Deferral_Level = 0 then
         Space (3, Buf'Unchecked_Access);
      else
         Put ('D', Buf'Unchecked_Access);
         if T.Deferral_Level < 0 then
            Put ("<0", 2, Buf'Unchecked_Access);
         elsif T.Deferral_Level > 1 then
            Put (Integer_Address (T.Deferral_Level), 2, Buf'Unchecked_Access);
         else
            Space (2, Buf'Unchecked_Access);
         end if;
      end if;

      Space (Buf'Unchecked_Access);
      Put (Integer_Address (T.Master_of_Task), 1, Buf'Unchecked_Access);
      Space (Buf'Unchecked_Access);
      Put (Integer_Address (T.Master_Within), 1, Buf'Unchecked_Access);
      Put (',', Buf'Unchecked_Access);
      Space (Buf'Unchecked_Access);
      Put (Integer_Address (T.Awake_Count), 1, Buf'Unchecked_Access);
      Space (Buf'Unchecked_Access);
      Put (Integer_Address (T.Alive_Count), 1, Buf'Unchecked_Access);
      Put (',', Buf'Unchecked_Access);
      Space (Buf'Unchecked_Access);
      Put (Integer_Address (T.ATC_Nesting_Level), 1, Buf'Unchecked_Access);
      Space (Buf'Unchecked_Access);
      Put (Integer_Address (T.Pending_ATC_Level), 1, Buf'Unchecked_Access);
      Put (',', Buf'Unchecked_Access);
      Space (Buf'Unchecked_Access);
      Put (Integer_Address (T.Common.Wait_Count), 1, Buf'Unchecked_Access);
      Put (',', Buf'Unchecked_Access);
      Space (Buf'Unchecked_Access);
      Put (Integer_Address (T.User_State), 1, Buf'Unchecked_Access);
      Write_Buf (Buf'Unchecked_Access);

      if T.Common.Call /= null then
         Entry_Call := T.Common.Call;
         Clear (Buf'Unchecked_Access);
         Space (10, Buf'Unchecked_Access);
         Put ("serving:", 8, Buf'Unchecked_Access);

         while Entry_Call /= null loop
            Put (Integer_Address
              (Entry_Call.Self.Serial_Number), 5, Buf'Unchecked_Access);
            Entry_Call := Entry_Call.Acceptor_Prev_Call;
         end loop;

         Write_Buf (Buf'Unchecked_Access);
      end if;

      Print_Accept_Info (T);
   end Print_Task_Info;

   ----------------------------
   -- Print_Task_Info_Header --
   ----------------------------

   procedure Print_Task_Info_Header is
      Buf : aliased Buffer;

   begin
      Clear (Buf'Unchecked_Access);
      Put ("TASK_ID", Hex_Address_Width, Buf'Unchecked_Access);
      Put (':', Buf'Unchecked_Access);
      Put ('F', Buf'Unchecked_Access);
      Put (':', Buf'Unchecked_Access);
      Put ("SERIAL_NUMBER", 4, Buf'Unchecked_Access);
      Space (Buf'Unchecked_Access);
      Put (" NAME", 15, Buf'Unchecked_Access);
      Put (" STATE", 10, Buf'Unchecked_Access);
      Space (11, Buf'Unchecked_Access);
      Put ("MAST", 5, Buf'Unchecked_Access);
      Put ("AWAK", 5, Buf'Unchecked_Access);
      Put ("ATC", 5, Buf'Unchecked_Access);
      Put ("WT", 3, Buf'Unchecked_Access);
      Put ("DBG", 3, Buf'Unchecked_Access);
      Write_Buf (Buf'Unchecked_Access);
   end Print_Task_Info_Header;

   ---------
   -- Put --
   ---------

   procedure Put
     (T      : ST.Task_ID;
      Width  : Integer;
      Buffer : Buffer_Ptr)
   is
      J     : Integer;
      X     : Integer_Address := +T;
      Next  : Buf_Range renames Buffer.Next;
      Buf   : Buf_Array renames Buffer.Chars;
      First : constant Integer := Next;
      Wdth  : Integer := Width;

   begin
      if Wdth > Buf'Last - Next then
         Wdth := Buf'Last - Next;
      end if;

      J := Next + (Wdth - 1);

      if X = 0 then
         Buf (J) := '0';

      else
         while X > 0 loop
            Buf (J) := Hex_Digits (X rem 16);
            J := J - 1;
            X := X / 16;

            --  Check for overflow

            if J < First and then X > 0 then
               Buf (J + 1) := '*';
               exit;
            end if;

         end loop;
      end if;

      Next := Next + Wdth;
   end Put;

   procedure Put
     (N      : Integer_Address;
      Width  : Integer;
      Buffer : Buffer_Ptr)
   is
      J     : Integer;
      X     : Integer_Address := N;
      Next  : Buf_Range renames Buffer.Next;
      Buf   : Buf_Array renames Buffer.Chars;
      First : constant Integer := Next;
      Wdth  : Integer := Width;

   begin
      if Wdth > Buf'Last - Next then
         Wdth := Buf'Last - Next;
      end if;

      J := Next + (Wdth - 1);

      if N = 0 then
         Buf (J) := '0';

      else
         while X > 0 loop
            Buf (J) := Hex_Digits (X rem 10);
            J := J - 1;
            X := X / 10;

            --  Check for overflow

            if J < First and then X > 0 then
               Buf (J + 1) := '*';
               exit;
            end if;
         end loop;
      end if;

      Next := Next + Wdth;
   end Put;

   procedure Put
     (S      : String;
      Width  : Integer;
      Buffer : Buffer_Ptr)
   is
      Next  : Buf_Range renames Buffer.Next;
      Buf   : Buf_Array renames Buffer.Chars;
      Bound : constant Integer := Integer'Min (Next + Width, Buf'Last);
      J     : Integer := Next;

   begin
      for K in S'Range loop

         --  Check overflow

         if J >= Bound then
            Buf (J - 1) := '*';
            exit;
         end if;

         Buf (J) := S (K);
         J := J + 1;
      end loop;

      Next := Bound;
   end Put;

   procedure Put
     (C      : Character;
      Buffer : Buffer_Ptr)
   is
      Next : Buf_Range renames Buffer.Next;
      Buf  : Buf_Array renames Buffer.Chars;

   begin
      if Next >= Buf'Last then
         Buf (Next) := '*';
      else Buf (Next) := C;
         Next := Next + 1;
      end if;
   end Put;

   ----------------------
   -- Resume_All_Tasks --
   ----------------------

   procedure Resume_All_Tasks (Thread_Self : OS_Interface.Thread_Id) is
      C : ST.Task_ID;
      R : Boolean;

   begin
      STPO.Lock_RTS;
      C := All_Tasks_List;

      while C /= null loop
         R := STPO.Resume_Task (C, Thread_Self);
         C := C.Common.All_Tasks_Link;
      end loop;

      STPO.Unlock_RTS;
   end Resume_All_Tasks;

   ----------
   -- Self --
   ----------

   function Self return Task_ID is
   begin
      return STPO.Self;
   end Self;

   ---------------
   -- Set_Trace --
   ---------------

   procedure Set_Trace
     (Flag  : Character;
      Value : Boolean := True)
   is
   begin
      Trace_On (Flag) := Value;
   end Set_Trace;

   --------------------
   -- Set_User_State --
   --------------------

   procedure Set_User_State (Value : Integer) is
   begin
      STPO.Self.User_State := Value;
   end Set_User_State;

   -----------
   -- Space --
   -----------

   procedure Space (Buffer : Buffer_Ptr) is
      Next : Buf_Range renames Buffer.Next;
      Buf  : Buf_Array renames Buffer.Chars;

   begin
      if Next >= Buf'Last then
         Buf (Next) := '*';
      else
         Next := Next + 1;
      end if;
   end Space;

   procedure Space
     (N      : Integer;
      Buffer : Buffer_Ptr)
   is
      Next : Buf_Range renames Buffer.Next;
      Buf  : Buf_Array renames Buffer.Chars;

   begin
      if Next + N > Buf'Last then
         Buf (Next) := '*';
      else
         Next := Next + N;
      end if;
   end Space;

   -----------------------
   -- Suspend_All_Tasks --
   -----------------------

   procedure Suspend_All_Tasks (Thread_Self : OS_Interface.Thread_Id) is
      C : ST.Task_ID;
      R : Boolean;

   begin
      STPO.Lock_RTS;
      C := All_Tasks_List;

      while C /= null loop
         R := STPO.Suspend_Task (C, Thread_Self);
         C := C.Common.All_Tasks_Link;
      end loop;

      STPO.Unlock_RTS;
   end Suspend_All_Tasks;

   ------------------------
   -- Task_Creation_Hook --
   ------------------------

   procedure Task_Creation_Hook (Thread : OS_Interface.Thread_Id) is
      pragma Inspection_Point (Thread);
      --  gdb needs to access the thread parameter in order to implement
      --  the multitask mode under VxWorks.

   begin
      null;
   end Task_Creation_Hook;

   ---------------------------
   -- Task_Termination_Hook --
   ---------------------------

   procedure Task_Termination_Hook is
   begin
      null;
   end Task_Termination_Hook;

   -----------
   -- Trace --
   -----------

   procedure Trace
     (Self_ID  : ST.Task_ID;
      Msg      : String;
      Other_ID : ST.Task_ID;
      Flag     : Character)
   is
      Buf : aliased Buffer;
      use type System.Task_Info.Task_Image_Type;

   begin
      if Trace_On (Flag) then
         Clear (Buf'Unchecked_Access);
         Put (Self_ID, Hex_Address_Width, Buf'Unchecked_Access);
         Put (':', Buf'Unchecked_Access);
         Put (Flag, Buf'Unchecked_Access);
         Put (':', Buf'Unchecked_Access);
         Put
           (Integer_Address (Self_ID.Serial_Number),
            4, Buf'Unchecked_Access);
         Space (Buf'Unchecked_Access);

         if Self_ID.Common.Task_Image = null then
            Put ("", 15, Buf'Unchecked_Access);
         else
            Put (Self_ID.Common.Task_Image.all, 15, Buf'Unchecked_Access);
         end if;

         Space (Buf'Unchecked_Access);

         if Other_ID /= null then
            Put
              (Integer_Address (Other_ID.Serial_Number),
               4, Buf'Unchecked_Access);
            Space (Buf'Unchecked_Access);
         end if;

         Put (Msg, Buf.Chars'Last - Buf.Next + 1, Buf'Unchecked_Access);
         Write_Buf (Buf'Unchecked_Access);
      end if;
   end Trace;

   procedure Trace
     (Self_ID : ST.Task_ID;
      Msg     : String;
      Flag    : Character)
   is
   begin
      Trace (Self_ID, Msg, null, Flag);
   end Trace;

   procedure Trace
     (Msg : String;
      Flag : Character)
   is
      Self_ID : constant ST.Task_ID := STPO.Self;

   begin
      Trace (Self_ID, Msg, null, Flag);
   end Trace;

   procedure Trace
     (Msg      : String;
      Other_ID : ST.Task_ID;
      Flag     : Character)
   is
      pragma Warnings (Off, Other_ID);

      Self_ID : constant ST.Task_ID := STPO.Self;

   begin
      Trace (Self_ID, Msg, null, Flag);
   end Trace;

   ---------------
   -- Write_Buf --
   ---------------

   procedure Write_Buf (Buffer : Buffer_Ptr) is
      Next : Buf_Range renames Buffer.Next;
      Buf  : Buf_Array renames Buffer.Chars;

      procedure put_char (C : Integer);
      pragma Import (C, put_char, "put_char");

   begin
      for J in 1 .. Next - 1 loop
         put_char (Character'Pos (Buf (J)));
      end loop;

      put_char (Character'Pos (ASCII.LF));
   end Write_Buf;

end System.Tasking.Debug;