Debug VLfeat’s Mex files in Visual Studio Community 2013 edition and Matlab release 2013a
It is highly recommended to go through this tutorial once. If you don’t know much about Mex files, please refer to this tutorial once. However it is not necessary. You’d be able to follow this entire tutorial without even knowing much about mex files. But it’d seem less magical if you refer to that link. 🙂
- Download Vlfeat.
- Inside downloaded folder, navigate to vl_compile.m inside the toolbox folder.
- In vl_compile.m find this snippet:
cmd = {['-I' toolboxDir], ...
['-I' vlDir], ...
'-o', ...
'-outdir', mexwDir, ...
filePath } ;
Replace -o flag with -g flag: [no optimization but debugging instead] i.e.
cmd = {['-I' toolboxDir], ...
['-I' vlDir], ...
'-g', ...
'-outdir', mexwDir, ...
filePath } ;
-
Compile Vlfeat source using vl_compile command. i.e In Matlab command window, navigate to folder which contains vl_compile.m and run vl_compile.
-
It takes a while to compile all the CMEX source files. Meanwhile you’d find a brand new folder named mex inside the toolbox folder. This mex folder contains all the mex binaries which are needed to use Vlfeat’s Matlab interface. Since I am using Windows x64 platform, folder named mexw64 is the one I should be looking into.
Note: You might not be able to run vlfeat Matlab commands even after compiling. You might run into some errors. I’ve made a list of those errors and their solutions which worked in my case.
-
Inside mexw64 (Choose folder according to the platform you’re working on), you’d find pdb [Program Debug database] files alongside mex binaries. These are the files which contain the debug information and hence used by Visual Studio later while debugging.
Note: When you don’t use -g flag, no pdb files are formed. -
Now you’re all set to debug.
Start Visual Studio (VS) [Don’t terminate your Matlab session]. In VS, navigate to tools, select Attach to Process and select MATLAB from the list.
-
Open file that you wish to debug in VS. In my case it was vl_siftdescriptor.c. So I navigated to sift folder inside toolbox folder and opened vl_siftdescriptor.c
-
Set breakpoints on desired locations.
-
From Matlab command window run vl_siftdescriptor with proper arguments. You’d be navigated to VS when the first debugger hits first breakpoint.
Just for the sake of this tutorial, I am pasting the exacts commands which I used to run vl_siftdescriptor in Matlab:
I1 = im2single(imread('lena.png'));
[F,D]=vl_sift(I1);
I_ = vl_imsmooth(I1, sqrt(F(3)^2 - 0.5^2)) ;
[Ix, Iy] = vl_grad(I_) ;
mod = sqrt(Ix.^2 + Iy.^2) ;
ang = atan2(Iy,Ix) ;
grd = shiftdim(cat(3,mod,ang),2) ;
grd = single(grd) ;
vl_siftdescriptor(grd, F);
Enjoy!
P.S: Just in case if you still can’t debug in VS.
In my case, this link helped a lot!