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 columnnumber := 0
133
134
135
136
137 if fileline == "??:0" || fileline == "??:0:0" {
138 fileline = ""
139 } else {
140 switch split := strings.Split(fileline, ":"); len(split) {
141 case 3:
142
143 if col, err := strconv.Atoi(split[2]); err == nil {
144 columnnumber = col
145 }
146 fallthrough
147 case 2:
148
149 if line, err := strconv.Atoi(split[1]); err == nil {
150 linenumber = line
151 }
152 fallthrough
153 case 1:
154
155 fileline = split[0]
156 default:
157
158 }
159 }
160
161 return plugin.Frame{Func: funcname, File: fileline, Line: linenumber, Column: columnnumber}, false
162 }
163
164
165
166 func (d *llvmSymbolizer) addrInfo(addr uint64) ([]plugin.Frame, error) {
167 d.Lock()
168 defer d.Unlock()
169
170 if err := d.rw.write(fmt.Sprintf("%s 0x%x", d.filename, addr-d.base)); err != nil {
171 return nil, err
172 }
173
174 var stack []plugin.Frame
175 for {
176 frame, end := d.readFrame()
177 if end {
178 break
179 }
180
181 if frame != (plugin.Frame{}) {
182 stack = append(stack, frame)
183 }
184 }
185
186 return stack, nil
187 }
188
View as plain text