views:

195

answers:

3

Hello

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.

+1  A: 

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
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
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
No, I can't get a lsstack. I can use gdb, and gdb has better abilities in working with my applications.
osgx
@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
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
When I have 3-4 hot points in application - I have no easy way of eliminating them.
osgx
@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
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
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
@osgx: Good luck, and I hope it works. I was just trying to help.
Mike Dunlavey
A: 

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.

ldarby
please, no exercises
osgx
A: 

It's probably not even possible to do the loop in gdb script. What I wrote was more helpful than anything else so far you ungrateful prick. [edit: this is meant to be a response to "please, no exercises", there's no reply button for that.]

ldarby
there is a button "add comment"
osgx