Skip to content

Commit 2807ad6

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 2807ad6

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

src/xenia/base/debugging_posix.cc

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,49 @@
77
******************************************************************************
88
*/
99

10-
#include "xenia/base/debugging.h"
11-
12-
#include <signal.h>
10+
#include <csignal>
1311
#include <cstdarg>
12+
#include <fstream>
13+
#include <iostream>
14+
#include <mutex>
15+
#include <sstream>
1416

17+
#include "xenia/base/debugging.h"
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+
void Break() {
43+
static std::once_flag flag;
44+
std::call_once(flag, []() {
45+
// Install handler for sigtrap only once
46+
std::signal(SIGTRAP, [](int) {
47+
// Forward signal to default handler after being caught
48+
std::signal(SIGTRAP, SIG_DFL);
49+
});
50+
});
51+
raise(SIGTRAP);
52+
}
2253

2354
void DebugPrint(const char* fmt, ...) {
2455
StringBuffer buff;
@@ -28,7 +59,7 @@ void DebugPrint(const char* fmt, ...) {
2859
buff.AppendVarargs(fmt, va);
2960
va_end(va);
3061

31-
// OutputDebugStringA(buff.GetString());
62+
std::clog << buff.GetString() << std::endl;
3263
}
3364

3465
} // namespace debugging

0 commit comments

Comments
 (0)