Search Ali's Blog

Tuesday, January 12, 2010

Tips for better cscoping

Cscope is a developer's tool for browsing source code. It has an impeccable Unix pedigree, having been originally developed at Bell Labs back in the days of the PDP-11. Cscope was part of the official AT&T Unix distribution for many years, and has been used to manage projects involving 20 million lines of code! I use it a lot of work to manage browse the code of several kernels (any other editor I tried failed to do the job in reasonable time, cscope did handle it!)

Here are few tips for better cscoping:
- Display more pathname components in search results with -pN. By default, cscope only displays the basename of a given matching file. In large codebases, files in different parts of the source tree can often have the same name (consider main.c), which makes for confusing search results. By passing the -pN option to cscope at startup (or including -pN in the CSCOPEOPTIONS environment variable) -- where N is the number of pathname components to display -- this confusion can be eliminated. I've generally found -p4 to be a good middle-ground. Note that -p0 will cause pathnames to be omitted entirely from search results, which can also be useful for certain specialized queries.

- Use the built-in history mechanisms. You can quickly restore previous search queries by using ^b (control-b); ^f will move forward through the history. This feature is especially useful when performing depth-first exploration of a given function hierarchy. You can also use ^a to replay the most recent search pattern (e.g., in a different search field), and the > and < commands to save and restore the results of a given search. Thus, you could save search results prior to refining it using ^ (as per the previous tip) and restore them later, or restore results from a past cscope session.


Quick Start for lazy engineers:
find -name *.[ch] > cscope.files
cscope -bqk
cscope -d -p2
and 1.. 2..  3.. browse..

No comments: