Wednesday, August 26, 2009

Executable file editor (ELF editor)

If you need to edit executable files, HT Editor is your friend!

Download it from their website, build it, and run. I use it specifically to edit ELF program and section headers. Run ht. To access the menu items at the top, I had to use Esc key (Esc + F to access File menu, for example). To access the menu items at the bottom, use function keys: You will see "6 mode" at the bottom, hit F6 to change the mode.

To edit ELF headers, first start the program, then load the executable (F3 - Open), then hit F6 (mode), select elf/header (or any other part that you are interested), scroll to where you want to edit, hit F4 to switch to edit mode, do your edits, and hit F2 to save. Note that you can't run the program from another shell while working on it in the edit mode; either switch to view mode again (F4), or close HT editor.

Friday, August 7, 2009

Profiling 101

Quick profiling tips. Nothing really new here. I am just putting down this info so that I can find it later on :D

In order to profile your executable, compile and link the code with -g and -pg options. Note that you have to use these flags for linking as well.

Once you create an executable file, just run it as you normally would. It will create a gmon.out file. Then run 'gprof gmon.out' and voila! Here is the profiling data. You might want to check flags for gprof.

If your executable file forks a new process that you want to profile as well; or your executable file is an MPI program, you soon will realize that you only get one gmon.out file when you run your program. gmon.out that is produced by the final process overwrites the old one. You must use an environment variable called GMON_OUT_PREFIX for a get around. Set it to 'gmon.out-'`/bin/uname -n`. For bash, add this line to your .bashrc file:

export GMON_OUT_PREFIX='gmon.out-'`/bin/uname -n`

This way, every process will create its own gmon.out version, i.e. a file that starts with gmon.out followed by the machine name and pid (process id).

What if you want to profile a shared library? Again, compile and link the shared library with -g and -pg options. Then, set LD_PROFILE environment variable to the name of the shared library you want to profile. Finally, run the executable which uses this shared library. Upon completion, a file named .profile will be created in
/var/tmp. Use the sprof program to get the profile information for the shared library.

I also read that you can use LD_PROFILE_OUTPUT to specify a directory other then /var/tmp, but I haven't used this myself.

You might ask how one can profile multiple shared libraries. I really don't know an easy answer.

P.S. The information provided above is valid for gnu tools. I haven't tried any of the information above on any other tool.