From 87f6c8087fe4378d8d76602e104d5444c6340b90 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Wed, 18 Sep 2024 22:05:41 +0800 Subject: [PATCH] ssa: debug info of global variable --- cl/_testdata/debug/in.go | 9 +++++++++ cl/compile.go | 7 ++++++- ssa/di.go | 14 +++++++++++++- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/cl/_testdata/debug/in.go b/cl/_testdata/debug/in.go index 89a6ddf5..3fa28a4a 100644 --- a/cl/_testdata/debug/in.go +++ b/cl/_testdata/debug/in.go @@ -140,6 +140,9 @@ func main() { pad1: 100, pad2: 200, } + globalStructPtr = &s + globalStruct = s + println("globalInt:", globalInt) println("s:", &s) FuncWithAllTypeStructParam(s) println("called function with struct") @@ -159,5 +162,11 @@ func main() { ) println(i, err) println("called function with types") + println(globalStructPtr) + println(&globalStruct) println("done") } + +var globalInt int = 301 +var globalStruct StructWithAllTypeFields +var globalStructPtr *StructWithAllTypeFields diff --git a/cl/compile.go b/cl/compile.go index 513ac599..09d29a95 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -797,7 +797,12 @@ func (p *context) compileValue(b llssa.Builder, v ssa.Value) llssa.Expr { } return pyFn.Expr case *ssa.Global: - return p.varOf(b, v) + val := p.varOf(b, v) + if debugSymbols { + pos := p.fset.Position(v.Pos()) + b.DIGlobal(val, v.Name(), pos) + } + return val case *ssa.Const: t := types.Default(v.Type()) bg := llssa.InGo diff --git a/ssa/di.go b/ssa/di.go index 3076db1a..3a2635b4 100644 --- a/ssa/di.go +++ b/ssa/di.go @@ -90,7 +90,7 @@ func (b diBuilder) createFile(filename string) DIFile { return &aDIFile{ll: b.di.CreateFile(file, dir)} } -func (f DIFile) scopeMeta(b diBuilder, cu CompilationUnit, pos token.Position) DIScopeMeta { +func (f DIFile) scopeMeta(b diBuilder, pos token.Position) DIScopeMeta { return &aDIScopeMeta{b.file(pos.Filename).ll} } @@ -633,6 +633,18 @@ func (b Builder) DIVarAuto(f Function, pos token.Position, varName string, vt Ty return b.Pkg.diBuilder().varAuto(f, pos, varName, t) } +func (b Builder) DIGlobal(v Expr, name string, pos token.Position) { + gv := b.Pkg.diBuilder().createGlobalVariableExpression( + b.Pkg.diBuilder().file(pos.Filename), + pos, + name, + name, + b.Pkg.diBuilder().diType(v.Type, pos), + false, + ) + v.impl.AddMetadata(0, gv.ll) +} + func (b Builder) DISetCurrentDebugLocation(f Function, pos token.Position) { b.impl.SetCurrentDebugLocation( uint(pos.Line),