Stack trace running UNIX application
How can I perform a live stack trace on a running UNIX applicaiton, and are there any utilities that are useful in digesting the stack trace once its done?
I'm looking to see if any functions are getting called more often than I would have expected them to be - the application works fine, it just recently slowed down and it doesn't appear that anything else in the system is respo开发者_开发问答nsible (no other processes are running with unusual memory/processor usage).
Profiling tools will show what bits of the program are taking up the CPU time. If you have to dig deeper, you may need other tooling. Depending on what species of unix you're after, the tools will vary, as this is sometimes quite platform specific. This article discusses process monitoring on Linux. Different versions of unix may have different sets of utilities for functions that have to interact with the kernel (e.g. Dtrace for Solaris). Some do work across platforms.
Have you tried using an actual profiler on your application? This will help you much more than just a stack trace. Usually you just have to compile your application with profile information. Then you can run it and use the information written to determine in which functions the most time is being spent, number of calls, etc.
How can I perform a live stack trace on a running UNIX applicaiton
gcore
can be used to grab a core file for a live process. This will give you a snapshot of the stack traces for all threads. This approach may, however, be a little too heavyweight for your needs.
If the suspect functions are system calls, you could try using strace
to see what's going on.
In any case, I think the first port of call should be a profiler, such as gprof
.
I'm guessing you want this at runtime without any debugger involvment. For that you could use glibc's backtrace functions. Documentation here and here(assuming Linux) just to get you started. This Linux Journal article might be of help also.
Your debugger may have the functionality to attach to a running process. With gdb
this looks like
$ gdb path/to/exec 1234
where 1234 is the PID of the running process.
(But consider those answers that direct you to a profiling utility.)
精彩评论