You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

125 lines
2.9 KiB

  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. "bytes"
  18. "io"
  19. "os/exec"
  20. "strconv"
  21. "strings"
  22. "github.com/google/pprof/internal/plugin"
  23. )
  24. const (
  25. defaultNM = "nm"
  26. )
  27. // addr2LinerNM is a connection to an nm command for obtaining address
  28. // information from a binary.
  29. type addr2LinerNM struct {
  30. m []symbolInfo // Sorted list of addresses from binary.
  31. }
  32. type symbolInfo struct {
  33. address uint64
  34. name string
  35. }
  36. // newAddr2LinerNM starts the given nm command reporting information about the
  37. // given executable file. If file is a shared library, base should be
  38. // the address at which it was mapped in the program under
  39. // consideration.
  40. func newAddr2LinerNM(cmd, file string, base uint64) (*addr2LinerNM, error) {
  41. if cmd == "" {
  42. cmd = defaultNM
  43. }
  44. var b bytes.Buffer
  45. c := exec.Command(cmd, "-n", file)
  46. c.Stdout = &b
  47. if err := c.Run(); err != nil {
  48. return nil, err
  49. }
  50. return parseAddr2LinerNM(base, &b)
  51. }
  52. func parseAddr2LinerNM(base uint64, nm io.Reader) (*addr2LinerNM, error) {
  53. a := &addr2LinerNM{
  54. m: []symbolInfo{},
  55. }
  56. // Parse nm output and populate symbol map.
  57. // Skip lines we fail to parse.
  58. buf := bufio.NewReader(nm)
  59. for {
  60. line, err := buf.ReadString('\n')
  61. if line == "" && err != nil {
  62. if err == io.EOF {
  63. break
  64. }
  65. return nil, err
  66. }
  67. line = strings.TrimSpace(line)
  68. fields := strings.SplitN(line, " ", 3)
  69. if len(fields) != 3 {
  70. continue
  71. }
  72. address, err := strconv.ParseUint(fields[0], 16, 64)
  73. if err != nil {
  74. continue
  75. }
  76. a.m = append(a.m, symbolInfo{
  77. address: address + base,
  78. name: fields[2],
  79. })
  80. }
  81. return a, nil
  82. }
  83. // addrInfo returns the stack frame information for a specific program
  84. // address. It returns nil if the address could not be identified.
  85. func (a *addr2LinerNM) addrInfo(addr uint64) ([]plugin.Frame, error) {
  86. if len(a.m) == 0 || addr < a.m[0].address || addr > a.m[len(a.m)-1].address {
  87. return nil, nil
  88. }
  89. // Binary search. Search until low, high are separated by 1.
  90. low, high := 0, len(a.m)
  91. for low+1 < high {
  92. mid := (low + high) / 2
  93. v := a.m[mid].address
  94. if addr == v {
  95. low = mid
  96. break
  97. } else if addr > v {
  98. low = mid
  99. } else {
  100. high = mid
  101. }
  102. }
  103. // Address is between a.m[low] and a.m[high].
  104. // Pick low, as it represents [low, high).
  105. f := []plugin.Frame{
  106. {
  107. Func: a.m[low].name,
  108. },
  109. }
  110. return f, nil
  111. }