From dad22b16863b4d77bd2c60b2e319f51eece46648 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Sat, 21 Sep 2024 21:09:16 +0800 Subject: [PATCH] cl: switch debug symbols with LLGO_DEBUG --- _lldb/README.md | 6 ++++-- _lldb/common.sh | 10 ++++++---- _lldb/runlldb.sh | 12 +++--------- _lldb/test.py | 14 ++++---------- chore/gentests/gentests.go | 8 ++------ chore/llgen/llgen.go | 13 ++++--------- cl/cltest/cltest.go | 4 ++-- cl/compile.go | 8 ++------ internal/build/build.go | 30 ++++++++++++++---------------- internal/build/clean.go | 2 +- internal/llgen/llgen.go | 4 +--- 11 files changed, 43 insertions(+), 68 deletions(-) diff --git a/_lldb/README.md b/_lldb/README.md index bee02779..1f6a4946 100644 --- a/_lldb/README.md +++ b/_lldb/README.md @@ -3,15 +3,17 @@ ### Build with debug info ```shell -llgo build -o cl/_testdata/debug/out -dbg ./cl/_testdata/debug +LLGO_DEBUG=1 llgo build -o cl/_testdata/debug/out ./cl/_testdata/debug ``` ### Debug with lldb ```shell -lldb -O "command script import _lldb/llgo_plugin.py" ./cl/_testdata/debug/out +_lldb/runlldb.sh ./cl/_testdata/debug/out ``` +or + ```shell /opt/homebrew/bin/lldb -O "command script import _lldb/llgo_plugin.py" ./cl/_testdata/debug/out # github.com/goplus/llgo/cl/_testdata/debug diff --git a/_lldb/common.sh b/_lldb/common.sh index b6fa506c..94dfe334 100644 --- a/_lldb/common.sh +++ b/_lldb/common.sh @@ -11,7 +11,8 @@ find_lldb() { for lldb_path in "${lldb_paths[@]}"; do if command -v "$lldb_path" >/dev/null 2>&1; then - local version=$("$lldb_path" --version | grep -oE '[0-9]+' | head -1) + local version + version=$("$lldb_path" --version | grep -oE '[0-9]+' | head -1) if [ "$version" -ge 18 ]; then echo "$lldb_path" return 0 @@ -25,12 +26,13 @@ find_lldb() { # Find LLDB 18+ LLDB_PATH=$(find_lldb) +export LLDB_PATH # Default package path -DEFAULT_PACKAGE_PATH="./cl/_testdata/debug" +export DEFAULT_PACKAGE_PATH="./cl/_testdata/debug" # Function to build the project build_project() { local package_path="$1" - go run ./cmd/llgo build -o "${package_path}/out" -dbg "${package_path}" -} \ No newline at end of file + LLGO_DEBUG=1 go run ./cmd/llgo build -o "${package_path}/out" "${package_path}" +} diff --git a/_lldb/runlldb.sh b/_lldb/runlldb.sh index efeeabed..39da6860 100755 --- a/_lldb/runlldb.sh +++ b/_lldb/runlldb.sh @@ -3,16 +3,10 @@ set -e # Source common functions and variables +# shellcheck source=./_lldb/common.sh source "$(dirname "$0")/common.sh" -# Check if a package path is provided as an argument -package_path="$DEFAULT_PACKAGE_PATH" -if [ $# -eq 1 ]; then - package_path="$1" -fi - -# Build the project -build_project "$package_path" +executable="$1" # Run LLDB -"$LLDB_PATH" "${package_path}/out" +"$LLDB_PATH" "$executable" diff --git a/_lldb/test.py b/_lldb/test.py index 849f2a03..dece6db8 100644 --- a/_lldb/test.py +++ b/_lldb/test.py @@ -91,7 +91,7 @@ class LLDBDebugger: bp = self.target.BreakpointCreateByLocation(file_spec, line_number) if not bp.IsValid(): raise LLDBTestException(f"Failed to set breakpoint at { - file_spec}:{line_number}") + file_spec}: {line_number}") return bp def run_to_breakpoint(self) -> None: @@ -225,7 +225,7 @@ def execute_tests(executable_path: str, test_cases: List[TestCase], verbose: boo try: if verbose: log(f"Setting breakpoint at { - test_case.source_file}:{test_case.end_line}") + test_case.source_file}: {test_case.end_line}") debugger.setup() debugger.set_breakpoint(test_case.source_file, test_case.end_line) debugger.run_to_breakpoint() @@ -269,6 +269,7 @@ def run_tests(executable_path: str, source_files: List[str], verbose: bool, inte results = execute_tests(executable_path, test_cases, verbose, interactive, plugin_path) + print_test_results(results) if results.total != results.passed: os._exit(1) @@ -331,14 +332,7 @@ def execute_single_variable_test(debugger: LLDBDebugger, test: Test) -> TestResu ) -def print_test_results(results: TestResults, verbose: bool) -> None: - for case_result in results.case_results: - case = case_result.test_case - loc = f"{case.source_file}:{case.start_line}-{case.end_line}" - log(f"\nTest case: {loc} in function '{case_result.function}'") - for result in case_result.results: - print_test_result(result, verbose) - +def print_test_results(results: TestResults) -> None: log("\nTest results:") log(f" Total tests: {results.total}") log(f" Passed tests: {results.passed}") diff --git a/chore/gentests/gentests.go b/chore/gentests/gentests.go index ca9ec331..ea465748 100644 --- a/chore/gentests/gentests.go +++ b/chore/gentests/gentests.go @@ -66,13 +66,9 @@ func llgenDir(dir string, pkgPath ...string) { } testDir := dir + "/" + name fmt.Fprintln(os.Stderr, "llgen", testDir) - os.Chdir(testDir) + check(os.Chdir(testDir)) dbg := isDbgSymEnabled("flags.txt") - if dbg { - cl.EnableDebugSymbols() - } else { - cl.DisableDebugSymbols() - } + cl.EnableDebugSymbols(dbg) llgen.SmartDoFile("in.go", pkgPath...) } diff --git a/chore/llgen/llgen.go b/chore/llgen/llgen.go index 7def38fe..d93e8d9d 100644 --- a/chore/llgen/llgen.go +++ b/chore/llgen/llgen.go @@ -17,24 +17,19 @@ package main import ( - "flag" "fmt" "os" + "github.com/goplus/llgo/internal/build" "github.com/goplus/llgo/internal/llgen" ) -var ( - enableDbg = flag.Bool("dbg", false, "enable debug symbols") -) - func main() { - flag.Parse() - if len(flag.Args()) < 1 { + if len(os.Args) < 2 { fmt.Fprintln(os.Stderr, "Usage: llgen [flags] [pkgPath]") return } - llgen.Init(*enableDbg) - args := flag.Args() + llgen.Init(build.IsDebugEnabled()) + args := os.Args[1:] llgen.SmartDoFile(args[0], args[1:]...) } diff --git a/cl/cltest/cltest.go b/cl/cltest/cltest.go index f25e25b3..bd0b0178 100644 --- a/cl/cltest/cltest.go +++ b/cl/cltest/cltest.go @@ -130,10 +130,10 @@ func testFrom(t *testing.T, pkgDir, sel string, byLLGen bool) { out := pkgDir + "/out.ll" dbg := isDbgSymEnabled(pkgDir + "/flags.txt") if dbg { - cl.EnableDebugSymbols() + cl.EnableDebugSymbols(true) + defer cl.EnableDebugSymbols(false) cl.DebugSymbols() // just for coverage } - defer cl.DisableDebugSymbols() b, err := os.ReadFile(out) if err != nil { t.Fatal("ReadFile failed:", err) diff --git a/cl/compile.go b/cl/compile.go index 82b09638..cb697c7e 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -58,12 +58,8 @@ func SetDebug(dbgFlags dbgFlags) { } // EnableDebugSymbols enables debug symbols. -func EnableDebugSymbols() { - debugSymbols = true -} - -func DisableDebugSymbols() { - debugSymbols = false +func EnableDebugSymbols(b bool) { + debugSymbols = b } func DebugSymbols() bool { diff --git a/internal/build/build.go b/internal/build/build.go index a09f3e91..7e970a78 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -120,10 +120,8 @@ const ( ) func Do(args []string, conf *Config) { - flags, patterns, verbose, dbg := ParseArgs(args, buildFlags) - if dbg { - cl.EnableDebugSymbols() - } + flags, patterns, verbose := ParseArgs(args, buildFlags) + cl.EnableDebugSymbols(IsDebugEnabled()) flags = append(flags, "-tags", "llgo") cfg := &packages.Config{ Mode: loadSyntax | packages.NeedDeps | packages.NeedModule | packages.NeedExportFile, @@ -602,7 +600,6 @@ var ( "-cover": false, // -cover: enable coverage analysis "-covermode": true, // -covermode mode: set the mode for coverage analysis "-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 "-x": false, // -x: print the commands "-tags": true, // -tags 'tag,list': a space-separated list of build tags to consider satisfied during the build @@ -611,19 +608,22 @@ var ( } ) -func ParseArgs(args []string, swflags map[string]bool) (flags, patterns []string, verbose, dbg bool) { +const llgoDebug = "LLGO_DEBUG" + +func IsDebugEnabled() bool { + llgoDbgVal := strings.ToLower(os.Getenv(llgoDebug)) + return llgoDbgVal == "1" || llgoDbgVal == "true" || llgoDbgVal == "on" +} + +func ParseArgs(args []string, swflags map[string]bool) (flags, patterns []string, verbose bool) { n := len(args) for i := 0; i < n; i++ { arg := args[i] if strings.HasPrefix(arg, "-") { - checkFlag(arg, &i, &verbose, &dbg, swflags) + checkFlag(arg, &i, &verbose, swflags) } else { patterns = args[i:] - for _, arg := range args[:i] { - if arg != "-dbg" { - flags = append(flags, arg) - } - } + flags = args[:i] return } } @@ -635,7 +635,7 @@ func SkipFlagArgs(args []string) int { for i := 0; i < n; i++ { arg := args[i] if strings.HasPrefix(arg, "-") { - checkFlag(arg, &i, nil, nil, buildFlags) + checkFlag(arg, &i, nil, buildFlags) } else { return i } @@ -643,7 +643,7 @@ func SkipFlagArgs(args []string) int { return -1 } -func checkFlag(arg string, i *int, verbose, dbg *bool, swflags map[string]bool) { +func checkFlag(arg string, i *int, verbose *bool, swflags map[string]bool) { if pos := strings.IndexByte(arg, '='); pos > 0 { if verbose != nil && arg == "-v=true" { *verbose = true @@ -653,8 +653,6 @@ func checkFlag(arg string, i *int, verbose, dbg *bool, swflags map[string]bool) *i++ } else if verbose != nil && arg == "-v" { *verbose = true - } else if dbg != nil && arg == "-dbg" { - *dbg = true } } else { panic("unknown flag: " + arg) diff --git a/internal/build/clean.go b/internal/build/clean.go index ed0130de..60ec5f46 100644 --- a/internal/build/clean.go +++ b/internal/build/clean.go @@ -33,7 +33,7 @@ var ( ) func Clean(args []string, conf *Config) { - flags, patterns, verbose, _ := ParseArgs(args, cleanFlags) + flags, patterns, verbose := ParseArgs(args, cleanFlags) cfg := &packages.Config{ Mode: loadSyntax | packages.NeedExportFile, BuildFlags: flags, diff --git a/internal/llgen/llgen.go b/internal/llgen/llgen.go index 90da51a3..9b6de2e6 100644 --- a/internal/llgen/llgen.go +++ b/internal/llgen/llgen.go @@ -29,9 +29,7 @@ func Init(enableDbg bool) { llssa.Initialize(llssa.InitAll) llssa.SetDebug(llssa.DbgFlagAll) cl.SetDebug(cl.DbgFlagAll) - if enableDbg { - cl.EnableDebugSymbols() - } + cl.EnableDebugSymbols(enableDbg) } func PkgPath(dir string) string {