Adding a command to aid in debugging Octave

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Adding a command to aid in debugging Octave

John W. Eaton
Administrator
When trying to debug an already running Octave session I often get the
pid and run "gdb -p PID" in a separate terminal window.  That works for
me on Linux systems.  I've also sometimes used a command like

   system (sprintf ("gnome-terminal -- gdb -p %d", getpid ()),
           false, "async");

at the Octave prompt.

I never expected something like that to work on Windows, but I recently
discovered that gdb can also connect to running processes on Windows.
Am I the only one who didn't know?  Until that discovery, I didn't know
any easy way to debug Octave on Windows.  But it still required opening
a command window and starting gdb.  Then I also found that it is
possible to start gdb in a separate window from the Octave prompt using

   system (sprintf ("start gdb -p %d", getpid ()));

Magic!  (Also somewhat surprising to me, passing the "async" parameter
on Windows seemed to have the opposite effect and Octave did not return
to the command prompt until the gdb process exited, but I'll worry about
that later.)

To make debugging Octave even easier, would it be useful to add
something like the following function?

function __debug_octave__ ()
   [status, ~] = system ("gdb --version");
   if (status != 0)
     error ("__debug_octave__: unable to execute gdb");
   endif
   if (isunix ())
     ## FIXME: Obviously, gnome-terminal is not always available.
     system (sprintf ("gnome-terminal -- gdb -p %d", getpid ()),
             false, "async");
   elseif (ispc ())
     system (sprintf ("start gdb -p %d", getpid ()));
   elseif (ismac ())
      ## FIXME: What works?
   else
      ## FIXME: Is this possible?
   endif
endfunction

What's the right thing for Mac systems?

Unless there is some magic command I don't know about that works on
Unixy systems, we might need to search for known terminal since we can't
expect to always find gnome-terminal.

Comments?  Is there a better way?

jwe


Reply | Threaded
Open this post in threaded view
|

Re: Adding a command to aid in debugging Octave

Carlo de Falco-2
Hi,

> Il giorno 14 mag 2020, alle ore 19:25, John W. Eaton <[hidden email]> ha scritto:
>
> What's the right thing for Mac systems?

this seems to work for me :

system (sprintf ("osascript -e 'tell application \"Terminal\" to do script \"lldb -p %d\"'", getpid ()))

the escaping looks a bit tricky though so it would be nice if someone else could verify it ...

c.



Reply | Threaded
Open this post in threaded view
|

Re: Adding a command to aid in debugging Octave

Andrew Janke-2


On 5/14/20 3:41 PM, Carlo De Falco wrote:

> Hi,
>
>> Il giorno 14 mag 2020, alle ore 19:25, John W. Eaton <[hidden email]> ha scritto:
>>
>> What's the right thing for Mac systems?
>
> this seems to work for me :
>
> system (sprintf ("osascript -e 'tell application \"Terminal\" to do script \"lldb -p %d\"'", getpid ()))
>
> the escaping looks a bit tricky though so it would be nice if someone else could verify it ...
>
> c.

Works for me in Octave 4.4.1 on macOS 10.14.6.

Cheers,
Andrew

Reply | Threaded
Open this post in threaded view
|

Re: Adding a command to aid in debugging Octave

John W. Eaton
Administrator
On 5/19/20 12:47 PM, Andrew Janke wrote:

>
>
> On 5/14/20 3:41 PM, Carlo De Falco wrote:
>> Hi,
>>
>>> Il giorno 14 mag 2020, alle ore 19:25, John W. Eaton <[hidden email]> ha scritto:
>>>
>>> What's the right thing for Mac systems?
>>
>> this seems to work for me :
>>
>> system (sprintf ("osascript -e 'tell application \"Terminal\" to do script \"lldb -p %d\"'", getpid ()))
>>
>> the escaping looks a bit tricky though so it would be nice if someone else could verify it ...
>>
>> c.
>
> Works for me in Octave 4.4.1 on macOS 10.14.6.

Thanks for the info about how to start the debugger on MacOS systems.

I pushed a changeset with a version of the new __debug_octave__ command
to the stable branch and merged with default.

I also updated the information on the wiki about debugging Octave:

   https://wiki.octave.org/Debugging_Octave


jwe


Reply | Threaded
Open this post in threaded view
|

Re: Adding a command to aid in debugging Octave

siko1056
On 7/30/20 11:31 AM, John W. Eaton wrote:

> On 7/29/20 9:56 PM, Kai Torben Ohlhus wrote:
>> On 7/30/20 1:07 AM, John W. Eaton wrote:
>>>
>>> I pushed a changeset with a version of the new __debug_octave__ command
>>> to the stable branch and merged with default.
>>>
>>> I also updated the information on the wiki about debugging Octave:
>>>
>>>    https://wiki.octave.org/Debugging_Octave
>>>
>>>
>>> jwe
>>>
>>
>>
>> Thank you for this handy feature.  Compiling Octave with "-g", as
>> described first in the wiki [1], is not necessary for using
>> `__debug_octave__`, right?
>>
>> In this case, I will reorder the information in the wiki.  As user I
>> would never use this command, as I expect Octave has to be compiled in a
>> special way beforehand.
>>
>> Kai
>>
>> [1] https://wiki.octave.org/Debugging_Octave#Preliminaries
>
> It's not necessary to build with debugging symbols but you will get
> better information from the debugger.  The default flags are "-g -O2"
> which generally works but it can be confusing when stepping through an
> optimized program because the instructions aren't as close to the source
> code as they are when optimization is disabled.  So program execution
> may appear to jump around in unexpected ways and some symbols may be
> inaccessible.  When I need to get reliable info from the debugger, I
> usually compile the C++ parts with -ggdb.
>
> jwe
>

Ups, forgot to add the maintainers list.

Thank you for the clarification.  Then I think a small info will be
sufficient to not discourage users like me from using it ;-)

Kai