-
Notifications
You must be signed in to change notification settings - Fork 6
/
recorder.c
2751 lines (2419 loc) · 92.2 KB
/
recorder.c
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
// *****************************************************************************
// recorder.c Recorder project
// *****************************************************************************
//
// File description:
//
// Implementation of a non-blocking flight recorder
//
//
//
//
//
//
//
//
// *****************************************************************************
// This software is licensed under the GNU Lesser General Public License v2+
// (C) 2017-2020, Christophe de Dinechin <[email protected]>
// (C) 2018-2020, Frediano Ziglio <[email protected]>
// *****************************************************************************
// This file is part of Recorder
//
// Recorder is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// Recorder is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Recorder, in a file named COPYING.
// If not, see <https://www.gnu.org/licenses/>.
// *****************************************************************************
#include "recorder.h"
#ifndef RECORDER_STANDALONE
#include "config.h"
#include <ctype.h>
#include <fcntl.h>
#include <pthread.h>
#if HAVE_REGEX_H
#include <regex.h>
#endif // HAVE_REGEX_H
#include <signal.h>
#include <errno.h>
#include <sys/time.h>
#if HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif // HAVE_SYS_MMAN_H
#include <sys/stat.h>
#else
#undef printf
#endif // RECORDER_STANDALONE
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <unistd.h>
// ============================================================================
//
// File-specific constants
//
// ============================================================================
enum
{
// Signals that are handled by default by recorder_dump_on_common_signals
RECORDER_SIGNALS_MASK = 0
#ifdef SIGQUIT
| (1U << SIGQUIT)
#endif // SIGQUIT
#ifdef SIGILL
| (1U << SIGILL)
#endif // SIGILL
#ifdef SIGABRT
| (1U << SIGABRT)
#endif // SIGABRT
#ifdef SIGFPE
| (1U << SIGFPE)
#endif // SIGFPE
#ifdef SIGBUS
| (1U << SIGBUS)
#endif // SIGBUS
#ifdef SIGSEGV
| (1U << SIGSEGV)
#endif // SIGSEGV
#ifdef SIGSYS
| (1U << SIGSYS)
#endif // SIGSYS
#ifdef SIGPIPE
| (1U << SIGPIPE)
#endif // SIGPIPE
#ifdef SIGXCPU
| (1U << SIGXCPU)
#endif // SIGXCPU
#ifdef SIGXFSZ
| (1U << SIGXFSZ)
#endif // SIGXFSZ
#ifdef SIGINFO
| (1U << SIGINFO)
#endif // SIGINFO
#ifdef SIGUSR1
| (1U << SIGUSR1)
#endif // SIGUSR1
#ifdef SIGUSR2
| (1U << SIGUSR2)
#endif // SIGUSR2
#ifdef SIGSTKFLT
| (1U << SIGSTKFLT)
#endif // SIGSTKFLT
#ifdef SIGPWR
| (1U << SIGPWR)
#endif // SIGPWR
,
// Signals that should run multiple times by default
RECORDER_SIGNALS_REPEATING = 0
#ifdef SIGINFO
| (1U << SIGINFO)
#endif // SIGINFO
#ifdef SIGUSR1
| (1U << SIGUSR1)
#endif // SIGUSR1
#ifdef SIGUSR2
| (1U << SIGUSR2)
#endif // SIGUSR2
#ifdef SIGSTKFLT
| (1U << SIGSTKFLT)
#endif // SIGSTKFLT
#ifdef SIGPWR
| (1U << SIGPWR)
#endif // SIGPWR
,
// Signals where the signal handler should exit
RECORDER_SIGNALS_EXITING = RECORDER_SIGNALS_MASK&~RECORDER_SIGNALS_REPEATING
};
// ============================================================================
//
// Available recorders exposed by recorder library
//
// ============================================================================
RECORDER(recorder, 32, "Recorder operations and configuration");
RECORDER(recorder_warning, 8, "Recorder warnings");
RECORDER(recorder_error, 8, "Recorder errors");
RECORDER(recorder_signals, 32, "Recorder signal handling");
RECORDER(recorder_traces, 64, "Recorder traces");
RECORDER_TWEAK_DEFINE(recorder_signals_mask,
RECORDER_SIGNALS_MASK,
"Recorder default mask for signals to catch");
RECORDER_TWEAK_DEFINE(recorder_signals_repeating,
RECORDER_SIGNALS_REPEATING,
"Recorder default mask for signals that can repeat");
RECORDER_TWEAK_DEFINE(recorder_signals_exiting,
RECORDER_SIGNALS_EXITING,
"Recorder default mask for signals that will exit");
RECORDER_TWEAK_DEFINE(recorder_dump_sleep, 100,
"Sleep time between background dumps (ms)");
RECORDER_TWEAK_DEFINE(recorder_export_size, 2048,
"Number of samples stored when exporting records");
RECORDER_TWEAK_DEFINE(recorder_configuration_sleep, 100,
"Sleep time between configuration checks (ms)");
RECORDER_TWEAK_DEFINE(recorder_time_precision,
RECORDER_HZ > 100000 ? 6
: RECORDER_HZ > 10000 ? 5
: RECORDER_HZ > 1000 ? 4
: RECORDER_HZ > 100 ? 3
: RECORDER_HZ > 10 ? 2
: RECORDER_HZ > 1 ? 1
: 0,
"Precision for displaying time");
#ifndef RECORDER_STANDALONE
RECORDER_TWEAK_DEFINE(recorder_alt_stack_size, SIGSTKSZ,
"Size of alternate stack for recorder (0 to disable)");
#endif // RECORDER_STANDALONE
#ifdef RECORDER_STANDALONE_PRINTF
RECORDER_DEFINE(printf, 32, "Recorder for non-transformed printf statements");
#endif // RECORDER_STANDALONE_PRINTF
// Display tweaks
RECORDER_TWEAK_DEFINE(recorder_location, 0,
"Set to show location in recorder dumps");
RECORDER_TWEAK_DEFINE(recorder_function, 0,
"Set to show function in recorder dumps");
RECORDER_TWEAK_DEFINE(recorder_order, 1,
"Set to show order number in recorder dumps");
RECORDER_TWEAK_DEFINE(recorder_abstime, 0,
"Set to show absolute time in recorder dumps");
RECORDER_TWEAK_DEFINE(recorder_reltime, 1,
"Set to show relative time in recorder dumps");
RECORDER_TWEAK_DEFINE(recorder_indent, 20,
"Set to show max indentation in recorder dumps");
// ============================================================================
//
// Utility functions wrapping pattern maching irrespective of regex.h
//
// ============================================================================
#if HAVE_REGEX_H
// In the regexp.h case, we use real regular expressions
typedef regex_t pattern_t;
#define pattern_comp(re, what) regcomp(re, what, REG_EXTENDED|REG_ICASE)
#define pattern_free(re) regfree(re)
static inline bool pattern_match(regex_t *re, const char *s)
// ----------------------------------------------------------------------------
// Match input string against a given "true" regexp
// ----------------------------------------------------------------------------
{
regmatch_t rm;
return regexec(re, s, 1, &rm, 0) == 0 &&
rm.rm_so == 0 && s[rm.rm_eo] == 0;
}
#else // !HAVE_REGEX_H
// In the non regexp.h case, we degrade to string comparisons
typedef const char *pattern_t;
static inline int pattern_comp(pattern_t *re, const char *what)
// ----------------------------------------------------------------------------
// No real regexp compilation in that case
// ----------------------------------------------------------------------------
{
*re = what;
return 0;
}
static inline bool pattern_match(pattern_t *re, const char *s)
// ----------------------------------------------------------------------------
// No regexp matching, replace with string search
// ----------------------------------------------------------------------------
{
return strstr(*re, s) != NULL;
}
static inline void pattern_free(pattern_t *re)
// ----------------------------------------------------------------------------
// No need to free input string
// ----------------------------------------------------------------------------
{
}
#endif
#define array_size(a) (sizeof(a) / sizeof(a[0]))
// ============================================================================
//
// Local prototypes (in case -Wmissing-prototypes is enabled)
//
// ============================================================================
size_t recorder_chan_write(recorder_chan_p chan, const void *ptr, size_t cnt);
size_t recorder_chan_writable(recorder_chan_p chan);
ringidx_t recorder_chan_writer(recorder_chan_p chan);
ringidx_t recorder_chan_reader(recorder_chan_p chan);
size_t recorder_chan_item_size(recorder_chan_p chan);
// ============================================================================
//
// User-configurable parameters and other static variables
//
// ============================================================================
static unsigned recorder_print(const char *ptr, size_t len, void *file_arg);
static void recorder_format_entry(recorder_show_fn show,
void *output,
const char *label,
const char *location,
uintptr_t order,
uintptr_t timestamp,
const char *message);
static void * recorder_output = NULL;
static recorder_show_fn recorder_show = recorder_print;
static recorder_format_fn recorder_format = recorder_format_entry;
static recorder_type_fn recorder_types[256] = { };
static uint8_t *recorder_alt_stack = NULL;
static uintptr_t recorder_time_at_start = 0;
// ============================================================================
//
// Recording data
//
// ============================================================================
ringidx_t recorder_append(recorder_info *rec,
const char *where,
const char *format,
uintptr_t a0,
uintptr_t a1,
uintptr_t a2,
uintptr_t a3)
// ----------------------------------------------------------------------------
// Enter a record entry in ring buffer with given set of args
// ----------------------------------------------------------------------------
{
recorder_ring_p ring = &rec->ring;
recorder_entry *data = rec->data;
ringidx_t writer = recorder_ring_fetch_add(ring->writer, 1);
size_t size = ring->size;
recorder_entry *entry = &data[writer % size];
entry->format = format;
entry->order = recorder_ring_fetch_add(recorder_order, 1);
entry->timestamp = recorder_tick();
entry->where = where;
entry->args[0] = a0;
entry->args[1] = a1;
entry->args[2] = a2;
entry->args[3] = a3;
recorder_ring_fetch_add(ring->commit, 1);
if (rec->trace)
recorder_trace_entry(rec, entry);
return writer;
}
ringidx_t recorder_append2(recorder_info *rec,
const char *where,
const char *format,
uintptr_t a0,
uintptr_t a1,
uintptr_t a2,
uintptr_t a3,
uintptr_t a4,
uintptr_t a5,
uintptr_t a6,
uintptr_t a7)
// ----------------------------------------------------------------------------
// Enter a double record (up to 8 args)
// ----------------------------------------------------------------------------
{
recorder_ring_p ring = &rec->ring;
recorder_entry *data = rec->data;
ringidx_t writer = recorder_ring_fetch_add(ring->writer, 2);
size_t size = ring->size;
recorder_entry *entry = &data[writer % size];
entry->format = format;
entry->order = recorder_ring_fetch_add(recorder_order, 1);
entry->timestamp = recorder_tick();
entry->where = where;
entry->args[0] = a0;
entry->args[1] = a1;
entry->args[2] = a2;
entry->args[3] = a3;
recorder_entry *entry2 = &data[(writer+1) % size];
entry2->format = NULL;
entry2->order = entry->order;
entry2->timestamp = entry->timestamp;
entry2->where = where;
entry2->args[0] = a4;
entry2->args[1] = a5;
entry2->args[2] = a6;
entry2->args[3] = a7;
recorder_ring_fetch_add(ring->commit, 2);
if (rec->trace)
recorder_trace_entry(rec, entry);
return writer;
}
ringidx_t recorder_append3(recorder_info *rec,
const char *where,
const char *format,
uintptr_t a0,
uintptr_t a1,
uintptr_t a2,
uintptr_t a3,
uintptr_t a4,
uintptr_t a5,
uintptr_t a6,
uintptr_t a7,
uintptr_t a8,
uintptr_t a9,
uintptr_t a10,
uintptr_t a11)
// ----------------------------------------------------------------------------
// Record a triple entry (up to 12 args)
// ----------------------------------------------------------------------------
{
recorder_ring_p ring = &rec->ring;
recorder_entry *data = rec->data;
ringidx_t writer = recorder_ring_fetch_add(ring->writer, 3);
size_t size = ring->size;
recorder_entry *entry = &data[writer % size];
entry->format = format;
entry->order = recorder_ring_fetch_add(recorder_order, 1);
entry->timestamp = recorder_tick();
entry->where = where;
entry->args[0] = a0;
entry->args[1] = a1;
entry->args[2] = a2;
entry->args[3] = a3;
recorder_entry *entry2 = &data[(writer+1) % size];
entry2->format = NULL;
entry2->order = entry->order;
entry2->timestamp = entry->timestamp;
entry2->where = where;
entry2->args[0] = a4;
entry2->args[1] = a5;
entry2->args[2] = a6;
entry2->args[3] = a7;
recorder_entry *entry3 = &data[(writer+2) % size];
entry3->format = NULL;
entry3->order = entry->order;
entry3->timestamp = entry->timestamp;
entry3->where = where;
entry3->args[0] = a8;
entry3->args[1] = a9;
entry3->args[2] = a10;
entry3->args[3] = a11;
recorder_ring_fetch_add(ring->commit, 3);
if (rec->trace)
recorder_trace_entry(rec, entry);
return writer;
}
ringidx_t recorder_append_fast(recorder_info *rec,
const char *where,
const char *format,
uintptr_t a0,
uintptr_t a1,
uintptr_t a2,
uintptr_t a3)
// ----------------------------------------------------------------------------
// Enter a record entry in ring buffer with given set of args
// ----------------------------------------------------------------------------
{
recorder_ring_p ring = &rec->ring;
recorder_entry *data = rec->data;
ringidx_t writer = recorder_ring_fetch_add(ring->writer, 1);
size_t size = ring->size;
recorder_entry *entry = &data[writer % size];
entry->format = format;
entry->order = recorder_ring_fetch_add(recorder_order, 1);
entry->timestamp = data[(writer - 1) % size].timestamp;
entry->where = where;
entry->args[0] = a0;
entry->args[1] = a1;
entry->args[2] = a2;
entry->args[3] = a3;
recorder_ring_fetch_add(ring->commit, 1);
if (rec->trace)
recorder_trace_entry(rec, entry);
return writer;
}
ringidx_t recorder_append_fast2(recorder_info *rec,
const char *where,
const char *format,
uintptr_t a0,
uintptr_t a1,
uintptr_t a2,
uintptr_t a3,
uintptr_t a4,
uintptr_t a5,
uintptr_t a6,
uintptr_t a7)
// ----------------------------------------------------------------------------
// Enter a double record (up to 8 args)
// ----------------------------------------------------------------------------
{
recorder_ring_p ring = &rec->ring;
recorder_entry *data = rec->data;
ringidx_t writer = recorder_ring_fetch_add(ring->writer, 2);
size_t size = ring->size;
recorder_entry *entry = &data[writer % size];
entry->format = format;
entry->order = recorder_ring_fetch_add(recorder_order, 1);
entry->timestamp = data[(writer - 1) % size].timestamp;
entry->where = where;
entry->args[0] = a0;
entry->args[1] = a1;
entry->args[2] = a2;
entry->args[3] = a3;
recorder_entry *entry2 = &data[(writer+1) % size];
entry2->format = NULL;
entry2->order = entry->order;
entry2->timestamp = entry->timestamp;
entry2->where = where;
entry2->args[0] = a4;
entry2->args[1] = a5;
entry2->args[2] = a6;
entry2->args[3] = a7;
recorder_ring_fetch_add(ring->commit, 2);
if (rec->trace)
recorder_trace_entry(rec, entry);
return writer;
}
ringidx_t recorder_append_fast3(recorder_info *rec,
const char *where,
const char *format,
uintptr_t a0,
uintptr_t a1,
uintptr_t a2,
uintptr_t a3,
uintptr_t a4,
uintptr_t a5,
uintptr_t a6,
uintptr_t a7,
uintptr_t a8,
uintptr_t a9,
uintptr_t a10,
uintptr_t a11)
// ----------------------------------------------------------------------------
// Record a triple entry (up to 12 args)
// ----------------------------------------------------------------------------
{
recorder_ring_p ring = &rec->ring;
recorder_entry *data = rec->data;
ringidx_t writer = recorder_ring_fetch_add(ring->writer, 3);
size_t size = ring->size;
recorder_entry *entry = &data[writer % size];
entry->format = format;
entry->order = recorder_ring_fetch_add(recorder_order, 1);
entry->timestamp = data[(writer - 1) % size].timestamp;
entry->where = where;
entry->args[0] = a0;
entry->args[1] = a1;
entry->args[2] = a2;
entry->args[3] = a3;
recorder_entry *entry2 = &data[(writer+1) % size];
entry2->format = NULL;
entry2->order = entry->order;
entry2->timestamp = entry->timestamp;
entry2->where = where;
entry2->args[0] = a4;
entry2->args[1] = a5;
entry2->args[2] = a6;
entry2->args[3] = a7;
recorder_entry *entry3 = &data[(writer+2) % size];
entry3->format = NULL;
entry3->order = entry->order;
entry3->timestamp = entry->timestamp;
entry3->where = where;
entry3->args[0] = a8;
entry3->args[1] = a9;
entry3->args[2] = a10;
entry3->args[3] = a11;
recorder_ring_fetch_add(ring->commit, 3);
if (rec->trace)
recorder_trace_entry(rec, entry);
return writer;
}
// ============================================================================
//
// Recorder dump utility
//
// ============================================================================
/// Global counter indicating the order of entries across recorders.
uintptr_t recorder_order = 0;
// Check if we are currently dumping the recorder
static unsigned recorder_dumping = 0;
/// List of the currently active flight recorders (ring buffers)
static recorder_info * recorders = NULL;
/// List of the currently active tweaks
static recorder_tweak *tweaks = NULL;
// The current indent for output
static unsigned indent = 0;
static void recorder_dump_entry(recorder_info *rec,
recorder_entry *entry,
recorder_format_fn format,
recorder_show_fn show,
void *output)
// ----------------------------------------------------------------------------
// Dump a recorder entry in a buffer between dst and dst_end, return last pos
// ----------------------------------------------------------------------------
{
char buffer[256];
char format_buffer[32];
const char *label = rec->name;
char *dst = buffer;
char *dst_end = buffer + sizeof buffer - 1;
const char *fmt = entry->format;
unsigned arg_index = 0;
const unsigned max_arg_index = array_size(entry->args);
unsigned colons = 0;
unsigned nextindent = indent;
// Exit if we get there for a long-format second entry
if (!fmt)
return;
// Apply formatting. This complicated loop is because
// we need to detect floating-point values, which are passed
// differently on many architectures such as x86 or ARM
// (passed in different registers). So we detect them from the format,
// convert intptr_t to float or double depending on its size,
// and call the variadic snprintf passing a double value that will
// naturally go in the right register. A bit ugly.
bool finished_in_newline = false;
while (dst < dst_end)
{
char c = *fmt++;
if (c != '%')
{
*dst = c;
if (!c)
break;
dst++;
if (c == ':')
{
// Skip file:line:
colons++;
}
else if (colons == 2)
{
// Check if first character marks indentation
switch(c)
{
case '>': nextindent = indent + 1; dst--; break;
case '<': if (indent) nextindent = --indent; dst--; break;
case '=': nextindent = indent = 0; dst--; break;
}
colons++;
}
}
else
{
char *fmt_copy = format_buffer;
char *fmt_end = format_buffer + sizeof format_buffer - 1;
bool floating_point = false;
recorder_type_fn special = NULL;
bool done = false;
bool unsupported = false;
bool safe_pointer = false;
int fields[2] = { 0 };
unsigned field_cnt = 0;
*fmt_copy++ = c;
while (!done && fmt_copy < fmt_end)
{
c = *fmt++;
*fmt_copy++ = c;
special = recorder_types[(uint8_t) c];
if (special)
break;
switch(c)
{
case 'f': case 'F': // Floating point formatting
case 'g': case 'G':
case 'e': case 'E':
case 'a': case 'A':
floating_point = true;
/* Falls through */
case 'b': // Integer formatting
case 'c': case 'C':
case 's': case 'S':
case 'd': case 'D':
case 'i':
case 'o': case 'O':
case 'u': case 'U':
case 'x':
case 'X':
case 'p':
case '%':
case 0: // End of string
done = true;
break;
// GCC: case '0' ... '9', not supported on IAR
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case '.':
case '-':
case 'l': case 'L':
case 'h':
case 'j':
case 't':
case 'z':
case 'q':
case 'v':
#ifdef _WIN32
// On Windows size specifiers "I", "I32", "I64"
case 'I':
#endif
break;
case '+':
safe_pointer = true;
break;
case 'n': // Expect two args
case '*':
// Check for long entry, need to skip to next entry
if (arg_index >= max_arg_index)
{
recorder_ring_p ring = &rec->ring;
recorder_entry *base = (recorder_entry *) (ring + 1);
ringidx_t idx = entry - base;
entry = &base[(idx + 1) % ring->size];
arg_index = 0;
}
unsupported = field_cnt >= 2;
if (!unsupported)
fields[field_cnt++] = (int) entry->args[arg_index++];
break;
default:
unsupported = true;
break;
}
}
if (!c || unsupported)
break;
bool is_string = (c == 's' || c == 'S');
if (is_string && !safe_pointer && recorder_dumping)
fmt_copy[-1] = 'p'; // Replace with a pointer if not tracing
*fmt_copy++ = 0;
// Check for long entry, need to skip to next entry
if (arg_index >= max_arg_index)
{
recorder_ring_p ring = &rec->ring;
recorder_entry *base = (recorder_entry *) (ring + 1);
ringidx_t idx = entry - base;
entry = &base[(idx + 1) % ring->size];
arg_index = 0;
}
// Warning: It is important for correctness that only
// one call to snprintf happens per loop, since snprintf
// return value can be larger than what is actually written.
if (special)
{
uintptr_t arg = entry->args[arg_index++];
intptr_t tracing = recorder_dumping ? 0 : rec->trace;
dst += special(safe_pointer | tracing,
format_buffer, dst, dst_end - dst, arg);
}
else if (floating_point)
{
double arg;
if (sizeof(intptr_t) == sizeof(float))
{
union { float f; intptr_t i; } u;
u.i = entry->args[arg_index++];
arg = (double) u.f;
}
else
{
union { double d; intptr_t i; } u;
u.i = entry->args[arg_index++];
arg = u.d;
}
switch(field_cnt)
{
case 0:
dst += snprintf(dst, dst_end - dst,
format_buffer, arg);
break;
case 1:
dst += snprintf(dst, dst_end - dst,
format_buffer, fields[0], arg);
break;
case 2:
dst += snprintf(dst, dst_end - dst,
format_buffer, fields[0], fields[1], arg);
break;
}
}
else
{
intptr_t arg = entry->args[arg_index++];
if (is_string && arg == 0)
arg = (intptr_t) "<NULL>";
switch (field_cnt)
{
case 0:
dst += snprintf(dst, dst_end - dst,
format_buffer, arg);
break;
case 1:
dst += snprintf(dst, dst_end - dst,
format_buffer, fields[0], arg);
break;
case 2:
dst += snprintf(dst, dst_end - dst,
format_buffer, fields[0], fields[1], arg);
break;
}
}
}
finished_in_newline = c == '\n';
}
// Check if snprintf returned a value beyond the buffer
if (dst > dst_end)
dst = dst_end;
if (finished_in_newline)
dst--;
*dst++ = 0;
format(show, output, label,
entry->where, entry->order, entry->timestamp, buffer);
indent = nextindent;
}
// ============================================================================
//
// Default output prints things to stderr
//
// ============================================================================
void *recorder_configure_output(void *output)
// ----------------------------------------------------------------------------
// Configure the output stream
// ----------------------------------------------------------------------------
{
record(recorder, "Configure output %p from %p", output, recorder_output);
void *previous = recorder_output;
recorder_output = output;
return previous;
}
static unsigned recorder_print(const char *ptr, size_t len, void *file_arg)
// ----------------------------------------------------------------------------
// The default printing function - prints to standard error
// ----------------------------------------------------------------------------
// This uses 'write' in order to avoid buffered output, which proves
// unreliable on the alternate stack (sigaltstack) because it uses malloc
{
int fd = file_arg >= (void *) 0x100
? fileno((FILE *) file_arg)
: file_arg
? (int) (intptr_t) file_arg
: 2;
return (unsigned) write(fd, ptr, len) + write(fd, "\n", 1);
}
recorder_show_fn recorder_configure_show(recorder_show_fn show)
// ----------------------------------------------------------------------------
// Configure the function used to output data to the stream
// ----------------------------------------------------------------------------
{
record(recorder, "Configure show %p from %p", show, recorder_show);
recorder_show_fn previous = recorder_show;
recorder_show = show;
return previous;
}
// ============================================================================
//
// Default format for recorder entries
//
// ============================================================================
// Truly shocking that Visual Studio before 2015 does not have a working
// snprintf or vsnprintf. Note that the proposed replacements are not accurate
// since they return -1 on overflow instead of the length that would have
// been written as the standard mandates.
// http://stackoverflow.com/questions/2915672/snprintf-and-visual-studio-2010.
#if defined(_MSC_VER) && _MSC_VER < 1900
# define snprintf _snprintf
#endif
static void recorder_format_entry(recorder_show_fn show,
void *output,
const char *label,
const char *function_name,
uintptr_t order,
uintptr_t timestamp,
const char *message)
// ----------------------------------------------------------------------------
// Default formatting for the entries
// ----------------------------------------------------------------------------
{
char buffer[256];
char *dst = buffer;
char *dst_end = buffer + sizeof buffer;
#define rsnprintf(...) \
snprintf(dst, dst_end >= dst ? dst_end - dst : 0, __VA_ARGS__)
// Look for file:line: in the input message
const char *end_of_fileline = message;
for (int colon = 0; colon < 2; colon++)
while (*end_of_fileline && *end_of_fileline++ != ':')
/* Empty */;
if (*end_of_fileline == 0) // Play it ultra-safe
end_of_fileline = message;
int size = (int) RECORDER_TWEAK(recorder_location);
if (size)
{
int fileline_size = (int) (end_of_fileline - message);
if (size != 1)
dst += rsnprintf("%*.*s", size, fileline_size, message);
else
dst += rsnprintf("%.*s", fileline_size, message);
}
message = end_of_fileline;
size = (int) RECORDER_TWEAK(recorder_function);
if (size)
{
if (size != 1)
dst += rsnprintf("%*s:", size, function_name);
else
dst += rsnprintf("%s:", function_name);
}
char spacing = '[';
if (RECORDER_TWEAK(recorder_order))
{
dst += rsnprintf("%c%"PRIdPTR, spacing, order);
spacing = ' ';
}
if (RECORDER_TWEAK(recorder_abstime))
{
uintptr_t abstime = timestamp + recorder_time_at_start;
const uintptr_t minute = (uintptr_t) 60 * RECORDER_HZ;
uintptr_t minutes = abstime / minute;
uintptr_t seconds = abstime % minute;
int precision = (int) RECORDER_TWEAK(recorder_time_precision);
dst += rsnprintf("%c%02"PRIdPTR":%02"PRIdPTR":%0*.*f",
spacing,
minutes / 60,
minutes % 60,
precision + 3, precision,
seconds * (1.0 / RECORDER_HZ));
spacing = ' ';
}
if (RECORDER_TWEAK(recorder_reltime))
{
dst += rsnprintf("%c%.*f",
spacing,
(int) RECORDER_TWEAK(recorder_time_precision),
(double) timestamp / RECORDER_HZ);
spacing = ' ';
}
if (spacing != '[')
dst += rsnprintf("] ");
if (RECORDER_TWEAK(recorder_indent))
{
unsigned i = indent % RECORDER_TWEAK(recorder_indent);
unsigned n = indent / RECORDER_TWEAK(recorder_indent);
if (n)
dst += rsnprintf("(%u)", n);
while (i--)
dst += rsnprintf(" ");
}
dst += rsnprintf("%s: %s", label, message);
// In case snprintf overflowed
show(buffer, (dst > dst_end ? dst_end : dst) - buffer, output);
#undef rsnprintf
}