可以停止并继续从调试器执行吗? [英] Stop and continue execution from debugger possible?

查看:21
本文介绍了可以停止并继续从调试器执行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以像 ctrl+c 那样从调试器中停止执行 matlab 程序,但随后能够继续执行(就像在 c# 中一样)?

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#)?

如果没有,除了尝试在 matlab 代码中预先设置断点或 dbstop 语句之外,还有什么更好的方法可以解决这个问题?

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 命令(或(条件)断点).缺点是有时我不想停止模拟几个小时,有时只想在几秒钟后停止(我不一定事先知道),这对这种方法不起作用:如果我将中断条件设置为每 5 分钟中断一次,我将无法在没有交互的情况下让 matlab 运行数小时.如果我将条件设置得更高,我必须等待太长时间才能达到条件.

  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.

包含每隔几秒/分钟保存工作区的代码,并将工作区导入到第二个 matlab 实例中.缺点是这是一个巨大的麻烦,也不一定允许我使用保存的工作区的状态恢复模拟,然后单步执行代码进行几次迭代.

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!

编辑:我想我要做的是编写简单的 matlab 函数,该函数在每次迭代时检查环境变量或磁盘上的文件,如果我在此文件中设置了标志,则调用 dbstop 或环境通过这种方式,我可以通过编辑文件来控制何时(以及如果需要的话)断点从 matlab 外部命中.凌乱,但应该工作.

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.

推荐答案

这不一定是最好的方法,但您可以模拟基于文件的信号/中断框架.这可以通过在长模拟循环中每隔一段时间检查是否存在特定文件来完成.如果是,则使用 keyboard 命令进入交互模式.

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.

大致情况:

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.
', toc)
        keyboard
        tic
    end

    %# ... long calculations ...    

    i = i + 1;
end

您将像往常一样运行模拟.当你想单步执行代码时,只需创建一个文件 debug.txt(即手动),执行将停止并得到提示:

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

然后您可以像往常一样检查变量...要继续,只需运行 return(不要忘记暂时重命名或删除文件).为了退出,使用 dbquit

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

我突然想到,一个更简单的解决方案是使用虚拟图形作为标志,而不是检查文件(只要图形处于打开状态,就继续运行).

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

这篇关于可以停止并继续从调试器执行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