ATLAS Offline Software
exctrace_collector.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
18 #include <dlfcn.h>
19 #include <execinfo.h>
20 #include <cstdio>
21 #include <typeinfo>
22 #include <utility>
23 #include <algorithm>
24 #include <exception>
26 
27 // Maximum stack depth.
28 static
29 const int bt_depth = 100;
30 
31 // Static buffer used to save the backtrace.
32 static thread_local int exctrace_last_depth = 0;
33 static thread_local void* exctrace_last_trace[bt_depth];
34 
35 // The real __cxa_throw function.
36 typedef void throwfn (void*, void*, void (*dest)(void*));
37 static throwfn* old_throw;
38 
39 
40 // Force a dependency on libstdc++ so that we will be able to find
41 // __cxa_throw via dlsym. Otherwise the link dependency may be removed
42 // since we usually link with as-needed.
44 {
45  std::terminate();
46 }
47 
48 
49 extern "C" {
50  // Function to retrieve the last trace.
51  // extern "C" because we want to find it with dlsym.
52  int exctrace_get_last_trace (int max_depth, void* trace[])
53  {
54  int ncopy = std::min (exctrace_last_depth, max_depth);
55  std::copy (exctrace_last_trace, exctrace_last_trace+ncopy, trace);
56  return ncopy;
57  }
58 }
59 
60 
61 // The __cxa_throw hook function.
62 // Record a backtrace, then chain to the real throw function.
63 extern "C" void __cxa_throw (void* thrown_exception,
64  void* tinfo,
65  void (*dest)(void*))
66 {
67  exctrace_last_depth = backtrace (exctrace_last_trace, bt_depth);
68  old_throw (thrown_exception, tinfo, dest);
69  // not reached
70  std::abort();
71 }
72 
73 
74 // Initialization: install the hook.
75 namespace CxxUtils {
76 
77 
79 {
80  extrace_init();
81 };
82 
83 
85 {
86  old_throw = (throwfn*)(long)dlsym (RTLD_NEXT, "__cxa_throw");
87 }
88 
89 
90 static extrace_init initer;
91 
92 
93 } // namespace CxxUtils
__cxa_throw
void __cxa_throw(void *thrown_exception, void *tinfo, void(*dest)(void *))
Definition: exctrace_collector.cxx:63
ATLAS_NOT_THREAD_SAFE
#define ATLAS_NOT_THREAD_SAFE
getNoisyStrip() Find noisy strips from hitmaps and write out into xml/db formats
Definition: checker_macros.h:212
min
constexpr double min()
Definition: ap_fixedTest.cxx:26
extrace_force_libstd_link
void extrace_force_libstd_link()
Definition: exctrace_collector.cxx:43
exctrace_get_last_trace
int exctrace_get_last_trace(int max_depth, void *trace[])
Definition: exctrace_collector.cxx:52
perfmonmt-printer.dest
dest
Definition: perfmonmt-printer.py:189
CxxUtils
Definition: aligned_vector.h:29
CxxUtils::extrace_init
Definition: exctrace_collector.cxx:79
CxxUtils::extrace_init::extrace_init
extrace_init()
Definition: exctrace_collector.cxx:84
throwfn
void throwfn(void *, void *, void(*dest)(void *))
Definition: exctrace_collector.cxx:36
calibdata.copy
bool copy
Definition: calibdata.py:27
checker_macros.h
Define macros for attributes used to control the static checker.
check_log.backtrace
backtrace
Definition: check_log.py:58