|
| 1 | +import lldb |
| 2 | + |
| 3 | +def StringSummaryProvider(valobj, internal_dict): |
| 4 | + # Try to access _M_dataplus._M_p |
| 5 | + # If fails, try to look at memory manually |
| 6 | + |
| 7 | + # This is a heuristic for GCC's std::string |
| 8 | + # It has _M_dataplus, _M_string_length, etc. |
| 9 | + |
| 10 | + try: |
| 11 | + # If we have debug info, this works |
| 12 | + dataplus = valobj.GetChildMemberWithName('_M_dataplus') |
| 13 | + if dataplus.IsValid(): |
| 14 | + mp = dataplus.GetChildMemberWithName('_M_p') |
| 15 | + if mp.IsValid(): |
| 16 | + data = mp.GetSummary() |
| 17 | + if data: return data |
| 18 | + # If summary is missing, read memory |
| 19 | + addr = mp.GetValueAsUnsigned(0) |
| 20 | + if addr == 0: return '""' |
| 21 | + |
| 22 | + # Read string from memory |
| 23 | + error = lldb.SBError() |
| 24 | + s = valobj.GetProcess().ReadCStringFromMemory(addr, 1024, error) |
| 25 | + if error.Success(): |
| 26 | + return f'"{s}"' |
| 27 | + except: |
| 28 | + pass |
| 29 | + |
| 30 | + # Fallback for missing debug info (incomplete type) |
| 31 | + # Assuming standard layout: pointer at offset 0 |
| 32 | + try: |
| 33 | + addr = valobj.GetAddress().GetLoadAddress(valobj.GetTarget()) |
| 34 | + if addr == lldb.LLDB_INVALID_ADDRESS: |
| 35 | + addr = valobj.GetValueAsUnsigned(0) |
| 36 | + |
| 37 | + # We need to read the pointer at this address. |
| 38 | + # But wait, std::string is a struct. |
| 39 | + # _M_dataplus is the first member. |
| 40 | + # _M_dataplus has _M_p as first member (usually). |
| 41 | + |
| 42 | + # So dereferencing the address of the string object gives the address of the char buffer. |
| 43 | + error = lldb.SBError() |
| 44 | + pointer_val = valobj.GetProcess().ReadPointerFromMemory(addr, error) |
| 45 | + if error.Success() and pointer_val != 0: |
| 46 | + s = valobj.GetProcess().ReadCStringFromMemory(pointer_val, 1024, error) |
| 47 | + if error.Success(): |
| 48 | + return f'"{s}"' |
| 49 | + except: |
| 50 | + pass |
| 51 | + |
| 52 | + return "" |
| 53 | + |
| 54 | +def WxStringSummaryProvider(valobj, internal_dict): |
| 55 | + # wxString is usually a wrapper around std::basic_string or compatible |
| 56 | + # In wx 3.x with std::string enabled, it might inherit from it or have it as member |
| 57 | + |
| 58 | + # Try 1: check if it has m_impl |
| 59 | + try: |
| 60 | + impl = valobj.GetChildMemberWithName('m_impl') |
| 61 | + if impl.IsValid(): |
| 62 | + return StringSummaryProvider(impl, internal_dict) |
| 63 | + except: |
| 64 | + pass |
| 65 | + |
| 66 | + # Try 2: treat as std::string directly (inheritance) |
| 67 | + res = StringSummaryProvider(valobj, internal_dict) |
| 68 | + if res and res != "": |
| 69 | + return res |
| 70 | + |
| 71 | + return "" |
| 72 | + |
| 73 | +def WxEventSummaryProvider(valobj, internal_dict): |
| 74 | + # For incomplete wxEvent, we try to resolve the vtable to get dynamic type |
| 75 | + |
| 76 | + try: |
| 77 | + addr = valobj.GetAddress().GetLoadAddress(valobj.GetTarget()) |
| 78 | + if addr == lldb.LLDB_INVALID_ADDRESS: |
| 79 | + # Maybe it's a pointer or reference, get its value |
| 80 | + addr = valobj.GetValueAsUnsigned(0) |
| 81 | + |
| 82 | + if addr == 0: |
| 83 | + return "NULL" |
| 84 | + |
| 85 | + # Read vtable pointer (first word) |
| 86 | + error = lldb.SBError() |
| 87 | + vptr = valobj.GetProcess().ReadPointerFromMemory(addr, error) |
| 88 | + |
| 89 | + if error.Success() and vptr != 0: |
| 90 | + # Resolve symbol for vptr |
| 91 | + addr_obj = valobj.GetTarget().ResolveLoadAddress(vptr) |
| 92 | + sym = addr_obj.GetSymbol() |
| 93 | + if sym.IsValid(): |
| 94 | + name = sym.GetName() |
| 95 | + # Name is like "vtable for wxCommandEvent" |
| 96 | + # Demangle usually happens automatically in LLDB API or we can just parse |
| 97 | + if "vtable for " in name: |
| 98 | + return name.replace("vtable for ", "") + f" @ {hex(addr)}" |
| 99 | + return name + f" @ {hex(addr)}" |
| 100 | + except: |
| 101 | + pass |
| 102 | + |
| 103 | + return f"wxEvent @ {hex(valobj.GetValueAsUnsigned(0))}" |
| 104 | + |
| 105 | +def __lldb_init_module(debugger, internal_dict): |
| 106 | + debugger.HandleCommand('type summary add -F lldb_formatters.StringSummaryProvider "std::string"') |
| 107 | + debugger.HandleCommand('type summary add -F lldb_formatters.StringSummaryProvider "std::__cxx11::string"') |
| 108 | + debugger.HandleCommand('type summary add -F lldb_formatters.StringSummaryProvider "std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >"') |
| 109 | + |
| 110 | + debugger.HandleCommand('type summary add -F lldb_formatters.WxStringSummaryProvider "wxString"') |
| 111 | + debugger.HandleCommand('type summary add -F lldb_formatters.WxEventSummaryProvider "wxEvent" -C yes') |
| 112 | + debugger.HandleCommand('type summary add -F lldb_formatters.WxEventSummaryProvider "wxCommandEvent" -C yes') |
| 113 | + debugger.HandleCommand('type summary add -F lldb_formatters.WxEventSummaryProvider "wxNotifyEvent" -C yes') |
| 114 | + debugger.HandleCommand('type summary add -F lldb_formatters.WxEventSummaryProvider "wxMouseEvent" -C yes') |
| 115 | + debugger.HandleCommand('type summary add -F lldb_formatters.WxEventSummaryProvider "wxKeyEvent" -C yes') |
0 commit comments