Debug on Linux Platforms
The GNU® Debugger gdb, available on Linux® systems, provides complete source code debugging, including the ability to set breakpoints, examine variables, and step through the source code line-by-line.
In this procedure, the MATLAB® command prompt >> is shown in front of
MATLAB commands, and linux> represents a Linux prompt; your system might show a different prompt. The debugger prompt is
<gdb>.
This example shows the general steps to debug yprime.c, found in
the MATLAB
fullfile(matlabroot,"extern","examples","mex") folder.
Attach to MATLAB Process with gdb
Start MATLAB.
Compile the source MEX file with the
-goption, which builds the file with debugging symbols included. At the MATLAB prompt, type:>> mex -g yprime.c
Get the MATLAB process ID number. (since R2025a)
>> matlabProcessID
ans = uint64 38932
At the Linux terminal, attach MATLAB to gbd using the process ID number.
linux> gdb -p 38932Start the debugger.
<gdb> continueWhen you want to start debugging MATLAB, interrupt gdb and enter the following commands.
<gdb> Ctrl+C <gdb> handle SIGUSR1 stop printIf the JVM® is loaded, type this command:
<gdb> handle SIGSEGV SIGBUS nostop noprintTo see if Java® is loaded, at the MATLAB prompt, type:
>> jenv
Set a breakpoint at
mexFunction, the entry point to your MEX function.<gdb> break mexFunction <gdb> continueIn MATLAB, run your MEX file.
>> yprime(1,1:4)
At this point you can step through your code.
<gdb> stepTo complete the debugging session, type:
<gdb> continueyprimefinishes and MATLAB displays:ans = 2.0000 8.9685 4.0000 -1.0947
Start MATLAB with gdb
To debug with gdb:
At the Linux prompt, start the gdb debugger using the
matlabfunction-Doption.linux> matlab -DgdbTell gdb to stop for debugging.
<gdb> handle SIGUSR1 stop printStart MATLAB without the desktop.
<gdb> run -nodesktopIf the JVM is loaded, type this command:
<gdb> handle SIGSEGV SIGBUS nostop noprintTo see if Java is loaded, at the MATLAB prompt, type:
>> jenv
Compile the source MEX file with the
-goption, which builds the file with debugging symbols included. For this example, at the MATLAB prompt, type:>> mex -g yprime.c
In MATLAB, enable debugging with the
dbmexfunction and run your binary MEX file.>> dbmex on >> yprime(1,1:4)
You are ready to start debugging.
It is often convenient to set a breakpoint at
mexFunctionso you stop at the beginning of the gateway routine.<gdb> break mexFunction <gdb> continueOnce you hit one of your breakpoints, you can make full use of any commands the debugger provides to examine variables, display memory, or inspect registers.
To proceed from a breakpoint, type:
<gdb> continueAfter stopping at the last breakpoint, type:
<gdb> continueyprimefinishes and MATLAB displays:ans = 2.0000 8.9685 4.0000 -1.0947From the MATLAB prompt you can return control to the debugger by typing:
>> dbmex stopOr, if you are finished running MATLAB, type:
>> quit
When you are finished with the debugger, type:
<gdb> quitYou return to the Linux prompt.
Refer to the documentation provided with your debugger for more information on its use.