Repository navigation
Expand file tree
/
Copy pathsingle.go
More file actions
149 lines (140 loc) · 4 KB
/
Copy pathsingle.go
File metadata and controls
149 lines (140 loc) · 4 KB
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
package main
import (
"cmp"
"fmt"
"io"
"slices"
"strings"
"github.com/loov/ixdiff/ixdiff"
)
// writeStats prints an overview of a single binary: its architecture,
// function count, text size, padding, opcode histogram, and the
// largest packages and functions.
func writeStats(w io.Writer, bin *ixdiff.Binary, top int) error {
funcs := bin.Funcs()
pad := bin.Padding()
fmt.Fprintf(w, "arch: %s\n", bin.Arch())
fmt.Fprintf(w, "functions: %d\n", len(funcs))
fmt.Fprintf(w, "total text size: %d bytes (+%d alignment padding, +%d large gaps)\n",
bin.TextBytes(), pad.Align, pad.Large)
type pkgStat struct {
name string
bytes int64
insts int
spills int
slots int
funcs int
}
pkgs := map[string]*pkgStat{}
ops := ixdiff.OpCount{}
var spills, slots int
for _, f := range funcs {
st, err := f.Stats()
if err != nil {
return fmt.Errorf("disassembling %s: %w", f.Name, err)
}
ops.Add(st.Ops)
spills += st.Spills
slots += st.StackSlots
p := pkgs[f.Package]
if p == nil {
p = &pkgStat{name: f.Package}
pkgs[f.Package] = p
}
p.bytes += f.Size
p.insts += st.Ops.Total()
p.spills += st.Spills
p.slots += st.StackSlots
p.funcs++
}
fmt.Fprintf(w, "total instructions: %d\n", ops.Total())
fmt.Fprintf(w, "total spills: %d registers moved\n", spills)
fmt.Fprintf(w, "total stack traffic: %d 8-byte slots\n", slots)
if len(ops) > 0 {
fmt.Fprintf(w, "\ninstructions by opcode:\n")
for _, op := range sortedOps(ops) {
fmt.Fprintf(w, " %7d %s\n", ops[op], op)
}
}
rollup := make([]*pkgStat, 0, len(pkgs))
for _, p := range pkgs {
rollup = append(rollup, p)
}
slices.SortFunc(rollup, func(a, b *pkgStat) int {
return cmp.Or(cmp.Compare(b.bytes, a.bytes), cmp.Compare(a.name, b.name))
})
if len(rollup) > pkgRollupCap {
rollup = rollup[:pkgRollupCap]
}
fmt.Fprintf(w, "\npackages by size:\n")
fmt.Fprintf(w, " %10s %8s %8s %8s %6s %s\n", "bytes", "insts", "spills", "slots", "funcs", "package")
for _, p := range rollup {
fmt.Fprintf(w, " %10d %8d %8d %8d %6d %s\n", p.bytes, p.insts, p.spills, p.slots, p.funcs, p.name)
}
ranked := slices.Clone(funcs)
slices.SortFunc(ranked, func(a, b *ixdiff.Func) int {
return cmp.Or(cmp.Compare(b.Size, a.Size), cmp.Compare(a.Name, b.Name))
})
if len(ranked) > top {
ranked = ranked[:top]
}
fmt.Fprintf(w, "\ntop %d functions by size:\n", len(ranked))
for _, f := range ranked {
fmt.Fprintf(w, " %8d bytes %s\n", f.Size, f.Name)
}
return nil
}
// writeFuncListings prints the disassembly of every function named by
// a --fn flag in a single binary, resolving names like matchFuncs.
func writeFuncListings(w io.Writer, bin *ixdiff.Binary, names []string) error {
funcs := bin.Funcs()
for i, name := range names {
if i > 0 {
fmt.Fprintln(w)
}
matches, err := matchNames(funcs, name)
if err != nil {
return err
}
if len(matches) > 1 {
fmt.Fprintf(w, "%q matches %d functions:\n", name, len(matches))
for _, f := range matches {
fmt.Fprintf(w, " %7d bytes %s\n", f.Size, f.Name)
}
continue
}
f := matches[0]
insts, err := f.Text()
if err != nil {
return fmt.Errorf("disassembling %s: %w", f.Name, err)
}
fmt.Fprintf(w, "%s (%d bytes, %d instructions)\n", f.Name, f.Size, len(insts))
for _, in := range insts {
fmt.Fprintf(w, " %#x %s\n", in.Addr, in.Text)
}
}
return nil
}
// matchNames is matchFuncs over the functions of one binary.
func matchNames(funcs []*ixdiff.Func, name string) ([]*ixdiff.Func, error) {
var matches []*ixdiff.Func
for _, f := range funcs {
if f.Name == name {
return []*ixdiff.Func{f}, nil
}
if strings.Contains(f.Name, name) {
matches = append(matches, f)
}
}
if len(matches) == 0 {
pairs := make([]ixdiff.Pair, len(funcs))
for i, f := range funcs {
pairs[i].Name = f.Name
}
if close := closestNames(pairs, name, 3); len(close) > 0 {
return nil, fmt.Errorf("function %q not found, did you mean: %s", name, strings.Join(close, ", "))
}
return nil, fmt.Errorf("function %q not found", name)
}
return matches, nil
}