|
| 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) |
0 commit comments