| 1 |
import gdb |
| 2 |
import traceback |
| 3 |
import gdb.printing |
| 4 |
import unicodedata |
| 5 |
|
| 6 |
def codepoints_to_rust_string(data_ptr, length: int) -> str: |
| 7 |
def to_rust_string(cp: int) -> str: |
| 8 |
c = chr(cp) |
| 9 |
|
| 10 |
if unicodedata.category(c) in ('Cs', 'Co'): |
| 11 |
return f"\\u{{{cp:04x}}}" if cp <= 0xffff else f"\\U{{{cp:08x}}}" |
| 12 |
if c in '\\"': |
| 13 |
return "\\" + c |
| 14 |
return c |
| 15 |
codepoints = (int((data_ptr + i).dereference()) for i in range(length)) |
| 16 |
return '"' + ''.join(to_rust_string(cp) for cp in codepoints) + '"' |
| 17 |
|
| 18 |
class Utf32StrPrinter: |
| 19 |
def __init__(self, val): |
| 20 |
self.val = val |
| 21 |
|
| 22 |
def to_string(self): |
| 23 |
try: |
| 24 |
ptr_to_slice = self.val.address.cast(gdb.lookup_type("usize").pointer()) |
| 25 |
length = int(ptr_to_slice[1]) |
| 26 |
ptr = ptr_to_slice[0].cast(gdb.lookup_type("u32").pointer()) |
| 27 |
return codepoints_to_rust_string(ptr, length) |
| 28 |
except Exception: |
| 29 |
return "error reading Utf32Str:\n" + traceback.format_exc() |
| 30 |
|
| 31 |
class Utf32StringPrinter: |
| 32 |
def __init__(self, val): |
| 33 |
self.val = val |
| 34 |
|
| 35 |
def to_string(self): |
| 36 |
try: |
| 37 |
inner = self.val['inner'] |
| 38 |
ptr = inner['buf']['inner']['ptr']['pointer']['pointer'].cast(gdb.lookup_type("u32").pointer()) |
| 39 |
len = int(inner['len']) |
| 40 |
return codepoints_to_rust_string(ptr, len) |
| 41 |
except Exception: |
| 42 |
return "error reading Utf32String:\n" + traceback.format_exc() |
| 43 |
|
| 44 |
def widestring_pretty_printer(): |
| 45 |
pp = gdb.printing.RegexpCollectionPrettyPrinter("widestring-rs") |
| 46 |
pp.add_printer('Utf32Str', '^&widestring::utfstr::Utf32Str$', Utf32StrPrinter) |
| 47 |
pp.add_printer('Utf32String', '^widestring::utfstring::Utf32String$', Utf32StringPrinter) |
| 48 |
return pp |