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.
5 // Implementation of runtime/debug.WriteHeapDump. Writes all
6 // objects in the heap plus additional info (roots, threads,
7 // finalizers, etc.) to a file.
9 // The format of the dumped file is described at
10 // http://code.google.com/p/go-wiki/wiki/heapdump13
21 #define KindNoPointers GO_NO_POINTERS
42 TagQueuedFinalizer
= 11,
50 TypeInfo_Conservative
= 127,
53 // static uintptr* playgcprog(uintptr offset, uintptr *prog, void (*callback)(void*,uintptr,uintptr), void *arg);
54 // static void dumpfields(uintptr *prog);
55 static void dumpefacetypes(void *obj
, uintptr size
, const Type
*type
, uintptr kind
);
57 // fd to write the dump to.
58 static uintptr dumpfd
;
60 // buffer of pending write data
64 static byte buf
[BufSize
];
68 hwrite(const byte
*data
, uintptr len
)
70 if(len
+ nbuf
<= BufSize
) {
71 runtime_memmove(buf
+ nbuf
, data
, len
);
75 runtime_write(dumpfd
, buf
, nbuf
);
77 runtime_write(dumpfd
, data
, len
);
80 runtime_memmove(buf
, data
, len
);
88 runtime_write(dumpfd
, buf
, nbuf
);
92 // Cache of types that have been serialized already.
93 // We use a type's hash field to pick a bucket.
94 // Inside a bucket, we keep a list of types that
95 // have been serialized so far, most recently used first.
96 // Note: when a bucket overflows we may end up
97 // serializing a type more than once. That's ok.
99 TypeCacheBuckets
= 256, // must be a power of 2
102 typedef struct TypeCacheBucket TypeCacheBucket
;
103 struct TypeCacheBucket
{
104 const Type
*t
[TypeCacheAssoc
];
106 static TypeCacheBucket typecache
[TypeCacheBuckets
];
108 // dump a uint64 in a varint format parseable by encoding/binary
129 // dump varint uint64 length followed by memory contents
131 dumpmemrange(const byte
*data
, uintptr len
)
140 dumpmemrange(s
.str
, s
.len
);
144 dumpcstr(const int8
*c
)
146 dumpmemrange((const byte
*)c
, runtime_findnull((const byte
*)c
));
149 // dump information for a type
151 dumptype(const Type
*t
)
160 // If we've definitely serialized the type before,
161 // no need to do it again.
162 b
= &typecache
[t
->hash
& (TypeCacheBuckets
-1)];
163 if(t
== b
->t
[0]) return;
164 for(i
= 1; i
< TypeCacheAssoc
; i
++) {
167 for(j
= i
; j
> 0; j
--) {
174 // Might not have been dumped yet. Dump it and
175 // remember we did so.
176 for(j
= TypeCacheAssoc
-1; j
> 0; j
--) {
185 if(t
->__uncommon
== nil
|| t
->__uncommon
->__pkg_path
== nil
|| t
->__uncommon
->__name
== nil
) {
186 dumpstr(*t
->__reflection
);
188 dumpint(t
->__uncommon
->__pkg_path
->len
+ 1 + t
->__uncommon
->__name
->len
);
189 hwrite(t
->__uncommon
->__pkg_path
->str
, t
->__uncommon
->__pkg_path
->len
);
190 hwrite((const byte
*)".", 1);
191 hwrite(t
->__uncommon
->__name
->str
, t
->__uncommon
->__name
->len
);
193 dumpbool(t
->__size
> PtrSize
|| (t
->__code
& KindNoPointers
) == 0);
194 // dumpfields((uintptr*)t->gc + 1);
197 // returns true if object is scannable
201 uintptr
*b
, off
, shift
;
203 off
= (uintptr
*)obj
- (uintptr
*)runtime_mheap
.arena_start
; // word offset
204 b
= (uintptr
*)runtime_mheap
.arena_start
- off
/wordsPerBitmapWord
- 1;
205 shift
= off
% wordsPerBitmapWord
;
206 return ((*b
>> shift
) & bitScan
) != 0;
211 dumpobj(byte
*obj
, uintptr size
, const Type
*type
, uintptr kind
)
215 dumpefacetypes(obj
, size
, type
, kind
);
219 dumpint((uintptr
)obj
);
220 dumpint((uintptr
)type
);
222 dumpmemrange(obj
, size
);
226 dumpotherroot(const char *description
, byte
*to
)
228 dumpint(TagOtherRoot
);
229 dumpcstr((const int8
*)description
);
230 dumpint((uintptr
)to
);
234 dumpfinalizer(byte
*obj
, FuncVal
*fn
, const FuncType
* ft
, const PtrType
*ot
)
236 dumpint(TagFinalizer
);
237 dumpint((uintptr
)obj
);
238 dumpint((uintptr
)fn
);
239 dumpint((uintptr
)fn
->fn
);
240 dumpint((uintptr
)ft
);
241 dumpint((uintptr
)ot
);
244 typedef struct ChildInfo ChildInfo
;
246 // Information passed up from the callee frame about
247 // the layout of the outargs region.
248 uintptr argoff
; // where the arguments start in the frame
249 uintptr arglen
; // size of args region
250 BitVector args
; // if args.n >= 0, pointer map of args region
252 byte
*sp
; // callee sp
253 uintptr depth
; // depth in call stack (0 == most recent)
263 dumpint(TagGoRoutine
);
264 dumpint((uintptr
)gp
);
269 dumpbool(gp
->issystem
);
270 dumpbool(gp
->isbackground
);
271 dumpint(gp
->waitsince
);
272 dumpcstr((const int8
*)gp
->waitreason
);
274 dumpint((uintptr
)gp
->m
);
275 dumpint((uintptr
)gp
->defer
);
276 dumpint((uintptr
)gp
->panic
);
279 // child.args.n = -1;
283 // if(!ScanStackByFrames)
284 // runtime_throw("need frame info to dump stacks");
285 // runtime_gentraceback(pc, sp, lr, gp, 0, nil, 0x7fffffff, dumpframe, &child, false);
287 // dump defer & panic records
288 for(d
= gp
->defer
; d
!= nil
; d
= d
->__next
) {
291 dumpint((uintptr
)gp
);
292 dumpint((uintptr
)d
->__arg
);
293 dumpint((uintptr
)d
->__frame
);
294 dumpint((uintptr
)d
->__pfn
);
296 dumpint((uintptr
)d
->__next
);
298 for (p
= gp
->panic
; p
!= nil
; p
= p
->__next
) {
301 dumpint((uintptr
)gp
);
302 dumpint((uintptr
)p
->__arg
.__type_descriptor
);
303 dumpint((uintptr
)p
->__arg
.__object
);
305 dumpint((uintptr
)p
->__next
);
315 // goroutines & stacks
316 for(i
= 0; i
< runtime_allglen
; i
++) {
317 gp
= runtime_allg
[i
];
320 runtime_printf("unexpected G.status %d\n", gp
->status
);
321 runtime_throw("mark - bad status");
334 finq_callback(FuncVal
*fn
, void *obj
, const FuncType
*ft
, const PtrType
*ot
)
336 dumpint(TagQueuedFinalizer
);
337 dumpint((uintptr
)obj
);
338 dumpint((uintptr
)fn
);
339 dumpint((uintptr
)fn
->fn
);
340 dumpint((uintptr
)ft
);
341 dumpint((uintptr
)ot
);
348 MSpan
*s
, **allspans
;
351 SpecialFinalizer
*spf
;
356 // dumpint((uintptr)data);
357 // dumpmemrange(data, edata - data);
358 // dumpfields((uintptr*)gcdata + 1);
362 // dumpint((uintptr)bss);
363 // dumpmemrange(bss, ebss - bss);
364 // dumpfields((uintptr*)gcbss + 1);
367 allspans
= runtime_mheap
.allspans
;
368 for(spanidx
=0; spanidx
<runtime_mheap
.nspan
; spanidx
++) {
369 s
= allspans
[spanidx
];
370 if(s
->state
== MSpanInUse
) {
371 // The garbage collector ignores type pointers stored in MSpan.types:
372 // - Compiler-generated types are stored outside of heap.
373 // - The reflect package has runtime-generated types cached in its data structures.
374 // The garbage collector relies on finding the references via that cache.
375 switch(s
->types
.compression
) {
381 dumpotherroot("runtime type info", (byte
*)s
->types
.data
);
386 for(sp
= s
->specials
; sp
!= nil
; sp
= sp
->next
) {
387 if(sp
->kind
!= KindSpecialFinalizer
)
389 spf
= (SpecialFinalizer
*)sp
;
390 p
= (byte
*)((s
->start
<< PageShift
) + spf
->offset
);
391 dumpfinalizer(p
, spf
->fn
, spf
->ft
, spf
->ot
);
397 runtime_iterate_finq(finq_callback
);
400 // Bit vector of free marks.
401 // Needs to be as big as the largest number of objects per span.
402 static byte hfree
[PageSize
/8];
407 uintptr i
, j
, size
, n
, off
, shift
, *bitp
, bits
, ti
, kind
;
413 for(i
= 0; i
< runtime_mheap
.nspan
; i
++) {
414 s
= runtime_mheap
.allspans
[i
];
415 if(s
->state
!= MSpanInUse
)
417 p
= (byte
*)(s
->start
<< PageShift
);
419 n
= (s
->npages
<< PageShift
) / size
;
421 runtime_throw("free array doesn't have enough entries");
422 for(l
= s
->freelist
; l
!= nil
; l
= l
->next
) {
423 hfree
[((byte
*)l
- p
) / size
] = true;
425 for(j
= 0; j
< n
; j
++, p
+= size
) {
430 off
= (uintptr
*)p
- (uintptr
*)runtime_mheap
.arena_start
;
431 bitp
= (uintptr
*)runtime_mheap
.arena_start
- off
/wordsPerBitmapWord
- 1;
432 shift
= off
% wordsPerBitmapWord
;
433 bits
= *bitp
>> shift
;
435 // Skip FlagNoGC allocations (stacks)
436 if((bits
& bitAllocated
) == 0)
439 // extract type and kind
440 ti
= runtime_gettype(p
);
441 t
= (Type
*)(ti
& ~(uintptr
)(PtrSize
-1));
442 kind
= ti
& (PtrSize
-1);
445 if(kind
== TypeInfo_Chan
)
446 t
= ((const ChanType
*)t
)->__element_type
; // use element type for chan encoding
447 if(t
== nil
&& scannable(p
))
448 kind
= TypeInfo_Conservative
; // special kind for conservatively scanned objects
449 dumpobj(p
, size
, t
, kind
);
462 dumpbool(false); // little-endian ptrs
464 dumpbool(true); // big-endian ptrs
466 dumpint(runtime_Hchansize
);
467 dumpint((uintptr
)runtime_mheap
.arena_start
);
468 dumpint((uintptr
)runtime_mheap
.arena_used
);
470 dumpcstr((const int8
*)"");
471 dumpint(runtime_ncpu
);
479 for(mp
= runtime_allm
; mp
!= nil
; mp
= mp
->alllink
) {
480 dumpint(TagOSThread
);
481 dumpint((uintptr
)mp
);
492 dumpint(TagMemStats
);
493 dumpint(mstats
.alloc
);
494 dumpint(mstats
.total_alloc
);
496 dumpint(mstats
.nlookup
);
497 dumpint(mstats
.nmalloc
);
498 dumpint(mstats
.nfree
);
499 dumpint(mstats
.heap_alloc
);
500 dumpint(mstats
.heap_sys
);
501 dumpint(mstats
.heap_idle
);
502 dumpint(mstats
.heap_inuse
);
503 dumpint(mstats
.heap_released
);
504 dumpint(mstats
.heap_objects
);
505 dumpint(mstats
.stacks_inuse
);
506 dumpint(mstats
.stacks_sys
);
507 dumpint(mstats
.mspan_inuse
);
508 dumpint(mstats
.mspan_sys
);
509 dumpint(mstats
.mcache_inuse
);
510 dumpint(mstats
.mcache_sys
);
511 dumpint(mstats
.buckhash_sys
);
512 dumpint(mstats
.gc_sys
);
513 dumpint(mstats
.other_sys
);
514 dumpint(mstats
.next_gc
);
515 dumpint(mstats
.last_gc
);
516 dumpint(mstats
.pause_total_ns
);
517 for(i
= 0; i
< 256; i
++)
518 dumpint(mstats
.pause_ns
[i
]);
519 dumpint(mstats
.numgc
);
523 dumpmemprof_callback(Bucket
*b
, uintptr nstk
, Location
*stk
, uintptr size
, uintptr allocs
, uintptr frees
)
532 for(i
= 0; i
< nstk
; i
++) {
534 if(stk
[i
].function
.len
== 0) {
535 runtime_snprintf(buf
, sizeof(buf
), "%X", (uint64
)pc
);
536 dumpcstr((int8
*)buf
);
537 dumpcstr((const int8
*)"?");
540 dumpstr(stk
[i
].function
);
541 dumpstr(stk
[i
].filename
);
542 dumpint(stk
[i
].lineno
);
552 MSpan
*s
, **allspans
;
558 runtime_iterate_memprof(dumpmemprof_callback
);
560 allspans
= runtime_mheap
.allspans
;
561 for(spanidx
=0; spanidx
<runtime_mheap
.nspan
; spanidx
++) {
562 s
= allspans
[spanidx
];
563 if(s
->state
!= MSpanInUse
)
565 for(sp
= s
->specials
; sp
!= nil
; sp
= sp
->next
) {
566 if(sp
->kind
!= KindSpecialProfile
)
568 spp
= (SpecialProfile
*)sp
;
569 p
= (byte
*)((s
->start
<< PageShift
) + spp
->offset
);
570 dumpint(TagAllocSample
);
572 dumpint((uintptr
)spp
->b
);
584 // make sure we're done sweeping
585 for(i
= 0; i
< runtime_mheap
.nspan
; i
++) {
586 s
= runtime_mheap
.allspans
[i
];
587 if(s
->state
== MSpanInUse
)
588 runtime_MSpan_EnsureSwept(s
);
591 runtime_memclr((byte
*)&typecache
[0], sizeof(typecache
));
592 hdr
= (const byte
*)"go1.3 heap dump\n";
593 hwrite(hdr
, runtime_findnull(hdr
));
605 gp
->status
= Grunning
;
609 void runtime_debug_WriteHeapDump(uintptr
)
610 __asm__(GOSYM_PREFIX
"runtime_debug.WriteHeapDump");
613 runtime_debug_WriteHeapDump(uintptr fd
)
619 runtime_semacquire(&runtime_worldsema
, false);
623 runtime_stoptheworld();
625 // Update stats so we can dump them.
626 // As a side effect, flushes all the MCaches so the MSpan.freelist
627 // lists contain all the free objects.
628 runtime_updatememstats(nil
);
633 // Call dump routine on M stack.
635 g
->status
= Gwaiting
;
636 g
->waitreason
= "dumping heap";
637 runtime_mcall(mdump
);
642 // Start up the world again.
644 runtime_semrelease(&runtime_worldsema
);
645 runtime_starttheworld();
649 // Runs the specified gc program. Calls the callback for every
650 // pointer-like field specified by the program and passes to the
651 // callback the kind and offset of that field within the object.
652 // offset is the offset in the object of the start of the program.
653 // Returns a pointer to the opcode that ended the gc program (either
654 // GC_END or GC_ARRAY_NEXT).
657 playgcprog(uintptr offset, uintptr *prog, void (*callback)(void*,uintptr,uintptr), void *arg)
659 uintptr len, elemsize, i, *end;
666 callback(arg, FieldKindPtr, offset + prog[1]);
670 callback(arg, FieldKindPtr, offset + prog[1]);
677 for(i = 0; i < len; i++) {
678 end = playgcprog(offset + prog[1] + i * elemsize, prog + 4, callback, arg);
679 if(end[0] != GC_ARRAY_NEXT)
680 runtime_throw("GC_ARRAY_START did not have matching GC_ARRAY_NEXT");
687 playgcprog(offset + prog[1], (uintptr*)((byte*)prog + *(int32*)&prog[2]), callback, arg);
691 callback(arg, FieldKindPtr, offset + prog[1]);
695 callback(arg, FieldKindString, offset + prog[1]);
699 callback(arg, FieldKindEface, offset + prog[1]);
703 callback(arg, FieldKindIface, offset + prog[1]);
707 callback(arg, FieldKindSlice, offset + prog[1]);
711 playgcprog(offset + prog[1], (uintptr*)prog[3] + 1, callback, arg);
715 runtime_printf("%D\n", (uint64)prog[0]);
716 runtime_throw("bad gc op");
722 dump_callback(void *p, uintptr kind, uintptr offset)
729 // dumpint() the kind & offset of each field in an object.
731 dumpfields(uintptr *prog)
733 playgcprog(0, prog, dump_callback, nil);
734 dumpint(FieldKindEol);
738 dumpeface_callback(void *p, uintptr kind, uintptr offset)
742 if(kind != FieldKindEface)
744 e = (Eface*)((byte*)p + offset);
745 dumptype(e->__type_descriptor);
749 // The heap dump reader needs to be able to disambiguate
750 // Eface entries. So it needs to know every type that might
751 // appear in such an entry. The following two routines accomplish
754 // Dump all the types that appear in the type field of
755 // any Eface contained in obj.
757 dumpefacetypes(void *obj
__attribute__ ((unused
)), uintptr size
, const Type
*type
, uintptr kind
)
762 case TypeInfo_SingleObject
:
763 //playgcprog(0, (uintptr*)type->gc + 1, dumpeface_callback, obj);
766 for(i
= 0; i
<= size
- type
->__size
; i
+= type
->__size
)
767 //playgcprog(i, (uintptr*)type->gc + 1, dumpeface_callback, obj);
770 if(type
->__size
== 0) // channels may have zero-sized objects in them
772 for(i
= runtime_Hchansize
; i
<= size
- type
->__size
; i
+= type
->__size
)
773 //playgcprog(i, (uintptr*)type->gc + 1, dumpeface_callback, obj);