1 /* Copyright (c) 2001, 2021, Oracle and/or its affiliates.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 #ifndef _my_stacktrace_h_
24 #define _my_stacktrace_h_
25 
26 #include <my_global.h>
27 
28 C_MODE_START
29 
30 /*
31   HAVE_BACKTRACE - Linux, FreeBSD, OSX
32   HAVE_PRINTSTACK - Solaris
33   _WIN32 - Windows
34 */
35 #if defined(HAVE_BACKTRACE) || defined(HAVE_PRINTSTACK) || defined(_WIN32)
36 #define HAVE_STACKTRACE 1
37 void my_init_stacktrace();
38 void my_print_stacktrace(uchar* stack_bottom, ulong thread_stack);
39 void my_safe_puts_stderr(const char* val, size_t max_len);
40 #if HAVE_BACKTRACE && HAVE_ABI_CXA_DEMANGLE
41 char *my_demangle(const char *mangled_name, int *status);
42 #endif
43 #ifdef _WIN32
44 void my_set_exception_pointers(EXCEPTION_POINTERS *ep);
45 void my_create_minidump(const char *name, HANDLE process, DWORD pid);
46 #endif
47 #endif /* HAVE_BACKTRACE || HAVE_PRINTSTACK || _WIN32 */
48 
49 void my_write_core(int sig);
50 #if HAVE_LIBCOREDUMPER
51 void my_write_libcoredumper(int sig, char *path, time_t curr_time);
52 #endif
53 #ifdef __linux__
54 void my_print_buildID();
55 #endif
56 
57 /**
58   Async-signal-safe utility functions used by signal handler routines.
59   Declared here in order to unit-test them.
60   These are not general-purpose, but tailored to the signal handling routines.
61 */
62 /**
63   Converts a longlong value to string.
64   @param   base 10 for decimal, 16 for hex values (0..9a..f)
65   @param   val  The value to convert
66   @param   buf  Assumed to point to the *end* of the buffer.
67   @returns Pointer to the first character of the converted string.
68            Negative values:
69            for base-10 the return string will be prepended with '-'
70            for base-16 the return string will contain 16 characters
71   Implemented with simplicity, and async-signal-safety in mind.
72 */
73 char *my_safe_itoa(int base, longlong val, char *buf);
74 
75 /**
76   Converts a ulonglong value to string.
77   @param   base 10 for decimal, 16 for hex values (0..9a..f)
78   @param   val  The value to convert
79   @param   buf  Assumed to point to the *end* of the buffer.
80   @returns Pointer to the first character of the converted string.
81   Implemented with simplicity, and async-signal-safety in mind.
82 */
83 char *my_safe_utoa(int base, ulonglong val, char *buf);
84 
85 /**
86   A (very) limited version of snprintf.
87   @param   to   Destination buffer.
88   @param   n    Size of destination buffer.
89   @param   fmt  printf() style format string.
90   @returns Number of bytes written, including terminating '\0'
91   Supports 'd' 'i' 'u' 'x' 'p' 's' conversion.
92   Supports 'l' and 'll' modifiers for integral types.
93   Does not support any width/precision.
94   Implemented with simplicity, and async-signal-safety in mind.
95 */
96 size_t my_safe_snprintf(char* to, size_t n, const char* fmt, ...)
97   MY_ATTRIBUTE((format(printf, 3, 4)));
98 
99 /**
100   A (very) limited version of snprintf, which writes the result to STDERR.
101   @sa my_safe_snprintf
102   Implemented with simplicity, and async-signal-safety in mind.
103   @note Has an internal buffer capacity of 512 bytes,
104   which should suffice for our signal handling routines.
105 */
106 size_t my_safe_printf_stderr(const char* fmt, ...)
107   MY_ATTRIBUTE((format(printf, 1, 2)));
108 
109 /**
110   Writes up to count bytes from buffer to STDERR.
111   Implemented with simplicity, and async-signal-safety in mind.
112   @param   buf   Buffer containing data to be written.
113   @param   count Number of bytes to write.
114   @returns Number of bytes written.
115 */
116 size_t my_write_stderr(const void *buf, size_t count);
117 
118 /**
119   Writes system time to STDERR without allocating new memory.
120 */
121 void my_safe_print_system_time();
122 
123 C_MODE_END
124 
125 #endif /* _my_stacktrace_h_ */
126