BDE 4.14.0 Production release
|
Provide a single function to perform and print a stack trace.
This component defines a namespace struct
, balst::StackTracePrintUtil
, containing static platform-independent functions that will perform a stack trace and print it to a supplied stream. Not all properties of a stack trace are printed on all platforms because the set of properties describing a stack trace that are obtainable varies according to both the platform and build parameters. For example, on Solaris, source file names and line numbers are not provided. Function names and addresses are provided on all platforms. The printStackTrace
function always prints a description of the stack of the calling thread.
The function StackTracePrintUtil::logExceptionStackTrace
is meant to be passed as a function pointer to the bslstl::StdExceptUtil::set*Hook
functions and will result in a stack trace being logged with fatal severity prior to the respective exception being thrown. It is a substitute for the function bslstl::StdExceptUtil::logCheapStackTrace
that takes longer to run but provides far superior output.
This section illustrates intended use of this component.
This example shows how to obtain a stack trace and print it to a stream, all by calling just the static function balst::StackTracePrintUtil::printStackTrace
.
First, we define a recursive function recurseAndPrintStack
that recurses to the specified depth
, then calls balst::StackTracePrintUtil::printStackTrace
to obtain a stack trace and print it to cout
. When we call printStackTrace
, neither of the optional arguments corresponding to maxFrames
or demanglingPreferredFlag
are supplied; maxFrames
defaults to at least 1024 (which is more than we need), and demanglingPreferredFlag
defaults to true
.
Then, we call recurseAndPrintStack
from the main program.
Now, invoking the main program on AIX produces the following output:
Finally, we observe the following about the above output to cout
. Notice that since the actual output would write each stack trace frame all on a single line, and all the lines here were longer than 80 characters, it has been manually edited to wrap and have every line be less than 80 columns. Also note the program name is truncated to 32 characters in length.