2

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 .. ?

artless noise
  • 21,212
  • 6
  • 68
  • 105
Othman Benchekroun
  • 1,998
  • 2
  • 17
  • 36

2 Answers2

2

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

Othman Benchekroun
  • 1,998
  • 2
  • 17
  • 36
  • 3
    You forgot the *most important* part of the answer! At least for gcc noobs like myself. That is how you do it from the `cfun` pointer. It is `get_name(cfun->decl)`. This is very helpful in gdb when trying to debug predicates... uugh. – Daniel Santos Aug 28 '16 at 01:11
2

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);
Jordy Baylac
  • 510
  • 6
  • 18