1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
|
/*
* uftrace - Function (Graph) Tracer for Userspace
*
* Copyright (C) 2014-2018 LG Electronics, Namhyung Kim <namhyung.kim@lge.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 <stdio.h>
#include <stdlib.h>
#include <argp.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <time.h>
/* This should be defined before #include "utils.h" */
#define PR_FMT "uftrace"
#include "uftrace.h"
#include "version.h"
#include "libmcount/mcount.h"
#include "libtraceevent/kbuffer.h"
#include "utils/utils.h"
#include "utils/symbol.h"
#include "utils/rbtree.h"
#include "utils/list.h"
#include "utils/fstack.h"
#include "utils/filter.h"
/* output of --version option (generated by argp runtime) */
const char *argp_program_version = "uftrace " UFTRACE_VERSION;
/* (a part of) output in --help option (generated by argp runtime) */
const char *argp_program_bug_address = "https://github.com/namhyung/uftrace/issues";
static bool dbg_domain_set = false;
enum options {
OPT_flat = 301,
OPT_no_libcall,
OPT_symbols,
OPT_logfile,
OPT_force,
OPT_threads,
OPT_no_merge,
OPT_nop,
OPT_time,
OPT_max_stack,
OPT_port,
OPT_nopager,
OPT_avg_total,
OPT_avg_self,
OPT_color,
OPT_disabled,
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_sample_time,
OPT_diff,
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_nest_libcall,
OPT_record,
OPT_auto_args,
OPT_libname,
OPT_match_type,
OPT_no_randomize_addr,
OPT_no_event,
};
static struct argp_option uftrace_options[] = {
{ "library-path", 'L', "PATH", 0, "Load libraries from this PATH" },
{ "filter", 'F', "FUNC", 0, "Only trace those FUNCs" },
{ "notrace", 'N', "FUNC", 0, "Don't trace those FUNCs" },
{ "trigger", 'T', "FUNC@act[,act,...]", 0, "Trigger action on those FUNCs" },
{ "depth", 'D', "DEPTH", 0, "Trace functions within DEPTH" },
{ "debug", 'v', 0, 0, "Print debug messages" },
{ "verbose", 'v', 0, 0, "Print verbose (debug) messages" },
{ "data", 'd', "DATA", 0, "Use this DATA instead of uftrace.data" },
{ "flat", OPT_flat, 0, 0, "Use flat output format" },
{ "no-libcall", OPT_no_libcall, 0, 0, "Don't trace library function calls" },
{ "symbols", OPT_symbols, 0, 0, "Print symbol tables" },
{ "buffer", 'b', "SIZE", 0, "Size of tracing buffer (default: 128K)" },
{ "logfile", OPT_logfile, "FILE", 0, "Save log messages to this file" },
{ "force", OPT_force, 0, 0, "Trace even if executable is not instrumented" },
{ "threads", OPT_threads, 0, 0, "Report thread stats instead" },
{ "tid", OPT_tid_filter, "TID[,TID,...]", 0, "Only replay those tasks" },
{ "no-merge", OPT_no_merge, 0, 0, "Don't merge leaf functions" },
{ "nop", OPT_nop, 0, 0, "No operation (for performance test)" },
{ "time", OPT_time, 0, 0, "Print time information" },
{ "max-stack", OPT_max_stack, "DEPTH", 0, "Set max stack depth to DEPTH (default: 1024)" },
{ "kernel", 'k', 0, 0, "Trace kernel functions also (if supported)" },
{ "host", 'H', "HOST", 0, "Send trace data to HOST instead of write to file" },
{ "port", OPT_port, "PORT", 0, "Use PORT for network connection (default: 8090)" },
{ "no-pager", OPT_nopager, 0, 0, "Do not use pager" },
{ "sort", 's', "KEY[,KEY,...]", 0, "Sort reported functions by KEYs (default: total)" },
{ "avg-total", OPT_avg_total, 0, 0, "Show average/min/max of total function time" },
{ "avg-self", OPT_avg_self, 0, 0, "Show average/min/max of self function time" },
{ "color", OPT_color, "SET", 0, "Use color for output: yes, no, auto (default: auto)" },
{ "disable", OPT_disabled, 0, 0, "Start with tracing disabled" },
{ "demangle", OPT_demangle, "TYPE", 0, "C++ symbol demangling: full, simple, no (default: simple)" },
{ "debug-domain", OPT_dbg_domain, "DOMAIN", 0, "Filter debugging domain" },
{ "report", OPT_report, 0, 0, "Show live report" },
{ "column-view", OPT_column_view, 0, 0, "Print tasks in separate columns" },
{ "column-offset", OPT_column_offset, "DEPTH", 0, "Offset of each column (default: 8)" },
{ "no-pltbind", OPT_bind_not, 0, 0, "Do not bind dynamic symbols (LD_BIND_NOT)" },
{ "task-newline", OPT_task_newline, 0, 0, "Interleave a newline when task is changed" },
{ "time-filter", 't', "TIME", 0, "Hide small functions run less than the TIME" },
{ "argument", 'A', "FUNC@arg[,arg,...]", 0, "Show function arguments" },
{ "retval", 'R', "FUNC@retval", 0, "Show function return value" },
{ "chrome", OPT_chrome_trace, 0, 0, "Dump recorded data in chrome trace format" },
{ "diff", OPT_diff, "DATA", 0, "Report differences" },
{ "sort-column", OPT_sort_column, "INDEX", 0, "Sort diff report on column INDEX (default: 2)" },
{ "num-thread", OPT_num_thread, "NUM", 0, "Create NUM recorder threads" },
{ "no-comment", OPT_no_comment, 0, 0, "Don't show comments of returned functions" },
{ "libmcount-single", OPT_libmcount_single, 0, 0, "Use single thread version of libmcount" },
{ "rt-prio", OPT_rt_prio, "PRIO", 0, "Record with real-time (FIFO) priority" },
{ "kernel-depth", 'K', "DEPTH", 0, "Trace kernel functions within DEPTH (default: 1)" },
{ "kernel-buffer", OPT_kernel_bufsize, "SIZE", 0, "Size of kernel tracing buffer (default: 1408K)" },
{ "kernel-skip-out", OPT_kernel_skip_out, 0, 0, "Skip kernel functions outside of user (deprecated)" },
{ "kernel-full", OPT_kernel_full, 0, 0, "Show kernel functions outside of user" },
{ "kernel-only", OPT_kernel_only, 0, 0, "Dump kernel data only" },
{ "flame-graph", OPT_flame_graph, 0, 0, "Dump recorded data in FlameGraph format" },
{ "sample-time", OPT_sample_time, "TIME", 0, "Show flame graph with this sampling time" },
{ "output-fields", 'f', "FIELD", 0, "Show FIELDs in the replay or graph output" },
{ "time-range", 'r', "TIME~TIME", 0, "Show output within the TIME(timestamp or elapsed time) range only" },
{ "patch", 'P', "FUNC", 0, "Apply dynamic patching for FUNCs" },
{ "event", 'E', "EVENT", 0, "Enable EVENT to save more information" },
{ "list-event", OPT_list_event, 0, 0, "List available events" },
{ "run-cmd", OPT_run_cmd, "CMDLINE", 0, "Command line that want to execute after tracing data received" },
{ "opt-file", OPT_opt_file, "FILE", 0, "Read command-line options from FILE" },
{ "keep-pid", OPT_keep_pid, 0, 0, "Keep same pid during execution of traced program" },
{ "script", 'S', "SCRIPT", 0, "Run a given SCRIPT in function entry and exit" },
{ "diff-policy", OPT_diff_policy, "POLICY", 0, "Control diff report policy (default: 'abs,compact,no-percent')" },
{ "event-full", OPT_event_full, 0, 0, "Show all events outside of function" },
{ "nest-libcall", OPT_nest_libcall, 0, 0, "Show nested library calls" },
{ "record", OPT_record, 0, 0, "Record a new trace data before running command" },
{ "auto-args", 'a', 0, 0, "Show arguments and return value of known functions" },
{ "libname", OPT_libname, 0, 0, "Show libname name with symbol name" },
{ "match", OPT_match_type, "TYPE", 0, "Support pattern match: regex, glob (default: regex)" },
{ "no-randomize-addr", OPT_no_randomize_addr, 0, 0, "Disable ASLR (Address Space Layout Randomization)" },
{ "no-event", OPT_no_event, 0, 0, "Disable (default) events" },
{ "help", 'h', 0, 0, "Give this help list" },
{ 0 }
};
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, "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;
}
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_timestamp(char *str, bool *elapsed)
{
char *time;
uint64_t nsec;
if (*str == '\0')
return 0;
if (has_time_unit(str)) {
*elapsed = true;
return parse_time(str, 3);
}
if (asprintf(&time, "%ssec", str) < 0)
return -1;
nsec = parse_time(time, 9);
free(time);
return nsec;
}
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_timestamp(str, &range->start_elapsed);
range->stop = parse_timestamp(pos, &range->stop_elapsed);
free(str);
return true;
}
static error_t parse_option(int key, char *arg, struct argp_state *state)
{
struct opts *opts = state->input;
switch (key) {
case 'L':
opts->lib_path = arg;
break;
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 'v':
debug++;
break;
case 'd':
opts->dirname = 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;
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 (ignoring...)\n", arg);
opts->kernel_depth = 0;
}
break;
case 'H':
opts->host = arg;
break;
case 's':
opts->sort_keys = opt_add_string(opts->sort_keys, arg);
break;
case 'S':
opts->script_file = arg;
break;
case 't':
opts->threshold = parse_time(arg, 3);
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 '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 '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 'h':
argp_state_help (state, state->out_stream, ARGP_HELP_STD_HELP);
break;
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_threads:
opts->report_thread = 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_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:
opts->disabled = true;
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);
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_diff:
opts->diff = arg;
break;
case OPT_diff_policy:
opts->diff_policy = arg;
break;
case OPT_sort_column:
opts->sort_column = strtol(arg, NULL, 0);
if (opts->sort_column < 0 || opts->sort_column > 2) {
pr_use("invalid column number: %d\n", opts->sort_column);
pr_use("force to set it to --sort-column=2 for diff percentage\n");
opts->sort_column = 2;
}
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;
case OPT_kernel_full:
opts->kernel_skip_out = false;
/* see setup_kernel_tracing() also */
break;
case OPT_kernel_only:
opts->kernel_only = true;
break;
case OPT_sample_time:
opts->sample_time = parse_time(arg, 9);
break;
case OPT_list_event:
opts->list_event = true;
break;
case OPT_run_cmd:
if (opts->run_cmd) {
pr_warn("intermediate --run-cmd argument is ignored.\n");
free_parsed_cmdline(opts->run_cmd);
}
opts->run_cmd = parse_cmdline(arg, NULL);
break;
case OPT_opt_file:
opts->opt_file = arg;
break;
case OPT_keep_pid:
opts->keep_pid = true;
break;
case OPT_event_full:
opts->event_skip_out = false;
break;
case OPT_nest_libcall:
/* --nest-libcall implies --force option */
opts->force = true;
opts->nest_libcall = true;
break;
case OPT_record:
opts->record = true;
break;
case OPT_libname:
opts->libname = true;
break;
case OPT_match_type:
opts->patt_type = parse_filter_pattern(arg);
if (opts->patt_type == PATT_NONE) {
pr_use("invalid match pattern: %s (ignoring...)\n", arg);
opts->patt_type = PATT_REGEX;
}
break;
case OPT_no_randomize_addr:
opts->no_randomize_addr = true;
break;
case OPT_no_event:
opts->no_event = true;
break;
case ARGP_KEY_ARG:
if (state->arg_num) {
/*
* This is a second non-option argument.
* Returning ARGP_ERR_UNKNOWN will pass control to
* the ARGP_KEY_ARGS case.
*/
return ARGP_ERR_UNKNOWN;
}
if (!strcmp("record", arg))
opts->mode = UFTRACE_MODE_RECORD;
else if (!strcmp("replay", arg))
opts->mode = UFTRACE_MODE_REPLAY;
else if (!strcmp("live", arg))
opts->mode = UFTRACE_MODE_LIVE;
else if (!strcmp("report", arg))
opts->mode = UFTRACE_MODE_REPORT;
else if (!strcmp("info", arg))
opts->mode = UFTRACE_MODE_INFO;
else if (!strcmp("recv", arg))
opts->mode = UFTRACE_MODE_RECV;
else if (!strcmp("dump", arg))
opts->mode = UFTRACE_MODE_DUMP;
else if (!strcmp("graph", arg))
opts->mode = UFTRACE_MODE_GRAPH;
else if (!strcmp("script", arg))
opts->mode = UFTRACE_MODE_SCRIPT;
else if (!strcmp("tui", arg))
opts->mode = UFTRACE_MODE_TUI;
else
return ARGP_ERR_UNKNOWN; /* almost same as fall through */
break;
case ARGP_KEY_ARGS:
/*
* process remaining non-option arguments
*/
if (opts->mode == UFTRACE_MODE_INVALID)
opts->mode = UFTRACE_MODE_DEFAULT;
opts->exename = state->argv[state->next];
opts->idx = state->next;
break;
case ARGP_KEY_NO_ARGS:
case ARGP_KEY_END:
if (opts->opt_file)
break;
if (state->arg_num < 1)
argp_usage(state);
if (opts->exename == NULL) {
switch (opts->mode) {
case UFTRACE_MODE_RECORD:
case UFTRACE_MODE_LIVE:
argp_usage(state);
break;
default:
/* will be set after read_ftrace_info() */
break;
}
}
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static void parse_opt_file(int *argc, char ***argv, char *filename, struct opts *opts)
{
int file_argc;
char **file_argv;
char *buf;
struct stat stbuf;
FILE *fp;
struct argp file_argp = {
.options = uftrace_options,
.parser = parse_option,
.args_doc = "[record|replay|live|report|info|dump|recv|graph|script|tui] [<program>]",
.doc = "uftrace -- function (graph) tracer for userspace",
};
char *orig_exename = NULL;
int orig_idx = 0;
if (stat(filename, &stbuf) < 0) {
pr_use("Cannot use opt-file: %s: %m\n", filename);
exit(0);
}
buf = xmalloc(stbuf.st_size + 1);
fp = fopen(filename, "r");
if (fp == NULL)
pr_err("Open failed: %s", filename);
fread_all(buf, stbuf.st_size, fp);
fclose(fp);
buf[stbuf.st_size] = '\0';
file_argv = parse_cmdline(buf, &file_argc);
/* clear opt_file for error reporting */
opts->opt_file = NULL;
if (opts->idx) {
orig_idx = opts->idx;
orig_exename = opts->exename;
opts->idx = 0;
}
argp_parse(&file_argp, file_argc, file_argv,
ARGP_IN_ORDER | ARGP_PARSE_ARGV0 | ARGP_NO_ERRS,
NULL, opts);
/* overwrite argv only if it's not given on command line */
if (orig_idx == 0 && opts->idx) {
*argc = file_argc;
*argv = file_argv;
/* mark it to free at the end */
opts->opt_file = filename;
}
else {
opts->idx = orig_idx;
opts->exename = orig_exename;
free_parsed_cmdline(file_argv);
}
free(buf);
}
/*
* Parse options in a script file header. For example,
*
* # uftrace-option: -F main -A malloc@arg1
* def uftrace_entry():
* pass
* ...
*
* Note that it only handles some options like filter, trigger,
* argument, return values and maybe some more.
*/
void parse_script_opt(struct opts *opts)
{
FILE *fp;
int orig_idx;
int opt_argc;
char **opt_argv;
char *line = NULL;
size_t len = 0;
static const char optname[] = "uftrace-option";
struct argp opt_argp = {
.options = uftrace_options,
.parser = parse_option,
.args_doc = "[record|replay|live|report|info|dump|recv|graph|script|tui] [<program>]",
.doc = "uftrace -- function (graph) tracer for userspace",
};
if (opts->script_file == NULL)
return;
fp = fopen(opts->script_file, "r");
if (fp == NULL)
pr_err("cannot open script file: %s", opts->script_file);
while (getline(&line, &len, fp) > 0) {
char *pos;
if (line[0] != '#')
continue;
pos = line + 1;
while (isspace(*pos))
pos++;
if (strncmp(pos, optname, strlen(optname)))
continue;
/* extract option value */
pos = strchr(line, ':');
if (pos == NULL)
break;
pr_dbg("adding record option from script: %s", pos + 1);
opt_argv = parse_cmdline(pos + 1, &opt_argc);
orig_idx = opts->idx;
argp_parse(&opt_argp, opt_argc, opt_argv,
ARGP_IN_ORDER | ARGP_PARSE_ARGV0 | ARGP_NO_ERRS,
NULL, opts);
opts->idx = orig_idx;
free_parsed_cmdline(opt_argv);
break;
}
free(line);
fclose(fp);
}
static void free_opts(struct opts *opts)
{
free(opts->filter);
free(opts->trigger);
free(opts->sort_keys);
free(opts->args);
free(opts->retval);
free(opts->tid);
free(opts->event);
}
#ifndef UNIT_TEST
int main(int argc, char *argv[])
{
struct opts opts = {
.mode = UFTRACE_MODE_INVALID,
.dirname = UFTRACE_DIR_NAME,
.libcall = true,
.bufsize = SHMEM_BUFFER_SIZE,
.depth = OPT_DEPTH_DEFAULT,
.max_stack = OPT_RSTACK_DEFAULT,
.port = UFTRACE_RECV_PORT,
.use_pager = true,
.color = COLOR_AUTO, /* default to 'auto' (turn on if terminal) */
.column_offset = 8,
.comment = true,
.kernel_skip_out= true,
.fields = NULL,
.sort_column = 2,
.event_skip_out = true,
.patt_type = PATT_REGEX,
};
struct argp argp = {
.options = uftrace_options,
.parser = parse_option,
.args_doc = "[record|replay|live|report|info|dump|recv|graph|script|tui] [<program>]",
.doc = "uftrace -- function (graph) tracer for userspace",
};
int ret = -1;
/* this must be done before argp_parse() */
logfp = stderr;
outfp = stdout;
argp_parse(&argp, argc, argv, ARGP_IN_ORDER, NULL, &opts);
if (opts.opt_file)
parse_opt_file(&argc, &argv, opts.opt_file, &opts);
if (dbg_domain_set && !debug)
debug = 1;
if (opts.logfile) {
logfp = fopen(opts.logfile, "a");
if (logfp == NULL)
pr_err("cannot open log file");
setvbuf(logfp, NULL, _IOLBF, 1024);
}
else if (debug) {
/* ensure normal output is not mixed by debug message */
setvbuf(outfp, NULL, _IOLBF, 1024);
}
if (debug) {
int d;
/* set default debug level */
for (d = 0; d < DBG_DOMAIN_MAX; d++) {
if (dbg_domain[d] == -1 || !dbg_domain_set)
dbg_domain[d] = debug;
}
}
opts.range.kernel_skip_out = opts.kernel_skip_out;
opts.range.event_skip_out = opts.event_skip_out;
setup_color(opts.color);
setup_signal();
if (opts.mode == UFTRACE_MODE_RECORD ||
opts.mode == UFTRACE_MODE_RECV ||
opts.mode == UFTRACE_MODE_TUI)
opts.use_pager = false;
if (opts.nop)
opts.use_pager = false;
if (opts.use_pager)
start_pager();
if (opts.idx == 0)
opts.idx = argc;
argc -= opts.idx;
argv += opts.idx;
switch (opts.mode) {
case UFTRACE_MODE_RECORD:
ret = command_record(argc, argv, &opts);
break;
case UFTRACE_MODE_REPLAY:
ret = command_replay(argc, argv, &opts);
break;
case UFTRACE_MODE_LIVE:
ret = command_live(argc, argv, &opts);
break;
case UFTRACE_MODE_REPORT:
ret = command_report(argc, argv, &opts);
break;
case UFTRACE_MODE_INFO:
ret = command_info(argc, argv, &opts);
break;
case UFTRACE_MODE_RECV:
ret = command_recv(argc, argv, &opts);
break;
case UFTRACE_MODE_DUMP:
ret = command_dump(argc, argv, &opts);
break;
case UFTRACE_MODE_GRAPH:
ret = command_graph(argc, argv, &opts);
break;
case UFTRACE_MODE_SCRIPT:
ret = command_script(argc, argv, &opts);
break;
case UFTRACE_MODE_TUI:
ret = command_tui(argc, argv, &opts);
break;
case UFTRACE_MODE_INVALID:
ret = 1;
break;
}
wait_for_pager();
if (opts.logfile)
fclose(logfp);
if (opts.opt_file)
free_parsed_cmdline(argv);
free_opts(&opts);
return ret;
}
#else
#define OPT_FILE "opt"
TEST_CASE(option_parsing1)
{
char *stropt = NULL;
int i;
bool elapsed_time;
TEST_EQ(parse_size("1234"), 1234);
TEST_EQ(parse_size("10k"), 10240);
TEST_EQ(parse_size("100M"), 100 * 1024 * 1024);
stropt = opt_add_string(stropt, "abc");
TEST_STREQ(stropt, "abc");
stropt = opt_add_string(stropt, "def");
TEST_STREQ(stropt, "abc;def");
free(stropt);
stropt = NULL;
stropt = opt_add_prefix_string(stropt, "!", "abc");
TEST_STREQ(stropt, "!abc");
stropt = opt_add_prefix_string(stropt, "?", "def");
TEST_STREQ(stropt, "!abc;?def");
free(stropt);
stropt = NULL;
TEST_EQ(parse_color("1"), COLOR_ON);
TEST_EQ(parse_color("true"), COLOR_ON);
TEST_EQ(parse_color("off"), COLOR_OFF);
TEST_EQ(parse_color("n"), COLOR_OFF);
TEST_EQ(parse_color("auto"), COLOR_AUTO);
TEST_EQ(parse_color("ok"), COLOR_UNKNOWN);
TEST_EQ(parse_demangle("simple"), DEMANGLE_SIMPLE);
TEST_EQ(parse_demangle("no"), DEMANGLE_NONE);
TEST_EQ(parse_demangle("0"), DEMANGLE_NONE);
/* full demangling might not supported */
TEST_NE(parse_demangle("full"), DEMANGLE_SIMPLE);
for (i = 0; i < DBG_DOMAIN_MAX; i++)
dbg_domain[i] = 0;
parse_debug_domain("mcount:1,uftrace:2,symbol:3");
TEST_EQ(dbg_domain[DBG_UFTRACE], 2);
TEST_EQ(dbg_domain[DBG_MCOUNT], 1);
TEST_EQ(dbg_domain[DBG_SYMBOL], 3);
TEST_EQ(parse_timestamp("1ns", &elapsed_time), 1ULL);
TEST_EQ(parse_timestamp("2us", &elapsed_time), 2000ULL);
TEST_EQ(parse_timestamp("3ms", &elapsed_time), 3000000ULL);
TEST_EQ(parse_timestamp("4s", &elapsed_time), 4000000000ULL);
TEST_EQ(parse_timestamp("5m", &elapsed_time), 300000000000ULL);
return TEST_OK;
}
TEST_CASE(option_parsing2)
{
struct opts opts = {
.mode = UFTRACE_MODE_INVALID,
};
struct argp argp = {
.options = uftrace_options,
.parser = parse_option,
.args_doc = "argument description",
.doc = "uftrace option parsing test",
};
char *argv[] = {
"uftrace",
"replay",
"-v",
"--data=abc.data",
"--kernel",
"-t", "1us",
"-F", "foo",
"-N", "bar",
"-Abaz@kernel",
};
int argc = ARRAY_SIZE(argv);
int saved_debug = debug;
argp_parse(&argp, argc, argv, ARGP_IN_ORDER, NULL, &opts);
TEST_EQ(opts.mode, UFTRACE_MODE_REPLAY);
TEST_EQ(debug, saved_debug + 1);
TEST_EQ(opts.kernel, 1);
TEST_EQ(opts.threshold, (uint64_t)1000);
TEST_STREQ(opts.dirname, "abc.data");
TEST_STREQ(opts.filter, "foo;!bar");
TEST_STREQ(opts.args, "baz@kernel");
free_opts(&opts);
return TEST_OK;
}
TEST_CASE(option_parsing3)
{
struct opts opts = {
.mode = UFTRACE_MODE_INVALID,
};
struct argp argp = {
.options = uftrace_options,
.parser = parse_option,
.args_doc = "argument description",
.doc = "uftrace option parsing test",
};
char *argv[] = { "uftrace", "-v", "--opt-file", OPT_FILE, };
int argc = ARRAY_SIZE(argv);
char opt_file[] = "-K 2\n" "-b4m\n" "--column-view\n" "--depth=3\n" "t-abc";
int file_argc;
char **file_argv;
FILE *fp;
int saved_debug = debug;
/* create opt-file */
fp = fopen(OPT_FILE, "w");
TEST_NE(fp, NULL);
fwrite(opt_file, strlen(opt_file), 1, fp);
fclose(fp);
argp_parse(&argp, argc, argv, ARGP_IN_ORDER, NULL, &opts);
TEST_STREQ(opts.opt_file, OPT_FILE);
parse_opt_file(&file_argc, &file_argv, opts.opt_file, &opts);
TEST_EQ(file_argc, 6);
unlink(OPT_FILE);
TEST_EQ(opts.mode, UFTRACE_MODE_LIVE);
TEST_EQ(debug, saved_debug + 1);
TEST_EQ(opts.kernel, 1);
TEST_EQ(opts.kernel_depth, 2);
TEST_EQ(opts.depth, 3);
TEST_EQ(opts.bufsize, 4 * 1024 * 1024);
TEST_EQ(opts.column_view, 1);
TEST_STREQ(opts.exename, "t-abc");
free_parsed_cmdline(file_argv);
free_opts(&opts);
return TEST_OK;
}
TEST_CASE(option_parsing4)
{
struct opts opts = {
.mode = UFTRACE_MODE_INVALID,
};
struct argp argp = {
.options = uftrace_options,
.parser = parse_option,
.args_doc = "argument description",
.doc = "uftrace option parsing test",
};
char *argv[] = { "uftrace", "-v", "--opt-file", OPT_FILE, };
int argc = ARRAY_SIZE(argv);
char opt_file[] = "-K 2\n"
"# buffer size: 4 MB\n"
"-b4m\n"
"\n"
"## show different thread with different indentation\n"
"--column-view\n"
"\n"
"# limit maximum function call depth to 3\n"
"--depth=3 # same as -D3 \n"
"\n"
"\n"
"#test program\n"
"t-abc\n"
"\n";
int file_argc;
char **file_argv;
FILE *fp;
int saved_debug = debug;
/* create opt-file */
fp = fopen(OPT_FILE, "w");
TEST_NE(fp, NULL);
fwrite(opt_file, strlen(opt_file), 1, fp);
fclose(fp);
argp_parse(&argp, argc, argv, ARGP_IN_ORDER, NULL, &opts);
TEST_STREQ(opts.opt_file, OPT_FILE);
parse_opt_file(&file_argc, &file_argv, opts.opt_file, &opts);
TEST_EQ(file_argc, 6);
unlink(OPT_FILE);
TEST_EQ(opts.mode, UFTRACE_MODE_LIVE);
TEST_EQ(debug, saved_debug + 1);
TEST_EQ(opts.kernel, 1);
TEST_EQ(opts.kernel_depth, 2);
TEST_EQ(opts.depth, 3);
TEST_EQ(opts.bufsize, 4 * 1024 * 1024);
TEST_EQ(opts.column_view, 1);
TEST_STREQ(opts.exename, "t-abc");
free_parsed_cmdline(file_argv);
free_opts(&opts);
return TEST_OK;
}
TEST_CASE(option_parsing5)
{
struct opts opts = {
.mode = UFTRACE_MODE_INVALID,
};
struct argp argp = {
.options = uftrace_options,
.parser = parse_option,
.args_doc = "argument description",
.doc = "uftrace option parsing test",
};
char *argv[] = { "uftrace", "-v", "--opt-file", OPT_FILE, "hello" };
int argc = ARRAY_SIZE(argv);
char opt_file[] = "record\n" "-F main\n" "--time-filter 1us\n" "--depth=3\n" "t-abc";
int file_argc = argc;
char **file_argv = argv;
FILE *fp;
int saved_debug = debug;
/* create opt-file */
fp = fopen(OPT_FILE, "w");
TEST_NE(fp, NULL);
fwrite(opt_file, strlen(opt_file), 1, fp);
fclose(fp);
argp_parse(&argp, argc, argv, ARGP_IN_ORDER, NULL, &opts);
TEST_STREQ(opts.opt_file, OPT_FILE);
parse_opt_file(&file_argc, &file_argv, opts.opt_file, &opts);
unlink(OPT_FILE);
TEST_EQ(opts.mode, UFTRACE_MODE_RECORD);
TEST_EQ(debug, saved_debug + 1);
/* preserve original arg[cv] if command line is given */
TEST_EQ(file_argc, argc);
TEST_EQ(file_argv, (char **)argv);
TEST_EQ(opts.threshold, (uint64_t)1000);
TEST_EQ(opts.depth, 3);
TEST_EQ(opts.idx, 4);
TEST_STREQ(opts.filter, "main");
/* it should not update exename to "t-abc" */
TEST_STREQ(opts.exename, "hello");
free_opts(&opts);
return TEST_OK;
}
#endif /* UNIT_TEST */
|