3

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.

osgx
  • 90,338
  • 53
  • 357
  • 513

3 Answers3

3

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.

Mike Dunlavey
  • 40,059
  • 14
  • 91
  • 135
  • I want automatic periodic stack dumps. For big applications, which can run several minutes. 10ms is impossible for me to pressing a ctrl-c's – osgx Mar 25 '10 at 15:38
  • in manual mode there is a risk not to get stack from most hot points, when there are >2-3 such points in application. – osgx Mar 25 '10 at 15:39
  • No, I can't get a lsstack. I can use gdb, and gdb has better abilities in working with my applications. – osgx Mar 25 '10 at 15:40
  • @osgx: Regarding your second comment, I think you will find that it works, and here's why. What you call a hot point, when fixed, will reduce execution time by some X percent (probably somewhere between 5% to 90%). Then the probability that any random stack sample will expose it is >= X. For example, if X is 20% then if you take 20 samples (no matter how slowly you take them) you will see the problem on about 4 of them, and that will tell you what to fix. Then, repeat. – Mike Dunlavey Mar 25 '10 at 17:08
  • I don't want to do it by hand! (ctrl-c will be killed after some time). I want to do it by script. With script I will get rather similar results, but when working manually, I will get very different points. If you can't help me with AUTOMATING gdb, please, feel free to delete this answer. – osgx Mar 25 '10 at 17:24
  • When I have 3-4 hot points in application - I have no easy way of eliminating them. – osgx Mar 25 '10 at 17:25
  • @osgx: 20 ctrl-c won't exhaust you, but suit yourself. Here's an example of an app speeded up by 43x, by eliminating a series of what you call hot points: http://stackoverflow.com/questions/926266/performance-optimization-strategies-of-last-resort/927773#927773 – Mike Dunlavey Mar 25 '10 at 17:27
  • In my case I want to use gdb with script (or small controlling program using `setitimer`). But I can't write such script now and asking for help from stackoverflow community. And If I need stack without periodic interrupts, I surely will use gdb with ctrl+c. And more, on modern distribs or on solaris I will use `lsstack`/`pstack`, thanks. – osgx Mar 25 '10 at 17:59
  • Consider, that I want to `profile` with such method (gdb+stacktraces every 10ms) a lot of programs, which all are using two libraries. And the libraries is waht I want to profile actually. – osgx Mar 25 '10 at 18:00
  • @osgx: Good luck, and I hope it works. I was just trying to help. – Mike Dunlavey Mar 25 '10 at 18:05
1

(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

S R Bandi
  • 81
  • 5
0
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.

osgx
  • 90,338
  • 53
  • 357
  • 513
ldarby
  • 19