-
Notifications
You must be signed in to change notification settings - Fork 19
/
load.go
53 lines (46 loc) · 907 Bytes
/
load.go
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
package main
import (
"bufio"
"os"
"regexp"
"strings"
)
var (
startLinePattern = regexp.MustCompile(`^goroutine\s+(\d+)\s+\[(.*)\]:$`)
)
func load(fn string) (*GoroutineDump, error) {
fn = strings.Trim(fn, "\"")
f, err := os.Open(fn)
if err != nil {
return nil, err
}
defer f.Close()
dump := NewGoroutineDump()
var goroutine *Goroutine
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
if startLinePattern.MatchString(line) {
goroutine, err = NewGoroutine(line)
if err != nil {
return nil, err
}
dump.Add(goroutine)
} else if line == "" {
// End of a goroutine section.
if goroutine != nil {
goroutine.Freeze()
}
goroutine = nil
} else if goroutine != nil {
goroutine.AddLine(line)
}
}
if goroutine != nil {
goroutine.Freeze()
}
if err := scanner.Err(); err != nil {
return nil, err
}
return dump, nil
}