Nenhuma descrição

addr2liner.go 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Copyright 2014 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package binutils
  15. import (
  16. "bufio"
  17. "fmt"
  18. "io"
  19. "os/exec"
  20. "strconv"
  21. "strings"
  22. "github.com/google/pprof/internal/plugin"
  23. )
  24. const (
  25. defaultAddr2line = "addr2line"
  26. // addr2line may produce multiple lines of output. We
  27. // use this sentinel to identify the end of the output.
  28. sentinel = ^uint64(0)
  29. )
  30. // addr2Liner is a connection to an addr2line command for obtaining
  31. // address and line number information from a binary.
  32. type addr2Liner struct {
  33. rw lineReaderWriter
  34. base uint64
  35. }
  36. // lineReaderWriter is an interface to abstract the I/O to an addr2line
  37. // process. It writes a line of input to the job, and reads its output
  38. // one line at a time.
  39. type lineReaderWriter interface {
  40. write(string) error
  41. readLine() (string, error)
  42. close()
  43. }
  44. type addr2LinerJob struct {
  45. cmd *exec.Cmd
  46. in io.WriteCloser
  47. out *bufio.Reader
  48. }
  49. func (a *addr2LinerJob) write(s string) error {
  50. _, err := fmt.Fprint(a.in, s+"\n")
  51. return err
  52. }
  53. func (a *addr2LinerJob) readLine() (string, error) {
  54. return a.out.ReadString('\n')
  55. }
  56. // close releases any resources used by the addr2liner object.
  57. func (a *addr2LinerJob) close() {
  58. a.in.Close()
  59. a.cmd.Wait()
  60. }
  61. // newAddr2liner starts the given addr2liner command reporting
  62. // information about the given executable file. If file is a shared
  63. // library, base should be the address at which is was mapped in the
  64. // program under consideration.
  65. func newAddr2Liner(cmd, file string, base uint64) (*addr2Liner, error) {
  66. if cmd == "" {
  67. cmd = defaultAddr2line
  68. }
  69. j := &addr2LinerJob{
  70. cmd: exec.Command(cmd, "-aif", "-e", file),
  71. }
  72. var err error
  73. if j.in, err = j.cmd.StdinPipe(); err != nil {
  74. return nil, err
  75. }
  76. outPipe, err := j.cmd.StdoutPipe()
  77. if err != nil {
  78. return nil, err
  79. }
  80. j.out = bufio.NewReader(outPipe)
  81. if err := j.cmd.Start(); err != nil {
  82. return nil, err
  83. }
  84. a := &addr2Liner{
  85. rw: j,
  86. base: base,
  87. }
  88. return a, nil
  89. }
  90. func (d *addr2Liner) readString() (string, error) {
  91. s, err := d.rw.readLine()
  92. if err != nil {
  93. return "", err
  94. }
  95. return strings.TrimSpace(s), nil
  96. }
  97. // readFrame parses the addr2line output for a single address. It
  98. // returns a populated plugin.Frame and whether it has reached the end of the
  99. // data.
  100. func (d *addr2Liner) readFrame() (plugin.Frame, bool) {
  101. funcname, err := d.readString()
  102. if err != nil {
  103. return plugin.Frame{}, true
  104. }
  105. if strings.HasPrefix(funcname, "0x") {
  106. // If addr2line returns a hex address we can assume it is the
  107. // sentinel. Read and ignore next two lines of output from
  108. // addr2line
  109. d.readString()
  110. d.readString()
  111. return plugin.Frame{}, true
  112. }
  113. fileline, err := d.readString()
  114. if err != nil {
  115. return plugin.Frame{}, true
  116. }
  117. linenumber := 0
  118. if funcname == "??" {
  119. funcname = ""
  120. }
  121. if fileline == "??:0" {
  122. fileline = ""
  123. } else {
  124. if i := strings.LastIndex(fileline, ":"); i >= 0 {
  125. // Remove discriminator, if present
  126. if disc := strings.Index(fileline, " (discriminator"); disc > 0 {
  127. fileline = fileline[:disc]
  128. }
  129. // If we cannot parse a number after the last ":", keep it as
  130. // part of the filename.
  131. if line, err := strconv.Atoi(fileline[i+1:]); err == nil {
  132. linenumber = line
  133. fileline = fileline[:i]
  134. }
  135. }
  136. }
  137. return plugin.Frame{funcname, fileline, linenumber}, false
  138. }
  139. // addrInfo returns the stack frame information for a specific program
  140. // address. It returns nil if the address could not be identified.
  141. func (d *addr2Liner) addrInfo(addr uint64) ([]plugin.Frame, error) {
  142. if err := d.rw.write(fmt.Sprintf("%x", addr-d.base)); err != nil {
  143. return nil, err
  144. }
  145. if err := d.rw.write(fmt.Sprintf("%x", sentinel)); err != nil {
  146. return nil, err
  147. }
  148. resp, err := d.readString()
  149. if err != nil {
  150. return nil, err
  151. }
  152. if !strings.HasPrefix(resp, "0x") {
  153. return nil, fmt.Errorf("unexpected addr2line output: %s", resp)
  154. }
  155. var stack []plugin.Frame
  156. for {
  157. frame, end := d.readFrame()
  158. if end {
  159. break
  160. }
  161. if frame != (plugin.Frame{}) {
  162. stack = append(stack, frame)
  163. }
  164. }
  165. return stack, nil
  166. }