reverse lone filenames as well

I've wanted this to more easily debug build failures.

To not force a build failure in a test script, as that would require
some trickery to remain stable, we use runtime.Caller without printing
the line number. Before this patch, those filenames without line numbers
would not be reversed at all.
pull/336/head
Daniel Martí 3 years ago committed by lu4p
parent b6dee63b32
commit adb4f44fb2

@ -110,21 +110,29 @@ func commandReverse(args []string) error {
}
case *ast.CallExpr:
// continues below
default:
return true
// Reverse position information of call sites.
pos := fset.Position(node.Pos())
origPos := fmt.Sprintf("%s:%d", goFile, pos.Offset)
newFilename := hashWith(lpkg.GarbleActionID, origPos) + ".go"
// Do "obfuscated.go:1", corresponding to the call site's line.
// Most common in stack traces.
replaces = append(replaces,
newFilename+":1",
fmt.Sprintf("%s/%s:%d", lpkg.ImportPath, goFile, pos.Line),
)
// Do "obfuscated.go" as a fallback.
// Most useful in build errors in obfuscated code,
// since those might land on any line.
// Any ":N" line number will end up being useless,
// but at least the filename will be correct.
replaces = append(replaces,
newFilename,
fmt.Sprintf("%s/%s", lpkg.ImportPath, goFile),
)
}
// Reverse position information.
pos := fset.Position(node.Pos())
origPos := fmt.Sprintf("%s:%d", goFile, pos.Offset)
newPos := hashWith(lpkg.GarbleActionID, origPos) + ".go:1"
replaces = append(replaces,
newPos,
fmt.Sprintf("%s/%s:%d", lpkg.ImportPath, goFile, pos.Line),
)
return true
})
}

@ -56,12 +56,17 @@ package main
import (
"os"
"runtime"
"test/main/lib"
)
func main() {
unexportedMainFunc()
_, filename, _, _ := runtime.Caller(0)
println()
println("main filename:", filename)
}
func unexportedMainFunc() {
@ -79,12 +84,17 @@ package lib
import (
"io"
"regexp"
"runtime"
"runtime/debug"
)
type ExportedLibType struct{}
func (*ExportedLibType) ExportedLibMethod(w io.Writer) error {
_, filename, _, _ := runtime.Caller(0)
println("lib filename:", filename)
println()
return printStackTrace(w)
}
@ -121,19 +131,23 @@ var _ = reflect.TypeOf(UnobfuscatedStruct{})
var _ = struct{SomeField int}(UnobfuscatedStruct{})
-- reverse.stdout --
lib filename: test/main/lib/lib.go
goroutine 1 [running]:
runtime/debug.Stack(0x??, 0x??, 0x??)
runtime/debug/stack.go:24 +0x??
test/main/lib.printStackTrace(0x??, 0x??, 0x??, 0x??)
test/main/lib/lib.go:23 +0x??
test/main/lib.(*ExportedLibType).ExportedLibMethod(...)
test/main/lib/lib.go:12
main.unexportedMainFunc.func1()
test/main/main.go:16 +0x??
test/main/lib/lib.go:28 +0x??
test/main/lib.(*ExportedLibType).ExportedLibMethod(0x??, 0x??, 0x??, 0x??, 0x??)
test/main/lib/lib.go:17 +0x??
main.unexportedMainFunc.func1(...)
test/main/main.go:21
main.unexportedMainFunc()
test/main/main.go:20 +0x??
test/main/main.go:25 +0x??
main.main()
test/main/main.go:10 +0x??
test/main/main.go:11 +0x??
main filename: test/main/main.go
-- build-error-reverse.stdout --
# test/main/build-error
test/main/build-error/error.go:18: cannot convert UnobfuscatedStruct{} (type UnobfuscatedStruct) to type struct { SomeField int }

Loading…
Cancel
Save