[debugging linux] Implement functions
Check TracerPid in /proc/self/status for attached debugger. Add SIGTRAP handler to prevent signal from halting app while not running in a debugger. Log DebugPrint in clog (stderr).
This commit is contained in:
parent
f8d7652dc4
commit
e348cacc6d
|
@ -9,21 +9,51 @@
|
||||||
|
|
||||||
#include "xenia/base/debugging.h"
|
#include "xenia/base/debugging.h"
|
||||||
|
|
||||||
#include <signal.h>
|
#include <csignal>
|
||||||
#include <cstdarg>
|
#include <cstdarg>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <mutex>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
#include "xenia/base/string_buffer.h"
|
#include "xenia/base/string_buffer.h"
|
||||||
|
|
||||||
namespace xe {
|
namespace xe {
|
||||||
namespace debugging {
|
namespace debugging {
|
||||||
|
|
||||||
bool IsDebuggerAttached() { return false; }
|
bool IsDebuggerAttached() {
|
||||||
void Break() { raise(SIGTRAP); }
|
std::ifstream proc_status_stream("/proc/self/status");
|
||||||
|
if (!proc_status_stream.is_open()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
std::string line;
|
||||||
|
while (std::getline(proc_status_stream, line)) {
|
||||||
|
std::istringstream line_stream(line);
|
||||||
|
std::string key;
|
||||||
|
line_stream >> key;
|
||||||
|
if (key == "TracerPid:") {
|
||||||
|
uint32_t tracer_pid;
|
||||||
|
line_stream >> tracer_pid;
|
||||||
|
return tracer_pid != 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Break() {
|
||||||
|
static std::once_flag flag;
|
||||||
|
std::call_once(flag, []() {
|
||||||
|
// Install handler for sigtrap only once
|
||||||
|
std::signal(SIGTRAP, [](int) {
|
||||||
|
// Forward signal to default handler after being caught
|
||||||
|
std::signal(SIGTRAP, SIG_DFL);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
std::raise(SIGTRAP);
|
||||||
|
}
|
||||||
|
|
||||||
namespace internal {
|
namespace internal {
|
||||||
void DebugPrint(const char* s) {
|
void DebugPrint(const char* s) { std::clog << s << std::endl; }
|
||||||
// TODO: proper implementation.
|
|
||||||
}
|
|
||||||
} // namespace internal
|
} // namespace internal
|
||||||
|
|
||||||
} // namespace debugging
|
} // namespace debugging
|
||||||
|
|
Loading…
Reference in New Issue