yanqi27 / core_analyzer

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

[arm64] catch optimized out value reading error and let the heap walk continue. #61

Closed Celthi closed 2 years ago

Celthi commented 2 years ago

In arm64, the tcache can not be recognized with the following error

(gdb) p tcache
'tcache' has unknown type; cast it to its declared type
(gdb) ptype tcache
type = <thread local variable, no debug info>

This PR catches the optimized out error and let the heap walk continue to give some information.

This fix seems can get the tcache and then read the tcache structure. I made this fix by reviewing the code in value.c and the variable_content_raw seems to be fine with value being half optimized out. https://github.com/bminor/binutils-gdb/blob/d17823bfd35adb4caf3724512c3cd40a5a66402e/gdb/value.c#L1156

Unlike value_content which will require the value not being optimized out. https://github.com/bminor/binutils-gdb/blob/d17823bfd35adb4caf3724512c3cd40a5a66402e/gdb/value.c#L1416

Celthi commented 2 years ago

The unit test will fail with this code change which indicates line if (!read_memory_wrapper(NULL, addr, &tcps, valsz)) { errs out.`


Thread 1 "mallocTest" hit Breakpoint 1, last_call () at mallocTest.cpp:126
126             std::cout << "This is the last function call\n";
[ca_test] Checking heap blocks ...
tcache for ptid.pid [157459]: 0x0
Failed to read thread-local variable "tcache"
tcache for ptid.pid [157459]: 0x0
Failed to read thread-local variable "tcache"
tcache for ptid.pid [157459]: 0x0
Failed to read thread-local variable "tcache"
[ca_test] core analyzer returns wrong heap info of block [4]
[ca_test]       expected:  addr=0xfffff0001240 size=360 inuse=0
[ca_test]       got:       addr=0xfffff0001240 size=360 inuse=1
[ca_test] Failed to check block at 0xfffff0001240
[ca_test] Test failed
make: *** [makefile:21: check] Error 1
Celthi commented 2 years ago

heap will print heap information except those in tcache

(gdb) heap
tcache for ptid.pid [157871]: 0x0
Failed to read thread-local variable "tcache"
tcache for ptid.pid [157871]: 0x0
Failed to read thread-local variable "tcache"
tcache for ptid.pid [157871]: 0x0
Failed to read thread-local variable "tcache"
        Tuning params & stats:
                mmap_threshold=131072
                pagesize=4096
                n_mmaps=17
                n_mmaps_max=65536
                total mmap regions created=17
                mmapped_mem=2887680
                sbrk_base=0xaaaaaaab7000
        Main arena (0xfffff7d4cb10) owns regions:
                [0xaaaaaaab7010 - 0xaaaaaaad8000] Total 131KB in-use 82(74KB) free 1(57KB)
        Dynamic arena (0xffffe8000030) owns regions:
                [0xffffe80008e0 - 0xffffe821c000] Total 2MB in-use 2359(1MB) free 1118(915KB)
        Dynamic arena (0xfffff0000030) owns regions:
                [0xfffff00008e0 - 0xfffff021b000] Total 2MB in-use 2533(1MB) free 971(820KB)
        mmap-ed large memory blocks:
                [0xfffff682f010 - 0xfffff685a000] Total 171KB in-use 1(171KB) free 0(0)
                [0xfffff685a010 - 0xfffff6883000] Total 163KB in-use 1(163KB) free 0(0)
                [0xfffff6883010 - 0xfffff68ad000] Total 167KB in-use 1(167KB) free 0(0)
                [0xfffff68ad010 - 0xfffff68ce000] Total 131KB in-use 1(131KB) free 0(0)
                [0xfffff68ce010 - 0xfffff68ef000] Total 131KB in-use 1(131KB) free 0(0)
                [0xfffff68ef010 - 0xfffff6912000] Total 139KB in-use 1(139KB) free 0(0)
                [0xfffff6912010 - 0xfffff6939000] Total 155KB in-use 1(155KB) free 0(0)
                [0xfffff6939010 - 0xfffff6967000] Total 183KB in-use 1(183KB) free 0(0)
                [0xfffff6967010 - 0xfffff6992000] Total 171KB in-use 1(171KB) free 0(0)
                [0xfffff6992010 - 0xfffff69bb000] Total 163KB in-use 1(163KB) free 0(0)
                [0xfffff69bb010 - 0xfffff69e8000] Total 179KB in-use 1(179KB) free 0(0)
                [0xfffff69e8010 - 0xfffff6a18000] Total 191KB in-use 1(191KB) free 0(0)
                [0xfffff6a18010 - 0xfffff6a42000] Total 167KB in-use 1(167KB) free 0(0)
                [0xfffff6a42010 - 0xfffff6a6c000] Total 167KB in-use 1(167KB) free 0(0)
                [0xfffff6a6c010 - 0xfffff6a9b000] Total 187KB in-use 1(187KB) free 0(0)
                [0xfffff6a9b010 - 0xfffff6abf000] Total 143KB in-use 1(143KB) free 0(0)
                [0xfffff7adf010 - 0xfffff7b10000] Total 195KB in-use 1(195KB) free 0(0)

        There are 3 arenas and 17 mmap-ed memory blocks Total 7MB
        Total 4991 blocks in-use of 5MB
        Total 2090 blocks free of 1MB
Celthi commented 2 years ago

gcc might have bug for tls on Arm64 as listed in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83010 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97344 cc @yanqi27

Celthi commented 2 years ago

Hi @yanqi27 I tried to refactor thread_tache() into one func, but pt_malloc_2_25.cpp will use the following code to get next entry,

            entry = REVEAL_PTR((address_t)entry, next_entry.next);

What is REVEAL_PTR doing here comparing with other versions of pt malloc

Celthi commented 2 years ago

@yanqi27 This PR catches the optimized out error and lets the heap walk continue to give some information.

Could you review if this is acceptable?

Following is the output of heap command in arm64 when break in last_call of the mallocTest unit test program.

(gdb) heap
Failed to evaluate thread-local variable "tcache": value has been optimized out
Failed to evaluate thread-local variable "tcache": value has been optimized out
Failed to evaluate thread-local variable "tcache": value has been optimized out
        Tuning params & stats:
                mmap_threshold=131072
                pagesize=4096
                n_mmaps=17
                n_mmaps_max=65536
                total mmap regions created=17
                mmapped_mem=2908160
                sbrk_base=0xaaaaaaab7000
        Main arena (0xfffff7d4cb10) owns regions:
                [0xaaaaaaab7010 - 0xaaaaaaad8000] Total 131KB in-use 82(74KB) free 1(57KB)
        Dynamic arena (0xffffe8000030) owns regions:
                [0xffffe80008e0 - 0xffffe820d000] Total 2MB in-use 2317(1MB) free 1154(911KB)
        Dynamic arena (0xfffff0000030) owns regions:
                [0xfffff00008e0 - 0xfffff020d000] Total 2MB in-use 2634(1MB) free 978(730KB)
        mmap-ed large memory blocks:
                [0xfffff682a010 - 0xfffff6856000] Total 175KB in-use 1(175KB) free 0(0)
                [0xfffff6856010 - 0xfffff6886000] Total 191KB in-use 1(191KB) free 0(0)
                [0xfffff6886010 - 0xfffff68aa000] Total 143KB in-use 1(143KB) free 0(0)
                [0xfffff68aa010 - 0xfffff68d7000] Total 179KB in-use 1(179KB) free 0(0)
                [0xfffff68d7010 - 0xfffff6904000] Total 179KB in-use 1(179KB) free 0(0)
--Type <RET> for more, q to quit, c to continue without paging--
                [0xfffff6904010 - 0xfffff6932000] Total 183KB in-use 1(183KB) free 0(0)
                [0xfffff6932010 - 0xfffff695b000] Total 163KB in-use 1(163KB) free 0(0)
                [0xfffff695b010 - 0xfffff697c000] Total 131KB in-use 1(131KB) free 0(0)
                [0xfffff697c010 - 0xfffff69a5000] Total 163KB in-use 1(163KB) free 0(0)
                [0xfffff69a5010 - 0xfffff69d3000] Total 183KB in-use 1(183KB) free 0(0)
                [0xfffff69d3010 - 0xfffff69fe000] Total 171KB in-use 1(171KB) free 0(0)
                [0xfffff69fe010 - 0xfffff6a21000] Total 139KB in-use 1(139KB) free 0(0)
                [0xfffff6a21010 - 0xfffff6a46000] Total 147KB in-use 1(147KB) free 0(0)
                [0xfffff6a46010 - 0xfffff6a6d000] Total 155KB in-use 1(155KB) free 0(0)
                [0xfffff6a6d010 - 0xfffff6a99000] Total 175KB in-use 1(175KB) free 0(0)
                [0xfffff6a99010 - 0xfffff6abf000] Total 151KB in-use 1(151KB) free 0(0)
                [0xfffff7adf010 - 0xfffff7b10000] Total 195KB in-use 1(195KB) free 0(0)

        There are 3 arenas and 17 mmap-ed memory blocks Total 6MB
        Total 5050 blocks in-use of 5MB
        Total 2133 blocks free of 1MB