Skip to content

Commit 5f7467b

Browse files
Enables debug info (a.k.a. DWARF) by default (#924)
This makes DWARF enabled by default, with an opt-out flag. This didn't need to be experimental as the feature was requested for a long time and there's no API impact to using it. Signed-off-by: Adrian Cole <adrian@tetrate.io>
1 parent 126bd90 commit 5f7467b

File tree

7 files changed

+64
-65
lines changed

7 files changed

+64
-65
lines changed

cmd/wazero/wazero.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ import (
1212
"strings"
1313

1414
"github.com/tetratelabs/wazero"
15-
"github.com/tetratelabs/wazero/experimental"
1615
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
1716
"github.com/tetratelabs/wazero/sys"
1817
)
1918

20-
var ctx = experimental.WithDWARFBasedStackTrace(context.Background())
19+
var ctx = context.Background()
2120

2221
func main() {
2322
doMain(os.Stdout, os.Stderr, os.Exit)

cmd/wazero/wazero_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"path/filepath"
1010
"testing"
1111

12-
"github.com/tetratelabs/wazero/experimental"
1312
"github.com/tetratelabs/wazero/internal/testing/require"
1413
)
1514

@@ -158,7 +157,3 @@ func runMain(t *testing.T, args []string) (int, string, string) {
158157

159158
return exitCode, stdOut.String(), stdErr.String()
160159
}
161-
162-
func TestContext(t *testing.T) {
163-
require.True(t, experimental.DWARFBasedStackTraceEnabled(ctx))
164-
}

config.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,41 @@ type RuntimeConfig interface {
6666
//
6767
// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#grow-mem
6868
WithMemoryCapacityFromMax(memoryCapacityFromMax bool) RuntimeConfig
69+
70+
// WithDebugInfoEnabled toggles DWARF based stack traces in the face of
71+
// runtime errors. Defaults to true.
72+
//
73+
// Those who wish to disable this, can like so:
74+
//
75+
// r := wazero.NewRuntimeWithConfig(wazero.NewRuntimeConfig().WithDebugInfoEnabled(false)
76+
//
77+
// When disabled, a stack trace message looks like:
78+
//
79+
// wasm stack trace:
80+
// .runtime._panic(i32)
81+
// .myFunc()
82+
// .main.main()
83+
// .runtime.run()
84+
// ._start()
85+
//
86+
// When enabled, the stack trace includes source code information:
87+
//
88+
// wasm stack trace:
89+
// .runtime._panic(i32)
90+
// 0x16e2: /opt/homebrew/Cellar/tinygo/0.26.0/src/runtime/runtime_tinygowasm.go:73:6
91+
// .myFunc()
92+
// 0x190b: /Users/XXXXX/wazero/internal/testing/dwarftestdata/testdata/main.go:19:7
93+
// .main.main()
94+
// 0x18ed: /Users/XXXXX/wazero/internal/testing/dwarftestdata/testdata/main.go:4:3
95+
// .runtime.run()
96+
// 0x18cc: /opt/homebrew/Cellar/tinygo/0.26.0/src/runtime/scheduler_none.go:26:10
97+
// ._start()
98+
// 0x18b6: /opt/homebrew/Cellar/tinygo/0.26.0/src/runtime/runtime_wasm_wasi.go:22:5
99+
//
100+
// Note: This only takes into effect when the original Wasm binary has the
101+
// DWARF "custom sections" that are often stripped, depending on
102+
// optimization flags passed to the compiler.
103+
WithDebugInfoEnabled(bool) RuntimeConfig
69104
}
70105

71106
// NewRuntimeConfig returns a RuntimeConfig using the compiler if it is supported in this environment,
@@ -79,6 +114,7 @@ type runtimeConfig struct {
79114
memoryLimitPages uint32
80115
memoryCapacityFromMax bool
81116
isInterpreter bool
117+
dwarfDisabled bool // negative as defaults to enabled
82118
newEngine func(context.Context, api.CoreFeatures) wasm.Engine
83119
}
84120

@@ -87,6 +123,7 @@ var engineLessConfig = &runtimeConfig{
87123
enabledFeatures: api.CoreFeaturesV2,
88124
memoryLimitPages: wasm.MemoryLimitPages,
89125
memoryCapacityFromMax: false,
126+
dwarfDisabled: false,
90127
}
91128

92129
// NewRuntimeConfigCompiler compiles WebAssembly modules into
@@ -148,6 +185,13 @@ func (c *runtimeConfig) WithMemoryCapacityFromMax(memoryCapacityFromMax bool) Ru
148185
return ret
149186
}
150187

