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-1301 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
41C_MODE_START
42
43#if defined(HAVE_STACKTRACE) || defined(HAVE_BACKTRACE)
44void my_init_stacktrace();
45void my_print_stacktrace(uchar* stack_bottom, ulong thread_stack,
46 my_bool silent);
47int my_safe_print_str(const char* val, size_t max_len);
48void my_write_core(int sig);
49#if BACKTRACE_DEMANGLE
50char *my_demangle(const char *mangled_name, int *status);
51#endif /* BACKTRACE_DEMANGLE */
52#ifdef __WIN__
53void my_set_exception_pointers(EXCEPTION_POINTERS *ep);
54#endif /* __WIN__ */
55#else
56#define my_init_stacktrace() do { } while(0)
57#endif /* ! (defined(HAVE_STACKTRACE) || defined(HAVE_BACKTRACE)) */
58
59#ifndef _WIN32
60#define MY_ADDR_RESOLVE_FORK
61#endif
62
63#if defined(HAVE_BFD_H) || defined(MY_ADDR_RESOLVE_FORK)
64#define HAVE_MY_ADDR_RESOLVE 1
65#endif
66
67typedef struct {
68 const char *file;
69 const char *func;
70 uint line;
71} my_addr_loc;
72
73#ifdef HAVE_MY_ADDR_RESOLVE
74int my_addr_resolve(void *ptr, my_addr_loc *loc);
75const char *my_addr_resolve_init();
76#else
77#define my_addr_resolve_init() (0)
78#define my_addr_resolve(A,B) (1)
79#endif
80
81#ifdef HAVE_WRITE_CORE
82void my_write_core(int sig);
83#endif
84
85/**
86 A (very) limited version of snprintf, which writes the result to STDERR.
87 @sa my_safe_snprintf
88 Implemented with simplicity, and async-signal-safety in mind.
89 @note Has an internal buffer capacity of 512 bytes,
90 which should suffice for our signal handling routines.
91*/
92size_t my_safe_printf_stderr(const char* fmt, ...)
93 ATTRIBUTE_FORMAT(printf, 1, 2);
94
95/**
96 Writes up to count bytes from buffer to STDERR.
97 Implemented with simplicity, and async-signal-safety in mind.
98 @param buf Buffer containing data to be written.
99 @param count Number of bytes to write.
100 @returns Number of bytes written.
101*/
102size_t my_write_stderr(const void *buf, size_t count);
103
104C_MODE_END
105
106#endif /* _my_stacktrace_h_ */
107