#include <csignal>
#include <cstdlib>
#include <sstream>
#include <unistd.h>
#include "Tracer.hpp"
#include "Lib/Environment.hpp"
#include "Shell/Options.hpp"
extern const char* VERSION_STRING;
static bool try_gdb(pid_t pid) {
std::stringstream command;
command
<< "gdb --batch -ex bt --pid="
<< pid;
std::cout << command.str() << '\n';
return std::system(command.str().c_str()) == 0;
}
static bool try_lldb(pid_t pid) {
std::stringstream command;
command
<< "lldb --batch -o bt "
<< " --attach-pid "
<< pid;
std::cout << command.str() << '\n';
return std::system(command.str().c_str()) == 0;
}
void Debug::Tracer::printStack() {
std::cout << "Version : " << VERSION_STRING << "\n";
if(env.options->traceback()) {
pid_t pid = getpid();
if(!try_gdb(pid) && !try_lldb(pid))
std::cout << "(neither GDB nor LLDB worked: perhaps you need to install one of them?)\n";
}
else
std::cout << "(use '--traceback on' to invoke a debugger and get a human-readable stack trace)\n";
std::raise(SIGTRAP);
}