Line data Source code
1 : /* Copyright (C) 2025 Wildfire Games.
2 : *
3 : * Permission is hereby granted, free of charge, to any person obtaining
4 : * a copy of this software and associated documentation files (the
5 : * "Software"), to deal in the Software without restriction, including
6 : * without limitation the rights to use, copy, modify, merge, publish,
7 : * distribute, sublicense, and/or sell copies of the Software, and to
8 : * permit persons to whom the Software is furnished to do so, subject to
9 : * the following conditions:
10 : *
11 : * The above copyright notice and this permission notice shall be included
12 : * in all copies or substantial portions of the Software.
13 : *
14 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 : * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 : * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 : * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 : * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 : * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 : * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 : */
22 :
23 : /* udbg.cpp
24 :
25 : This file contains debug helpers that are common for all unix systems. See
26 : linux/ldbg.cpp for the linux-specific stuff (Using BFD and backtrace() for
27 : symbol lookups and backtraces)
28 : */
29 :
30 : #include "precompiled.h"
31 :
32 : #include "lib/debug.h"
33 :
34 : #include "lib/posix/posix_types.h"
35 : #include "lib/status.h"
36 : #include "lib/sysdep/os.h"
37 :
38 : #include <cstdio>
39 :
40 : #ifndef NDEBUG
41 : # include <signal.h>
42 : #endif
43 :
44 :
45 29 : Status debug_CaptureContext(void* /*context*/)
46 : {
47 : // (not needed unless/until we support stack traces)
48 29 : return INFO::SKIPPED;
49 : }
50 :
51 0 : void debug_break()
52 : {
53 : #ifndef NDEBUG
54 : raise(SIGTRAP);
55 : #endif
56 0 : }
57 :
58 : #define DEBUGGER_WAIT 3
59 : #define DEBUGGER_CMD "gdb"
60 : #define DEBUGGER_ARG_FORMAT "--pid=%d"
61 : #define DEBUGGER_BREAK_AFTER_WAIT 0
62 :
63 : /*
64 : Start the debugger and tell it to attach to the current process/thread
65 : (called by display_error)
66 : */
67 0 : void udbg_launch_debugger()
68 : {
69 0 : pid_t orgpid=getpid();
70 0 : pid_t ret=fork();
71 0 : if (ret == 0)
72 : {
73 : // Child Process: exec() gdb (Debugger), set to attach to old fork
74 0 : char buf[16];
75 0 : snprintf(buf, 16, DEBUGGER_ARG_FORMAT, orgpid);
76 :
77 0 : int ret=execlp(DEBUGGER_CMD, DEBUGGER_CMD, buf, NULL);
78 : // In case of success, we should never get here anyway, though...
79 0 : if (ret != 0)
80 : {
81 0 : perror("Debugger launch failed");
82 : }
83 : }
84 0 : else if (ret > 0)
85 : {
86 : // Parent (original) fork:
87 0 : debug_printf("Sleeping until debugger attaches.\nPlease wait.\n");
88 0 : sleep(DEBUGGER_WAIT);
89 : }
90 : else // fork error, ret == -1
91 : {
92 0 : perror("Debugger launch: fork failed");
93 : }
94 0 : }
95 :
96 : #if OS_ANDROID
97 :
98 : #include <android/log.h>
99 :
100 : void debug_puts(const char* text)
101 : {
102 : __android_log_print(ANDROID_LOG_WARN, "pyrogenesis", "%s", text);
103 : }
104 :
105 : #else
106 :
107 0 : void debug_puts(const char* text)
108 : {
109 0 : printf("%s", text);
110 0 : fflush(stdout);
111 0 : }
112 :
113 : #endif
114 :
115 0 : int debug_IsPointerBogus(const void*)
116 : {
117 : // TODO: maybe this should do some checks
118 0 : return false;
119 : }
|