This repository has been archived by the owner on Feb 15, 2022. It is now read-only.
/
http_symbol_supplier.cc
421 lines (368 loc) · 13.2 KB
/
http_symbol_supplier.cc
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// Copyright (c) 2011 The Mozilla Foundation
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of The Mozilla Foundation nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "http_symbol_supplier.h"
#include <algorithm>
#include <curl/curl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>
#include "google_breakpad/processor/code_module.h"
#include "google_breakpad/processor/system_info.h"
#include "processor/logging.h"
#include "processor/pathname_stripper.h"
namespace breakpad_extra {
using google_breakpad::CodeModule;
using google_breakpad::PathnameStripper;
using google_breakpad::SystemInfo;
static bool file_exists(const string& file_name) {
struct stat sb;
return stat(file_name.c_str(), &sb) == 0;
}
static string dirname(const string& path) {
size_t i = path.rfind('/');
if (i == string::npos) {
return path;
}
return path.substr(0, i);
}
static bool mkdirs(const string& file) {
vector<string> dirs;
string dir = dirname(file);
while (!file_exists(dir)) {
dirs.push_back(dir);
string new_dir = dirname(dir);
if (new_dir == dir || dir.empty()) {
break;
}
dir = new_dir;
}
for (auto d = dirs.rbegin(); d != dirs.rend(); ++d) {
if (mkdir(d->c_str(), 0755) != 0) {
BPLOG(ERROR) << "Error creating " << *d << ": " << errno;
return false;
}
}
return true;
}
static vector<string> vector_from(const string& front,
const vector<string>& rest) {
vector<string> vec(1, front);
std::copy(rest.begin(), rest.end(), std::back_inserter(vec));
return vec;
}
HTTPSymbolSupplier::HTTPSymbolSupplier(const vector<string>& server_urls,
const string& cache_path,
const vector<string>& local_paths,
const string& tmp_path)
: SimpleSymbolSupplier(vector_from(cache_path, local_paths)),
server_urls_(server_urls),
cache_path_(cache_path),
tmp_path_(tmp_path),
curl_(curl_easy_init()) {
for (auto i = server_urls_.begin(); i < server_urls_.end(); ++i) {
if (*(i->end() - 1) != '/') {
i->push_back('/');
}
}
// Remove any trailing slash on tmp_path.
if (!tmp_path_.empty() && tmp_path_.back() == '/') {
tmp_path_.erase(tmp_path_.end() - 1);
}
}
HTTPSymbolSupplier::~HTTPSymbolSupplier() {
curl_easy_cleanup(curl_);
}
void
HTTPSymbolSupplier::StoreSymbolStats(const CodeModule* module,
const SymbolStats& stats) {
const auto& key =
std::make_pair(module->debug_file(), module->debug_identifier());
if (symbol_stats_.find(key) == symbol_stats_.end()) {
symbol_stats_[key] = stats;
}
}
string
HTTPSymbolSupplier::GuessURL(const CodeModule* module)
{
string path, url;
if (!server_urls_.empty()
&& GetRelativeURLAndPathToSymbolFile(module, url, path)) {
return server_urls_.front() + url;
}
return "";
}
void
HTTPSymbolSupplier::StoreCacheHit(const CodeModule* module)
{
// We don't know the original URL, so guess that it would have
// been from the first URL we would have tried.
SymbolStats stats = {true, 0.0f, GuessURL(module)};
StoreSymbolStats(module, stats);
}
void
HTTPSymbolSupplier::StoreCacheMiss(const CodeModule* module,
float fetch_time,
const string& url)
{
SymbolStats stats = {false, fetch_time, url};
StoreSymbolStats(module, stats);
}
SymbolSupplier::SymbolResult
HTTPSymbolSupplier::GetSymbolFile(const CodeModule* module,
const SystemInfo* system_info,
string* symbol_file) {
SymbolSupplier::SymbolResult res =
SimpleSymbolSupplier::GetSymbolFile(module, system_info, symbol_file);
if (res != SymbolSupplier::NOT_FOUND) {
StoreCacheHit(module);
return res;
}
if (!FetchSymbolFile(module, system_info)) {
return SymbolSupplier::NOT_FOUND;
}
return SimpleSymbolSupplier::GetSymbolFile(module, system_info, symbol_file);
}
SymbolSupplier::SymbolResult
HTTPSymbolSupplier::GetSymbolFile(const CodeModule* module,
const SystemInfo* system_info,
string* symbol_file,
string* symbol_data) {
SymbolSupplier::SymbolResult res =
SimpleSymbolSupplier::GetSymbolFile(module, system_info,
symbol_file, symbol_data);
if (res != SymbolSupplier::NOT_FOUND) {
StoreCacheHit(module);
return res;
}
if (!FetchSymbolFile(module, system_info)) {
return SymbolSupplier::NOT_FOUND;
}
return SimpleSymbolSupplier::GetSymbolFile(module, system_info,
symbol_file, symbol_data);
}
SymbolSupplier::SymbolResult
HTTPSymbolSupplier::GetCStringSymbolData(const CodeModule *module,
const SystemInfo *system_info,
string *symbol_file,
char **symbol_data,
size_t* size) {
SymbolSupplier::SymbolResult res =
SimpleSymbolSupplier::GetCStringSymbolData(module, system_info,
symbol_file, symbol_data,
size);
if (res != SymbolSupplier::NOT_FOUND) {
StoreCacheHit(module);
return res;
}
if (!FetchSymbolFile(module, system_info)) {
return SymbolSupplier::NOT_FOUND;
}
return SimpleSymbolSupplier::GetCStringSymbolData(module, system_info,
symbol_file, symbol_data,
size);
}
namespace {
string URLEncode(CURL* curl, const string& url) {
char* escaped_url_raw = curl_easy_escape(curl,
const_cast<char*>(url.c_str()),
url.length());
if (not escaped_url_raw) {
BPLOG(INFO) << "HTTPSymbolSupplier: couldn't escape URL: " << url;
return "";
}
string escaped_url(escaped_url_raw);
curl_free(escaped_url_raw);
return escaped_url;
}
string JoinPath(const string& path, const string& sub) {
return path + "/" + sub;
}
string JoinURL(CURL* curl, const string& url, const string& sub) {
return url + "/" + URLEncode(curl, sub);
}
string GetCodeIDAndFileQueryString(CURL* curl, const CodeModule* module) {
string code_file = PathnameStripper::File(module->code_file());
if (code_file.empty()) {
return "";
}
string code_id = PathnameStripper::File(module->code_identifier());
if (code_id.empty()) {
return "";
}
return "?code_file=" + URLEncode(curl, code_file) + "&code_id=" + URLEncode(curl, code_id);
}
}
bool
HTTPSymbolSupplier::GetRelativeURLAndPathToSymbolFile(const CodeModule* module,
string& out_url,
string& out_path)
{
// Copied from simple_symbol_supplier.cc
string debug_file_name = PathnameStripper::File(module->debug_file());
if (debug_file_name.empty()) {
return false;
}
string path = debug_file_name;
string url = URLEncode(curl_, debug_file_name);
// Append the identifier as a directory name.
string identifier = module->debug_identifier();
if (identifier.empty()) {
return false;
}
path = JoinPath(path, identifier);
url = JoinURL(curl_, url, identifier);
// Transform the debug file name into one ending in .sym. If the existing
// name ends in .pdb, strip the .pdb. Otherwise, add .sym to the non-.pdb
// name.
string debug_file_extension;
if (debug_file_name.size() > 4) {
debug_file_extension = debug_file_name.substr(debug_file_name.size() - 4);
}
std::transform(debug_file_extension.begin(), debug_file_extension.end(),
debug_file_extension.begin(), tolower);
if (debug_file_extension == ".pdb") {
debug_file_name = debug_file_name.substr(0, debug_file_name.size() - 4);
}
debug_file_name += ".sym";
out_path = JoinPath(path, debug_file_name);
out_url = JoinURL(curl_, url, debug_file_name);
return true;
}
bool HTTPSymbolSupplier::FetchSymbolFile(const CodeModule* module,
const SystemInfo* system_info) {
// See if we already attempted to fetch this symbol file.
if (SymbolWasError(module, system_info)) {
return false;
}
string path, url;
if (!GetRelativeURLAndPathToSymbolFile(module, url, path)) {
return false;
}
string query = GetCodeIDAndFileQueryString(curl_, module);
string full_path = JoinPath(cache_path_, path);
bool result = false;
for (auto server_url = server_urls_.begin();
server_url < server_urls_.end();
++server_url) {
string full_url = *server_url + url;
string url_with_query = full_url;
if (!query.empty()) {
url_with_query += query;
}
float fetch_time;
if (FetchURLToFile(curl_, url_with_query, full_path, &fetch_time)) {
StoreCacheMiss(module, fetch_time, full_url);
result = true;
break;
}
}
if (!result) {
error_symbols_.insert(std::make_pair(module->debug_file(),
module->debug_identifier()));
}
return result;
}
bool HTTPSymbolSupplier::FetchURLToFile(CURL* curl,
const string& url,
const string& file,
float* fetch_time) {
BPLOG(INFO) << "HTTPSymbolSupplier: fetching " << url;
*fetch_time = 0.0f;
string tempfile = JoinPath(tmp_path_, "symbolXXXXXX");
int fd = mkstemp(&tempfile[0]);
if (fd == -1) {
return false;
}
FILE* f = fdopen(fd, "w");
BPLOG(INFO) << "HTTPSymbolSupplier: querying " << url;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_ENCODING, "");
curl_easy_setopt(curl, CURLOPT_STDERR, stderr);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, f);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
struct timeval t1, t2;
gettimeofday(&t1, nullptr);
bool result = false;
long retcode = -1;
if (curl_easy_perform(curl) != 0) {
BPLOG(INFO) << "HTTPSymbolSupplier: curl_easy_perform failed";
}
else if (curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &retcode) != 0) {
BPLOG(INFO) << "HTTPSymbolSupplier: curl_easy_getinfo failed";
}
else if (retcode != 200) {
BPLOG(INFO) << "HTTPSymbolSupplier: HTTP response code: " << retcode;
}
else {
BPLOG(INFO) << "HTTPSymbolSupplier: fetch succeeded, saving to " << file;
result = true;
}
gettimeofday(&t2, nullptr);
*fetch_time = (t2.tv_sec - t1.tv_sec) * 1000.0
+ (t2.tv_usec - t1.tv_usec) / 1000.0;
fclose(f);
close(fd);
if (result) {
result = mkdirs(file);
if (!result) {
BPLOG(INFO) << "HTTPSymbolSupplier: failed to create directories for "
<< file;
}
}
if (result) {
result = 0 == rename(tempfile.c_str(), file.c_str());
if (!result) {
int e = errno;
BPLOG(INFO) << "HTTPSymbolSupplier: failed to rename file, errno=" << e;
}
}
if (!result) {
unlink(tempfile.c_str());
}
return result;
}
bool
HTTPSymbolSupplier::GetStats(const CodeModule* module, SymbolStats* stats) const {
const auto& found =
symbol_stats_.find(std::make_pair(module->debug_file(),
module->debug_identifier()));
if (found == symbol_stats_.end()) {
return false;
}
*stats = found->second;
return true;
}
bool HTTPSymbolSupplier::SymbolWasError(const CodeModule* module,
const SystemInfo* system_info) {
return error_symbols_.find(std::make_pair(module->debug_file(),
module->debug_identifier())) !=
error_symbols_.end();
}
}