yanqi27 / core_analyzer

A power tool to debug memory-related issues
376 stars 72 forks source link

how to force rebuild heap info while living debug #89

Closed fish2bird closed 1 year ago

fish2bird commented 1 year ago

While I stepping my programming, core_analyzer walk failed util /proc/pid/maps changes. so I have to use gdb over gdb to hack linux_nat_find_memory_regions return false.

Step 1: re-walk heap failed after step throng some functions

(gdb) heap /v
        Tuning params & stats:
                mmap_threshold=131072
                pagesize=4096
                n_mmaps=0
                n_mmaps_max=65536
                total mmap regions created=0
                mmapped_mem=0
                sbrk_base=0x555555771000
        Main arena (0x7ffff779f6e0) owns regions:
                [0x555555771010 - 0x555555792000] Total 131KB
Error: chunk at 0x54552e53555f6e65 in fastbin[0] is misaligned
        Chunk address comes from previous fastbin chunk at 0x555555771000 with fd=0x54552e53555f6e65

Error: chunk at 0x555555771010 witch bk=0x81 that does not point to previous chunk
        Chunk address comes from previous bin[1] chunk at 0x555555771020 with {fd=0x555555771010, bk=0x7ffff6b105e0}

1 Errors encountered while walking the heap!

Step2: use ANOTHER gdb to hijack linux_nat_find_memory_regions

(gdb) return false
Make linux_nat_find_memory_regions(bool) return now? (y or n) y

#0  0x00000000005d7a2f in update_memory_segments_and_heaps () at gdb_dep.c:457
457                     if (g_debug_core || linux_nat_find_memory_regions(false))

step 3: now heap walk works.

(gdb) heap /v
Target process has changed. Rebuild heap information
        Tuning params & stats:
                mmap_threshold=131072
                pagesize=4096
                n_mmaps=0
                n_mmaps_max=65536
                total mmap regions created=0
                mmapped_mem=0
                sbrk_base=0x555555771000
        Main arena (0x7ffff779f6e0) owns regions:
                [0x555555771010 - 0x555555792000] Total 131KB in-use 9(2KB) free 2(129KB)

        There are 1 arenas Total 131KB
        Total 9 blocks in-use of 2KB
        Total 2 blocks free of 129KB

        ========== In-use Memory Histogram ==========
        Size-Range     Count       Total-Bytes
        16 - 32        1(11%)      24(0%)
        64 - 128       4(44%)      432(15%)
        128 - 256      1(11%)      216(7%)
        256 - 512      1(11%)      440(15%)
        512 - 1024     2(22%)      1KB(60%)
        Total          9           2KB
        ========== Free Memory Histogram ==========
        Size-Range     Count       Total-Bytes
        64 - 128       1(50%)      72(0%)
        128KB - 256KB  1(50%)      129KB(99%)
        Total          2           129KB

Rebuild policy expect to be more smart or support manual.

Best regards.

yanqi27 commented 1 year ago

The cost to rebuild the heap metadata is high though it is easy to code it with a single function call. The tool is not designed to debug a live process like stepping through code line by line.

Celthi commented 1 year ago

There is a switch_heapcommand. @fish2bird could you give it a try and see if it will work? I don't have a vivid memory for the command now.

fish2bird commented 1 year ago

There is a switch_heapcommand. @fish2bird could you give it a try and see if it will work? I don't have a vivid memory for the command now.

Thank you, twice-switch works!

(gdb) switch_heap
Please provide the heap manager name, currently supported heap managers: jemalloc, (current)pt 2.27, pt 2.28-2.31, pt 2.32-2.37, tc.
(gdb) switch_heap pt 2.27
switch to heap pt 2.27
(gdb) heap /v
        Tuning params & stats:
                mmap_threshold=131072
                pagesize=4096
                n_mmaps=0
                n_mmaps_max=65536
                total mmap regions created=0
                mmapped_mem=0
                sbrk_base=0x555555771000
        Main arena (0x7ffff779f6e0) owns regions:
                [0x555555771010 - 0x555555792000] Total 131KB
Error: chunk at 0x54552e53555f6e65 in fastbin[0] is misaligned
        Chunk address comes from previous fastbin chunk at 0x555555771000 with fd=0x54552e53555f6e65

1 Errors encountered while walking the heap!
[Error] Failed to walk heap
(gdb) switch_heap tc
switch to heap tc
Failed to lookup gv "kPageShift"
(gdb) switch_heap pt 2.27
switch to heap pt 2.27
(gdb) heap /v
        Tuning params & stats:
                mmap_threshold=131072
                pagesize=4096
                n_mmaps=0
                n_mmaps_max=65536
                total mmap regions created=0
                mmapped_mem=0
                sbrk_base=0x555555771000
        Main arena (0x7ffff779f6e0) owns regions:
                [0x555555771010 - 0x555555792000] Total 131KB in-use 3(920) free 2(131KB)

        There are 1 arenas Total 131KB
        Total 3 blocks in-use of 920
        Total 2 blocks free of 131KB

        ========== In-use Memory Histogram ==========
        Size-Range     Count       Total-Bytes
        16 - 32        1(33%)      24(2%)
        64 - 128       1(33%)      120(13%)
        512 - 1024     1(33%)      776(84%)
        Total          3           920
        ========== Free Memory Histogram ==========
        Size-Range     Count       Total-Bytes
        512 - 1024     1(50%)      536(0%)
        128KB - 256KB  1(50%)      130KB(99%)
        Total          2           131KB
(gdb)