-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTbbLogger.patch
919 lines (914 loc) · 29.7 KB
/
TbbLogger.patch
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
diff --git a/dom/ipc/ContentChild.cpp b/dom/ipc/ContentChild.cpp
index 7efd288ba11e..7ef64137f30a 100644
--- a/dom/ipc/ContentChild.cpp
+++ b/dom/ipc/ContentChild.cpp
@@ -4,6 +4,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+#include "TbbLogger.h"
+
#ifdef MOZ_WIDGET_ANDROID
# include "AndroidDecoderModule.h"
#endif
@@ -703,6 +705,7 @@ class nsGtkNativeInitRunnable : public Runnable {
void ContentChild::Init(base::ProcessId aParentPid, const char* aParentBuildID,
mozilla::ipc::ScopedPort aPort, uint64_t aChildID,
bool aIsForBrowser) {
+ TBB_LOG("Child Process {} Launch", aChildID);
#ifdef MOZ_WIDGET_GTK
// When running X11 only build we need to pass a display down
// to gtk_init because it's not going to use the one from the environment
diff --git a/security/sandbox/win/src/sandboxbroker/sandboxBroker.cpp b/security/sandbox/win/src/sandboxbroker/sandboxBroker.cpp
index 0f884b30c3ac..79ab59091f34 100644
--- a/security/sandbox/win/src/sandboxbroker/sandboxBroker.cpp
+++ b/security/sandbox/win/src/sandboxbroker/sandboxBroker.cpp
@@ -274,7 +274,6 @@ bool SandboxBroker::LaunchApp(const wchar_t* aPath, const wchar_t* aArguments,
ApplyLoggingPolicy();
}
-#if defined(DEBUG)
// Allow write access to TEMP directory in debug builds for logging purposes.
// The path from GetTempPathW can have a length up to MAX_PATH + 1, including
// the null, so we need MAX_PATH + 2, so we can add an * to the end.
@@ -287,7 +286,6 @@ bool SandboxBroker::LaunchApp(const wchar_t* aPath, const wchar_t* aArguments,
mPolicy->AddRule(sandbox::TargetPolicy::SUBSYS_FILES,
sandbox::TargetPolicy::FILES_ALLOW_ANY, tempPath);
}
-#endif
// Enable the child process to write log files when setup
AddMozLogRulesToPolicy(mPolicy, aEnvironment);
diff --git a/toolkit/xre/nsAppRunner.cpp b/toolkit/xre/nsAppRunner.cpp
index c5e285c7e746..128d93794f0c 100644
--- a/toolkit/xre/nsAppRunner.cpp
+++ b/toolkit/xre/nsAppRunner.cpp
@@ -3,6 +3,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+#include "TbbLogger.h"
+
#include "mozilla/dom/ContentParent.h"
#include "mozilla/dom/ContentChild.h"
#include "mozilla/ipc/GeckoChildProcessHost.h"
@@ -5977,6 +5979,7 @@ static already_AddRefed<nsIFile> GreOmniPath(int argc, char** argv) {
* .app/Contents/Resources.
*/
int XREMain::XRE_main(int argc, char* argv[], const BootstrapConfig& aConfig) {
+ TBB_LOG("Parent Process Launch");
gArgc = argc;
gArgv = argv;
diff --git a/xpcom/build/TbbLogger.h b/xpcom/build/TbbLogger.h
new file mode 100644
index 000000000000..f1fe6e2ceb2a
--- /dev/null
+++ b/xpcom/build/TbbLogger.h
@@ -0,0 +1,839 @@
+#ifndef TBB_LOGGER_H
+#define TBB_LOGGER_H
+
+// C
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <stddef.h>
+#include <stdio.h>
+
+// C++
+#include <atomic>
+#include <memory>
+#include <vector>
+#include <functional>
+#include <type_traits>
+#include <utility>
+#include <string>
+
+#ifdef _WIN32
+# include <windows.h>
+#else
+# include <unistd.h>
+# include <sys/prctl.h>
+# include <sys/stat.h>
+# include <sys/types.h>
+# include <sys/syscall.h>
+#endif
+
+#define TBB_LOG_IMPL(...) tbb::logger::log(__FUNCTION__, __FILE__, __LINE__, __VA_ARGS__)
+
+#if 0
+#define TBB_LOG(...) (void)(0)
+#else
+#define TBB_LOG(...) TBB_LOG_IMPL(__VA_ARGS__)
+#endif
+#define TBB_TRACE(...) TBB_LOG("")
+
+namespace tbb
+{
+ namespace serialization
+ {
+ enum class data_type : uint8_t
+ {
+ invalid = 0xFF,
+ // strings
+ utf8 = 0,
+ utf16,
+ utf32,
+ // pointers
+ p32,
+ p64,
+ // integers
+ i8,
+ u8,
+ i16,
+ u16,
+ i32,
+ u32,
+ i64,
+ u64,
+ // floating points
+ f32,
+ f64,
+ };
+
+ // used to determine size of param in bytes
+ constexpr size_t param_size() { return 0; }
+
+ // string size of unknown size implementation
+ size_t param_size_impl(const char* str);
+ size_t param_size_impl(const char16_t* str);
+ size_t param_size_impl(const char32_t* str);
+ size_t param_size_impl(const wchar_t* str);
+ // arbitatry pointer size impemetation
+ constexpr size_t param_size_impl(const void*) { return sizeof(void*) + sizeof(data_type); }
+ // fixed string literal size
+ template<size_t N>
+ constexpr size_t param_size(const char (&str)[N]) { return sizeof(char) * N + sizeof(data_type); }
+ template<size_t N>
+ constexpr size_t param_size(const char16_t (&str)[N]){ return sizeof(char16_t) * N + sizeof(data_type); }
+ template<size_t N>
+ constexpr size_t param_size(const char32_t (&str)[N]){ return sizeof(char32_t) * N + sizeof(data_type); }
+ template<size_t N>
+ constexpr size_t param_size(const wchar_t (&str)[N]) { return sizeof(wchar_t) * N + sizeof(data_type); }
+ // read/write string buffer size
+ size_t param_size(char str[]);
+ size_t param_size(char16_t str[]);
+ size_t param_size(char32_t str[]);
+ size_t param_size(wchar_t str[]);
+ // catch-all template functionf or pointers
+ template<typename T>
+ typename std::enable_if<
+ std::is_pointer<T>::value, size_t>::type
+ param_size(T ptr) { return param_size_impl(ptr); }
+ constexpr size_t param_size(std::nullptr_t) { return sizeof(void*) + sizeof(data_type); }
+ constexpr size_t param_size(int8_t) { return sizeof(int8_t) + sizeof(data_type); }
+ constexpr size_t param_size(uint8_t) { return sizeof(uint8_t) + sizeof(data_type); }
+ constexpr size_t param_size(int16_t) { return sizeof(int16_t) + sizeof(data_type); }
+ constexpr size_t param_size(uint16_t) { return sizeof(uint16_t) + sizeof(data_type); }
+ constexpr size_t param_size(int32_t) { return sizeof(int32_t) + sizeof(data_type); }
+ constexpr size_t param_size(uint32_t) { return sizeof(uint32_t) + sizeof(data_type); }
+ constexpr size_t param_size(int64_t) { return sizeof(int64_t) + sizeof(data_type); }
+ constexpr size_t param_size(uint64_t) { return sizeof(uint64_t) + sizeof(data_type); }
+ constexpr size_t param_size(float) { return sizeof(float) + sizeof(data_type); }
+ constexpr size_t param_size(double) { return sizeof(double) + sizeof(data_type); }
+
+ template<typename FIRST, typename ...ARGS>
+ size_t param_size(FIRST&& first, ARGS&&... args)
+ {
+ return param_size(std::forward<FIRST>(first)) + param_size(std::forward<ARGS>(args)...);
+ }
+
+ uint8_t* pack_param_impl(uint8_t* dest, const char* str);
+ uint8_t* pack_param_impl(uint8_t* dest, const char16_t* str);
+ uint8_t* pack_param_impl(uint8_t* dest, const char32_t* str);
+ uint8_t* pack_param_impl(uint8_t* dest, const wchar_t* str);
+ uint8_t* pack_param_impl(uint8_t* dest, char str[]);
+ uint8_t* pack_param_impl(uint8_t* dest, wchar_t str[]);
+ uint8_t* pack_param_impl(uint8_t* dest, char16_t str[]);
+ uint8_t* pack_param_impl(uint8_t* dest, char32_t str[]);
+ uint8_t* pack_param_impl(uint8_t* dest, const void* ptr);
+ uint8_t* pack_param_impl(uint8_t* dest, void* ptr);
+ uint8_t* pack_param_impl(uint8_t* dest, std::nullptr_t);
+ uint8_t* pack_param_impl(uint8_t* dest, int8_t val);
+ uint8_t* pack_param_impl(uint8_t* dest, uint8_t val);
+ uint8_t* pack_param_impl(uint8_t* dest, int16_t val);
+ uint8_t* pack_param_impl(uint8_t* dest, uint16_t val);
+ uint8_t* pack_param_impl(uint8_t* dest, int32_t val);
+ uint8_t* pack_param_impl(uint8_t* dest, uint32_t val);
+ uint8_t* pack_param_impl(uint8_t* dest, int64_t val);
+ uint8_t* pack_param_impl(uint8_t* dest, uint64_t val);
+ uint8_t* pack_param_impl(uint8_t* dest, float val);
+ uint8_t* pack_param_impl(uint8_t* dest, double val);
+
+ inline uint8_t* pack_param(uint8_t* dest) {return dest;}
+ template<typename FIRST, typename ...ARGS>
+ uint8_t* pack_param(uint8_t* dest, FIRST&& first, ARGS&&... args)
+ {
+ return pack_param(pack_param_impl(dest, std::forward<FIRST>(first)), std::forward<ARGS>(args)...);
+ }
+
+ #pragma pack(1)
+ struct message
+ {
+ uint32_t length;
+ uint32_t process_id;
+ uint32_t thread_id;
+ uint64_t timestamp;
+ };
+ #pragma pack()
+
+ message* alloc_msg(size_t size);
+ void free_msg(message* msg);
+
+ template<typename ...ARGS>
+ size_t msg_size(ARGS&&... args)
+ {
+ return sizeof(message) + serialization::param_size(std::forward<ARGS>(args)...);
+ }
+
+ template<typename ...ARGS>
+ void write_msg(message* msg, ARGS&&... args)
+ {
+ uint8_t* head = reinterpret_cast<uint8_t*>(msg);
+ uint8_t* tail = head + sizeof(message);
+ tail = serialization::pack_param(tail, std::forward<ARGS>(args)...);
+ size_t len = (size_t)(tail - head);
+ *reinterpret_cast<uint32_t*>(head) = static_cast<uint32_t>(len);
+ }
+
+ #define NULL_STRING(PREFIX) PREFIX##"(nil)"
+ #define UTF8_NULLSTRING NULL_STRING(u8)
+ #define UTF16_NULLSTRING NULL_STRING(u)
+ #define UTF32_NULLSTRING NULL_STRING(U)
+ #define WIDE_NULLSTRING NULL_STRING(L)
+
+ template<typename CharType>
+ constexpr size_t param_size_string(const CharType* str)
+ {
+ const auto* head = str;
+ while(*str++ != (CharType)0);
+ return (str - head) * sizeof(CharType) + sizeof(data_type);
+ }
+
+ /// Size Params
+ inline size_t param_size_impl(const char* str)
+ {
+ if (!str) {
+ str = UTF8_NULLSTRING;
+ }
+ return param_size_string(str);
+ }
+ inline size_t param_size_impl(const char16_t* str)
+ {
+ if (!str) {
+ str = UTF16_NULLSTRING;
+ }
+ return param_size_string(str);
+ }
+ inline size_t param_size_impl(const char32_t* str)
+ {
+ if (!str) {
+ str = UTF32_NULLSTRING;
+ }
+ return param_size_string(str);
+ }
+ inline size_t param_size_impl(const wchar_t* str)
+ {
+ if (!str) {
+ str = WIDE_NULLSTRING;
+ }
+ return param_size_string(str);
+ }
+
+ inline size_t param_size(char str[])
+ {
+ return param_size_impl(str);
+ }
+ inline size_t param_size(char16_t str[])
+ {
+ return param_size_impl(str);
+ }
+ inline size_t param_size(char32_t str[])
+ {
+ return param_size_impl(str);
+ }
+ inline size_t param_size(wchar_t str[])
+ {
+ return param_size_impl(str);
+ }
+
+ /// Pack Params
+ template<typename CharType>
+ uint8_t* pack_param_string(uint8_t* dest, const CharType* str)
+ {
+ static_assert((sizeof(CharType) == sizeof(char) ||
+ sizeof(CharType) == sizeof(char16_t) ||
+ sizeof(CharType) == sizeof(char32_t)), "Invalid character size");
+ switch(sizeof(CharType))
+ {
+ case sizeof(char):
+ *dest++ = (uint8_t)data_type::utf8; break;
+ case sizeof(char16_t):
+ *dest++ = (uint8_t)data_type::utf16; break;
+ case sizeof(char32_t):
+ *dest++ = (uint8_t)data_type::utf32; break;
+ }
+ auto* str_dest = reinterpret_cast<CharType*>(dest);
+ while((*str_dest++ = *str++));
+ return reinterpret_cast<uint8_t*>(str_dest);
+ }
+
+ // various string overloads
+ inline uint8_t* pack_param_impl(uint8_t* dest, const char* str)
+ {
+ if (!str) {
+ str = UTF8_NULLSTRING;
+ }
+ return pack_param_string(dest, str);
+ }
+ inline uint8_t* pack_param_impl(uint8_t* dest, const char16_t* str)
+ {
+ if (!str) {
+ str = UTF16_NULLSTRING;
+ }
+ return pack_param_string(dest, str);
+ }
+ inline uint8_t* pack_param_impl(uint8_t* dest, const char32_t* str)
+ {
+ if (!str) {
+ str = UTF32_NULLSTRING;
+ }
+ return pack_param_string(dest, str);
+ }
+ inline uint8_t* pack_param_impl(uint8_t* dest, const wchar_t* str)
+ {
+ if (!str) {
+ str = WIDE_NULLSTRING;
+ }
+ return pack_param_string(dest, str);
+ }
+ inline uint8_t* pack_param_impl(uint8_t* dest, char str[])
+ {
+ return pack_param_impl(dest, (const char*)str);
+ }
+ inline uint8_t* pack_param_impl(uint8_t* dest, char16_t str[])
+ {
+ return pack_param_impl(dest, (const char16_t*)str);
+ }
+ inline uint8_t* pack_param_impl(uint8_t* dest, char32_t str[])
+ {
+ return pack_param_impl(dest, (const char32_t*)str);
+ }
+ inline uint8_t* pack_param_impl(uint8_t* dest, wchar_t str[])
+ {
+ return pack_param_impl(dest, (const wchar_t*)str);
+ }
+
+ inline uint8_t* pack_param_impl(uint8_t* dest, const void* ptr)
+ {
+ static_assert(sizeof(void*) == sizeof(uint32_t) ||
+ sizeof(void*) == sizeof(uint64_t),
+ "Invalid pointer size");
+
+ constexpr size_t N = sizeof(void*);
+ if(N == sizeof(uint32_t)) {
+ *dest++ = (uint8_t)(data_type::p32);
+ } else if(N == sizeof(uint64_t)) {
+ *dest++ = (uint8_t)(data_type::p64);
+ }
+
+ memcpy(dest, &ptr, N);
+ return dest + N;
+ }
+
+ inline uint8_t* pack_param_impl(uint8_t* dest, void* ptr)
+ {
+ return pack_param_impl(dest, (const void*)ptr);
+ }
+
+ inline uint8_t* pack_param_impl(uint8_t* dest, std::nullptr_t)
+ {
+ return pack_param_impl(dest, (void*)nullptr);
+ }
+
+ inline uint8_t* pack_param_impl(uint8_t* dest, int8_t val)
+ {
+ constexpr size_t N = sizeof(int8_t);
+ *dest++ = (uint8_t)(data_type::i8);
+ memcpy(dest, &val, N);
+ return dest + N;
+ }
+
+ inline uint8_t* pack_param_impl(uint8_t* dest, uint8_t val)
+ {
+ constexpr size_t N = sizeof(uint8_t);
+ *dest++ = (uint8_t)(data_type::u8);
+ memcpy(dest, &val, N);
+ return dest + N;
+ }
+
+ inline uint8_t* pack_param_impl(uint8_t* dest, int16_t val)
+ {
+ constexpr size_t N = sizeof(int16_t);
+ *dest++ = (uint8_t)(data_type::i16);
+ memcpy(dest, &val, N);
+ return dest + N;
+ }
+
+ inline uint8_t* pack_param_impl(uint8_t* dest, uint16_t val)
+ {
+ constexpr size_t N = sizeof(uint16_t);
+ *dest++ = (uint8_t)(data_type::u16);
+ memcpy(dest, &val, N);
+ return dest + N;
+ }
+
+ inline uint8_t* pack_param_impl(uint8_t* dest, int32_t val)
+ {
+ constexpr size_t N = sizeof(int32_t);
+ *dest++ = (uint8_t)(data_type::i32);
+ memcpy(dest, &val, N);
+ return dest + N;
+ }
+
+ inline uint8_t* pack_param_impl(uint8_t* dest, uint32_t val)
+ {
+ constexpr size_t N = sizeof(uint32_t);
+ *dest++ = (uint8_t)(data_type::u32);
+ memcpy(dest, &val, N);
+ return dest + N;
+ }
+
+ inline uint8_t* pack_param_impl(uint8_t* dest, int64_t val)
+ {
+ constexpr size_t N = sizeof(int64_t);
+ *dest++ = (uint8_t)(data_type::i64);
+ memcpy(dest, &val, N);
+ return dest + N;
+ }
+
+ inline uint8_t* pack_param_impl(uint8_t* dest, uint64_t val)
+ {
+ constexpr size_t N = sizeof(uint64_t);
+ *dest++ = (uint8_t)(data_type::u64);
+ memcpy(dest, &val, N);
+ return dest + N;
+ }
+
+ inline uint8_t* pack_param_impl(uint8_t* dest, float val)
+ {
+ constexpr size_t N = sizeof(float);
+ *dest++ = (uint8_t)(data_type::f32);
+ memcpy(dest, &val, N);
+ return dest + N;
+ }
+
+ inline uint8_t* pack_param_impl(uint8_t* dest, double val)
+ {
+ constexpr size_t N = sizeof(double);
+ *dest++ = (uint8_t)(data_type::f64);
+ memcpy(dest, &val, N);
+ return dest + N;
+ }
+
+ inline message* alloc_msg(size_t size)
+ {
+ return reinterpret_cast<message*>(new uint8_t[size]);
+ }
+
+ inline void free_msg(message* msg)
+ {
+ delete[] reinterpret_cast<uint8_t*>(msg);
+ }
+ }
+
+ // Utilities
+ namespace internal
+ {
+ // timestamp in nanoseconds
+ inline uint64_t get_timestamp()
+ {
+ constexpr uint64_t NANOSECONDS_PER_SECOND = 1000000000;
+#ifdef _WIN32
+ static LARGE_INTEGER frequency = []()
+ {
+ LARGE_INTEGER frequency;
+ QueryPerformanceFrequency(&frequency);
+ return frequency;
+ }();
+
+ LARGE_INTEGER ticks;
+ QueryPerformanceCounter(&ticks);
+ ticks.QuadPart *= NANOSECONDS_PER_SECOND;
+ ticks.QuadPart /= frequency.QuadPart;
+
+ return ticks.QuadPart;
+#else
+ timespec counter;
+ clock_gettime(CLOCK_MONOTONIC_RAW, &counter);
+ uint64_t retval = uint64_t(counter.tv_sec) * NANOSECONDS_PER_SECOND + uint64_t(counter.tv_nsec);
+ return retval;
+#endif
+ }
+
+ inline uint32_t get_thread_id()
+ {
+
+#ifdef _WIN32
+ static thread_local uint32_t thread_id = GetCurrentThreadId();
+#else
+ static thread_local uint32_t thread_id = syscall(SYS_gettid);
+#endif
+ return thread_id;
+ }
+
+ inline void thread_yield()
+ {
+#ifdef _WIN32
+ SwitchToThread();
+#else
+ pthread_yield();
+#endif
+ }
+
+ inline void thread_sleep(size_t milliseconds)
+ {
+#ifdef _WIN32
+ Sleep(milliseconds);
+#else
+ const auto microseconds = milliseconds * 1000;
+ usleep(microseconds);
+#endif
+ }
+
+ inline void set_thread_name()
+ {
+#ifdef _WIN32
+ // only works on Win10 and up :(
+ typedef HRESULT (WINAPI *SetThreadDescription_t)(HANDLE,PCWSTR);
+ SetThreadDescription_t SetThreadDescription =
+ (SetThreadDescription_t)GetProcAddress(
+ GetModuleHandle(TEXT("kernel32.dll")),
+ "SetThreadDescription");
+
+ if(SetThreadDescription != nullptr)
+ {
+ SetThreadDescription(GetCurrentThread(), L"tbblogger_thread");
+ }
+#else
+ prctl(PR_SET_NAME,"tbblogger_thread",0,0,0);
+#endif // _WIN32
+ }
+
+ inline const char* get_command_line()
+ {
+#ifdef _WIN32
+ return GetCommandLineA();
+#else
+ static char cmdline[2048];
+ FILE* cmdline_file = fopen("/proc/self/cmdline", "rb");
+ size_t len = fread(cmdline, 1, sizeof(cmdline) - 1, cmdline_file);
+ fclose(cmdline_file);
+ for(size_t i = 0; i < len; ++i)
+ {
+ cmdline[i] = (cmdline[i] == 0 ? ' ' : cmdline[i]);
+ }
+ cmdline[len] = 0;
+ return cmdline;
+#endif
+ }
+
+ inline size_t get_temp_path(char* buffer, size_t len)
+ {
+#ifdef _WIN32
+ return GetTempPathA(len, buffer);
+#else
+ const char tmp[] = "/tmp";
+ memcpy(buffer, tmp, sizeof(tmp));
+ return sizeof(tmp) - 1;
+#endif
+ }
+
+ inline int32_t get_child_id()
+ {
+ const char* command_line = get_command_line();
+ if (strstr(command_line, "firefox"))
+ {
+ if (auto childIDStart = strstr(command_line, "-childID"); childIDStart != nullptr)
+ {
+ int32_t retval = -1;
+ sscanf(childIDStart, "-childID %d", &retval);
+ return retval;
+ }
+ return 0;
+ }
+ else
+ {
+ auto hash = std::hash<std::string>{}(command_line);
+ int32_t retval = (int32_t)hash;
+ if (retval > 0) retval *= -1;
+ return retval;
+ }
+ }
+
+#ifdef _WIN32
+ typedef HANDLE file_t;
+#else
+ typedef FILE* file_t;
+#endif
+
+ inline file_t open_file(const char* filename)
+ {
+#ifdef _WIN32
+ return CreateFileA(filename,
+ GENERIC_WRITE,
+ FILE_SHARE_READ,
+ nullptr,
+ CREATE_ALWAYS,
+ FILE_ATTRIBUTE_NORMAL,
+ nullptr);
+#else
+ return fopen(filename, "wb");
+#endif
+ }
+
+ inline file_t get_log_file(int32_t childID)
+ {
+ char filename[1024];
+ char* head = filename;
+ head += get_temp_path(head, sizeof(filename));
+#if _WIN32
+ head += sprintf(head, "firefox");
+ CreateDirectoryA(filename, nullptr);
+ if (childID >= 0)
+ {
+ sprintf(head, "\\firefox%i.bin", childID);
+ }
+ else
+ {
+ sprintf(head, "\\other%i.bin", -childID);
+ }
+
+#else
+ head += sprintf(head, "/firefox");
+ mkdir(filename, 0777);
+ if (childID >=0)
+ {
+ sprintf(head, "/firefox%i.bin", childID);
+ }
+ else
+ {
+ sprintf(head, "/other%i.bin", -childID);
+ }
+
+#endif
+ return open_file(filename);
+ }
+
+ inline void write_file(void* buf, size_t bytes, file_t file)
+ {
+#ifdef _WIN32
+ WriteFile(file, buf, bytes, nullptr, nullptr);
+#else
+ fwrite(buf, 1, bytes, file);
+#endif
+ }
+
+ inline void flush_file(file_t file)
+ {
+#ifdef _WIN32
+ FlushFileBuffers(file);
+#else
+ fflush(file);
+#endif
+ }
+
+ inline void close_file(file_t file)
+ {
+#ifdef _WIN32
+ CloseHandle(file);
+#else
+ fclose(file);
+#endif
+ }
+ }
+
+
+
+ class mutex
+ {
+ public:
+ mutex()
+ {
+#ifdef _WIN32
+ InitializeCriticalSection(&cs);
+#else
+ pthread_mutex_init(&mut, nullptr);
+#endif
+ }
+
+ ~mutex()
+ {
+#ifdef _WIN32
+ DeleteCriticalSection(&cs);
+#else
+ pthread_mutex_destroy(&mut);
+#endif
+ }
+
+ void lock()
+ {
+#ifdef _WIN32
+ EnterCriticalSection(&cs);
+#else
+ pthread_mutex_lock(&mut);
+#endif
+ }
+
+ void unlock()
+ {
+#ifdef _WIN32
+ LeaveCriticalSection(&cs);
+#else
+ pthread_mutex_unlock(&mut);
+#endif
+ }
+ private:
+#ifdef _WIN32
+ CRITICAL_SECTION cs;
+#else
+ pthread_mutex_t mut;
+#endif
+ };
+
+ class logger
+ {
+ typedef std::vector<serialization::message*> message_queue_t;
+ public:
+
+ template<size_t N, typename... ARGS>
+ static void __attribute__((noinline)) debug_log(const char* func, const char* file, uint32_t line, const char(&fmt)[N], ARGS&&... args)
+ {
+ const uint64_t timestamp = internal::get_timestamp();
+
+ auto& self = logger::get();
+
+ size_t size = serialization::msg_size(func, file, line, fmt, std::forward<ARGS>(args)...);
+ auto* msg = serialization::alloc_msg(size);
+ serialization::write_msg(msg, func, file, line, fmt, std::forward<ARGS>(args)...);
+
+ // write info about logger
+ self.enqueue_msg(msg, timestamp);
+ }
+
+ template<size_t N, size_t M, size_t O, typename... ARGS>
+ static void __attribute__((noinline)) log(const char (&func)[N], const char (&file)[M], uint32_t line, const char (&fmt)[O], ARGS&&... args)
+ {
+ const uint64_t timestamp = internal::get_timestamp();
+
+ auto& self = logger::get();
+
+ size_t size = serialization::msg_size(func, file, line, fmt, std::forward<ARGS>(args)...);
+ auto* msg = serialization::alloc_msg(size);
+ serialization::write_msg(msg, func, file, line, fmt, std::forward<ARGS>(args)...);
+
+ // write info about logger
+ self.enqueue_msg(msg, timestamp);
+ }
+
+ private:
+
+ void enqueue_msg(serialization::message* msg, uint64_t timestamp)
+ {
+ msg->timestamp= timestamp;
+ msg->thread_id = internal::get_thread_id();
+
+ ++remaining_messages;
+ queue_lock.lock();
+ producer->push_back(msg);
+ queue_lock.unlock();
+ }
+
+ message_queue_t* swap_queues()
+ {
+ queue_lock.lock();
+ std::swap(producer, consumer);
+ queue_lock.unlock();
+
+ return consumer.get();
+ }
+
+ ~logger()
+ {
+ while(!thread_started) {
+ internal::thread_yield();
+ }
+ signal_exit.store(true);
+
+ // wait for logger to finish
+#ifdef _WIN32
+ WaitForSingleObject(logger_thread, INFINITE);
+#else
+ pthread_join(logger_thread, nullptr);
+#endif // _WIN32
+ }
+
+ /// Constructor
+ logger()
+ {
+ producer.reset(new message_queue_t());
+ consumer.reset(new message_queue_t());
+
+ remaining_messages.store(0);
+ thread_started.store(false);
+ signal_exit.store(false);
+
+#ifdef _WIN32
+ logger_thread = CreateThread(nullptr, 0, &logger::logger_func, nullptr, 0, nullptr);
+#else
+ pthread_create(&this->logger_thread, nullptr, &logger::logger_func, nullptr);
+#endif
+ }
+
+ // Getter
+ static logger& get()
+ {
+ static logger retval;
+ return retval;
+ }
+
+ // Thread Func
+#ifdef _WIN32
+ static DWORD WINAPI logger_func(LPVOID)
+#else
+ static void* logger_func(void*)
+#endif
+ {
+ auto& self = logger::get();
+ self.thread_started.store(true);
+
+ // name thread for debugging
+ internal::set_thread_name();
+ // init logging file
+ const int32_t childID = internal::get_child_id();
+ auto log_file = internal::get_log_file(childID);
+ size_t total_messages_written = 0;
+ // spin until exit is signalled (unless there are remaining messages to write)
+ while(!self.signal_exit || self.remaining_messages != 0)
+ {
+ // repeatedly spin until message queues are empty
+ while (self.remaining_messages != 0)
+ {
+ auto queue = self.swap_queues();
+
+ // write out messages to disk and free their memory
+ for(auto* msg : *queue) {
+ msg->process_id = childID;
+ internal::write_file(msg, msg->length, log_file);
+ free_msg(msg);
+ }
+
+ const auto messages_written = queue->size();
+ queue->clear();
+
+ self.remaining_messages -= messages_written;
+ total_messages_written += messages_written;
+ }
+ internal::flush_file(log_file);
+
+ // sleep for a bit rather than spinning
+ while (!self.signal_exit && self.remaining_messages == 0) {
+ internal::thread_sleep(20);
+ }
+ }
+
+ // flush to disk
+ internal::flush_file(log_file);
+ internal::close_file(log_file);
+
+ return 0;
+ }
+
+ mutex queue_lock;
+ std::unique_ptr<message_queue_t> producer;
+ std::unique_ptr<message_queue_t> consumer;
+ std::atomic_size_t remaining_messages;
+ std::atomic_bool thread_started;
+ std::atomic_bool signal_exit;
+#ifdef _WIN32
+ HANDLE logger_thread;
+#else
+ pthread_t logger_thread;
+#endif
+ };
+}
+
+#endif // TBB_LOGGER_H
diff --git a/xpcom/build/moz.build b/xpcom/build/moz.build
index d86af97cfe0e..885d642fbc30 100755
--- a/xpcom/build/moz.build
+++ b/xpcom/build/moz.build
@@ -9,6 +9,7 @@ EXPORTS += [
"nsXPCOMCID.h",
"nsXPCOMCIDInternal.h",
"nsXULAppAPI.h",
+ "TbbLogger.h",
"XREChildData.h",
"xrecore.h",
"XREShellData.h",