breakpoint_command

版本 1.0.0 (2.8 KB) 作者: Benjamin Davis
Automatically execute commands on breakpoint stop as in gdb, pdb, and other debuggers, including dbstep and dbcont.
7.0 次下载
更新时间 2022/5/17

查看许可证

Simulates the "commands on breakpoint" functionality that is present in many debuggers, e.g.
but is not officially present in MATLAB debugger.
This is done by leveraging conditional breakpoint side effects and the ability to cue commands in the console with com.mathworks.mlservices.MLExecuteServices.consoleEval as described in [1].
Other tools such as Darin_dbtools make use of conditional breakpoint side effects to print variables and even modify values. However, this technique is fundamentally limited due to the fact that actual debugger commands (such as dbstep) cannot be run in this mode.
Example use:
Consider f.m:
function [x,y] = f(a)
x = a + 1;
y = a + 2;
It is desired to print the function output 'y' at line 3 before returning (without modifying the code using disp statements).
This could be done with the Darin_dbtools dbdisp as a breakpoint evaluation side effect if we could only attach the breakpoint to a line after y has been assigned. (Note this function has no end statement).
Instead, place conditional breakpoint at line 3 with condition:
breakpoint_command({'dbstep', 'disp(y)'})
Now, run:
>> [x,y] = f(1)
3 y = a + 2;
End of function f.
3
x =
2
y =
3
>>
Despite the breakpoint being at the start of line 3, the dbstep command moves the debugger to "End of function f" (for which a breakpoint cannot be placed) where the value of y can be printed.
The condition can also be given using strings rather than chars.
breakpoint_command(["dbstep", "disp(y)"])
Single commands do not have to be placed in an array.
breakpoint_command('disp(x)')
or
breakpoint_command("disp(x)")
By passing optional second argument as false, the debugger will not be made to continue after the commands and interactive debugging can proceed normally.
breakpoint_command("disp(x)", false)
This can even be used to end the run completely with dbquit.
breakpoint_command("dbquit", false)
In this case, the false is necessary because if a dbcont is attempted after dbquit, this will result in an error.
In short, this function allows you to script any MATLAB debugger behavior that could occur in an interactive session. This should enable powerful workflows including runtime instrumentation of code and swapping of function implementations before/after call (using path modifications). If you find yourself using a complex workflow (such as runtime injected plotting), place it in a script which you then call with this command.
The function has been developed and tested in R2020a. Other releases will probably work too (even old ones without string type support if you use char only).
Since the function makes use of undocumented feature com.mathworks.mlservices.MLExecuteServices.consoleEval, it is not guaranteed to work in a future version.
References:
[1] Altman, Yair M. Undocumented secrets of MATLAB-Java programming. CRC Press, 2011.

引用格式

Benjamin Davis (2024). breakpoint_command (https://www.mathworks.com/matlabcentral/fileexchange/111715-breakpoint_command), MATLAB Central File Exchange. 检索时间: .

MATLAB 版本兼容性
创建方式 R2020a
兼容任何版本
平台兼容性
Windows macOS Linux
致谢

参考作品: Darin_dbtools 2015-01-05.zip

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
版本 已发布 发行说明
1.0.0