terminate_handler.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // Copyright Antony Polukhin, 2016-2019.
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/array.hpp>
  7. BOOST_NOINLINE void foo(int i);
  8. BOOST_NOINLINE void bar(int i);
  9. BOOST_NOINLINE void bar(int i) {
  10. boost::array<int, 5> a = {{-1, -231, -123, -23, -32}};
  11. if (i >= 0) {
  12. foo(a[i]);
  13. } else {
  14. std::terminate();
  15. }
  16. }
  17. BOOST_NOINLINE void foo(int i) {
  18. bar(--i);
  19. }
  20. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  21. //[getting_started_terminate_handlers
  22. #include <signal.h> // ::signal, ::raise
  23. #include <boost/stacktrace.hpp>
  24. void my_signal_handler(int signum) {
  25. ::signal(signum, SIG_DFL);
  26. boost::stacktrace::safe_dump_to("./backtrace.dump");
  27. ::raise(SIGABRT);
  28. }
  29. //]
  30. void setup_handlers() {
  31. //[getting_started_setup_handlers
  32. ::signal(SIGSEGV, &my_signal_handler);
  33. ::signal(SIGABRT, &my_signal_handler);
  34. //]
  35. }
  36. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  37. BOOST_CONSTEXPR_OR_CONST std::size_t shared_memory_size = 4096 * 8;
  38. //[getting_started_terminate_handlers_shmem
  39. #include <boost/stacktrace.hpp>
  40. #include <boost/interprocess/shared_memory_object.hpp>
  41. #include <boost/interprocess/mapped_region.hpp>
  42. boost::interprocess::shared_memory_object g_shm; // inited at program start
  43. boost::interprocess::mapped_region g_region; // inited at program start
  44. void my_signal_handler2(int signum) {
  45. ::signal(signum, SIG_DFL);
  46. void** f = static_cast<void**>(g_region.get_address());
  47. *f = reinterpret_cast<void*>(1); // Setting flag that shared memory now constains stacktrace.
  48. boost::stacktrace::safe_dump_to(f + 1, g_region.get_size() - sizeof(void*));
  49. ::raise(SIGABRT);
  50. }
  51. //]
  52. #include <iostream> // std::cerr
  53. #include <fstream> // std::ifstream
  54. #include <boost/filesystem/path.hpp>
  55. #include <boost/filesystem/operations.hpp>
  56. inline void copy_and_run(const char* exec_name, char param, bool not_null) {
  57. std::cout << "Running with param " << param << std::endl;
  58. boost::filesystem::path command = exec_name;
  59. command = command.parent_path() / (command.stem().string() + param + command.extension().string());
  60. boost::filesystem::copy_file(exec_name, command, boost::filesystem::copy_option::overwrite_if_exists);
  61. boost::filesystem::path command_args = command;
  62. command_args += ' ';
  63. command_args += param;
  64. const int ret = std::system(command_args.string().c_str());
  65. std::cout << "End Running with param " << param << "; ret code is " << ret << std::endl;
  66. boost::system::error_code ignore;
  67. boost::filesystem::remove(command, ignore);
  68. if (not_null && !ret) {
  69. std::exit(97);
  70. } else if (!not_null && ret) {
  71. std::exit(ret);
  72. }
  73. }
  74. int run_1(const char* /*argv*/[]) {
  75. setup_handlers();
  76. foo(5);
  77. return 11;
  78. }
  79. int run_2(const char* argv[]) {
  80. if (!boost::filesystem::exists("./backtrace.dump")) {
  81. if (std::string(argv[0]).find("noop") == std::string::npos) {
  82. return 21;
  83. }
  84. boost::stacktrace::stacktrace st = boost::stacktrace::stacktrace::from_dump(std::cin);
  85. if (st) {
  86. return 22;
  87. }
  88. return 0;
  89. }
  90. //[getting_started_on_program_restart
  91. if (boost::filesystem::exists("./backtrace.dump")) {
  92. // there is a backtrace
  93. std::ifstream ifs("./backtrace.dump");
  94. boost::stacktrace::stacktrace st = boost::stacktrace::stacktrace::from_dump(ifs);
  95. std::cout << "Previous run crashed:\n" << st << std::endl; /*<-*/
  96. if (!st) {
  97. return 23;
  98. } /*->*/
  99. // cleaning up
  100. ifs.close();
  101. boost::filesystem::remove("./backtrace.dump");
  102. }
  103. //]
  104. return 0;
  105. }
  106. int run_3(const char* /*argv*/[]) {
  107. using namespace boost::interprocess;
  108. {
  109. shared_memory_object shm_obj(open_or_create, "shared_memory", read_write);
  110. shm_obj.swap(g_shm);
  111. }
  112. g_shm.truncate(shared_memory_size);
  113. {
  114. mapped_region m(g_shm, read_write, 0, shared_memory_size);
  115. m.swap(g_region);
  116. }
  117. void** f = static_cast<void**>(g_region.get_address());
  118. *f = 0;
  119. ::signal(SIGSEGV, &my_signal_handler2);
  120. ::signal(SIGABRT, &my_signal_handler2);
  121. foo(5);
  122. return 31;
  123. }
  124. int run_4(const char* argv[]) {
  125. using namespace boost::interprocess;
  126. {
  127. shared_memory_object shm_obj(open_only, "shared_memory", read_write);
  128. shm_obj.swap(g_shm);
  129. }
  130. {
  131. mapped_region m(g_shm, read_write, 0, shared_memory_size);
  132. m.swap(g_region);
  133. }
  134. //[getting_started_on_program_restart_shmem
  135. void** f = static_cast<void**>(g_region.get_address());
  136. if (*f) { // Checking if memory constains stacktrace.
  137. boost::stacktrace::stacktrace st
  138. = boost::stacktrace::stacktrace::from_dump(f + 1, g_region.get_size() - sizeof(bool));
  139. std::cout << "Previous run crashed and left trace in shared memory:\n" << st << std::endl;
  140. *f = 0; /*<-*/
  141. shared_memory_object::remove("shared_memory");
  142. if (std::string(argv[0]).find("noop") == std::string::npos) {
  143. if (!st) {
  144. return 43;
  145. }
  146. } else {
  147. if (st) {
  148. return 44;
  149. }
  150. }
  151. } else {
  152. return 42; /*->*/
  153. }
  154. //]
  155. return 0;
  156. }
  157. #include <sstream>
  158. int test_inplace() {
  159. const bool is_noop = !boost::stacktrace::stacktrace();
  160. {
  161. // This is very dependent on compiler and link flags. No sane way to make it work, because:
  162. // * BOOST_NOINLINE could be ignored by MSVC compiler if link-time optimization is enabled.
  163. // * BOOST_FORCEINLINE could be ignored by GCC depending on the std::vector default constructor length.
  164. const std::size_t frames_ss1 = boost::stacktrace::safe_dump_to("./backtrace2.dump");
  165. boost::stacktrace::stacktrace ss2;
  166. std::ifstream ifs("./backtrace2.dump");
  167. boost::stacktrace::stacktrace ss1 = boost::stacktrace::stacktrace::from_dump(ifs);
  168. ifs.close();
  169. boost::filesystem::remove("./backtrace2.dump");
  170. if (ss1.size() + 1 != frames_ss1 || ss2.size() != ss1.size()) {
  171. std::cerr << "51: Stacktraces differ. Dumped size == " << frames_ss1 << ".\n" << ss1 << "\n vs \n" << ss2 << '\n';
  172. } else if (ss1.size() > 1 && ss1[1].name() != ss2[1].name()) {
  173. std::cerr << "52: Stacktraces differ:\n" << ss1 << "\n vs \n" << ss2 << '\n';
  174. }
  175. }
  176. {
  177. // This is very dependent on compiler and link flags. No sane way to make it work, because:
  178. // * BOOST_NOINLINE could be ignored by MSVC compiler if link-time optimization is enabled.
  179. // * BOOST_FORCEINLINE could be ignored by GCC depending on the std::vector default constructor length.
  180. void* data[1024];
  181. const std::size_t frames_ss1 = boost::stacktrace::safe_dump_to(data, sizeof(data));
  182. boost::stacktrace::stacktrace ss2;
  183. boost::stacktrace::stacktrace ss1 = boost::stacktrace::stacktrace::from_dump(data, sizeof(data));
  184. if (ss1.size() + 1 != frames_ss1 || ss1.size() != ss2.size()) {
  185. std::cerr << "53: Stacktraces differ. Dumped size == " << frames_ss1 << ".\n" << ss1 << "\n vs \n" << ss2 << '\n';
  186. } else if (ss1.size() > 1 && ss1[1].name() != ss2[1].name()) {
  187. std::cerr << "54: Stacktraces differ:\n" << ss1 << "\n vs \n" << ss2 << '\n';
  188. }
  189. }
  190. {
  191. void* data[1024];
  192. boost::stacktrace::safe_dump_to(1024, data, sizeof(data));
  193. if (boost::stacktrace::stacktrace::from_dump(data, sizeof(data))) {
  194. std::cerr << "Stacktrace not empty!\n";
  195. return 55;
  196. }
  197. }
  198. {
  199. void* data[1024];
  200. boost::stacktrace::safe_dump_to(1, data, sizeof(data));
  201. if (!is_noop && !boost::stacktrace::stacktrace::from_dump(data, sizeof(data))) {
  202. std::cerr << "Stacktrace empty!\n";
  203. return 56;
  204. }
  205. const std::size_t size_1_skipped = boost::stacktrace::stacktrace::from_dump(data, sizeof(data)).size();
  206. boost::stacktrace::safe_dump_to(0, data, sizeof(data));
  207. const std::size_t size_0_skipped = boost::stacktrace::stacktrace::from_dump(data, sizeof(data)).size();
  208. if (!is_noop && (size_1_skipped + 1 != size_0_skipped)) {
  209. std::cerr << "failed to skip 1 frame!\n";
  210. return 57;
  211. }
  212. }
  213. {
  214. boost::stacktrace::safe_dump_to(0, 1, "./backtrace3.dump");
  215. std::ifstream ifs("./backtrace3.dump");
  216. boost::stacktrace::stacktrace ss1 = boost::stacktrace::stacktrace::from_dump(ifs);
  217. ifs.close();
  218. boost::stacktrace::safe_dump_to(1, 1, "./backtrace3.dump");
  219. ifs.open("./backtrace3.dump");
  220. boost::stacktrace::stacktrace ss2 = boost::stacktrace::stacktrace::from_dump(ifs);
  221. ifs.close();
  222. boost::filesystem::remove("./backtrace3.dump");
  223. #ifdef BOOST_WINDOWS
  224. // `ss2` could be empty on some combinations of Windows+MSVC.
  225. if (!ss2) {
  226. return 0;
  227. }
  228. #endif
  229. if (ss1.size() != ss2.size()) {
  230. std::cerr << "Stacktraces differ:\n" << ss1 << "\n vs \n" << ss2 << '\n';
  231. return 58;
  232. }
  233. if (!is_noop && ss1.size() != 1) {
  234. std::cerr << "Stacktraces does not have size 1:\n" << ss1 << '\n';
  235. return 59;
  236. }
  237. if (ss1 && ss1[0].address() == ss2[0].address()) {
  238. std::cerr << "Stacktraces must differ:\n" << ss1 << "\n vs \n" << ss2 << '\n';
  239. return 60;
  240. }
  241. }
  242. return 0;
  243. }
  244. int main(int argc, const char* argv[]) {
  245. if (argc < 2) {
  246. #ifndef BOOST_WINDOWS
  247. // We are copying files to make sure that stacktrace printing works independently from executable name
  248. copy_and_run(argv[0], '1', true);
  249. copy_and_run(argv[0], '2', false);
  250. // There are some issues with async-safety of shared memory writes on Windows.
  251. copy_and_run(argv[0], '3', true);
  252. copy_and_run(argv[0], '4', false);
  253. #endif
  254. return test_inplace();
  255. }
  256. switch (argv[1][0]) {
  257. case '1': return run_1(argv);
  258. case '2': return run_2(argv);
  259. case '3': return run_3(argv);
  260. case '4': return run_4(argv);
  261. }
  262. return 404;
  263. }