Monday, December 31, 2012

My Favorite Code Development Tools, Part 2:
Cscope and Doxygen

To continue my previous post about my favorite code development tools, I'll share two tools I've recently come across that greatly enhance text-mode code development: Doxygen and Cscope.

I learned more about these tools while attempting to familiarize myself with the new version of the Sniper Simulator that was released at the beginning of December. For architectural simulators such as Sniper and gem5, being the fairly large code projects that they are, the task of wrapping one's head around the code sections of interest - or even finding those codes at all - can be a daunting task. In the process of navigating the source code and trying to overcome this hurdle, I discovered two tools that assist in the process of code comprehension through static code analysis.

Although many are familiar with Doxygen, some of its less prominent features may remain unknown to many users. While most probably know that it will generate documentation from code that has been commented, just as useful is its ability to generate other information about code even when it is entirely uncommented. The feature that has become very useful for me recently has been its ability to generate call graphs. To enable this, in your Doxygen configuration file, set HAVE_DOT, CALL_GRAPH, and CALLER_GRAPH to "YES". Doing this will tell Doxygen to generate nice graphs using the Dot program, which can be used to navigate the code by determining for each function f which functions f calls and which functions are called by f. You must have the Dot program installed. (Note that Dot is a very useful program in its own right.)

Viewing the call graphs from Doxygen requires a program with a graphical interface such a browser to view the generated graphs. For working in text mode, another option for navigating code based on function calls is Cscope. The interface of Cscope is similar to that of ctags and omnicpp, which were mentioned in the previous post. There is a plugin for Vim available which provides the Cscope features. Cscope provides a number of incredibly useful features for code navigation - and all in text mode. A series of keystrokes will display a list of functions that call the function under the cursor, for example. There is a tutorial for using Cscope with Vim here.

Since Doxygen and Cscope provide information about the program without actually running it, these are static analysis tools. Additional information about applications can be provided by analyzing them dynamically, or at run-time. Some dynamic code analysis tools that are useful are gprof and gdb. I will plan to discuss these in a future post. Another tool worth mentioning about which I learned recently is the -finstrument-functions switch for gdb, which is used with Dot to generate dynamic call graphs. A tutorial explaining how to use this switch is available here.