2019-06-11 06:19:11 +00:00
|
|
|
// Copyright (C) 2019 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-01-23 07:38:55 +00:00
|
|
|
"context"
|
2019-06-11 06:19:11 +00:00
|
|
|
"errors"
|
2021-11-22 07:59:47 +00:00
|
|
|
"io"
|
2023-01-23 07:38:55 +00:00
|
|
|
"log"
|
2019-06-11 06:19:11 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2019-07-09 09:08:59 +00:00
|
|
|
"sync"
|
2019-06-11 06:19:11 +00:00
|
|
|
|
|
|
|
raven "github.com/getsentry/raven-go"
|
2022-10-01 20:03:14 +00:00
|
|
|
"github.com/maruel/panicparse/v2/stack"
|
2024-06-03 05:16:56 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/build"
|
2019-06-11 06:19:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const reportServer = "https://crash.syncthing.net/report/"
|
|
|
|
|
2019-06-29 06:50:09 +00:00
|
|
|
var loader = newGithubSourceCodeLoader()
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
raven.SetSourceCodeLoader(loader)
|
|
|
|
}
|
|
|
|
|
2019-07-09 09:08:59 +00:00
|
|
|
var (
|
|
|
|
clients = make(map[string]*raven.Client)
|
|
|
|
clientsMut sync.Mutex
|
|
|
|
)
|
|
|
|
|
2023-01-23 07:38:55 +00:00
|
|
|
type sentryService struct {
|
|
|
|
dsn string
|
|
|
|
inbox chan sentryRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
type sentryRequest struct {
|
|
|
|
reportID string
|
2023-09-20 06:39:01 +00:00
|
|
|
userID string
|
2023-01-23 07:38:55 +00:00
|
|
|
data []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sentryService) Serve(ctx context.Context) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case req := <-s.inbox:
|
|
|
|
pkt, err := parseCrashReport(req.reportID, req.data)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Failed to parse crash report:", err)
|
|
|
|
continue
|
|
|
|
}
|
2023-09-20 06:39:01 +00:00
|
|
|
if err := sendReport(s.dsn, pkt, req.userID); err != nil {
|
2023-01-23 07:38:55 +00:00
|
|
|
log.Println("Failed to send crash report:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-20 06:39:01 +00:00
|
|
|
func (s *sentryService) Send(reportID, userID string, data []byte) bool {
|
2023-01-23 07:38:55 +00:00
|
|
|
select {
|
2023-09-20 06:39:01 +00:00
|
|
|
case s.inbox <- sentryRequest{reportID, userID, data}:
|
2023-01-23 07:38:55 +00:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-07 08:05:13 +00:00
|
|
|
func sendReport(dsn string, pkt *raven.Packet, userID string) error {
|
2020-04-07 11:19:49 +00:00
|
|
|
pkt.Interfaces = append(pkt.Interfaces, &raven.User{ID: userID})
|
|
|
|
|
2019-07-09 09:08:59 +00:00
|
|
|
clientsMut.Lock()
|
|
|
|
defer clientsMut.Unlock()
|
|
|
|
|
|
|
|
cli, ok := clients[dsn]
|
|
|
|
if !ok {
|
2020-10-07 08:05:13 +00:00
|
|
|
var err error
|
2019-07-09 09:08:59 +00:00
|
|
|
cli, err = raven.New(dsn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
clients[dsn] = cli
|
2019-06-11 06:19:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The client sets release and such on the packet before sending, in the
|
|
|
|
// misguided idea that it knows this better than than the packet we give
|
|
|
|
// it. So we copy the values from the packet to the client first...
|
|
|
|
cli.SetRelease(pkt.Release)
|
|
|
|
cli.SetEnvironment(pkt.Environment)
|
|
|
|
|
2019-07-09 09:08:59 +00:00
|
|
|
defer cli.Wait()
|
2019-06-11 06:19:11 +00:00
|
|
|
_, errC := cli.Capture(pkt, nil)
|
|
|
|
return <-errC
|
|
|
|
}
|
|
|
|
|
2020-10-07 08:05:13 +00:00
|
|
|
func parseCrashReport(path string, report []byte) (*raven.Packet, error) {
|
2019-06-11 06:19:11 +00:00
|
|
|
parts := bytes.SplitN(report, []byte("\n"), 2)
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return nil, errors.New("no first line")
|
|
|
|
}
|
|
|
|
|
2024-06-03 05:16:56 +00:00
|
|
|
version, err := build.ParseVersion(string(parts[0]))
|
2019-06-11 06:19:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
report = parts[1]
|
|
|
|
|
|
|
|
foundPanic := false
|
|
|
|
var subjectLine []byte
|
|
|
|
for {
|
|
|
|
parts = bytes.SplitN(report, []byte("\n"), 2)
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return nil, errors.New("no panic line found")
|
|
|
|
}
|
|
|
|
|
|
|
|
line := parts[0]
|
|
|
|
report = parts[1]
|
|
|
|
|
|
|
|
if foundPanic {
|
|
|
|
// The previous line was our "Panic at ..." header. We are now
|
|
|
|
// at the beginning of the real panic trace and this is our
|
|
|
|
// subject line.
|
|
|
|
subjectLine = line
|
|
|
|
break
|
|
|
|
} else if bytes.HasPrefix(line, []byte("Panic at")) {
|
|
|
|
foundPanic = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r := bytes.NewReader(report)
|
2022-10-01 20:03:14 +00:00
|
|
|
ctx, _, err := stack.ScanSnapshot(r, io.Discard, stack.DefaultOpts())
|
|
|
|
if err != nil && err != io.EOF {
|
2019-06-11 06:19:11 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-10-01 20:03:14 +00:00
|
|
|
if ctx == nil || len(ctx.Goroutines) == 0 {
|
|
|
|
return nil, errors.New("no goroutines found")
|
|
|
|
}
|
2019-06-11 06:19:11 +00:00
|
|
|
|
2019-06-29 06:50:09 +00:00
|
|
|
// Lock the source code loader to the version we are processing here.
|
2024-06-03 05:16:56 +00:00
|
|
|
if version.Commit != "" {
|
2019-06-29 06:50:09 +00:00
|
|
|
// We have a commit hash, so we know exactly which source to use
|
2024-06-03 05:16:56 +00:00
|
|
|
loader.LockWithVersion(version.Commit)
|
|
|
|
} else if strings.HasPrefix(version.Tag, "v") {
|
2019-06-29 06:50:09 +00:00
|
|
|
// Lets hope the tag is close enough
|
2024-06-03 05:16:56 +00:00
|
|
|
loader.LockWithVersion(version.Tag)
|
2019-06-29 06:50:09 +00:00
|
|
|
} else {
|
|
|
|
// Last resort
|
2020-06-16 05:01:55 +00:00
|
|
|
loader.LockWithVersion("main")
|
2019-06-29 06:50:09 +00:00
|
|
|
}
|
|
|
|
defer loader.Unlock()
|
|
|
|
|
2019-06-11 06:19:11 +00:00
|
|
|
var trace raven.Stacktrace
|
|
|
|
for _, gr := range ctx.Goroutines {
|
|
|
|
if gr.First {
|
|
|
|
trace.Frames = make([]*raven.StacktraceFrame, len(gr.Stack.Calls))
|
|
|
|
for i, sc := range gr.Stack.Calls {
|
2022-10-01 20:03:14 +00:00
|
|
|
trace.Frames[len(trace.Frames)-1-i] = raven.NewStacktraceFrame(0, sc.Func.Name, sc.RemoteSrcPath, sc.Line, 3, nil)
|
2019-06-11 06:19:11 +00:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:16:32 +00:00
|
|
|
pkt := packet(version, "crash")
|
2020-10-07 08:05:13 +00:00
|
|
|
pkt.Message = string(subjectLine)
|
|
|
|
pkt.Extra = raven.Extra{
|
|
|
|
"url": reportServer + path,
|
2020-06-03 13:00:46 +00:00
|
|
|
}
|
2020-10-07 08:05:13 +00:00
|
|
|
pkt.Interfaces = []raven.Interface{&trace}
|
2020-10-08 15:37:45 +00:00
|
|
|
pkt.Fingerprint = crashReportFingerprint(pkt.Message)
|
2019-06-11 06:19:11 +00:00
|
|
|
|
|
|
|
return pkt, nil
|
|
|
|
}
|
|
|
|
|
2020-10-08 15:37:45 +00:00
|
|
|
var (
|
|
|
|
indexRe = regexp.MustCompile(`\[[-:0-9]+\]`)
|
|
|
|
sizeRe = regexp.MustCompile(`(length|capacity) [0-9]+`)
|
|
|
|
ldbPosRe = regexp.MustCompile(`(\(pos=)([0-9]+)\)`)
|
|
|
|
ldbChecksumRe = regexp.MustCompile(`(want=0x)([a-z0-9]+)( got=0x)([a-z0-9]+)`)
|
|
|
|
ldbFileRe = regexp.MustCompile(`(\[file=)([0-9]+)(\.ldb\])`)
|
|
|
|
ldbInternalKeyRe = regexp.MustCompile(`(internal key ")[^"]+(", len=)[0-9]+`)
|
|
|
|
ldbPathRe = regexp.MustCompile(`(open|write|read) .+[\\/].+[\\/]index[^\\/]+[\\/][^\\/]+: `)
|
|
|
|
)
|
|
|
|
|
2021-07-22 09:16:24 +00:00
|
|
|
func sanitizeMessageLDB(message string) string {
|
2020-10-08 15:37:45 +00:00
|
|
|
message = ldbPosRe.ReplaceAllString(message, "${1}x)")
|
|
|
|
message = ldbFileRe.ReplaceAllString(message, "${1}x${3}")
|
|
|
|
message = ldbChecksumRe.ReplaceAllString(message, "${1}X${3}X")
|
|
|
|
message = ldbInternalKeyRe.ReplaceAllString(message, "${1}x${2}x")
|
|
|
|
message = ldbPathRe.ReplaceAllString(message, "$1 x: ")
|
2021-07-22 09:16:24 +00:00
|
|
|
return message
|
|
|
|
}
|
|
|
|
|
|
|
|
func crashReportFingerprint(message string) []string {
|
|
|
|
// Do not fingerprint on the stack in case of db corruption or fatal
|
|
|
|
// db io error - where it occurs doesn't matter.
|
|
|
|
orig := message
|
|
|
|
message = sanitizeMessageLDB(message)
|
2020-10-08 15:37:45 +00:00
|
|
|
if message != orig {
|
|
|
|
return []string{message}
|
|
|
|
}
|
|
|
|
|
|
|
|
message = indexRe.ReplaceAllString(message, "[x]")
|
|
|
|
message = sizeRe.ReplaceAllString(message, "$1 x")
|
|
|
|
|
|
|
|
// {{ default }} is what sentry uses as a fingerprint by default. While
|
|
|
|
// never specified, the docs point at this being some hash derived from the
|
|
|
|
// stack trace. Here we include the filtered panic message on top of that.
|
|
|
|
// https://docs.sentry.io/platforms/go/data-management/event-grouping/sdk-fingerprinting/#basic-example
|
|
|
|
return []string{"{{ default }}", message}
|
|
|
|
}
|
|
|
|
|
2024-06-03 05:16:56 +00:00
|
|
|
func packet(version build.VersionParts, reportType string) *raven.Packet {
|
2020-10-07 08:05:13 +00:00
|
|
|
pkt := &raven.Packet{
|
|
|
|
Platform: "go",
|
2024-06-03 05:16:56 +00:00
|
|
|
Release: version.Tag,
|
|
|
|
Environment: version.Environment(),
|
2020-10-07 08:05:13 +00:00
|
|
|
Tags: raven.Tags{
|
2024-06-03 05:16:56 +00:00
|
|
|
raven.Tag{Key: "version", Value: version.Version},
|
|
|
|
raven.Tag{Key: "tag", Value: version.Tag},
|
|
|
|
raven.Tag{Key: "codename", Value: version.Codename},
|
|
|
|
raven.Tag{Key: "runtime", Value: version.Runtime},
|
|
|
|
raven.Tag{Key: "goos", Value: version.GOOS},
|
|
|
|
raven.Tag{Key: "goarch", Value: version.GOARCH},
|
|
|
|
raven.Tag{Key: "builder", Value: version.Builder},
|
2021-02-18 13:16:32 +00:00
|
|
|
raven.Tag{Key: "report_type", Value: reportType},
|
2020-10-07 08:05:13 +00:00
|
|
|
},
|
|
|
|
}
|
2024-06-03 05:16:56 +00:00
|
|
|
if version.Commit != "" {
|
|
|
|
pkt.Tags = append(pkt.Tags, raven.Tag{Key: "commit", Value: version.Commit})
|
2020-10-07 08:05:13 +00:00
|
|
|
}
|
2024-06-03 05:16:56 +00:00
|
|
|
for _, tag := range version.Extra {
|
2020-10-07 08:05:13 +00:00
|
|
|
pkt.Tags = append(pkt.Tags, raven.Tag{Key: tag, Value: "1"})
|
|
|
|
}
|
|
|
|
return pkt
|
|
|
|
}
|