forked from facebook/redex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDebug.cpp
243 lines (203 loc) · 5.46 KB
/
Debug.cpp
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "Debug.h"
#include <atomic>
#include <exception>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <memory>
#include <regex>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#ifndef _MSC_VER
#include <execinfo.h>
#include <unistd.h>
#endif
#ifdef __linux__
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#endif
#include <boost/algorithm/string.hpp>
#include <boost/exception/all.hpp>
#ifdef __APPLE__
#define BOOST_STACKTRACE_GNU_SOURCE_NOT_REQUIRED
#endif
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wshadow"
#include <boost/stacktrace.hpp>
#pragma GCC diagnostic pop
namespace {
void crash_backtrace() {
#ifndef _MSC_VER
constexpr int max_bt_frames = 256;
void* buf[max_bt_frames];
auto frames = backtrace(buf, max_bt_frames);
backtrace_symbols_fd(buf, frames, STDERR_FILENO);
#endif
}
}; // namespace
void crash_backtrace_handler(int sig) {
crash_backtrace();
signal(sig, SIG_DFL);
raise(sig);
}
std::string v_format2string(const char* fmt, va_list ap) {
va_list backup;
va_copy(backup, ap);
size_t size = vsnprintf(NULL, 0, fmt, ap);
// size is the number of chars would had been written
std::unique_ptr<char[]> buffer = std::make_unique<char[]>(size + 1);
vsnprintf(buffer.get(), size + 1, fmt, backup);
va_end(backup);
std::string ret(buffer.get());
return ret;
}
std::string format2string(const char* fmt, ...) {
va_list ap;
va_start(ap, fmt);
auto ret = v_format2string(fmt, ap);
va_end(ap);
return ret;
}
namespace {
#ifndef _MSC_VER
// To have unified decoding in redex.py, use GNU backtrace here with a helper.
struct StackTrace {
std::array<void*, 256> trace;
size_t len;
StackTrace() { len = backtrace(trace.data(), trace.size()); }
void print_to_stderr() const {
backtrace_symbols_fd(trace.data(), len, STDERR_FILENO);
}
};
using StType = StackTrace;
void print_stack_trace_impl(std::ostream& /* os */, const StackTrace* st) {
// Does not support ostream right now.
st->print_to_stderr();
}
#else
// Use boost stacktrace on Windows.
using StType = boost::stacktrace::stacktrace;
void print_stack_trace_impl(std::ostream& os, const StType* st) {
os << *st << std::endl;
}
#endif
using traced = boost::error_info<struct tag_stacktrace, StType>;
#ifdef __linux__
std::atomic<pid_t> g_aborting{0};
pid_t get_tid() { return syscall(SYS_gettid); }
#endif
bool g_block_multi_asserts{false};
} // namespace
void block_multi_asserts(bool block) {
g_block_multi_asserts = block; // Ignore races and such.
}
void assert_fail(const char* expr,
const char* file,
unsigned line,
const char* func,
RedexError type,
const char* fmt,
...) {
va_list ap;
va_start(ap, fmt);
std::string msg = format2string(
"%s:%u: %s: assertion `%s' failed.\n", file, line, func, expr);
msg += v_format2string(fmt, ap);
va_end(ap);
bool do_throw;
#ifdef __linux__
pid_t cur = get_tid();
pid_t expected = 0;
do_throw = g_aborting.compare_exchange_strong(expected, cur);
if (!do_throw) {
do_throw = expected == cur;
}
#else
do_throw = true;
#endif
if (do_throw || !g_block_multi_asserts) {
throw boost::enable_error_info(RedexException(type, msg))
<< traced(StType());
}
// Another thread already threw. Avoid "terminate called recursively."
// Infinite loop.
for (;;) {
sleep(1000);
}
}
void print_stack_trace(std::ostream& os, const std::exception& e) {
const StType* st = boost::get_error_info<traced>(e);
if (st) {
print_stack_trace_impl(os, st);
}
}
VmStats get_mem_stats() {
VmStats res;
std::ifstream ifs("/proc/self/status");
if (ifs.fail()) {
return res;
}
std::string line;
std::regex re("[^:]*:\\s*([0-9]*)\\s*(.)B");
while (std::getline(ifs, line)) {
bool is_vm_peak = boost::starts_with(line, "VmPeak:");
bool is_vm_hwm = boost::starts_with(line, "VmHWM:");
if (is_vm_peak || is_vm_hwm) {
std::smatch match;
bool matched = std::regex_match(line, match, re);
if (!matched) {
std::cerr << "Error: could not match " << line << std::endl;
continue;
}
std::string num_str = match.str(1);
std::string size_prefix_str = match.str(2);
uint64_t val;
try {
size_t idx;
val = std::stoull(num_str, &idx);
} catch (...) {
std::cerr << "Failed to parse numeric value in " << line << std::endl;
continue;
}
if (size_prefix_str == "k" or size_prefix_str == "K") {
val *= 1024;
} else if (size_prefix_str == "M") {
val *= 1024 * 1024;
} else if (size_prefix_str == "G") {
val *= 1024 * 1024 * 1024;
} else {
std::cerr << "Unknown size modifier in " << line << std::endl;
continue;
}
if (is_vm_peak) {
res.vm_peak = val;
} else {
res.vm_hwm = val;
}
if (res.vm_peak != 0 && res.vm_hwm != 0) {
break;
}
}
}
return res;
}
bool try_reset_hwm_mem_stat() {
// See http://man7.org/linux/man-pages/man5/proc.5.html for `clear_refs` and
// the magic `5`.
std::ofstream ifs("/proc/self/clear_refs");
if (ifs.fail()) {
return false;
}
ifs << 5;
return true;
}