Debugging Matlab-Mex files with Valgrind and DDD

Mex files from Matlab debug.




In general: Compile mex files with -g -DDEBUG option.

DDD

Start matlab:
matlab -Dddd

inside DDD:

run -nojvm -nosplash -r "yourMatlabFunc(yourParams)"

Using Valgrind

matlab -nojvm -nosplash -r "yourMatlabFunc(yourParams)" -D"valgrind --error-limit=no --tool=memcheck -v --log-file=valMatlabLog"

Additional useful valgrind option: --leak-check=yes

Useful Macros

C++ output in matlab (useful macros):

/* macros for debugging */
#ifdef DEBUG

#include "mex.h"
#define MPRINT_MSG(_msg)  mexPrintf("[%s|%s, %3d]: %s\n", __FILE__, __func__, __LINE__, _msg);
#define MPRINT_INT(_val)  mexPrintf("[%s|%s, %3d]: %s = %d\n", __FILE__, __func__, __LINE__, #_val, _val);
#define MPRINT_DBL(_val)  mexPrintf("[%s|%s, %3d]: %s = %f\n", __FILE__, __func__, __LINE__, #_val, _val);
#define MPRINT_PTR(_val)  mexPrintf("[%s|%s, %3d]: %s = %p\n", __FILE__, __func__, __LINE__, #_val, _val);

#else       /* #ifdef DEBUG */

/* do not print messages and values */
#define MPRINT_MSG(_msg)
#define MPRINT_INT(_val)
#define MPRINT_DBL(_val)
#define MPRINT_PTR(_val)

/* assert needs that when no debugging is desired and therefore, assertion is ignored */
#ifndef NDEBUG
#define NDEBUG 1
#endif

#endif       /* #ifdef DEBUG */
Author: Christoph Hermes, published: 2009-02-24 19:19:15