Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for DWARF based stack traces #881

Merged
merged 7 commits into from
Dec 5, 2022
Merged

Adds support for DWARF based stack traces #881

merged 7 commits into from
Dec 5, 2022

Conversation

mathetake
Copy link
Member

@mathetake mathetake commented Dec 2, 2022

This implements the support for DWARF-based stack traces. This new feature is enabled by
the experimental API experimental.DWARFBasedStackTraceEnabled and passing that context
to the runtime at compilation time.

For example, let's have the following program:

package main

func main() { a() }
func a() { b() }
func b() { c() }
func c() { panic("NOOOOOOOOOOOOOOO") }

and use experimental.DWARFBasedStackTraceEnabled during the compilation:

ctx := experimental.WithDWARFBasedStackTrace(ctx)
compiled, _ := r.CompileModule(ctx, wasmBinary)

Now, instantiate the compiled module, which results in a runtime error:

_, err = r.InstantiateModule(context.Background(), compiled, wazero.NewModuleConfig())
require.Error(t, err)
fmt.Println(err)

and we have the output like the following:

module[] function[_start] failed: wasm error: unreachable
wasm stack trace:
	.runtime._panic(i32)
		0x16e2: /opt/homebrew/Cellar/tinygo/0.26.0/src/runtime/runtime_tinygowasm.go:73:6
	.c()
		0x190b: /Users/mathetake/wazero/internal/testing/dwarftestdata/testdata/main.go:19:7
	.b()
		0x1901: /Users/mathetake/wazero/internal/testing/dwarftestdata/testdata/main.go:14:3
	.a()
		0x18f7: /Users/mathetake/wazero/internal/testing/dwarftestdata/testdata/main.go:9:3
	.main.main()
		0x18ed: /Users/mathetake/wazero/internal/testing/dwarftestdata/testdata/main.go:4:3
	.runtime.run()
		0x18cc: /opt/homebrew/Cellar/tinygo/0.26.0/src/runtime/scheduler_none.go:26:10
	._start()
		0x18b6: /opt/homebrew/Cellar/tinygo/0.26.0/src/runtime/runtime_wasm_wasi.go:22:5

from which we can easily infer how the execution reached the runtime error because the trace comes with the source code locations.

closes #58

@mathetake
Copy link
Member Author

        wasm stack trace:
        	.runtime._panic(i32)
        		 0x16e2: /opt/homebrew/Cellar/tinygo/0.26.0/src/runtime/runtime_tinygowasm.go:73:6
        	.main.c()
        		 0x190b: /Users/mathetake/wazero/experimental/testdata/dwarf.go:16:7
        	.main.b()
        		 0x1901: /Users/mathetake/wazero/experimental/testdata/dwarf.go:12:3
        	.main.a()
        		 0x18f7: /Users/mathetake/wazero/experimental/testdata/dwarf.go:8:3
        	.main.main()
        		 0x18ed: /Users/mathetake/wazero/experimental/testdata/dwarf.go:4:3
        	.runtime.run()
        		 0x18cc: /opt/homebrew/Cellar/tinygo/0.26.0/src/runtime/scheduler_none.go:26:10
        	._start()
        		 0x18b6: /opt/homebrew/Cellar/tinygo/0.26.0/src/runtime/runtime_wasm_wasi.go:22:5

Ok got the minimal PoC version has started working for interpreter!! next week refine it and will add support for compiler

@mathetake
Copy link
Member Author

almost there on the compiler - the impl looks much more straightforward than I initially thought!

@codefromthecrypt
Copy link
Contributor

thanks for not choosing something that would result in huge dwarf ;)

@mathetake
Copy link
Member Author

ok passed on compilers as well. backfilling tests.

Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
@mathetake mathetake changed the title DWARF based stack trace Adds support for DWARF based stack traces Dec 5, 2022
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
@mathetake mathetake marked this pull request as ready for review December 5, 2022 05:29
Copy link
Contributor

@codefromthecrypt codefromthecrypt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great to solve a highly demanded older issue!

only thing I'm worried about is if a module is compiled with the experimental flag on + compiler cache and ran with it off.. does it crash. visa versa also. Maybe add this to the tests.


type enableDWARFBasedStackTraceKey struct{}

// WithDWARFBasedStackTrace enables the DWARF based stack traces in the face of runtime errors.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this invalidate or otherwise make complication cache tricky? ex if the cache was built with this off and used when on or visa versa?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, will do it in another pr

experimental/dwarf.go Show resolved Hide resolved
internal/engine/compiler/engine.go Show resolved Hide resolved
internal/engine/compiler/engine.go Show resolved Hide resolved
internal/engine/compiler/engine_test.go Show resolved Hide resolved
internal/wasm/module.go Show resolved Hide resolved
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
@mathetake
Copy link
Member Author

will add the support via compilation cache later

Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
@mathetake mathetake merged commit 6c4dd1c into main Dec 5, 2022
@mathetake mathetake deleted the dwarf branch December 5, 2022 05:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement DWARF parser for better backtraces
2 participants