Linux – Elapsed time in gdb

gdbkernellinux

Is there a way to measure elapsed time running the program under gdb?

Look to this:

<------------bp---------------------------------->

Assume that we are debugging a file and in some random place, we set a breakpoint. Now in gdb we perform something and then we let the program continue the execution by using the gdb command line (run).

My question is here.
I want to measure the elapsed time from the bp until the program either successfully ends or some error occurs.

My suggestion is to use .gdbinit file, and in that file we call some C function to start the timer after run command and at the end of the execution we also call a gettime() C fun.

So, my pseudo code is a bit like this (.gdbinit file):

break *0x8048452 (random place)
//start time
run
//get time

Best Answer

The easiest way to do this (if your gdb has python support):

break *0xaddress
run
# target process is now stopped at breakpoint
python import time
python starttime=time.time()
continue
python print (time.time()-starttime)

If your gdb doesn't have python support but can run shell commands, then use this:

shell echo set \$starttime=$(date +%s.%N) > ~/gdbtmp
source ~/gdbtmp
continue
shell echo print $(date +%s.%N)-\$starttime > ~/gdbtmp
source ~/gdbtmp

Alternatively, you could make the target process call gettimeofday or clock_gettime, but that is a lot more tedious. These functions return the time by writing to variables in the process's address space, which you'd probably have to allocate by calling something like malloc, and that may not work if your breakpoint stopped the program in the middle of a call to another malloc or free.

Related Question