debug: do not include inlined sources in frame counts (#2199)

Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
This commit is contained in:
Takeshi Yoneda
2024-04-30 17:23:46 +09:00
committed by GitHub
parent 60dbe962c1
commit b7df8b1130
2 changed files with 25 additions and 8 deletions

View File

@@ -109,7 +109,10 @@ func NewErrorBuilder() ErrorBuilder {
}
type stackTrace struct {
frames []string
// frameCount is the number of stack frame currently pushed into lines.
frameCount int
// lines contains the stack trace and possibly the inlined source code information.
lines []string
}
// GoRuntimeErrorTracePrefix is the prefix coming before the Go runtime stack trace included in the face of runtime.Error.
@@ -125,7 +128,7 @@ func (s *stackTrace) FromRecovered(recovered interface{}) error {
return exitErr
}
stack := strings.Join(s.frames, "\n\t")
stack := strings.Join(s.lines, "\n\t")
// If the error was internal, don't mention it was recovered.
if wasmErr, ok := recovered.(*wasmruntime.Error); ok {
@@ -152,12 +155,16 @@ const MaxFrames = 30
// AddFrame implements ErrorBuilder.AddFrame
func (s *stackTrace) AddFrame(funcName string, paramTypes, resultTypes []api.ValueType, sources []string) {
sig := signature(funcName, paramTypes, resultTypes)
s.frames = append(s.frames, sig)
for _, source := range sources {
s.frames = append(s.frames, "\t"+source)
if s.frameCount == MaxFrames {
return
}
if len(s.frames) == MaxFrames {
s.frames = append(s.frames, "... maybe followed by omitted frames")
s.frameCount++
sig := signature(funcName, paramTypes, resultTypes)
s.lines = append(s.lines, sig)
for _, source := range sources {
s.lines = append(s.lines, "\t"+source)
}
if s.frameCount == MaxFrames {
s.lines = append(s.lines, "... maybe followed by omitted frames")
}
}

View File

@@ -157,3 +157,13 @@ func (e testRuntimeErr) RuntimeError() {}
func (e testRuntimeErr) Error() string {
return string(e)
}
func Test_AddFrame_MaxFrame(t *testing.T) {
builder := NewErrorBuilder().(*stackTrace)
for i := 0; i < MaxFrames+10; i++ {
builder.AddFrame("x.y", nil, nil, []string{"a.go:1:2", "b.go:3:4"})
}
require.Equal(t, MaxFrames, builder.frameCount)
require.Equal(t, MaxFrames*3 /* frame + two inlined sources */ +1, len(builder.lines))
require.Equal(t, "... maybe followed by omitted frames", builder.lines[len(builder.lines)-1])
}