5

I am looking for a way to get a list of all the function and variable names for a set of c source files. I know that gcc breaks down those elements when compiling and linking so is there a way to piggyback that process? Or any other tool that could do the same thing?

EDIT: It's mostly because I am curious, I have been playing with things like make auto dependency and graphing include trees and would like to be able to get more stats on the source files. And it seems like something that would already exist but i haven't found any options or flags for it.

William Hogg
  • 53
  • 1
  • 4
  • Possible duplicate or at least suggest looking at http://stackoverflow.com/questions/1496497/how-can-i-see-parse-tree-intermediate-code-optimization-code-and-assembly-code – Foon Feb 16 '17 at 14:49
  • **Why exactly do you ask?** You should edit your question to motivate it a lot more and explain your overall goal and application... – Basile Starynkevitch Feb 16 '17 at 15:03
  • @Foon: I don't feel that this question is a duplicate of the [other](http://stackoverflow.com/questions/1496497/how-can-i-see-parse-tree-intermediate-code-optimization-code-and-assembly-code) one. – Basile Starynkevitch Feb 16 '17 at 15:06
  • Can you use gprof? That gives a list of all functions. I don't know of a tool which lists all variables, though as you say, a compiler obviously does so internally. – Malcolm McLean Feb 16 '17 at 15:18

1 Answers1

6

If you are only interested by names of global functions and variables, you might (assuming you are on Linux) use the nm or objdump utilities on the ELF binary executable or object files.

Otherwise, you might customize the GCC compiler (assuming you have a recent version, e.g. 5.3 or 6 at least) thru plugins. You could code them directly in C++, or you might consider using GCC MELT, a Lisp-like domain specific language to customize GCC. Perhaps even the findgimple mode of GCC MELT might be enough....

If you consider extending GCC, be aware that you'll need to spend a significant time (perhaps months) understanding its internal representations (notably Generic Trees & Gimple) in details. The links and slides on GCC MELT documentation page might be useful.

Your main issue is that you probably need to understand most of the details about GCC internal representations, and that takes time!

Also, the details of GCC internals are slightly changing from one version of GCC to the next one.

You could also consider (instead of working inside GCC) using the Clang/LLVM framework (but learning that is also a lot of time). Maybe you might also look into Frama-C or Coccinnelle.

Another approach might be to compile with debug info and parse DWARF information.

PS. My point is that your problem is probably much more difficult than what you believe. Parsing C is not that simple ... You might spend months or even years working on that... And details could be target-processor & system & compiler specific...

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547