diagnostics.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // This file is part of BOINC.
  2. // http://boinc.berkeley.edu
  3. // Copyright (C) 2008 University of California
  4. //
  5. // BOINC is free software; you can redistribute it and/or modify it
  6. // under the terms of the GNU Lesser General Public License
  7. // as published by the Free Software Foundation,
  8. // either version 3 of the License, or (at your option) any later version.
  9. //
  10. // BOINC is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. // See the GNU Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public License
  16. // along with BOINC. If not, see <http://www.gnu.org/licenses/>.
  17. #ifndef BOINC_DIAGNOSTICS_H
  18. #define BOINC_DIAGNOSTICS_H
  19. #ifdef _WIN32
  20. #include "boinc_win.h"
  21. #else
  22. #include <signal.h>
  23. #ifdef __cplusplus
  24. #include <cassert>
  25. #else
  26. #include <assert.h>
  27. #endif
  28. #endif
  29. #ifdef HAVE_DLFCN_H
  30. #include <dlfcn.h>
  31. #endif
  32. extern bool main_exited;
  33. // some of the Android stuff below causes seg faults on some devices.
  34. // Disable by default.
  35. // Set this to enable it.
  36. //
  37. //#define ANDROID_VOODOO
  38. // flags for boinc_init_diagnostics()
  39. //
  40. #define BOINC_DIAG_DUMPCALLSTACKENABLED 0x00000001L
  41. #define BOINC_DIAG_HEAPCHECKENABLED 0x00000002L
  42. #define BOINC_DIAG_MEMORYLEAKCHECKENABLED 0x00000004L
  43. #define BOINC_DIAG_ARCHIVESTDERR 0x00000008L
  44. #define BOINC_DIAG_ARCHIVESTDOUT 0x00000010L
  45. #define BOINC_DIAG_REDIRECTSTDERR 0x00000020L
  46. #define BOINC_DIAG_REDIRECTSTDOUT 0x00000040L
  47. #define BOINC_DIAG_REDIRECTSTDERROVERWRITE 0x00000080L
  48. #define BOINC_DIAG_REDIRECTSTDOUTOVERWRITE 0x00000100L
  49. #define BOINC_DIAG_TRACETOSTDERR 0x00000200L
  50. #define BOINC_DIAG_TRACETOSTDOUT 0x00000400L
  51. #define BOINC_DIAG_HEAPCHECKEVERYALLOC 0x00000800L
  52. #define BOINC_DIAG_BOINCAPPLICATION 0x00001000L
  53. #define BOINC_DIAG_PERUSERLOGFILES 0x00002000L
  54. #define BOINC_DIAG_DEFAULTS \
  55. BOINC_DIAG_DUMPCALLSTACKENABLED | \
  56. BOINC_DIAG_HEAPCHECKENABLED | \
  57. BOINC_DIAG_MEMORYLEAKCHECKENABLED | \
  58. BOINC_DIAG_REDIRECTSTDERR | \
  59. BOINC_DIAG_TRACETOSTDERR
  60. // filenames
  61. //
  62. #define BOINC_DIAG_STDERR "stderr"
  63. #define BOINC_DIAG_STDOUT "stdout"
  64. #define BOINC_DIAG_GFX_STDERR "stderrgfx"
  65. #define BOINC_DIAG_GFX_STDOUT "stdoutgfx"
  66. #ifdef __cplusplus
  67. extern "C" {
  68. #endif
  69. // These are functions common to all platforms
  70. extern int boinc_init_diagnostics( int flags );
  71. extern int boinc_init_graphics_diagnostics( int flags );
  72. extern int boinc_install_signal_handlers(void);
  73. extern int boinc_finish_diag(void);
  74. extern int diagnostics_init(
  75. int flags, const char* stdout_prefix, const char* stderr_prefix
  76. );
  77. extern int diagnostics_thread_init(void);
  78. extern int diagnostics_finish(void);
  79. extern int diagnostics_is_initialized(void);
  80. extern int diagnostics_is_flag_set(int flags);
  81. // Properties
  82. extern char* diagnostics_get_boinc_dir(void);
  83. extern char* diagnostics_get_boinc_install_dir(void);
  84. extern char* diagnostics_get_symstore(void);
  85. extern int diagnostics_set_symstore(char* symstore);
  86. extern int diagnostics_is_proxy_enabled(void);
  87. extern char* diagnostics_get_proxy(void);
  88. extern int diagnostics_is_aborted_via_gui(void);
  89. extern int diagnostics_set_aborted_via_gui(void);
  90. // Log rotation
  91. extern int diagnostics_cycle_logs(void);
  92. extern void diagnostics_set_max_file_sizes(double stdout_size, double stderr_size);
  93. // Thread Tracking
  94. extern int diagnostics_init_thread_list(void);
  95. extern int diagnostics_finish_thread_list(void);
  96. extern int diagnostics_update_thread_list(void);
  97. extern int diagnostics_set_thread_exempt_suspend(void);
  98. extern int diagnostics_is_thread_exempt_suspend(long thread_id);
  99. // Message Monitoring (debugger viewport)
  100. extern int diagnostics_init_message_monitor(void);
  101. extern int diagnostics_finish_message_monitor(void);
  102. #ifdef _WIN32
  103. extern UINT WINAPI diagnostics_message_monitor(LPVOID lpParameter);
  104. #endif
  105. extern int diagnostics_trace_to_debugger(const char* msg);
  106. // Unhandled exception monitor
  107. extern int diagnostics_init_unhandled_exception_monitor(void);
  108. extern int diagnostics_finish_unhandled_exception_monitor(void);
  109. #ifdef _WIN32
  110. extern UINT WINAPI diagnostics_unhandled_exception_monitor(LPVOID lpParameter);
  111. extern LONG CALLBACK boinc_catch_signal(EXCEPTION_POINTERS *ExceptionInfo);
  112. #else
  113. #ifdef HAVE_SIGACTION
  114. typedef void (*handler_t)(int, siginfo_t*, void *);
  115. extern void boinc_catch_signal(int signal, siginfo_t *siginfo, void *sigcontext);
  116. #else
  117. typedef void (*handler_t)(int);
  118. extern void boinc_catch_signal(int signal);
  119. #endif
  120. extern void boinc_set_signal_handler(int sig, handler_t handler);
  121. extern void boinc_set_signal_handler_force(int sig, handler_t handler);
  122. #endif
  123. // These functions are used to log the various messages that are
  124. // defined in the BOINC Diagnostics Library
  125. extern void boinc_trace(const char *pszFormat, ...);
  126. extern void boinc_info(const char *pszFormat, ...);
  127. extern void set_signal_exit_code(int);
  128. #ifdef __cplusplus
  129. }
  130. #endif
  131. #ifdef ANDROID_VOODOO
  132. // Yes, these are undocumented android functions located
  133. // libcorkscrew.so . They may not always be there, but it's better than
  134. // nothing. And we've got source so we could reimplement them if necessary.
  135. extern const char *argv0;
  136. typedef struct map_info_t map_info_t;
  137. typedef struct {
  138. uintptr_t absolute_pc;
  139. uintptr_t stack_top;
  140. size_t stack_size;
  141. } backtrace_frame_t;
  142. typedef struct {
  143. uintptr_t relative_pc;
  144. uintptr_t relative_symbol_addr;
  145. char* map_name;
  146. char* symbol_name;
  147. char* demangled_name;
  148. } backtrace_symbol_t;
  149. typedef struct {
  150. uintptr_t start;
  151. uintptr_t end;
  152. char* name;
  153. } symbol_t;
  154. typedef struct {
  155. symbol_t* symbols;
  156. size_t num_symbols;
  157. } symbol_table_t;
  158. typedef ssize_t (*unwind_backtrace_signal_arch_t)(
  159. siginfo_t *, void *, const map_info_t *, backtrace_frame_t *,
  160. size_t , size_t
  161. );
  162. extern unwind_backtrace_signal_arch_t unwind_backtrace_signal_arch;
  163. typedef map_info_t *(*acquire_my_map_info_list_t)();
  164. extern acquire_my_map_info_list_t acquire_my_map_info_list;
  165. typedef void (*release_my_map_info_list_t)(map_info_t *);
  166. extern release_my_map_info_list_t release_my_map_info_list;
  167. typedef void (*get_backtrace_symbols_t)(
  168. const backtrace_frame_t *, size_t, backtrace_symbol_t *
  169. );
  170. extern get_backtrace_symbols_t get_backtrace_symbols;
  171. typedef void (*free_backtrace_symbols_t)(backtrace_symbol_t* symbols,
  172. size_t frames);
  173. extern free_backtrace_symbols_t free_backtrace_symbols;
  174. typedef symbol_table_t *(*load_symbol_table_t)(const char *);
  175. extern load_symbol_table_t load_symbol_table;
  176. typedef void (*free_symbol_table_t)(symbol_table_t *);
  177. extern free_symbol_table_t free_symbol_table;
  178. typedef symbol_t *(*find_symbol_t)(const symbol_table_t *, uintptr_t );
  179. extern find_symbol_t find_symbol;
  180. typedef void (* format_backtrace_line_t)(unsigned, const backtrace_frame_t *, const backtrace_symbol_t *, char *, size_t);
  181. extern format_backtrace_line_t format_backtrace_line;
  182. #endif // ANDROID_VOODOO
  183. #ifdef _WIN32
  184. // Define macros for both debug and release builds.
  185. //
  186. // We are using the native debugging technology built into the Microsoft
  187. // C Runtime Libraries to trap and report the asserts and traces.
  188. //
  189. #ifdef _DEBUG
  190. #if defined(WXDEBUG) || defined(WXNDEBUG)
  191. // wxWidgets UI Framework
  192. //
  193. #define BOINCASSERT(expr) wxASSERT(expr)
  194. #ifdef _UNICODE
  195. #define BOINCTRACE __noop
  196. #else
  197. #define BOINCTRACE wxLogDebug
  198. #endif
  199. #elif defined(_CONSOLE) && !(defined(__MINGW32__) || defined(__CYGWIN32__))
  200. // Microsoft CRT
  201. //
  202. #define BOINCASSERT(expr) _ASSERTE(expr)
  203. #define BOINCTRACE boinc_trace
  204. #endif // _CONSOLE
  205. #else // _DEBUG
  206. #if defined(__MINGW32__) || defined(__CYGWIN32__)
  207. #define BOINCASSERT(expr) assert(expr)
  208. #define BOINCTRACE boinc_trace
  209. #else // __MINGW32__
  210. #define BOINCASSERT(expr) __noop
  211. #define BOINCTRACE __noop
  212. #endif // __MINGW32__
  213. #endif // _DEBUG
  214. #else
  215. #ifdef _DEBUG
  216. // Standard Frameworks
  217. //
  218. #define BOINCASSERT assert
  219. #define BOINCTRACE boinc_trace
  220. #else // _DEBUG
  221. #define BOINCASSERT(expr)
  222. #ifndef IRIX
  223. #if defined(__MINGW32__) || defined(__CYGWIN32__)
  224. #define BOINCTRACE
  225. #else
  226. #define BOINCTRACE(...)
  227. #endif
  228. #endif
  229. #endif // _DEBUG
  230. #endif // ! _WIN32
  231. // If none of the TRACE and INFO macros have been defined for
  232. // any existing framework to plug into, null them out.
  233. // ASSERT is a special case and should point to the CRT version
  234. // if it hasn't already been redirected.
  235. #ifndef BOINCASSERT
  236. #define BOINCASSERT assert
  237. #endif
  238. #ifndef BOINCTRACE
  239. #define BOINCTRACE
  240. #endif
  241. #ifndef BOINCINFO
  242. #define BOINCINFO boinc_info
  243. #endif
  244. #endif