How to debug MEX file on Mac Platforms?

4 views (last 30 days)

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 3 Mar 2021
Edited: MathWorks Support Team on 3 Mar 2021
Debug on Mac Platforms
Using Xcode
This example shows how to debug the MEX file, yprime.c, using Xcode.
Copy
the
Source
MEX
File
The yprime.c source code is in the 'matlabroot' folder. In MATLAB
®
, copy the file to a local, writable folder, for example /Users/Shared/work, and compile it (the -g option adds debugging symbols):
>> workdir = fullfile('/','Users','Shared','work')>> mkdir(workdir)>> copyfile(fullfile(matlabroot,'extern','examples','mex','yprime.c'), workdir)>> cd(workdir)
Compile the Source MEX File
Compile the source MEX file with the -g option, which adds debugging symbols.
mex -g yprime.c
MATLAB creates the binary MEX file yprime.mexmaci64.
Create an Empty Xcode Workspace for Debugging
* In Xcode, select File > New > Workspace.
* In the file selection dialog, set the Workspace name in the “Save As:” field to debug_yprime.
* Select the folder in which you would like to store the workspace, for example, in /Users/Shared/work. You can do this by navigating to the folder, or press the "Command+Shift+G" keyboard shortcut to toggle the Go to the folder: menu and type the full path /Users/Shared/work. Then click “Save”. 
Add yprime Files to the Xcode Workspace
* Add the yprime.c file to the workspace by dragging it from the /Users/Shared/work folder in the Finder into the navigator column on the left side of the Xcode workspace window.
* Make sure to uncheck "Destination" option, Copy items into destination group's folder (if needed). Clearing this option enables breakpoints to be added to the file that MATLAB runs.
* Click "Finish" to add the file.
Create a Scheme
* Select Product > Scheme > New Scheme.
* Set "Name" to debug.
* Set "Target" to None.
* Press OK. The scheme editing dialog box opens.
* Set the Run > Info > Executable option to "Other..." and a file selection dialog will appear. Press the "Command+Shift+G" keyboard shortcut to toggle the "Go to the folder:" menu. Specify the full path to the    MATLAB_maci64 executable inside the MATLAB application bundle. An example of a full path is /Applications/MATLAB_R2016b.app/Contents/MacOS/MATLAB_maci64.
* Select "Wait for executable to be launched."
* Click "Close".
Add a Symbolic Breakpoint
* Select Debug > Breakpoints > Create Symbolic Breakpoint.
* Set "Symbol" to NSApplicationMain.
* Click "Add action" to add the following debugger command (if the breakpoint editor pane disappears, right-click on the new breakpoint and select Edit Breakpoint… to get back to it):
process handle -p true -n false -s false SIGSEGV SIGBUS
* Check "Automatically continue after evaluating."
Set Breakpoints in your MEX file
* Select View > Navigators > Show Project Navigator
* Click on yprime.c in the navigator column
* Click the gutter next to the line where you want execution to pause, for example, at the first line in mexFunction()
For more information, refer to the Xcode documentation.
Start the Xcode Debugger
* In Xcode, select Product > Run (or just click the “Run” button with the triangle icon near the top-left corner of the workspace window) to start the debugger.* Wait for Xcode to display the message "Waiting for MATLAB to launch" at the top of the Workspace window. This action might take some seconds, especially the first time you use this procedure.
Launch MATLAB
* Start the MATLAB executable from the Mac Terminal prompt (see https://www.mathworks.com/help/matlab/matlab_env/start-matlab-on-macintosh-platforms.html#buf5vqe-1) or from the Finder, or if MATLAB is already running, right-click on the MATLAB icon in the Dock and select “Open Additional Instance of MATLAB”.
* Xcode will display the message “Running MATLAB : debug”.
Run the Binary MEX File in MATLAB
In this new instance of MATLAB, change the current folder to the folder with the yprime files and run the binary MEX file:
Run the binary MEX file.
>> workdir = fullfile('/','Users','Shared','work')>> cd(workdir)>> yprime(1,1:4
The Xcode debugger halts in yprime.c at the first breakpoint.
At this point you can step through your code, examine variables, etc., but for this exercise just select "Continue" from the "Debug" menu. The execution of yprime finishes and MATLAB displays:
Press Continue. MATLAB displays:
ans =
2.0000 8.9685 4.0000 1.0947
As long as this instance of MATLAB continues running, you can execute your MEX file repeatedly and Xcode will continue to halt at the breakpoints you set.

More Answers (0)

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!