3

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
  • If your gdb has python support, you could try python import time; starttime=time.time() followed by python print (time.time()-starttime). – Mark Plotnick Sep 25 '16 at 2:43
  • @MarkPlotnick,thanks, yes you are correct. Please post your comment as an answer to mark it as a correct answer. – husin alhaj ahmade Sep 25 '16 at 8:38
4

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.

  • Thanks a lot. Yes, I did it . but I am confused about the printed value. Is it in milliseconds or micro ? – husin alhaj ahmade Sep 25 '16 at 16:30
  • The time is in seconds, with fractional seconds after the decimal point. The number of digits after the decimal point that are actually significant depends on the timer clock rate of your system. Clocks can be as slow as 1/60 second. – Mark Plotnick Sep 25 '16 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.