Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
track all the useless void
[simgrid.git] / src / xbt / backtrace.cpp
1 /* Copyright (c) 2005-2016. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <cstddef>
7 #include <cstdlib>
8
9 #include <vector>
10
11 // Try to detect and use the C++ intanium ABI for name demangling:
12 #ifdef __GXX_ABI_VERSION
13 #include <cxxabi.h>
14 #endif
15
16 #include <xbt/backtrace.h>
17 #include <xbt/backtrace.hpp>
18 #include <xbt/log.h>
19 #include <xbt/sysdep.h>
20
21 #include "src/internal_config.h"
22
23 extern "C" {
24
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_backtrace, xbt, "Backtrace");
26
27 }
28
29 /** @brief show the backtrace of the current point (lovely while debugging) */
30 void xbt_backtrace_display_current()
31 {
32   const std::size_t size = 10;
33   xbt_backtrace_location_t bt[size];
34   size_t used = xbt_backtrace_current(bt, size);
35   xbt_backtrace_display(bt, used);
36 }
37
38 namespace simgrid {
39 namespace xbt {
40
41 std::unique_ptr<char, void(*)(void*)> demangle(const char* name)
42 {
43 #ifdef __GXX_ABI_VERSION
44   int status;
45   auto res = std::unique_ptr<char, void(*)(void*)>(
46     abi::__cxa_demangle(name, nullptr, nullptr, &status),
47     std::free
48   );
49   if (res != nullptr)
50     return res;
51   // We did not manage to resolve this. Probably because this is not a mangled symbol:
52 #endif
53   // Return the symbol:
54   return std::unique_ptr<char, void(*)(void*)>(xbt_strdup(name), std::free);
55 }
56
57 std::vector<xbt_backtrace_location_t> backtrace()
58 {
59   const std::size_t size = 10;
60   xbt_backtrace_location_t loc[size];
61   size_t used = xbt_backtrace_current(loc, size);
62   return std::vector<xbt_backtrace_location_t>(loc, loc + used);
63 }
64
65 }
66 }
67
68 #if HAVE_BACKTRACE && HAVE_EXECINFO_H && HAVE_POPEN && defined(ADDR2LINE)
69 # include "src/xbt/backtrace_linux.cpp"
70 #else
71 # include "src/xbt/backtrace_dummy.cpp"
72 #endif