3

I am looking for a way to save 'everything' in the matlab session when it is stopped for debugging.

Example

function funmain
a=1;
if a>1
  funsub(1)
end
funsub(2)
end

function funsub(c)
b = c + 1;
funsubsub(c)
end
function funsubsub(c)
c = c + 2; %Line with breakpoint
end

When I finally reach the line with the breakpoint, I can easily navigate all workspaces and see where all function calls are made.

The question

How can I preserve this situation?


When debugging nested programs that take a long time to run, I often find myself waiting for a long time to reach a breakpoint. And sometimes I just have to close matlab, or want to try some stuff and later return to this point, so therefore finding a way to store this state would be quite desirable. I work in Windows Server 2008, but would prefer a platform independant solution that does not require installation of any software.

What have I tried

1. Saving all variables in the workspace: This works sometimes, but often I will also need to navigate other workspaces

2. Saving all variables in the calling workspace: This is already better as I can run the lowest function again, but may still be insufficient. Doing this for all nested workspaces is not very convenient, and navigating the saved workspaces may be even worse.

Besides the mentioned inconveniences, this also doesn't allow me to see the exact route via which the breakpoint is reached. Therefore I hope there is a better solution!


Code structure example

The code looks a bit like this

function fmain
fsub1()
fsub2()
fsub3()
end

function fsub1
fsubsub11
fsubsub12
...
fsubsub19
end

function fsub2
fsubsub21
fsubsub22
...
fsubsub29
end

function fsub3
fsubsub31
fsubsub32
...
fsubsub39
end

function fsubsub29
fsubsubsub291
fsubsubsub292% The break may occur in here
...
fsubsubsub299

The break can of course occur anywhere, and normally I would be able to navigate the workspace and all those above it.

