使用GDB调试va_list args [英] debug va_list args with GDB

查看:69
本文介绍了使用GDB调试va_list args的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图调试Va_list参数并打印变量值示例代码为:

I tried to debug Va_list argument and print the variable value sample code is :

#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参数,然后我写了

so i tried to debug ap va_list argument and i wrote

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

但是我从GDB得到了结果

but i get as result from GDB

Attempt to dereference a generic pointer.

这是结果的图像:

推荐答案

当您在gdb中的第8行停止时,尚未执行以下代码行:

When you stopped at line 8 in gdb, this line of code was not yet executed:

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

因此, ap 变量尚未初始化,因此无法打印.您应该执行下一行代码并再次打印 ap :

Therefore ap variable was not yet initialized and you can't print it. You should execute next line of code and print ap once again:

(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调试va_list args的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