How do I write a loop in a gdb script?

Viewed 18693

After adapting this answer, I wrote the following loop to simply print an array in gdb in a script called "gdb_script.gs". What am I doing wrong?

set $end=64  
while ($i<$end)
   print $i
   print volfrac($i, :, 1)
   set $i=$i+1
end

where volfrac(:,:,:) is a fortran array. I am getting the error:

 gdb_script.gs:14: Error in sourced command file:
 A syntax error in expression, near `<$end)'.
4 Answers

GDB newbies (as myself) should know that the accepted answer does not work unless you remove the blank between while and the first brace.

Also, the .lt. syntax probably works only for Fortran (https://sourceware.org/gdb/current/onlinedocs/gdb/Fortran.html). As the title of the question is not formulated specifically toward Fortran developers, the accepted answer can be misleading.

set $i = 0
p $i++

keep pressing Enter this is one of the easiest logic I found

Since voltrac() is an array, then I think it is a syntax error as show in your output - it should be "print volfrac[]" instead.

Below I showed you the step by step in detail for a C program (since you are working with gdb, and gdb only worked with ELF file, and so it is the same here - gdb + ELF file for C):

(gdb) run
Starting program: /home/tthtlc/a.out 

Breakpoint 1, main () at main.c:5
5 main(){

First I step through a few times and noticed my assignment:

(gdb) s
8     for(i=0;i<10;i++)
(gdb) 
9     for(j=0;j<10;j++)
(gdb) 
10        for(k=0;k<10;k++) {
(gdb) 
**11      volfrac[i][j][k]=0xdeadbeef;**
(gdb)

Now is the printing out (and notice the different ways of printing the array):

(gdb) print /x volfrac[0][0][0]
$5 = 0xdeadbeef
(gdb) print /x volfrac[0][0]
$6 = {0xdeadbeef, 0xdeadbeef, 0xdeadbeef, 0xdeadbeef, 0xdeadbeef, 0xdeadbeef, 
  0x0, 0x0, 0x0, 0x0}
(gdb) print /x volfrac[0]
$7 = {{0xdeadbeef, 0xdeadbeef, 0xdeadbeef, 0xdeadbeef, 0xdeadbeef, 0xdeadbeef, 
(gdb) print /x volfrac
$8 = {{{0xdeadbeef, 0xdeadbeef, 0xdeadbeef, 0xdeadbeef, 0xdeadbeef, 
      0xdeadbeef, 0x0, 0x0, 0x0, 0x0}, {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 
(gdb) print /x volfrac(0,0,0)
Invalid data type for function to be called.
Related