mirage
mirage

Reputation: 53

How to instrument gcc?

I have to instrument gcc for some purposes. The goal is to be able to track what GCC functions are called during a particularly compile. Unfortunately I'm not really familiar with the architecture of GCC so I need a little help. I tried the following steps:

1) Hacking gcc/Makefile.in and adding "-finstrument-functions" flag to T_CFLAGS.
2) I have an already implemented and tested version of start_test and end_test functions. They are called from gcc/main.c, before and after toplev_main() call. The containing file is linked to gcc (the object is added to OBJS-common and the dependency is defined later in gcc/Makefile.in)
3) Downloading prerequisites with contrib/download_prerequisites.
4) Executing the configuration from a clean build directory (on the same level with the source dir): ./../gcc-4.6.2/configure --prefix="/opt/gcc-4.6.2/" --enable-languages="c,c++"
5) Starting the build with "make all"

This way I runned out of memory, although I had 28G.
Next I tried to remove the T_CFLAGS settings from the Makefile and give -finstrument-functions to the make command: make CFLAGS="-finstrument-functions". The build was successful this way but when I tried to compile something it resulted empty output files. (Theoretically end_test should have written its result to a given file.)

What do I make wrong? Thanks in advance!

Upvotes: 3

Views: 4121

Answers (1)

ArjunShankar
ArjunShankar

Reputation: 23680

Unless you specifically exclude it from being instrumented, main itself is subject to instrumentation, so placing calls to your start_test and end_test inside main is not how you want to do it. The 'correct' way to ensure that the file is opened and closed at the right times is to define a 'constructor' and 'destructor', and GCC automatically generates calls to them before and after main:

void start_test (void)
  __attribute__ ( (no_instrument_function, constructor));

void end_test (void)
  __attribute__ ( (no_instrument_function, destructor));

/* FILE to write profiling information.  */
static FILE *profiler_out;

void start_test (void)
{
  profiler_out = fopen ("profiler.out", "w");
  if (profiler_out == NULL)
    exit (-1);
}

void end_test (void)
{
  fclose (profiler_out);
}

Footnotes:

  1. Read more about constructor, destructor and no_instrument_function attributes here. They are function attributes that GCC understands.
  2. Read this excellent guide to instrumentation, on the IBM website.

Upvotes: 2

Related Questions