C 使用GDB调试va_列表参数

C 使用GDB调试va_列表参数,c,gcc,gdb,variadic-functions,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) */ fo

我尝试调试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;
}
但我从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
(gdb)n
9表示(j=0;j
运算符优先级?看起来Ubuntu 16.04上的gcc没有将
ap
的类型完全输入可执行文件的debuginfo部分。它说这是一个
va_列表
,它是一个
\uu gnuc_va_列表
,但不包括
\uu gnuc_va_列表
的任何其他信息。在16.04上与默认版本的clang一起工作正常.那么,我能用clang编译什么呢?是的,谢谢我用clang编译它,它在Fedora 24上对我有效。调试这种问题的一种方法是分离表达式,然后
ptype
每个元素;最终你会发现一些奇怪的东西,如果你感兴趣,你可以追溯到debuginfo它应该打印(void*)0x7fff5fbffa18之类的内容,这很奇怪,尝试在第9行设置bp并打印
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