print called function name using GCC plugin

1.2k Views Asked by At

I need to print the name of the called functions of a program using gcc plugins for this I created a pass that will be called after ssa pass, I already initiated the plugin and I can loop on its statements, using a gimple_stmt_iterator :

int read_calls(){
  unsigned i;
  const_tree str, op;
  basic_block bb;
  gimple stmt;
  tree fnt;
  FOR_EACH_BB_FN(bb, cfun) {
    gimple_stmt_iterator gsi;
    for (gsi=gsi_start_bb(bb); !gsi_end_p(gsi); gsi_next(&gsi))
    {
        stmt = gsi_stmt(gsi);
        if (is_gimple_call(stmt)){
          const char* name = THE_FUNCTION_I_NEED(stmt);
          cerr << " Function : " << name << " is called \n";
        }
    }
  }
  return 0;
}

How can I print the name of the called function using its gimple node ?? Can I also print other informations like the line number where it was called, the name of the function where it was called etc .. ?

2

There are 2 best solutions below

1
On BEST ANSWER

I've been looking for the answer for hours, the answer is actually pretty easy : get_name(tree node)... I've been trying many functions since the documentation is really poor... I found it here : GCC Middle and Back End API Reference

As you can see, there is no comments about what the functions does, and it quit the best documentation I found about gcc, anyway get_name(..) is working fine, bit I haven't find how to print the source line yet

0
On

I know three ways:

1:

tree current_fn_decl = gimple_call_fndecl(stmt);
const char* name = function_name(DECL_STRUCT_FUNCTION(current_fn_decl);

2:

const char* name = IDENTIFIER_POINTER(DECL_NAME(current_fn_decl));

3:

tree current_fn_decl = gimple_call_fndecl(stmt);
const char* name = get_name(current_fn_decl);