Debian Codegraph @

gcc/ada/libgnat/s-trasym__dwarf.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- S Y S T E M . T R A C E B A C K . S Y M B O L I C --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1999-2025, 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 -- Run-time symbolic traceback support for targets using DWARF debug data
33
34 with Ada.Unchecked_Deallocation;
35
36 with Ada.Exceptions.Traceback; use Ada.Exceptions.Traceback;
37 with Ada.Containers.Generic_Array_Sort;
38
39 with System.Address_To_Access_Conversions;
40 with System.Soft_Links;
41 with System.CRTL;
42 with System.Dwarf_Lines;
43 with System.Exception_Traces;
44 with System.Standard_Library;
45 with System.Traceback_Entries;
46 with System.Strings;
47 with System.Bounded_Strings;
48 with Interfaces.C;
49
50 package body System.Traceback.Symbolic is
51
52 use System.Bounded_Strings;
53 use System.Dwarf_Lines;
54
55 subtype Big_String is String (Positive);
56 -- To deal with C strings
57
58 package Big_String_Conv is new System.Address_To_Access_Conversions
59 (Big_String);
60
61 type Module_Cache;
62 type Module_Cache_Acc is access all Module_Cache;
63
64 type Module_Cache is record
65 Name : Strings.String_Access;
66 -- Name of the module
67
68 C : Dwarf_Context (In_Exception => True);
69 -- Context to symbolize an address within this module
70
71 Chain : Module_Cache_Acc;
72 end record;
73
74 procedure Free is new Ada.Unchecked_Deallocation
75 (Module_Cache,
76 Module_Cache_Acc);
77
78 Cache_Chain : Module_Cache_Acc;
79 -- Simply linked list of modules
80
81 type Module_Array is array (Natural range <>) of Module_Cache_Acc;
82 type Module_Array_Acc is access Module_Array;
83
84 Modules_Cache : Module_Array_Acc;
85 -- Sorted array of cached modules (if not null)
86
87 Exec_Module : aliased Module_Cache;
88 -- Context for the executable
89
90 type Init_State is (Uninitialized, Initialized, Failed);
91 Exec_Module_State : Init_State := Uninitialized;
92 -- How Exec_Module is initialized
93
94 procedure Init_Exec_Module;
95 -- Initialize Exec_Module if not already initialized
96
97 function Symbolic_Traceback
98 (Traceback : System.Traceback_Entries.Tracebacks_Array;
99 Suppress_Hex : Boolean) return String;
100 function Symbolic_Traceback
101 (E : Ada.Exceptions.Exception_Occurrence;
102 Suppress_Hex : Boolean) return String;
103 -- Suppress_Hex means do not print any hexadecimal addresses, even if the
104 -- symbol is not available.
105
106 function Lt (Left, Right : Module_Cache_Acc) return Boolean;
107 -- Sort function for Module_Cache
108
109 procedure Init_Module
110 (Module : out Module_Cache;
111 Success : out Boolean;
112 Module_Name : String;
113 Load_Address : Address := Null_Address);
114 -- Initialize Module
115
116 procedure Close_Module (Module : in out Module_Cache);
117 -- Finalize Module
118
119 function Value (Item : System.Address) return String;
120 -- Return the String contained in Item, up until the first NUL character
121
122 pragma Warnings (Off, "*Add_Module_To_Cache*");
123 procedure Add_Module_To_Cache (Module_Name : String;
124 Load_Address : System.Address);
125 -- To be called by Build_Cache_For_All_Modules to add a new module to the
126 -- list. May not be referenced.
127
128 package Module_Name is
129
130 procedure Build_Cache_For_All_Modules;
131 -- Create the cache for all current modules
132
133 function Get (Addr : System.Address;
134 Load_Addr : access System.Address) return String;
135 -- Returns the module name for the given address Addr, or an empty
136 -- string for the main executable. Load_Addr is set to the shared
137 -- library load address if this information is available, or to
138 -- System.Null_Address otherwise.
139
140 function Is_Supported return Boolean;
141 pragma Inline (Is_Supported);
142 -- Returns True if Module_Name is supported, so if the traceback is
143 -- supported for shared libraries.
144
145 end Module_Name;
146
147 package body Module_Name is separate;
148
149 function Executable_Name return String;
150 -- Returns the executable name as reported by argv[0]. If gnat_argv not
151 -- initialized, return an empty string. If the argv[0] executable is not
152 -- found in the PATH, return it unresolved.
153
154 function Get_Executable_Load_Address return System.Address;
155 pragma Import
156 (C,
157 Get_Executable_Load_Address,
158 "__gnat_get_executable_load_address");
159 -- Get the load address of the executable, or Null_Address if not known
160
161 procedure Hexa_Traceback
162 (Traceback : Tracebacks_Array;
163 Suppress_Hex : Boolean;
164 Res : in out Bounded_String);
165 -- Non-symbolic traceback (simply write addresses in hexa)
166
167 procedure Symbolic_Traceback_No_Lock
168 (Traceback : Tracebacks_Array;
169 Suppress_Hex : Boolean;
170 Res : in out Bounded_String);
171 -- Like the public Symbolic_Traceback_No_Lock except there is no provision
172 -- against concurrent accesses.
173
174 procedure Module_Symbolic_Traceback
175 (Traceback : Tracebacks_Array;
176 Module : Module_Cache;
177 Suppress_Hex : Boolean;
178 Res : in out Bounded_String);
179 -- Returns the Traceback for a given module
180
181 procedure Multi_Module_Symbolic_Traceback
182 (Traceback : Tracebacks_Array;
183 Suppress_Hex : Boolean;
184 Res : in out Bounded_String);
185 -- Build string containing symbolic traceback for the given call chain
186
187 procedure Multi_Module_Symbolic_Traceback
188 (Traceback : Tracebacks_Array;
189 Module : Module_Cache;
190 Suppress_Hex : Boolean;
191 Res : in out Bounded_String);
192 -- Likewise but using Module
193
194 Max_String_Length : constant := 4096;
195 -- Arbitrary limit on Bounded_Str length
196
197 -----------
198 -- Value --
199 -----------
200
201 function Value (Item : System.Address) return String is
202 begin
203 if Item /= Null_Address then
204 for J in Big_String'Range loop
205 if Big_String_Conv.To_Pointer (Item) (J) = ASCII.NUL then
206 return Big_String_Conv.To_Pointer (Item) (1 .. J - 1);
207 end if;
208 end loop;
209 end if;
210
211 return "";
212 end Value;
213
214 -------------------------
215 -- Add_Module_To_Cache --
216 -------------------------
217
218 procedure Add_Module_To_Cache (Module_Name : String;
219 Load_Address : System.Address)
220 is
221 Module : Module_Cache_Acc;
222 Success : Boolean;
223 begin
224 Module := new Module_Cache;
225 Init_Module (Module.all, Success, Module_Name, Load_Address);
226 if not Success then
227 Free (Module);
228 return;
229 end if;
230 Module.Chain := Cache_Chain;
231 Cache_Chain := Module;
232 end Add_Module_To_Cache;
233
234 ----------------------
235 -- Init_Exec_Module --
236 ----------------------
237
238 procedure Init_Exec_Module is
239 begin
240 if Exec_Module_State = Uninitialized then
241 declare
242 Exec_Path : constant String := Executable_Name;
243 Exec_Load : constant Address := Get_Executable_Load_Address;
244 Success : Boolean;
245 begin
246 Init_Module (Exec_Module, Success, Exec_Path, Exec_Load);
247
248 if Success then
249 Exec_Module_State := Initialized;
250 else
251 Exec_Module_State := Failed;
252 end if;
253 end;
254 end if;
255 end Init_Exec_Module;
256
257 --------
258 -- Lt --
259 --------
260
261 function Lt (Left, Right : Module_Cache_Acc) return Boolean is
262 begin
263 return Low_Address (Left.C) < Low_Address (Right.C);
264 end Lt;
265
266 -----------------------------
267 -- Module_Cache_Array_Sort --
268 -----------------------------
269
270 procedure Module_Cache_Array_Sort is new Ada.Containers.Generic_Array_Sort
271 (Natural,
272 Module_Cache_Acc,
273 Module_Array,
274 Lt);
275
276 ------------------
277 -- Enable_Cache --
278 ------------------
279
280 procedure Enable_Cache (Include_Modules : Boolean := False) is
281 begin
282 -- Can be called at most once
283 if Cache_Chain /= null then
284 return;
285 end if;
286
287 -- Add all modules
288 Init_Exec_Module;
289
290 if Exec_Module_State = Failed then
291 raise Program_Error with
292 "cannot enable cache, executable state initialization failed.";
293 end if;
294
295 Cache_Chain := Exec_Module'Access;
296
297 if Include_Modules then
298 Module_Name.Build_Cache_For_All_Modules;
299 end if;
300
301 -- Build and fill the array of modules
302 declare
303 Count : Natural;
304 Module : Module_Cache_Acc;
305 begin
306 for Phase in 1 .. 2 loop
307 Count := 0;
308 Module := Cache_Chain;
309 while Module /= null loop
310 Count := Count + 1;
311
312 if Phase = 1 then
313 Enable_Cache (Module.C);
314 else
315 Modules_Cache (Count) := Module;
316 end if;
317 Module := Module.Chain;
318 end loop;
319
320 if Phase = 1 then
321 Modules_Cache := new Module_Array (1 .. Count);
322 end if;
323 end loop;
324 end;
325
326 -- Sort the array
327 Module_Cache_Array_Sort (Modules_Cache.all);
328 end Enable_Cache;
329
330 ---------------------
331 -- Executable_Name --
332 ---------------------
333
334 function Executable_Name return String is
335 -- We have to import gnat_argv as an Address to match the type of
336 -- gnat_argv in the binder generated file. Otherwise, we get spurious
337 -- warnings about type mismatch when LTO is turned on.
338
339 Gnat_Argv : System.Address;
340 pragma Import (C, Gnat_Argv, "gnat_argv");
341
342 type Argv_Array is array (0 .. 0) of System.Address;
343 package Conv is new System.Address_To_Access_Conversions (Argv_Array);
344
345 function locate_exec_on_path
346 (A : System.Address;
347 Current_Dir_On_Win : Interfaces.C.int) return System.Address;
348 pragma Import (C, locate_exec_on_path, "__gnat_locate_exec_on_path");
349
350 begin
351 if Gnat_Argv = Null_Address then
352 return "";
353 end if;
354
355 -- See if we can resolve argv[0] to a full path (to a file that we will
356 -- be able to open). If the resolution fails, we were probably spawned
357 -- by an imprecise exec call, typically passing a mere file name as
358 -- argv[0] for a program in the current directory with '.' not on PATH.
359 -- Best we can do is fallback to argv[0] unchanged in this case. If we
360 -- fail opening that downstream, we'll just bail out.
361
362 declare
363 Argv0 : constant System.Address :=
364 Conv.To_Pointer (Gnat_Argv) (0);
365
366 Resolved_Argv0 : constant System.Address :=
367 locate_exec_on_path (Argv0, 0);
368
369 Exe_Argv : constant System.Address :=
370 (if Resolved_Argv0 /= System.Null_Address
371 then Resolved_Argv0
372 else Argv0);
373
374 Result : constant String := Value (Exe_Argv);
375
376 begin
377 -- The buffer returned by locate_exec_on_path was allocated using
378 -- malloc and we should release this memory.
379
380 if Resolved_Argv0 /= Null_Address then
381 System.CRTL.free (Resolved_Argv0);
382 end if;
383
384 return Result;
385 end;
386 end Executable_Name;
387
388 ------------------
389 -- Close_Module --
390 ------------------
391
392 procedure Close_Module (Module : in out Module_Cache) is
393 begin
394 Close (Module.C);
395 Strings.Free (Module.Name);
396 end Close_Module;
397
398 -----------------
399 -- Init_Module --
400 -----------------
401
402 procedure Init_Module
403 (Module : out Module_Cache;
404 Success : out Boolean;
405 Module_Name : String;
406 Load_Address : Address := Null_Address)
407 is
408 begin
409 -- Early return if the module is not known
410
411 if Module_Name = "" then
412 Success := False;
413 return;
414 end if;
415
416 Open (Module_Name, Module.C, Success);
417
418 -- If a module can't be opened just return now, we just cannot give more
419 -- information in this case.
420
421 if not Success then
422 return;
423 end if;
424
425 Set_Load_Address (Module.C, Load_Address);
426
427 Module.Name := new String'(Module_Name);
428 end Init_Module;
429
430 -------------------------------
431 -- Module_Symbolic_Traceback --
432 -------------------------------
433
434 procedure Module_Symbolic_Traceback
435 (Traceback : Tracebacks_Array;
436 Module : Module_Cache;
437 Suppress_Hex : Boolean;
438 Res : in out Bounded_String)
439 is
440 Success : Boolean;
441 begin
442 if Symbolic.Module_Name.Is_Supported then
443 Append (Res, '[');
444 Append (Res, Module.Name.all);
445 Append (Res, ']' & ASCII.LF);
446 end if;
447
448 Dwarf_Lines.Symbolic_Traceback
449 (Module.C,
450 Traceback,
451 Suppress_Hex,
452 Success,
453 Res);
454
455 if not Success then
456 Hexa_Traceback (Traceback, Suppress_Hex, Res);
457 end if;
458
459 -- We must not allow an unhandled exception here, since this function
460 -- may be installed as a decorator for all automatic exceptions.
461
462 exception
463 when others =>
464 return;
465 end Module_Symbolic_Traceback;
466
467 -------------------------------------
468 -- Multi_Module_Symbolic_Traceback --
469 -------------------------------------
470
471 procedure Multi_Module_Symbolic_Traceback
472 (Traceback : Tracebacks_Array;
473 Suppress_Hex : Boolean;
474 Res : in out Bounded_String)
475 is
476 F : constant Natural := Traceback'First;
477 begin
478 if Traceback'Length = 0 or else Is_Full (Res) then
479 return;
480 end if;
481
482 if Modules_Cache /= null then
483 -- Search in the cache
484
485 declare
486 Addr : constant Address := Traceback (F);
487 Hi, Lo, Mid : Natural;
488 begin
489 Lo := Modules_Cache'First;
490 Hi := Modules_Cache'Last;
491 while Lo <= Hi loop
492 Mid := (Lo + Hi) / 2;
493 if Addr < Low_Address (Modules_Cache (Mid).C) then
494 Hi := Mid - 1;
495 elsif Is_Inside (Modules_Cache (Mid).C, Addr) then
496 Multi_Module_Symbolic_Traceback
497 (Traceback,
498 Modules_Cache (Mid).all,
499 Suppress_Hex,
500 Res);
501 return;
502 else
503 Lo := Mid + 1;
504 end if;
505 end loop;
506
507 -- Not found
508 Hexa_Traceback (Traceback (F .. F), Suppress_Hex, Res);
509 Multi_Module_Symbolic_Traceback
510 (Traceback (F + 1 .. Traceback'Last),
511 Suppress_Hex,
512 Res);
513 end;
514 else
515
516 -- First try the executable
517 if Is_Inside (Exec_Module.C, Traceback (F)) then
518 Multi_Module_Symbolic_Traceback
519 (Traceback,
520 Exec_Module,
521 Suppress_Hex,
522 Res);
523 return;
524 end if;
525
526 -- Otherwise, try a shared library
527 declare
528 Load_Addr : aliased System.Address;
529 M_Name : constant String :=
530 Module_Name.Get (Addr => Traceback (F),
531 Load_Addr => Load_Addr'Access);
532 Module : Module_Cache;
533 Success : Boolean;
534 begin
535 Init_Module (Module, Success, M_Name, Load_Addr);
536 if Success then
537 Multi_Module_Symbolic_Traceback
538 (Traceback,
539 Module,
540 Suppress_Hex,
541 Res);
542 Close_Module (Module);
543 else
544 -- Module not found
545 Hexa_Traceback (Traceback (F .. F), Suppress_Hex, Res);
546 Multi_Module_Symbolic_Traceback
547 (Traceback (F + 1 .. Traceback'Last),
548 Suppress_Hex,
549 Res);
550 end if;
551 end;
552 end if;
553 end Multi_Module_Symbolic_Traceback;
554
555 procedure Multi_Module_Symbolic_Traceback
556 (Traceback : Tracebacks_Array;
557 Module : Module_Cache;
558 Suppress_Hex : Boolean;
559 Res : in out Bounded_String)
560 is
561 Pos : Positive;
562 begin
563 -- Will symbolize the first address...
564
565 Pos := Traceback'First + 1;
566
567 -- ... and all addresses in the same module
568
569 Same_Module :
570 loop
571 exit Same_Module when Pos > Traceback'Last;
572
573 -- Get address to check for corresponding module name
574
575 exit Same_Module when not Is_Inside (Module.C, Traceback (Pos));
576
577 Pos := Pos + 1;
578 end loop Same_Module;
579
580 Module_Symbolic_Traceback
581 (Traceback (Traceback'First .. Pos - 1),
582 Module,
583 Suppress_Hex,
584 Res);
585 Multi_Module_Symbolic_Traceback
586 (Traceback (Pos .. Traceback'Last),
587 Suppress_Hex,
588 Res);
589 end Multi_Module_Symbolic_Traceback;
590
591 --------------------
592 -- Hexa_Traceback --
593 --------------------
594
595 procedure Hexa_Traceback
596 (Traceback : Tracebacks_Array;
597 Suppress_Hex : Boolean;
598 Res : in out Bounded_String)
599 is
600 use System.Traceback_Entries;
601 begin
602 if Suppress_Hex then
603 Append (Res, "...");
604 Append (Res, ASCII.LF);
605 else
606 for J in Traceback'Range loop
607 Append_Address (Res, PC_For (Traceback (J)));
608 Append (Res, ASCII.LF);
609 end loop;
610 end if;
611 end Hexa_Traceback;
612
613 --------------------------------
614 -- Symbolic_Traceback_No_Lock --
615 --------------------------------
616
617 procedure Symbolic_Traceback_No_Lock
618 (Traceback : Tracebacks_Array;
619 Suppress_Hex : Boolean;
620 Res : in out Bounded_String)
621 is
622 begin
623 if Symbolic.Module_Name.Is_Supported then
624 Multi_Module_Symbolic_Traceback (Traceback, Suppress_Hex, Res);
625 else
626 if Exec_Module_State = Failed then
627 Append (Res, "Call stack traceback locations:" & ASCII.LF);
628 Hexa_Traceback (Traceback, Suppress_Hex, Res);
629 else
630 Module_Symbolic_Traceback
631 (Traceback,
632 Exec_Module,
633 Suppress_Hex,
634 Res);
635 end if;
636 end if;
637 end Symbolic_Traceback_No_Lock;
638
639 ------------------------
640 -- Symbolic_Traceback --
641 ------------------------
642
643 LDAD_Header : constant String := "Load address: ";
644 -- Copied from Ada.Exceptions.Exception_Data
645
646 function Symbolic_Traceback
647 (Traceback : Tracebacks_Array;
648 Suppress_Hex : Boolean) return String
649 is
650 Load_Address : constant Address := Get_Executable_Load_Address;
651 Res : Bounded_String (Max_Length => Max_String_Length);
652
653 begin
654 System.Soft_Links.Lock_Task.all;
655 Init_Exec_Module;
656 if Load_Address /= Null_Address then
657 Append (Res, LDAD_Header);
658 Append_Address (Res, Load_Address);
659 Append (Res, ASCII.LF);
660 end if;
661 Symbolic_Traceback_No_Lock (Traceback, Suppress_Hex, Res);
662 System.Soft_Links.Unlock_Task.all;
663
664 return To_String (Res);
665
666 exception
667 when others =>
668 System.Soft_Links.Unlock_Task.all;
669 raise;
670 end Symbolic_Traceback;
671
672 function Symbolic_Traceback
673 (Traceback : System.Traceback_Entries.Tracebacks_Array) return String is
674 begin
675 return Symbolic_Traceback (Traceback, Suppress_Hex => False);
676 end Symbolic_Traceback;
677
678 function Symbolic_Traceback_No_Hex
679 (Traceback : System.Traceback_Entries.Tracebacks_Array) return String is
680 begin
681 return Symbolic_Traceback (Traceback, Suppress_Hex => True);
682 end Symbolic_Traceback_No_Hex;
683
684 function Symbolic_Traceback
685 (E : Ada.Exceptions.Exception_Occurrence;
686 Suppress_Hex : Boolean) return String
687 is
688 begin
689 return Symbolic_Traceback
690 (Ada.Exceptions.Traceback.Tracebacks (E),
691 Suppress_Hex);
692 end Symbolic_Traceback;
693
694 function Symbolic_Traceback
695 (E : Ada.Exceptions.Exception_Occurrence) return String
696 is
697 begin
698 return Symbolic_Traceback (E, Suppress_Hex => False);
699 end Symbolic_Traceback;
700
701 function Symbolic_Traceback_No_Hex
702 (E : Ada.Exceptions.Exception_Occurrence) return String is
703 begin
704 return Symbolic_Traceback (E, Suppress_Hex => True);
705 end Symbolic_Traceback_No_Hex;
706
707 Exception_Tracebacks_Symbolic : constant Integer;
708 pragma Import
709 (C,
710 Exception_Tracebacks_Symbolic,
711 "__gl_exception_tracebacks_symbolic");
712 -- Boolean indicating whether symbolic tracebacks should be generated.
713
714 use Standard_Library;
715 begin
716 -- If this version of this package is available, and the binder switch -Es
717 -- was given, then we want to use this as the decorator by default, and we
718 -- want to turn on tracing for Unhandled_Raise_In_Main. Note that the user
719 -- cannot have already set Exception_Trace, because the runtime library is
720 -- elaborated before user-defined code.
721
722 if Exception_Tracebacks_Symbolic /= 0 then
723 Exception_Traces.Set_Trace_Decorator (Symbolic_Traceback'Access);
724 pragma Assert (Exception_Trace = RM_Convention);
725 Exception_Trace := Unhandled_Raise_In_Main;
726 end if;
727 end System.Traceback.Symbolic;