-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy patherror_tracking_debug_images_test.go
More file actions
217 lines (190 loc) · 6.97 KB
/
Copy patherror_tracking_debug_images_test.go
File metadata and controls
217 lines (190 loc) · 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package posthog
import (
"context"
"encoding/hex"
"encoding/json"
"log/slog"
"strconv"
"strings"
"testing"
"time"
)
// stubMainImage forces a known image (or none) so grouped-frame emission is
// deterministic regardless of the build environment. Tests using it must not
// run in parallel.
func stubMainImage(t *testing.T, info mainImageInfo) {
t.Helper()
prev := mainImageFn
mainImageFn = func() mainImageInfo { return info }
t.Cleanup(func() { mainImageFn = prev })
}
func coveringImage() mainImageInfo {
return mainImageInfo{
image: DebugImage{
Type: "elf",
DebugID: "eb985355-1cd0-2890-5a3d-85138a19cbf9",
ImageAddr: "0x400000",
ImageSize: 1 << 40,
Arch: "x86_64",
},
base: 0,
end: ^uint64(0),
ok: true,
}
}
func TestDebugIDFromGNUBuildID(t *testing.T) {
// Vector verified against symbolic's ElfObject::debug_id, which the
// PostHog server and CLI use: the first 16 bytes as a little-endian GUID.
buildID, err := hex.DecodeString("555398ebd01c90285a3d85138a19cbf9bbcec352")
if err != nil {
t.Fatal(err)
}
if got, want := debugIDFromGNUBuildID(buildID, true), "eb985355-1cd0-2890-5a3d-85138a19cbf9"; got != want {
t.Errorf("debugIDFromGNUBuildID() = %q, want %q", got, want)
}
// Big-endian ELF files keep the GUID fields unswapped, matching symbolic.
if got, want := debugIDFromGNUBuildID(buildID, false), "555398eb-d01c-9028-5a3d-85138a19cbf9"; got != want {
t.Errorf("debugIDFromGNUBuildID(BE) = %q, want %q", got, want)
}
// Short build ids are zero-padded to 16 bytes
if got, want := debugIDFromGNUBuildID([]byte{0xab, 0xcd}, true), "0000cdab-0000-0000-0000-000000000000"; got != want {
t.Errorf("debugIDFromGNUBuildID(short) = %q, want %q", got, want)
}
if got := debugIDFromGNUBuildID(nil, true); got != "" {
t.Errorf("debugIDFromGNUBuildID(nil) = %q, want empty", got)
}
}
func TestNativeImageArch(t *testing.T) {
// amd64 is Go-only vocabulary; arm64 is already the shared spelling.
if arch := nativeImageArch(); arch == "amd64" || arch == "aarch64" {
t.Errorf("nativeImageArch() = %q, want shared vocabulary", arch)
}
}
//go:noinline
func defaultCaptureSite() Exception {
return NewDefaultException(time.Now(), "user-1", "GroupTest", "grouped capture test")
}
// The real running executable, no stub: an integration check that discovered
// images are well formed and referenced by frames. Skips where the test
// binary carries no identity (Linux Go test binaries have no GNU build id).
func TestRealDebugImages(t *testing.T) {
exception := defaultCaptureSite()
if len(exception.DebugImages) == 0 {
t.Skip("no debug image for the test binary on this platform/build")
}
image := exception.DebugImages[0]
if image.Type != "elf" && image.Type != "macho" {
t.Errorf("image.Type = %q", image.Type)
}
if len(image.DebugID) < 36 {
t.Errorf("image.DebugID = %q, want uuid-shaped", image.DebugID)
}
if image.Arch == "amd64" || image.Arch == "aarch64" {
t.Errorf("image.Arch = %q, want shared vocabulary (x86_64/arm64)", image.Arch)
}
base, err := strconv.ParseUint(strings.TrimPrefix(image.ImageAddr, "0x"), 16, 64)
if err != nil || base == 0 {
t.Fatalf("image.ImageAddr = %q, want non-zero hex", image.ImageAddr)
}
// Frames in the main executable reference the image's address, and their
// instruction addresses fall inside its extent.
frames := exception.ExceptionList[0].Stacktrace.Frames
referenced := false
for _, frame := range frames {
if frame.ImageAddr != image.ImageAddr {
continue
}
referenced = true
addr, err := strconv.ParseUint(strings.TrimPrefix(frame.InstructionAddr, "0x"), 16, 64)
if err != nil {
t.Fatalf("frame.InstructionAddr = %q", frame.InstructionAddr)
}
if addr < base || addr >= base+image.ImageSize {
t.Errorf("frame addr %#x outside image [%#x, %#x)", addr, base, base+image.ImageSize)
}
}
if !referenced {
t.Error("no frame references the main image")
}
}
func TestExceptionAPIfyIncludesDebugImages(t *testing.T) {
stubMainImage(t, coveringImage())
exception := defaultCaptureSite()
exception.Type = "exception"
payload, err := json.Marshal(exception.APIfy())
if err != nil {
t.Fatal(err)
}
var decoded struct {
Properties struct {
DebugImages []DebugImage `json:"$debug_images"`
} `json:"properties"`
}
if err := json.Unmarshal(payload, &decoded); err != nil {
t.Fatal(err)
}
if len(decoded.Properties.DebugImages) != 1 {
t.Fatalf("expected $debug_images in payload, got %s", payload)
}
}
func TestExceptionApifyEventIncludesDebugImages(t *testing.T) {
stubMainImage(t, coveringImage())
exception := defaultCaptureSite()
ev := exception.apifyEvent()
images, ok := ev.properties["$debug_images"].([]DebugImage)
if !ok || len(images) != 1 {
t.Fatalf("expected $debug_images in v1 properties, got %v", ev.properties["$debug_images"])
}
// And absent (not an empty array) when there are none.
exception.DebugImages = nil
if _, present := exception.apifyEvent().properties["$debug_images"]; present {
t.Error("expected no $debug_images key without images")
}
}
func TestPanicFallbackStackTraceCarriesDebugImages(t *testing.T) {
stubMainImage(t, coveringImage())
// Unparseable panic stack: falls back to the default extractor, which
// emits native frames, so the images must come along.
stacktrace, images := stackTraceFromDebugStack([]byte("not a panic stack"))
if stacktrace == nil || len(stacktrace.Frames) == 0 {
t.Fatal("expected fallback frames")
}
if len(images) != 1 {
t.Errorf("expected debug images with the extractor fallback, got %v", images)
}
// Parseable stack: frames come from the panic text with no addresses, so
// no images are attached.
goroutineStack := []byte("goroutine 1 [running]:\npanic({0x0, 0x0})\n\t/goroot/src/runtime/panic.go:770 +0x1\nmain.crash(...)\n\t/app/main.go:10 +0x1\nmain.main()\n\t/app/main.go:5 +0x2\n")
stacktrace, images = stackTraceFromDebugStack(goroutineStack)
if stacktrace == nil || len(stacktrace.Frames) == 0 {
t.Fatal("expected parsed frames")
}
if images != nil {
t.Errorf("expected no debug images for parsed panic frames, got %v", images)
}
}
func TestSlogHandlerAttachesDebugImages(t *testing.T) {
stubMainImage(t, coveringImage())
client := &fakeEnqueueClient{}
next := &fakeNextSlogHandler{isEnabled: false}
handler := NewSlogCaptureHandler(next, client,
WithDistinctIDFn(func(_ context.Context, _ slog.Record) string { return "user-1" }),
)
if err := handler.Handle(context.Background(), createLogRecord(slog.LevelError, "boom")); err != nil {
t.Fatal(err)
}
if len(client.enqueuedMsgs) != 1 {
t.Fatalf("expected 1 enqueue, got %d", len(client.enqueuedMsgs))
}
exception, ok := client.enqueuedMsgs[0].(Exception)
if !ok {
t.Fatalf("expected Exception, got %T", client.enqueuedMsgs[0])
}
if len(exception.DebugImages) != 1 {
t.Fatalf("expected the default extractor to provide debug images")
}
frames := exception.ExceptionList[0].Stacktrace.Frames
if len(frames) == 0 || frames[len(frames)-1].Platform != "native" {
t.Errorf("expected native frames from the covered image")
}
}