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
|
/*
* Copyright 2009, Google Inc.
* 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 Google Inc. 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.
*/
//
// Breakpad crash report uploader
// (adapted from code in Google Gears)
//
#include <map>
#include <windows.h>
#include <assert.h>
#include <shellapi.h>
#include <tchar.h>
#include <time.h>
#include "breakpad_config.h"
#include "client/windows/sender/crash_report_sender.h"
using std::map;
using std::wstring;
using google_breakpad::CrashReportSender;
using google_breakpad::ReportResult;
bool CanSendMinidump(const wchar_t *product_name);
void SendMinidump(const wchar_t *minidump_filename,
const wchar_t *product_name,
const wchar_t *product_version);
int _tmain(int argc, _TCHAR* argv[]) {
SendMinidump(argv[1], argv[2], argv[3]);
return 0;
}
bool CanSendMinidump(const wchar_t *product_name) {
// useful for testing purposes
if (kCrashReportAlwaysUpload) {
return true;
}
bool can_send = false;
time_t current_time;
time(¤t_time);
// For throttling, we remember when the last N minidumps were sent.
time_t past_send_times[kCrashReportsMaxPerInterval];
DWORD bytes = sizeof(past_send_times);
memset(&past_send_times, 0, bytes);
HKEY reg_key;
DWORD create_type;
if (ERROR_SUCCESS != RegCreateKeyExW(HKEY_CURRENT_USER,
kCrashReportThrottlingRegKey,
0,
NULL,
0,
KEY_READ | KEY_WRITE, NULL,
®_key, &create_type)) {
return false; // this should never happen, but just in case
}
if (ERROR_SUCCESS != RegQueryValueEx(reg_key, product_name, NULL,
NULL,
reinterpret_cast<BYTE*>(past_send_times),
&bytes)) {
// this product hasn't sent any crash reports yet
can_send = true;
} else {
// find crash reports within the last interval
int crashes_in_last_interval = 0;
for (int i = 0; i < kCrashReportsMaxPerInterval; ++i) {
if (current_time - past_send_times[i] < kCrashReportsIntervalSeconds) {
++crashes_in_last_interval;
}
}
can_send = crashes_in_last_interval < kCrashReportsMaxPerInterval;
}
if (can_send) {
memmove(&past_send_times[1],
&past_send_times[0],
sizeof(time_t) * (kCrashReportsMaxPerInterval - 1));
past_send_times[0] = current_time;
}
RegSetValueEx(reg_key, product_name, 0, REG_BINARY,
reinterpret_cast<BYTE*>(past_send_times),
sizeof(past_send_times));
return can_send;
}
void SendMinidump(const wchar_t *minidump_filename,
const wchar_t *product_name,
const wchar_t *product_version) {
if (CanSendMinidump(product_name)) {
map<std::wstring, std::wstring> parameters;
parameters[kCrashReportProductParam] = product_name;
parameters[kCrashReportVersionParam] = std::wstring(product_version);
std::wstring minidump_wstr(minidump_filename);
CrashReportSender sender(L"");
for (int i = 0; i < kCrashReportAttempts; ++i) {
wstring upload_result;
ReportResult result = sender.SendCrashReport(kCrashReportUrl,
parameters,
minidump_wstr,
&upload_result);
if (result == google_breakpad::RESULT_FAILED) {
Sleep(kCrashReportResendPeriodMs);
} else {
// RESULT_SUCCEEDED or RESULT_REJECTED
break;
}
}
}
DeleteFileW(minidump_filename);
}
|