暫無描述

addr2liner_nm.go 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 is 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. a := &addr2LinerNM{
  45. m: []symbolInfo{},
  46. }
  47. var b bytes.Buffer
  48. c := exec.Command(cmd, "-n", file)
  49. c.Stdout = &b
  50. if err := c.Run(); err != nil {
  51. return nil, err
  52. }
  53. // Parse nm output and populate symbol map.
  54. // Skip lines we fail to parse.
  55. buf := bufio.NewReader(&b)
  56. for {
  57. line, err := buf.ReadString('\n')
  58. if line == "" && err != nil {
  59. if err == io.EOF {
  60. break
  61. }
  62. return nil, err
  63. }
  64. line = strings.TrimSpace(line)
  65. fields := strings.SplitN(line, " ", 3)
  66. if len(fields) != 3 {
  67. continue
  68. }
  69. address, err := strconv.ParseUint(fields[0], 16, 64)
  70. if err != nil {
  71. continue
  72. }
  73. a.m = append(a.m, symbolInfo{
  74. address: address + base,
  75. name: fields[2],
  76. })
  77. }
  78. return a, nil
  79. }
  80. // addrInfo returns the stack frame information for a specific program
  81. // address. It returns nil if the address could not be identified.
  82. func (a *addr2LinerNM) addrInfo(addr uint64) ([]plugin.Frame, error) {
  83. if len(a.m) == 0 || addr < a.m[0].address || addr > a.m[len(a.m)-1].address {
  84. return nil, nil
  85. }
  86. // Binary search. Search until low, high are separated by 1.
  87. low, high := 0, len(a.m)
  88. for low+1 < high {
  89. mid := (low + high) / 2
  90. v := a.m[mid].address
  91. if addr == v {
  92. low = mid
  93. break
  94. } else if addr > v {
  95. low = mid
  96. } else {
  97. high = mid
  98. }
  99. }
  100. // Address is between a.m[low] and a.m[high].
  101. // Pick low, as it represents [low, high).
  102. f := []plugin.Frame{
  103. {
  104. Func: a.m[low].name,
  105. },
  106. }
  107. return f, nil
  108. }