Debugger for Fortran in Linux

I have been trying to search for a good debugger for Fortran code in Linux. But it is very hard to find. I know that GDB is supposed to be able to do it. But when I debug Fortran 95 code compiled using GFortran, I simply don’t know how to display the dynamic size array. I tried to google, and found that other people also find the same issue with GDB and fortran:
comp.lang.fortran

this is what I get when I tried to print an array which was allocatable, and allocated afterward:

(gdb) p myarray
{ 0x80614c8, -1, 537, ({ 1, 1, 6})}

and when I get the type:

(gdb) pt myarray
type = Type array_descriptor1
PTR TO -> ( void ) :: data
int4 :: offset
int4 :: dtype
Type descriptor_dimension
int4 :: stride
int4 :: lbound
int4 :: ubound
End Type descriptor_dimension (0:0) :: dim
End Type array_descriptor1

so for me, the best solution currently is to use Intel compiler and debugger in Linux. The debugger is called “idb”. You can download freely from intel website.

So what I did was that I recompiled my code using “ifort” which is the intel fortran compiler, and use the “idb” to debug. To run it type:

$ /opt/intel/Compiler/11.1/038/bin/ia32/idb

the location will depend on where you install the compiler and the debugger.

To start debug, click “File->Load executable”, and choose the executable which has been compiled using Ifort. To view the source as well, click “View->Source Files”.

You can set a break point, etc, in the code, and to view an array simply type:

(idb) p myarray

or you can also access the element

(idb) p myarray(3)

Below is the snapshot of the debugger. I used Kubuntu 8.04.

idb

Advertisements

Another useful debugging command in Octave

In a previous post, I shared some Octave functions to do debugging.  Somehow I got problem in using dbstop function in my main script. It kept on telling me it can’t find the function. However, I found another useful way to stop the program and enter into debug mode. It’s to use the “keyboard” function.

From GNU Octave manual it says

Built-in Function: keyboard (prompt)
This function is normally used for simple debugging. When the keyboard function is executed, Octave prints a prompt and waits for user input. The input strings are then evaluated and the results are printed. This makes it possible to examine the values of variables within a function, and to assign new values to variables. No value is returned from the keyboard function, and it continues to prompt for input until the user types ‘quit’, or ‘exit’.

If keyboard is invoked without any arguments, a default prompt of ‘debug> ’ is used.

so what you need to do is just to insert “keyboard” into the line where you want to stop. That easy!

note: I somehow got strange behaviour if I rerun the script again, it gives me

debug> “error: input: reading user-input failed!”

Debugging in Octave

One of the most useful command to debug your code in octave is to enable these two parameters in the beginning of your code:

debug_on_warning(1);
debug_on_error(1);

this is to enter debug mode in Octave whenever a warning or error occurs.

To add a breakpoint, use
dbstop('yourfunctionname',1)
where 1 is the line number where you want to stop.
To go to next line, type
dbnext
To step into type
dbstep
To continue running, type
dbcont

to see the variable value, just type the name of the variable.