Debian Codegraph @

internal/binutils/disasm.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 "bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/"
19 "ioscip-go gomod github.com/golang/go/src go1.24.0 io/"
20 "regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/"
21 "strconvscip-go gomod github.com/golang/go/src go1.24.0 strconv/"
22 "stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/"
23
24 "github.com/google/pprof/internal/pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/"
25 "github.com/ianlancetaylor/demanglescip-go gomod github.com/ianlancetaylor/demangle f615e6bd150b `github.com/ianlancetaylor/demangle`/"
26 )
27
28 var (
29 nmOutputREscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/nmOutputRE. = regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.24.0 regexp/MustCompile().(`^\s*([[:xdigit:]]+)\s+(.)\s+(.*)`)
30 objdumpAsmOutputREscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/objdumpAsmOutputRE. = regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.24.0 regexp/MustCompile().(`^\s*([[:xdigit:]]+):\s+(.*)`)
31 objdumpOutputFileLinescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/objdumpOutputFileLine. = regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.24.0 regexp/MustCompile().(`^;?\s?(.*):([0-9]+)`)
32 objdumpOutputFunctionscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/objdumpOutputFunction. = regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.24.0 regexp/MustCompile().(`^;?\s?(\S.*)\(\):`)
33 objdumpOutputFunctionLLVMscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/objdumpOutputFunctionLLVM. = regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.24.0 regexp/MustCompile().(`^([[:xdigit:]]+)?\s?(.*):`)
34 )
35
36 func findSymbolsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/findSymbols().(syms []byte, file string, r *regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/.Regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#, address uint64) ([]*pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.Symscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#, error) {
37 // Collect all symbols from the nm output, grouping names mapped to
38 // the same address into a single symbol.
39
40 // The symbols to return.
41 var symbols []*pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.Symscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#
42
43 // The current group of symbol names, and the address they are all at.
44 names, start := []string{}, uint64(0)
45
46 buf := bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/.NewBufferscip-go gomod github.com/golang/go/src go1.24.0 bytes/NewBuffer().(syms)
47
48 for {
49 symAddr, name, err := nextSymbolscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/nextSymbol().(buf)
50 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. {
51 // Done. If there was an unfinished group, append it.
52 if len(names) != 0 {
53 if match := matchSymbolscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/matchSymbol().(names, start, symAddr-1, r, address); match != nil {
54 symbols = append(symbols, &pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.Symscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#{Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Name.: match, Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#File.: file, Startscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Start.: start, Endscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#End.: symAddr - 1})
55 }
56 }
57
58 // And return the symbols.
59 return symbols, nil
60 }
61
62 if err != nil {
63 // There was some kind of serious error reading nm's output.
64 return nil, err
65 }
66
67 // If this symbol is at the same address as the current group, add it to the group.
68 if symAddr == start {
69 names = append(names, name)
70 continue
71 }
72
73 // Otherwise append the current group to the list of symbols.
74 if match := matchSymbolscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/matchSymbol().(names, start, symAddr-1, r, address); match != nil {
75 symbols = append(symbols, &pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.Symscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#{Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Name.: match, Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#File.: file, Startscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Start.: start, Endscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#End.: symAddr - 1})
76 }
77
78 // And start a new group.
79 names, start = []string{name}, symAddr
80 }
81 }
82
83 // matchSymbol checks if a symbol is to be selected by checking its
84 // name to the regexp and optionally its address. It returns the name(s)
85 // to be used for the matched symbol, or nil if no match
86 func matchSymbolscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/matchSymbol().(names []string, start, end uint64, r *regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/.Regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#, address uint64) []string {
87 if address != 0 && address >= start && address <= end {
88 return names
89 }
90 for _, name := range names {
91 if r == nil || r.MatchStringscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#MatchString().(name) {
92 return []string{name}
93 }
94
95 // Match all possible demangled versions of the name.
96 for _, o := range [][]demanglescip-go gomod github.com/ianlancetaylor/demangle f615e6bd150b `github.com/ianlancetaylor/demangle`/.Optionscip-go gomod github.com/ianlancetaylor/demangle f615e6bd150b `github.com/ianlancetaylor/demangle`/Option#{
97 {demanglescip-go gomod github.com/ianlancetaylor/demangle f615e6bd150b `github.com/ianlancetaylor/demangle`/.NoClonesscip-go gomod github.com/ianlancetaylor/demangle f615e6bd150b `github.com/ianlancetaylor/demangle`/NoClones.},
98 {demanglescip-go gomod github.com/ianlancetaylor/demangle f615e6bd150b `github.com/ianlancetaylor/demangle`/.NoParamsscip-go gomod github.com/ianlancetaylor/demangle f615e6bd150b `github.com/ianlancetaylor/demangle`/NoParams., demanglescip-go gomod github.com/ianlancetaylor/demangle f615e6bd150b `github.com/ianlancetaylor/demangle`/.NoEnclosingParamsscip-go gomod github.com/ianlancetaylor/demangle f615e6bd150b `github.com/ianlancetaylor/demangle`/NoEnclosingParams.},
99 {demanglescip-go gomod github.com/ianlancetaylor/demangle f615e6bd150b `github.com/ianlancetaylor/demangle`/.NoParamsscip-go gomod github.com/ianlancetaylor/demangle f615e6bd150b `github.com/ianlancetaylor/demangle`/NoParams., demanglescip-go gomod github.com/ianlancetaylor/demangle f615e6bd150b `github.com/ianlancetaylor/demangle`/.NoEnclosingParamsscip-go gomod github.com/ianlancetaylor/demangle f615e6bd150b `github.com/ianlancetaylor/demangle`/NoEnclosingParams., demanglescip-go gomod github.com/ianlancetaylor/demangle f615e6bd150b `github.com/ianlancetaylor/demangle`/.NoTemplateParamsscip-go gomod github.com/ianlancetaylor/demangle f615e6bd150b `github.com/ianlancetaylor/demangle`/NoTemplateParams.},
100 } {
101 if demangled, err := demanglescip-go gomod github.com/ianlancetaylor/demangle f615e6bd150b `github.com/ianlancetaylor/demangle`/.ToStringscip-go gomod github.com/ianlancetaylor/demangle f615e6bd150b `github.com/ianlancetaylor/demangle`/ToString().(name, o...); err == nil && r.MatchStringscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#MatchString().(demangled) {
102 return []string{demangled}
103 }
104 }
105 }
106 return nil
107 }
108
109 // disassemble parses the output of the objdump command and returns
110 // the assembly instructions in a slice.
111 func disassemblescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/disassemble().(asm []byte) ([]pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.Instscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#, error) {
112 buf := bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/.NewBufferscip-go gomod github.com/golang/go/src go1.24.0 bytes/NewBuffer().(asm)
113 function, file, line := "", "", 0
114 var assembly []pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.Instscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#
115 for {
116 input, err := buf.ReadStringscip-go gomod github.com/golang/go/src go1.24.0 bytes/Buffer#ReadString().('\n')
117 if err != nil {
118 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. {
119 return nil, err
120 }
121 if input == "" {
122 break
123 }
124 }
125 input = 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().(input)
126
127 if fields := objdumpAsmOutputREscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/objdumpAsmOutputRE..FindStringSubmatchscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#FindStringSubmatch().(input); len(fields) == 3 {
128 if 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[1], 16, 64); err == nil {
129 assembly = append(assembly,
130 pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.Instscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#{
131 Addrscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#Addr.: address,
132 Textscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#Text.: fields[2],
133 Functionscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#Function.: function,
134 Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#File.: file,
135 Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#Line.: line,
136 })
137 continue
138 }
139 }
140 if fields := objdumpOutputFileLinescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/objdumpOutputFileLine..FindStringSubmatchscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#FindStringSubmatch().(input); len(fields) == 3 {
141 if l, 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], 10, 32); err == nil {
142 file, line = fields[1], int(l)
143 }
144 continue
145 }
146 if fields := objdumpOutputFunctionscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/objdumpOutputFunction..FindStringSubmatchscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#FindStringSubmatch().(input); len(fields) == 2 {
147 function = fields[1]
148 continue
149 } else {
150 if fields := objdumpOutputFunctionLLVMscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/objdumpOutputFunctionLLVM..FindStringSubmatchscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#FindStringSubmatch().(input); len(fields) == 3 {
151 function = fields[2]
152 continue
153 }
154 }
155 // Reset on unrecognized lines.
156 function, file, line = "", "", 0
157 }
158
159 return assembly, nil
160 }
161
162 // nextSymbol parses the nm output to find the next symbol listed.
163 // Skips over any output it cannot recognize.
164 func nextSymbolscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/nextSymbol().(buf *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#) (uint64, string, error) {
165 for {
166 line, err := buf.ReadStringscip-go gomod github.com/golang/go/src go1.24.0 bytes/Buffer#ReadString().('\n')
167 if err != nil {
168 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. || line == "" {
169 return 0, "", err
170 }
171 }
172 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)
173
174 if fields := nmOutputREscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/nmOutputRE..FindStringSubmatchscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#FindStringSubmatch().(line); len(fields) == 4 {
175 if 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[1], 16, 64); err == nil {
176 return address, fields[3], nil
177 }
178 }
179 }
180 }