compiler: split LLGO_DEBUG into LLGO_DEBUG, LLGO_DBG_SYMBOLS, disableInline

This commit is contained in:
Li Jie
2025-02-22 09:31:41 +08:00
parent 9f38338c58
commit 8cd5924bf2
7 changed files with 45 additions and 27 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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{

View File

@@ -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)
}
}

View File

@@ -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 {

View File

@@ -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,

View File

@@ -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)
}