-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.go
More file actions
63 lines (59 loc) · 1.47 KB
/
filter.go
File metadata and controls
63 lines (59 loc) · 1.47 KB
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
package main
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
)
func newFilterCmd() *cobra.Command {
var shared sharedFlags
var method string
var inclCallers bool
cmd := &cobra.Command{
Use: "filter <file>",
Short: "Output stacks passing through a method (-m required)",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if method == "" {
return fmt.Errorf("-m/--method required")
}
pctx, err := preprocessProfile(shared.toOpts(args[0], "filter"))
if err != nil {
return err
}
cmdFilter(pctx.sf, method, inclCallers)
return nil
},
}
shared.register(cmd)
cmd.Flags().StringVarP(&method, "method", "m", "", "Substring match on method name (required)")
cmd.Flags().BoolVar(&inclCallers, "include-callers", false, "Include caller frames in output")
return cmd
}
func cmdFilter(sf *stackFile, method string, includeCallers bool) {
if sf.totalSamples == 0 {
fmt.Fprintln(os.Stdout, "no samples (empty profile or all filtered out)")
return
}
matched := 0
for i := range sf.stacks {
st := &sf.stacks[i]
for j, fr := range st.frames {
if matchesMethod(fr, method) {
var outFrames []string
if includeCallers {
outFrames = st.frames
} else {
outFrames = st.frames[j:]
}
tp := threadPrefix(st.thread)
fmt.Printf("%s%s %d\n", tp, strings.Join(outFrames, ";"), st.count)
matched++
break
}
}
}
if matched == 0 {
noMatchMessage(os.Stdout, sf, method)
}
}