Debugging Octave: Difference between revisions

Jump to navigation Jump to search
2,451 bytes added ,  1 June 2019
No edit summary
(6 intermediate revisions by 3 users not shown)
Line 5: Line 5:
mkdir dbg-octave
mkdir dbg-octave
cd dbg-octave
cd dbg-octave
/path/to/octave/source/configure FFLAGS=-g CFLAGS=-g CXXFLAGS=-g --enable-bounds-check --prefix=/opt/dbg-octave
/path/to/octave/source/configure FFLAGS=-g CFLAGS=-g CXXFLAGS=-g --enable-address-sanitizer-flags --prefix=/opt/dbg-octave
make # or make -jN where N is the number of CPU cores
make # or make -jN where N is the number of CPU cores
</syntaxhighlight>
</syntaxhighlight>
Line 24: Line 24:
echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
</syntaxhighlight>
</syntaxhighlight>
and then reopen the gdb using the command mentioned above from within an Octave session.
and then reopen the gdb using the command mentioned above from within an Octave session or if you have admin right you can simply do:
<syntaxhighlight lang="bash">
octave> system (sprintf ("gnome-terminal --command 'sudo gdb -p %d'", getpid ()), "async");
</syntaxhighlight>




Line 41: Line 44:
</syntaxhighlight>
</syntaxhighlight>


start now the GNU debugger with octaveOn most Unixy systems, you can start gdb from within an Octave session by evaluating a command like
In next step you will use GNU debugger or gdb. The symbols from your oct-file will only be available to gdb once the oct-file is loaded in OctaveTo do that without executing any functions from the oct-file, you can ask Octave to display the help for the oct-file:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
octave> system (sprintf ("gnome-terminal --command 'gdb -p %d'", getpid ()), "async");
octave> help file
</syntaxhighlight>
</syntaxhighlight>


at the Octave promptThis command will open a terminal window running gdb attached to the Octave process.  At this point, Octave will be stopped.  To tell Octave to continue, type
start now the GNU debugger with octaveOn most Unixy systems, you can start gdb from within an Octave session by evaluating a command like


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
(gdb) continue
octave> system (sprintf ("gnome-terminal --command 'gdb -p %d'", getpid ()), "async");
</syntaxhighlight>
</syntaxhighlight>


at the gdb prompt.  The symbols from your oct-file will only be available to gdb once the oct-file is loaded in Octave.  To do that without executing any functions from the oct-file, you can ask Octave to display the help for the oct-file:
at the Octave prompt.  This command will open a terminal window running gdb attached to the Octave processAt this point, Octave will be stopped. 


<syntaxhighlight lang="bash">
Now you can set a breakpoint in the line of interest of your oct-file in gdb prompt:
octave> help file
</syntaxhighlight>
 
Now halt execution of Octave by typing ctrl+c, you'll see again the gdb prompt. Set now a breakpoint in the line of interest


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 76: Line 75:
== Producing a stack trace ==
== Producing a stack trace ==


Sometimes Octave will crash, meaning, it terminates abruptly and returns control to the operating system shell. In these cases, it is very helpful to produce a stack trace to diagnose the problem. To do so, you need to (re)compile Octave with debugging symbols and run the debugger, as explained above.  Then execute whatever commands are necessary to cause Octave to crash.  At that point, you will be back in a gdb session. Type <code>where</code> at the gdb prompt to obtain a stack trace.
Sometimes Octave will crash, meaning, it terminates abruptly and returns control to the operating system shell. In these cases, it is very helpful to produce a stack trace to diagnose the problem. To do so, you need to (re)compile Octave with debugging symbols , only if you can afford such a huge work, and run the debugger, as explained above.  Then execute whatever commands are necessary to cause Octave to crash.  At that point, you will be back in a gdb session. Type <code>where</code> at the gdb prompt to obtain a stack trace.
 
But if you can't or don't want to recompile octave ,which is totally normal, then you can get some help from your system tools. In most GNU/Linux systems whenever a crash happens in a software, a <i>core dump</i> will be generated. These core dumps are handled by a registered component of the system and finally can be stored somewhere in the directory tree. You should find them, view them and inspect them.
 
=== Where are core dumps stored? ===
It differs on each system. First you should see what does handle core dumps on your system. to do so, type this in a shell terminal:
<syntaxhighlight lang="bash">
$ cat /proc/sys/kernel/core_pattern
</syntaxhighlight>
This may print a file name pattern along with a path, where all core dumps will be saved <b>ONLY</b> if it does not start by a pipe or '|'. If it does, <i>the kernel will treat the rest of the pattern as a command to run.  The core dump will be written to the standard input of that program instead of to a file</i>, and you need to consult that program's help or manual.
 
=== How to view a core dump? ===
To do this you should use gdb. Core dumps are saved under root user, so you may need to change owner of the core dump you are interested in if you are not logged in as root. After that type in the terminal:
<syntaxhighlight lang="bash">
gdb octave -c <Path to core dump>
</syntaxhighlight>
 
Always expect some warnings from gdb in a few first times of doing this. Most likely gdb will tell you that:
 
1. The core dump file is not in the correct format. It is the case if the core dump handler of your system compresses core dumps before storing them, and you need to decompress the core dump first.
 
2. Core dump was generated by a-path-to/octave-gui. Then quit gdb and start it again by:
<syntaxhighlight lang="bash">
gdb octave-gui -c <Path to core dump>
</syntaxhighlight>
 
3. Some debug info are missing. In this case gdb itself will tell you how to install them. Install them and start gdb again.
 
If everything worked fine, you can use 'where' command in gdb prompt to see a full stack trace of the crash.


=== Most used commands ===
=== Most used commands ===
[http://www.gnu.org/software/gdb/documentation gdb documentation]
[http://www.gnu.org/software/gdb/documentation gdb documentation]
For debugging octave_value variables (e.g. <code>my_octave_value</code>) to find out what the variable actually is (instead of just it's base class):
<syntaxhighlight lang="bash">
(gdb) print *(my_octave_value.rep)
</syntaxhighlight>


== ddd ==
== ddd ==

Navigation menu