9

Here's a problem I've had recently that just HAS to be a common pain to others here.

I'm working with someone else's legacy C code and need to find where a function or macro was defined. The code #includes a bunch of different standard system libraries in addition to those from the specific project.

Is there a tool or technique to quickly find where a specific function, macro, (or other global for that matter) was defined?

I tried:

grep -R 'function' /usr/lib

and other similar *nix/bash-fu with only limited success and lots of annoying chaff to cull. One of you sage coders out there must have a good solution to this seemingly common scenario.

I was very surprised to not find another question on this particular pain here or in my searches of the interwebs. (I'm sure there will be angry comments if I missed one... ;-))

Thanks in advance for any tips!

jww
  • 97,681
  • 90
  • 411
  • 885
JJC
  • 9,547
  • 8
  • 48
  • 53

7 Answers7

4

Use etags/ctags from the exuberant ctags project in conjunction with an editor (emacs, vim) that understands them, or GNU GLOBAL.

Oh, and if you happen to use automake it generates a target TAGS. So no need for complicated manual calls to {c,e}tags.

hroptatyr
  • 4,702
  • 1
  • 35
  • 38
  • Thanks. Although I had used ctags before, I hadn't realized I could use them for existing system libraries. I like GNU GLOBAL's ability to also show instances of a function (i.e. reverse mapping of what CTAGS does). – JJC Mar 27 '12 at 19:21
1

Use ctags/cscope + vim/emacs

you can google for their detail use.

if you use ctags + vim, you can :

1.go to the /usr/include directory, excute ctags -f tags1 -R . generate the tags

2.generate tags for your code in your code directory ctags -f tags2 -R.

3.run :set path+=tags1,tags2 in your vim

4.under a function or marco try CTRL+]

llj098
  • 1,404
  • 11
  • 13
1

Here is what you can do, assuming you use gcc, if not just modify it accordingly.

gcc -E myfile.c | grep '^#' | cut -f 3 -d ' ' | sort |uniq | xargs -n 1 grep -l "MYMACROORFUNCTIONNAME"
pizza
  • 7,296
  • 1
  • 25
  • 22
0

You can use Eclipse CDT. For example here is described how to setup CDT project to navigate Linux kernel source - HowTo use the CDT to navigate Linux kernel source.

ks1322
  • 33,961
  • 14
  • 109
  • 164
0

vim + ctags is the way to go. You can jump to and definition of functions, global variables, macros, etc. etc.

FYI, browsing programs with tags

Also, if you want to quickly switch between .c and .h files, please refer to this blog

Jinghao Shi
  • 1,077
  • 2
  • 10
  • 15
0

you can use cscope or emacs/vim + xcscope.el to do that easily. I think it's batter than ctage and etage.

madper
  • 806
  • 1
  • 10
  • 25
0

Provided the correct headers are included that directly or indirectly define what you look for, most IDEs have a jump-to-definition-functionality that works.

The tags-approaches are of course nice because they don't depend on correctly included headers.

Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130