In this post, we will discuss how to search code with the help of cscope. Let’s begin by checking its description and capabilities (quoting directly from http://cscope.sourceforge.net/):
Cscope is a developer’s tool for browsing source code.
Allows searching code for:
- all references to a symbol
- global definitions
- functions called by a function
- functions calling a function
- text string
- regular expression pattern
- a file
- files including a file
- Curses based (text screen)
- An information database is generated for faster searches and later reference
- The fuzzy parser supports C, but is flexible enough to be useful for C++ and Java, and for use as a generalized ‘grep database’ (use it to browse large text documents!)
Of course, developers aren’t the only ones browsing the code (as implied by the tool’s description). In the Support team, we find ourselves having to check code many times. This tool is a great aid in doing so. As you can imagine already, this tool can replace
grep -R "<keyword(s)>" *, and will even add more functionality! Not only this, but our searches run faster (since they are indexed).
The main focus of this post is to explore cscope’s searching capabilities regarding code, but note that you can also use it for text searches that aren’t linked to function names or symbols (supporting regular expressions) and for file searches. This also means that even if the tool doesn’t recognize a function name, you can still use the text search as a fallback.
There is an online manual page, for quick reference:
To install it under RHEL/CentOS, simply issue:
shell> yum install cscope
You can use cscope with MySQL, Percona Server for MySQL or MariaDB code alike. In my case, I had a VM with Percona Server for MySQL 5.7.18 already available, so I’ve used that for demonstration purposes.
We should first get the source code for the exact version we are working with, and build the cscope database (used by the tool to perform searches):
shell> wget https://www.percona.com/downloads/Percona-Server-LATEST/Percona-Server-5.7.18-15/source/tarball/percona-server-5.7.18-15.tar.gz shell> tar xzf percona-server-5.7.18-15.tar.gz shell> cd percona-server-5.7.18-15 shell> cscope -bR
-b will build the database only, without accessing the CLI;
-R will recursively build the symbol database from the directory it’s executed, down. We can also add
-q for fast symbol lookup, at the expense of a larger database (we’ll check how much more below).
Now that we have built the cscope database, we will see a new file created:
cscope.out. If we used
-q, we will also see:
cscope.po.out. Their sizes depend on the size of the codebase in question. Here are the sizes before and after building the cscope database (with
shell> du -d 1 -h .. 615M ../percona-server-5.7.18-15 shell> cscope -bqR shell> du -h cscope.* 8.2M cscope.in.out 69M cscope.out 103M cscope.po.out shell> du -d 1 -h .. 794M ../percona-server-5.7.18-15
This gives around 30% increase in size while using
-q, and around 10% increase without it. Your mileage may vary: be aware of this if you are using it on a test server with many different versions, or if the project size is considerably larger. It shouldn’t be much of a problem, but it’s something to take into account.
Ok, enough preamble already, let’s see it in action! To access the CLI, we can use
A picture is worth a thousand words. The following output corresponds to searching for the MAX_MAX_ALLOWED_PACKET symbol:
If there are multiple potential matches, the tool lists them for our review. If there is only one match, it will automatically open the file, with the cursor at the appropriate position. To check a match, either select it with the arrow keys and hit enter, or use the number/letter listed. When you are done and need to get back to cscope to continue checking other matches, simply exit the text editor (which can be defined by using CSCOPE_EDITOR). To get back to the main menu to modify the search, press
CTRL-f. To exit the tool press
CTRL-c toggles case insensitive mode on and off.
To show how the tool displays searches with many hits, let’s search for functions that call printf:
We can now see that letters are also used to list options, and that we can hit space to page down for more matches (from a total of 4508).
Lastly, as mentioned before if everything else fails and you are not able to find the function or symbol you need (due to limitations or bugs), you can use the “Find this text string” and “Find this egrep pattern” functionality.
I hope this brief tour of cscope has been useful, and helps you get you started using it. Note that you can use it for other projects, and it can be handy if you need to dive into the Linux kernel too.
For even more power, you can read this vim tutorial (http://cscope.sourceforge.net/cscope_vim_tutorial.html), or set up ctags (http://ctags.sourceforge.net/) along with cscope.