forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCrashWriter.cpp
More file actions
212 lines (180 loc) · 7.06 KB
/
Copy pathCrashWriter.cpp
File metadata and controls
212 lines (180 loc) · 7.06 KB
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
#include <Daemon/CrashWriter.h>
#include <Poco/Util/LayeredConfiguration.h>
#include <Poco/Environment.h>
#include <base/getMemoryAmount.h>
#include <Common/logger_useful.h>
#include <Common/Exception.h>
#include <Common/ErrorCodes.h>
#include <Common/SymbolIndex.h>
#include <Common/StackTrace.h>
#include <Common/getNumberOfCPUCoresToUse.h>
#include <Core/ServerUUID.h>
#include <Core/UUID.h>
#include <IO/WriteHelpers.h>
#include <IO/WriteBufferFromHTTP.h>
#include <Common/config_version.h>
using namespace DB;
namespace
{
constexpr std::string logger_name = "CrashWriter";
}
std::unique_ptr<CrashWriter> CrashWriter::instance;
void CrashWriter::initialize(Poco::Util::LayeredConfiguration & config)
{
auto logger = getLogger(logger_name);
if (!config.getBool("send_crash_reports.enabled", false))
{
LOG_DEBUG(logger, "Sending crash reports is disabled");
return;
}
auto endpoint = config.getString("send_crash_reports.endpoint");
if (endpoint.empty())
{
LOG_DEBUG(logger, "Sending crash reports is disabled because send_crash_reports.endpoint is not set");
return;
}
instance.reset(new CrashWriter(std::move(endpoint), config.getString("path", "")));
}
bool CrashWriter::initialized()
{
return instance != nullptr;
}
CrashWriter::CrashWriter(std::string endpoint_, std::string server_data_path_)
: endpoint(std::move(endpoint_))
, server_data_path(std::move(server_data_path_))
, logger(getLogger(logger_name))
{
LOG_DEBUG(logger, "Sending crash reports is initialized with {} endpoint (anonymized)", endpoint);
}
void CrashWriter::onSignal(int sig, std::string_view error_message, const FramePointers & frame_pointers, size_t offset, size_t size)
{
if (instance)
instance->sendError(Type::SIGNAL, sig, error_message, frame_pointers, offset, size);
}
void CrashWriter::onException(int code, std::string_view format_string, const FramePointers & frame_pointers, size_t offset, size_t size)
{
if (instance)
instance->sendError(Type::EXCEPTION, code, format_string, frame_pointers, offset, size);
}
void CrashWriter::sendError(Type type, int sig_or_error, std::string_view error_message, const FramePointers & frame_pointers, size_t offset, size_t size)
{
if (!instance)
{
LOG_INFO(logger, "Not sending crash report");
return;
}
try
{
LOG_INFO(logger, "Sending crash report");
auto out = BuilderWriteBufferFromHTTP(Poco::URI{endpoint})
.withBufferSize(4096).withConnectionGroup(HTTPConnectionGroupType::HTTP).create();
auto & json = *out;
FormatSettings settings;
writeChar('{', json);
writeCString("\"message\":", json);
writeJSONString(error_message, json, settings);
switch (type)
{
case SIGNAL:
{
writeCString(",\"type\":\"signal\"", json);
writeCString(",\"signal_number\":", json);
writeIntText(sig_or_error, json);
break;
}
case EXCEPTION:
{
writeCString(",\"type\":\"exception\"", json);
writeCString(",\"exception_code\":", json);
writeIntText(sig_or_error, json);
writeCString(",\"exception_message\":", json);
writeJSONString(ErrorCodes::getName(sig_or_error), json, settings);
break;
}
}
#if (defined(__ELF__) && !defined(OS_FREEBSD)) || defined(OS_DARWIN)
const String & build_id_hex = SymbolIndex::instance().getBuildIDHex();
writeCString(",\"build_id\":", json);
writeJSONString(build_id_hex, json, settings);
#endif
UUID server_uuid = ServerUUID::get();
if (server_uuid != UUIDHelpers::Nil)
{
std::string server_uuid_str = toString(server_uuid);
writeCString(",\"server_uuid\":", json);
writeJSONString(server_uuid_str, json, settings);
}
writeCString(",\"version\":", json);
writeJSONString(VERSION_STRING, json, settings);
writeCString(",\"git_hash\":", json);
writeJSONString(VERSION_GITHASH, json, settings);
writeCString(",\"git_version\":", json);
writeJSONString(VERSION_DESCRIBE, json, settings);
writeCString(",\"revision\":", json);
writeIntText(VERSION_REVISION, json);
writeCString(",\"official\":", json);
writeJSONString(VERSION_OFFICIAL, json, settings);
writeCString(",\"os\":", json);
writeJSONString(Poco::Environment::osName(), json, settings);
writeCString(",\"os_version\":", json);
writeJSONString(Poco::Environment::osVersion(), json, settings);
writeCString(",\"architecture\":", json);
writeJSONString(Poco::Environment::osArchitecture(), json, settings);
writeCString(",\"total_ram\":", json);
writeIntText(getMemoryAmountOrZero(), json);
writeCString(",\"cpu_cores\":", json);
writeIntText(getNumberOfCPUCoresToUse(), json);
if (!server_data_path.empty())
{
writeCString(",\"disk_free_space\":", json);
writeIntText(std::filesystem::space(server_data_path).free, json);
}
/// Prepare data for the stack trace.
if (size > 0)
{
char instruction_addr[19]
{
'0', 'x',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
'\0'
};
bool first = true;
auto add_frame = [&](const StackTrace::Frame & current_frame)
{
UInt64 frame_ptr = reinterpret_cast<UInt64>(current_frame.physical_addr);
writeHexUIntLowercase(frame_ptr, instruction_addr + 2);
if (!first)
writeChar(',', json);
first = false;
writeCString("{\"addr\":", json);
writeJSONString(instruction_addr, json, settings);
if (current_frame.symbol)
{
writeCString(",\"function\":", json);
writeJSONString(current_frame.symbol.value(), json, settings);
}
if (current_frame.file)
{
writeCString(",\"file\":", json);
writeJSONString(current_frame.file.value(), json, settings);
}
if (current_frame.line)
{
writeCString(",\"line\":", json);
writeIntText(current_frame.line.value(), json);
}
writeChar('}', json);
};
writeCString(",\"trace\":[", json);
StackTrace::forEachFrame(frame_pointers, offset, size, add_frame, /* fatal= */ true);
writeChar(']', json);
}
writeChar('}', json);
json.next();
json.finalize();
}
catch (...)
{
LOG_INFO(logger, "Cannot send a crash report: {}", getCurrentExceptionMessage(__PRETTY_FUNCTION__));
}
}