-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7e5250e
commit 5a0409a
Showing
7 changed files
with
1,201 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
= GDB pretty printers | ||
|
||
Create or modify your `.gdbinit` file to contain the following: | ||
|
||
[source,python] | ||
---- | ||
python | ||
import sys | ||
sys.path.insert(0, '/path/share/gdb') <1> | ||
from mrdox_printers import register_mrdox_printers <2> | ||
register_mrdox_printers() <3> | ||
end | ||
---- | ||
|
||
<1> Make GDB see the directory with the printers | ||
<2> Import the function that registers the printers with GDB | ||
<3> Effectively register the printers | ||
|
||
Note that this pattern does not register the printers unless the user explicitly asks for it by calling the `register_mrdox_printers` function. | ||
This helps the scripts separate these concerns. | ||
|
||
NOTE: The printers require Python 3 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Intentionally empty |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
# | ||
# Copyright (c) 2023 alandefreitas ([email protected]) | ||
# | ||
# Distributed under the Boost Software License, Version 1.0. | ||
# https://www.boost.org/LICENSE_1_0.txt | ||
# | ||
|
||
import gdb | ||
|
||
|
||
class utils: | ||
@staticmethod | ||
def resolve_type(t): | ||
if t.code == gdb.TYPE_CODE_REF: | ||
t = t.target() | ||
t = t.unqualified().strip_typedefs() | ||
typename = t.tag | ||
if typename is None: | ||
return None | ||
return t | ||
|
||
@staticmethod | ||
def resolved_typename(val): | ||
t = val.type | ||
t = utils.resolve_type(t) | ||
if t is not None: | ||
return str(t) | ||
else: | ||
return str(val.type) | ||
|
||
@staticmethod | ||
def pct_decode(s: str): | ||
return urllib.parse.unquote(s) | ||
|
||
sv_pool = [] | ||
|
||
@staticmethod | ||
def make_string_view(cstr: gdb.Value, n: int): | ||
sv_ptr: gdb.Value = gdb.parse_and_eval('malloc(sizeof(class boost::core::basic_string_view<char>))') | ||
sv_ptr_str = cstr.format_string(format='x') | ||
gdb.execute( | ||
f'call ((boost::core::basic_string_view<char>*){sv_ptr})->basic_string_view((const char*){sv_ptr_str}, {n})', | ||
to_string=True) | ||
sv = gdb.parse_and_eval(f'*((boost::core::basic_string_view<char>*){sv_ptr})') | ||
copy: gdb.Value = sv | ||
utils.sv_pool.append(sv_ptr) | ||
if len(utils.sv_pool) > 5000: | ||
gdb.execute(f'call free({utils.sv_pool[0]})', to_string=True) | ||
utils.sv_pool.pop(0) | ||
return copy | ||
|
||
pct_sv_pool = [] | ||
|
||
@staticmethod | ||
def make_pct_string_view(cstr: gdb.Value, n: int): | ||
sv_ptr: gdb.Value = gdb.parse_and_eval('malloc(sizeof(class boost::urls::pct_string_view))') | ||
sv_ptr_str = cstr.format_string(format='x') | ||
gdb.execute( | ||
f'call ((boost::urls::pct_string_view*){sv_ptr})->pct_string_view((const char*){sv_ptr_str}, {n})', | ||
to_string=True) | ||
sv = gdb.parse_and_eval(f'*((boost::urls::pct_string_view*){sv_ptr})') | ||
copy: gdb.Value = sv | ||
utils.pct_sv_pool.append(sv_ptr) | ||
if len(utils.sv_pool) > 5000: | ||
gdb.execute(f'call free({utils.pct_sv_pool[0]})', to_string=True) | ||
utils.sv_pool.pop(0) | ||
return copy | ||
|
||
|
||
class DomValuePrinter: | ||
def __init__(self, value): | ||
self.value = value | ||
|
||
def children(self): | ||
# Get kind enum | ||
kind = self.value['kind_'] | ||
if kind == 1: | ||
yield 'Boolean', self.value['b_'] | ||
elif kind == 2: | ||
yield 'Integer', self.value['i_'] | ||
elif kind == 3: | ||
yield 'String', self.value['str_'] | ||
elif kind == 4: | ||
yield 'Array', self.value['arr_'] | ||
elif kind == 5: | ||
yield 'Object', self.value['obj_']['impl_']['_M_ptr'] | ||
elif kind == 6: | ||
yield 'Function', self.value['fn_'] | ||
else: | ||
yield 'kind_', self.value['kind_'] | ||
|
||
|
||
class EnumPrinter: | ||
def __init__(self, value): | ||
self.value = value | ||
|
||
def to_string(self): | ||
s: str = self.value.format_string(raw=True) | ||
return s.rsplit(':', 1)[-1] | ||
|
||
|
||
if __name__ != "__main__": | ||
def lookup_function(val: gdb.Value): | ||
if val.type.code == gdb.TYPE_CODE_ENUM: | ||
return EnumPrinter(val) | ||
|
||
typename: str = utils.resolved_typename(val) | ||
if typename == 'clang::mrdox::dom::Value': | ||
return DomValuePrinter(val) | ||
return None | ||
|
||
|
||
def register_mrdox_printers(obj=None): | ||
if obj is None: | ||
obj = gdb | ||
obj.pretty_printers.append(lookup_function) |
Oops, something went wrong.