1 /*
2    Copyright (c) 2001, 2011, Oracle and/or its affiliates.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; version 2 of the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335  USA */
16 
17 #ifndef _my_stacktrace_h_
18 #define _my_stacktrace_h_
19 
20 #ifdef TARGET_OS_LINUX
21 #if defined (__x86_64__) || defined (__i386__) || \
22     (defined(__alpha__) && defined(__GNUC__))
23 #define HAVE_STACKTRACE 1
24 #endif
25 #elif defined(__WIN__) || defined(HAVE_PRINTSTACK)
26 #define HAVE_STACKTRACE 1
27 #endif
28 
29 #if HAVE_BACKTRACE && (HAVE_BACKTRACE_SYMBOLS || HAVE_BACKTRACE_SYMBOLS_FD)
30 #undef HAVE_STACKTRACE
31 #define HAVE_STACKTRACE 1
32 #endif
33 
34 #define HAVE_WRITE_CORE
35 
36 #if HAVE_BACKTRACE && HAVE_BACKTRACE_SYMBOLS && HAVE_ABI_CXA_DEMANGLE && \
37     HAVE_WEAK_SYMBOL
38 #define BACKTRACE_DEMANGLE 1
39 #endif
40 
41 C_MODE_START
42 
43 #if defined(HAVE_STACKTRACE) || defined(HAVE_BACKTRACE)
44 void my_print_stacktrace(uchar* stack_bottom, ulong thread_stack,
45                          my_bool silent);
46 int my_safe_print_str(const char* val, size_t max_len);
47 void my_write_core(int sig);
48 #if BACKTRACE_DEMANGLE
49 char *my_demangle(const char *mangled_name, int *status);
50 #endif /* BACKTRACE_DEMANGLE */
51 #ifdef __WIN__
52 void my_set_exception_pointers(EXCEPTION_POINTERS *ep);
53 #endif /* __WIN__ */
54 #endif /* ! (defined(HAVE_STACKTRACE) || defined(HAVE_BACKTRACE)) */
55 
56 #ifndef _WIN32
57 #define MY_ADDR_RESOLVE_FORK
58 #endif
59 
60 #if defined(HAVE_BFD_H) || defined(MY_ADDR_RESOLVE_FORK)
61 #define HAVE_MY_ADDR_RESOLVE 1
62 #endif
63 
64 typedef struct {
65   const char *file;
66   const char *func;
67   uint line;
68 } my_addr_loc;
69 
70 #ifdef HAVE_MY_ADDR_RESOLVE
71 int my_addr_resolve(void *ptr, my_addr_loc *loc);
72 const char *my_addr_resolve_init();
73 #else
74 #define my_addr_resolve_init()  (0)
75 #define my_addr_resolve(A,B)    (1)
76 #endif
77 
78 #ifdef HAVE_WRITE_CORE
79 void my_write_core(int sig);
80 #endif
81 
82 /**
83   A (very) limited version of snprintf, which writes the result to STDERR.
84   @sa my_safe_snprintf
85   Implemented with simplicity, and async-signal-safety in mind.
86   @note Has an internal buffer capacity of 512 bytes,
87   which should suffice for our signal handling routines.
88 */
89 size_t my_safe_printf_stderr(const char* fmt, ...)
90   ATTRIBUTE_FORMAT(printf, 1, 2);
91 
92 /**
93   Writes up to count bytes from buffer to STDERR.
94   Implemented with simplicity, and async-signal-safety in mind.
95   @param   buf   Buffer containing data to be written.
96   @param   count Number of bytes to write.
97   @returns Number of bytes written.
98 */
99 size_t my_write_stderr(const void *buf, size_t count);
100 
101 C_MODE_END
102 
103 #endif /* _my_stacktrace_h_ */
104