In software development, a breakpoint is an intentional stopping or pausing place in a program, put in place for debugging purposes.
More generally, a breakpoint is a means of acquiring knowledge about a program during its execution. During the interruption, the programmer inspects the test environment (general purpose registers, memory, logs, files, etc.) to find out whether the program is functioning as expected. In practice, a breakpoint consists of one or more conditions that determine when a program's execution should be interrupted.
-Wikipedia
In MATLAB, when execution pauses at a breakpoint, variables existing in the current workspace (a.k.a. scope) or any of the calling workspaces, can be inspected (and usually also modified).
MATLAB allow users to place two types of breakpoints in .m
files:
true
.Both types of breakpoints can be created in several ways:
Using the MATLAB Editor GUI, by right clicking the horizontal line next to the line number.
Using the dbstop
command:
% Create an unrestricted breakpoint:
dbstop in file at location
% Create a conditional breakpoint:
dbstop in file at location if expression
% Examples and special cases:
dbstop in fit at 99 % Standard unrestricted breakpoint.
dbstop in fit at 99 if nargin==3 % Standard conditional breakpoint.
dbstop if error % This special type of breakpoint is not limited to a specific file, and
% will trigger *whenever* an error is encountered in "debuggable" code.
dbstop in file % This will create an unrestricted breakpoint on the first executable line
% of "file".
dbstop if naninf % This special breakpoint will trigger whenever a computation result
% contains either a NaN (indicates a division by 0) or an Inf
Using keyboard shortcuts: the default key for creating a standard breakpoint on Windows is F12; the default key for conditional breakpoints is unset.
Disable a breakpoint to temporarily ignore it: disabled breakpoints do not pause execution. Disabling a breakpoint can be done in several ways:
All breakpoints remain in a file until removed, either manually or automatically. Breakpoints are cleared automatically when ending the MATLAB session (i.e. terminating the program). Clearing breakpoints manually is done in one of the following ways:
Using the dbclear
command:
dbclear all
dbclear in file
dbclear in file at location
dbclear if condition
Left clicking a standard breakpoint icon, or a disabled conditional breakpoint icon.
Right clicking on any breakpoint > Clear Breakpoint.
In the Editor tab > Breakpoints > Clear All.
In pre-R2015b versions of MATLAB, using the command clear
.
When execution is paused at a breakpoint, there are two ways to continue executing the program:
Execute the current line and pause again before the next line.
F101 in the Editor, dbstep
in the Command Window, "Step" in Ribbon > Editor > DEBUG.
Execute until the next breakpoint (if there are no more breakpoints, the execution proceeds until the end of the program).
F121 in the Editor, dbcont
in the Command Window, "Continue" in Ribbon > Editor > DEBUG.
1 - default on Windows.
In order to debug Java classes that are called during MATLAB execution, it is necessary to perform two steps:
When MATLAB is started in JVM debugging mode, the following message appears in the command window:
JVM is being started with debugging enabled.
Use "jdb -connect com.sun.jdi.SocketAttach:port=4444" to attach debugger.
Create a shortcut to the MATLAB executable (matlab.exe
) and add the -jdb
flag at the end as shown below:
When running MATLAB using this shortcut JVM debugging will be enabled.
Alternatively the java.opts
file can be created/updated. This file is stored in "matlab-root\bin\arch", where "matlab-root" is the MATLAB installation directoy and "arch" is the architecture (e.g. "win32").
The following should be added in the file:
-Xdebug
-Xrunjdwp:transport=dt_socket,address=1044,server=y,suspend=n
Attaching this debugger requires the creation of a "remote debugging" configuration with the port exposed by MATLAB:
Then the debugger is started:
If everything is working as expected, the following message will appear in the console:
Parameter | Details |
---|---|
file | Name of .m file (without extension), e.g. fit . This parameter is (Required) unless setting special conditional breakpoint types such as dbstop if error or dbstop if naninf . |
location | Line number where the breakpoint should be placed. If the specified line does not contain runnable code, the breakpoint will be placed on the first valid line after the specified one. |
expression | Any expression or combination thereof that evaluates to a boolean value. Examples: ind == 1 , nargin < 4 && isdir('Q:\') . |