debug symbols switch

This commit is contained in:
Li Jie
2024-09-13 17:15:36 +08:00
parent 3e5338c902
commit 4c5f37db0f
7 changed files with 76 additions and 28 deletions

View File

@@ -17,18 +17,24 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"os" "os"
"github.com/goplus/llgo/internal/llgen" "github.com/goplus/llgo/internal/llgen"
) )
var (
enableDbg = flag.Bool("dbg", false, "enable debug symbols")
)
func main() { func main() {
if len(os.Args) < 2 { flag.Parse()
fmt.Fprintln(os.Stderr, "Usage: llgen <pkg> [pkgPath]") if len(flag.Args()) < 1 {
fmt.Fprintln(os.Stderr, "Usage: llgen [flags] <pkg> [pkgPath]")
return return
} }
llgen.Init(*enableDbg)
llgen.Init() args := flag.Args()
llgen.SmartDoFile(os.Args[1], os.Args[2:]...) llgen.SmartDoFile(args[0], args[1:]...)
} }

View File

@@ -46,8 +46,9 @@ const (
) )
var ( var (
debugInstr bool debugInstr bool
debugGoSSA bool debugGoSSA bool
debugSymbols bool
) )
// SetDebug sets debug flags. // SetDebug sets debug flags.
@@ -56,6 +57,15 @@ func SetDebug(dbgFlags dbgFlags) {
debugGoSSA = (dbgFlags & DbgFlagGoSSA) != 0 debugGoSSA = (dbgFlags & DbgFlagGoSSA) != 0
} }
// EnableDebugSymbols enables debug symbols.
func EnableDebugSymbols() {
debugSymbols = true
}
func DebugSymbols() bool {
return debugSymbols
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
type instrOrValue interface { type instrOrValue interface {
@@ -228,7 +238,9 @@ func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function) (llssa.Fun
sig = types.NewSignatureType(nil, nil, nil, params, results, false) sig = types.NewSignatureType(nil, nil, nil, params, results, false)
} }
fn = pkg.NewFuncEx(name, sig, llssa.Background(ftype), hasCtx, f.Origin() != nil) fn = pkg.NewFuncEx(name, sig, llssa.Background(ftype), hasCtx, f.Origin() != nil)
p.pkg.DIBuilder().DebugFunction(fn, p.goProg.Fset.Position(f.Pos())) if debugSymbols {
p.pkg.DIBuilder().DebugFunction(fn, p.goProg.Fset.Position(f.Pos()))
}
} }
if nblk := len(f.Blocks); nblk > 0 { if nblk := len(f.Blocks); nblk > 0 {
@@ -250,14 +262,18 @@ func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function) (llssa.Fun
log.Println("==> FuncBody", name) log.Println("==> FuncBody", name)
} }
b := fn.NewBuilder() b := fn.NewBuilder()
b.SetCurrentDebugLocation(p.fn, p.goProg.Fset.Position(f.Pos())) if debugSymbols {
b.SetCurrentDebugLocation(p.fn, p.goProg.Fset.Position(f.Pos()))
}
p.bvals = make(map[ssa.Value]llssa.Expr) p.bvals = make(map[ssa.Value]llssa.Expr)
off := make([]int, len(f.Blocks)) off := make([]int, len(f.Blocks))
for i, block := range f.Blocks { for i, block := range f.Blocks {
off[i] = p.compilePhis(b, block) off[i] = p.compilePhis(b, block)
} }
p.blkInfos = blocks.Infos(f.Blocks) p.blkInfos = blocks.Infos(f.Blocks)
p.debugParams(b, f) if debugSymbols {
p.debugParams(b, f)
}
i := 0 i := 0
for { for {
block := f.Blocks[i] block := f.Blocks[i]
@@ -469,8 +485,10 @@ func (p *context) compileInstrOrValue(b llssa.Builder, iv instrOrValue, asValue
} }
log.Panicln("unreachable:", iv) log.Panicln("unreachable:", iv)
} }
if v, ok := iv.(ssa.Instruction); ok { if debugSymbols {
b.SetCurrentDebugLocation(p.fn, p.goProg.Fset.Position(v.Pos())) if v, ok := iv.(ssa.Instruction); ok {
b.SetCurrentDebugLocation(p.fn, p.goProg.Fset.Position(v.Pos()))
}
} }
switch v := iv.(type) { switch v := iv.(type) {
case *ssa.Call: case *ssa.Call:
@@ -881,6 +899,9 @@ func NewPackageEx(prog llssa.Program, patches Patches, pkg *ssa.Package, files [
prog.SetRuntime(pkgTypes) prog.SetRuntime(pkgTypes)
} }
ret = prog.NewPackage(pkgName, pkgPath) ret = prog.NewPackage(pkgName, pkgPath)
if debugSymbols {
ret.EnableDebugSymbols(pkgName, pkgPath)
}
ctx := &context{ ctx := &context{
prog: prog, prog: prog,

View File

@@ -120,7 +120,10 @@ const (
) )
func Do(args []string, conf *Config) { func Do(args []string, conf *Config) {
flags, patterns, verbose := ParseArgs(args, buildFlags) flags, patterns, verbose, dbg := ParseArgs(args, buildFlags)
if dbg {
cl.EnableDebugSymbols()
}
flags = append(flags, "-tags", "llgo") flags = append(flags, "-tags", "llgo")
cfg := &packages.Config{ cfg := &packages.Config{
Mode: loadSyntax | packages.NeedDeps | packages.NeedModule | packages.NeedExportFile, Mode: loadSyntax | packages.NeedDeps | packages.NeedModule | packages.NeedExportFile,
@@ -436,7 +439,9 @@ func linkMainPkg(ctx *context, pkg *packages.Package, pkgs []*aPackage, llFiles
} }
} }
args = append(args, exargs...) args = append(args, exargs...)
args = append(args, "-gdwarf-5", "-v") if cl.DebugSymbols() {
args = append(args, "-gdwarf-5")
}
// TODO(xsw): show work // TODO(xsw): show work
if verbose { if verbose {
@@ -598,6 +603,7 @@ var (
"-cover": false, // -cover: enable coverage analysis "-cover": false, // -cover: enable coverage analysis
"-covermode": true, // -covermode mode: set the mode for coverage analysis "-covermode": true, // -covermode mode: set the mode for coverage analysis
"-v": false, // -v: print the names of packages as they are compiled "-v": false, // -v: print the names of packages as they are compiled
"-dbg": false, // -dbg: build with debug symbols (temporarily)
"-work": false, // -work: print the name of the temporary work directory and do not delete it when exiting "-work": false, // -work: print the name of the temporary work directory and do not delete it when exiting
"-x": false, // -x: print the commands "-x": false, // -x: print the commands
"-tags": true, // -tags 'tag,list': a space-separated list of build tags to consider satisfied during the build "-tags": true, // -tags 'tag,list': a space-separated list of build tags to consider satisfied during the build
@@ -606,18 +612,22 @@ var (
} }
) )
func ParseArgs(args []string, swflags map[string]bool) (flags, patterns []string, verbose bool) { func ParseArgs(args []string, swflags map[string]bool) (flags, patterns []string, verbose, dbg bool) {
n := len(args) n := len(args)
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
arg := args[i] arg := args[i]
if strings.HasPrefix(arg, "-") { if strings.HasPrefix(arg, "-") {
checkFlag(arg, &i, &verbose, swflags) checkFlag(arg, &i, &verbose, &dbg, swflags)
} else { } else {
flags, patterns = args[:i], args[i:] patterns = args[i:]
for _, arg := range args[:i] {
if arg != "-dbg" {
flags = append(flags, arg)
}
}
return return
} }
} }
flags = args
return return
} }
@@ -626,7 +636,7 @@ func SkipFlagArgs(args []string) int {
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
arg := args[i] arg := args[i]
if strings.HasPrefix(arg, "-") { if strings.HasPrefix(arg, "-") {
checkFlag(arg, &i, nil, buildFlags) checkFlag(arg, &i, nil, nil, buildFlags)
} else { } else {
return i return i
} }
@@ -634,7 +644,7 @@ func SkipFlagArgs(args []string) int {
return -1 return -1
} }
func checkFlag(arg string, i *int, verbose *bool, swflags map[string]bool) { func checkFlag(arg string, i *int, verbose, dbg *bool, swflags map[string]bool) {
if pos := strings.IndexByte(arg, '='); pos > 0 { if pos := strings.IndexByte(arg, '='); pos > 0 {
if verbose != nil && arg == "-v=true" { if verbose != nil && arg == "-v=true" {
*verbose = true *verbose = true
@@ -644,6 +654,8 @@ func checkFlag(arg string, i *int, verbose *bool, swflags map[string]bool) {
*i++ *i++
} else if verbose != nil && arg == "-v" { } else if verbose != nil && arg == "-v" {
*verbose = true *verbose = true
} else if dbg != nil && arg == "-dbg" {
*dbg = true
} }
} else { } else {
panic("unknown flag: " + arg) panic("unknown flag: " + arg)

View File

@@ -33,7 +33,7 @@ var (
) )
func Clean(args []string, conf *Config) { func Clean(args []string, conf *Config) {
flags, patterns, verbose := ParseArgs(args, cleanFlags) flags, patterns, verbose, _ := ParseArgs(args, cleanFlags)
cfg := &packages.Config{ cfg := &packages.Config{
Mode: loadSyntax | packages.NeedExportFile, Mode: loadSyntax | packages.NeedExportFile,
BuildFlags: flags, BuildFlags: flags,

View File

@@ -25,10 +25,13 @@ import (
llssa "github.com/goplus/llgo/ssa" llssa "github.com/goplus/llgo/ssa"
) )
func Init() { func Init(enableDbg bool) {
llssa.Initialize(llssa.InitAll) llssa.Initialize(llssa.InitAll)
llssa.SetDebug(llssa.DbgFlagAll) llssa.SetDebug(llssa.DbgFlagAll)
cl.SetDebug(cl.DbgFlagAll) cl.SetDebug(cl.DbgFlagAll)
if enableDbg {
cl.EnableDebugSymbols()
}
} }
func PkgPath(dir string) string { func PkgPath(dir string) string {

View File

@@ -55,7 +55,7 @@ type aCompilationUnit struct {
type CompilationUnit = *aCompilationUnit type CompilationUnit = *aCompilationUnit
func (b diBuilder) CreateCompileUnit(filename, dir string) CompilationUnit { func (b diBuilder) createCompileUnit(filename, dir string) CompilationUnit {
return &aCompilationUnit{ll: b.di.CreateCompileUnit(llvm.DICompileUnit{ return &aCompilationUnit{ll: b.di.CreateCompileUnit(llvm.DICompileUnit{
Language: llvm.DW_LANG_Go, Language: llvm.DW_LANG_Go,
File: filename, File: filename,
@@ -121,7 +121,6 @@ type aDIType struct {
type DIType = *aDIType type DIType = *aDIType
func (b diBuilder) createType(ty Type, pos token.Position) DIType { func (b diBuilder) createType(ty Type, pos token.Position) DIType {
fmt.Printf("Create type: %T, %v\n", ty.RawType(), ty.RawType())
var typ llvm.Metadata var typ llvm.Metadata
switch t := ty.RawType().(type) { switch t := ty.RawType().(type) {
case *types.Basic: case *types.Basic:
@@ -179,6 +178,8 @@ func (b diBuilder) createType(ty Type, pos token.Position) DIType {
return b.createBasicType(ty) return b.createBasicType(ty)
case *types.Array: case *types.Array:
return b.createBasicType(ty) return b.createBasicType(ty)
case *types.Chan:
return b.createBasicType(ty)
default: default:
panic(fmt.Errorf("can't create debug info of type: %v, %T", ty.RawType(), ty.RawType())) panic(fmt.Errorf("can't create debug info of type: %v, %T", ty.RawType(), ty.RawType()))
} }

View File

@@ -338,8 +338,6 @@ func (p Program) tyComplex128() llvm.Type {
// NewPackage creates a new package. // NewPackage creates a new package.
func (p Program) NewPackage(name, pkgPath string) Package { func (p Program) NewPackage(name, pkgPath string) Package {
mod := p.ctx.NewModule(pkgPath) mod := p.ctx.NewModule(pkgPath)
di := newDIBuilder(p, mod)
cu := di.CreateCompileUnit(name, pkgPath)
// TODO(xsw): Finalize may cause panic, so comment it. // TODO(xsw): Finalize may cause panic, so comment it.
// mod.Finalize() // mod.Finalize()
gbls := make(map[string]Global) gbls := make(map[string]Global)
@@ -354,7 +352,7 @@ func (p Program) NewPackage(name, pkgPath string) Package {
// p.needPyInit = false // p.needPyInit = false
ret := &aPackage{ ret := &aPackage{
mod: mod, vars: gbls, fns: fns, stubs: stubs, mod: mod, vars: gbls, fns: fns, stubs: stubs,
pyobjs: pyobjs, pymods: pymods, strs: strs, named: named, Prog: p, di: di, cu: cu} pyobjs: pyobjs, pymods: pymods, strs: strs, named: named, Prog: p, di: nil, cu: nil}
ret.abi.Init(pkgPath) ret.abi.Init(pkgPath)
return ret return ret
} }
@@ -711,13 +709,20 @@ func (p Package) AfterInit(b Builder, ret BasicBlock) {
} }
func (p Package) Finalize() { func (p Package) Finalize() {
p.di.Finalize() if p.di != nil {
p.di.Finalize()
}
} }
func (p Package) DIBuilder() diBuilder { func (p Package) DIBuilder() diBuilder {
return p.di return p.di
} }
func (p Package) EnableDebugSymbols(name, pkgPath string) {
p.di = newDIBuilder(p.Prog, p.mod)
p.cu = p.di.createCompileUnit(name, pkgPath)
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
/* /*