From 7f4d14d36eeb028109b20846984805a8fd923cce Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 19 Sep 2017 12:57:49 +0300 Subject: [PATCH] [debug] add Kotlin Object pretty printers for lldb Now lldb commands like `frame variable` print non-primitive Kotlin objects nicely if you `command script import dist/tools/konan_lldb.py` --- llvmDebugInfoC/src/scripts/konan_lldb.py | 41 ++++++++++++++++++------ 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/llvmDebugInfoC/src/scripts/konan_lldb.py b/llvmDebugInfoC/src/scripts/konan_lldb.py index 0d2c8c1a4fd..d8560136e9b 100644 --- a/llvmDebugInfoC/src/scripts/konan_lldb.py +++ b/llvmDebugInfoC/src/scripts/konan_lldb.py @@ -18,18 +18,41 @@ # # (lldb) command script import llvmDebugInfoC/src/scripts/konan_lldb.py -# (lldb) show_variable +# (lldb) p kotlin_variable # import lldb -import commands -import optparse -import shlex +import ctypes + +def kotlin_object_type_summary(lldb_val, internal_dict): + """Hook that is run by lldb to display a Kotlin object.""" + fallback = lldb_val.GetValue() + if str(lldb_val.type) != "struct ObjHeader *": + return fallback + + def evaluate(expr): + return lldb_val.GetTarget().EvaluateExpression(expr, lldb.SBExpressionOptions()) + + buff_len = evaluate( + "Konan_DebugObjectToUtf8Array((struct ObjHeader *) %s, Konan_DebugBuffer(), Konan_DebugBufferSize());" % lldb_val.GetValueAsUnsigned() + ).GetValueAsUnsigned() + + if not buff_len: + return fallback + + buff_addr = evaluate("Konan_DebugBuffer()").GetValueAsUnsigned() + + error = lldb.SBError() + s = lldb_val.GetProcess().ReadCStringFromMemory(buff_addr, buff_len, error) + return s if error.Success() else fallback -def show_variable(debugger, command, result, internal_dict): - debugger.GetCommandInterpreter().HandleCommand('expr -- Konan_DebugPrint(%s)' % command, result) - return result def __lldb_init_module(debugger, internal_dict): - debugger.HandleCommand('command script add -f konan_lldb.show_variable show_variable') - + debugger.HandleCommand('\ + type summary add \ + --no-value \ + --python-function konan_lldb.kotlin_object_type_summary \ + -x "ObjHeader \*" \ + --category Kotlin\ + ') + debugger.HandleCommand('type category enable Kotlin')