Skip to content

Commit 4e9d123

Browse files
committed
[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).
1 parent cf0251c commit 4e9d123

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

src/xenia/base/debugging_posix.cc

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,51 @@
99

1010
#include "xenia/base/debugging.h"
1111

12-
#include <signal.h>
12+
#include <csignal>
1313
#include <cstdarg>
14+
#include <fstream>
15+
#include <iostream>
16+
#include <mutex>
17+
#include <sstream>
1418

1519
#include "xenia/base/string_buffer.h"
1620

1721
namespace xe {
1822
namespace debugging {
1923

20-
bool IsDebuggerAttached() { return false; }
21-
void Break() { raise(SIGTRAP); }
24+
bool IsDebuggerAttached() {
25+
std::ifstream proc_status_stream("/proc/self/status");
26+
if (!proc_status_stream.is_open()) {
27+
return false;
28+
}
29+
std::string line;
30+
while (std::getline(proc_status_stream, line)) {
31+
std::istringstream line_stream(line);
32+
std::string key;
33+
line_stream >> key;
34+
if (key == "TracerPid:") {
35+
uint32_t tracer_pid;
36+
line_stream >> tracer_pid;
37+
return tracer_pid != 0;
38+
}
39+
}
40+
return false;
41+
}
2242

23-
namespace internal {
24-
void DebugPrint(const char* s) {
25-
// TODO: proper implementation.
43+
void Break() {
44+
static std::once_flag flag;
45+
std::call_once(flag, []() {
46+
// Install handler for sigtrap only once
47+
std::signal(SIGTRAP, [](int) {
48+
// Forward signal to default handler after being caught
49+
std::signal(SIGTRAP, SIG_DFL);
50+
});
51+
});
52+
std::raise(SIGTRAP);
2653
}
54+
55+
namespace internal {
56+
void DebugPrint(const char* s) { std::clog << s << std::endl; }
2757
} // namespace internal
2858

2959
} // namespace debugging

0 commit comments

Comments
 (0)