Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C code parser to track function calls and variable accesses within a project (emacs compatibility would be nice)

What I would like is a tool to be able to tell me which functions call a particular function A() (in a C project), and which functions call those functions etc so that I can have a list of functions which i know that when they are called, there is a possibility that function A() will be called.

For example we have the following functions scattered in a project:

 void A()
 { /*does something*/ }

 void B()
 {
   A();
   /*and more stuff*/
 }

 void C()
 {
if(unlikely_to_be_false)
    A()
/* moar stoff */
 }

 void D()
 {
/* code that does not refer to A() at all */
 }

 void E()
 {
C()
 }

When the awesome tool is run with parameter A, It will return somehow a the functions B C and E.

Close to this but not exactly i would like to accomplish this: Given a variable somewhere in a project find all read/write operations(direct or indirect) to it.

For example:

void A()
{
    char* c; // this is our little variable

    B(c); // this is in the resulting list
}

void B(char* x)
{
    printf("%c", x); // this is definately in the list

    *x='d' // this is also in the list

    C(x); // also in the list
}

void C(void* ptr)
{
    ptr = something; // also in the list
}

If the above could play well with emacs i would be most delighted!

like image 529
fakedrake Avatar asked Jan 18 '23 09:01

fakedrake


2 Answers

You could have a look on cscope tool (http://cscope.sourceforge.net/). It supports very large projects and a lot of different queries type :

  • Find this C symbol
  • Find this global definition
  • Find functions called by this function
  • Find functions calling this function ...
like image 85
Nic_tfm Avatar answered Jan 19 '23 22:01

Nic_tfm


First, there is the issue of calls between different compilation units, e.g. foo.c defining function foo1 calling function bar2 defined in bar.c (and that bar2 might call a foobar defined in foo.c or in another file foofoo.c)

Then, you might consider perhaps developing a GCC plugin or a MELT extension to suit your needs.

You could also buy a costly static analyzer tool.

Emacs has cedet which might interest you.

like image 27
Basile Starynkevitch Avatar answered Jan 20 '23 00:01

Basile Starynkevitch