gpt4 book ai didi

c - 使用 GDB 调试 va_list args

转载 作者:行者123 更新时间:2023-12-03 02:07:47 52 4
gpt4 key购买 nike

我尝试调试 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;
}

所以我尝试调试 ap va_list 参数并写道

   (gdb) p *(int *)(((char *)ap[0].reg_save_area)+ap[0].gp_offset)

但我从 GDB 得到结果

Attempt to dereference a generic pointer.

这是结果的图像:

最佳答案

当你在gdb中停在第8行时,这行代码还没有执行:

va_start(ap, count); /* Requires the last fixed parameter (to get the address) */

因此 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

关于c - 使用 GDB 调试 va_list args,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42072355/

52 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com