1
1
import gdb
2
+ import gdb .printing
2
3
3
4
4
5
from gnatdbg .lists import (
18
19
from gnatdbg .utils import register_pretty_printers
19
20
20
21
21
- setup_done = False
22
- printers = None
22
+ _setup_done = False
23
+ _printers = None
23
24
24
25
25
- def setup ():
26
- global setup_done
27
- global printers
28
- if setup_done :
29
- return
30
-
31
- printers = GDBPrettyPrinters ('gnat-runtime' )
26
+ def create_printers (name = 'gnat-runtime' ):
27
+ """
28
+ Instantiate GDBPrettyPrinters with the given name and register all
29
+ pretty-printers for the GNAT runtime in it. Return this instance.
30
+ """
31
+ printers = GDBPrettyPrinters (name )
32
32
33
33
for printer in [
34
34
DoublyLinkedListPrinter ,
@@ -52,10 +52,43 @@ def setup():
52
52
]:
53
53
printers .append (printer )
54
54
55
- # Registers our printers only for objfiles that are Ada main entry points.
56
- def objfile_filter (objfile ):
57
- adainit = gdb .lookup_global_symbol ('adainit' or '_adainit' )
58
- return adainit is not None and adainit .symtab .objfile == objfile
59
- register_pretty_printers (printers , objfile_filter )
55
+ return printers
56
+
57
+
58
+ def setup (name = 'gnat-runtime' , globally = True ):
59
+ """
60
+ Instantiate pretty-printers for the GNAT runtime with the given name and
61
+ register them.
62
+
63
+ If `globally` is true, just register the pretty-printers globally. This is
64
+ the most simple way to make them work.
65
+
66
+ If `globally is false, only register them in objfiles that define the
67
+ 'adainit' symbol. On one hande, this will not make them work on shared
68
+ objects, but on the other hand this will avoid GDB the cost of looking them
69
+ up in objfiles that don't contain Ada. You can see this as an unsound
70
+ optimization.
71
+
72
+ This function does its work the first time it is called, but then does
73
+ nothing if it's called afterwards.
74
+ """
75
+ global _setup_done
76
+ global _printers
77
+ if _setup_done :
78
+ return
79
+
80
+ _printers = create_printers (name )
81
+
82
+ if globally :
83
+ gdb .printing .register_pretty_printer (None , _printers )
84
+
85
+ else :
86
+ # Registers our printers only for objfiles that are Ada main entry
87
+ # points.
88
+ def objfile_filter (objfile ):
89
+ adainit = gdb .lookup_global_symbol ('adainit' or '_adainit' )
90
+ return adainit is not None and adainit .symtab .objfile == objfile
91
+
92
+ register_pretty_printers (_printers , objfile_filter )
60
93
61
- setup_done = True
94
+ _setup_done = True
0 commit comments