Debian Codegraph @

internal/binutils/addr2liner_nm.go

raw symbols SCIP
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
15 package binutilsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/
16
17 import (
18 "bufioscip-go gomod github.com/golang/go/src go1.24.0 bufio/"
19 "bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/"
20 "ioscip-go gomod github.com/golang/go/src go1.24.0 io/"
21 "os/execscip-go gomod github.com/golang/go/src go1.24.0 `os/exec`/"
22 "strconvscip-go gomod github.com/golang/go/src go1.24.0 strconv/"
23 "stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/"
24
25 "github.com/google/pprof/internal/pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/"
26 )
27
28 const (
29 defaultNMscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/defaultNM. = "nm"
30 )
31
32 // addr2LinerNM is a connection to an nm command for obtaining symbol
33 // information from a binary.
34 type addr2LinerNMscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/addr2LinerNM# struct {
35 mscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/addr2LinerNM#m. []symbolInfoscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/symbolInfo# // Sorted list of symbol addresses from binary.
36 }
37
38 type symbolInfoscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/symbolInfo# struct {
39 addressscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/symbolInfo#address. uint64
40 sizescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/symbolInfo#size. uint64
41 namescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/symbolInfo#name. string
42 symTypescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/symbolInfo#symType. string
43 }
44
45 // isData returns if the symbol has a known data object symbol type.
46 func (s *symbolInfoscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/symbolInfo#) isDatascip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/symbolInfo#isData().() bool {
47 // The following symbol types are taken from https://linux.die.net/man/1/nm:
48 // Lowercase letter means local symbol, uppercase denotes a global symbol.
49 // - b or B: the symbol is in the uninitialized data section, e.g. .bss;
50 // - d or D: the symbol is in the initialized data section;
51 // - r or R: the symbol is in a read only data section;
52 // - v or V: the symbol is a weak object;
53 // - W: the symbol is a weak symbol that has not been specifically tagged as a
54 // weak object symbol. Experiments with some binaries, showed these to be
55 // mostly data objects.
56 return stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.ContainsAnyscip-go gomod github.com/golang/go/src go1.24.0 strings/ContainsAny().(s.symTypescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/symbolInfo#symType., "bBdDrRvVW")
57 }
58
59 // newAddr2LinerNM starts the given nm command reporting information about the
60 // given executable file. If file is a shared library, base should be the
61 // address at which it was mapped in the program under consideration.
62 func newAddr2LinerNMscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/newAddr2LinerNM().(cmd, file string, base uint64) (*addr2LinerNMscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/addr2LinerNM#, error) {
63 if cmd == "" {
64 cmd = defaultNMscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/defaultNM.
65 }
66 var b bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.24.0 bytes/Buffer#
67 c := execscip-go gomod github.com/golang/go/src go1.24.0 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.24.0 `os/exec`/Command().(cmd, "--numeric-sort", "--print-size", "--format=posix", file)
68 c.Stdoutscip-go gomod github.com/golang/go/src go1.24.0 `os/exec`/Cmd#Stdout. = &b
69 if err := c.Runscip-go gomod github.com/golang/go/src go1.24.0 `os/exec`/Cmd#Run().(); err != nil {
70 return nil, err
71 }
72 return parseAddr2LinerNMscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/parseAddr2LinerNM().(base, &b)
73 }
74
75 func parseAddr2LinerNMscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/parseAddr2LinerNM().(base uint64, nm ioscip-go gomod github.com/golang/go/src go1.24.0 io/.Readerscip-go gomod github.com/golang/go/src go1.24.0 io/Reader#) (*addr2LinerNMscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/addr2LinerNM#, error) {
76 a := &addr2LinerNMscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/addr2LinerNM#{
77 mscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/addr2LinerNM#m.: []symbolInfoscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/symbolInfo#{},
78 }
79
80 // Parse nm output and populate symbol map.
81 // Skip lines we fail to parse.
82 buf := bufioscip-go gomod github.com/golang/go/src go1.24.0 bufio/.NewReaderscip-go gomod github.com/golang/go/src go1.24.0 bufio/NewReader().(nm)
83 for {
84 line, err := buf.ReadStringscip-go gomod github.com/golang/go/src go1.24.0 bufio/Reader#ReadString().('\n')
85 if line == "" && err != nil {
86 if err == ioscip-go gomod github.com/golang/go/src go1.24.0 io/.EOFscip-go gomod github.com/golang/go/src go1.24.0 io/EOF. {
87 break
88 }
89 return nil, err
90 }
91 line = stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.24.0 strings/TrimSpace().(line)
92 fields := stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.Splitscip-go gomod github.com/golang/go/src go1.24.0 strings/Split().(line, " ")
93 if len(fields) != 4 {
94 continue
95 }
96 address, err := strconvscip-go gomod github.com/golang/go/src go1.24.0 strconv/.ParseUintscip-go gomod github.com/golang/go/src go1.24.0 strconv/ParseUint().(fields[2], 16, 64)
97 if err != nil {
98 continue
99 }
100 size, err := strconvscip-go gomod github.com/golang/go/src go1.24.0 strconv/.ParseUintscip-go gomod github.com/golang/go/src go1.24.0 strconv/ParseUint().(fields[3], 16, 64)
101 if err != nil {
102 continue
103 }
104 a.mscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/addr2LinerNM#m. = append(a.mscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/addr2LinerNM#m., symbolInfoscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/symbolInfo#{
105 addressscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/symbolInfo#address.: address + base,
106 sizescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/symbolInfo#size.: size,
107 namescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/symbolInfo#name.: fields[0],
108 symTypescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/symbolInfo#symType.: fields[1],
109 })
110 }
111
112 return a, nil
113 }
114
115 // addrInfo returns the stack frame information for a specific program
116 // address. It returns nil if the address could not be identified.
117 func (a *addr2LinerNMscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/addr2LinerNM#) addrInfoscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/addr2LinerNM#addrInfo().(addr uint64) ([]pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.Framescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Frame#, error) {
118 if len(a.mscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/addr2LinerNM#m.) == 0 || addr < a.mscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/addr2LinerNM#m.[0].addressscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/symbolInfo#address. || addr >= (a.mscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/addr2LinerNM#m.[len(a.mscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/addr2LinerNM#m.)-1].addressscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/symbolInfo#address.+a.mscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/addr2LinerNM#m.[len(a.mscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/addr2LinerNM#m.)-1].sizescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/symbolInfo#size.) {
119 return nil, nil
120 }
121
122 // Binary search. Search until low, high are separated by 1.
123 low, high := 0, len(a.mscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/addr2LinerNM#m.)
124 for low+1 < high {
125 mid := (low + high) / 2
126 v := a.mscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/addr2LinerNM#m.[mid].addressscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/symbolInfo#address.
127 if addr == v {
128 low = mid
129 break
130 } else if addr > v {
131 low = mid
132 } else {
133 high = mid
134 }
135 }
136
137 // Address is between a.m[low] and a.m[high]. Pick low, as it represents
138 // [low, high). For data symbols, we use a strict check that the address is in
139 // the [start, start + size) range of a.m[low].
140 if a.mscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/addr2LinerNM#m.[low].isDatascip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/symbolInfo#isData().() && addr >= (a.mscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/addr2LinerNM#m.[low].addressscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/symbolInfo#address.+a.mscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/addr2LinerNM#m.[low].sizescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/symbolInfo#size.) {
141 return nil, nil
142 }
143 return []pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.Framescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Frame#{{Funcscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Frame#Func.: a.mscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/addr2LinerNM#m.[low].namescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/symbolInfo#name.}}, nil
144 }