-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexec.go
95 lines (82 loc) · 2.17 KB
/
exec.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
package perf
import (
"fmt"
"os/exec"
"syscall"
)
// command implements shared functionality between Command() and
// (*Group).Command().
func command(cmd *exec.Cmd, setupCounters func() error) error {
if cmd.SysProcAttr == nil {
cmd.SysProcAttr = &syscall.SysProcAttr{}
}
cmd.SysProcAttr.Ptrace = true
err := cmd.Start()
if err != nil {
return err
}
state, err := cmd.Process.Wait()
if err != nil {
// For good measure to avoid leaking a process.
_ = cmd.Process.Kill()
return err
}
if state.Sys().(syscall.WaitStatus).TrapCause() == -1 {
// For good measure to avoid leaking a process.
_ = cmd.Process.Kill()
_ = cmd.Wait()
return fmt.Errorf("tracee did not trap as expected")
}
// Note unusual error flow - if this fails, we still need to detach from
// the process and wait on it.
errCounters := setupCounters()
err = syscall.PtraceDetach(cmd.Process.Pid)
if err != nil {
// For good measure to avoid leaking a process.
_ = cmd.Process.Kill()
_ = cmd.Wait()
return err
}
err = cmd.Wait()
// Note unusual error flow - it's necessary need to detach and wait to
// avoid leaking a process, but if there was an error setting up the
// counters, this is what the caller needs to know about.
if errCounters != nil {
return errCounters
}
return err
}
// Command invokes the given exec.Cmd and measures the given counter,
// analogously to Measure().
func Command(a *Attr, cmd *exec.Cmd, cpu int, event *Event) (Count, error) {
var event2 *Event
err := command(cmd, func() (err2 error) {
event2, err2 = Open(a, cmd.Process.Pid, cpu, event)
if err2 != nil {
return err2
}
return event2.Enable()
})
if err != nil {
return Count{}, err
}
defer event2.Close()
return event2.ReadCount()
}
// Command invokes the given exec.Cmd and measures the given counter,
// analogously to MeasureGroup().
func (g *Group) Command(cmd *exec.Cmd, cpu int) (GroupCount, error) {
var event2 *Event
err := command(cmd, func() (err2 error) {
event2, err2 = g.Open(cmd.Process.Pid, cpu)
if err2 != nil {
return err2
}
return event2.Enable()
})
if err != nil {
return GroupCount{}, err
}
defer event2.Close()
return event2.ReadGroupCount()
}