Skip to content

Commit 4f03669

Browse files
mvertesthehowl
andauthored
chore(gnovm): instrument debug tracing to display the caller site. (#3702)
Running the gnovm with the debug traces enabled (build tag `debug`) allows to display the details of preprocessing and opcodes operations. This PR adds the caller location in source (file + line) to each trace, allowing to identify the calling context of opcodes, to ease understanding the gnovm behavior. No change when debug is disabled (the default mode). Tracing can be activated by: `go run -tags debug ./cmd/gno run args` Before: ```console DEBUG: |||| -v (true <untyped> bool) DEBUG: EXEC: (const (println func(xs ...interface{})()))((const ("i:" string)), i<VPBlock(1,0)>) DEBUG: |||| -s bodyStmt[0/0/1]=(end) DEBUG: |||| +o OpPopResults DEBUG: ||||| +x (const (println func(xs ...interface{})()))((const ("i:" string)), i<VPBlock(1,0)>) DEBUG: ||||| +o OpEval DEBUG: |||||| -o OpEval DEBUG: EVAL: (*gnolang.CallExpr) (const (println func(xs ...interface{})()))((const ("i:" string)), i<VPBlock(1,0)>) DEBUG: ||||| +o OpPrecall DEBUG: |||||| +x i<VPBlock(1,0)> DEBUG: |||||| +o OpEval DEBUG: ||||||| +x (const ("i:" string)) DEBUG: ||||||| +o OpEval DEBUG: |||||||| +x (const (println func(xs ...interface{})())) DEBUG: |||||||| +o OpEval DEBUG: ||||||||| -o OpEval DEBUG: EVAL: (*gnolang.ConstExpr) (const (println func(xs ...interface{})())) DEBUG: |||||||| -x (const (println func(xs ...interface{})())) DEBUG: |||||||| +v (println func(xs ...interface{})()) DEBUG: |||||||| -o OpEval DEBUG: EVAL: (*gnolang.ConstExpr) (const ("i:" string)) DEBUG: ||||||| -x (const ("i:" string)) DEBUG: ||||||| +v ("i:" string) DEBUG: ||||||| -o OpEval ``` After: ```console DEBUG: op_exec.go:99 : |||| -v (true <untyped> bool) DEBUG: machine.go:1535: EXEC: (const (println func(xs ...interface{})()))((const ("i:" string)), i<VPBlock(1,0)>) DEBUG: op_exec.go:484 : |||| -s bodyStmt[0/0/1]=(end) DEBUG: op_exec.go:488 : |||| +o OpPopResults DEBUG: op_exec.go:493 : ||||| +x (const (println func(xs ...interface{})()))((const ("i:" string)), i<VPBlock(1,0)>) DEBUG: op_exec.go:494 : ||||| +o OpEval DEBUG: machine.go:1218: |||||| -o OpEval DEBUG: machine.go:1380: EVAL: (*gnolang.CallExpr) (const (println func(xs ...interface{})()))((const ("i:" string)), i<VPBlock(1,0)>) DEBUG: op_eval.go:243 : ||||| +o OpPrecall DEBUG: op_eval.go:247 : |||||| +x i<VPBlock(1,0)> DEBUG: op_eval.go:248 : |||||| +o OpEval DEBUG: op_eval.go:247 : ||||||| +x (const ("i:" string)) DEBUG: op_eval.go:248 : ||||||| +o OpEval DEBUG: op_eval.go:251 : |||||||| +x (const (println func(xs ...interface{})())) DEBUG: op_eval.go:252 : |||||||| +o OpEval DEBUG: machine.go:1218: ||||||||| -o OpEval DEBUG: machine.go:1380: EVAL: (*gnolang.ConstExpr) (const (println func(xs ...interface{})())) DEBUG: op_eval.go:317 : |||||||| -x (const (println func(xs ...interface{})())) DEBUG: op_eval.go:319 : |||||||| +v (println func(xs ...interface{})()) DEBUG: machine.go:1218: |||||||| -o OpEval DEBUG: machine.go:1380: EVAL: (*gnolang.ConstExpr) (const ("i:" string)) DEBUG: op_eval.go:317 : ||||||| -x (const ("i:" string)) DEBUG: op_eval.go:319 : ||||||| +v ("i:" string) DEBUG: machine.go:1218: ||||||| -o OpEval ``` --------- Co-authored-by: Morgan <[email protected]>
1 parent 07e3c49 commit 4f03669

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

gnovm/pkg/gnolang/debug.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package gnolang
33
import (
44
"fmt"
55
"net/http"
6+
"path"
7+
"runtime"
68
"strings"
79
"time"
810

@@ -48,15 +50,21 @@ var enabled bool = true
4850
func (debugging) Println(args ...interface{}) {
4951
if debug {
5052
if enabled {
51-
fmt.Println(append([]interface{}{"DEBUG:"}, args...)...)
53+
_, file, line, _ := runtime.Caller(2)
54+
caller := fmt.Sprintf("%-.12s:%-4d", path.Base(file), line)
55+
prefix := fmt.Sprintf("DEBUG: %17s: ", caller)
56+
fmt.Println(append([]interface{}{prefix}, args...)...)
5257
}
5358
}
5459
}
5560

5661
func (debugging) Printf(format string, args ...interface{}) {
5762
if debug {
5863
if enabled {
59-
fmt.Printf("DEBUG: "+format, args...)
64+
_, file, line, _ := runtime.Caller(2)
65+
caller := fmt.Sprintf("%.12s:%-4d", path.Base(file), line)
66+
prefix := fmt.Sprintf("DEBUG: %17s: ", caller)
67+
fmt.Printf(prefix+format, args...)
6068
}
6169
}
6270
}

gnovm/pkg/gnolang/machine.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package gnolang
33
import (
44
"fmt"
55
"io"
6+
"path"
67
"reflect"
8+
"runtime"
79
"slices"
810
"strconv"
911
"strings"
@@ -2120,17 +2122,23 @@ func (m *Machine) Panic(ex TypedValue) {
21202122
func (m *Machine) Println(args ...interface{}) {
21212123
if debug {
21222124
if enabled {
2123-
s := strings.Repeat("|", m.NumOps)
2124-
debug.Println(append([]interface{}{s}, args...)...)
2125+
_, file, line, _ := runtime.Caller(2) // get caller info
2126+
caller := fmt.Sprintf("%-.12s:%-4d", path.Base(file), line)
2127+
prefix := fmt.Sprintf("DEBUG: %17s: ", caller)
2128+
s := prefix + strings.Repeat("|", m.NumOps)
2129+
fmt.Println(append([]interface{}{s}, args...)...)
21252130
}
21262131
}
21272132
}
21282133

21292134
func (m *Machine) Printf(format string, args ...interface{}) {
21302135
if debug {
21312136
if enabled {
2132-
s := strings.Repeat("|", m.NumOps)
2133-
debug.Printf(s+" "+format, args...)
2137+
_, file, line, _ := runtime.Caller(2) // get caller info
2138+
caller := fmt.Sprintf("%-.12s:%-4d", path.Base(file), line)
2139+
prefix := fmt.Sprintf("DEBUG: %17s: ", caller)
2140+
s := prefix + strings.Repeat("|", m.NumOps)
2141+
fmt.Printf(s+" "+format, args...)
21342142
}
21352143
}
21362144
}

0 commit comments

Comments
 (0)