188+
// WithDebugInfoEnabled implements RuntimeConfig.WithDebugInfoEnabled
189+
func (c *runtimeConfig) WithDebugInfoEnabled(dwarfEnabled bool) RuntimeConfig {
190+
ret := c.clone()
191+
ret.dwarfDisabled = !dwarfEnabled
192+
return ret
193+
}
194+
151195
// CompiledModule is a WebAssembly module ready to be instantiated (Runtime.InstantiateModule) as an api.Module.
152196
//
153197
// In WebAssembly terminology, this is a decoded, validated, and possibly also compiled module. wazero avoids using

config_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ func TestRuntimeConfig(t *testing.T) {
5050
memoryCapacityFromMax: true,
5151
},
5252
},
53+
{
54+
name: "WithDebugInfoEnabled",
55+
with: func(c RuntimeConfig) RuntimeConfig {
56+
return c.WithDebugInfoEnabled(false)
57+
},
58+
expected: &runtimeConfig{
59+
dwarfDisabled: true, // dwarf is a more technical name and ok here.
60+
},
61+
},
5362
}
5463

5564
for _, tt := range tests {

experimental/dwarf_test.go renamed to dwarf_test.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package experimental_test
1+
package wazero_test
22

33
import (
44
"bufio"
@@ -8,25 +8,24 @@ import (
88
"testing"
99

1010
"github.com/tetratelabs/wazero"
11-
"github.com/tetratelabs/wazero/experimental"
1211
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
1312
"github.com/tetratelabs/wazero/internal/platform"
1413
"github.com/tetratelabs/wazero/internal/testing/dwarftestdata"
1514
"github.com/tetratelabs/wazero/internal/testing/require"
1615
)
1716

18-
func TestWithDWARFBasedStackTrace(t *testing.T) {
17+
func TestWithDebugInfo(t *testing.T) {
1918
ctx := context.Background()
20-
require.False(t, experimental.DWARFBasedStackTraceEnabled(ctx))
21-
ctx = experimental.WithDWARFBasedStackTrace(ctx)
22-
require.True(t, experimental.DWARFBasedStackTraceEnabled(ctx))
2319

2420
type testCase struct {
2521
name string
2622
r wazero.Runtime
2723
}
2824

29-
tests := []testCase{{name: "interpreter", r: wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfigInterpreter())}}
25+
tests := []testCase{{
26+
name: "interpreter",
27+
r: wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfigInterpreter()),
28+
}}
3029

3130
if platform.CompilerSupported() {
3231
tests = append(tests, testCase{
@@ -167,8 +166,7 @@ wasm stack trace:
167166
compiled, err := r.CompileModule(ctx, lang.bin)
168167
require.NoError(t, err)
169168

170-
// Use context.Background to ensure that DWARF is a compile-time option.
171-
_, err = r.InstantiateModule(context.Background(), compiled, wazero.NewModuleConfig())
169+
_, err = r.InstantiateModule(ctx, compiled, wazero.NewModuleConfig())
172170
require.Error(t, err)
173171

174172
errStr := err.Error()

experimental/dwarf.go

Lines changed: 0 additions & 47 deletions
This file was deleted.

runtime.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ func NewRuntimeWithConfig(ctx context.Context, rConfig RuntimeConfig) Runtime {
138138
memoryLimitPages: config.memoryLimitPages,
139139
memoryCapacityFromMax: config.memoryCapacityFromMax,
140140
isInterpreter: config.isInterpreter,
141+
dwarfDisabled: config.dwarfDisabled,
141142
}
142143
}
143144

@@ -149,6 +150,7 @@ type runtime struct {
149150
memoryLimitPages uint32
150151
memoryCapacityFromMax bool
151152
isInterpreter bool
153+
dwarfDisabled bool
152154
compiledModules []*compiledModule
153155
}
154156

@@ -172,9 +174,8 @@ func (r *runtime) CompileModule(ctx context.Context, binary []byte) (CompiledMod
172174
return nil, errors.New("invalid binary")
173175
}
174176

175-
dwarfEnabled := experimentalapi.DWARFBasedStackTraceEnabled(ctx)
176177
internal, err := binaryformat.DecodeModule(binary, r.enabledFeatures,
177-
r.memoryLimitPages, r.memoryCapacityFromMax, dwarfEnabled, false)
178+
r.memoryLimitPages, r.memoryCapacityFromMax, !r.dwarfDisabled, false)
178179
if err != nil {
179180
return nil, err
180181
} else if err = internal.Validate(r.enabledFeatures); err != nil {

0 commit comments

Comments
 (0)