1

I have custom C bindings called from Matlab and sometimes I get a segmentation fault. How can I identify in my source code what the corresponding statement is producing the SEGFAULT?

My C function is called Pairing in the source file Pairing.c

Stack Trace (from fault):
[  0] 0x00007fff6bc76d00 Pairing.mexa64+00015616
[  1] 0x00007fff6bc74330 Pairing.mexa64+00004912 mexFunction+00001862
[  2] 0x00007fffe2b4f213 MATLAB/R2020a/bin/glnxa64/libmex.so+00582163

The result of nm -a Pairing.mexa64 | grep ' N ' is

0000000000000000 N .debug_abbrev
0000000000000000 N .debug_aranges
0000000000000000 N .debug_info
0000000000000000 N .debug_line
0000000000000000 N .debug_str
kameranis
  • 284
  • 1
  • 13
  • 1
    Did you try building the MEX-file with debug information? Add `-g` to the compilation command. – Cris Luengo Mar 26 '20 at 01:40
  • Yes I have. This stack trace is with building it with `-g`. I have also tried what is suggested here: https://www.mathworks.com/help/matlab/matlab_external/debugging-on-linux-platforms.html. But gdb does not find Pairing in the symbol table. – kameranis Mar 26 '20 at 15:25
  • Are you sure you are running the MEX-file built with `-g` and not some other copy elsewhere on your MATLAB path? GDB should be able to find the your function... Try `nm -a Pairing.mexa64` from the shell, it should list all symbols in the MEX-file. Any symbol with an `N` in front of it will be a debugging symbol, exported functions should have a `T` in front of them. – Cris Luengo Mar 26 '20 at 15:33
  • Also, consider user AddressSanitizer: https://stackoverflow.com/questions/56976550/can-mex-files-be-run-with-fsanitize-address -- it's not as trivial to use for a MEX-file because of MATLAB, just like all other debugging options. But I found it to be the best way to debug when simple `printf` statements are not enough. – Cris Luengo Mar 26 '20 at 15:38
  • I added the results of the command in the question – kameranis Mar 26 '20 at 15:59
  • But look at the symbols with `T` and `t` as well. Or try `grep Pairing` to see if your function is listed in any form. – Cris Luengo Mar 26 '20 at 16:05

2 Answers2

3

here is my trick (works every single time), run this in a terminal window

matlab -nojvm -nosplash -r 'my_script'  -D"valgrind --error-limit=no --tool=memcheck -v --log-file=valgrind.log" 

preferably run this under Linux or Mac, but you can also do this in Windows using cygwin64/msys2. Need to install valgrind before use - once it dumps the log in valgrind.log, open it using a text editor, and you can see all memory errors captured by valgrind.

for CUDA codes, you can also replace the valgrind command and parameters by cuda-memcheck, does something similar, but for the GPU.

make your test script my_script.m very simple, for example, load a .mat file, and then call your mex function immediately to avoid lengthy overhead.

FangQ
  • 1,444
  • 10
  • 18
0

The way I solved it was following these steps

1) Use objdump -d Pairing.mexa64 > Pairing_obj.

2) Translate 00015616 to hex=0x3d00.

3) Find the relevant statement and recognize the produced assembly.

4) Realize this is the first time a certain variable is dereferenced.

I am still looking for some way that this could be done easier.

kameranis
  • 284
  • 1
  • 13