| 1 |
// Copyright 2014 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
// Implementation of runtime/debug.WriteHeapDump. Writes all |
| 6 |
// objects in the heap plus additional info (roots, threads, |
| 7 |
// finalizers, etc.) to a file. |
| 8 |
|
| 9 |
// The format of the dumped file is described at |
| 10 |
// https://golang.org/s/go15heapdump. |
| 11 |
|
| 12 |
package runtime |
| 13 |
|
| 14 |
import ( |
| 15 |
"internal/goarch" |
| 16 |
"unsafe" |
| 17 |
) |
| 18 |
|
| 19 |
//go:linkname runtime_debug_WriteHeapDump runtime_1debug.WriteHeapDump |
| 20 |
func runtime_debug_WriteHeapDump(fd uintptr) { |
| 21 |
stopTheWorld("write heap dump") |
| 22 |
|
| 23 |
// Keep m on this G's stack instead of the system stack. |
| 24 |
// Both readmemstats_m and writeheapdump_m have pretty large |
| 25 |
// peak stack depths and we risk blowing the system stack. |
| 26 |
// This is safe because the world is stopped, so we don't |
| 27 |
// need to worry about anyone shrinking and therefore moving |
| 28 |
// our stack. |
| 29 |
var m MemStats |
| 30 |
systemstack(func() { |
| 31 |
// Call readmemstats_m here instead of deeper in |
| 32 |
// writeheapdump_m because we might blow the system stack |
| 33 |
// otherwise. |
| 34 |
readmemstats_m(&m) |
| 35 |
writeheapdump_m(fd, &m) |
| 36 |
}) |
| 37 |
|
| 38 |
startTheWorld() |
| 39 |
} |
| 40 |
|
| 41 |
const ( |
| 42 |
fieldKindEol = 0 |
| 43 |
fieldKindPtr = 1 |
| 44 |
fieldKindIface = 2 |
| 45 |
fieldKindEface = 3 |
| 46 |
tagEOF = 0 |
| 47 |
tagObject = 1 |
| 48 |
tagOtherRoot = 2 |
| 49 |
tagType = 3 |
| 50 |
tagGoroutine = 4 |
| 51 |
tagStackFrame = 5 |
| 52 |
tagParams = 6 |
| 53 |
tagFinalizer = 7 |
| 54 |
tagItab = 8 |
| 55 |
tagOSThread = 9 |
| 56 |
tagMemStats = 10 |
| 57 |
tagQueuedFinalizer = 11 |
| 58 |
tagData = 12 |
| 59 |
tagBSS = 13 |
| 60 |
tagDefer = 14 |
| 61 |
tagPanic = 15 |
| 62 |
tagMemProf = 16 |
| 63 |
tagAllocSample = 17 |
| 64 |
) |
| 65 |
|
| 66 |
var dumpfd uintptr // fd to write the dump to. |
| 67 |
var tmpbuf []byte |
| 68 |
|
| 69 |
// buffer of pending write data |
| 70 |
const ( |
| 71 |
bufSize = 4096 |
| 72 |
) |
| 73 |
|
| 74 |
var buf [bufSize]byte |
| 75 |
var nbuf uintptr |
| 76 |
|
| 77 |
func dwrite(data unsafe.Pointer, len uintptr) { |
| 78 |
if len == 0 { |
| 79 |
return |
| 80 |
} |
| 81 |
if nbuf+len <= bufSize { |
| 82 |
copy(buf[nbuf:], (*[bufSize]byte)(data)[:len]) |
| 83 |
nbuf += len |
| 84 |
return |
| 85 |
} |
| 86 |
|
| 87 |
write(dumpfd, unsafe.Pointer(&buf), int32(nbuf)) |
| 88 |
if len >= bufSize { |
| 89 |
write(dumpfd, data, int32(len)) |
| 90 |
nbuf = 0 |
| 91 |
} else { |
| 92 |
copy(buf[:], (*[bufSize]byte)(data)[:len]) |
| 93 |
nbuf = len |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
func dwritebyte(b byte) { |
| 98 |
dwrite(unsafe.Pointer(&b), 1) |
| 99 |
} |
| 100 |
|
| 101 |
func flush() { |
| 102 |
write(dumpfd, unsafe.Pointer(&buf), int32(nbuf)) |
| 103 |
nbuf = 0 |
| 104 |
} |
| 105 |
|
| 106 |
// Cache of types that have been serialized already. |
| 107 |
// We use a type's hash field to pick a bucket. |
| 108 |
// Inside a bucket, we keep a list of types that |
| 109 |
// have been serialized so far, most recently used first. |
| 110 |
// Note: when a bucket overflows we may end up |
| 111 |
// serializing a type more than once. That's ok. |
| 112 |
const ( |
| 113 |
typeCacheBuckets = 256 |
| 114 |
typeCacheAssoc = 4 |
| 115 |
) |
| 116 |
|
| 117 |
type typeCacheBucket struct { |
| 118 |
t [typeCacheAssoc]*_type |
| 119 |
} |
| 120 |
|
| 121 |
var typecache [typeCacheBuckets]typeCacheBucket |
| 122 |
|
| 123 |
// dump a uint64 in a varint format parseable by encoding/binary |
| 124 |
func dumpint(v uint64) { |
| 125 |
var buf [10]byte |
| 126 |
var n int |
| 127 |
for v >= 0x80 { |
| 128 |
buf[n] = byte(v | 0x80) |
| 129 |
n++ |
| 130 |
v >>= 7 |
| 131 |
} |
| 132 |
buf[n] = byte(v) |
| 133 |
n++ |
| 134 |
dwrite(unsafe.Pointer(&buf), uintptr(n)) |
| 135 |
} |
| 136 |
|
| 137 |
func dumpbool(b bool) { |
| 138 |
if b { |
| 139 |
dumpint(1) |
| 140 |
} else { |
| 141 |
dumpint(0) |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
// dump varint uint64 length followed by memory contents |
| 146 |
func dumpmemrange(data unsafe.Pointer, len uintptr) { |
| 147 |
dumpint(uint64(len)) |
| 148 |
dwrite(data, len) |
| 149 |
} |
| 150 |
|
| 151 |
func dumpslice(b []byte) { |
| 152 |
dumpint(uint64(len(b))) |
| 153 |
if len(b) > 0 { |
| 154 |
dwrite(unsafe.Pointer(&b[0]), uintptr(len(b))) |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
func dumpstr(s string) { |
| 159 |
sp := stringStructOf(&s) |
| 160 |
dumpmemrange(sp.str, uintptr(sp.len)) |
| 161 |
} |
| 162 |
|
| 163 |
// dump information for a type |
| 164 |
func dumptype(t *_type) { |
| 165 |
if t == nil { |
| 166 |
return |
| 167 |
} |
| 168 |
|
| 169 |
// If we've definitely serialized the type before, |
| 170 |
// no need to do it again. |
| 171 |
b := &typecache[t.hash&(typeCacheBuckets-1)] |
| 172 |
if t == b.t[0] { |
| 173 |
return |
| 174 |
} |
| 175 |
for i := 1; i < typeCacheAssoc; i++ { |
| 176 |
if t == b.t[i] { |
| 177 |
// Move-to-front |
| 178 |
for j := i; j > 0; j-- { |
| 179 |
b.t[j] = b.t[j-1] |
| 180 |
} |
| 181 |
b.t[0] = t |
| 182 |
return |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
// Might not have been dumped yet. Dump it and |
| 187 |
// remember we did so. |
| 188 |
for j := typeCacheAssoc - 1; j > 0; j-- { |
| 189 |
b.t[j] = b.t[j-1] |
| 190 |
} |
| 191 |
b.t[0] = t |
| 192 |
|
| 193 |
// dump the type |
| 194 |
dumpint(tagType) |
| 195 |
dumpint(uint64(uintptr(unsafe.Pointer(t)))) |
| 196 |
dumpint(uint64(t.size)) |
| 197 |
if x := t.uncommontype; x == nil || t.pkgPath == nil || *t.pkgPath == "" { |
| 198 |
dumpstr(t.string()) |
| 199 |
} else { |
| 200 |
pkgpathstr := *t.pkgPath |
| 201 |
pkgpath := stringStructOf(&pkgpathstr) |
| 202 |
namestr := *t.name |
| 203 |
name := stringStructOf(&namestr) |
| 204 |
dumpint(uint64(uintptr(pkgpath.len) + 1 + uintptr(name.len))) |
| 205 |
dwrite(pkgpath.str, uintptr(pkgpath.len)) |
| 206 |
dwritebyte('.') |
| 207 |
dwrite(name.str, uintptr(name.len)) |
| 208 |
} |
| 209 |
dumpbool(t.kind&kindDirectIface == 0 || t.ptrdata != 0) |
| 210 |
} |
| 211 |
|
| 212 |
// dump an object |
| 213 |
func dumpobj(obj unsafe.Pointer, size uintptr, bv bitvector) { |
| 214 |
dumpint(tagObject) |
| 215 |
dumpint(uint64(uintptr(obj))) |
| 216 |
dumpmemrange(obj, size) |
| 217 |
dumpfields(bv) |
| 218 |
} |
| 219 |
|
| 220 |
func dumpotherroot(description string, to unsafe.Pointer) { |
| 221 |
dumpint(tagOtherRoot) |
| 222 |
dumpstr(description) |
| 223 |
dumpint(uint64(uintptr(to))) |
| 224 |
} |
| 225 |
|
| 226 |
func dumpfinalizer(obj unsafe.Pointer, fn *funcval, ft *functype, ot *ptrtype) { |
| 227 |
dumpint(tagFinalizer) |
| 228 |
dumpint(uint64(uintptr(obj))) |
| 229 |
dumpint(uint64(uintptr(unsafe.Pointer(fn)))) |
| 230 |
dumpint(uint64(uintptr(unsafe.Pointer(fn.fn)))) |
| 231 |
dumpint(uint64(uintptr(unsafe.Pointer(ft)))) |
| 232 |
dumpint(uint64(uintptr(unsafe.Pointer(ot)))) |
| 233 |
} |
| 234 |
|
| 235 |
type childInfo struct { |
| 236 |
// Information passed up from the callee frame about |
| 237 |
// the layout of the outargs region. |
| 238 |
argoff uintptr // where the arguments start in the frame |
| 239 |
arglen uintptr // size of args region |
| 240 |
args bitvector // if args.n >= 0, pointer map of args region |
| 241 |
sp *uint8 // callee sp |
| 242 |
depth uintptr // depth in call stack (0 == most recent) |
| 243 |
} |
| 244 |
|
| 245 |
// dump kinds & offsets of interesting fields in bv |
| 246 |
func dumpbv(cbv *bitvector, offset uintptr) { |
| 247 |
for i := uintptr(0); i < uintptr(cbv.n); i++ { |
| 248 |
if cbv.ptrbit(i) == 1 { |
| 249 |
dumpint(fieldKindPtr) |
| 250 |
dumpint(uint64(offset + i*goarch.PtrSize)) |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
func dumpgoroutine(gp *g) { |
| 256 |
sp := gp.syscallsp |
| 257 |
|
| 258 |
dumpint(tagGoroutine) |
| 259 |
dumpint(uint64(uintptr(unsafe.Pointer(gp)))) |
| 260 |
dumpint(uint64(sp)) |
| 261 |
dumpint(uint64(gp.goid)) |
| 262 |
dumpint(uint64(gp.gopc)) |
| 263 |
dumpint(uint64(readgstatus(gp))) |
| 264 |
dumpbool(isSystemGoroutine(gp, false)) |
| 265 |
dumpbool(false) // isbackground |
| 266 |
dumpint(uint64(gp.waitsince)) |
| 267 |
dumpstr(gp.waitreason.String()) |
| 268 |
dumpint(0) |
| 269 |
dumpint(uint64(uintptr(unsafe.Pointer(gp.m)))) |
| 270 |
dumpint(uint64(uintptr(unsafe.Pointer(gp._defer)))) |
| 271 |
dumpint(uint64(uintptr(unsafe.Pointer(gp._panic)))) |
| 272 |
|
| 273 |
// dump defer & panic records |
| 274 |
for d := gp._defer; d != nil; d = d.link { |
| 275 |
dumpint(tagDefer) |
| 276 |
dumpint(uint64(uintptr(unsafe.Pointer(d)))) |
| 277 |
dumpint(uint64(uintptr(unsafe.Pointer(gp)))) |
| 278 |
dumpint(0) |
| 279 |
dumpint(0) |
| 280 |
dumpint(uint64(uintptr(unsafe.Pointer(d.pfn)))) |
| 281 |
dumpint(0) |
| 282 |
dumpint(uint64(uintptr(unsafe.Pointer(d.link)))) |
| 283 |
} |
| 284 |
for p := gp._panic; p != nil; p = p.link { |
| 285 |
dumpint(tagPanic) |
| 286 |
dumpint(uint64(uintptr(unsafe.Pointer(p)))) |
| 287 |
dumpint(uint64(uintptr(unsafe.Pointer(gp)))) |
| 288 |
eface := efaceOf(&p.arg) |
| 289 |
dumpint(uint64(uintptr(unsafe.Pointer(eface._type)))) |
| 290 |
dumpint(uint64(uintptr(unsafe.Pointer(eface.data)))) |
| 291 |
dumpint(0) // was p->defer, no longer recorded |
| 292 |
dumpint(uint64(uintptr(unsafe.Pointer(p.link)))) |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
func dumpgs() { |
| 297 |
assertWorldStopped() |
| 298 |
|
| 299 |
// goroutines & stacks |
| 300 |
forEachG(func(gp *g) { |
| 301 |
status := readgstatus(gp) // The world is stopped so gp will not be in a scan state. |
| 302 |
switch status { |
| 303 |
default: |
| 304 |
print("runtime: unexpected G.status ", hex(status), "\n") |
| 305 |
throw("dumpgs in STW - bad status") |
| 306 |
case _Gdead: |
| 307 |
// ok |
| 308 |
case _Grunnable, |
| 309 |
_Gsyscall, |
| 310 |
_Gwaiting: |
| 311 |
dumpgoroutine(gp) |
| 312 |
} |
| 313 |
}) |
| 314 |
} |
| 315 |
|
| 316 |
func finq_callback(fn *funcval, obj unsafe.Pointer, ft *functype, ot *ptrtype) { |
| 317 |
dumpint(tagQueuedFinalizer) |
| 318 |
dumpint(uint64(uintptr(obj))) |
| 319 |
dumpint(uint64(uintptr(unsafe.Pointer(fn)))) |
| 320 |
dumpint(uint64(uintptr(unsafe.Pointer(fn.fn)))) |
| 321 |
dumpint(uint64(uintptr(unsafe.Pointer(ft)))) |
| 322 |
dumpint(uint64(uintptr(unsafe.Pointer(ot)))) |
| 323 |
} |
| 324 |
|
| 325 |
func dumproots() { |
| 326 |
// To protect mheap_.allspans. |
| 327 |
assertWorldStopped() |
| 328 |
|
| 329 |
// mspan.types |
| 330 |
for _, s := range mheap_.allspans { |
| 331 |
if s.state.get() == mSpanInUse { |
| 332 |
// Finalizers |
| 333 |
for sp := s.specials; sp != nil; sp = sp.next { |
| 334 |
if sp.kind != _KindSpecialFinalizer { |
| 335 |
continue |
| 336 |
} |
| 337 |
spf := (*specialfinalizer)(unsafe.Pointer(sp)) |
| 338 |
p := unsafe.Pointer(s.base() + uintptr(spf.special.offset)) |
| 339 |
dumpfinalizer(p, spf.fn, spf.ft, spf.ot) |
| 340 |
} |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
// Finalizer queue |
| 345 |
iterate_finq(finq_callback) |
| 346 |
} |
| 347 |
|
| 348 |
// Bit vector of free marks. |
| 349 |
// Needs to be as big as the largest number of objects per span. |
| 350 |
var freemark [_PageSize / 8]bool |
| 351 |
|
| 352 |
func dumpobjs() { |
| 353 |
// To protect mheap_.allspans. |
| 354 |
assertWorldStopped() |
| 355 |
|
| 356 |
for _, s := range mheap_.allspans { |
| 357 |
if s.state.get() != mSpanInUse { |
| 358 |
continue |
| 359 |
} |
| 360 |
p := s.base() |
| 361 |
size := s.elemsize |
| 362 |
n := (s.npages << _PageShift) / size |
| 363 |
if n > uintptr(len(freemark)) { |
| 364 |
throw("freemark array doesn't have enough entries") |
| 365 |
} |
| 366 |
|
| 367 |
for freeIndex := uintptr(0); freeIndex < s.nelems; freeIndex++ { |
| 368 |
if s.isFree(freeIndex) { |
| 369 |
freemark[freeIndex] = true |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
for j := uintptr(0); j < n; j, p = j+1, p+size { |
| 374 |
if freemark[j] { |
| 375 |
freemark[j] = false |
| 376 |
continue |
| 377 |
} |
| 378 |
dumpobj(unsafe.Pointer(p), size, makeheapobjbv(p, size)) |
| 379 |
} |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
func dumpparams() { |
| 384 |
dumpint(tagParams) |
| 385 |
x := uintptr(1) |
| 386 |
if *(*byte)(unsafe.Pointer(&x)) == 1 { |
| 387 |
dumpbool(false) // little-endian ptrs |
| 388 |
} else { |
| 389 |
dumpbool(true) // big-endian ptrs |
| 390 |
} |
| 391 |
dumpint(goarch.PtrSize) |
| 392 |
var arenaStart, arenaEnd uintptr |
| 393 |
for i1 := range mheap_.arenas { |
| 394 |
if mheap_.arenas[i1] == nil { |
| 395 |
continue |
| 396 |
} |
| 397 |
for i, ha := range mheap_.arenas[i1] { |
| 398 |
if ha == nil { |
| 399 |
continue |
| 400 |
} |
| 401 |
base := arenaBase(arenaIdx(i1)<<arenaL1Shift | arenaIdx(i)) |
| 402 |
if arenaStart == 0 || base < arenaStart { |
| 403 |
arenaStart = base |
| 404 |
} |
| 405 |
if base+heapArenaBytes > arenaEnd { |
| 406 |
arenaEnd = base + heapArenaBytes |
| 407 |
} |
| 408 |
} |
| 409 |
} |
| 410 |
dumpint(uint64(arenaStart)) |
| 411 |
dumpint(uint64(arenaEnd)) |
| 412 |
dumpstr(goarch.GOARCH) |
| 413 |
dumpstr(buildVersion) |
| 414 |
dumpint(uint64(ncpu)) |
| 415 |
} |
| 416 |
|
| 417 |
func dumpms() { |
| 418 |
for mp := allm; mp != nil; mp = mp.alllink { |
| 419 |
dumpint(tagOSThread) |
| 420 |
dumpint(uint64(uintptr(unsafe.Pointer(mp)))) |
| 421 |
dumpint(uint64(mp.id)) |
| 422 |
dumpint(mp.procid) |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
//go:systemstack |
| 427 |
func dumpmemstats(m *MemStats) { |
| 428 |
assertWorldStopped() |
| 429 |
|
| 430 |
// These ints should be identical to the exported |
| 431 |
// MemStats structure and should be ordered the same |
| 432 |
// way too. |
| 433 |
dumpint(tagMemStats) |
| 434 |
dumpint(m.Alloc) |
| 435 |
dumpint(m.TotalAlloc) |
| 436 |
dumpint(m.Sys) |
| 437 |
dumpint(m.Lookups) |
| 438 |
dumpint(m.Mallocs) |
| 439 |
dumpint(m.Frees) |
| 440 |
dumpint(m.HeapAlloc) |
| 441 |
dumpint(m.HeapSys) |
| 442 |
dumpint(m.HeapIdle) |
| 443 |
dumpint(m.HeapInuse) |
| 444 |
dumpint(m.HeapReleased) |
| 445 |
dumpint(m.HeapObjects) |
| 446 |
dumpint(m.StackInuse) |
| 447 |
dumpint(m.StackSys) |
| 448 |
dumpint(m.MSpanInuse) |
| 449 |
dumpint(m.MSpanSys) |
| 450 |
dumpint(m.MCacheInuse) |
| 451 |
dumpint(m.MCacheSys) |
| 452 |
dumpint(m.BuckHashSys) |
| 453 |
dumpint(m.GCSys) |
| 454 |
dumpint(m.OtherSys) |
| 455 |
dumpint(m.NextGC) |
| 456 |
dumpint(m.LastGC) |
| 457 |
dumpint(m.PauseTotalNs) |
| 458 |
for i := 0; i < 256; i++ { |
| 459 |
dumpint(m.PauseNs[i]) |
| 460 |
} |
| 461 |
dumpint(uint64(m.NumGC)) |
| 462 |
} |
| 463 |
|
| 464 |
func dumpmemprof_callback(b *bucket, nstk uintptr, pstk *uintptr, size, allocs, frees uintptr) { |
| 465 |
stk := (*[100000]uintptr)(unsafe.Pointer(pstk)) |
| 466 |
dumpint(tagMemProf) |
| 467 |
dumpint(uint64(uintptr(unsafe.Pointer(b)))) |
| 468 |
dumpint(uint64(size)) |
| 469 |
dumpint(uint64(nstk)) |
| 470 |
for i := uintptr(0); i < nstk; i++ { |
| 471 |
pc := stk[i] |
| 472 |
fn, file, line, _ := funcfileline(pc, -1, false) |
| 473 |
if fn == "" { |
| 474 |
var buf [64]byte |
| 475 |
n := len(buf) |
| 476 |
n-- |
| 477 |
buf[n] = ')' |
| 478 |
if pc == 0 { |
| 479 |
n-- |
| 480 |
buf[n] = '0' |
| 481 |
} else { |
| 482 |
for pc > 0 { |
| 483 |
n-- |
| 484 |
buf[n] = "0123456789abcdef"[pc&15] |
| 485 |
pc >>= 4 |
| 486 |
} |
| 487 |
} |
| 488 |
n-- |
| 489 |
buf[n] = 'x' |
| 490 |
n-- |
| 491 |
buf[n] = '0' |
| 492 |
n-- |
| 493 |
buf[n] = '(' |
| 494 |
dumpslice(buf[n:]) |
| 495 |
dumpstr("?") |
| 496 |
dumpint(0) |
| 497 |
} else { |
| 498 |
dumpstr(fn) |
| 499 |
dumpstr(file) |
| 500 |
dumpint(uint64(line)) |
| 501 |
} |
| 502 |
} |
| 503 |
dumpint(uint64(allocs)) |
| 504 |
dumpint(uint64(frees)) |
| 505 |
} |
| 506 |
|
| 507 |
func dumpmemprof() { |
| 508 |
// To protect mheap_.allspans. |
| 509 |
assertWorldStopped() |
| 510 |
|
| 511 |
iterate_memprof(dumpmemprof_callback) |
| 512 |
for _, s := range mheap_.allspans { |
| 513 |
if s.state.get() != mSpanInUse { |
| 514 |
continue |
| 515 |
} |
| 516 |
for sp := s.specials; sp != nil; sp = sp.next { |
| 517 |
if sp.kind != _KindSpecialProfile { |
| 518 |
continue |
| 519 |
} |
| 520 |
spp := (*specialprofile)(unsafe.Pointer(sp)) |
| 521 |
p := s.base() + uintptr(spp.special.offset) |
| 522 |
dumpint(tagAllocSample) |
| 523 |
dumpint(uint64(p)) |
| 524 |
dumpint(uint64(uintptr(unsafe.Pointer(spp.b)))) |
| 525 |
} |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
var dumphdr = []byte("go1.7 heap dump\n") |
| 530 |
|
| 531 |
func mdump(m *MemStats) { |
| 532 |
assertWorldStopped() |
| 533 |
|
| 534 |
// make sure we're done sweeping |
| 535 |
for _, s := range mheap_.allspans { |
| 536 |
if s.state.get() == mSpanInUse { |
| 537 |
s.ensureSwept() |
| 538 |
} |
| 539 |
} |
| 540 |
memclrNoHeapPointers(unsafe.Pointer(&typecache), unsafe.Sizeof(typecache)) |
| 541 |
dwrite(unsafe.Pointer(&dumphdr[0]), uintptr(len(dumphdr))) |
| 542 |
dumpparams() |
| 543 |
dumpobjs() |
| 544 |
dumpgs() |
| 545 |
dumpms() |
| 546 |
dumproots() |
| 547 |
dumpmemstats(m) |
| 548 |
dumpmemprof() |
| 549 |
dumpint(tagEOF) |
| 550 |
flush() |
| 551 |
} |
| 552 |
|
| 553 |
func writeheapdump_m(fd uintptr, m *MemStats) { |
| 554 |
assertWorldStopped() |
| 555 |
|
| 556 |
_g_ := getg() |
| 557 |
casgstatus(_g_.m.curg, _Grunning, _Gwaiting) |
| 558 |
_g_.waitreason = waitReasonDumpingHeap |
| 559 |
|
| 560 |
// Update stats so we can dump them. |
| 561 |
// As a side effect, flushes all the mcaches so the mspan.freelist |
| 562 |
// lists contain all the free objects. |
| 563 |
updatememstats() |
| 564 |
|
| 565 |
// Set dump file. |
| 566 |
dumpfd = fd |
| 567 |
|
| 568 |
// Call dump routine. |
| 569 |
mdump(m) |
| 570 |
|
| 571 |
// Reset dump file. |
| 572 |
dumpfd = 0 |
| 573 |
if tmpbuf != nil { |
| 574 |
sysFree(unsafe.Pointer(&tmpbuf[0]), uintptr(len(tmpbuf)), &memstats.other_sys) |
| 575 |
tmpbuf = nil |
| 576 |
} |
| 577 |
|
| 578 |
casgstatus(_g_.m.curg, _Gwaiting, _Grunning) |
| 579 |
} |
| 580 |
|
| 581 |
// dumpint() the kind & offset of each field in an object. |
| 582 |
func dumpfields(bv bitvector) { |
| 583 |
dumpbv(&bv, 0) |
| 584 |
dumpint(fieldKindEol) |
| 585 |
} |
| 586 |
|
| 587 |
func makeheapobjbv(p uintptr, size uintptr) bitvector { |
| 588 |
// Extend the temp buffer if necessary. |
| 589 |
nptr := size / goarch.PtrSize |
| 590 |
if uintptr(len(tmpbuf)) < nptr/8+1 { |
| 591 |
if tmpbuf != nil { |
| 592 |
sysFree(unsafe.Pointer(&tmpbuf[0]), uintptr(len(tmpbuf)), &memstats.other_sys) |
| 593 |
} |
| 594 |
n := nptr/8 + 1 |
| 595 |
p := sysAlloc(n, &memstats.other_sys) |
| 596 |
if p == nil { |
| 597 |
throw("heapdump: out of memory") |
| 598 |
} |
| 599 |
tmpbuf = (*[1 << 30]byte)(p)[:n] |
| 600 |
} |
| 601 |
// Convert heap bitmap to pointer bitmap. |
| 602 |
for i := uintptr(0); i < nptr/8+1; i++ { |
| 603 |
tmpbuf[i] = 0 |
| 604 |
} |
| 605 |
i := uintptr(0) |
| 606 |
hbits := heapBitsForAddr(p) |
| 607 |
for ; i < nptr; i++ { |
| 608 |
if !hbits.morePointers() { |
| 609 |
break // end of object |
| 610 |
} |
| 611 |
if hbits.isPointer() { |
| 612 |
tmpbuf[i/8] |= 1 << (i % 8) |
| 613 |
} |
| 614 |
hbits = hbits.next() |
| 615 |
} |
| 616 |
return bitvector{int32(i), &tmpbuf[0]} |
| 617 |
} |
| 618 |
|
| 619 |
type gobitvector struct { |
| 620 |
n uintptr |
| 621 |
bytedata []uint8 |
| 622 |
} |
| 623 |
|
| 624 |
func gobv(bv bitvector) gobitvector { |
| 625 |
return gobitvector{ |
| 626 |
uintptr(bv.n), |
| 627 |
(*[1 << 30]byte)(unsafe.Pointer(bv.bytedata))[:(bv.n+7)/8], |
| 628 |
} |
| 629 |
} |