cdebugginggdbmemset

gdb - how to call memset for the array of pointers


I debug an example program which defines the array of pointers:

int a = 1, b = 2, c = 3;
int* t[] = {&a, &b, &c};

I would like to set all pointers in the array to NULL during debugging. When I use the following command:

call memset(t, 0x0, sizeof(int*)*3)

I get this output:

$3 = (void *(*)(void *, int, size_t)) 0x7ffff77e7e10 <__memset_avx2_unaligned_erms>

When I print the array pointers are not set to NULL:

(gdb) print t
$4 = {0x7fffffffddc0, 0x7fffffffddc4, 0x7fffffffddc8}

What is wrong ?


Solution

  • I get this output:

    You get this output because in your version of GLIBC memset is a GNU indirect function. It doesn't write any memory, it returns an address of the actual implementation (__memset_avx2_unaligned_erms in your case).

    You can verify that this is the case:

    $ readelf -Ws /lib64/libc.so.6 | grep ' memset'
      1233: 00000000000b2df0   241 IFUNC   GLOBAL DEFAULT   14 memset@@GLIBC_2.2.5
       557: 00000000000b2df0   241 FUNC    LOCAL  DEFAULT   14 memset_ifunc
      6000: 00000000000b2df0   241 IFUNC   GLOBAL DEFAULT   14 memset
    

    To actually set the memory, you need to call the implementation function, such as __memset_avx2_unaligned_erms.

    P.S. To memset an array of 3 pointers, it's easier to simply set each one individually: (gdb) t[0]=0. But I assume the object you actually want to zero out is larger.

    For ease of debugging, you may write a trivial local_memset() and call it instead.