Question

I want to write a script for gdb, which will save backtrace (stack) of process every 10 ms. How can I do this?

It can be smth like call graph profiling for 'penniless' (for people, who can't use any sort of advanced profiler).

Yes, there are a lot of advanced profilers. For popular CPUs and for popular OSes. Shark is very impressive and easy to use, but I want to get a basic functionality with such script, working with gdb.

Was it helpful?

Solution

Can you get lsstack? Perhaps you could run that from a script outside your app. Why 10ms? Percentages will be about the same at 100ms or more. If the app is too fast, you could artificially slow it down with an outer loop, and that wouldn't change the percentages either. For that matter, you could just use Ctrl-C to get the samples manually under gdb, if the app runs long enough and if your goal is to find out where the performance problems are.

OTHER TIPS

(1) Manual. Execute the following in a shell. Keep pressing Ctrl+C repeatedly on shell prompt.

gdb -x print_callstack.gdb -p pid

or, (2) send signals to pid repeatedly same number of times on another shell as in below loop

let count=0; \
while [ $count -le 100 ]; do \
  kill -INT pid ; sleep 0.10; \
  let $count=$count+1; \
done

The source of print_callstack.gdb from (1) is as below:

set pagination 0
set $count = 0
while $count < 100
    backtrace
    continue
    set $count = $count + 1
end
detach
quit

man page of pstack https://linux.die.net/man/1/pstack

cat > gdb.run
set pagination 0 
backtrace 
continue 
backtrace 
continue 
... as many more backtrace + continue's as needed
backtrace 
continue 
detach 
quit

Of course, omit the duplicate newlines, how do you do single newlines in this forum software? :(

gdb -x gdb.run -p $pid

Then just use do

kill -INT $pid ; sleep 0.01

in a loop in another script.

kill -INT is what the OS does when you hit ctrl-C. Exercise for the reader: make the gdb script use a loop with $n iterations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top