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)'.
1
  • Don't you want to initialize $i before comparing it to anything? Commented Sep 28, 2017 at 2:19

4 Answers 4

13

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

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.

3
  • 1
    You should perhaps mention the GDB version you used.
    – Armali
    Commented Nov 7, 2019 at 13:57
  • Not in title but the text of the question does mention Fortran. Also when I was posting the question, I did not know gdb syntax depends on the language being debugged.
    – wander95
    Commented Nov 7, 2019 at 17:56
  • 1
    @Armali, I am using GDB 7.7.1.
    – Nefira
    Commented Nov 15, 2019 at 13:06
2
set $i = 0
p $i++

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

2
  • 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
    Commented Jan 6, 2020 at 14:59
  • 1
    this was helpful for my small use case, thank you Commented May 6, 2023 at 2: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.
1
  • volfrac is a fortran array
    – wander95
    Commented Oct 11, 2017 at 17:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.