Project
Loading...
Searching...
No Matches
BacktraceHelpers.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
12#include <climits>
13#include <cstddef>
14#include <cstdio>
15#include <cstdlib>
16#include <unistd.h>
17#include <cxxabi.h>
18#include <execinfo.h>
19
20namespace o2::framework
21{
22
23void BacktraceHelpers::demangled_backtrace_symbols(void** stackTrace, unsigned int stackDepth, int fd)
24{
25 char** stackStrings;
26 stackStrings = backtrace_symbols(stackTrace, stackDepth);
27 char exe[PATH_MAX];
28 bool hasExe = false;
29 int exeSize = 0;
30
31#if __linux__
32 exeSize = readlink("/proc/self/exe", exe, PATH_MAX);
33 if (exeSize == -1) {
34 dprintf(fd, "Unable to detect exectuable name\n");
35 hasExe = false;
36 } else {
37 dprintf(fd, "Executable is %.*s\n", exeSize, exe);
38 hasExe = true;
39 }
40#endif
41
42 // We skip the level 0, which is the actual logging function.
43 for (size_t i = 1; i < stackDepth; i++) {
44
45 size_t sz = 64000; // 64K ought to be enough for our templates...
46 char* function = static_cast<char*>(malloc(sz));
47 char *begin = nullptr, *end = nullptr;
48 // find the last space and address offset surrounding the mangled name
49#if __APPLE__
50 for (char* j = stackStrings[i]; *j; ++j) {
51 if (*j == ' ' && *(j + 1) != '+') {
52 begin = j;
53 } else if (*j == ' ' && *(j + 1) == '+') {
54 end = j;
55 break;
56 }
57 }
58 bool tryAddr2Line = false;
59#else
60 for (char* j = stackStrings[i]; j && *j; ++j) {
61 if (*j == '(') {
62 begin = j;
63 } else if (*j == '+') {
64 end = j;
65 break;
66 }
67 }
68 bool tryAddr2Line = true;
69#endif
70 if (begin && end) {
71 // The first byte is a ' ' which we need to skip.
72 *begin++ = '\0';
73 *end = '\0';
74 // found our mangled name, now in [begin, end)
75
76 int status;
77 char* ret = abi::__cxa_demangle(begin, function, &sz, &status);
78 if (status == 0) {
79 // return value may be a realloc() of the input
80 function = ret;
81 dprintf(fd, " %s: %s\n", stackStrings[i], function);
82 tryAddr2Line = false;
83 } else {
84 tryAddr2Line = true;
85 }
86 }
87 if (tryAddr2Line) {
88 // didn't find the mangled name, just print the whole line
89 dprintf(fd, " %s: ", stackStrings[i]);
90 if (stackTrace[i] && hasExe) {
91 char syscom[4096 + PATH_MAX];
92
93 // Find c++filt from the environment
94 // This is needed for platforms where we still need c++filt -r
95 static char const* cxxfilt = getenv("CXXFILT");
96 if (cxxfilt == nullptr) {
97 cxxfilt = "c++filt";
98 }
99 // Do the same for addr2line, just in case we wanted to pass some options
100 static char const* addr2line = getenv("ADDR2LINE");
101 if (addr2line == nullptr) {
102 addr2line = "addr2line";
103 }
104 snprintf(syscom, 4096, "%s %p -p -s -f -e %.*s 2>/dev/null | %s ", addr2line, stackTrace[i], exeSize, exe, cxxfilt); // last parameter is the name of this app
105
106 FILE* fp;
107 char path[1024];
108
109 fp = popen(syscom, "r");
110 if (fp == nullptr) {
111 // We could not run addr2line, print whatever we have.
112 // Also free function, which is allocated at the beginning of the loop.
113 dprintf(fd, "%s\n", begin);
114 free(function);
115 continue;
116 }
117
118 while (fgets(path, sizeof(path) - 1, fp) != nullptr) {
119 dprintf(fd, " %s", path);
120 }
121
122 pclose(fp);
123 } else {
124 dprintf(fd, "%s\n", begin);
125 }
126 }
127 free(function);
128 }
129 free(stackStrings); // malloc()ed by backtrace_symbols
130 fsync(fd);
131}
132} // namespace o2::framework
int32_t i
uint32_t j
Definition RawData.h:0
GLuint GLuint end
Definition glcorearb.h:469
GLsizei const GLchar *const * path
Definition glcorearb.h:3591
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
static void demangled_backtrace_symbols(void **backtrace, unsigned int total, int fd)