Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/etc/debugger_tester/gdb/check_gdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,12 @@ def var_matches(

value_ok = var.value == expected.value

work_list = [c for _i, c in get_children(valobj)]
work_list = [c for _i, c in get_children(make_visualizer(valobj))]
child_types_ok = Result.Ok

while len(work_list) != 0:
child = work_list.pop()
work_list.extend([c for _i, c in get_children(child)])
work_list.extend([c for _i, c in get_children(make_visualizer(child))])

# similar to the above, child type mismatches aren't super important if the providers still
# work fine. We do still want to output them so they're visible if something else fails
Expand Down
59 changes: 35 additions & 24 deletions src/etc/debugger_tester/gdb/from_gdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,39 @@
Variable,
)


# For whatever reason, gdb doesn't export several of the modules it distributes. It adds its own
# top level module to the search path, but that doesn't let us import the unexported parts inside
# the `gdb` package. To sidestep that, we can manually invoke Python's import pipeline on the
# desired file.
def import_from_path(module_name, file_path) -> ModuleType:
spec = importlib.util.spec_from_file_location(module_name, file_path)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)
return module


gdb_printing = import_from_path("gdb_printing", gdb.PYTHONDIR + "/gdb/printing.py")

make_visualizer: Callable[[gdb.Value], "gdb._PrettyPrinter"] = (
gdb_printing.make_visualizer
)
"""If a pretty printer exists that handles this value's type, returns the pretty printer
instantiated with the given value. If none can be found, returns a "NoOp" pretty printer with the
same interface as a custom pretty printer.
"""
try:
from gdb.printing import make_visualizer
except: # noqa: E722
pass
# (Walnut356): The below was true for older versions of gdb I think? In newer versions, this

@Kobzol Kobzol Nov 14, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wow. I never saw this hack. That's nasty.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every time I have to reach for importlib I rub my hands together like a dastardly villain lmao It's absolutely the most cursed module in python

# workaround results in an error during import:
# "pretty-printer already registered: builtin"
# so we just try both

# For whatever reason, gdb doesn't export several of the modules it distributes. It adds its own
# top level module to the search path, but that doesn't let us import the unexported parts
# inside the `gdb` package. To sidestep that, we can manually invoke Python's import pipeline on
# the desired file.
def import_from_path(module_name, file_path) -> ModuleType:
spec = importlib.util.spec_from_file_location(module_name, file_path)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)
return module

# `printing.py` automatically registers `builtin` providers upon import. Reregistering the same
# provider throws an exception, so we yoink it out just before the import.
gdb.pretty_printers = [p for p in gdb.pretty_printers if p.name != "builtin"]

gdb_printing = import_from_path("gdb_printing", gdb.PYTHONDIR + "/gdb/printing.py")

make_visualizer: Callable[[gdb.Value], "gdb._PrettyPrinter"] = (
gdb_printing.make_visualizer
)
"""If a pretty printer exists that handles this value's type, returns the pretty printer
instantiated with the given value. If none can be found, returns a "NoOp" pretty printer with
the same interface as a custom pretty printer.
"""


# Same trick as from_lldb.TypeClass/from_lldb.BasicType
Expand Down Expand Up @@ -106,7 +117,7 @@ def type_from_gdb(ty: gdb.Type) -> Type:


def field_from_gdb(field: gdb.Field) -> Field:
return Field(field.name, field.type.name, field.bitpos // 8)
return Field(field.name, get_type_name(field.type), field.bitpos // 8)


def variable_from_gdb(var: gdb.Value) -> Variable:
Expand Down Expand Up @@ -177,7 +188,7 @@ def get_children(obj) -> List[Tuple[str, gdb.Value]]:
if children is None:
return []

return children()
return list(children())


def child_from_gdb(ident: str, child: gdb.Value) -> Child:
Expand Down
13 changes: 7 additions & 6 deletions src/etc/debugger_tester/gdb/gdb_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ def __init__(self):
super().__init__("repr", gdb.COMMAND_OBSCURE)

def invoke(self, argument: str, from_tty: bool):
from ..common import Result
from .check_gdb import check
try:
from ..common import Result
from .check_gdb import check

print(f"(gdb) repr {argument}")
print(f"(gdb) repr {argument}")

global REPR_COMMAND_RUN
REPR_COMMAND_RUN = True

global REPR_COMMAND_RUN
REPR_COMMAND_RUN = True
try:
if check(argument) == Result.Mismatch:
global REPR_ERROR
REPR_ERROR = True
Expand Down
Loading
Loading