my_stacktrace.h 4.4 KB

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