14

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)'.
user202729
  • 3,358
  • 3
  • 25
  • 36
wander95
  • 1,298
  • 1
  • 15
  • 22

4 Answers4

12

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

wander95
  • 1,298
  • 1
  • 15
  • 22
4

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.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
Nefira
  • 77
  • 6
3
set $i = 0
p $i++

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

Ravi Kumar Yadav
  • 193
  • 2
  • 15
  • 1
    Irrelevant. You didn't read the question. I am trying to print an big Fortran array. If the array is 512x512x4 are you willing to press enter 262144x4 times ? See accepted answer – wander95 Jan 06 '20 at 14:59
  • 1
    this was helpful for my small use case, thank you – TheGaldozer May 06 '23 at 02:02
0

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.
Peter Teoh
  • 6,337
  • 4
  • 42
  • 58