-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
101 lines (85 loc) · 2.09 KB
/
main.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
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
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"path"
"strconv"
)
var (
flagSyscalls = flag.String("e", "", "only trace specified syscalls")
flagOutput = flag.String("o", "stracefile.json", "json output file")
flagTimeout = flag.Int64("t", 10, "strace timeout (secs)")
)
var (
// -f trace child processes
// -T time spent in each syscall
// -ttt timestamp of each event (microseconds)
// -qq don't display process exit status
defaultStraceArgs = []string{"-f", "-T", "-ttt", "-qq"}
)
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS] command\n", path.Base(os.Args[0]))
flag.PrintDefaults()
}
flag.Parse()
if len(flag.Args()) == 0 {
flag.Usage()
os.Exit(1)
}
// run strace
userStraceArgs := []string{}
if *flagSyscalls != "" {
userStraceArgs = append(userStraceArgs, "-e", *flagSyscalls)
}
userStraceArgs = append(userStraceArgs, flag.Args()...)
tmp, err := os.CreateTemp("", "stracefile")
if err != nil {
log.Fatal(err)
}
defer os.Remove(tmp.Name())
defaultStraceArgs = append(defaultStraceArgs, "-o", tmp.Name())
strace := Strace{
DefaultArgs: defaultStraceArgs,
UserArgs: userStraceArgs,
Timeout: *flagTimeout,
}
strace.Run()
// parse results
var events []*Event
preserved := make(map[string]*Event) // [pid+syscall]*Event
scanner := bufio.NewScanner(tmp)
for scanner.Scan() {
e := NewEvent(scanner.Text())
switch {
case e.Cat == "unfinished":
k := strconv.Itoa(e.Pid) + e.Name
preserved[k] = e
break
case e.Cat == "detached":
k := strconv.Itoa(e.Pid) + e.Name
p := preserved[k]
e.Args.First = p.Args.First
events = append(events, e)
delete(preserved, k)
break
case e.Cat == "other":
break
default:
events = append(events, e)
}
}
// add any unfinished/preserved traces to events
for _, p := range preserved {
p.Ph = "i" // instant event
events = append(events, p)
}
// save results
te := TraceEvents{events}
te.Save(*flagOutput)
fmt.Printf("[+] Trace file saved to: %s\n", *flagOutput)
fmt.Printf("[+] Analyze results: %s\n", "https://ui.perfetto.dev/")
}