23 January 2011

gdb trick for printing array content

This is taken from Fedora planet, somekind of blog aggregator of the member of Fedora Community. This post discusses about the way we can print array values in gdb.

Here is the link http://wagiaalla.com/2011/01/20/gdb-tricks-printing-arrays/, written by Sami Wagiaalla.

....And here is some excerpt from it:

 int main(){
   int *a;
   int b[3] = {1,2,3};
   a = b;

   int *c[3] = {a, b, 0};
   int **d = c;
   return 0;
 }

   While debugging the above code if you do:

 (gdb) print b
 $4 = {1, 2, 3}

   that works.

 (gdb) print a
 $5 = (int *) 0x7fffffffe0f0

   that works too, but in order to print a as an array you must do:

 (gdb) print (int []) *a
 $7 = {1}

   and when you specify the size it gets better:

 (gdb) print (int [3]) *a
 $8 = {1, 2, 3}

regards,

Mulyadi

1 comment:

Anonymous said...

You can also use '@' variable.

(gdb) p *a@3
$4 = {1, 2, 3}

(gdb) p a[0]@3
$5 = {1, 2, 3}

Or to see addresses.

(gdb) p a@3
$3 = {0xbffff864, 0xbffff8f8, 0xb7e80e37}

How to execute multiple commands directly as ssh argument?

 Perhaps sometimes you need to do this: ssh user@10.1.2.3 ls It is easy understand the above: run ls after getting into 10.1.2.3 via ssh. Pi...