]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: add memorydump() debugging function
authorJan Ziak <0xe2.0x9a.0x9b@gmail.com>
Thu, 1 Nov 2012 16:56:25 +0000 (12:56 -0400)
committerRuss Cox <rsc@golang.org>
Thu, 1 Nov 2012 16:56:25 +0000 (12:56 -0400)
R=golang-dev
CC=golang-dev, remyoudompheng, rsc
https://golang.org/cl/6780059

src/pkg/runtime/malloc.h
src/pkg/runtime/mgc0.c

index e221faae37fbe4b05ef5dd5e46e41ff0601b2c95..765cd02eb2f6f4369596e2e7341a0ae906de7b97 100644 (file)
@@ -489,3 +489,5 @@ enum
 
 // defined in mgc0.go
 void   runtime·gc_m_ptr(Eface*);
+
+void   runtime·memorydump(void);
index 4d857bf0b7260408e2dabf5bc15832156dff502d..ab68619d0093eaea98253e68f45d2da4950496fa 100644 (file)
@@ -806,6 +806,81 @@ sweepspan(ParFor *desc, uint32 idx)
        }
 }
 
+static void
+dumpspan(uint32 idx)
+{
+       int32 sizeclass, n, npages, i, column;
+       uintptr size;
+       byte *p;
+       byte *arena_start;
+       MSpan *s;
+       bool allocated, special;
+
+       s = runtime·mheap.allspans[idx];
+       if(s->state != MSpanInUse)
+               return;
+       arena_start = runtime·mheap.arena_start;
+       p = (byte*)(s->start << PageShift);
+       sizeclass = s->sizeclass;
+       size = s->elemsize;
+       if(sizeclass == 0) {
+               n = 1;
+       } else {
+               npages = runtime·class_to_allocnpages[sizeclass];
+               n = (npages << PageShift) / size;
+       }
+       
+       runtime·printf("%p .. %p:\n", p, p+n*size);
+       column = 0;
+       for(; n>0; n--, p+=size) {
+               uintptr off, *bitp, shift, bits;
+
+               off = (uintptr*)p - (uintptr*)arena_start;
+               bitp = (uintptr*)arena_start - off/wordsPerBitmapWord - 1;
+               shift = off % wordsPerBitmapWord;
+               bits = *bitp>>shift;
+
+               allocated = ((bits & bitAllocated) != 0);
+               special = ((bits & bitSpecial) != 0);
+
+               for(i=0; i<size; i+=sizeof(void*)) {
+                       if(column == 0) {
+                               runtime·printf("\t");
+                       }
+                       if(i == 0) {
+                               runtime·printf(allocated ? "(" : "[");
+                               runtime·printf(special ? "@" : "");
+                               runtime·printf("%p: ", p+i);
+                       } else {
+                               runtime·printf(" ");
+                       }
+
+                       runtime·printf("%p", *(void**)(p+i));
+
+                       if(i+sizeof(void*) >= size) {
+                               runtime·printf(allocated ? ") " : "] ");
+                       }
+
+                       column++;
+                       if(column == 8) {
+                               runtime·printf("\n");
+                               column = 0;
+                       }
+               }
+       }
+       runtime·printf("\n");
+}
+
+// A debugging function to dump the contents of memory
+void
+runtime·memorydump(void)
+{
+       uint32 spanidx;
+
+       for(spanidx=0; spanidx<runtime·mheap.nspan; spanidx++) {
+               dumpspan(spanidx);
+       }
+}
 void
 runtime·gchelper(void)
 {