Profile of a shared object without recompiling the main program

I am currently developing a generic lilbrary for loading into PostgreSQL (as C-Language functions, see here ). Now I would like to profile the function in this library without recompiling PostgreSQL itself.

I tried callgrind with

valgrind --tool=callgrind path/to/postgres arguments-to-postgres

This gives me profiling information for PostgreSQL itself, but I cannot register the shared library that interests me.

I also tried sprof, but I have no idea how to make it work.

Any ideas would be highly appreciated.

PS: Please do not suggest just pausing the application in the debugger. With a runtimes function of less than 0.01 seconds, I need more detailed results.

+5
source share
2 answers

Using callgrind should work as expected. To test this, I set up a simple project using a simple library and the main Makefile function file:

CFLAGS=-fpic
exe:exe.o lib.so
        cc -o exe exe.o lib.so
lib.so:lib.o
        cc -shared lib.o -o lib.so
clean:
        rm -f exe lib.so *.o

lib.c is a simple library containing 2 functions:

#include <stdio.h>
void someOtherFunction() { printf("someOtherFunction\n"); }
void someFunction() { printf("someFunction\n"); someOtherFunction(); }

exe.c is a very simple executable file:

int someFunction();
void main() { someFunction(); }

Use the Makefile to create the executable and run it with valgrind as follows:

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PWD valgrind --tool=callgrind ./exe

If you look at the output of callgrind, you will find the profiling data for both functions in the shared library. If you do not see these functions, you can use a non-standard environment that does not support this function. I am using Linux Mint 11 x64 with the latest fixes.

+2

1: Valgrind , Postgres, . http://www.mail-archive.com/valgrind-users@lists.sourceforge.net/msg02355.html

2: (, strace), SL ? --trace-children = yes?

3: , exe.o exe -g0 dlopen , :

all: exe lib.so
exe : exe.c 
        cc -g0 -o exe exe.c -ldl

lib.so:lib.c
        cc -shared lib.c -o lib.so
clean:
        rm -f exe lib.so *.o

#include 
#include 

void main() { 
    void *handle;
    void (*p)();
    int i;

    handle = dlopen("./lib.so", RTLD_LAZY);
    if ( ! handle ) { printf("Object not found\n"); return; }
    p = dlsym(handle, "someFunction");
    if ( ! p ) { printf("Function not found\n"); return; }
    for (i = 0; i < 100; ++i) (*p)();
    dlclose(handle);
}

callgrind. , Postgres -ldl ?

+1