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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// For linux_syscall_support.h. This makes it safe to call embedded system
// calls when in seccomp mode.
#include "chrome/app/breakpad_linux.h"
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include <algorithm>
#include <string>
#include "base/command_line.h"
#include "base/debug/crash_logging.h"
#include "base/files/file_path.h"
#include "base/linux_util.h"
#include "base/path_service.h"
#include "base/platform_file.h"
#include "base/posix/eintr_wrapper.h"
#include "base/posix/global_descriptors.h"
#include "base/process_util.h"
#include "base/strings/string_util.h"
#include "breakpad/src/client/linux/handler/exception_handler.h"
#include "breakpad/src/client/linux/minidump_writer/directory_reader.h"
#include "breakpad/src/common/linux/linux_libc_support.h"
#include "breakpad/src/common/memory.h"
#include "chrome/app/breakpad_linux_impl.h"
#include "chrome/browser/crash_upload_list.h"
#include "chrome/common/child_process_logging.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/chrome_version_info_posix.h"
#include "chrome/common/crash_keys.h"
#include "chrome/common/dump_without_crashing.h"
#include "chrome/common/env_vars.h"
#include "chrome/common/logging_chrome.h"
#include "components/breakpad/common/breakpad_paths.h"
#include "content/public/common/content_descriptors.h"
#if defined(OS_ANDROID)
#include <android/log.h>
#include <sys/stat.h>
#include "base/android/build_info.h"
#include "base/android/path_utils.h"
#include "chrome/common/descriptors_android.h"
#endif
#include "third_party/lss/linux_syscall_support.h"
#if defined(ADDRESS_SANITIZER)
#include <ucontext.h> // for getcontext().
#endif
#if defined(OS_ANDROID)
#define STAT_STRUCT struct stat
#define FSTAT_FUNC fstat
#else
#define STAT_STRUCT struct kernel_stat
#define FSTAT_FUNC sys_fstat
#endif
// Some versions of gcc are prone to warn about unused return values. In cases
// where we either a) know the call cannot fail, or b) there is nothing we
// can do when a call fails, we mark the return code as ignored. This avoids
// spurious compiler warnings.
#define IGNORE_RET(x) do { if (x); } while (0)
using google_breakpad::ExceptionHandler;
using google_breakpad::MinidumpDescriptor;
namespace {
const char kUploadURL[] = "https://clients2.google.com/cr/report";
bool g_is_crash_reporter_enabled = false;
uint64_t g_process_start_time = 0;
char* g_crash_log_path = NULL;
ExceptionHandler* g_breakpad = NULL;
#if defined(ADDRESS_SANITIZER)
const char* g_asan_report_str = NULL;
#endif
#if defined(OS_ANDROID)
char* g_process_type = NULL;
#endif
CrashKeyStorage* g_crash_keys = NULL;
// Writes the value |v| as 16 hex characters to the memory pointed at by
// |output|.
void write_uint64_hex(char* output, uint64_t v) {
static const char hextable[] = "0123456789abcdef";
for (int i = 15; i >= 0; --i) {
output[i] = hextable[v & 15];
v >>= 4;
}
}
// The following helper functions are for calculating uptime.
// Converts a struct timeval to milliseconds.
uint64_t timeval_to_ms(struct timeval *tv) {
uint64_t ret = tv->tv_sec; // Avoid overflow by explicitly using a uint64_t.
ret *= 1000;
ret += tv->tv_usec / 1000;
return ret;
}
// Converts a struct timeval to milliseconds.
uint64_t kernel_timeval_to_ms(struct kernel_timeval *tv) {
uint64_t ret = tv->tv_sec; // Avoid overflow by explicitly using a uint64_t.
ret *= 1000;
ret += tv->tv_usec / 1000;
return ret;
}
// String buffer size to use to convert a uint64_t to string.
size_t kUint64StringSize = 21;
void SetProcessStartTime() {
// Set the base process start time value.
struct timeval tv;
if (!gettimeofday(&tv, NULL))
g_process_start_time = timeval_to_ms(&tv);
else
g_process_start_time = 0;
}
// uint64_t version of my_int_len() from
// breakpad/src/common/linux/linux_libc_support.h. Return the length of the
// given, non-negative integer when expressed in base 10.
unsigned my_uint64_len(uint64_t i) {
if (!i)
return 1;
unsigned len = 0;
while (i) {
len++;
i /= 10;
}
return len;
}
// uint64_t version of my_uitos() from
// breakpad/src/common/linux/linux_libc_support.h. Convert a non-negative
// integer to a string (not null-terminated).
void my_uint64tos(char* output, uint64_t i, unsigned i_len) {
for (unsigned index = i_len; index; --index, i /= 10)
output[index - 1] = '0' + (i % 10);
}
#if defined(OS_ANDROID)
char* my_strncpy(char* dst, const char* src, size_t len) {
int i = len;
char* p = dst;
if (!dst || !src)
return dst;
while (i != 0 && *src != '\0') {
*p++ = *src++;
i--;
}
while (i != 0) {
*p++ = '\0';
i--;
}
return dst;
}
char* my_strncat(char *dest, const char* src, size_t len) {
char* ret = dest;
while (*dest)
dest++;
while (len--)
if (!(*dest++ = *src++))
return ret;
*dest = 0;
return ret;
}
#endif
// Populates the passed in allocated strings and their sizes with the GUID,
// crash url and distro of the crashing process.
// The passed strings are expected to be at least kGuidSize, kMaxActiveURLSize
// and kDistroSize bytes long respectively.
void PopulateGUIDAndURLAndDistro(char* guid, size_t* guid_len_param,
char* crash_url, size_t* crash_url_len_param,
char* distro, size_t* distro_len_param) {
size_t guid_len = std::min(my_strlen(child_process_logging::g_client_id),
kGuidSize);
size_t crash_url_len =
std::min(my_strlen(child_process_logging::g_active_url),
kMaxActiveURLSize);
size_t distro_len = std::min(my_strlen(base::g_linux_distro), kDistroSize);
memcpy(guid, child_process_logging::g_client_id, guid_len);
memcpy(crash_url, child_process_logging::g_active_url, crash_url_len);
memcpy(distro, base::g_linux_distro, distro_len);
if (guid_len_param)
*guid_len_param = guid_len;
if (crash_url_len_param)
*crash_url_len_param = crash_url_len;
if (distro_len_param)
*distro_len_param = distro_len;
}
void SetClientIdFromCommandLine(const CommandLine& command_line) {
// Get the guid and linux distro from the command line switch.
std::string switch_value =
command_line.GetSwitchValueASCII(switches::kEnableCrashReporter);
size_t separator = switch_value.find(",");
if (separator != std::string::npos) {
child_process_logging::SetClientId(switch_value.substr(0, separator));
base::SetLinuxDistro(switch_value.substr(separator + 1));
} else {
child_process_logging::SetClientId(switch_value);
}
}
// MIME substrings.
const char g_rn[] = "\r\n";
const char g_form_data_msg[] = "Content-Disposition: form-data; name=\"";
const char g_quote_msg[] = "\"";
const char g_dashdash_msg[] = "--";
const char g_dump_msg[] = "upload_file_minidump\"; filename=\"dump\"";
#if defined(ADDRESS_SANITIZER)
const char g_log_msg[] = "upload_file_log\"; filename=\"log\"";
#endif
const char g_content_type_msg[] = "Content-Type: application/octet-stream";
// MimeWriter manages an iovec for writing MIMEs to a file.
class MimeWriter {
public:
static const int kIovCapacity = 30;
static const size_t kMaxCrashChunkSize = 64;
MimeWriter(int fd, const char* const mime_boundary);
~MimeWriter();
// Append boundary.
void AddBoundary();
// Append end of file boundary.
void AddEnd();
// Append key/value pair with specified sizes.
void AddPairData(const char* msg_type,
size_t msg_type_size,
const char* msg_data,
size_t msg_data_size);
// Append key/value pair.
void AddPairString(const char* msg_type,
const char* msg_data) {
AddPairData(msg_type, my_strlen(msg_type), msg_data, my_strlen(msg_data));
}
// Append key/value pair, splitting value into chunks no larger than
// |chunk_size|. |chunk_size| cannot be greater than |kMaxCrashChunkSize|.
// The msg_type string will have a counter suffix to distinguish each chunk.
void AddPairDataInChunks(const char* msg_type,
size_t msg_type_size,
const char* msg_data,
size_t msg_data_size,
size_t chunk_size,
bool strip_trailing_spaces);
// Add binary file contents to be uploaded with the specified filename.
void AddFileContents(const char* filename_msg,
uint8_t* file_data,
size_t file_size);
// Flush any pending iovecs to the output file.
void Flush() {
IGNORE_RET(sys_writev(fd_, iov_, iov_index_));
iov_index_ = 0;
}
private:
void AddItem(const void* base, size_t size);
// Minor performance trade-off for easier-to-maintain code.
void AddString(const char* str) {
AddItem(str, my_strlen(str));
}
void AddItemWithoutTrailingSpaces(const void* base, size_t size);
struct kernel_iovec iov_[kIovCapacity];
int iov_index_;
// Output file descriptor.
int fd_;
const char* const mime_boundary_;
DISALLOW_COPY_AND_ASSIGN(MimeWriter);
};
MimeWriter::MimeWriter(int fd, const char* const mime_boundary)
: iov_index_(0),
fd_(fd),
mime_boundary_(mime_boundary) {
}
MimeWriter::~MimeWriter() {
}
void MimeWriter::AddBoundary() {
AddString(mime_boundary_);
AddString(g_rn);
}
void MimeWriter::AddEnd() {
AddString(mime_boundary_);
AddString(g_dashdash_msg);
AddString(g_rn);
}
void MimeWriter::AddPairData(const char* msg_type,
size_t msg_type_size,
const char* msg_data,
size_t msg_data_size) {
AddString(g_form_data_msg);
AddItem(msg_type, msg_type_size);
AddString(g_quote_msg);
AddString(g_rn);
AddString(g_rn);
AddItem(msg_data, msg_data_size);
AddString(g_rn);
}
void MimeWriter::AddPairDataInChunks(const char* msg_type,
size_t msg_type_size,
const char* msg_data,
size_t msg_data_size,
size_t chunk_size,
bool strip_trailing_spaces) {
if (chunk_size > kMaxCrashChunkSize)
return;
unsigned i = 0;
size_t done = 0, msg_length = msg_data_size;
while (msg_length) {
char num[16];
const unsigned num_len = my_uint_len(++i);
my_uitos(num, i, num_len);
size_t chunk_len = std::min(chunk_size, msg_length);
AddString(g_form_data_msg);
AddItem(msg_type, msg_type_size);
AddItem(num, num_len);
AddString(g_quote_msg);
AddString(g_rn);
AddString(g_rn);
if (strip_trailing_spaces) {
AddItemWithoutTrailingSpaces(msg_data + done, chunk_len);
} else {
AddItem(msg_data + done, chunk_len);
}
AddString(g_rn);
AddBoundary();
Flush();
done += chunk_len;
msg_length -= chunk_len;
}
}
void MimeWriter::AddFileContents(const char* filename_msg, uint8_t* file_data,
size_t file_size) {
AddString(g_form_data_msg);
AddString(filename_msg);
AddString(g_rn);
AddString(g_content_type_msg);
AddString(g_rn);
AddString(g_rn);
AddItem(file_data, file_size);
AddString(g_rn);
}
void MimeWriter::AddItem(const void* base, size_t size) {
// Check if the iovec is full and needs to be flushed to output file.
if (iov_index_ == kIovCapacity) {
Flush();
}
iov_[iov_index_].iov_base = const_cast<void*>(base);
iov_[iov_index_].iov_len = size;
++iov_index_;
}
void MimeWriter::AddItemWithoutTrailingSpaces(const void* base, size_t size) {
while (size > 0) {
const char* c = static_cast<const char*>(base) + size - 1;
if (*c != ' ')
break;
size--;
}
AddItem(base, size);
}
void DumpProcess() {
if (g_breakpad)
g_breakpad->WriteMinidump();
}
const char kGoogleBreakpad[] = "google-breakpad";
size_t WriteLog(const char* buf, size_t nbytes) {
#if defined(OS_ANDROID)
return __android_log_write(ANDROID_LOG_WARN, kGoogleBreakpad, buf);
#else
return sys_write(2, buf, nbytes);
#endif
}
#if defined(OS_ANDROID)
// Android's native crash handler outputs a diagnostic tombstone to the device
// log. By returning false from the HandlerCallbacks, breakpad will reinstall
// the previous (i.e. native) signal handlers before returning from its own
// handler. A Chrome build fingerprint is written to the log, so that the
// specific build of Chrome and the location of the archived Chrome symbols can
// be determined directly from it.
bool FinalizeCrashDoneAndroid() {
base::android::BuildInfo* android_build_info =
base::android::BuildInfo::GetInstance();
__android_log_write(ANDROID_LOG_WARN, kGoogleBreakpad,
"### ### ### ### ### ### ### ### ### ### ### ### ###");
__android_log_write(ANDROID_LOG_WARN, kGoogleBreakpad,
"Chrome build fingerprint:");
__android_log_write(ANDROID_LOG_WARN, kGoogleBreakpad,
android_build_info->package_version_name());
__android_log_write(ANDROID_LOG_WARN, kGoogleBreakpad,
android_build_info->package_version_code());
__android_log_write(ANDROID_LOG_WARN, kGoogleBreakpad,
CHROME_BUILD_ID);
__android_log_write(ANDROID_LOG_WARN, kGoogleBreakpad,
"### ### ### ### ### ### ### ### ### ### ### ### ###");
return false;
}
#endif
bool CrashDone(const MinidumpDescriptor& minidump,
const bool upload,
const bool succeeded) {
// WARNING: this code runs in a compromised context. It may not call into
// libc nor allocate memory normally.
if (!succeeded) {
const char msg[] = "Failed to generate minidump.";
WriteLog(msg, sizeof(msg) - 1);
return false;
}
DCHECK(!minidump.IsFD());
BreakpadInfo info = {0};
info.filename = minidump.path();
info.fd = minidump.fd();
#if defined(ADDRESS_SANITIZER)
google_breakpad::PageAllocator allocator;
const size_t log_path_len = my_strlen(minidump.path());
char* log_path = reinterpret_cast<char*>(allocator.Alloc(log_path_len + 1));
my_memcpy(log_path, minidump.path(), log_path_len);
my_memcpy(log_path + log_path_len - 4, ".log", 4);
log_path[log_path_len] = '\0';
info.log_filename = log_path;
#endif
info.process_type = "browser";
info.process_type_length = 7;
info.crash_url = NULL;
info.crash_url_length = 0;
info.guid = child_process_logging::g_client_id;
info.guid_length = my_strlen(child_process_logging::g_client_id);
info.distro = base::g_linux_distro;
info.distro_length = my_strlen(base::g_linux_distro);
info.upload = upload;
info.process_start_time = g_process_start_time;
info.oom_size = base::g_oom_size;
info.pid = 0;
info.crash_keys = g_crash_keys;
HandleCrashDump(info);
#if defined(OS_ANDROID)
return FinalizeCrashDoneAndroid();
#else
return true;
#endif
}
// Wrapper function, do not add more code here.
bool CrashDoneNoUpload(const MinidumpDescriptor& minidump,
void* context,
bool succeeded) {
return CrashDone(minidump, false, succeeded);
}
#if !defined(OS_ANDROID)
// Wrapper function, do not add more code here.
bool CrashDoneUpload(const MinidumpDescriptor& minidump,
void* context,
bool succeeded) {
return CrashDone(minidump, true, succeeded);
}
#endif
#if defined(ADDRESS_SANITIZER)
extern "C"
void __asan_set_error_report_callback(void (*cb)(const char*));
extern "C"
void AsanLinuxBreakpadCallback(const char* report) {
g_asan_report_str = report;
// Send minidump here.
g_breakpad->SimulateSignalDelivery(SIGKILL);
}
#endif
void EnableCrashDumping(bool unattended) {
g_is_crash_reporter_enabled = true;
base::FilePath tmp_path("/tmp");
PathService::Get(base::DIR_TEMP, &tmp_path);
base::FilePath dumps_path(tmp_path);
if (PathService::Get(breakpad::DIR_CRASH_DUMPS, &dumps_path)) {
base::FilePath logfile =
dumps_path.AppendASCII(CrashUploadList::kReporterLogFilename);
std::string logfile_str = logfile.value();
const size_t crash_log_path_len = logfile_str.size() + 1;
g_crash_log_path = new char[crash_log_path_len];
strncpy(g_crash_log_path, logfile_str.c_str(), crash_log_path_len);
}
DCHECK(!g_breakpad);
MinidumpDescriptor minidump_descriptor(dumps_path.value());
minidump_descriptor.set_size_limit(kMaxMinidumpFileSize);
#if defined(OS_ANDROID)
unattended = true; // Android never uploads directly.
#endif
if (unattended) {
g_breakpad = new ExceptionHandler(
minidump_descriptor,
NULL,
CrashDoneNoUpload,
NULL,
true, // Install handlers.
-1); // Server file descriptor. -1 for in-process.
return;
}
#if !defined(OS_ANDROID)
// Attended mode
g_breakpad = new ExceptionHandler(
minidump_descriptor,
NULL,
CrashDoneUpload,
NULL,
true, // Install handlers.
-1); // Server file descriptor. -1 for in-process.
#endif
}
#if defined(OS_ANDROID)
bool CrashDoneInProcessNoUpload(
const google_breakpad::MinidumpDescriptor& descriptor,
void* context,
const bool succeeded) {
// WARNING: this code runs in a compromised context. It may not call into
// libc nor allocate memory normally.
if (!succeeded) {
static const char msg[] = "Crash dump generation failed.\n";
WriteLog(msg, sizeof(msg) - 1);
return false;
}
// Start constructing the message to send to the browser.
char guid[kGuidSize + 1] = {0};
char crash_url[kMaxActiveURLSize + 1] = {0};
char distro[kDistroSize + 1] = {0};
size_t guid_length = 0;
size_t crash_url_length = 0;
size_t distro_length = 0;
PopulateGUIDAndURLAndDistro(guid, &guid_length, crash_url, &crash_url_length,
distro, &distro_length);
BreakpadInfo info = {0};
info.filename = NULL;
info.fd = descriptor.fd();
info.process_type = g_process_type;
info.process_type_length = my_strlen(g_process_type);
info.crash_url = crash_url;
info.crash_url_length = crash_url_length;
info.guid = guid;
info.guid_length = guid_length;
info.distro = distro;
info.distro_length = distro_length;
info.upload = false;
info.process_start_time = g_process_start_time;
HandleCrashDump(info);
return FinalizeCrashDoneAndroid();
}
void EnableNonBrowserCrashDumping(int minidump_fd) {
// This will guarantee that the BuildInfo has been initialized and subsequent
// calls will not require memory allocation.
base::android::BuildInfo::GetInstance();
SetClientIdFromCommandLine(*CommandLine::ForCurrentProcess());
// On Android, the current sandboxing uses process isolation, in which the
// child process runs with a different UID. That breaks the normal crash
// reporting where the browser process generates the minidump by inspecting
// the child process. This is because the browser process now does not have
// the permission to access the states of the child process (as it has a
// different UID).
// TODO(jcivelli): http://b/issue?id=6776356 we should use a watchdog
// process forked from the renderer process that generates the minidump.
if (minidump_fd == -1) {
LOG(ERROR) << "Minidump file descriptor not found, crash reporting will "
" not work.";
return;
}
SetProcessStartTime();
g_is_crash_reporter_enabled = true;
// Save the process type (it is leaked).
const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
const std::string process_type =
parsed_command_line.GetSwitchValueASCII(switches::kProcessType);
const size_t process_type_len = process_type.size() + 1;
g_process_type = new char[process_type_len];
strncpy(g_process_type, process_type.c_str(), process_type_len);
new google_breakpad::ExceptionHandler(MinidumpDescriptor(minidump_fd),
NULL, CrashDoneInProcessNoUpload, NULL, true, -1);
}
#else
// Non-Browser = Extension, Gpu, Plugins, Ppapi and Renderer
bool NonBrowserCrashHandler(const void* crash_context,
size_t crash_context_size,
void* context) {
const int fd = reinterpret_cast<intptr_t>(context);
int fds[2] = { -1, -1 };
if (sys_socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
static const char msg[] = "Failed to create socket for crash dumping.\n";
WriteLog(msg, sizeof(msg) - 1);
return false;
}
// Start constructing the message to send to the browser.
char guid[kGuidSize + 1] = {0};
char crash_url[kMaxActiveURLSize + 1] = {0};
char distro[kDistroSize + 1] = {0};
PopulateGUIDAndURLAndDistro(guid, NULL, crash_url, NULL, distro, NULL);
char b; // Dummy variable for sys_read below.
const char* b_addr = &b; // Get the address of |b| so we can create the
// expected /proc/[pid]/syscall content in the
// browser to convert namespace tids.
// The length of the control message:
static const unsigned kControlMsgSize = sizeof(fds);
static const unsigned kControlMsgSpaceSize = CMSG_SPACE(kControlMsgSize);
static const unsigned kControlMsgLenSize = CMSG_LEN(kControlMsgSize);
struct kernel_msghdr msg;
my_memset(&msg, 0, sizeof(struct kernel_msghdr));
struct kernel_iovec iov[kCrashIovSize];
iov[0].iov_base = const_cast<void*>(crash_context);
iov[0].iov_len = crash_context_size;
iov[1].iov_base = guid;
iov[1].iov_len = kGuidSize + 1;
iov[2].iov_base = crash_url;
iov[2].iov_len = kMaxActiveURLSize + 1;
iov[3].iov_base = distro;
iov[3].iov_len = kDistroSize + 1;
iov[4].iov_base = &b_addr;
iov[4].iov_len = sizeof(b_addr);
iov[5].iov_base = &fds[0];
iov[5].iov_len = sizeof(fds[0]);
iov[6].iov_base = &g_process_start_time;
iov[6].iov_len = sizeof(g_process_start_time);
iov[7].iov_base = &base::g_oom_size;
iov[7].iov_len = sizeof(base::g_oom_size);
google_breakpad::SerializedNonAllocatingMap* serialized_map;
iov[8].iov_len = g_crash_keys->Serialize(
const_cast<const google_breakpad::SerializedNonAllocatingMap**>(
&serialized_map));
iov[8].iov_base = serialized_map;
#if defined(ADDRESS_SANITIZER)
iov[9].iov_base = const_cast<char*>(g_asan_report_str);
iov[9].iov_len = kMaxAsanReportSize + 1;
#endif
msg.msg_iov = iov;
msg.msg_iovlen = kCrashIovSize;
char cmsg[kControlMsgSpaceSize];
my_memset(cmsg, 0, kControlMsgSpaceSize);
msg.msg_control = cmsg;
msg.msg_controllen = sizeof(cmsg);
struct cmsghdr *hdr = CMSG_FIRSTHDR(&msg);
hdr->cmsg_level = SOL_SOCKET;
hdr->cmsg_type = SCM_RIGHTS;
hdr->cmsg_len = kControlMsgLenSize;
((int*) CMSG_DATA(hdr))[0] = fds[0];
((int*) CMSG_DATA(hdr))[1] = fds[1];
if (HANDLE_EINTR(sys_sendmsg(fd, &msg, 0)) < 0) {
static const char errmsg[] = "Failed to tell parent about crash.\n";
WriteLog(errmsg, sizeof(errmsg) - 1);
IGNORE_RET(sys_close(fds[1]));
return false;
}
IGNORE_RET(sys_close(fds[1]));
if (HANDLE_EINTR(sys_read(fds[0], &b, 1)) != 1) {
static const char errmsg[] = "Parent failed to complete crash dump.\n";
WriteLog(errmsg, sizeof(errmsg) - 1);
}
return true;
}
void EnableNonBrowserCrashDumping() {
const int fd = base::GlobalDescriptors::GetInstance()->Get(kCrashDumpSignal);
g_is_crash_reporter_enabled = true;
// We deliberately leak this object.
DCHECK(!g_breakpad);
g_breakpad = new ExceptionHandler(
MinidumpDescriptor("/tmp"), // Unused but needed or Breakpad will assert.
NULL,
NULL,
reinterpret_cast<void*>(fd), // Param passed to the crash handler.
true,
-1);
g_breakpad->set_crash_handler(NonBrowserCrashHandler);
}
#endif // defined(OS_ANDROID)
void SetCrashKeyValue(const base::StringPiece& key,
const base::StringPiece& value) {
g_crash_keys->SetKeyValue(key.data(), value.data());
}
void ClearCrashKey(const base::StringPiece& key) {
g_crash_keys->RemoveKey(key.data());
}
} // namespace
void LoadDataFromFD(google_breakpad::PageAllocator& allocator,
int fd, bool close_fd, uint8_t** file_data, size_t* size) {
STAT_STRUCT st;
if (FSTAT_FUNC(fd, &st) != 0) {
static const char msg[] = "Cannot upload crash dump: stat failed\n";
WriteLog(msg, sizeof(msg) - 1);
if (close_fd)
IGNORE_RET(sys_close(fd));
return;
}
*file_data = reinterpret_cast<uint8_t*>(allocator.Alloc(st.st_size));
if (!(*file_data)) {
static const char msg[] = "Cannot upload crash dump: cannot alloc\n";
WriteLog(msg, sizeof(msg) - 1);
if (close_fd)
IGNORE_RET(sys_close(fd));
return;
}
my_memset(*file_data, 0xf, st.st_size);
*size = st.st_size;
int byte_read = sys_read(fd, *file_data, *size);
if (byte_read == -1) {
static const char msg[] = "Cannot upload crash dump: read failed\n";
WriteLog(msg, sizeof(msg) - 1);
if (close_fd)
IGNORE_RET(sys_close(fd));
return;
}
if (close_fd)
IGNORE_RET(sys_close(fd));
}
void LoadDataFromFile(google_breakpad::PageAllocator& allocator,
const char* filename,
int* fd, uint8_t** file_data, size_t* size) {
// WARNING: this code runs in a compromised context. It may not call into
// libc nor allocate memory normally.
*fd = sys_open(filename, O_RDONLY, 0);
*size = 0;
if (*fd < 0) {
static const char msg[] = "Cannot upload crash dump: failed to open\n";
WriteLog(msg, sizeof(msg) - 1);
return;
}
LoadDataFromFD(allocator, *fd, true, file_data, size);
}
void HandleCrashDump(const BreakpadInfo& info) {
int dumpfd;
bool keep_fd = false;
size_t dump_size;
uint8_t* dump_data;
google_breakpad::PageAllocator allocator;
if (info.fd != -1) {
// Dump is provided with an open FD.
keep_fd = true;
dumpfd = info.fd;
// The FD is pointing to the end of the file.
// Rewind, we'll read the data next.
if (lseek(dumpfd, 0, SEEK_SET) == -1) {
static const char msg[] = "Cannot upload crash dump: failed to "
"reposition minidump FD\n";
WriteLog(msg, sizeof(msg) - 1);
IGNORE_RET(sys_close(dumpfd));
return;
}
LoadDataFromFD(allocator, info.fd, false, &dump_data, &dump_size);
} else {
// Dump is provided with a path.
keep_fd = false;
LoadDataFromFile(allocator, info.filename, &dumpfd, &dump_data, &dump_size);
}
// TODO(jcivelli): make log work when using FDs.
#if defined(ADDRESS_SANITIZER)
int logfd;
size_t log_size;
uint8_t* log_data;
// Load the AddressSanitizer log into log_data.
LoadDataFromFile(allocator, info.log_filename, &logfd, &log_data, &log_size);
#endif
// We need to build a MIME block for uploading to the server. Since we are
// going to fork and run wget, it needs to be written to a temp file.
const int ufd = sys_open("/dev/urandom", O_RDONLY, 0);
if (ufd < 0) {
static const char msg[] = "Cannot upload crash dump because /dev/urandom"
" is missing\n";
WriteLog(msg, sizeof(msg) - 1);
return;
}
static const char temp_file_template[] =
"/tmp/chromium-upload-XXXXXXXXXXXXXXXX";
char temp_file[sizeof(temp_file_template)];
int temp_file_fd = -1;
if (keep_fd) {
temp_file_fd = dumpfd;
// Rewind the destination, we are going to overwrite it.
if (lseek(dumpfd, 0, SEEK_SET) == -1) {
static const char msg[] = "Cannot upload crash dump: failed to "
"reposition minidump FD (2)\n";
WriteLog(msg, sizeof(msg) - 1);
IGNORE_RET(sys_close(dumpfd));
return;
}
} else {
if (info.upload) {
memcpy(temp_file, temp_file_template, sizeof(temp_file_template));
for (unsigned i = 0; i < 10; ++i) {
uint64_t t;
sys_read(ufd, &t, sizeof(t));
write_uint64_hex(temp_file + sizeof(temp_file) - (16 + 1), t);
temp_file_fd = sys_open(temp_file, O_WRONLY | O_CREAT | O_EXCL, 0600);
if (temp_file_fd >= 0)
break;
}
if (temp_file_fd < 0) {
static const char msg[] = "Failed to create temporary file in /tmp: "
"cannot upload crash dump\n";
WriteLog(msg, sizeof(msg) - 1);
IGNORE_RET(sys_close(ufd));
return;
}
} else {
temp_file_fd = sys_open(info.filename, O_WRONLY, 0600);
if (temp_file_fd < 0) {
static const char msg[] = "Failed to save crash dump: failed to open\n";
WriteLog(msg, sizeof(msg) - 1);
IGNORE_RET(sys_close(ufd));
return;
}
}
}
// The MIME boundary is 28 hyphens, followed by a 64-bit nonce and a NUL.
char mime_boundary[28 + 16 + 1];
my_memset(mime_boundary, '-', 28);
uint64_t boundary_rand;
sys_read(ufd, &boundary_rand, sizeof(boundary_rand));
write_uint64_hex(mime_boundary + 28, boundary_rand);
mime_boundary[28 + 16] = 0;
IGNORE_RET(sys_close(ufd));
// The MIME block looks like this:
// BOUNDARY \r\n
// Content-Disposition: form-data; name="prod" \r\n \r\n
// Chrome_Linux \r\n
// BOUNDARY \r\n
// Content-Disposition: form-data; name="ver" \r\n \r\n
// 1.2.3.4 \r\n
// BOUNDARY \r\n
// Content-Disposition: form-data; name="guid" \r\n \r\n
// 1.2.3.4 \r\n
// BOUNDARY \r\n
//
// zero or one:
// Content-Disposition: form-data; name="ptime" \r\n \r\n
// abcdef \r\n
// BOUNDARY \r\n
//
// zero or one:
// Content-Disposition: form-data; name="ptype" \r\n \r\n
// abcdef \r\n
// BOUNDARY \r\n
//
// zero or more gpu entries:
// Content-Disposition: form-data; name="gpu-xxxxx" \r\n \r\n
// <gpu-xxxxx> \r\n
// BOUNDARY \r\n
//
// zero or one:
// Content-Disposition: form-data; name="lsb-release" \r\n \r\n
// abcdef \r\n
// BOUNDARY \r\n
//
// zero or more:
// Content-Disposition: form-data; name="url-chunk-1" \r\n \r\n
// abcdef \r\n
// BOUNDARY \r\n
//
// zero or one:
// Content-Disposition: form-data; name="channel" \r\n \r\n
// beta \r\n
// BOUNDARY \r\n
//
// zero or one:
// Content-Disposition: form-data; name="num-views" \r\n \r\n
// 3 \r\n
// BOUNDARY \r\n
//
// zero or one:
// Content-Disposition: form-data; name="num-extensions" \r\n \r\n
// 5 \r\n
// BOUNDARY \r\n
//
// zero to 10:
// Content-Disposition: form-data; name="extension-1" \r\n \r\n
// abcdefghijklmnopqrstuvwxyzabcdef \r\n
// BOUNDARY \r\n
//
// zero to 4:
// Content-Disposition: form-data; name="prn-info-1" \r\n \r\n
// abcdefghijklmnopqrstuvwxyzabcdef \r\n
// BOUNDARY \r\n
//
// zero or one:
// Content-Disposition: form-data; name="num-switches" \r\n \r\n
// 5 \r\n
// BOUNDARY \r\n
//
// zero to 15:
// Content-Disposition: form-data; name="switch-1" \r\n \r\n
// --foo \r\n
// BOUNDARY \r\n
//
// zero or one:
// Content-Disposition: form-data; name="oom-size" \r\n \r\n
// 1234567890 \r\n
// BOUNDARY \r\n
//
// zero or more (up to CrashKeyStorage::num_entries = 64):
// Content-Disposition: form-data; name=crash-key-name \r\n
// crash-key-value \r\n
// BOUNDARY \r\n
//
// Content-Disposition: form-data; name="dump"; filename="dump" \r\n
// Content-Type: application/octet-stream \r\n \r\n
// <dump contents>
// \r\n BOUNDARY -- \r\n
MimeWriter writer(temp_file_fd, mime_boundary);
{
#if defined(OS_ANDROID)
static const char chrome_product_msg[] = "Chrome_Android";
#elif defined(OS_CHROMEOS)
static const char chrome_product_msg[] = "Chrome_ChromeOS";
#else // OS_LINUX
#if !defined(ADDRESS_SANITIZER)
static const char chrome_product_msg[] = "Chrome_Linux";
#else
static const char chrome_product_msg[] = "Chrome_Linux_ASan";
#endif
#endif
static const char version_msg[] = PRODUCT_VERSION;
writer.AddBoundary();
writer.AddPairString("prod", chrome_product_msg);
writer.AddBoundary();
writer.AddPairString("ver", version_msg);
writer.AddBoundary();
writer.AddPairString("guid", info.guid);
writer.AddBoundary();
if (info.pid > 0) {
char pid_value_buf[kUint64StringSize];
uint64_t pid_value_len = my_uint64_len(info.pid);
my_uint64tos(pid_value_buf, info.pid, pid_value_len);
static const char pid_key_name[] = "pid";
writer.AddPairData(pid_key_name, sizeof(pid_key_name) - 1,
pid_value_buf, pid_value_len);
writer.AddBoundary();
}
#if defined(OS_ANDROID)
// Addtional MIME blocks are added for logging on Android devices.
static const char android_build_id[] = "android_build_id";
static const char android_build_fp[] = "android_build_fp";
static const char device[] = "device";
static const char model[] = "model";
static const char brand[] = "brand";
static const char exception_info[] = "exception_info";
base::android::BuildInfo* android_build_info =
base::android::BuildInfo::GetInstance();
writer.AddPairString(
android_build_id, android_build_info->android_build_id());
writer.AddBoundary();
writer.AddPairString(
android_build_fp, android_build_info->android_build_fp());
writer.AddBoundary();
writer.AddPairString(device, android_build_info->device());
writer.AddBoundary();
writer.AddPairString(model, android_build_info->model());
writer.AddBoundary();
writer.AddPairString(brand, android_build_info->brand());
writer.AddBoundary();
if (android_build_info->java_exception_info() != NULL) {
writer.AddPairString(exception_info,
android_build_info->java_exception_info());
writer.AddBoundary();
}
#endif
writer.Flush();
}
if (info.process_start_time > 0) {
struct kernel_timeval tv;
if (!sys_gettimeofday(&tv, NULL)) {
uint64_t time = kernel_timeval_to_ms(&tv);
if (time > info.process_start_time) {
time -= info.process_start_time;
char time_str[kUint64StringSize];
const unsigned time_len = my_uint64_len(time);
my_uint64tos(time_str, time, time_len);
static const char process_time_msg[] = "ptime";
writer.AddPairData(process_time_msg, sizeof(process_time_msg) - 1,
time_str, time_len);
writer.AddBoundary();
writer.Flush();
}
}
}
if (info.process_type_length) {
writer.AddPairString("ptype", info.process_type);
writer.AddBoundary();
writer.Flush();
}
// If GPU info is known, send it.
if (*child_process_logging::g_gpu_vendor_id) {
#if !defined(OS_ANDROID)
static const char vendor_msg[] = "gpu-venid";
static const char device_msg[] = "gpu-devid";
#endif
static const char gl_vendor_msg[] = "gpu-gl-vendor";
static const char gl_renderer_msg[] = "gpu-gl-renderer";
static const char driver_msg[] = "gpu-driver";
static const char psver_msg[] = "gpu-psver";
static const char vsver_msg[] = "gpu-vsver";
#if !defined(OS_ANDROID)
writer.AddPairString(vendor_msg, child_process_logging::g_gpu_vendor_id);
writer.AddBoundary();
writer.AddPairString(device_msg, child_process_logging::g_gpu_device_id);
writer.AddBoundary();
#endif
writer.AddPairString(gl_vendor_msg, child_process_logging::g_gpu_gl_vendor);
writer.AddBoundary();
writer.AddPairString(gl_renderer_msg,
child_process_logging::g_gpu_gl_renderer);
writer.AddBoundary();
writer.AddPairString(driver_msg, child_process_logging::g_gpu_driver_ver);
writer.AddBoundary();
writer.AddPairString(psver_msg, child_process_logging::g_gpu_ps_ver);
writer.AddBoundary();
writer.AddPairString(vsver_msg, child_process_logging::g_gpu_vs_ver);
writer.AddBoundary();
writer.Flush();
}
if (info.distro_length) {
static const char distro_msg[] = "lsb-release";
writer.AddPairString(distro_msg, info.distro);
writer.AddBoundary();
writer.Flush();
}
// For renderers and plugins.
if (info.crash_url_length) {
static const char url_chunk_msg[] = "url-chunk-";
static const unsigned kMaxUrlLength = 8 * MimeWriter::kMaxCrashChunkSize;
writer.AddPairDataInChunks(url_chunk_msg, sizeof(url_chunk_msg) - 1,
info.crash_url, std::min(info.crash_url_length, kMaxUrlLength),
MimeWriter::kMaxCrashChunkSize, false /* Don't strip whitespaces. */);
}
if (*child_process_logging::g_channel) {
writer.AddPairString("channel", child_process_logging::g_channel);
writer.AddBoundary();
writer.Flush();
}
if (*child_process_logging::g_num_views) {
writer.AddPairString("num-views", child_process_logging::g_num_views);
writer.AddBoundary();
writer.Flush();
}
if (*child_process_logging::g_num_extensions) {
writer.AddPairString("num-extensions",
child_process_logging::g_num_extensions);
writer.AddBoundary();
writer.Flush();
}
unsigned extension_ids_len =
my_strlen(child_process_logging::g_extension_ids);
if (extension_ids_len) {
static const char extension_msg[] = "extension-";
static const unsigned kMaxExtensionsLen =
kMaxReportedActiveExtensions * child_process_logging::kExtensionLen;
writer.AddPairDataInChunks(extension_msg, sizeof(extension_msg) - 1,
child_process_logging::g_extension_ids,
std::min(extension_ids_len, kMaxExtensionsLen),
child_process_logging::kExtensionLen,
false /* Don't strip whitespace. */);
}
unsigned printer_info_len =
my_strlen(child_process_logging::g_printer_info);
if (printer_info_len) {
static const char printer_info_msg[] = "prn-info-";
static const unsigned kMaxPrnInfoLen =
kMaxReportedPrinterRecords * child_process_logging::kPrinterInfoStrLen;
writer.AddPairDataInChunks(printer_info_msg, sizeof(printer_info_msg) - 1,
child_process_logging::g_printer_info,
std::min(printer_info_len, kMaxPrnInfoLen),
child_process_logging::kPrinterInfoStrLen,
true);
}
if (*child_process_logging::g_num_switches) {
writer.AddPairString("num-switches",
child_process_logging::g_num_switches);
writer.AddBoundary();
writer.Flush();
}
unsigned switches_len =
my_strlen(child_process_logging::g_switches);
if (switches_len) {
static const char switch_msg[] = "switch-";
static const unsigned kMaxSwitchLen =
kMaxSwitches * child_process_logging::kSwitchLen;
writer.AddPairDataInChunks(switch_msg, sizeof(switch_msg) - 1,
child_process_logging::g_switches,
std::min(switches_len, kMaxSwitchLen),
child_process_logging::kSwitchLen,
true /* Strip whitespace since switches are padded to kSwitchLen. */);
}
if (*child_process_logging::g_num_variations) {
writer.AddPairString("num-experiments",
child_process_logging::g_num_variations);
writer.AddBoundary();
writer.Flush();
}
unsigned variation_chunks_len =
my_strlen(child_process_logging::g_variation_chunks);
if (variation_chunks_len) {
static const char variation_msg[] = "experiment-chunk-";
static const unsigned kMaxVariationsLen =
kMaxReportedVariationChunks * kMaxVariationChunkSize;
writer.AddPairDataInChunks(variation_msg, sizeof(variation_msg) - 1,
child_process_logging::g_variation_chunks,
std::min(variation_chunks_len, kMaxVariationsLen),
kMaxVariationChunkSize,
true /* Strip whitespace since variation chunks are padded. */);
}
if (info.oom_size) {
char oom_size_str[kUint64StringSize];
const unsigned oom_size_len = my_uint64_len(info.oom_size);
my_uint64tos(oom_size_str, info.oom_size, oom_size_len);
static const char oom_size_msg[] = "oom-size";
writer.AddPairData(oom_size_msg, sizeof(oom_size_msg) - 1,
oom_size_str, oom_size_len);
writer.AddBoundary();
writer.Flush();
}
if (info.crash_keys) {
CrashKeyStorage::Iterator crash_key_iterator(*info.crash_keys);
const CrashKeyStorage::Entry* entry;
while ((entry = crash_key_iterator.Next())) {
writer.AddPairString(entry->key, entry->value);
writer.AddBoundary();
writer.Flush();
}
}
writer.AddFileContents(g_dump_msg, dump_data, dump_size);
#if defined(ADDRESS_SANITIZER)
// Append a multipart boundary and the contents of the AddressSanitizer log.
writer.AddBoundary();
writer.AddFileContents(g_log_msg, log_data, log_size);
#endif
writer.AddEnd();
writer.Flush();
IGNORE_RET(sys_close(temp_file_fd));
#if defined(OS_ANDROID)
if (info.filename) {
int filename_length = my_strlen(info.filename);
// If this was a file, we need to copy it to the right place and use the
// right file name so it gets uploaded by the browser.
const char msg[] = "Output crash dump file:";
WriteLog(msg, sizeof(msg) - 1);
WriteLog(info.filename, filename_length - 1);
char pid_buf[kUint64StringSize];
uint64_t pid_str_length = my_uint64_len(info.pid);
my_uint64tos(pid_buf, info.pid, pid_str_length);
// -1 because we won't need the null terminator on the original filename.
unsigned done_filename_len = filename_length - 1 + pid_str_length;
char* done_filename = reinterpret_cast<char*>(
allocator.Alloc(done_filename_len));
// Rename the file such that the pid is the suffix in order signal to other
// processes that the minidump is complete. The advantage of using the pid
// as the suffix is that it is trivial to associate the minidump with the
// crashed process.
// Finally, note strncpy prevents null terminators from
// being copied. Pad the rest with 0's.
my_strncpy(done_filename, info.filename, done_filename_len);
// Append the suffix a null terminator should be added.
my_strncat(done_filename, pid_buf, pid_str_length);
// Rename the minidump file to signal that it is complete.
if (rename(info.filename, done_filename)) {
const char failed_msg[] = "Failed to rename:";
WriteLog(failed_msg, sizeof(failed_msg) - 1);
WriteLog(info.filename, filename_length - 1);
const char to_msg[] = "to";
WriteLog(to_msg, sizeof(to_msg) - 1);
WriteLog(done_filename, done_filename_len - 1);
}
}
#endif
if (!info.upload)
return;
// The --header argument to wget looks like:
// --header=Content-Type: multipart/form-data; boundary=XYZ
// where the boundary has two fewer leading '-' chars
static const char header_msg[] =
"--header=Content-Type: multipart/form-data; boundary=";
char* const header = reinterpret_cast<char*>(allocator.Alloc(
sizeof(header_msg) - 1 + sizeof(mime_boundary) - 2));
memcpy(header, header_msg, sizeof(header_msg) - 1);
memcpy(header + sizeof(header_msg) - 1, mime_boundary + 2,
sizeof(mime_boundary) - 2);
// We grab the NUL byte from the end of |mime_boundary|.
// The --post-file argument to wget looks like:
// --post-file=/tmp/...
static const char post_file_msg[] = "--post-file=";
char* const post_file = reinterpret_cast<char*>(allocator.Alloc(
sizeof(post_file_msg) - 1 + sizeof(temp_file)));
memcpy(post_file, post_file_msg, sizeof(post_file_msg) - 1);
memcpy(post_file + sizeof(post_file_msg) - 1, temp_file, sizeof(temp_file));
const pid_t child = sys_fork();
if (!child) {
// Spawned helper process.
//
// This code is called both when a browser is crashing (in which case,
// nothing really matters any more) and when a renderer/plugin crashes, in
// which case we need to continue.
//
// Since we are a multithreaded app, if we were just to fork(), we might
// grab file descriptors which have just been created in another thread and
// hold them open for too long.
//
// Thus, we have to loop and try and close everything.
const int fd = sys_open("/proc/self/fd", O_DIRECTORY | O_RDONLY, 0);
if (fd < 0) {
for (unsigned i = 3; i < 8192; ++i)
IGNORE_RET(sys_close(i));
} else {
google_breakpad::DirectoryReader reader(fd);
const char* name;
while (reader.GetNextEntry(&name)) {
int i;
if (my_strtoui(&i, name) && i > 2 && i != fd)
IGNORE_RET(sys_close(i));
reader.PopEntry();
}
IGNORE_RET(sys_close(fd));
}
IGNORE_RET(sys_setsid());
// Leave one end of a pipe in the wget process and watch for it getting
// closed by the wget process exiting.
int fds[2];
if (sys_pipe(fds) >= 0) {
const pid_t wget_child = sys_fork();
if (!wget_child) {
// Wget process.
IGNORE_RET(sys_close(fds[0]));
IGNORE_RET(sys_dup2(fds[1], 3));
static const char kWgetBinary[] = "/usr/bin/wget";
const char* args[] = {
kWgetBinary,
header,
post_file,
kUploadURL,
"--timeout=10", // Set a timeout so we don't hang forever.
"--tries=1", // Don't retry if the upload fails.
"-O", // output reply to fd 3
"/dev/fd/3",
NULL,
};
execve(kWgetBinary, const_cast<char**>(args), environ);
static const char msg[] = "Cannot upload crash dump: cannot exec "
"/usr/bin/wget\n";
WriteLog(msg, sizeof(msg) - 1);
sys__exit(1);
}
// Helper process.
if (wget_child > 0) {
IGNORE_RET(sys_close(fds[1]));
char id_buf[17]; // Crash report IDs are expected to be 16 chars.
ssize_t len = -1;
// Wget should finish in about 10 seconds. Add a few more 500 ms
// internals to account for process startup time.
for (size_t wait_count = 0; wait_count < 24; ++wait_count) {
struct kernel_pollfd poll_fd;
poll_fd.fd = fds[0];
poll_fd.events = POLLIN | POLLPRI | POLLERR;
int ret = sys_poll(&poll_fd, 1, 500);
if (ret < 0) {
// Error
break;
} else if (ret > 0) {
// There is data to read.
len = HANDLE_EINTR(sys_read(fds[0], id_buf, sizeof(id_buf) - 1));
break;
}
// ret == 0 -> timed out, continue waiting.
}
if (len > 0) {
// Write crash dump id to stderr.
id_buf[len] = 0;
static const char msg[] = "\nCrash dump id: ";
WriteLog(msg, sizeof(msg) - 1);
WriteLog(id_buf, my_strlen(id_buf));
WriteLog("\n", 1);
// Write crash dump id to crash log as: seconds_since_epoch,crash_id
struct kernel_timeval tv;
if (g_crash_log_path && !sys_gettimeofday(&tv, NULL)) {
uint64_t time = kernel_timeval_to_ms(&tv) / 1000;
char time_str[kUint64StringSize];
const unsigned time_len = my_uint64_len(time);
my_uint64tos(time_str, time, time_len);
int log_fd = sys_open(g_crash_log_path,
O_CREAT | O_WRONLY | O_APPEND,
0600);
if (log_fd > 0) {
sys_write(log_fd, time_str, time_len);
sys_write(log_fd, ",", 1);
sys_write(log_fd, id_buf, my_strlen(id_buf));
sys_write(log_fd, "\n", 1);
IGNORE_RET(sys_close(log_fd));
}
}
}
if (sys_waitpid(wget_child, NULL, WNOHANG) == 0) {
// Wget process is still around, kill it.
sys_kill(wget_child, SIGKILL);
}
}
}
// Helper process.
IGNORE_RET(sys_unlink(info.filename));
#if defined(ADDRESS_SANITIZER)
IGNORE_RET(sys_unlink(info.log_filename));
#endif
IGNORE_RET(sys_unlink(temp_file));
sys__exit(0);
}
// Main browser process.
if (child <= 0)
return;
(void) HANDLE_EINTR(sys_waitpid(child, NULL, 0));
}
void InitCrashReporter() {
#if defined(OS_ANDROID)
// This will guarantee that the BuildInfo has been initialized and subsequent
// calls will not require memory allocation.
base::android::BuildInfo::GetInstance();
#endif
// Determine the process type and take appropriate action.
const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
if (parsed_command_line.HasSwitch(switches::kDisableBreakpad))
return;
// By setting the BREAKPAD_DUMP_LOCATION environment variable, an alternate
// location to write brekapad crash dumps can be set.
const char* alternate_minidump_location = getenv("BREAKPAD_DUMP_LOCATION");
if (alternate_minidump_location) {
base::FilePath alternate_minidump_location_path(
alternate_minidump_location);
PathService::Override(
breakpad::DIR_CRASH_DUMPS,
base::FilePath(alternate_minidump_location));
}
const std::string process_type =
parsed_command_line.GetSwitchValueASCII(switches::kProcessType);
if (process_type.empty()) {
EnableCrashDumping(getenv(env_vars::kHeadless) != NULL);
} else if (process_type == switches::kRendererProcess ||
process_type == switches::kPluginProcess ||
process_type == switches::kPpapiPluginProcess ||
process_type == switches::kZygoteProcess ||
process_type == switches::kGpuProcess) {
#if defined(OS_ANDROID)
NOTREACHED() << "Breakpad initialized with InitCrashReporter() instead of "
"InitNonBrowserCrashReporter in " << process_type << " process.";
return;
#else
// We might be chrooted in a zygote or renderer process so we cannot call
// GetCollectStatsConsent because that needs access the the user's home
// dir. Instead, we set a command line flag for these processes.
// Even though plugins are not chrooted, we share the same code path for
// simplicity.
if (!parsed_command_line.HasSwitch(switches::kEnableCrashReporter))
return;
SetClientIdFromCommandLine(parsed_command_line);
EnableNonBrowserCrashDumping();
VLOG(1) << "Non Browser crash dumping enabled for: " << process_type;
#endif // #if defined(OS_ANDROID)
}
SetProcessStartTime();
logging::SetDumpWithoutCrashingFunction(&DumpProcess);
#if defined(ADDRESS_SANITIZER)
// Register the callback for AddressSanitizer error reporting.
__asan_set_error_report_callback(AsanLinuxBreakpadCallback);
#endif
g_crash_keys = new CrashKeyStorage;
crash_keys::RegisterChromeCrashKeys();
base::debug::SetCrashKeyReportingFunctions(
&SetCrashKeyValue, &ClearCrashKey);
}
#if defined(OS_ANDROID)
void InitNonBrowserCrashReporterForAndroid() {
const CommandLine* command_line = CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(switches::kEnableCrashReporter)) {
// On Android we need to provide a FD to the file where the minidump is
// generated as the renderer and browser run with different UIDs
// (preventing the browser from inspecting the renderer process).
int minidump_fd = base::GlobalDescriptors::GetInstance()->
MaybeGet(kAndroidMinidumpDescriptor);
if (minidump_fd == base::kInvalidPlatformFileValue) {
NOTREACHED() << "Could not find minidump FD, crash reporting disabled.";
} else {
EnableNonBrowserCrashDumping(minidump_fd);
}
}
}
#endif // OS_ANDROID
bool IsCrashReporterEnabled() {
return g_is_crash_reporter_enabled;
}
|