Skip to content

Commit 9b80669

Browse files
committed
gnatdbg.setup: by default, register pretty-printers globally
Change-Id: Ibb632f5a3de4e0708db94850b2b74806bc036247 For GitHub issue AdaCore#2 no-tn-check
1 parent e86cfb7 commit 9b80669

File tree

2 files changed

+50
-16
lines changed

2 files changed

+50
-16
lines changed

gnatdbg/__init__.py

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import gdb
2+
import gdb.printing
23

34

45
from gnatdbg.lists import (
@@ -18,17 +19,16 @@
1819
from gnatdbg.utils import register_pretty_printers
1920

2021

21-
setup_done = False
22-
printers = None
22+
_setup_done = False
23+
_printers = None
2324

2425

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)
3232

3333
for printer in [
3434
DoublyLinkedListPrinter,
@@ -52,10 +52,43 @@ def setup():
5252
]:
5353
printers.append(printer)
5454

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)
6093

61-
setup_done = True
94+
_setup_done = True

gnatdbg/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import gdb
2+
import gdb.printing
23

34

45
gdb_code_names = {
@@ -22,7 +23,7 @@ def register_pretty_printers(printers, objfile_filter=lambda objfile: True):
2223

2324
def register(objfile):
2425
if objfile_filter(objfile):
25-
objfile.pretty_printers.append(printers)
26+
gdb.printing.register_pretty_printer(objfile, printers)
2627

2728
# Give a chance to register pretty-printers for all objfiles already
2829
# loaded...

0 commit comments

Comments
 (0)