Skip to content

Commit 46fabca

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 1cce25d commit 46fabca

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

src/xenia/base/debugging_posix.cc

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,46 @@
1111

1212
#include <signal.h>
1313
#include <cstdarg>
14+
#include <fstream>
15+
#include <iostream>
16+
#include <sstream>
1417

1518
#include "xenia/base/string_buffer.h"
1619

1720
namespace xe {
1821
namespace debugging {
1922

20-
bool IsDebuggerAttached() { return false; }
21-
void Break() { raise(SIGTRAP); }
23+
bool IsDebuggerAttached() {
24+
std::ifstream proc_status_stream("/proc/self/status");
25+
if (proc_status_stream.is_open()) {
26+
return false;
27+
}
28+
std::string line;
29+
while (std::getline(proc_status_stream, line)) {
30+
std::istringstream line_stream(line);
31+
std::string key;
32+
line_stream >> key;
33+
if (key == "TracerPid:") {
34+
uint32_t tracer_pid;
35+
line_stream >> tracer_pid;
36+
return tracer_pid != 0;
37+
}
38+
}
39+
return false;
40+
}
41+
42+
static bool SigTrapInstalled = false;
43+
static void SigTrapHandler(int signum) {
44+
signal(SIGTRAP, SIG_DFL);
45+
SigTrapInstalled = true;
46+
}
47+
48+
void Break() {
49+
if (!SigTrapInstalled) {
50+
signal(SIGTRAP, SigTrapHandler);
51+
}
52+
raise(SIGTRAP);
53+
}
2254

2355
void DebugPrint(const char* fmt, ...) {
2456
StringBuffer buff;
@@ -28,7 +60,7 @@ void DebugPrint(const char* fmt, ...) {
2860
buff.AppendVarargs(fmt, va);
2961
va_end(va);
3062

31-
// OutputDebugStringA(buff.GetString());
63+
std::clog << buff.GetString() << std::endl;
3264
}
3365

3466
} // namespace debugging

0 commit comments

Comments
 (0)