Skip to content

Commit 8b16b9f

Browse files
committed
Move gdb-related utility functions into their own module.
The decorable_printer.py module will soon require gdb_lookup_value() and moving the function into a separate module avoids a circular import.
1 parent ae4e88e commit 8b16b9f

6 files changed

+52
-30
lines changed

gdbmongo/abseil_printers.py

+1-19
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,10 @@
2020
import gdb
2121

2222
from gdbmongo import stdlib_printers
23+
from gdbmongo.gdbutil import gdb_resolve_type
2324
from gdbmongo.printer_protocol import PrettyPrinterProtocol, SupportsDisplayHint
2425

2526

26-
def gdb_resolve_type(typ: gdb.Type, /) -> gdb.Type:
27-
"""Look up the name of a C++ type with any typedefs, pointers, and references stripped.
28-
29-
This function is useful in contexts where template arguments can be pointers because GDB may not
30-
load the fields of the templated entity otherwise.
31-
"""
32-
typ = typ.strip_typedefs()
33-
34-
while typ.code in (gdb.TYPE_CODE_PTR, gdb.TYPE_CODE_REF):
35-
typ = typ.target().strip_typedefs()
36-
37-
if typ.code == gdb.TYPE_CODE_FUNC:
38-
return typ
39-
40-
typename = typ.tag if typ.tag is not None else typ.name
41-
assert typename is not None
42-
return gdb.lookup_type(typename)
43-
44-
4527
# pylint: disable-next=invalid-name
4628
def AbslHashContainerIterator(container: gdb.Value, /) -> typing.Iterator[gdb.Value]:
4729
"""Return a generator of every node in the given absl::container_internal::raw_hash_set or

gdbmongo/boost_printers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import gdb
2222

23-
from gdbmongo.abseil_printers import gdb_resolve_type
23+
from gdbmongo.gdbutil import gdb_resolve_type
2424
from gdbmongo.printer_protocol import PrettyPrinterProtocol, SupportsChildren, SupportsDisplayHint
2525

2626

gdbmongo/bsonobj_printer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from gdbmongo.bsonmisc_printer import (MongoBSONBinData, MongoBSONCode, MongoBSONDBRef,
2828
MongoBSONRegEx, MongoBSONSymbol)
2929
from gdbmongo.date_printer import MongoDateT
30-
from gdbmongo.lock_manager_printer import gdb_lookup_value
30+
from gdbmongo.gdbutil import gdb_lookup_value
3131
from gdbmongo.objectid_printer import MongoOID
3232
from gdbmongo.printer_protocol import PrettyPrinterProtocol, SupportsDisplayHint
3333
from gdbmongo.string_data_printer import MongoStringData

gdbmongo/gdbutil.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
###
2+
# Copyright 2022-present MongoDB, Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
###
16+
"""Utility functions for gdb.Types and gdb.Values."""
17+
18+
import typing
19+
20+
import gdb
21+
22+
23+
def gdb_lookup_value(symbol_name: str, /) -> typing.Optional[gdb.Value]:
24+
"""Return the gdb.Value corresponding to the symbol name given."""
25+
if (symbol := gdb.lookup_symbol(symbol_name)[0]) is not None:
26+
return symbol.value()
27+
28+
return None
29+
30+
31+
def gdb_resolve_type(typ: gdb.Type, /) -> gdb.Type:
32+
"""Look up the name of a C++ type with any typedefs, pointers, and references stripped.
33+
34+
This function is useful in contexts where template arguments can be pointers because GDB may not
35+
load the fields of the templated entity otherwise.
36+
"""
37+
typ = typ.strip_typedefs()
38+
39+
while typ.code in (gdb.TYPE_CODE_PTR, gdb.TYPE_CODE_REF):
40+
typ = typ.target().strip_typedefs()
41+
42+
if typ.code == gdb.TYPE_CODE_FUNC:
43+
return typ
44+
45+
typename = typ.tag if typ.tag is not None else typ.name
46+
assert typename is not None
47+
return gdb.lookup_type(typename)

gdbmongo/lock_manager_printer.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,12 @@
3434
from gdbmongo.abseil_printers import (AbslFlatHashMapPrinter, AbslNodeHashMapPrinter,
3535
AbslFlatHashSetPrinter)
3636
from gdbmongo.decorable_printer import DecorationContainerPrinter
37+
from gdbmongo.gdbutil import gdb_lookup_value
3738
from gdbmongo.printer_protocol import PrettyPrinterProtocol, SupportsDisplayHint, SupportsToString
3839
from gdbmongo.static_immortal_printer import StaticImmortalPrinter
3940
from gdbmongo.string_data_printer import StdStringPrinter
4041

4142

42-
def gdb_lookup_value(symbol_name: str, /) -> typing.Optional[gdb.Value]:
43-
"""Return the gdb.Value corresponding to the symbol name given."""
44-
if (symbol := gdb.lookup_symbol(symbol_name)[0]) is not None:
45-
return symbol.value()
46-
47-
return None
48-
49-
5043
class ServiceContextDecorationMixin(typing.Protocol):
5144
"""Class to add support for constructing from the global ServiceContext if the subclass already
5245
supports constructing from a ServiceContext explicitly.

gdbmongo/static_immortal_printer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
import gdb
1919

20-
from gdbmongo.abseil_printers import gdb_resolve_type
2120
from gdbmongo.boost_printers import SingletonPrinterBase
21+
from gdbmongo.gdbutil import gdb_resolve_type
2222
from gdbmongo.printer_protocol import PrettyPrinterProtocol
2323

2424

0 commit comments

Comments
 (0)