I have a program written in Ruby and C. The C portion is a shared library, which is an extension for the Ruby program. I want to profile the C shared library I wrote, using gprof. I compile the shared library like this:
gcc -I. -I/usr/lib/ruby/1.8/i486-linux -I/usr/lib/ruby/1.8/i486-linux -I. -D_FILE_OFFSET_BITS=64 -fPIC -fno-strict-aliasing -g -march=i686 -O2 -ggdb -pg -fPIC -c extension.c
gcc -shared -o extension.so extension.o -L. -L/usr/lib -L. -Wl,-Bsymbolic-functions -rdynamic -Wl,-export-dynamic -lruby1.8 -lpthread -lrt -ldl -lcrypt -lm -lc
Then I execute the ruby program, which loads this shared library, and I expect a gmon.out file in the current directory, but for some reason the file gmon.out does not get created. How do I do this?
I googled for this but couldn't find a satisfactory answer (which worked).
P.S. - As a workaround I can have a modified version of the extension which is a pure C program (instead of being created as a shared library) that I can use to profile, but it becomes tedious to maintain two versions of the same C extension (large number of differences between the two).
I tried writing a C program which uses the shared library directly too. I immediately get a page fault in one of the ruby library functions which get called during the initialization of the shared library. I think it's really expecting to be loaded from a ruby program, which may internally be doing some magic.
(gdb) bt
#0 0x0091556e in st_lookup () from /usr/lib/libruby1.8.so.1.8
#1 0x008e87c2 in rb_intern () from /usr/lib/libruby1.8.so.1.8
#2 0x008984a5 in rb_define_module () from /usr/lib/libruby1.8.so.1.8
#3 0x08048dd0 in Init_SimilarStr () at extension.c:542
#4 0x0804933e in main (argc=2, argv=0xbffff454) at extension.c:564
Update: Never mind. I used #ifdef to compile out Ruby portions of the extension and get a profile. Closing.