Reputation: 43
I have some Fortran source code that I can understand the general idea behind, but I have never used Fortran before so I would like to see exactly what is happening as each line is being executed (like you can with VBA in Excel by stepping through the code line by line and observing what values variables and arrays have at any point in the code).
Is there a way to step through the source code with a user-interface of some kind so that I can see exactly what variables have been defined, what values they are taking etc..?
For some context: I work in Science and Engineering, but coding is not normally a significant part of my job (as you can probably tell from the content of my question), I have normally only deal with simple scripts in VBA to manipulate data. I have the compiled version of the Fortran code and it works fine, but I know I will need to modify the source and recompile it for my purposes. Unfortunately the person who wrote the original code is not available for advice/input. Another note: I'm not sure how to tell which version of Fortran was used...
Thanks!
Upvotes: 1
Views: 253
Reputation: 7432
Any respectable debugger will allow you to single step through a code. Below is a simple example showing one, gdb
, in use. The important points:
-g
flagrun
runs the codebreak
sets a break point at a given line in the code, i.e. when the code is running it will stop at that linestep
steps one linestep n
steps n linesfinish
runs the program until the endNote this is very much an oversimplified introduction to show what you want to do is possible. In real life you'll have to learn a bit more about your debugger, they are very powerful and useful pieces of software with many abilities not even hinted at here. For gdb there are a number of tutorials you can find by searching, e.g. https://sourceware.org/gdb/onlinedocs/gdb/index.html
ian@eris:~/work/stack$ gfortran -fcheck=all -Wall -Wextra -std=f2008 -g step.f90 -o step
ian@eris:~/work/stack$ gdb step
GNU gdb (Ubuntu 8.1-0ubuntu3.2) 8.1.0.20180409-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from step...done.
(gdb) list
1 Program single_step
2
3 Implicit None
4
5 Integer :: i
6
7 Do i = 1, 10
8 Write( *, * ) 'I is now ', i
9 End Do
10
(gdb) run
Starting program: /home/ian/work/stack/step
I is now 1
I is now 2
I is now 3
I is now 4
I is now 5
I is now 6
I is now 7
I is now 8
I is now 9
I is now 10
[Inferior 1 (process 23965) exited normally]
(gdb) break 1
Breakpoint 1 at 0x5555555548c6: file step.f90, line 1.
(gdb) run
Starting program: /home/ian/work/stack/step
Breakpoint 1, single_step () at step.f90:1
1 Program single_step
(gdb) step
7 Do i = 1, 10
(gdb) step
8 Write( *, * ) 'I is now ', i
(gdb) step
I is now 1
7 Do i = 1, 10
(gdb) step
8 Write( *, * ) 'I is now ', i
(gdb) step
I is now 2
7 Do i = 1, 10
(gdb) step
8 Write( *, * ) 'I is now ', i
(gdb) step 5
I is now 3
I is now 4
I is now 5
7 Do i = 1, 10
(gdb) step
8 Write( *, * ) 'I is now ', i
(gdb) step 2
I is now 6
8 Write( *, * ) 'I is now ', i
(gdb) finish
Run till exit from #0 single_step () at step.f90:8
I is now 7
I is now 8
I is now 9
I is now 10
0x0000555555554a30 in main (argc=1, argv=0x7fffffffe2fa) at step.f90:11
11 End Program single_step
(gdb) quit
A debugging session is active.
Inferior 1 [process 23969] will be killed.
Quit anyway? (y or n) y
ian@eris:~/work/stack$
Upvotes: 5