1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include <ostream>
#include <cxxabi.h>
#include <string.h>
#include "BackTrace.h"
#include "common/version.h"
#include "common/Formatter.h"
#define _STR(x) #x
#define STRINGIFY(x) _STR(x)
namespace ceph {
void BackTrace::print(std::ostream& out) const
{
out << " " << pretty_version_to_str() << std::endl;
for (size_t i = skip; i < size; i++) {
// out << " " << (i-skip+1) << ": " << strings[i] << std::endl;
size_t sz = 1024; // just a guess, template names will go much wider
char *function = (char *)malloc(sz);
if (!function)
return;
char *begin = 0, *end = 0;
// find the parentheses and address offset surrounding the mangled name
#ifdef __FreeBSD__
static constexpr char OPEN = '<';
#else
static constexpr char OPEN = '(';
#endif
for (char *j = strings[i]; *j; ++j) {
if (*j == OPEN)
begin = j+1;
else if (*j == '+')
end = j;
}
if (begin && end) {
int len = end - begin;
char *foo = (char *)malloc(len+1);
if (!foo) {
free(function);
return;
}
memcpy(foo, begin, len);
foo[len] = 0;
int status;
char *ret = nullptr;
// only demangle a C++ mangled name
if (foo[0] == '_' && foo[1] == 'Z')
ret = abi::__cxa_demangle(foo, function, &sz, &status);
if (ret) {
// return value may be a realloc() of the input
function = ret;
}
else {
// demangling failed, just pretend it's a C function with no args
strncpy(function, foo, sz);
strncat(function, "()", sz);
function[sz-1] = 0;
}
out << " " << (i-skip+1) << ": " << OPEN << function << end << std::endl;
//fprintf(out, " %s:%s\n", stack.strings[i], function);
free(foo);
} else {
// didn't find the mangled name, just print the whole line
out << " " << (i-skip+1) << ": " << strings[i] << std::endl;
}
free(function);
}
}
void BackTrace::dump(Formatter *f) const
{
f->open_array_section("backtrace");
for (size_t i = skip; i < size; i++) {
// out << " " << (i-skip+1) << ": " << strings[i] << std::endl;
size_t sz = 1024; // just a guess, template names will go much wider
char *function = (char *)malloc(sz);
if (!function)
return;
char *begin = 0, *end = 0;
// find the parentheses and address offset surrounding the mangled name
#ifdef __FreeBSD__
static constexpr char OPEN = '<';
#else
static constexpr char OPEN = '(';
#endif
for (char *j = strings[i]; *j; ++j) {
if (*j == OPEN)
begin = j+1;
else if (*j == '+')
end = j;
}
if (begin && end) {
int len = end - begin;
char *foo = (char *)malloc(len+1);
if (!foo) {
free(function);
return;
}
memcpy(foo, begin, len);
foo[len] = 0;
int status;
char *ret = nullptr;
// only demangle a C++ mangled name
if (foo[0] == '_' && foo[1] == 'Z')
ret = abi::__cxa_demangle(foo, function, &sz, &status);
if (ret) {
// return value may be a realloc() of the input
function = ret;
}
else {
// demangling failed, just pretend it's a C function with no args
strncpy(function, foo, sz);
strncat(function, "()", sz);
function[sz-1] = 0;
}
f->dump_stream("frame") << OPEN << function << end;
//fprintf(out, " %s:%s\n", stack.strings[i], function);
free(foo);
} else {
// didn't find the mangled name, just print the whole line
//out << " " << (i-skip+1) << ": " << strings[i] << std::endl;
f->dump_string("frame", strings[i]);
}
free(function);
}
f->close_section();
}
}
|