我尝试调试 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.
here is an image for the result:
当你在 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)