forked from namhyung/uftrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uftrace.c
1836 lines (1564 loc) · 44.8 KB
/
uftrace.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
/*
* uftrace - Function (Graph) Tracer for Userspace
*
* Copyright (C) 2014-2018 LG Electronics
* Author: Namhyung Kim <namhyung@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <dirent.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
/* This should be defined before #include "utils.h" */
#define PR_FMT "uftrace"
#include "uftrace.h"
#include "utils/script.h"
#include "utils/utils.h"
#include "version.h"
static const char uftrace_version[] = "uftrace " UFTRACE_VERSION;
static bool dbg_domain_set = false;
static bool parsing_default_opts = false;
enum uftrace_short_options {
OPT_flat = 301,
OPT_no_libcall,
OPT_symbols,
OPT_logfile,
OPT_force,
OPT_task,
OPT_no_merge,
OPT_nop,
OPT_time,
OPT_max_stack,
OPT_host,
OPT_port,
OPT_nopager,
OPT_avg_total,
OPT_avg_self,
OPT_color,
OPT_disabled,
OPT_trace,
OPT_demangle,
OPT_dbg_domain,
OPT_report,
OPT_column_view,
OPT_column_offset,
OPT_bind_not,
OPT_task_newline,
OPT_chrome_trace,
OPT_flame_graph,
OPT_graphviz,
OPT_sample_time,
OPT_diff,
OPT_format,
OPT_sort_column,
OPT_tid_filter,
OPT_num_thread,
OPT_no_comment,
OPT_libmcount_single,
OPT_rt_prio,
OPT_kernel_bufsize,
OPT_kernel_skip_out,
OPT_kernel_full,
OPT_kernel_only,
OPT_list_event,
OPT_run_cmd,
OPT_opt_file,
OPT_keep_pid,
OPT_diff_policy,
OPT_event_full,
OPT_record,
OPT_no_args,
OPT_libname,
OPT_match_type,
OPT_no_randomize_addr,
OPT_no_event,
OPT_no_sched,
OPT_no_sched_preempt,
OPT_signal,
OPT_srcline,
OPT_with_syms,
OPT_clock,
OPT_usage,
OPT_libmcount_path,
OPT_mermaid,
OPT_library_path,
OPT_loc_filter,
};
/* clang-format off */
__used static const char uftrace_usage[] =
" uftrace -- function (graph) tracer for userspace\n"
"\n"
" usage: uftrace [COMMAND] [OPTION...] [<program>]\n"
"\n"
" COMMAND:\n"
" record Run a program and saves the trace data\n"
" replay Show program execution in the trace data\n"
" report Show performance statistics in the trace data\n"
" live Do record and replay in a row (default)\n"
" info Show system and program info in the trace data\n"
" dump Show low-level trace data\n"
" recv Save the trace data from network\n"
" graph Show function call graph in the trace data\n"
" script Run a script for recorded trace data\n"
" tui Show text user interface for graph and report\n"
"\n";
__used static const char uftrace_help[] =
" OPTION:\n"
" --avg-self Show average/min/max of self function time\n"
" --avg-total Show average/min/max of total function time\n"
" -a, --auto-args Show arguments and return value of known functions\n"
" -A, --argument=FUNC@arg[,arg,...]\n"
" Show function arguments\n"
" -b, --buffer=SIZE Size of tracing buffer (default: "
stringify(SHMEM_BUFFER_SIZE_KB) "K)\n"
" --chrome Dump recorded data in chrome trace format\n"
" --clock Set clock source for timestamp (default: mono)\n"
" --color=SET Use color for output: yes, no, auto (default: auto)\n"
" --column-offset=DEPTH Offset of each column (default: "
stringify(OPT_COLUMN_OFFSET) ")\n"
" --column-view Print tasks in separate columns\n"
" -C, --caller-filter=FUNC Only trace callers of those FUNCs\n"
" -d, --data=DATA Use this DATA instead of uftrace.data\n"
" --debug-domain=DOMAIN Filter debugging domain\n"
" --demangle=TYPE C++ symbol demangling: full, simple, no\n"
" (default: simple)\n"
" --diff=DATA Report differences\n"
" --diff-policy=POLICY Control diff report policy\n"
" (default: 'abs,compact,no-percent')\n"
" --disable Start with tracing disabled (deprecated)\n"
" -D, --depth=DEPTH Trace functions within DEPTH\n"
" -e, --estimate-return Use only entry record type for safety\n"
" --event-full Show all events outside of function\n"
" -E, --Event=EVENT Enable EVENT to save more information\n"
" --flame-graph Dump recorded data in FlameGraph format\n"
" --flat Use flat output format\n"
" --force Trace even if executable is not instrumented\n"
" --format=FORMAT Use FORMAT for output: normal, html (default: normal)\n"
" -f, --output-fields=FIELD Show FIELDs in the replay or graph output\n"
" -F, --filter=FUNC Only trace those FUNCs\n"
" -g --agent Start an agent in mcount to listen to commands\n"
" --graphviz Dump recorded data in DOT format\n"
" -H, --hide=FUNC Hide FUNCs from trace\n"
" --host=HOST Send trace data to HOST instead of write to file\n"
" -k, --kernel Trace kernel functions also (if supported)\n"
" --keep-pid Keep same pid during execution of traced program\n"
" --kernel-buffer=SIZE Size of kernel tracing buffer (default: 1408K)\n"
" --kernel-full Show kernel functions outside of user\n"
" --kernel-only Dump kernel data only\n"
" --kernel-skip-out Skip kernel functions outside of user (deprecated)\n"
" -K, --kernel-depth=DEPTH Trace kernel functions within DEPTH\n"
" --libmcount-single Use single thread version of libmcount\n"
" --list-event List available events\n"
" -L, --loc-filter=LOCATION Only trace functions in the source LOCATION\n"
" --logfile=FILE Save log messages to this file\n"
" -l, --nest-libcall Show nested library calls\n"
" --libname Show libname name with symbol name\n"
" --libmcount-path=PATH Load libmcount libraries from this PATH\n"
" --match=TYPE Support pattern match: regex, glob (default:\n"
" regex)\n"
" --max-stack=DEPTH Set max stack depth to DEPTH (default: "
stringify(OPT_RSTACK_MAX) ")\n"
" --no-args Do not show arguments and return value\n"
" --no-comment Don't show comments of returned functions\n"
" --no-event Disable (default) events\n"
" --no-sched Disable schedule events\n"
" --no-sched-preempt Hide pre-emptive schedule event\n"
" but show regular(sleeping) schedule event\n"
" --no-libcall Don't trace library function calls\n"
" --no-merge Don't merge leaf functions\n"
" --no-pager Do not use pager\n"
" --no-pltbind Do not bind dynamic symbols (LD_BIND_NOT)\n"
" --no-randomize-addr Disable ASLR (Address Space Layout Randomization)\n"
" --nop No operation (for performance test)\n"
" --num-thread=NUM Create NUM recorder threads\n"
" -N, --notrace=FUNC Don't trace those FUNCs\n"
" --opt-file=FILE Read command-line options from FILE\n"
" -p --pid=PID PID of an interactive mcount instance\n"
" --port=PORT Use PORT for network connection (default: "
stringify(UFTRACE_RECV_PORT) ")\n"
" -P, --patch=FUNC Apply dynamic patching for FUNCs\n"
" --record Record a new trace data before running command\n"
" --report Show live report\n"
" --rt-prio=PRIO Record with real-time (FIFO) priority\n"
" -r, --time-range=TIME~TIME Show output within the TIME(timestamp or elapsed time)\n"
" range only\n"
" --run-cmd=CMDLINE Command line that want to execute after tracing\n"
" data received\n"
" -R, --retval=FUNC@retval Show function return value\n"
" --sample-time=TIME Show flame graph with this sampling time\n"
" --signal=SIG@act[,act,...] Trigger action on those SIGnal\n"
" --sort-column=INDEX Sort diff report on column INDEX (default: 2)\n"
" --srcline Enable recording source line info\n"
" --symbols Print symbol tables\n"
" -s, --sort=KEY[,KEY,...] Sort reported functions by KEYs (default: "
stringify(OPT_SORT_COLUMN) ")\n"
" -S, --script=SCRIPT Run a given SCRIPT in function entry and exit\n"
" -t, --time-filter=TIME Hide small functions run less than the TIME\n"
" --task Show task info instead\n"
" --task-newline Interleave a newline when task is changed\n"
" --tid=TID[,TID,...] Only replay those tasks\n"
" --time Print time information\n"
" --trace=STATE Set the recording state: on, off (default: on)\n"
" -T, --trigger=FUNC@act[,act,...]\n"
" Trigger action on those FUNCs\n"
" -U, --unpatch=FUNC Don't apply dynamic patching for FUNCs\n"
" -v, --debug Print debug messages\n"
" --verbose Print verbose (debug) messages\n"
" --with-syms=DIR Use symbol files in the DIR\n"
" -W, --watch=POINT Watch and report POINT if it's changed\n"
" -Z, --size-filter=SIZE Apply dynamic patching for functions bigger than SIZE\n"
" -h, --help Give this help list\n"
" --usage Give a short usage message\n"
" -V, --version Print program version\n"
"\n"
" Try `man uftrace [COMMAND]' for more information.\n"
"\n";
__used static const char uftrace_footer[] =
" Try `uftrace --help' or `man uftrace [COMMAND]' for more information.\n"
"\n";
static const char uftrace_shopts[] =
"+aA:b:C:d:D:eE:f:F:ghH:kK:lL:N:p:P:r:R:s:S:t:T:U:vVW:Z:";
#define REQ_ARG(name, shopt) { #name, required_argument, 0, shopt }
#define NO_ARG(name, shopt) { #name, no_argument, 0, shopt }
static const struct option uftrace_options[] = {
REQ_ARG(libmcount-path, OPT_libmcount_path),
REQ_ARG(library-path, OPT_libmcount_path),
REQ_ARG(filter, 'F'),
REQ_ARG(notrace, 'N'),
REQ_ARG(depth, 'D'),
REQ_ARG(time-filter, 't'),
REQ_ARG(caller-filter, 'C'),
REQ_ARG(argument, 'A'),
REQ_ARG(trigger, 'T'),
REQ_ARG(retval, 'R'),
NO_ARG(auto-args, 'a'),
NO_ARG(no-args, OPT_no_args),
REQ_ARG(patch, 'P'),
REQ_ARG(unpatch, 'U'),
REQ_ARG(size-filter, 'Z'),
NO_ARG(debug, 'v'),
NO_ARG(verbose, 'v'),
REQ_ARG(debug-domain, OPT_dbg_domain),
NO_ARG(force, OPT_force),
REQ_ARG(data, 'd'),
NO_ARG(flat, OPT_flat),
NO_ARG(symbols, OPT_symbols),
REQ_ARG(buffer, 'b'),
REQ_ARG(logfile, OPT_logfile),
NO_ARG(task, OPT_task),
REQ_ARG(tid, OPT_tid_filter),
NO_ARG(no-merge, OPT_no_merge),
NO_ARG(nop, OPT_nop),
NO_ARG(time, OPT_time),
REQ_ARG(max-stack, OPT_max_stack),
REQ_ARG(host, OPT_host),
REQ_ARG(port, OPT_port),
NO_ARG(no-pager, OPT_nopager),
REQ_ARG(sort, 's'),
NO_ARG(avg-total, OPT_avg_total),
NO_ARG(avg-self, OPT_avg_self),
REQ_ARG(color, OPT_color),
NO_ARG(disable, OPT_disabled),
REQ_ARG(trace, OPT_trace),
REQ_ARG(demangle, OPT_demangle),
NO_ARG(record, OPT_record),
NO_ARG(report, OPT_report),
NO_ARG(column-view, OPT_column_view),
REQ_ARG(column-offset, OPT_column_offset),
NO_ARG(no-pltbind, OPT_bind_not),
NO_ARG(task-newline, OPT_task_newline),
NO_ARG(chrome, OPT_chrome_trace),
NO_ARG(graphviz, OPT_graphviz),
NO_ARG(flame-graph, OPT_flame_graph),
NO_ARG(mermaid, OPT_mermaid),
REQ_ARG(sample-time, OPT_sample_time),
REQ_ARG(diff, OPT_diff),
REQ_ARG(format, OPT_format),
REQ_ARG(sort-column, OPT_sort_column),
REQ_ARG(num-thread, OPT_num_thread),
NO_ARG(no-comment, OPT_no_comment),
NO_ARG(libmcount-single, OPT_libmcount_single),
REQ_ARG(rt-prio, OPT_rt_prio),
NO_ARG(kernel, 'k'),
REQ_ARG(kernel-depth, 'K'),
REQ_ARG(kernel-buffer, OPT_kernel_bufsize),
NO_ARG(kernel-skip-out, OPT_kernel_skip_out),
NO_ARG(kernel-full, OPT_kernel_full),
NO_ARG(kernel-only, OPT_kernel_only),
REQ_ARG(output-fields, 'f'),
REQ_ARG(time-range, 'r'),
REQ_ARG(Event, 'E'),
NO_ARG(no-event, OPT_no_event),
NO_ARG(no-sched, OPT_no_sched),
NO_ARG(no-sched-preempt, OPT_no_sched_preempt),
NO_ARG(list-event, OPT_list_event),
REQ_ARG(run-cmd, OPT_run_cmd),
REQ_ARG(opt-file, OPT_opt_file),
NO_ARG(keep-pid, OPT_keep_pid),
REQ_ARG(script, 'S'),
REQ_ARG(diff-policy, OPT_diff_policy),
NO_ARG(event-full, OPT_event_full),
NO_ARG(no-libcall, OPT_no_libcall),
NO_ARG(nest-libcall, 'l'),
NO_ARG(libname, OPT_libname),
REQ_ARG(match, OPT_match_type),
NO_ARG(no-randomize-addr, OPT_no_randomize_addr),
REQ_ARG(watch, 'W'),
REQ_ARG(signal, OPT_signal),
NO_ARG(srcline, OPT_srcline),
REQ_ARG(hide, 'H'),
REQ_ARG(loc-filter, OPT_loc_filter),
REQ_ARG(loc-filter-warning, 'L'), /* the long option is dummy, will change later */
REQ_ARG(clock, OPT_clock),
NO_ARG(help, 'h'),
NO_ARG(usage, OPT_usage),
NO_ARG(version, 'V'),
NO_ARG(estimate-return, 'e'),
REQ_ARG(with-syms, OPT_with_syms),
NO_ARG(agent, 'g'),
REQ_ARG(pid, 'p'),
{ 0 }
};
/* clang-format on */
#undef REQ_ARG
#undef NO_ARG
static unsigned long parse_size(char *str)
{
unsigned long size;
char *unit;
size = strtoul(str, &unit, 0);
switch (*unit) {
case '\0':
break;
case 'k':
case 'K':
size <<= 10;
break;
case 'm':
case 'M':
size <<= 20;
break;
case 'g':
case 'G':
size <<= 30;
break;
default:
pr_use("invalid size: %s\n", str);
size = 0;
break;
}
return size;
}
static char *opt_add_string(char *old_opt, char *new_opt)
{
return strjoin(old_opt, new_opt, ";");
}
static char *opt_add_prefix_string(char *old_opt, char *prefix, char *new_opt)
{
new_opt = strjoin(xstrdup(prefix), new_opt, "");
if (old_opt) {
old_opt = strjoin(old_opt, new_opt, ";");
free(new_opt);
new_opt = old_opt;
}
return new_opt;
}
static const char *true_str[] = {
"true", "yes", "on", "y", "1",
};
static const char *false_str[] = {
"false", "no", "off", "n", "0",
};
static enum color_setting parse_color(char *arg)
{
size_t i;
for (i = 0; i < ARRAY_SIZE(true_str); i++) {
if (!strcmp(arg, true_str[i]))
return COLOR_ON;
}
for (i = 0; i < ARRAY_SIZE(false_str); i++) {
if (!strcmp(arg, false_str[i]))
return COLOR_OFF;
}
if (!strcmp(arg, "auto"))
return COLOR_AUTO;
return COLOR_UNKNOWN;
}
static int parse_demangle(char *arg)
{
size_t i;
if (!strcmp(arg, "simple"))
return DEMANGLE_SIMPLE;
if (!strcmp(arg, "full")) {
if (support_full_demangle())
return DEMANGLE_FULL;
return DEMANGLE_NOT_SUPPORTED;
}
for (i = 0; i < ARRAY_SIZE(false_str); i++) {
if (!strcmp(arg, false_str[i]))
return DEMANGLE_NONE;
}
return DEMANGLE_ERROR;
}
static void parse_debug_domain(char *arg)
{
struct strv strv = STRV_INIT;
char *tok, *tmp;
int i;
strv_split(&strv, arg, ",");
strv_for_each(&strv, tok, i) {
int level = -1;
tmp = strchr(tok, ':');
if (tmp) {
*tmp++ = '\0';
level = strtol(tmp, NULL, 0);
}
if (!strcmp(tok, "ftrace")) /* for backward compatibility */
dbg_domain[DBG_UFTRACE] = level;
else if (!strcmp(tok, "uftrace"))
dbg_domain[DBG_UFTRACE] = level;
else if (!strcmp(tok, "symbol"))
dbg_domain[DBG_SYMBOL] = level;
else if (!strcmp(tok, "demangle"))
dbg_domain[DBG_DEMANGLE] = level;
else if (!strcmp(tok, "filter"))
dbg_domain[DBG_FILTER] = level;
else if (!strcmp(tok, "fstack"))
dbg_domain[DBG_FSTACK] = level;
else if (!strcmp(tok, "session"))
dbg_domain[DBG_SESSION] = level;
else if (!strcmp(tok, "kernel"))
dbg_domain[DBG_KERNEL] = level;
else if (!strcmp(tok, "mcount"))
dbg_domain[DBG_MCOUNT] = level;
else if (!strcmp(tok, "plthook"))
dbg_domain[DBG_PLTHOOK] = level;
else if (!strcmp(tok, "dynamic"))
dbg_domain[DBG_DYNAMIC] = level;
else if (!strcmp(tok, "event"))
dbg_domain[DBG_EVENT] = level;
else if (!strcmp(tok, "script"))
dbg_domain[DBG_SCRIPT] = level;
else if (!strcmp(tok, "dwarf"))
dbg_domain[DBG_DWARF] = level;
else if (!strcmp(tok, "wrap"))
dbg_domain[DBG_WRAP] = level;
}
dbg_domain_set = true;
strv_free(&strv);
}
static bool has_time_unit(const char *str)
{
if (isalpha(str[strlen(str) - 1]))
return true;
else
return false;
}
static uint64_t parse_any_timestamp(char *str, bool *elapsed)
{
if (*str == '\0')
return 0;
if (has_time_unit(str)) {
*elapsed = true;
return parse_time(str, 3);
}
*elapsed = false;
return parse_timestamp(str);
}
static bool parse_time_range(struct uftrace_time_range *range, char *arg)
{
char *str, *pos;
str = xstrdup(arg);
pos = strchr(str, '~');
if (pos == NULL) {
free(str);
return false;
}
*pos++ = '\0';
range->start = parse_any_timestamp(str, &range->start_elapsed);
range->stop = parse_any_timestamp(pos, &range->stop_elapsed);
free(str);
return true;
}
static char *remove_trailing_slash(char *path)
{
size_t len = strlen(path);
if (path[len - 1] == '/')
path[len - 1] = '\0';
return path;
}
static bool is_libmcount_directory(const char *path)
{
DIR *dp = NULL;
struct dirent *ent;
int ret = false;
dp = opendir(path);
if (dp == NULL)
return false;
while ((ent = readdir(dp)) != NULL) {
if ((ent->d_type == DT_DIR && !strcmp(ent->d_name, "libmcount")) ||
((ent->d_type == DT_LNK || ent->d_type == DT_REG) &&
!strcmp(ent->d_name, "libmcount.so"))) {
ret = true;
break;
}
}
closedir(dp);
return ret;
}
static int parse_option(struct uftrace_opts *opts, int key, char *arg)
{
char *pos;
switch (key) {
case 'F':
opts->filter = opt_add_string(opts->filter, arg);
break;
case 'N':
opts->filter = opt_add_prefix_string(opts->filter, "!", arg);
break;
case 'T':
opts->trigger = opt_add_string(opts->trigger, arg);
break;
case 'D':
opts->depth = strtol(arg, NULL, 0);
if (opts->depth <= 0 || opts->depth >= OPT_DEPTH_MAX) {
pr_use("invalid depth given: %s (ignoring..)\n", arg);
opts->depth = OPT_DEPTH_DEFAULT;
}
break;
case 'C':
opts->caller = opt_add_string(opts->caller, arg);
/*
* caller filter focuses onto a given function,
* displaying sched event with it is annoying.
*/
opts->no_sched = true;
break;
case 'H':
opts->hide = opt_add_string(opts->hide, arg);
break;
case 'L':
if (is_libmcount_directory(arg))
pr_warn("--libmcount-path option should be used to set libmcount path.\n");
/* fall through */
case OPT_loc_filter:
pos = strstr(arg, "@hide");
if (!pos)
opts->loc_filter = opt_add_string(opts->loc_filter, arg);
else {
*pos = '\0';
opts->loc_filter = opt_add_prefix_string(opts->loc_filter, "!", arg);
}
/*
* location filter focuses onto a given location,
* displaying sched event with it is annoying.
*/
opts->no_sched = true;
break;
case 'v':
debug++;
break;
case 'd':
opts->dirname = remove_trailing_slash(arg);
break;
case 'b':
opts->bufsize = parse_size(arg);
if (opts->bufsize & (getpagesize() - 1)) {
pr_use("buffer size should be multiple of page size\n");
opts->bufsize = ROUND_UP(opts->bufsize, getpagesize());
}
break;
case 'k':
opts->kernel = true;
opts->kernel_depth = 1;
break;
case 'K':
opts->kernel = true;
opts->kernel_depth = strtol(arg, NULL, 0);
if (opts->kernel_depth < 1 || opts->kernel_depth > 50) {
pr_use("invalid kernel depth: %s. Set depth to 1.\n", arg);
opts->kernel_depth = 1;
}
break;
case 's':
opts->sort_keys = opt_add_string(opts->sort_keys, arg);
break;
case 'S':
opts->script_file = arg;
break;
case 't':
/* do not override time-filter or time-range if it's already set */
if (parsing_default_opts) {
if (opts->threshold || opts->range.start || opts->range.stop)
break;
}
/* add time-filter to uftrace.data/default.opts */
strv_append(&default_opts, "-t");
strv_append(&default_opts, arg);
opts->threshold = parse_time(arg, 3);
if (opts->threshold >= OPT_THRESHOLD_MAX) {
pr_use("invalid time given: %lu (ignoring..)\n", opts->threshold);
opts->threshold = OPT_THRESHOLD_MAX - 1;
}
if (opts->range.start || opts->range.stop) {
pr_use("--time-range cannot be used with --time-filter\n");
opts->range.start = opts->range.stop = 0;
}
break;
case 'A':
opts->args = opt_add_string(opts->args, arg);
break;
case 'R':
opts->retval = opt_add_string(opts->retval, arg);
break;
case 'a':
opts->auto_args = true;
break;
case 'l':
/* --nest-libcall implies --force option */
opts->force = true;
opts->nest_libcall = true;
break;
case 'f':
opts->fields = arg;
break;
case 'r':
if (!parse_time_range(&opts->range, arg))
pr_use("invalid time range: %s (ignoring...)\n", arg);
if (opts->threshold) {
pr_use("--time-filter cannot be used with --time-range\n");
opts->threshold = 0;
}
break;
case 'P':
opts->patch = opt_add_string(opts->patch, arg);
break;
case 'U':
opts->patch = opt_add_prefix_string(opts->patch, "!", arg);
break;
case 'Z':
opts->size_filter = strtol(arg, NULL, 0);
if (opts->size_filter <= 0) {
pr_use("--size-filter should be positive\n");
opts->size_filter = 0;
}
break;
case 'E':
if (!strcmp(arg, "list")) {
pr_use("'-E list' is deprecated, use --list-event instead.\n");
opts->list_event = true;
}
else
opts->event = opt_add_string(opts->event, arg);
break;
case 'W':
opts->watch = opt_add_string(opts->watch, arg);
break;
case 'e':
opts->estimate_return = true;
break;
case 'V':
pr_out("%s\n", uftrace_version);
return -1;
case 'g':
opts->agent = true;
break;
case 'h':
return -3;
case 'p':
opts->pid = strtol(arg, NULL, 0);
opts->exename = "";
break;
case OPT_libmcount_path:
opts->lib_path = arg;
break;
case OPT_usage:
return -2;
case OPT_flat:
opts->flat = true;
break;
case OPT_no_libcall:
opts->libcall = false;
break;
case OPT_symbols:
opts->print_symtab = true;
break;
case OPT_logfile:
opts->logfile = arg;
break;
case OPT_force:
opts->force = true;
break;
case OPT_task:
opts->show_task = true;
break;
case OPT_tid_filter:
if (strtol(arg, NULL, 0) <= 0)
pr_use("invalid thread id: %s\n", arg);
else
opts->tid = opt_add_string(opts->tid, arg);
break;
case OPT_no_merge:
opts->no_merge = true;
break;
case OPT_nop:
opts->nop = true;
break;
case OPT_time:
opts->time = true;
break;
case OPT_max_stack:
opts->max_stack = strtol(arg, NULL, 0);
if (opts->max_stack <= 0 || opts->max_stack > OPT_RSTACK_MAX) {
pr_use("max stack depth should be >0 and <%d\n", OPT_RSTACK_MAX);
opts->max_stack = OPT_RSTACK_DEFAULT;
}
break;
case OPT_host:
opts->host = arg;
break;
case OPT_port:
opts->port = strtol(arg, NULL, 0);
if (opts->port <= 0) {
pr_use("invalid port number: %s (ignoring..)\n", arg);
opts->port = UFTRACE_RECV_PORT;
}
break;
case OPT_nopager:
opts->use_pager = false;
break;
case OPT_avg_total:
opts->avg_total = true;
break;
case OPT_avg_self:
opts->avg_self = true;
break;
case OPT_color:
opts->color = parse_color(arg);
if (opts->color == COLOR_UNKNOWN) {
pr_use("unknown color setting: %s (ignoring..)\n", arg);
opts->color = COLOR_AUTO;
}
break;
case OPT_disabled:
pr_use("'--disable' is deprecated, use --trace=off instead.\n");
opts->trace = TRACE_STATE_OFF;
break;
case OPT_trace:
if (!strcmp(arg, "on"))
opts->trace = TRACE_STATE_ON;
else if (!strcmp(arg, "off"))
opts->trace = TRACE_STATE_OFF;
else
pr_use("unknown tracing state: %s (ignoring..)\n", arg);
break;
case OPT_demangle:
demangler = parse_demangle(arg);
if (demangler == DEMANGLE_ERROR) {
pr_use("unknown demangle value: %s (ignoring..)\n", arg);
demangler = DEMANGLE_SIMPLE;
}
else if (demangler == DEMANGLE_NOT_SUPPORTED) {
pr_use("'%s' demangler is not supported\n", arg);
demangler = DEMANGLE_SIMPLE;
}
break;
case OPT_dbg_domain:
parse_debug_domain(arg);
break;
case OPT_report:
opts->report = true;
break;
case OPT_column_view:
opts->column_view = true;
break;
case OPT_column_offset:
opts->column_offset = strtol(arg, NULL, 0);
if (opts->column_offset < 0)
opts->column_offset = OPT_COLUMN_OFFSET;
break;
case OPT_bind_not:
opts->want_bind_not = true;
break;
case OPT_task_newline:
opts->task_newline = true;
break;
case OPT_chrome_trace:
opts->chrome_trace = true;
break;
case OPT_flame_graph:
opts->flame_graph = true;
break;
case OPT_graphviz:
opts->graphviz = true;
break;
case OPT_diff:
opts->diff = arg;
break;
case OPT_diff_policy:
opts->diff_policy = arg;
break;
case OPT_format:
if (!strcmp(arg, "normal"))
format_mode = FORMAT_NORMAL;
else if (!strcmp(arg, "html")) {
format_mode = FORMAT_HTML;
if (opts->color == COLOR_AUTO)
opts->color = COLOR_ON;
}
else {
pr_use("invalid format argument: %s\n", arg);
format_mode = FORMAT_NORMAL;
}
break;
case OPT_sort_column:
opts->sort_column = strtol(arg, NULL, 0);
if (opts->sort_column < 0 || opts->sort_column > OPT_SORT_COLUMN) {
pr_use("invalid column number: %d\n", opts->sort_column);
pr_use("force to set it to --sort-column=%d for diff percentage\n",
OPT_SORT_COLUMN);
opts->sort_column = OPT_SORT_COLUMN;
}
break;
case OPT_num_thread:
opts->nr_thread = strtol(arg, NULL, 0);
if (opts->nr_thread < 0) {
pr_use("invalid thread number: %s\n", arg);
opts->nr_thread = 0;
}
break;
case OPT_no_comment:
opts->comment = false;
break;
case OPT_libmcount_single:
opts->libmcount_single = true;
break;
case OPT_rt_prio:
opts->rt_prio = strtol(arg, NULL, 0);
if (opts->rt_prio < 1 || opts->rt_prio > 99) {
pr_use("invalid rt prioity: %d (ignoring...)\n", opts->rt_prio);
opts->rt_prio = 0;
}
break;
case OPT_kernel_bufsize:
opts->kernel_bufsize = parse_size(arg);
if (opts->kernel_bufsize & (getpagesize() - 1)) {
pr_use("buffer size should be multiple of page size\n");
opts->kernel_bufsize = ROUND_UP(opts->kernel_bufsize, getpagesize());
}
break;
case OPT_kernel_skip_out: /* deprecated */
opts->kernel_skip_out = true;
break;