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

Look to this:


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
//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

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

break *0xaddress
# target process is now stopped at breakpoint
python import time
python starttime=time.time()
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
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
  • Worth noting, to get more or less accurate time representation one have to use commands command to make the code get executed automatically on entering the breakpoint. – Hi-Angel Aug 28 '20 at 7:12
  • 1
    @Hi-Angel Good point. The time calculations need to be done as soon as possible after the breakpoint is hit and after the program exits. Unfortunately, when executing a commands list attached to a breakpoint, if the list contains continue, any commands after it in the list won't be run. So we likely need to have the run/time-calc/continue/time-calc commands in a canned script (source or define). I will modify my answer. Thanks. – Mark Plotnick Aug 28 '20 at 15:04

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.