5
  • @Acorbe I work on windows server 2008, May 2, 2014 at 12:10
  • I see. As per my understanding of your question, you are are willing to have a restart option for debug. A possibility is to dump the whole matlab process on a file to reload it later on. That is in principle possible on linux via crtools. Don't know about windows though.
    – Acorbe
    May 2, 2014 at 12:21
  • 1
    @Acorbe As for windows I believe state saving is not possible - a reliable source in the form of Raymond Chen... So this method may be out of the window... I have read (can't remember where) about possibility of using a VM running another windows instance to state save, but I have not seen evidence of it actually working.
    – RTL
    May 2, 2014 at 13:26
  • Have added the following in the question: I work in Windows Server 2008, but would prefer a platform independant solution that does not require installation of any software. May 2, 2014 at 13:34
  • Seems like you need unit tests. Most common errors do not really depend on the specifics of the input (i.e. data may be bogus, but it will not break on an error anyhow).
    – Bernhard
    May 4, 2017 at 6:07

2 Answers 2

3

Checkpointing

What you're looking to implement is known as checkpointing code. This can be very useful on pieces of code that run for a very long time. Let's take a very simple example:

f=zeros(1e6,1);
for i=1:1e6
   f(i) = g(i) + i*2+5; % //do some stuff with f, not important for this example
end

This would obviously take a while on most machines so it would be a pain if it ran half way, and then you had to restart. So let's add a checkpoint!

f=zeros(1e6,1);
i=1; % //start at 1
% //unless there is a previous checkpoint, in which case skip all those iterations
if exist('checkpoint.mat')==2
  load('checkpoint.mat'); % //this will load f and i
end

while i<1e6+1
   f(i) = g(i) + i*2+5;
   i=i+1;

   if mod(i,1000)==0 % //let's save our state every 1000 iterations
      save('checkpoint.mat','f','i');
   end
end
delete('checkpoint.mat') % //make sure to remove it when we're done!

This allows you to quit your code midway through processing without losing all of that computation time. Deciding when and how often to checkpoint is the balance between performance and lost time!

Sample Code Implementation

Your Sample code would need to be updated as follows:

function fmain
sub1done=false; % //These really wouldn't be necessary if each function returns
sub2done=false; % //something, you could just check if the return exists
sub3done=false;
if exist('checkpoint_main.mat')==2, load('checkpoint_main.mat');end

if ~sub1done
    fprintf('Entering fsub1\n');
    fsub1()
    fprintf('Finished with fsub1\n');
    sub1done=true;
    save('checkpoint_main.mat');
end

if ~sub2done
    fprintf('Entering fsub2\n');
    fsub2()
    fprintf('Finished with fsub2\n');
    sub2done=true;
    save('checkpoint_main.mat');
end

if ~sub3done
    fprintf('Entering fsub3\n');
    fsub3()
    fprintf('Finished with fsub3\n');
    sub3done=true;
    save('checkpoint_main.mat');
end
delete('checkpoint_main.mat');
end



function fsub2
subsub21_done=false;subsub22_done=false;...subsub29_done=false;
if exist('checkpoint_fsub2')==2, load('checkpoint_fsub2');end
if ~subsub21_done
     fprintf('\tEntering fsubsub21\n');
     fsubsub21
     fprintf('\tFinished with fsubsub21\n');
     subsub21_done=true;
     save('checkpoint_fsub2.mat');
end


...
if ~subsub29_done
    fprintf('\tEntering fsubsub29\n');
    fsubsub29
    fprintf('\tFinished with fsubsub29\n');
    subsub29_done=true;
    save('checkpoint_fsub2.mat');
end
delete('checkpoint_fsub2.mat');
end


function fsubsub29
subsubsub291_done=false;...subsubsub299_done=false;
if exist('checkpoint_fsubsub29.mat')==2,load('checkpoint_fsubsub29.mat');end

if ~subsubsub291_done
    fprintf('\t\tEntering fsubsubsub291\n');
    fsubsubsub291
    fprintf('\t\tFinished with fsubsubsub291\n');
    subsubsub291_done=true;
    save('checkpoint_fsubsub29.mat');
end 

if ~subsubsub292_done
    fprintf('\t\tEntering fsubsubsub292\n');
    fsubsubsub292% The break may occur in here
    fprintf('\t\tFinished with fsubsubsub292\n')
    subsubsub292_done=true;
    save(checkpoint_fsubsub29.mat');
end
delete('checkpoint_fsubsub29.mat');
end

So in this structure if you restarted the program after it was killed it would resume back to the last saved checkpoint. So for example if the program died in subsubsub291, the program would skip fsub1 altogether, just loading the result. And then it would skip subsub21 all the way down to subsub29 where it would enter subsub29. Then it would skip subsubsub291 and enter 292 where it left off, having loaded all of the variables in that workspace and in previous workspaces. So if you backed out of 292 into 29 you would have the same workspace as if the code just ran. Note that this will also print a nice tree structure as it enters and exits functions to help debug execution order.

Reference:

https://wiki.hpcc.msu.edu/pages/viewpage.action?pageId=14781653

6
  • I think this is just saving some variables. (Bit like point 1 of what I tried). This may be convenient for running code, but it will not save the debugging state and will not deal with nested functions. -- Thanks for your attention,but I believe it does not answer the right question. May 2, 2014 at 12:37
  • @DennisJaheruddin, this does not really answer your question, but it might be a workaround. Instead of saving the debug state, it makes it faster to recreate that debug state.
    – A. Donda
    May 2, 2014 at 15:10
  • Exactly, I realize this is saving variables but it allows for you to resume where you left off. You could then put a breakpoint after the load statement and you would have resumed your state without waiting for the computations. Why did this get downvoted? If you are unsure of how this solves your specific problem I can provide an example of how this would work while calling functions.
    – Raab70
    May 2, 2014 at 16:12
  • This does not look bad to me as well. It's straightforward to implement, robustness is gained and through a proper saving strategy the faulty point can be reached quickly.
    – Acorbe
    May 2, 2014 at 19:08
  • 1
    @DennisJaheruddin I have added a sample implementation to your code structure. Please also note that you could checkpoint in 292 as described in the top section.
    – Raab70
    May 5, 2014 at 12:33
0

After a bit of googling, I found that using putvar (custom function from here: http://au.mathworks.com/matlabcentral/fileexchange/27106-putvar--uigetvar ) solved this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.