1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package binutils
16
17 import (
18 "bufio"
19 "fmt"
20 "io"
21 "os/exec"
22 "strconv"
23 "strings"
24 "sync"
25
26 "github.com/google/pprof/internal/plugin"
27 )
28
29 const (
30 defaultLLVMSymbolizer = "llvm-symbolizer"
31 )
32
33
34
35 type llvmSymbolizer struct {
36 sync.Mutex
37 filename string
38 rw lineReaderWriter
39 base uint64
40 }
41
42 type llvmSymbolizerJob struct {
43 cmd *exec.Cmd
44 in io.WriteCloser
45 out *bufio.Reader
46
47 symType string
48 }
49
50 func (a *llvmSymbolizerJob) write(s string) error {
51 _, err := fmt.Fprintln(a.in, a.symType, s)
52 return err
53 }
54
55 func (a *llvmSymbolizerJob) readLine() (string, error) {
56 s, err := a.out.ReadString('\n')
57 if err != nil {
58 return "", err
59 }
60 return strings.TrimSpace(s), nil
61 }
62
63
64 func (a *llvmSymbolizerJob) close() {
65 a.in.Close()
66 a.cmd.Wait()
67 }
68
69
70
71
72
73 func newLLVMSymbolizer(cmd, file string, base uint64, isData bool) (*llvmSymbolizer, error) {
74 if cmd == "" {
75 cmd = defaultLLVMSymbolizer
76 }
77
78 j := &llvmSymbolizerJob{
79 cmd: exec.Command(cmd, "--inlining", "-demangle=false"),
80 symType: "CODE",
81 }
82 if isData {
83 j.symType = "DATA"
84 }
85
86 var err error
87 if j.in, err = j.cmd.StdinPipe(); err != nil {
88 return nil, err
89 }
90
91 outPipe, err := j.cmd.StdoutPipe()
92 if err != nil {
93 return nil, err
94 }
95
96 j.out = bufio.NewReader(outPipe)
97 if err := j.cmd.Start(); err != nil {
98 return nil, err
99 }
100
101 a := &llvmSymbolizer{
102 filename: file,
103 rw: j,
104 base: base,
105 }
106
107 return a, nil
108 }
109
110
111
112
113 func (d *llvmSymbolizer) readFrame() (plugin.Frame, bool) {
114 funcname, err := d.rw.readLine()
115 if err != nil {
116 return plugin.Frame{}, true
117 }
118
119 switch funcname {
120 case "":
121 return plugin.Frame{}, true
122 case "??":
123 funcname = ""
124 }
125
126 fileline, err := d.rw.readLine()
127 if err != nil {
128 return plugin.Frame{Func: funcname}, true
129 }
130
131 linenumber := 0
132
133
134
135
136 if fileline == "??:0" || fileline == "??:0:0" {
137 fileline = ""
138 } else {
139 switch split := strings.Split(fileline, ":"); len(split) {
140 case 1:
141
142 fileline = split[0]
143 case 2, 3:
144
145
146 fileline = split[0]
147 if line, err := strconv.Atoi(split[1]); err == nil {
148 linenumber = line
149 }
150 default:
151
152 }
153 }
154
155 return plugin.Frame{Func: funcname, File: fileline, Line: linenumber}, false
156 }
157
158
159
160 func (d *llvmSymbolizer) addrInfo(addr uint64) ([]plugin.Frame, error) {
161 d.Lock()
162 defer d.Unlock()
163
164 if err := d.rw.write(fmt.Sprintf("%s 0x%x", d.filename, addr-d.base)); err != nil {
165 return nil, err
166 }
167
168 var stack []plugin.Frame
169 for {
170 frame, end := d.readFrame()
171 if end {
172 break
173 }
174
175 if frame != (plugin.Frame{}) {
176 stack = append(stack, frame)
177 }
178 }
179
180 return stack, nil
181 }
182
View as plain text