From f71e34fd9fa94d664b962ef73d0259900233ff24 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Fri, 20 Sep 2024 12:22:08 +0800 Subject: [PATCH] ssa: fix function and global var debug info scope --- _lldbtest/main.py | 7 +++++-- _lldbtest/runmain.lldb | 2 +- cl/_testdata/debug/in.go | 20 ++++++++++++++++++-- cl/compile.go | 11 ++++++++--- ssa/decl.go | 2 +- ssa/di.go | 6 +++++- 6 files changed, 38 insertions(+), 10 deletions(-) diff --git a/_lldbtest/main.py b/_lldbtest/main.py index f45d6455..3c639f6e 100644 --- a/_lldbtest/main.py +++ b/_lldbtest/main.py @@ -309,12 +309,15 @@ def parse_expected_values(source_files): return test_cases -def execute_tests(executable_path, test_cases, interactive, plugin_path): +def execute_tests(executable_path, test_cases, verbose, interactive, plugin_path): results = TestResults() for test_case in test_cases: debugger = LLDBDebugger(executable_path, plugin_path) try: + if verbose: + log(f"Setting breakpoint at { + test_case.source_file}:{test_case.end_line}") debugger.setup() debugger.set_breakpoint( test_case.source_file, test_case.end_line) @@ -356,7 +359,7 @@ def run_tests(executable_path, source_files, verbose, interactive, plugin_path): ', '.join(source_files)} with {executable_path}") log(f"Found {len(test_cases)} test cases") - results = execute_tests(executable_path, test_cases, + results = execute_tests(executable_path, test_cases, verbose, interactive, plugin_path) if not interactive: print_test_results(results, verbose) diff --git a/_lldbtest/runmain.lldb b/_lldbtest/runmain.lldb index a75456e3..de8abff9 100644 --- a/_lldbtest/runmain.lldb +++ b/_lldbtest/runmain.lldb @@ -5,5 +5,5 @@ # lldb -S _lldbtest/runmain.lldb command script import _lldbtest/main.py -script main.run_tests("cl/_testdata/debug/out", ["cl/_testdata/debug/in.go"], True, True, None) +script main.run_tests("cl/_testdata/debug/out", ["cl/_testdata/debug/in.go"], True, False, None) quit diff --git a/cl/_testdata/debug/in.go b/cl/_testdata/debug/in.go index e68cdc01..085cb229 100644 --- a/cl/_testdata/debug/in.go +++ b/cl/_testdata/debug/in.go @@ -78,7 +78,10 @@ func FuncWithAllTypeStructParam(s StructWithAllTypeFields) { // s.e: github.com/goplus/llgo/cl/_testdata/debug.E{i = 30} // s.pad1: 100 // s.pad2: 200 - println(len(s.s)) + s.i8 = 8 + // Expected: + // s.i8: '\x08' + println(len(s.s), s.i8) } // Params is a function with all types of parameters. @@ -125,7 +128,7 @@ func FuncWithAllTypeParams( fn, ) // Expected: - // all variables: i8 i16 i32 i64 i u8 u16 u32 u64 u f32 f64 b c64 c128 slice arr arr2 s e f pf pi intr m c err fn globalInt globalStruct globalStructPtr + // all variables: i8 i16 i32 i64 i u8 u16 u32 u64 u f32 f64 b c64 c128 slice arr arr2 s e f pf pi intr m c err fn // i8: '\x01' // i16: 2 // i32: 3 @@ -146,6 +149,10 @@ func FuncWithAllTypeParams( // arr2: [3]github.com/goplus/llgo/cl/_testdata/debug.E{{i = 27}, {i = 28}, {i = 29}} // s: hello // e: github.com/goplus/llgo/cl/_testdata/debug.E{i = 30} + i8 = 9 + // Expected: + // i8: '\x09' + println(i8) return 1, errors.New("some error") } @@ -214,8 +221,17 @@ func main() { // all variables: globalInt globalStruct globalStructPtr s i err // s.i8: '\x01' // s.i16: 2 + s.i8 = 0x12 + println(s.i8) + // Expected: + // all variables: globalInt globalStruct globalStructPtr s i err + // s.i8: '\x12' + // globalStruct.i8: '\x01' + println((*globalStructPtr).i8) println("done") println("") + println(&s, &globalStruct, globalStructPtr.i16, globalStructPtr) + globalStructPtr = nil } var globalInt int = 301 diff --git a/cl/compile.go b/cl/compile.go index ed9924d2..82b09638 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -730,15 +730,20 @@ func (p *context) compileInstr(b llssa.Builder, instr ssa.Instruction) { // Not a local variable. return } - if v.IsAddr { - // skip *ssa.Alloc or *ssa.FieldAddr + if variable.IsField() { + // skip *ssa.FieldAddr return } pos := p.goProg.Fset.Position(v.Pos()) value := p.compileValue(b, v.X) fn := v.Parent() dbgVar := p.getLocalVariable(b, fn, variable) - b.DIValue(value, dbgVar, p.fn, pos, b.Func.Block(v.Block().Index)) + if v.IsAddr { + // *ssa.Alloc + b.DIDeclare(value, dbgVar, p.fn, pos, b.Func.Block(v.Block().Index)) + } else { + b.DIValue(value, dbgVar, p.fn, pos, b.Func.Block(v.Block().Index)) + } } default: panic(fmt.Sprintf("compileInstr: unknown instr - %T\n", instr)) diff --git a/ssa/decl.go b/ssa/decl.go index 7858c9b2..18b194b6 100644 --- a/ssa/decl.go +++ b/ssa/decl.go @@ -339,7 +339,7 @@ func (p Function) scopeMeta(b diBuilder, pos token.Position) DIScopeMeta { }) p.diFunc = &aDIFunction{ b.di.CreateFunction( - p.Pkg.cu.ll, + b.file(pos.Filename).ll, llvm.DIFunction{ Type: diFuncType, Name: p.Name(), diff --git a/ssa/di.go b/ssa/di.go index bffe0c15..76c8dc32 100644 --- a/ssa/di.go +++ b/ssa/di.go @@ -56,6 +56,10 @@ type aCompilationUnit struct { type CompilationUnit = *aCompilationUnit +func (c CompilationUnit) scopeMeta(b diBuilder, pos token.Position) DIScopeMeta { + return &aDIScopeMeta{c.ll} +} + var DWARF_LANG_C llvm.DwarfLang = 0x2 var DWARF_LANG_GO llvm.DwarfLang = 0x16 @@ -576,7 +580,7 @@ func (b Builder) DIGlobal(v Expr, name string, pos token.Position) { return } gv := b.di().createGlobalVariableExpression( - b.di().file(pos.Filename), + b.Pkg.cu, pos, name, name,