| 1 | /* Copyright (C) 2019 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 <cstdio>
|
|---|
| 33 | #include <sys/types.h>
|
|---|
| 34 | #include <signal.h>
|
|---|
| 35 |
|
|---|
| 36 | #include "lib/timer.h"
|
|---|
| 37 | #include "lib/sysdep/sysdep.h"
|
|---|
| 38 | #include "lib/debug.h"
|
|---|
| 39 | #include "lib/utf8.h"
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 | Status debug_CaptureContext(void* UNUSED(context))
|
|---|
| 43 | {
|
|---|
| 44 | // (not needed unless/until we support stack traces)
|
|---|
| 45 | return INFO::SKIPPED;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | void debug_break()
|
|---|
| 49 | {
|
|---|
| 50 | #ifndef NDEBUG
|
|---|
| 51 | raise(SIGTRAP);
|
|---|
| 52 | #endif
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | #define DEBUGGER_WAIT 3
|
|---|
| 56 | #define DEBUGGER_CMD "gdb"
|
|---|
| 57 | #define DEBUGGER_ARG_FORMAT "--pid=%d"
|
|---|
| 58 | #define DEBUGGER_BREAK_AFTER_WAIT 0
|
|---|
| 59 |
|
|---|
| 60 | /*
|
|---|
| 61 | Start the debugger and tell it to attach to the current process/thread
|
|---|
| 62 | (called by display_error)
|
|---|
| 63 | */
|
|---|
| 64 | void udbg_launch_debugger()
|
|---|
| 65 | {
|
|---|
| 66 | pid_t orgpid=getpid();
|
|---|
| 67 | pid_t ret=fork();
|
|---|
| 68 | if (ret == 0)
|
|---|
| 69 | {
|
|---|
| 70 | // Child Process: exec() gdb (Debugger), set to attach to old fork
|
|---|
| 71 | char buf[16];
|
|---|
| 72 | snprintf(buf, 16, DEBUGGER_ARG_FORMAT, orgpid);
|
|---|
| 73 |
|
|---|
| 74 | int ret=execlp(DEBUGGER_CMD, DEBUGGER_CMD, buf, NULL);
|
|---|
| 75 | // In case of success, we should never get here anyway, though...
|
|---|
| 76 | if (ret != 0)
|
|---|
| 77 | {
|
|---|
| 78 | perror("Debugger launch failed");
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|
| 81 | else if (ret > 0)
|
|---|
| 82 | {
|
|---|
| 83 | // Parent (original) fork:
|
|---|
| 84 | debug_printf("Sleeping until debugger attaches.\nPlease wait.\n");
|
|---|
| 85 | sleep(DEBUGGER_WAIT);
|
|---|
| 86 | }
|
|---|
| 87 | else // fork error, ret == -1
|
|---|
| 88 | {
|
|---|
| 89 | perror("Debugger launch: fork failed");
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | #if OS_ANDROID
|
|---|
| 94 |
|
|---|
| 95 | #include <android/log.h>
|
|---|
| 96 |
|
|---|
| 97 | void debug_puts(const char* text)
|
|---|
| 98 | {
|
|---|
| 99 | __android_log_print(ANDROID_LOG_WARN, "pyrogenesis", "%s", text);
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | #else
|
|---|
| 103 |
|
|---|
| 104 | void debug_puts(const char* text)
|
|---|
| 105 | {
|
|---|
| 106 | printf("%s", text);
|
|---|
| 107 | fflush(stdout);
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | #endif
|
|---|
| 111 |
|
|---|
| 112 | int debug_IsPointerBogus(const void* UNUSED(p))
|
|---|
| 113 | {
|
|---|
| 114 | // TODO: maybe this should do some checks
|
|---|
| 115 | return false;
|
|---|
| 116 | }
|
|---|