A year ago, I have switched to Vim for my C++ development stuff. At the beginning it was hard to manage the new environment but after some uncomfortable weeks I felt deep in love with Vim. Today, I wouldn’t go back and deal with all these stupid cursor operations. But until last night there was a tiny thing that bothered me. No debugging in Vim.
Okay, debugging programs in C++ is a bit different from other programming languages. I would like to say 80% of bug detections could be done by static analysis. The debugger isn’t that important in the most cases. In the last year I used gdb or lldb at the command line and I was pretty satisfied with the results, except with the procedure necessary to set breakpoints. A simple click into the source code would be so much easier. Yesterday, after a long long debugging session, my frustration exploded and I was set to change something. This was harder than expected. So, I decided to write some notes into my fancy and successful blog.
Before we can start I would like to provide some notes about my environment: I’m using MacVim 7.3 on OSX 10.8 and compile all my programs with LLVM/Clang. I have found four opportunities to get debugger integration in Vim: vim-lldb, vimgdb, clewn and pyclewn. vim-lldb makes use of lldb and therefore it was my first choice, but after some tests I realized this is not the right thing for me. I guess lldb is much better without this vim-lldb shebang. vimgdb, clewn and pyclewn are for the gdb. According to the table on the pyclewn website, surprise surprise, pyclewn provides the most functionalities. Hence, I decided to go with pyclewn.
I found out that pyclewn is not compatible with the preinstalled version of gdb on OSX 10.8. We need to install another version of gdb. This can be done with homebrew.
brew install gdb
Now we have two versions of gdb on the system. Apple’s version is located at
/usr/bin/gdb and homebrew’s version at
/usr/local/bin/gdb. To guarantee that the
/usr/local/bin/gdb will be selected by default, we have to change the
sudo vi /etc/paths
Make sure that
/usr/local/bin is above
/usr/bin. In my case I end up with this:
/usr/local/bin /usr/bin /bin /usr/sbin /sbin
gdb needs to be code-signed to have the privilege to attach to other processes. I have found a good how-to here. Thus, quoted from there:
Start Keychain Access application (/Applications/Utilities/Keychain Access.app) Open menu /Keychain Access/Certificate Assistant/Create a Certificate… Choose a name (gdb-cert in the example), set Identity Type to Self Signed Root, set Certificate Type to Code Signing and select the Let me override defaults. Click several times on Continue until you get to the Specify a Location For The Certificate screen, then set Keychain to System. [...] Finally, using the contextual menu for the certificate, select Get Info, open the Trust item, and set Code Signing to Always Trust. You must quit Keychain Access application in order to use the certificate [...].
Now we can sign the new gdb executable:
codesign -s gdb-cert /usr/local/gdb
A reboot was necessary before the code signing worked for me.
Vim starts pyclewn as a standalone program connected to Vim with a netbeans socket. Therefore our Vim version needs the netbeans integration feature. Another dependence is autocmd. Your Vim versions must support theses features but that should already be the case if you are using the preinstalled Vim version on OSX 10.8 and MacVim 7.3 like me. To be sure you should find
+netbeans_intg in the version info.
/Applications/MacVim-snapshot-65/mvim --version | grep "+autocmd\|+netbeans_intg" /usr/bin/vim --version | grep "+autocmd\|+netbeans_intg"
Congratulations! Now we can download pyclewn and install it how it is described on the website by the following commands. I’m not sure but I believe OSX has Python 2.7 preinstalled. Hence the *.py2.tar.gz should be okay.
tar xzf pyclewn-1.11.py2.tar.gz cd pyclewn-1.11.py2 export EDITOR=/usr/bin/vim sudo vimdir=$HOME/.vim python setup.py install --force
Finished! Now you can open Vim and start pyclewn with
:Pyclewn. If you came across this blog post I suppose that you already know that pyclewn will not turn your Vim into an Eclipse or something like that. It doesn’t shelter you to grapple with gdb’s commands. I would say my environment is still expert friendly, which means I’m not really sure if my frustration level will reduce. No, I’m just kidding. Pyclewn provides the gdb command line directly in Vim and gives the possibility to bind gdb commands to keys and so on. After decades of configuring it will fit perfectly. Hopefully.
Please find my current Vim configuration here.