-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump.c
2546 lines (2236 loc) · 64.9 KB
/
dump.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
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <stdbool.h>
#include <getopt.h>
#include <time.h>
#if 0
/* When things stabilize and we can move it to the sysroot */
#include <linux/owl.h>
#else
#define __user /* This is stripped from uapi headers by linux */
#include "owl.h"
#endif
#include "owl-user.h"
#include "syscalltable.h"
#include "mcalltable.h"
#include "source_hashmap.h"
#define DEFAULT_SYSROOT "/opt/riscv/sysroot"
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#define max(x,y) (x > y ? x : y)
#define min(x,y) (x > y ? y : x)
#define ERROR_ON(cond, fmt, ...) \
do { \
if (cond) { \
fprintf(stderr, "ERROR: " fmt, __VA_ARGS__); \
exit(EXIT_FAILURE); \
} \
} while (0)
#define WARN_ON_ONCE(cond, fmt, ...) \
do { \
static int _warned = 0; \
if (!_warned && cond) { \
fprintf(stdwarn, "WARNING: " fmt, __VA_ARGS__); \
_warned = 1; \
} \
} while (0)
#define STRNCMP_LIT(s, lit) strncmp((s), ""lit"", sizeof((lit)-1))
/* Where warnings should be printed to. This is set in main after the call to
* parse_options_or_die() */
static FILE *stdwarn = NULL;
static void *source_info_hashmap;
/* A preprocessed trace with context */
struct dump_trace {
uint64_t timestamp;
union owl_trace trace;
uint64_t sched_in_timestamp;
const struct owl_sched_info_full *sched_info;
};
struct call_frame {
const struct dump_trace *enter_trace; /* Trace entering this frame */
const struct dump_trace *return_trace; /* Trace returning to this frame */
uint32_t pchi; /* TODO?!?: Move pchi to struct dump_trace??? */
};
struct callstack {
struct call_frame frames[3];
int frameno; /* The 'frame' we're at */
const struct owl_task *task;
};
struct print_args {
/* Used for timestamp pchi & invalid */
struct dump_trace *trace;
/* Current task and memory mapping info */
const struct owl_map_info *maps;
size_t num_map_entries;
/* Arch settings */
unsigned pc_bits;
bool sign_extend_pc;
char delim;
const char *sysroot;
uint64_t start_time;
int cpu;
float clock_freq;
};
struct map_search_key {
int pid;
uint64_t pc;
};
typedef long long unsigned int llu_t;
typedef void (* const printfn_t)(struct print_args *, struct callstack *);
typedef void (* const print_schedfn_t)
(const struct owl_sched_info_full *, uint64_t, uint64_t, float, int, char);
typedef void (* const print_prologuefn_t)
(const struct owl_trace_file_header *file_header);
typedef void (* const print_epiloguefn_t)
(const struct owl_trace_file_header *file_header);
struct printer {
printfn_t *print_trace;
print_schedfn_t print_sched;
print_prologuefn_t print_prologue;
print_epiloguefn_t print_epilogue;
};
/**
* owl_trace_valid_p - Sanity check trace
* @trace: The trace
*
* Return: true if trace is valid, false otherwise.
*/
static bool
owl_trace_valid_p(union owl_trace trace)
{
const union owl_trace empty = { 0 };
switch (trace.kind) {
case OWL_TRACE_KIND_UECALL:
case OWL_TRACE_KIND_RETURN:
case OWL_TRACE_KIND_SECALL:
case OWL_TRACE_KIND_TIMESTAMP:
case OWL_TRACE_KIND_EXCEPTION:
case OWL_TRACE_KIND_PCHI:
break;
default:
return false;
}
return memcmp(&trace, &empty, sizeof(empty)) != 0;
}
/**
* timestamp_trace_to_clocks - Get absolute clock cycles for timestamp trace
* @curr: Current timestamp trace
* @prev: Previous timestamp trace
* @absclocks: Previous absolute clock cycles
*
* Return: Absolute clock cycle for trace
*/
static uint64_t
timestamp_trace_to_clocks(union owl_trace curr, union owl_trace prev,
uint64_t absclocks)
{
uint64_t currclocks, prevclocks;
const int width = 61;
/* Assume we will never overflow 64 bits.
* This translates to >100 years at 5GHz */
currclocks = curr.timestamp.timestamp;
prevclocks = prev.timestamp.timestamp;
/* Assume that (prevclocks == currclocks) is not a wrap but rather
* triggered by a duplicate timestamp caused by H/W bug. */
WARN_ON_ONCE(prevclocks == currclocks,
"Treating timestamp as duplicate t=[%020llu]\n",
(llu_t) curr.timestamp.timestamp);
if (prevclocks > currclocks) {
/* Timestamp wrapped */
absclocks += (1ULL << width);
}
absclocks &= ~((1ULL << width) - 1);
currclocks &= ((1ULL << width) - 1);
return absclocks | currclocks;
}
/* Begin print helper functions */
static struct owl_map_info *find_map(const struct map_search_key *key,
const struct owl_map_info *maps,
size_t num_map_entries);
/**
* sign_extend_pchi - Sign extend high bits of PC.
* @pchi: Highest 32 bits of 64-bit program counter
* @pc_bits: Number of bits in effective address
*
* Return: High 32 bits in PC sign extended
*/
static uint32_t
sign_extend_pchi(uint32_t pchi, unsigned pc_bits)
{
unsigned sign_bits = pc_bits - 32;
pchi &= ((1 << sign_bits) - 1);
if (pchi & (1ULL << (sign_bits - 1)))
pchi = (~0ULL ^ ((1 << sign_bits) - 1)) | pchi;
return pchi;
}
/**
* full_pc - Reconstruct the full PC value
* @frame: The call frame
* @pc_bits: Number of bits in effective address
* @sign_extend: Should PC be sign extended
*
* Return: Full PC value
*/
static uint64_t
full_pc(struct call_frame *frame, unsigned pc_bits,
bool sign_extend)
{
uint32_t pclo, pchi;
uint64_t pc;
pclo = frame->return_trace->trace.ret.pc;
pchi = frame->pchi;
assert(pc_bits > 32 && "Support =<32 bit addresses");
if (sign_extend)
pchi = sign_extend_pchi(pchi, pc_bits);
pc = (((uint64_t) pchi) << 32) | pclo;
return pc;
}
/**
* this_frameno - Get current frame level
* @c: Pointer to callstack
*
* Return: Current frame number
*/
static int
this_frameno(struct callstack *c)
{
return c->frameno;
}
/**
* this_frame - Get current frame
* @c: Pointer to callstack
*
* Return: Pointer to current frame
*/
static struct call_frame *
this_frame(struct callstack *c)
{
assert (0 <= c->frameno && c->frameno <= 2);
return &c->frames[c->frameno];
}
/**
* frame_down - Get frame one level down
* @c: Pointer to callstack
*
* Return: Pointer to frame one level down
*/
static struct call_frame *
frame_down(struct callstack *c)
{
assert (0 <= c->frameno && c->frameno < 2);
return &c->frames[c->frameno + 1];
}
/**
* frame_up - Get frame one level up
* @c: Pointer to callstack
*
* Return: Pointer to frame one level up
*/
static struct call_frame *
frame_up(struct callstack *c)
{
assert (1 <= c->frameno && c->frameno <= 2);
return &c->frames[c->frameno - 1];
}
/**
* return_type - Get description string for return type
* @c: Pointer to frame
*
* Return: String with return type
*/
static const char *
return_type(struct call_frame *frame)
{
if (frame->enter_trace == NULL || frame->enter_trace->trace.kind == 7) {
/* Return from other task */
return "scdret";
}
switch (frame->enter_trace->trace.kind) {
case OWL_TRACE_KIND_UECALL: return "eret";
case OWL_TRACE_KIND_SECALL: return "mret";
case OWL_TRACE_KIND_EXCEPTION:
if (frame->enter_trace->trace.exception.cause & 128)
return "iret";
else
return "exret";
default:
printf("enter trace kind=%d\n", frame->enter_trace->trace.kind);
assert("wrong return trace" && 0);
return "?????";
}
}
/**
* describe_binary - Get the name of the binary for the current frame
* @a: Print arguments
* @c: Callstack
* @pc: Pointer to store pc
* @offset: Pointer to store offset
*
* Return: Pointer to name of the binary or 'none'
*/
static const char *
describe_binary(struct print_args *a, struct callstack *c, uint64_t *pc,
uint64_t *offset)
{
const struct owl_sched_info_full *sched =
this_frame(c)->return_trace->sched_info;
*pc = full_pc(this_frame(c), a->pc_bits, a->sign_extend_pc);
*offset = *pc;
const char *binary = "'none'";
if (frame_down(c)->enter_trace != NULL &&
frame_down(c)->enter_trace->sched_info) {
assert(frame_down(c)->enter_trace->sched_info->base.pid ==
this_frame(c)->return_trace->sched_info->base.pid);
}
if (this_frameno(c) == 1) {
binary = "/boot/vmlinux";
} else if (this_frameno(c) == 0) {
struct owl_map_info *map;
struct map_search_key key = {
.pid = sched->base.pid,
.pc = *pc
};
/* Detecting when we should use the parent's memory mapping
* seems fragile. We might have to add timestamping to the
* mmaping sched_info to detect whether the region is alive at
* this point in time. */
map = find_map(&key, a->maps, a->num_map_entries);
if (!map && sched->base.in_execve) {
key.pid = sched->base.ppid;
map = find_map(&key, a->maps, a->num_map_entries);
}
if (map) {
binary = map->path;
*offset = *pc - map->vm_start;
} else {
binary = "'none'";
}
}
assert(this_frameno(c) < 2);
return binary;
}
/**
* describe_exception_frame - Describe an exception frame
* @frame: Pointer to frame
* @type: Pointer to store type string
* @name: Pointer to store name string
* @desc: Pointer to store description string
* @cause: Pointer to store cause value
*
* Return: None
*/
static void
describe_exception_frame(struct call_frame *frame, const char **type,
const char **name, const char **desc, unsigned *cause)
{
const char *type2;
const char *name2;
const char *desc2;
unsigned cause2;
type = type == NULL ? &type2 : type;
name = name == NULL ? &name2 : name;
desc = desc == NULL ? &desc2 : desc;
cause = cause == NULL ? &cause2 : cause;
/* Exception causes */
const char *causes[16] = {
[0x0] = "misaligned_fetch",
[0x1] = "fetch_access",
[0x2] = "illegal_instruction",
[0x3] = "breakpoint",
[0x4] = "misaligned_load",
[0x5] = "load_access",
[0x6] = "misaligned_store",
[0x7] = "store_access",
[0x8] = "user_ecall",
[0x9] = "supervisor_ecall",
[0xa] = "hypervisor_ecall",
[0xb] = "machine_ecall",
[0xc] = "fetch_page_fault",
[0xd] = "load_page_fault",
[0xe] = "???",
[0xf] = "store_page_fault"
};
/* Standard interrupts: */
const char *interrupts[16] = {
[11] = "MEI",
[ 3] = "MSI",
[ 7] = "MTI",
[ 9] = "SEI",
[ 1] = "SSI",
[ 5] = "STI",
[ 8] = "UEI",
[ 0] = "USI",
[ 4] = "UTI"
};
*cause = frame->enter_trace->trace.exception.cause;
*name = NULL;
if (*cause & 128) {
*cause &= ~128;
*type = "interrupt";
*desc = "irqno";
*name = *cause < ARRAY_SIZE(interrupts) ?
interrupts[*cause] : "???";
} else {
*type = "exception";
*desc = "cause";
*name = *cause < ARRAY_SIZE(causes) ?
causes[*cause] : "???";
}
*name = *name ? : "???";
}
/**
* describe_frame_enter - Describe frame enter reason
* @frame: Pointer to frame
* @type: Pointer to store type string
* @name: Pointer to store name string
* @desc: Pointer to store description string
* @cause: Pointer to store cause value
*
* Return: None
*/
static void
describe_frame_enter(struct call_frame *frame, const char **type,
const char **name, const char **desc, unsigned *cause)
{
const char *type2;
const char *name2;
const char *desc2;
unsigned cause2;
type = type == NULL ? &type2 : type;
name = name == NULL ? &name2 : name;
desc = desc == NULL ? &desc2 : desc;
cause = cause == NULL ? &cause2 : cause;
switch (frame->enter_trace->trace.kind) {
case OWL_TRACE_KIND_UECALL:
*type = "ecall";
*desc = "ecall";
*cause = frame->enter_trace->trace.ecall.regval;
if (*cause >= ARRAY_SIZE(syscalltable)) {
fprintf(stdwarn, "WARNING: invalid syscall %d\n", *cause);
*name = "sys_INVALID";
} else {
*name = syscalltable[*cause];
}
break;
case OWL_TRACE_KIND_SECALL:
*type = "mcall";
*desc = "mcall";
*cause = frame->enter_trace->trace.ecall.regval;
if (*cause >= ARRAY_SIZE(mcalltable)) {
*name = NULL;
} else {
*name = mcalltable[*cause];
}
*cause = frame->enter_trace->trace.ecall.regval;
break;
case OWL_TRACE_KIND_EXCEPTION:
describe_exception_frame(frame, type, name, desc, cause);
break;
default:
assert(0 && "Wrong trace kind\n");
}
}
/**
* rel_timestamp - Make timestamp relative to start time
* @a: Printer arguments
* @trace: The trace
*
* Return: Relative timestamp
*/
static llu_t
rel_timestamp(struct print_args *a, const struct dump_trace *trace)
{
return trace->timestamp - a->start_time;
}
/**
* sched_in_rel_timestamp - Make schedule in timestamp relative to start time
* @a: Printer arguments
* @trace: The trace
*
* Return: Relative timestamp of schedule in event this trace belongs to
*/
static llu_t
sched_in_rel_timestamp(struct print_args *a, const struct dump_trace *trace)
{
return trace->sched_in_timestamp - a->start_time;
}
/**
* sched_out_rel_timestamp - Make schedule out timestamp relative to start time
* @a: Printer arguments
* @trace: The trace
*
* Return: Relative timestamp of schedule out event this trace belongs to
*/
static llu_t
sched_out_rel_timestamp(struct print_args *a, const struct dump_trace *trace)
{
return trace->sched_info->base.timestamp - a->start_time;
}
/**
* source_info - Extract source information (file and line)
* @a: Printer arguments
* @c: Callstack
* @buf: Return buffer
* @bufsize: Size of return buffer
* @binary Pointer to binary
* @poffs Physical offset in binary
* @vaddr Pointer to store virtual address
*
* Return: true if lookup was successful, false otherwise.
*/
static bool
source_info(struct print_args *a, struct callstack *c,
char *buf, size_t bufsize, const char *binary,
uint64_t poffs, uint64_t *vaddr)
{
FILE *stream;
char cmdline[2048], tmp[64], *p;
char path[1024];
size_t pos = 0;
struct stat statbuf;
int ret;
if (source_hash_find(source_info_hashmap, binary, poffs,
buf, bufsize, vaddr)) {
return true;
}
/* Set fallback here */
*vaddr = poffs;
/* Path */
pos = 0;
strncpy(&path[pos], a->sysroot, sizeof(path) - pos - 1);
pos += strnlen(a->sysroot, 1024);
if (pos >= sizeof(path))
return false;
strncpy(&path[pos], binary, sizeof(path) - pos);
pos += strnlen(binary, 1024);
if (pos >= sizeof(cmdline))
return false;
path[pos] = '\0';
if (stat(path, &statbuf) != 0)
return false;
if (strncmp(binary, "'none'", sizeof("'none'")) == 0)
return false;
if (this_frameno(c) > 0) {
*vaddr = poffs;
goto have_vaddr;
}
/* offs2vaddr */
pos = 0;
strncpy(&cmdline[pos], "./offs2vaddr ", sizeof(cmdline) - pos);
pos += strlen("./offs2vaddr ");
strncpy(&cmdline[pos], path, sizeof(cmdline) - pos);
pos += strnlen(path, 1024);
if (pos >= sizeof(cmdline))
return false;
if (pos >= sizeof(cmdline) + 25)
return false;
if ((ret = snprintf(&cmdline[pos], 25, " 0x%llx", (llu_t) poffs)) <= 0)
return false;
pos += ret;
if (pos >= sizeof(cmdline))
return false;
cmdline[pos] = '\0';
stream = popen(cmdline, "r");
if (!stream)
return false;
if (fscanf(stream, "%32s", tmp) != 1) {
pclose(stream);
return false;
}
pclose(stream);
*vaddr = strtoull(tmp, NULL, 0);
have_vaddr:
/* addr2line */
pos = 0;
strncpy(&cmdline[pos], "addr2line -e ", sizeof(cmdline) - pos);
pos += strlen("addr2line -e ");
strncpy(&cmdline[pos], path, sizeof(cmdline) - pos);
pos += strnlen(path, 1024);
if (pos >= sizeof(cmdline))
return false;
ret = snprintf(&cmdline[pos], sizeof(cmdline) - pos, " 0x%llx ",
(llu_t) *vaddr);
if (ret <= 0)
return false;
pos += ret;
if (pos >= sizeof(cmdline))
return false;
strncpy(&cmdline[pos], "-f -s", sizeof(cmdline) - pos);
pos += sizeof("-f -p");
if (pos >= sizeof(cmdline))
return false;
cmdline[pos] = '\0';
stream = popen(cmdline, "r");
if (!stream)
return false;
if ((ret = fread(buf, 1, bufsize - 1, stream)) <= 0) {
pclose(stream);
return false;
}
pclose(stream);
buf[ret - 1] = '\0';
p = strstr(buf, "\n");
if (p) {
*p = ':';
p = strstr(p, " (");
if (p)
*p = '\0';
}
/* Cache the result */
source_hash_insert(source_info_hashmap, binary, poffs, buf, *vaddr);
return true;
}
static void
print_nop(struct print_args *a, struct callstack *c)
{
(void)a;
(void)c;
}
/* End print helper functions */
/* Begin default output format */
/**
* print_ecall_trace - Print an ecall trace
* @a: Printer arguments
* @c: Callstack
*
* Return: None
*/
static void
print_ecall_trace(struct print_args *a, struct callstack *c)
{
const char *type, *name;
unsigned function;
describe_frame_enter(this_frame(c), &type, &name, NULL, &function);
/* TODO: Support hcall if we add support for it in H/W */
printf("@=[%020llu] cpu=%03d %s\t\tfunction=[%05d] name=[%s]%c",
rel_timestamp(a, this_frame(c)->enter_trace), a->cpu, type, function, name, a->delim);
}
/**
* print_return_trace - Print a return trace
* @a: Printer arguments
* @c: Callstack
*
* Return: None
*/
static void
print_return_trace(struct print_args *a, struct callstack *c)
{
char buf[2048];
bool have_source_info;
/* TODO: Support hcall if we add support for it in H/W */
/* TODO: Print time delta */
const char *type;
uint64_t pc, offset, vaddr;
const char *binary = "'none'";
pc = full_pc(this_frame(c), a->pc_bits, a->sign_extend_pc);
offset = pc;
if (frame_down(c)->enter_trace == NULL) {
/* Return from other task */
type = "scdret";
} else {
type = return_type(frame_down(c));
}
binary = describe_binary(a, c, &pc, &offset);
have_source_info = source_info(a, c, buf, sizeof(buf), binary, offset, &vaddr);
printf("@=[%020llu] cpu=%03d %-5s\t\tpc=[%016llx] retval=[%05d] file=[%s+0x%llx] source=[%s]%c",
rel_timestamp(a, this_frame(c)->return_trace), a->cpu, type,
(llu_t) pc, this_frame(c)->return_trace->trace.ret.regval, binary,
(llu_t) vaddr, have_source_info ? buf : "'none'", a->delim);
}
/**
* print_exception_trace - Print an exception trace
* @a: Printer arguments
* @c: Callstack
*
* Return: None
*/
static void
print_exception_trace(struct print_args *a, struct callstack *c)
{
const char *type, *name, *desc;
unsigned cause;
describe_frame_enter(this_frame(c), &type, &name, &desc, &cause);
printf("@=[%020llu] cpu=%03d %s\t%s=[0x%03x] name=%s%c",
rel_timestamp(a, this_frame(c)->enter_trace), a->cpu, type, desc, cause, name,
a->delim);
}
/**
* print_timestamp_trace - Print a timestamp trace
* @a: Printer arguments
* @c: Callstack
*
* Return: None
*/
static void
print_timestamp_trace(struct print_args *a, struct callstack *c)
{
(void)c;
printf("@=[%020llu] cpu=%03d timestamp\tt=[%020llu]%c",
rel_timestamp(a, a->trace), a->cpu,
(llu_t) a->trace->trace.timestamp.timestamp,
a->delim);
}
/**
* print_invalid_trace - Print an invalid trace kind
* @a: Printer arguments
* @c: Callstack
*
* Return: None
*/
static void
print_invalid_trace(struct print_args *a, struct callstack *c)
{
(void)c;
long long data;
unsigned kind = a->trace->trace.kind;
memcpy(&data, &a->trace, sizeof(data));
printf("INVALID TRACE kind=%u data=[%016llx]%c", kind, data, a->delim);
}
/**
* print_pchi_trace - Print a PCHI trace
* @a: Printer arguments
* @c: Callstack
*
* Return: None
*/
static void
print_pchi_trace(struct print_args *a, struct callstack *c)
{
(void)c;
uint32_t pchi = a->trace->trace.pchi.pchi;
if (a->sign_extend_pc)
pchi = sign_extend_pchi(pchi, a->pc_bits);
printf("@=[%020llu] cpu=%03d pchi=[0x%08x] priv=[%d]%c",
rel_timestamp(a, a->trace), a->cpu, pchi,
a->trace->trace.pchi.priv, a->delim);
}
/**
* print_sched_info - Print a scheduling info entry
* @entry: The entry
* @timestamp: Timestamp
* @until: Schedule out time
* @clock_freq: Clock frequency (unused)
* @cpu: Which cpu
* @delim: Delimiter
*
* Return: None
*/
static void
print_sched_info(const struct owl_sched_info_full *entry,
uint64_t timestamp, uint64_t until,
float clock_freq, int cpu, char delim)
{
(void)clock_freq;
assert(entry->comm[OWL_TASK_COMM_LEN - 1] == '\0');
if (entry->base.cpu != cpu)
return;
printf("@=[%020llu] cpu=%03d sched\t\tcomm=[%s] pid=[%05d] until=[%020llu] cpu=[%d]%c",
(llu_t) timestamp, (int) entry->base.cpu,
entry->comm, entry->base.pid,
(llu_t) until, (int) entry->base.cpu, delim);
}
static void print_file_header(const struct owl_trace_file_header *fh);
static void print_stream_info(const struct owl_stream_info *si, uint64_t size);
static void
default_verbose_print_prologue(const struct owl_trace_file_header *fh)
{
const struct owl_stream_info *stream_info;
stream_info = (const struct owl_stream_info *)
(((uintptr_t) &fh[1]) + fh->stream_info_offs);
print_file_header(fh);
print_stream_info(stream_info, fh->stream_info_size);
}
static printfn_t default_print_trace[8] = {
[OWL_TRACE_KIND_UECALL] = print_ecall_trace,
[OWL_TRACE_KIND_RETURN] = print_return_trace,
[OWL_TRACE_KIND_SECALL] = print_ecall_trace,
[OWL_TRACE_KIND_TIMESTAMP] = print_nop,
[OWL_TRACE_KIND_EXCEPTION] = print_exception_trace,
[OWL_TRACE_KIND_PCHI] = print_nop,
[6] = print_invalid_trace,
[7] = print_invalid_trace,
};
static printfn_t default_verbose_print_trace[8] = {
[OWL_TRACE_KIND_UECALL] = print_ecall_trace,
[OWL_TRACE_KIND_RETURN] = print_return_trace,
[OWL_TRACE_KIND_SECALL] = print_ecall_trace,
[OWL_TRACE_KIND_TIMESTAMP] = print_timestamp_trace,
[OWL_TRACE_KIND_EXCEPTION] = print_exception_trace,
[OWL_TRACE_KIND_PCHI] = print_pchi_trace,
[6] = print_invalid_trace,
[7] = print_invalid_trace,
};
static struct printer default_printer = {
.print_trace = default_print_trace,
.print_sched = print_sched_info
};
static struct printer default_verbose_printer = {
.print_trace = default_verbose_print_trace,
.print_sched = print_sched_info,
.print_prologue = default_verbose_print_prologue
};
/* End default output format */
/* Begin FlameChart friendly output format */
/* FlameChart friendly output
* NB: For simplicity we only output the timestamp here, and not the duration.
* Post process with script */
static printfn_t print_flame_trace[8];
static printfn_t real_print_flame_trace[8];
static void
flame_recurse_down(struct print_args *a, struct callstack *c, int level)
{
const bool terminate = c->frameno >= level;
a->delim = terminate ? ' ' : ';';
real_print_flame_trace[this_frame(c)->enter_trace->trace.kind](a, c);
if (terminate) {
printf("%llu\n", rel_timestamp(a, this_frame(c)->enter_trace));
return;
}
c->frameno++;
flame_recurse_down(a, c, level);
}
static void
flame_recurse_up(struct print_args *a, struct callstack *c, int level)
{
const bool terminate = c->frameno >= level;
a->delim = terminate ? ' ' : ';';
if (terminate) {
real_print_flame_trace[this_frame(c)->return_trace->trace.kind](a, c);
printf("%llu\n", rel_timestamp(a, this_frame(c)->return_trace));
return;
} else {
c->frameno++;
real_print_flame_trace[this_frame(c)->enter_trace->trace.kind](a, c);
c->frameno--;
}
c->frameno++;
flame_recurse_up(a, c, level);
}
static void
flame_recurse_callgraph(struct print_args *orig_a, struct callstack *orig_c)
{
struct print_args a;
struct callstack c;
a = *orig_a;
c = *orig_c;
if (c.task->pid == 0) {
/* Special case the idle task */
printf("%s;", c.task->comm);
} else {
printf("%s/%d;", c.task->comm, c.task->pid);
}
c.frameno = 0;
if (orig_a->trace->trace.kind != OWL_TRACE_KIND_RETURN)
flame_recurse_down(&a, &c, orig_c->frameno);
else
flame_recurse_up(&a, &c, orig_c->frameno);
}
static void
print_flame_enter_trace(struct print_args *a, struct callstack *c)
{
const char *type = NULL, *name = NULL;
unsigned cause;
const char save_delim = a->delim;
describe_frame_enter(this_frame(c), &type, &name, NULL, &cause);
a->delim = ';';
c->frameno--;
if (this_frame(c)->return_trace != NULL)
real_print_flame_trace[this_frame(c)->return_trace->trace.kind](a, c);
c->frameno++;
a->delim = save_delim;
if (!name) {
name = alloca(32);
snprintf((char *)name, 32, "%s_[%u]", type, cause);
}
if (this_frame(c)->enter_trace->trace.kind == OWL_TRACE_KIND_EXCEPTION) {
printf("%s_%s%c", type, name, a->delim);
} else {
if (frame_up(c)->return_trace != NULL) {
printf("%s=%d%c", name,
frame_up(c)->return_trace->trace.ret.regval, a->delim);
} else {
printf("%s%c", name, a->delim);
}
}
}
static void
print_flame_return_trace(struct print_args *a, struct callstack *c)
{
/* TODO: Support hcall if we add support for it in H/W */
uint64_t pc, offset, vaddr;
char buf[2048];
bool have_source_info;
const char *binary = "'none'";
pc = full_pc(this_frame(c), a->pc_bits, a->sign_extend_pc);