Technology vs. Humanity

Enabling STL Container Inspection in Eclipse (Ubuntu)

All information in this post was fetched from this link under the Eclipse Wiki.

This site is not responsible for the content from external sites/repositories.

Programming and debugging is part of life for EECS students here in Michigan, and possibly also applies to anywhere else. Millions of tools have been introduced to make programming easier, and IDEs are just among a portion of them. Visual Studio and XCode by Microsoft and Apple are the two most famous IDEs in the software industry, whereas Eclipse platform reins the open-source world alone. One thing good about IDEs is their friendliness to debuggers. Command line based debuggers like GDB under Linux can be quite powerful, but due to the limitation of its interface, it is somehow less intuitive to use than graphical solutions. Almost all IDEs provide well-developed debugging interfaces which use sophisticated command line based debuggers as the core and interact with the debugger with instant memory/variable inspections and code analyses, which greatly improves the efficiency of debugging. Eclipse uses GDB as its debugging core, but unfortunately the Java-oriented platform does not pay much attention on the Standard Template Library of C++. One consequence is that we cannot have a user-friendly lay-out of the print-out values of STL containers such as vectors and stacks, but some weird symbols defined by STL. Eclipse officially implements a work around for this problem, but the solution does not come as a native feature of the distribution of Eclipse. This article mainly introduces how to install the STL-pretty-printing feature and how to enable it in debugging.

The following method only works under Ubuntu Linux. Under Windows Visual C++ will be a much more reasonable tool to develop applications.

Before everything starts, please make sure that you have python installed on your computer. When you install Eclipse from apt-get, it should install python as one of the dependencies. In case that things go wrong, double check by typing

$ sudo apt-get install python

If it says no package needs to updated then everything is okay, if it does find some package to download it should tell you the size to download/install and just type y (for yes) when prompted. After it finishes you are ready to proceed.

Because Eclipse used Subversion (SVN) for its version control, well they recently have switched to git but some old implementations such as this STL-pretty-printer we are looking for is only available through SVN repository. To retrieve content from the repository, we need to have Subversion installed as well. Type

$ sudo apt-get install subversion

in the terminal. After it finishes we should be able to use the command svn. Now let’s fetch the source file of the STL-pretty-printer. In a reasonable local directory, say home directory, run the following command:

$ svn co svn://

The command above should create a directory called python/ under your working directory, and it contains all you need to setup the pretty printer. According to its official documentations, there exists a unresolved bug and you should revise it by yourself in the source file. Open file under python/libstdcxx/v6, look for line 624, there is a class definition as follows

class StdStringPrinter:

Right beneath it would be the implementation of the std::string pretty printer. Look for a line saying:

len = header.dereference ()['_M_length']

Right after this line, add

if len > 100:
	len = 100

and save the file. Now we have the STL pretty printer installed and bug fixed. Now we need to configure Eclipse to enable the pretty printer when debugging.

Run Eclipse, and go to Window->Preferences->C/C++->Debug->GDB. Notice that the GDB menu won’t appear unless you run your program under debug mode at least one after Eclipse gets installed. Now you need to create a file called .gdbinit containing the following content:

import sys
sys.path.insert(0, '/home/xxx/gdb_printers/python')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)

where xxx should be your user name and the path in the single quotation mark points to the python/ directory you have just pulled from SVN. Save the file and tell Eclipse where the file is where we left off above: Window->Preferences->C/C++->Debug->GDB. Click apply and restart everything. Now Eclipse’s debugging mode should support STL-container inspection. Give it a try! Oh yes, like everything else, don’t try on containers that are too large — by large I mean containing millions of elements… This will cause you to wait for a long time and the program will appear to be not responding… But it won’t crash. It really won’t crash, you just need some patience. And I certainly did not try that myself.