Linux.com

Feature

Using Cscope and SilentBob to analyze source code

By Aleksey 'LXj' Alekseyev on March 09, 2007 (8:00:00 AM)

Share    Print    Comments   

When you start learning the source code of an unfamiliar project, you don't have the knowledge of its structure or the meaning of specific functions, classes, and units in the project. You can use tags to browse for definitions, but it's hard to get an overall picture by just looking through every definition one by one. Cscope and SilentBob are two tools that can help you analyze unfamiliar source code. They help you find symbol definitions, determine where specific functions are used, determine which functions are called by other given functions, and search for strings and patterns throughout the code base. With them, you can save time by doing fast, targeted searches instead of grepping through source files by hand.

SilentBob

Why using SilentBob for generating tags?

SilentBob uses syntax analysis to parse source files, which makes it faster than a utility like Exuberant Ctags, which uses regular expressions to locate the appropriate line in a file. In a test run on the sources for the Linux kernel (version 2.6.19) Exuberant Ctags generated the tags table in 90 seconds, while SilentBob did its job in 10 seconds (on a 2.6MHz Celeron). SilentBob also supports multithreading optimizations.

Another difference is the format of tags tables: In tables created by Exuberant Ctags, regular expressions are used to locate the appropriate line in a file. This means that if you edit the file, and the position of some definition is changed, you don't need to rebuild the tags table. This is good for Exuberant Ctags because it means you don't need to regenerate the table often, but it also means that if you are viewing huge files, jumping to a tag definition will be much slower than if line numbers were used. That's why SilentBob uses line number in its tags tables; however, that means you have to update the tags table after every major edit.

SilentBob is another new tool for analyzing source code. It currently supports C/C++, Perl, and Python, but its plugin framework (which is not documented at the moment) enables users to add support for new languages and other features easily.

You can get source tarball and deb packages from the downloads section of the software's site. After installation, invoke SilentBob three times in the directory with source code:

  bob --make-ctags
  bob --cfiles
  bob -L cfiles --call-tags

This will generate three files: tags, the common tags table; cfiles, a list of C/C++ files; and call_tags, the call tags table. The call tags table contains tags for function calls, so if you want to find all the calls of some function, you can point Vim to use the call tags table (:set tags=./call_tags) and use the same commands as for searching tags (:tag function-name and :tnext and :tprevious to cycle through results). These index files can be used by SilentBob to build call trees and backward call trees.

For Perl and Python, only tags tables and file lists are supported:

  bob <--perl | --python> --make-ctags
  bob <--perl | --python> --files
The second command will generate a perl_files or python_files file.

Once you have a tags table, SilentBob can show you a call tree:

  bob [--depth N] function

--depth option allows you to limit the depth of the tree. If the only thing you need to know is which functions are called by function, specify --depth 0. Otherwise, you will be shown which functions are called by functions called by functions (...) called by function.

Note that you can use this option with the tags table generated by SilentBob for Python and Perl files. Tables generated by Exuberant Ctags are not supported.

The call tags table is used to generate a backward call tree:

  bob [--depth N] -u function

This will show functions which call functions (...) which call function. In this case specify --depth 1 to see only functions which call function

SilentBob can also use the created cfiles file to search for text within C/C++ code. It checks operators; strings and comments are ignored.

  bob list of files --cgrep pieces of text, separated by comma

You can specify -L ./cfiles to use generated file list. Pieces of text should be from one operator, so if you are looking for testing of T variable, use:

  bob -L ./cfiles --cgrep if,T

SilentBob also includes a tags utility that lets you to view the tag definitions in a C/C++ file in a console. Invoke tags tag1 tag2 ... tagN to strip the fragments of code you are interested in from code -- function definitions, global variable declarations, etc. -- and you will see the fragments of code you need.

 

Share    Print    Comments   

Comments

on Using Cscope and SilentBob to analyze source code

Note: Comments are owned by the poster. We are not responsible for their content.

Do they support events/function pointers?

Posted by: Anonymous Coward on March 10, 2007 01:39 AM
The most difficult thing to figure out when you start on a new project is events (function pointers). It is almost never obvious how they are handled by reading the source. You need a tool to tell you which events triggers which functions and all possible functions a function pointer could point to.

Do Cscope or SilentBob support that?

#

Re:Do they support events/function pointers?

Posted by: Anonymous Coward on March 10, 2007 06:12 AM
Hehe, that's why whenever possible, I like to resort to using Qt's signals and slots which are much easier to work with than function pointers.

#

LXR is good

Posted by: Anonymous Coward on March 12, 2007 06:11 PM
Try LXR.

Check out <a href="http://lxr.mozilla.org/seamonkey/" title="mozilla.org">http://lxr.mozilla.org/seamonkey/</a mozilla.org>
and <a href="http://lxr.mozilla.org/seamonkey/source/js/src/jsparse.c" title="mozilla.org">http://lxr.mozilla.org/seamonkey/source/js/src/js<nobr>p<wbr></nobr> arse.c</a mozilla.org> for an example.

Setting up may be a little tricky. I think I remember that after running a program to compile a database, you had to adjust apache config files to view it (or else generate dbase in area where apache can already find it).

You browse the code through a web interface (from a browser pointing to the correct url). Hyperlinks provide easy and quick cross-referencing. I think pages are generated in real-time which is why you need a cgi webserver (I think cgi is in perl). See <a href="http://lxr.linux.no/New-INSTALL" title="linux.no">http://lxr.linux.no/New-INSTALL</a linux.no>

#

kscope

Posted by: Anonymous Coward on March 12, 2007 09:09 PM
kscope is a GUI on cscope. I have been using it for years. Check it out!

<a href="http://kscope.sourceforge.net/" title="sourceforge.net">http://kscope.sourceforge.net/</a sourceforge.net>

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya