From 8cd5924bf27165f974a50f47aa561113d5ecc04c Mon Sep 17 00:00:00 2001 From: Li Jie Date: Sat, 22 Feb 2025 09:31:41 +0800 Subject: [PATCH] compiler: split LLGO_DEBUG into LLGO_DEBUG, LLGO_DBG_SYMBOLS, disableInline --- compiler/_lldb/README.md | 2 +- compiler/_lldb/common.sh | 2 +- compiler/cl/compile.go | 39 ++++++++++++++++++------------- compiler/cl/instr.go | 2 +- compiler/internal/build/build.go | 18 +++++++++----- compiler/internal/llgen/llgenf.go | 7 +++++- compiler/ssa/package.go | 2 +- 7 files changed, 45 insertions(+), 27 deletions(-) diff --git a/compiler/_lldb/README.md b/compiler/_lldb/README.md index 1f6a4946..9b9e1315 100644 --- a/compiler/_lldb/README.md +++ b/compiler/_lldb/README.md @@ -3,7 +3,7 @@ ### Build with debug info ```shell -LLGO_DEBUG=1 llgo build -o cl/_testdata/debug/out ./cl/_testdata/debug +LLGO_DEBUG_SYMBOLS=1 llgo build -o cl/_testdata/debug/out ./cl/_testdata/debug ``` ### Debug with lldb diff --git a/compiler/_lldb/common.sh b/compiler/_lldb/common.sh index 4925255a..e4c9af1e 100644 --- a/compiler/_lldb/common.sh +++ b/compiler/_lldb/common.sh @@ -44,7 +44,7 @@ build_project() { return 1 fi - LLGO_DEBUG=1 llgo build -o "debug.out" . || { + LLGO_DEBUG_SYMBOLS=1 llgo build -o "debug.out" . || { local ret=$? cd "$current_dir" || return return $ret diff --git a/compiler/cl/compile.go b/compiler/cl/compile.go index 0d3337cf..fb76eb25 100644 --- a/compiler/cl/compile.go +++ b/compiler/cl/compile.go @@ -46,10 +46,13 @@ const ( ) var ( - debugInstr bool - debugGoSSA bool - debugSymbols bool - debugTrace bool + debugInstr bool + debugGoSSA bool + + enableCallTracing bool + enableDbg bool + enableDbgSyms bool + disableInline bool ) // SetDebug sets debug flags. @@ -58,12 +61,16 @@ func SetDebug(dbgFlags dbgFlags) { debugGoSSA = (dbgFlags & DbgFlagGoSSA) != 0 } -func EnableDebugSymbols(b bool) { - debugSymbols = b +func EnableDebug(b bool) { + enableDbg = b +} + +func EnableDbgSyms(b bool) { + enableDbgSyms = b } func EnableTrace(b bool) { - debugTrace = b + enableCallTracing = b } // ----------------------------------------------------------------------------- @@ -246,7 +253,7 @@ func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function) (llssa.Fun } if fn == nil { fn = pkg.NewFuncEx(name, sig, llssa.Background(ftype), hasCtx, f.Origin() != nil) - if debugSymbols { + if disableInline { fn.Inline(llssa.NoInline) } } @@ -278,7 +285,7 @@ func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function) (llssa.Fun log.Println("==> FuncBody", name) } b := fn.NewBuilder() - if debugSymbols { + if enableDbg { pos := p.goProg.Fset.Position(f.Pos()) bodyPos := p.getFuncBodyPos(f) b.DebugFunction(fn, pos, bodyPos) @@ -385,11 +392,11 @@ func (p *context) compileBlock(b llssa.Builder, block *ssa.BasicBlock, n int, do var instrs = block.Instrs[n:] var ret = fn.Block(block.Index) b.SetBlock(ret) - if block.Index == 0 && debugTrace && !strings.HasPrefix(fn.Name(), "github.com/goplus/llgo/runtime/internal/runtime.Print") { + if block.Index == 0 && enableCallTracing && !strings.HasPrefix(fn.Name(), "github.com/goplus/llgo/runtime/internal/runtime.Print") { b.Printf("call " + fn.Name() + "\n\x00") } // place here to avoid wrong current-block - if debugSymbols && block.Index == 0 { + if enableDbgSyms && block.Index == 0 { p.debugParams(b, block.Parent()) } if doModInit { @@ -781,7 +788,7 @@ func (p *context) compileInstr(b llssa.Builder, instr ssa.Instruction) { p.compileInstrOrValue(b, iv, false) return } - if debugSymbols { + if enableDbg { scope := p.getDebugLocScope(instr.Parent(), instr.Pos()) if scope != nil { diScope := b.DIScope(p.fn, scope) @@ -844,7 +851,7 @@ func (p *context) compileInstr(b llssa.Builder, instr ssa.Instruction) { x := p.compileValue(b, v.X) b.Send(ch, x) case *ssa.DebugRef: - if debugSymbols { + if enableDbgSyms { p.debugRef(b, v) } default: @@ -901,7 +908,7 @@ func (p *context) compileValue(b llssa.Builder, v ssa.Value) llssa.Expr { if isCgoVar(varName) { p.cgoSymbols = append(p.cgoSymbols, val.Name()) } - if debugSymbols { + if enableDbgSyms { pos := p.fset.Position(v.Pos()) b.DIGlobal(val, v.Name(), pos) } @@ -984,8 +991,8 @@ func NewPackageEx(prog llssa.Program, patches Patches, pkg *ssa.Package, files [ prog.SetRuntime(pkgTypes) } ret = prog.NewPackage(pkgName, pkgPath) - if debugSymbols { - ret.InitDebugSymbols(pkgName, pkgPath, pkgProg.Fset) + if enableDbg { + ret.InitDebug(pkgName, pkgPath, pkgProg.Fset) } ctx := &context{ diff --git a/compiler/cl/instr.go b/compiler/cl/instr.go index d938d40f..d872947f 100644 --- a/compiler/cl/instr.go +++ b/compiler/cl/instr.go @@ -348,7 +348,7 @@ func (p *context) funcOf(fn *ssa.Function) (aFn llssa.Function, pyFn llssa.PyObj } sig := fn.Signature aFn = pkg.NewFuncEx(name, sig, llssa.Background(ftype), false, fn.Origin() != nil) - if debugSymbols { + if disableInline { aFn.Inline(llssa.NoInline) } } diff --git a/compiler/internal/build/build.go b/compiler/internal/build/build.go index 5b5f5a1f..75f2617d 100644 --- a/compiler/internal/build/build.go +++ b/compiler/internal/build/build.go @@ -142,7 +142,8 @@ func Do(args []string, conf *Config) ([]Package, error) { } } - cl.EnableDebugSymbols(IsDebugEnabled()) + cl.EnableDebug(IsDbgEnabled()) + cl.EnableDbgSyms(IsDbgSymsEnabled()) cl.EnableTrace(IsTraceEnabled()) llssa.Initialize(llssa.InitAll) @@ -201,7 +202,7 @@ func Do(args []string, conf *Config) ([]Package, error) { }) buildMode := ssaBuildMode - if IsDebugEnabled() { + if IsDbgEnabled() { buildMode |= ssa.GlobalDebug } if !IsOptimizeEnabled() { @@ -451,7 +452,7 @@ func linkMainPkg(ctx *context, pkg *packages.Package, pkgs []*aPackage, linkArgs if err != nil { panic(err) } - defer os.Remove(entryLLFile) + // defer os.Remove(entryLLFile) args = append(args, entryLLFile) var aPkg *aPackage @@ -480,7 +481,7 @@ func linkMainPkg(ctx *context, pkg *packages.Package, pkgs []*aPackage, linkArgs } } args = append(args, exargs...) - if IsDebugEnabled() { + if IsDbgSymsEnabled() { args = append(args, "-gdwarf-4") } @@ -751,6 +752,7 @@ var ( ) const llgoDebug = "LLGO_DEBUG" +const llgoDbgSyms = "LLGO_DEBUG_SYMBOLS" const llgoTrace = "LLGO_TRACE" const llgoOptimize = "LLGO_OPTIMIZE" const llgoCheck = "LLGO_CHECK" @@ -768,8 +770,12 @@ func IsTraceEnabled() bool { return isEnvOn(llgoTrace, false) } -func IsDebugEnabled() bool { - return isEnvOn(llgoDebug, false) +func IsDbgEnabled() bool { + return isEnvOn(llgoDebug, false) || isEnvOn(llgoDbgSyms, false) +} + +func IsDbgSymsEnabled() bool { + return isEnvOn(llgoDbgSyms, false) } func IsOptimizeEnabled() bool { diff --git a/compiler/internal/llgen/llgenf.go b/compiler/internal/llgen/llgenf.go index c179940b..f7af75aa 100644 --- a/compiler/internal/llgen/llgenf.go +++ b/compiler/internal/llgen/llgenf.go @@ -33,11 +33,16 @@ func GenFrom(fileOrPkg string) string { func genFrom(pkgPath string) (build.Package, error) { oldDbg := os.Getenv("LLGO_DEBUG") + oldDbgSyms := os.Getenv("LLGO_DEBUG_SYMBOLS") dbg := isDbgSymEnabled(filepath.Join(pkgPath, "flags.txt")) if dbg { os.Setenv("LLGO_DEBUG", "1") + os.Setenv("LLGO_DEBUG_SYMBOLS", "1") } - defer os.Setenv("LLGO_DEBUG", oldDbg) + defer func() { + os.Setenv("LLGO_DEBUG", oldDbg) + os.Setenv("LLGO_DEBUG_SYMBOLS", oldDbgSyms) + }() conf := &build.Config{ Mode: build.ModeGen, diff --git a/compiler/ssa/package.go b/compiler/ssa/package.go index 015b1fd6..7edac049 100644 --- a/compiler/ssa/package.go +++ b/compiler/ssa/package.go @@ -759,7 +759,7 @@ func (p Package) AfterInit(b Builder, ret BasicBlock) { } } -func (p Package) InitDebugSymbols(name, pkgPath string, positioner Positioner) { +func (p Package) InitDebug(name, pkgPath string, positioner Positioner) { p.di = newDIBuilder(p.Prog, p, positioner) p.cu = p.di.createCompileUnit(name, pkgPath) }