Skip to content

Commit

Permalink
Don't panic in thread dump parsing (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmoylan authored Apr 15, 2019
1 parent 3bb688d commit 6b96781
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
23 changes: 15 additions & 8 deletions wlog/diaglog/diag1log/thread_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,25 @@ func unmarshalFuncLine(funcLine []byte, frame *logging.StackFrameV1) {

func unmarshalFileLine(fileLine []byte, frame *logging.StackFrameV1) {
segments := strings.Split(string(bytes.TrimSpace(fileLine)), " +")
frame.Address = &segments[1]

if len(segments) > 1 {
frame.Address = &segments[1]
}

sepIdx := strings.LastIndex(segments[0], ":")
absPath := segments[0][:sepIdx]

file := gopath.TrimPrefix(absPath)
frame.File = &file
if sepIdx > -1 {
absPath := segments[0][:sepIdx]
file := gopath.TrimPrefix(absPath)
frame.File = &file
}

lineNumStr := segments[0][sepIdx+1:]
lineNum, err := strconv.Atoi(lineNumStr)
if err == nil {
frame.Line = &lineNum
if sepIdx+1 < len(segments[0]) {
lineNumStr := segments[0][sepIdx+1:]
lineNum, err := strconv.Atoi(lineNumStr)
if err == nil {
frame.Line = &lineNum
}
}
}

Expand Down
36 changes: 36 additions & 0 deletions wlog/diaglog/diag1log/thread_dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,42 @@ created by net/http.(*Transport).dialConn
},
},
},
{
Name: "missing fields",
Input: `goroutine 14 [select]:
net/http.(*persistConn).writeLoop(0xc0000bd0e0)
/usr/local/Cellar/go/1.11.2/libexec/src/net/http/transport.go:1885
created by net/http.(*Transport).dialConn
/usr/local/Cellar/go/1.11.2/libexec/src/net/http/transport.go +0x966
`,
Expected: logging.ThreadDumpV1{
Threads: []logging.ThreadInfoV1{
{
Name: strPtr("goroutine 14 [select]"),
Id: safelongPtr(14),
Params: map[string]interface{}{"status": "select"},
StackTrace: []logging.StackFrameV1{
{
Address: nil,
Procedure: strPtr("net/http.(*persistConn).writeLoop"),
File: strPtr("net/http/transport.go"),
Line: intPtr(1885),
Params: map[string]interface{}{},
},
{
Address: strPtr("0x966"),
Procedure: strPtr("net/http.(*Transport).dialConn"),
File: nil,
Line: nil,
Params: map[string]interface{}{
"goroutineCreator": true,
},
},
},
},
},
},
},
} {
t.Run(test.Name, func(t *testing.T) {
dump := diag1log.ThreadDumpV1FromGoroutines([]byte(test.Input))
Expand Down

0 comments on commit 6b96781

Please sign in to comment.