Ebr*_*ush 5 c gcc gdb variadic-functions
我试图调试 Va_list 参数并打印变量值示例代码是:
#include <stdarg.h>
#include <stdio.h>
double average(int count, ...)
{
va_list ap;
int j;
double sum = 0;
va_start(ap, count); /* Requires the last fixed parameter (to get the address) */
for (j = 0; j < count; j++) {
sum += va_arg(ap, int); /* Increments ap to the next argument. */
}
va_end(ap);
return sum / count;
}
int main(int argc, char const *argv[])
{
printf("%f\n", average(3, 1, 2, 3) );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
所以我试图调试 ap va_list 参数,我写道
(gdb) p *(int *)(((char *)ap[0].reg_save_area)+ap[0].gp_offset)
Run Code Online (Sandbox Code Playgroud)
但我得到了 GDB 的结果
Attempt to dereference a generic pointer.
Run Code Online (Sandbox Code Playgroud)
这是结果的图像:

当你在 gdb 中停在第 8 行时,这行代码还没有执行:
va_start(ap, count); /* Requires the last fixed parameter (to get the address) */
Run Code Online (Sandbox Code Playgroud)
因此ap变量尚未初始化,您无法打印它。您应该执行下一行代码并ap再次打印:
(gdb) n
9 for (j = 0; j < count; j++) {
(gdb) p *(int *)(((char *)ap[0].reg_save_area)+ap[0].gp_offset)
$1 = 1
Run Code Online (Sandbox Code Playgroud)