# Debugging
# Working with Breakpoints
# Definition
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
# Breakpoints in MATLAB
# Motivation
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).
# Types of Breakpoints
MATLAB allow users to place two types of breakpoints in .m
files:
- Standard (or "unrestricted") breakpoints (shown in red) - pause execution whenever the marked line is reached.
- "Conditional" breakpoints (shown in yellow) - pause execution whenever the marked line is reached AND the condition defined in the breakpoint is evaluated as
true
.
# Placing Breakpoints
Both types of breakpoints can be created in several ways:
% 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
# Disabling and Re-enabling Breakpoints
Disable a breakpoint to temporarily ignore it: disabled breakpoints do not pause execution. Disabling a breakpoint can be done in several ways:
- Right click on the red/yellow breakpoint circle > Disable Breakpoint.
- Left click on a conditional (yellow) breakpoint.
- In the Editor tab > Breakpoints > Enable\Disable.
# Removing Breakpoints
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:
dbclear all
dbclear in file
dbclear in file at location
dbclear if condition
# Resuming Execution
When execution is paused at a breakpoint, there are two ways to continue executing the program:
1 - default on Windows.
# Debugging Java code invoked by MATLAB
# Overview
In order to debug Java classes that are called during MATLAB execution, it is necessary to perform two steps:
- Run MATLAB in JVM debugging mode.
- Attach a Java debugger to the MATLAB process.
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.
# MATLAB end
# Windows:
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
# Debugger end
# IntelliJ IDEA
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:
# Syntax
- dbstop in file at location if expression
# Parameters
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:\') . |