Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
udbg.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2010 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 
43 {
44  // (not needed unless/until we support stack traces)
45  return INFO::SKIPPED;
46 }
47 
49 {
50  kill(getpid(), SIGTRAP);
51 }
52 
53 #define DEBUGGER_WAIT 3
54 #define DEBUGGER_CMD "gdb"
55 #define DEBUGGER_ARG_FORMAT "--pid=%d"
56 #define DEBUGGER_BREAK_AFTER_WAIT 0
57 
58 /*
59 Start the debugger and tell it to attach to the current process/thread
60 (called by display_error)
61 */
63 {
64  pid_t orgpid=getpid();
65  pid_t ret=fork();
66  if (ret == 0)
67  {
68  // Child Process: exec() gdb (Debugger), set to attach to old fork
69  char buf[16];
70  snprintf(buf, 16, DEBUGGER_ARG_FORMAT, orgpid);
71 
72  int ret=execlp(DEBUGGER_CMD, DEBUGGER_CMD, buf, NULL);
73  // In case of success, we should never get here anyway, though...
74  if (ret != 0)
75  {
76  perror("Debugger launch failed");
77  }
78  }
79  else if (ret > 0)
80  {
81  // Parent (original) fork:
82  debug_printf(L"Sleeping until debugger attaches.\nPlease wait.\n");
84  }
85  else // fork error, ret == -1
86  {
87  perror("Debugger launch: fork failed");
88  }
89 }
90 
91 #if OS_ANDROID
92 
93 #include <android/log.h>
94 
95 void debug_puts(const wchar_t* text)
96 {
97  // The Android logger doesn't like "%ls" format strings, so convert
98  // the message to UTF-8 before outputting
99  Status err;
100  std::string str = utf8_from_wstring(text, &err);
101  __android_log_print(ANDROID_LOG_WARN, "pyrogenesis", "%s", str.c_str());
102 }
103 
104 #else
105 
106 void debug_puts(const wchar_t* text)
107 {
108  printf("%ls", text); // must not use printf, since stdout is byte-oriented
109  fflush(stdout);
110 }
111 
112 #endif
113 
114 int debug_IsPointerBogus(const void* UNUSED(p))
115 {
116  // TODO: maybe this should do some checks
117  return false;
118 }
LIB_API void debug_puts(const wchar_t *text)
[system-dependent] write a string to the debug channel.
Definition: udbg.cpp:106
#define DEBUGGER_CMD
Definition: udbg.cpp:54
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
unsigned sleep(unsigned sec)
Definition: wtime.cpp:151
std::string utf8_from_wstring(const std::wstring &src, Status *err)
opposite of wstring_from_utf8
Definition: utf8.cpp:208
#define DEBUGGER_WAIT
Definition: udbg.cpp:53
LIB_API Status debug_CaptureContext(void *context)
Definition: udbg.cpp:42
LIB_API int debug_IsPointerBogus(const void *p)
check if a pointer appears to be totally invalid.
Definition: udbg.cpp:114
i64 Status
Error handling system.
Definition: status.h:171
void debug_break()
trigger a breakpoint when reached/&quot;called&quot;.
Definition: udbg.cpp:48
const Status SKIPPED
Definition: status.h:392
#define DEBUGGER_ARG_FORMAT
Definition: udbg.cpp:55
void udbg_launch_debugger()
Definition: udbg.cpp:62
void debug_printf(const wchar_t *fmt,...)
write a formatted string to the debug channel, subject to filtering (see below).
Definition: debug.cpp:142