-
Notifications
You must be signed in to change notification settings - Fork 17
/
shotizam.go
471 lines (429 loc) · 10 KB
/
shotizam.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// Copyright 2020 Brad Fitzpatrick. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Shotizam parses a Go binary and breaks down its size into SQL
// output for analysis in SQLite.
package main
import (
"debug/elf"
"debug/macho"
"debug/pe"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
"syscall"
"github.com/bradfitz/shotizam/ar"
"github.com/bradfitz/shotizam/gosym"
)
var (
base = flag.String("base", "", "base file to diff from; must be in json format")
mode = flag.String("mode", "sql", "output mode; tsv, json, sql, nameinfo")
sqlite = flag.Bool("sqlite", false, "launch SQLite on data (when true, mode flag is ignored)")
verbose = flag.Bool("verbose", false, "verbose logging of file parsing")
)
type File struct {
Size int64
TextOffset uint64
Gopclntab []byte
}
func Open(ra io.ReaderAt, size int64) (*File, error) {
mo, err := macho.NewFile(ra)
if err == nil {
return machoFile(mo, ra, size)
}
ef, err := elf.NewFile(ra)
if err == nil {
return elfFile(ef, size)
}
pf, err := pe.NewFile(ra)
if err == nil {
return peFile(pf, ra, size)
}
if f, ok := arFile(ra); ok {
return f, nil
}
return nil, fmt.Errorf("unsupported binary format")
}
func arFile(ra io.ReaderAt) (f *File, ok bool) {
arr, err := ar.NewReader(ra)
if err != nil {
return nil, false
}
for {
af, err := arr.Next()
if err != nil {
return nil, false
}
if af.Name == "go.o" {
f, err := Open(af, af.Size)
if err == nil {
return f, true
}
}
}
}
func elfFile(ef *elf.File, size int64) (*File, error) {
f := &File{Size: size}
syms, err := ef.Symbols()
if err != nil && !errors.Is(err, elf.ErrNoSymbols) {
return nil, fmt.Errorf("elf symbols: %w", err)
}
for _, sym := range syms {
if sym.Name == "runtime.text" {
f.TextOffset = sym.Value
break
}
}
if f.TextOffset == 0 {
text := ef.Section(".text")
if text != nil {
f.TextOffset = text.Offset
}
}
if f.TextOffset == 0 {
return nil, errors.New("no runtime.text symbol or .text section in ELF file")
}
pclntab := ef.Section(".gopclntab")
if pclntab == nil {
pclntab = ef.Section(".data.rel.ro.gopclntab")
if pclntab == nil {
return nil, errors.New("no .gopclntab or .data.rel.ro.gopclntab section found in ELF file")
}
}
b, err := pclntab.Data()
if err != nil {
return nil, err
}
f.Gopclntab = b
return f, nil
}
func machoFile(mo *macho.File, ra io.ReaderAt, size int64) (*File, error) {
f := &File{Size: size}
if *verbose {
log.Printf("Got: %+v", mo.FileHeader)
log.Printf("%d sections:", len(mo.Sections))
sort.Slice(mo.Sections, func(i, j int) bool {
return mo.Sections[i].Size > mo.Sections[j].Size
})
for i, s := range mo.Symtab.Syms {
log.Printf("sym[%d]: %+v", i, s)
}
}
for i, s := range mo.Sections {
if *verbose {
log.Printf("sect[%d] = %+v\n", i, s.SectionHeader)
}
if s.Name == "__text" {
f.TextOffset = uint64(s.Offset)
}
if s.Name == "__gopclntab" {
f.Gopclntab = make([]byte, s.Size)
_, err := ra.ReadAt(f.Gopclntab, int64(s.Offset))
if err != nil {
return nil, err
}
}
}
if f.Gopclntab == nil {
return nil, errors.New("no __gopclntab section found in macho file")
}
return f, nil
}
func peFile(pf *pe.File, ra io.ReaderAt, size int64) (*File, error) {
f := &File{Size: size}
for i, s := range pf.Sections {
if s.Name == ".text" {
f.TextOffset = uint64(s.Offset)
}
if *verbose {
log.Printf("sect[%d] = %+v", i, s.SectionHeader)
}
}
var start, end int64
var pclnSect int // 0-based
for i, s := range pf.Symbols {
if *verbose {
log.Printf("sym[%d] = %+v", i, s)
}
switch s.Name {
case "runtime.pclntab":
start = int64(s.Value)
if s.SectionNumber == 0 {
return nil, errors.New("bogus section number 0 for runtime.pclntab")
}
// It's 1-based on the file.
pclnSect = int(s.SectionNumber - 1)
case "runtime.epclntab":
end = int64(s.Value)
}
}
if start == 0 {
return nil, errors.New("didn't find runtime.pclntab symbol")
}
if end == 0 {
return nil, errors.New("didn't find runtime.epclntab symbol")
}
pcLnOff := int64(pf.Sections[pclnSect].Offset) + start
pcLnSize := end - start
if *verbose {
log.Printf("got sect %d, start %d, end %d, size %d", pclnSect, start, end, pcLnSize)
log.Printf("sect off = %d, pcLnOff = %d", int64(pf.Sections[pclnSect].Offset), pcLnOff)
}
f.Gopclntab = make([]byte, pcLnSize)
_, err := ra.ReadAt(f.Gopclntab, pcLnOff)
if err != nil {
return nil, err
}
return f, nil
}
func main() {
log.SetFlags(0)
flag.Parse()
if flag.NArg() != 1 {
log.Fatalf("Usage: shotizam <go-binary>")
}
bin := flag.Arg(0)
if bin == "SELF" {
var err error
bin, err = os.Executable()
if err != nil {
log.Fatal(err)
}
}
of, err := os.Open(bin)
if err != nil {
log.Fatal(err)
}
fi, err := of.Stat()
if err != nil {
log.Fatal(err)
}
binSize := fi.Size()
f, err := Open(of, binSize)
of.Close()
if err != nil {
log.Fatal(err)
}
lt := gosym.NewLineTable(f.Gopclntab, f.TextOffset)
t, err := gosym.NewTable(nil, lt)
if err != nil {
log.Fatal(err)
}
// TODO: data
if *sqlite {
*mode = "sql"
}
if *base != "" && *mode != "json" {
log.Fatalf("--base only works with json mode")
}
var w io.WriteCloser = os.Stdout
switch *mode {
case "sql":
case "json":
case "tsv":
case "nameinfo":
w = nopWriteCloser()
default:
log.Fatalf("unknown mode %q", *mode)
}
var cmd *exec.Cmd
var dbPath string
if *sqlite {
sqlBin, err := exec.LookPath("sqlite3")
if err != nil {
log.Fatalf("sqlite3 not found")
}
td, err := os.MkdirTemp("", "shotizam")
if err != nil {
log.Fatal(err)
}
dbPath = filepath.Join(td, "shotizam.db")
cmd = exec.Command(sqlBin, dbPath)
w, err = cmd.StdinPipe()
if err != nil {
log.Fatal(err)
}
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
}
switch *mode {
case "sql":
fmt.Fprintln(w, "DROP TABLE IF EXISTS Bin;")
fmt.Fprintln(w, "CREATE TABLE Bin (Func varchar, Pkg varchar, What varchar, Size int64);")
fmt.Fprintln(w, "BEGIN TRANSACTION;")
}
unaccountedSize := binSize
var names []string
var recs []Rec
for i := range t.Funcs {
f := &t.Funcs[i]
names = append(names, f.Name)
emit := func(what string, size int64) {
unaccountedSize -= size
if size == 0 {
return
}
switch *mode {
case "sql":
// TODO: include truncated name, stopping at first ".func" closure.
// Likewise, add field for func truncated just past type too. ("Type"?)
fmt.Fprintf(w, "INSERT INTO Bin VALUES (%s, %s, %s, %v);\n",
sqlString(f.Name),
sqlString(f.PackageName()),
sqlString(what),
size)
case "tsv":
fmt.Fprintf(w, "%s\t%s\t%s\t%v\n", f.Name, f.PackageName(), what, size)
case "json":
recs = append(recs, Rec{RecKey{f.Name, f.PackageName(), what}, size})
}
}
emit("fixedheader", int64(t.PtrSize()+8*4)) // uintptr + 8 x int32s in _func
emit("funcdata", int64(t.PtrSize()*f.NumFuncData)) // TODO: add optional 4 byte alignment padding before first funcdata
emit("pcsp", int64(f.TableSizePCSP()))
emit("pcfile", int64(f.TableSizePCFile()))
emit("pcln", int64(f.TableSizePCLn()))
for tab := 0; tab < f.NumPCData; tab++ {
emit(fmt.Sprintf("pcdata%d%s", tab, pcdataSuffix(tab)), int64(4 /* offset pointer */ +f.TableSizePCData(tab)))
}
// TODO: the other funcdata and pcdata tables
emit("text", int64(f.End-f.Entry))
emit("funcname", int64(len(f.Name)+len("\x00")))
}
switch *mode {
case "sql":
fmt.Fprintf(w, "INSERT INTO Bin (What, Size) VALUES ('TODO', %v);\n", unaccountedSize)
fmt.Fprintln(w, "END TRANSACTION;")
case "json":
if *base != "" {
old := readBaseRecs()
oldm := recMap(old)
newm := recMap(recs)
recs = diffMap(oldm, newm)
}
je := json.NewEncoder(w)
je.SetIndent("", "\t")
if err := je.Encode(recs); err != nil {
log.Fatal(err)
}
case "nameinfo":
sort.Strings(names)
var totNames, skip int
for i, name := range names {
totNames += len(name)
var next string
if i < len(names)-1 {
next = names[i+1]
}
if strings.HasPrefix(next, name) {
skip += len(name)
}
}
log.Printf(" total length of func names: %d", totNames)
log.Printf("bytes of func names which are prefixes of other func: %d", skip)
return
}
w.Close()
if cmd != nil {
if err := cmd.Wait(); err != nil {
log.Fatal(err)
}
if err := syscall.Exec(cmd.Path, cmd.Args, cmd.Env); err != nil {
log.Fatal(err)
}
}
}
func pcdataSuffix(n int) string {
switch n {
case 0:
return "-unsafepoint"
case 1:
return "-stackmap"
case 2:
return "-inltree"
}
return ""
}
func sqlString(s string) string {
var sb strings.Builder
sb.WriteByte('\'')
for _, r := range s {
if r == '\'' {
sb.WriteString("''")
} else {
sb.WriteRune(r)
}
}
sb.WriteByte('\'')
return sb.String()
}
func nopWriteCloser() io.WriteCloser {
return struct {
io.Writer
io.Closer
}{
io.Discard,
io.NopCloser(nil),
}
}
func readBaseRecs() []Rec {
f, err := os.Open(*base)
if err != nil {
log.Fatal(err)
}
defer f.Close()
var recs []Rec
if err := json.NewDecoder(f).Decode(&recs); err != nil {
log.Fatal(err)
}
return recs
}
type RecKey struct {
Name string `json:"name,omitempty"`
Package string `json:"package,omitempty"`
What string `json:"what"`
}
type Rec struct {
RecKey
Size int64 `json:"size"`
}
func recMap(recs []Rec) map[RecKey]int64 {
m := make(map[RecKey]int64)
for _, r := range recs {
m[r.RecKey] = r.Size
}
return m
}
func diffMap(a, b map[RecKey]int64) []Rec {
diff := make(map[RecKey]int64)
for k, size := range b {
oldSize, ok := a[k]
change := size - oldSize
if change != 0 {
diff[k] = change
}
if ok {
delete(a, k)
}
}
// Anything not deleted in a is stuff we dropped. Count it as
// negative size.
for k, size := range a {
diff[k] = -size
}
recs := make([]Rec, 0, len(diff))
for k, size := range diff {
recs = append(recs, Rec{k, size})
}
sort.Slice(recs, func(i, j int) bool { return recs[i].Size < recs[j].Size })
return recs
}