Debugging Octave: Difference between revisions

Jump to navigation Jump to search
m
Minor fixes.
No edit summary
m (Minor fixes.)
(One intermediate revision by one other user not shown)
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 9: Line 10:
</syntaxhighlight>
</syntaxhighlight>


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.
This will create a new build of Octave in a different directory without optimizations (no -O2 gcc parameter) and with debug symbols compiled in. This build is useful for debugging Octave itself.


= Tools for debugging =
= Tools for debugging =
There are different tools for debugging. This article concentrates on describing how to use <code>gdb</code>.
There are different tools for debugging. This article concentrates on describing how to use <code>gdb</code>.


Line 17: Line 19:


Alternatively, you can attach a debugger to a running Octave session.  Current development versions of Octave include the command <code>__debug_octave__</code> to manage the details.  Executing this command at the Octave prompt should open a separate window for a debugger session attached to the current Octave process.  On Linux systems, the default terminal window is <code>gnome-terminal</code>.  On MacOS systems, the default debugger is <code>lldb</code>.
Alternatively, you can attach a debugger to a running Octave session.  Current development versions of Octave include the command <code>__debug_octave__</code> to manage the details.  Executing this command at the Octave prompt should open a separate window for a debugger session attached to the current Octave process.  On Linux systems, the default terminal window is <code>gnome-terminal</code>.  On MacOS systems, the default debugger is <code>lldb</code>.
For some kinds of errors on some OS, the last approach might not be useful. The OS might kill the shell that runs gdb as soon as the spawning process (i.e. Octave) crashes. In that case, you can attach to Octave from an "independent" shell. Execute <code>getpid ()</code> in Octave and take note of the displayed *PID*. Open a shell and execute <code>gdb -p *PID*</code> (replace <code>*PID*</code> with the actual PID). On Windows, use the msys2 shell that can be started with the file <code>cmdshell.bat</code> in Octave's installation folder.


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


*NOTE*:  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.
*NOTE:  Ubuntu introduced a patch to disallow ptracing of non-child processes by non-root users - i.e. 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 <code>__debug_octave__</code> command  
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 <code>__debug_octave__</code> command.


You can temporarily disable this restriction by doing:
You can temporarily disable this restriction by doing:
Line 27: Line 31:
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 or if you have admin right you can simply do:
and then reopen <code>gdb</code> using the command mentioned above from within an Octave session or if you have admin rights you can simply do:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
__debug_octave__ ("gnome-terminal --command 'sudo gdb -p %d'")
__debug_octave__ ("gnome-terminal --command 'sudo gdb -p %d'")
Line 73: Line 77:


=== 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 80: Line 85:


=== 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 99: Line 105:


=== Helpful gdb commands ===
=== Helpful gdb commands ===
[http://www.gnu.org/software/gdb/documentation gdb documentation]
[http://www.gnu.org/software/gdb/documentation gdb documentation]


Line 118: Line 125:


== ddd ==
== ddd ==
[http://www.gnu.org/software/ddd gui for gdb]
 
[http://www.gnu.org/software/ddd GUI for gdb]


[[Category:Development]]
[[Category:Development]]

Navigation menu