#include <iostream>#include <execinfo.h>intmain(){// Capture the stack tracevoid*buffer[50];int size =backtrace(buffer,50);// Print the stack tracebacktrace_symbols_fd(buffer, size, STDOUT_FILENO);return0;}
2. Capturing Stack Trace and Storing in a String
#include <iostream>#include <sstream>#include <execinfo.h>intmain(){// Capture the stack tracevoid*buffer[50];int size =backtrace(buffer,50);// Convert the stack trace to a stringstd::ostringstream ss;backtrace_symbols_fd(buffer, size,ss.rdbuf()->fd());// Print the stack tracestd::cout <<ss.str()<<std::endl;return0;}
3. Capture Stack Trace and Filter by Function Name
4. Capture Stack Trace and Truncate to a Specific Size
5. Capture and Store Stack Trace in a Buffer
6. Capture and Store Stack Trace in a File
7. Capture and Format Stack Trace Using Custom Formatter