MATLAB Answers

0

How to debug MEX-file compiled with MinGW64 and -g flags

How to debug MEX-file compiled with MinGW64 and -g flags

9 Answers

Answer by MathWorks Support Team on 29 Jan 2016
 Accepted Answer

Follow the steps below to debug a MEX-file compiled with MingW64 and the -g flag:
1. Start Eclipse and choose a new workspace.
Download Eclipse from link <https://www.eclipse.org/downloads/packages/eclipse-ide-cc-developers/lunasr2>
2. Use "File" -> "New" -> "Project"... and select "General"->"Project"
3. Choose a name for your project and click "Finish"
4. Use "File"->"New"->"Other"..." and then "General"-> "Untitled Text File" to create a new text file.
Enter the following in the text file 
handle SIGSEGV nostop
Then save the file as "gdbinit" in "mexdebug" folder as shown in the figure.
5. Drag the C/Cpp source code of your MEX-file into the project. When asked to copy or link choose "Link to file" so that the source codes stay in their original locations.
6. Go to "Run" -> "Debug Configurations...". Select "C/C++ Attach to Application" and select "New" button.
7. On the "Debugger" tab enter the location of the file created under 4 as "GDB command file".
 
8. Click "Debug"
9. Select MATLAB.exe from the list.
10. The debugger will suspend MATLAB. Use "Resume" button to continue running MATLAB.
11. Open the C/Cpp code in Eclipse and place a breakpoint.
12. Run the MEX-file in MATLAB
13. When the breakpoint is hit you will stop in the debugger and can for example step through the code and inspect variables.
 

  3 Comments

Is this the new preferred method to debug C code mex'ed to run in Matlab? Other documentation indicates using Microsoft Visual Studio 2012 Professional. However, Microsoft is no longer supporting VS 2012, and hence not selling keys to the professional edition any longer.
This answer, as well as the comments to the other two answers, were super helpful, thank you very much! However, how did everyone deal with the large amount of errors that popped out..
Unfortunately, this is not working for me. I don't see the MATLAB Process in the process list to select from. I use MATLAB R2017a 64bit und Eclipse Oxygen. I wonder if this can be due to 32/64bit mismatches, especially between the GDB and the MATLAB-Process, since I have both MinGW (32bit) and MinGW-w64 installed on my computer. I assume a GDB 64bit is required to attach to a 64 bit Application, but don't know how to tell eclipse which GDB to use. Are there other possible reasons why I can't select MATLAB.exe to attach to, and if not, how can I tell Eclipse to use the MinGW64 GDB? Thank you very much.

Sign in to comment.


Answer by Chuong Nguyen on 10 Jun 2017

Thank you very much, but for my case, when the window "select process" open, I can't see the matlab.exe process in the windows?, can you help me to solve the problem?

  1 Comment

I just tried applying this procedure and ran into two issues. Fortunately, both were small and easily resolved.
1. the version of gdb included with the latest version of TDM-GCC MinGW has a compatibility problem with Eclipse. For more information, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=474311
I was able to solve this problem by using an earlier version. By trial and error, I found that the following version works:
tdm64-gcc-4.7.1-3.exe
(Note, this issue has nothing to do with MATLAB, per se).
2. As noted in the previous message, the task does not come up as MATLAB.com. The correct task is one of the generically named tasks called "Program" (at least in windows 10). But by clicking on the name, you can quickly identify the correct task (see attachment).
With these changes to the procedure, it works great.

Sign in to comment.


Answer by Chuong Nguyen on 10 Jun 2017

and when I detect "Program 15920" is the link to matlab, I press "ok" but again, Eclipse show the error:
Error in final launch sequence Failure to attach to process: MATLAB.exe [15920] Error: Failed to execute MI command: -target-attach 15920 Error message from debugger back end: Can't attach to process.
can you help me to solve this problem

  1 Comment

Maybe, this is because you use not compatable with matlab debugging version of gdb. I've got the same problem and solution was to install minGW64 (https://sourceforge.net/projects/mingw-w64/), version 4.9.1 (it uses GNU gdb (GDB) 7.8.1 ver.), Architecture x86_64, threads win32, exception dwarf, Build version 3. And also 64bit Matlab and eclipse(oxygen, neon, luna)

Sign in to comment.


Answer by Esfandiar Farahvashi on 5 Apr 2018

I have followed the guide and successfully attached eclipse to my MATLAB process. I also linked the Header and CPP files with my project. However, if I insert break points in the cpp file, MATLAB will not pause during the calling to the mex. It simply calls it and continues without any breakpoints beeing triggered. I would highly appreciate any suggestions!

  1 Comment

Sign in to comment.


Answer by Patrick Schmidt on 19 Apr 2018
Edited by Patrick Schmidt on 19 Apr 2018

I've followed the steps in this tutorial, but when I press debug I get an error: "gdborig.exe has stopped working". Does somebody have an idea why this could happen?

  1 Comment

Hi Patrick, I got same error. Issue resolved after using gdb-7.8.1. You can download gdb(gdb32-7.9.1-tdm-1.zip) from https://sourceforge.net/projects/tdm-gcc/files/GDB/

Sign in to comment.


Answer by LodL93
on 8 May 2018

Debugging a mex-file works very very easy with QT Creator instead of Eclipse (what not worked for me). Just select "Attach to running application" as shown in the image, and then select the Matlab Process. You then can set your breakpoints in the source files of your Mex-file, and even dive into linked libraries.
I need to emphasize that QT Creator is a very good IDE for coding C/C++. ;))

  0 Comments

Sign in to comment.


Answer by Jacob Mannhardt on 2 Aug 2018

Thank you for this tutorial. Unfortunately the Attach Process Window doesn't show up when I hit debug. I am absolutely new to Eclipse and debugging. Matlab is open, but I don't think that that's the issue. Any ideas?
Cheers Jacob

  0 Comments

Sign in to comment.


Answer by Till Blaha on 25 Sep 2018

Hey Jacob,
I had the exact same issue and I am, just like you a complete beginner on this subject. It turned out not to be a MATLAB related issue. Any gdb version after 7.8 has some sort of issue with eclipse (or the other way around, I don't know). See: https://bugs.eclipse.org/bugs/show_bug.cgi?id=474311
Unfortunately for us, 7.8 is no longer available in the repos of cygwin (they start at 7.9). Fortunately for us, this guy has found them somewhere, uploaded the 7.8 version and wrote a detailed explanation on how to install it (takes around 20 minutes). After that everything worked like a charm:
https://stackoverflow.com/questions/35826635/clion-gdb-current-version-is-gnu-gdb-gdb-cygwin-7-10-1-1-7-10-1-supporte
Greetings, Till

  0 Comments

Sign in to comment.


Answer by Adam Gogacz on 3 Dec 2018

I've never used Eclipse for debugging, can someone provide more clarification on going from step 8 to step 9? I was never presented with a "Select Processes" window.
Thanks,
Adam

  0 Comments

Sign in to comment.