-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
482 lines (455 loc) · 12.3 KB
/
main.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
472
473
474
475
476
477
478
479
480
481
482
package main
import (
"flag"
"fmt"
"os"
"runtime/pprof"
"sort"
"strconv"
"text/tabwriter"
"github.com/randall77/corelib/core"
"github.com/randall77/corelib/gocore"
)
func usage() {
fmt.Println(`
Usage:
corelib command corefile
The commands are:
help: print this message
overview: print a few overall statistics
mappings: print virtual memory mappings
goroutines: list goroutines
histogram: print histogram of heap memory use by Go type
breakdown: print memory use by class
freespace: print free regions of heap by size
objects: print a list of all live objects
objgraph: dump object graph to a .dot file
reachable: find path from root to an object
Flags applicable to all commands:
`)
flag.PrintDefaults()
}
func main() {
base := flag.String("base", "", "root directory to find core dump file references")
prof := flag.String("prof", "", "profile file")
flag.Parse()
// Extract command.
args := flag.Args()
if len(args) < 1 {
fmt.Fprintf(os.Stderr, "%s: no command specified\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Run 'corelib help' for usage.\n")
os.Exit(2)
}
cmd := args[0]
if cmd == "help" {
usage()
return
}
if *prof != "" {
f, err := os.Create(*prof)
if err != nil {
panic(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
var flags gocore.Flags
switch cmd {
default:
fmt.Fprintf(os.Stderr, "%s: unknown command %s\n", os.Args[0], cmd)
fmt.Fprintf(os.Stderr, "Run 'corelib help' for usage.\n")
os.Exit(2)
case "overview":
case "mappings":
case "goroutines":
case "histogram":
flags = gocore.FlagTypes
case "breakdown":
case "freespace":
case "objgraph":
flags = gocore.FlagTypes
case "objects":
flags = gocore.FlagTypes
case "reachable":
flags = gocore.FlagTypes | gocore.FlagReverse
case "html":
flags = gocore.FlagTypes | gocore.FlagReverse
}
// All commands other than "help" need a core file.
if len(args) < 2 {
fmt.Fprintf(os.Stderr, "%s: no core dump specified for command %s\n", os.Args[0], cmd)
fmt.Fprintf(os.Stderr, "Run 'corelib help' for usage.\n")
os.Exit(2)
}
file := args[1]
p, err := core.Core(file, *base)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
c, err := gocore.Core(p, flags)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
switch cmd {
default:
fmt.Fprintf(os.Stderr, "%s: unknown command %s\n", os.Args[0], cmd)
fmt.Fprintf(os.Stderr, "Run 'corelib help' for usage.\n")
os.Exit(2)
case "overview":
t := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
fmt.Fprintf(t, "arch\t%s\n", p.Arch())
fmt.Fprintf(t, "runtime\t%s\n", c.BuildVersion())
var total int64
for _, m := range p.Mappings() {
total += m.Max().Sub(m.Min())
}
fmt.Fprintf(t, "memory\t%.1f MB\n", float64(total)/(1<<20))
t.Flush()
case "mappings":
t := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.AlignRight)
fmt.Fprintf(t, "min\tmax\tperm\tsource\toriginal\t\n")
for _, m := range p.Mappings() {
perm := ""
if m.Perm()&core.Read != 0 {
perm += "r"
} else {
perm += "-"
}
if m.Perm()&core.Write != 0 {
perm += "w"
} else {
perm += "-"
}
if m.Perm()&core.Exec != 0 {
perm += "x"
} else {
perm += "-"
}
file, off := m.Source()
fmt.Fprintf(t, "%x\t%x\t%s\t%s@%x\t", m.Min(), m.Max(), perm, file, off)
if m.CopyOnWrite() {
file, off = m.OrigSource()
fmt.Fprintf(t, "%s@%x", file, off)
}
fmt.Fprintf(t, "\t\n")
}
t.Flush()
case "goroutines":
for _, g := range c.Goroutines() {
fmt.Printf("G stacksize=%x\n", g.Stack())
for _, f := range g.Frames() {
pc := f.PC()
entry := f.Func().Entry()
var adj string
switch {
case pc == entry:
adj = ""
case pc < entry:
adj = fmt.Sprintf("-%d", entry.Sub(pc))
default:
adj = fmt.Sprintf("+%d", pc.Sub(entry))
}
fmt.Printf(" %016x %016x %s%s\n", f.Min(), f.Max(), f.Func().Name(), adj)
}
}
case "histogram":
// Produce an object histogram (bytes per type).
type bucket struct {
name string
size int64
count int64
}
var buckets []*bucket
m := map[string]*bucket{}
c.ForEachObject(func(x gocore.Object) bool {
name := typeName(c, x)
b := m[name]
if b == nil {
b = &bucket{name: name, size: c.Size(x)}
buckets = append(buckets, b)
m[name] = b
}
b.count++
return true
})
sort.Slice(buckets, func(i, j int) bool {
return buckets[i].size*buckets[i].count > buckets[j].size*buckets[j].count
})
t := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.AlignRight)
fmt.Fprintf(t, "%s\t%s\t%s\t %s\n", "count", "size", "bytes", "type")
for _, e := range buckets {
fmt.Fprintf(t, "%d\t%d\t%d\t %s\n", e.count, e.size, e.count*e.size, e.name)
}
t.Flush()
case "breakdown":
t := tabwriter.NewWriter(os.Stdout, 0, 8, 1, ' ', tabwriter.AlignRight)
all := c.Stats().Size
var printStat func(*gocore.Stats, string)
printStat = func(s *gocore.Stats, indent string) {
comment := ""
switch s.Name {
case "bss":
comment = "(grab bag, includes OS thread stacks, ...)"
case "manual spans":
comment = "(Go stacks)"
case "retained":
comment = "(kept for reuse by Go)"
case "released":
comment = "(given back to the OS)"
}
fmt.Fprintf(t, "%s\t%d\t%6.2f%%\t %s\n", fmt.Sprintf("%-20s", indent+s.Name), s.Size, float64(s.Size)*100/float64(all), comment)
for _, c := range s.Children {
printStat(c, indent+" ")
}
}
printStat(c.Stats(), "")
t.Flush()
case "freespace":
// hist is a map from size to # of slots of that size
hist := map[int64]int64{}
c.ForEachFreeRegion(func(a core.Address, size int64) bool {
hist[size]++
return true
})
type entry struct {
size int64
count int64
}
entries := make([]entry, 0, len(hist))
for size, count := range hist {
entries = append(entries, entry{size: size, count: count})
}
sort.Slice(entries, func(i, j int) bool {
return entries[i].size > entries[j].size
})
t := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.AlignRight)
fmt.Fprintf(t, "size\tcount\ttotal\t\n")
for _, e := range entries {
fmt.Fprintf(t, "%d\t%d\t%d\t\n", e.size, e.count, e.size*e.count)
}
t.Flush()
case "objgraph":
// Dump object graph to output file.
w, err := os.Create("tmp.dot")
if err != nil {
panic(err)
}
fmt.Fprintf(w, "digraph {\n")
for k, r := range c.Globals() {
printed := false
c.ForEachRootPtr(r, func(i int64, y gocore.Object, j int64) bool {
if !printed {
fmt.Fprintf(w, "r%d [label=\"%s\n%s\",shape=hexagon]\n", k, r.Name, r.Type)
printed = true
}
fmt.Fprintf(w, "r%d -> o%x [label=\"%s\"", k, c.Addr(y), typeFieldName(r.Type, i))
if j != 0 {
fmt.Fprintf(w, " ,headlabel=\"+%d\"", j)
}
fmt.Fprintf(w, "]\n")
return true
})
}
for _, g := range c.Goroutines() {
last := fmt.Sprintf("o%x", g.Addr())
for _, f := range g.Frames() {
frame := fmt.Sprintf("f%x", f.Max())
fmt.Fprintf(w, "%s [label=\"%s\",shape=rectangle]\n", frame, f.Func().Name())
fmt.Fprintf(w, "%s -> %s [style=dotted]\n", last, frame)
last = frame
for _, r := range f.Roots() {
c.ForEachRootPtr(r, func(i int64, y gocore.Object, j int64) bool {
fmt.Fprintf(w, "%s -> o%x [label=\"%s%s\"", frame, c.Addr(y), r.Name, typeFieldName(r.Type, i))
if j != 0 {
fmt.Fprintf(w, " ,headlabel=\"+%d\"", j)
}
fmt.Fprintf(w, "]\n")
return true
})
}
}
}
c.ForEachObject(func(x gocore.Object) bool {
addr := c.Addr(x)
size := c.Size(x)
fmt.Fprintf(w, "o%x [label=\"%s\\n%d\"]\n", addr, typeName(c, x), size)
c.ForEachPtr(x, func(i int64, y gocore.Object, j int64) bool {
fmt.Fprintf(w, "o%x -> o%x [label=\"%s\"", addr, c.Addr(y), fieldName(c, x, i))
if j != 0 {
fmt.Fprintf(w, ",headlabel=\"+%d\"", j)
}
fmt.Fprintf(w, "]\n")
return true
})
return true
})
fmt.Fprintf(w, "}")
w.Close()
case "objects":
c.ForEachObject(func(x gocore.Object) bool {
fmt.Printf("%16x %s\n", c.Addr(x), typeName(c, x))
return true
})
case "reachable":
if len(args) < 3 {
fmt.Fprintf(os.Stderr, "no object address provided\n")
os.Exit(1)
}
n, err := strconv.ParseInt(args[2], 16, 64)
if err != nil {
fmt.Fprintf(os.Stderr, "can't parse %s as an object address\n", args[2])
os.Exit(1)
}
a := core.Address(n)
obj, _ := c.FindObject(a)
if obj == 0 {
fmt.Fprintf(os.Stderr, "can't find object at address %s\n", args[2])
os.Exit(1)
}
// Breadth-first search backwards until we reach a root.
type hop struct {
i int64 // offset in "from" object (the key in the path map) where the pointer is
x gocore.Object // the "to" object
j int64 // the offset in the "to" object
}
depth := map[gocore.Object]int{}
depth[obj] = 0
q := []gocore.Object{obj}
done := false
for !done {
if len(q) == 0 {
panic("can't find a root that can reach the object")
}
y := q[0]
q = q[1:]
c.ForEachReversePtr(y, func(x gocore.Object, r *gocore.Root, i, j int64) bool {
if r != nil {
// found it.
if r.Frame == nil {
// Print global
fmt.Printf("%s", r.Name)
} else {
// Print stack up to frame in question.
var frames []*gocore.Frame
for f := r.Frame.Parent(); f != nil; f = f.Parent() {
frames = append(frames, f)
}
for k := len(frames) - 1; k >= 0; k-- {
fmt.Printf("%s\n", frames[k].Func().Name())
}
// Print frame + variable in frame.
fmt.Printf("%s.%s", r.Frame.Func().Name(), r.Name)
}
fmt.Printf("%s → \n", field(r.Type, i))
z := y
for {
fmt.Printf("%x %s", c.Addr(z), typeName(c, z))
if z == obj {
fmt.Println()
break
}
// Find an edge out of z which goes to an object
// closer to obj.
c.ForEachPtr(z, func(i int64, w gocore.Object, j int64) bool {
if d, ok := depth[w]; ok && d < depth[z] {
fmt.Printf(" %s → %s", objField(c, z, i), objRegion(c, w, j))
z = w
return false
}
return true
})
fmt.Println()
}
done = true
return false
}
if _, ok := depth[x]; ok {
// we already found a shorter path to this object.
return true
}
depth[x] = depth[y] + 1
q = append(q, x)
return true
})
}
case "html":
serveHtml(c)
}
}
// typeName returns a string representing the type of this object.
func typeName(c *gocore.Process, x gocore.Object) string {
size := c.Size(x)
typ, repeat := c.Type(x)
if typ == nil {
return fmt.Sprintf("unk%d", size)
}
name := typ.String()
n := size / typ.Size
if n > 1 {
if repeat < n {
name = fmt.Sprintf("[%d+%d?]%s", repeat, n-repeat, name)
} else {
name = fmt.Sprintf("[%d]%s", repeat, name)
}
}
return name
}
// fieldName returns the name of the field at offset off in x.
func fieldName(c *gocore.Process, x gocore.Object, off int64) string {
size := c.Size(x)
typ, repeat := c.Type(x)
if typ == nil {
return fmt.Sprintf("f%d", off)
}
n := size / typ.Size
i := off / typ.Size
if i == 0 && repeat == 1 {
// Probably a singleton object, no need for array notation.
return typeFieldName(typ, off)
}
if i >= n {
// Partial space at the end of the object - the type can't be complete.
return fmt.Sprintf("f%d", off)
}
q := ""
if i >= repeat {
// Past the known repeat section, add a ? because we're not sure about the type.
q = "?"
}
return fmt.Sprintf("[%d]%s%s", i, typeFieldName(typ, off-i*typ.Size), q)
}
// typeFieldName returns the name of the field at offset off in t.
func typeFieldName(t *gocore.Type, off int64) string {
switch t.Kind {
case gocore.KindBool, gocore.KindInt, gocore.KindUint, gocore.KindFloat, gocore.KindComplex:
return ""
case gocore.KindIface, gocore.KindEface:
if off == 0 {
return ".type"
}
return ".data"
case gocore.KindPtr, gocore.KindFunc:
return ""
case gocore.KindString, gocore.KindSlice:
if off == 0 {
return ".ptr"
}
if off <= t.Size/2 {
return ".len"
}
return ".cap"
case gocore.KindArray:
s := t.Elem.Size
i := off / s
return fmt.Sprintf("[%d]", i) + typeFieldName(t.Elem, off-i*s)
case gocore.KindStruct:
for _, f := range t.Fields {
if f.Off <= off && off < f.Off+f.Type.Size {
return "." + f.Name + typeFieldName(f.Type, off-f.Off)
}
}
}
return "???"
}