wander95
wander95

Reputation: 1366

How do I write a loop in a gdb script?

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)'.

Upvotes: 14

Views: 31730

Answers (5)

Ravi Kumar Yadav
Ravi Kumar Yadav

Reputation: 339

set $i = 0
while ($i<5)
  p $i
  //call function
  set $i = $i + 1
end

Upvotes: 1

Ravi Kumar Yadav
Ravi Kumar Yadav

Reputation: 339

set $i = 0
p $i++

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

Upvotes: 2

Nefira
Nefira

Reputation: 85

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.

Upvotes: 4

wander95
wander95

Reputation: 1366

The other answer totally missed the point. The hint was the reported error:

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

The hint is <$end), which means there is a syntax error in the while statement. By experimenting further, I am posting my results if others need it in the future:

 set $ipx=0
 set $end=32

 while ($ipx .lt. 32)
     print $ipx
     print ro($ipx, 1)
     set $ipx=$ipx+1
 end

The key was to use the fortran syntax for comparison ($ipx .lt. 32) instead of the usual "c" syntax ($ipx < 32).

Upvotes: 13

Peter Teoh
Peter Teoh

Reputation: 6733

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.

Upvotes: 0

Related Questions