Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop and continue execution from debugger possible?

Is there any way to stop the execution of a matlab program from the debugger like ctrl+c does, but then being able to continue execution (like you can in say c#)?

If not, is there any better way to workaround this other than trying to pre-emptively set break points or dbstop statements in your matlab code?

I would like to be able to interrupt a long running simulation to look at the current state and then continue the simulation.

The two options I'm currently using/considering are

  1. dbstop commands (or (conditional) breakpoints) in the code. Drawback is that sometimes I don't want to stop the simulation for a few hours, sometimes want to stop after only a few seconds (and I don't necessarily know that in advance) and this doesn't work well with this approach: If I set the break condition to break every 5 minutes, I can't leave matlab running for hours without interaction. If I set the condition to higher, I have to wait too long for the condition to hit.

  2. include code to save the workspace every few seconds/minutes and import the workspace into a second matlab instance. Drawback is that this is a huge hassle and also doesn't necessarily allows me to resume the simulation with the state of the saved workspace then step through the code for a few iterations.

I'm hoping there is a better solution than either of the 2. Thanks for any advice!

Edit: I think what I'm going to do is write simple matlab function that checks an environment variable or a file on disk every iteration and calls dbstop if I set a flag in this file or env. This way I can control when (and if needed which of several) the breakpoint hits from outside matlab by editing the file. Messy, but should work.

like image 585
Ben Schwehn Avatar asked Jul 17 '10 17:07

Ben Schwehn


1 Answers

This is not necessarily the best way, but you could simulate a file-based signal/interrupt framework. It could be done by checking every once in a while inside the long simulation loop for the existence of a specific file. If it does, you enter interactive mode using the keyboard command.

Something along the lines:

CHECK_EVERY = 10;    %# like a polling rate

tic
i = 1;               %# loop counter
while true           %# long running loop
    if rem(i,CHECK_EVERY) == 0 && exist('debug.txt','file')
        fprintf('%f seconds since last time.\n', toc)
        keyboard
        tic
    end

    %# ... long calculations ...    

    i = i + 1;
end

You would run your simulation as usual. When you would like to step in the code, simply create a file debug.txt (manually that is), and the execution will halt and you get the prompt:

2.803095 seconds since last time.
K>> 

You could then inspect your variables as usual... To continue, simply run return (dont forget to temporarily rename or remove the file). In order to exit, use dbquit


EDIT: Just occurred to me, instead of checking for files, an easier solution would be to use a dummy figure as the flag (as long as the figure is open, keep running).

hFig = figure; drawnow
while true
    if ~ishandle(hFig)
        keyboard
        hFig = figure; drawnow
    end

    %# ...
    pause(0.5)
end
like image 170
Amro Avatar answered Nov 14 '22 11:11

Amro