Editing Debugging Octave
Jump to navigation
Jump to search
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
= Preliminaries = | = Preliminaries = | ||
Since compilation of all the source from scratch can take long it is good to have a source folder where most of the source has been compiled. To do this, you can create a parallel build: | Since compilation of all the source from scratch can take long it is good to have a source folder where most of the source has been compiled. To do this, you can create a parallel build: | ||
Line 10: | Line 9: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
This will create a new build of Octave in a different directory without | This will create a new build of Octave in a different directory without optimisations (no -O2 gcc parameter) and with debug symbols compiled in. This build is useful for debugging Octave itself. | ||
= | = Debugging Source files without GUI= | ||
Ubuntu introduced a patch to disallow ptracing of non-child processes by non-root users - ie. only a process which is a parent of another process can ptrace it for normal users - whilst root can still ptrace every process. | |||
That's why gdb won't be able to link to the octave process if you start gdb from within an Octave session using the command | |||
<syntaxhighlight lang="bash"> | |||
octave> system (sprintf ("gnome-terminal --command 'gdb -p %d'", getpid ()), "async"); | |||
</syntaxhighlight> | |||
You can temporarily disable this restriction by doing: | You can temporarily disable this restriction by doing: | ||
Line 31: | 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 | 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"> | <syntaxhighlight lang="bash"> | ||
octave> system (sprintf ("gnome-terminal --command 'sudo gdb -p %d'", getpid ()), "async"); | |||
</syntaxhighlight> | |||
Instead you can debug octave without gui using: | |||
<syntaxhighlight lang="bash"> | |||
./run-octave -g --no-gui | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 50: | Line 50: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
start now the GNU debugger with octave by | start now the GNU debugger with octave. On most Unixy systems, you can start gdb from within an Octave session by evaluating a command like | ||
<syntaxhighlight lang="bash"> | |||
octave> system (sprintf ("gnome-terminal --command 'gdb -p %d'", getpid ()), "async"); | |||
</syntaxhighlight> | |||
at the Octave prompt. This command will open a terminal window running gdb attached to the Octave process. At this point, Octave will be stopped. | |||
Now you can set a breakpoint in the line of interest of your oct-file in gdb prompt: | Now you can set a breakpoint in the line of interest of your oct-file in gdb prompt: | ||
Line 65: | Line 71: | ||
the debugger will stop on the above defined line and you can start debugging according to the manual of GNU debugger. | the debugger will stop on the above defined line and you can start debugging according to the manual of GNU debugger. | ||
= Tools for debugging = | |||
There are different tools for debugging. This article concentrates on describing how to use <code>gdb</code>. | |||
If you run Octave from a build tree, execute <code>./run-octave -g</code> to start a gdb session that is prepared to run Octave (with the necessary environment correctly set up). | |||
Alternatively, you can attach <code>gdb</code> to a running Octave. For this, execute <code>gdb</code> in a shell. (On Windows, use the msys2 shell that can be started with the file <code>cmdshell.bat</code> in Octave's installation folder). Find the PID of the running Octave process and attach to it from the <code>(gdb)</code> prompt with <code>attach *PID*</code> (replace <code>*PID*</code> with the actual PID). | |||
Yet another possibility is to start <code>gdb</code> using the <code>system</code> command at the Octave prompt. Depending on the used Linux shell, the command might look similar to: | |||
system (sprintf ("gnome-terminal -- gdb -p %d", getpid ()), false, "async"); | |||
On Windows, you can issue: | |||
system (sprintf ("start gdb -p %d", getpid ())); | |||
Independent of how <code>gdb</code> was started and Octave was attached to it, it is now possible to issue gdb commands on the <code>(gdb)</code> prompt. See e.g. the [https://sourceware.org/gdb/download/onlinedocs/gdb/index.html gdb documentation]. To return to Octave while gdb is still attached to it, execute <code>continue</code> (or <code>c</code>) at the <code>(gdb)</code> prompt. | |||
== Producing a stack trace == | == Producing a stack trace == | ||
Line 71: | Line 91: | ||
Attach <code>gdb</code> to Octave as described before and return execution to Octave. Then, execute whatever commands are necessary to cause Octave to crash. At that point, you will be back in the gdb session. Type <code>where</code> or <code>bt</code> at the gdb prompt to obtain a stack trace. | Attach <code>gdb</code> to Octave as described before and return execution to Octave. Then, execute whatever commands are necessary to cause Octave to crash. At that point, you will be back in the gdb session. Type <code>where</code> or <code>bt</code> at the gdb prompt to obtain a stack trace. | ||
You could also 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 might be stored somewhere in the directory tree. You should find them, view them and inspect them. | You could also 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 might be stored somewhere in the directory tree. You should find them, view them and inspect them. | ||
=== Where are core dumps stored? === | === Where are core dumps stored? === | ||
It differs on each system. First you should see how core dumps are handled on your system. To do so, type this in a shell terminal: | It differs on each system. First you should see how core dumps are handled on your system. To do so, type this in a shell terminal: | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
Line 85: | Line 102: | ||
=== How to view a core dump? === | === 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: | 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"> | <syntaxhighlight lang="bash"> | ||
Line 105: | Line 121: | ||
=== Helpful gdb commands === | === Helpful gdb 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"> | <syntaxhighlight lang="bash"> | ||
(gdb) print *(my_octave_value.rep) | (gdb) print *(my_octave_value.rep) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== ddd == | == ddd == | ||
[http://www.gnu.org/software/ddd gui for gdb] | |||
[http://www.gnu.org/software/ddd | |||
[[Category:Development]] | [[Category:Development]] |