11

All the cscope tutorials I found online talk about how to use the interactive mode of cscope to search for symbols in editors such as vim and emacs. But I think it should be possible to issue a command in terminal to do something like

cscope -d -some_options <my symbol>

And I should be able to see a list of results in stdout, instead of having to enter the ncurse UI and do everything there. I think this is possible because the "only" frontend cbrowser can do things like that in its TclTK UI. But the code unfortunately is quite beyond me.

However, I found no documentation about this capability.

Am I dreaming or is there an undocumented way of doing this?

Thanks!

UPDATE

Some progress: If I make a small project of a few files with sub-dir structure. Then rici's answer works out of the box. With a bigger project (thousands of files with complex folder structure). Even with a cscope.out and cscope.files present at the root of the project folder (also my current working directory), I got nothing from the same command and same symbol. I suspect that there is a scalability issue with the command. I also tried command

cat cscope.files | xargs cscope -d -L1 <symbol> -i

to no avail.

UPDATE

Extremely bizarre! I tried to use some other symbols. Turned out that the particular symbol I was searching for cannot be shown using the command line. But all other symbols I tried worked. And cbrowser has no problem finding that "failed" symbol. Anyways, I was just in bad luck. I'll ask a separate question about this anomaly in command line.

I marked rici's answer as correct.

kakyo
  • 10,460
  • 14
  • 76
  • 140
  • I use [`grep`](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/grep.html): `grep -options *.c` – pmg Feb 16 '13 at 22:50
  • 2
    But that is plain text search. cscope is much more precise. – kakyo Feb 16 '13 at 23:26
  • Right, but sometimes you don't need a cannon to kill one fly. – pmg Feb 16 '13 at 23:54
  • Did you ask a separate question? If yes, can you link it? – maxschlepzig Nov 30 '17 at 22:18
  • No I have not. Sorry it's been a long long time. I don't remember the details anymore. My bad! But I think your answer makes a lot of sense. I marked it as correct and will test it out later. – kakyo Dec 03 '17 at 13:06
  • Whoever is coming here in 2019, get cscope 15.9. @rici's answer is working on not-so-large code base of about 150 files. – tpb261 Sep 08 '19 at 10:44

2 Answers2

22

You are probably looking for this:

cscope -L1<symbol>

You could use -d as well, although if you're modifying the files, it's good for cscope to update it's database.

-L means "execute a single line-oriented command", and the following digit (1 in this case), which could also have been written as a separate option, is the specific command, which the manpage confusingly calls a "field". The "fields" are given by the interactive cscope prompt; I added the digit for convenience. "this" refers to the text which follows the digit; remember that it's a pattern so you don't necessarily have to type the full symbol.

 0 Find this C symbol:
 1 Find this function definition:
 2 Find functions called by this function:
 3 Find functions calling this function:
 4 Find this text string:
 5 Change this text string:
 6 Find this egrep pattern:
 7 Find this file:
 8 Find files #including this file:

Manpage references

       -d     Do not update the cross-reference.

       -L     Do a single search with line-oriented output when used with the -num pattern option.

       -l     Line-oriented interface (see ``Line-Oriented Interface'' below).

https://linux.die.net/man/1/cscope

Sridhar Sarnobat
  • 25,183
  • 12
  • 93
  • 106
rici
  • 234,347
  • 28
  • 237
  • 341
  • I tried your line "cscope -d -L1 " under the folder where my cscope.out is. Unfortunately it gave me nothing. On the other hand, firing up "cbrowser" at the same location, I can submit the same query and have results listed in the UI. Am I missing something? I tried the command line with and without the space between the option and the symbol argument. – kakyo Feb 16 '13 at 23:32
  • @kakyo: All I can say is that it works for me. You didn't actually type the `<` and `>`, right? Did you try deleting cscope.out and letting cscope rebuild it (i.e. without using the -d flag)? – rici Feb 16 '13 at 23:39
  • No, I know symbol does not come with <> by itself:). I just tried removing cscope.out, then "scope -L1 ". To my surprise, cscope didn't generate the file on the fly; instead, I got "cscope: no source files found". Is this because my code was in sub folders relative to cscope.out? Do I have to specify source files in that command? – kakyo Feb 16 '13 at 23:49
  • @kakyo: yes, if it's not obvious where they are. You can use wildcards. – rici Feb 16 '13 at 23:51
  • so the real command should have been "cscope [-d] -L1 ". Something like that?? – kakyo Feb 16 '13 at 23:53
  • Ok, I tried something like "find . -type f \( -name "*.h" -o -name "*.cpp" -o -name "*.hpp" -o -name "*.c" -o -name "*.inl" \) | cscope -d -L1 ". Still nothing... – kakyo Feb 16 '13 at 23:58
  • Ok, I think I needed "-i" to specify the files. I'll report back. – kakyo Feb 17 '13 at 00:04
  • This is weird. If I make a small project of a few files with sub-dir structure. Then your command works out of the box. With a bigger project (thousands of files with complex folder structure). Even with a cscope.out and cscope.files present, I got nothing from the same command and same symbol. I'll vote up your answer. There is probably a scalability issue with cscope itself that I haven't figured out. Thank you for you help so far! – kakyo Feb 17 '13 at 00:23
  • 1
    Make sure file name is specified in cscope.file and this works – Tectrendz Sep 24 '13 at 20:45
  • Is it possible to tell cscope to format the output the same way it formats it in the ncurses "GUI"? (with function names nicely aligned in columns) – SasQ Jul 21 '22 at 07:00
7

You can call cscope with the -R version for recursive searching. For example:

cscope -d -f/path/to/cscope.out -R -L1 some_symbol

(searches for the definition of some_symbol)

cscope -d -f/path/to/cscope.out -R -L3 some_symbol

(shows all locations where some_symbol is called)

You can omit the -f option if cscope.out is located in the current working directory.

Note that the above call yield zero results for an indexed symbol if -R is omitted. Very old cscope versions don't support -R. For example, version 15.8a does support it.

The list of possible values for -L is:

0: Find this C symbol
1: Find this definition
2: Find functions called by this function
3: Find functions calling this function
4: Find this text string
6: Find this egrep pattern
7: Find this file
8: Find files #including this file
9: Find places where this symbol is assigned a value

The -R option can also be used when creating the cscope.out file, e.g.:

cscope -bR
maxschlepzig
  • 35,645
  • 14
  • 145
  • 182