Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

debug va_list args with GDB

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;
}

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)

but i get as result from GDB

Attempt to dereference a generic pointer.

here is an image for the result:

like image 240
Ebraheem Kashkoush Avatar asked Aug 15 '26 17:08

Ebraheem Kashkoush


1 Answers

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) */

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
like image 124
ks1322 Avatar answered Aug 18 '26 06:08

ks1322



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!