简体   繁体   English

gdb - 如何为指针数组调用 memset

[英]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.我想在调试期间将数组中的所有指针设置为 NULL。 When I use the following command:当我使用以下命令时:

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

I get this output:我得到这个 output:

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

When I print the array pointers are not set to NULL:当我打印数组指针未设置为 NULL 时:

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

What is wrong?怎么了?

I get this output:我得到这个 output:

You get this output because in your version of GLIBC memset is a GNU indirect function .你得到这个 output 因为在你的 GLIBC memset版本中是一个GNU 间接 function It doesn't write any memory, it returns an address of the actual implementation ( __memset_avx2_unaligned_erms in your case).它不写任何 memory,它返回实际实现的地址(在您的情况下为__memset_avx2_unaligned_erms )。

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 .要实际设置 memory,需要调用实现function,例如__memset_avx2_unaligned_erms

PS To memset an array of 3 pointers, it's easier to simply set each one individually: (gdb) t[0]=0 . PS 要memset一个由 3 个指针组成的数组,简单地单独设置每个指针会更容易: (gdb) t[0]=0 But I assume the object you actually want to zero out is larger.但我假设您实际上想要归零的 object 更大。

For ease of debugging, you may write a trivial local_memset() and call it instead.为了便于调试,您可以编写一个简单的local_memset()并调用

Building on Employed Russian's answer, insert () and use基于 Employed Russian 的回答,插入()并使用

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

That works because memset() returns the function you actually want to call.这是有效的,因为memset()返回您实际想要调用的 function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM