Automate gdb: show backtrace every 10 ms

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

It could be something like profiling a call graph for penniless (for people who can't use any advanced profiler).

Yes, there are many advanced profilers. For popular processors and popular OS. Shark is very impressive and easy to use, but I want to get basic functionality with a script while working with gdb.

+3
source share
3 answers

Can you get lsstack ? Perhaps you can run this from a script outside your application. Why 10 ms? The percentages will be approximately the same at 100 ms or more. If the application is too fast, you can artificially slow it down with an external loop, and that won't change the percentage either. In this case, you can simply use Ctrl-C to get samples manually under gdb if the application is running long enough and if your goal is to find out where the performance problems are.

+3
source

(1) Manually. Complete the following in a shell. Continue to press Ctrl + C on the command line.

gdb -x print_callstack.gdb -p pid

or, (2) send signals to pid several times at the same time on another shell, as in the lower loop

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

print_callstack.gdb (1) :

set pagination 0
set $count = 0
while $count < 100
    backtrace
    continue
    set $count = $count + 1
end
detach
quit
+1
cat > gdb.run
set pagination 0 
backtrace 
continue 
backtrace 
continue 
... as many more backtrace + continue as needed
backtrace 
continue 
detach 
quit

, , ?: (

gdb -x gdb.run -p $pid

do

kill -INT $pid ; sleep 0.01

in a loop in another script.

kill -INT- this is what the OS does when clicked ctrl-C. Reader Exercise: Make a gdb script use a loop with iterations of $ n.

0
source

Source: https://habr.com/ru/post/1738425/


All Articles