Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  * perf.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Performance analysis utility.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * This is the main hub from which the sub-commands (perf stat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * perf top, perf record, perf report, etc.) are started.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include "builtin.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include "perf.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include "util/build-id.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include "util/cache.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include "util/env.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <internal/lib.h> // page_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <subcmd/exec-cmd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include "util/config.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <subcmd/run-command.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include "util/parse-events.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <subcmd/parse-options.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include "util/bpf-loader.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include "util/debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include "util/event.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include "util/util.h" // usage()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include "ui/ui.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include "perf-sys.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <api/fs/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <api/fs/tracing_path.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <perf/core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <pthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include <time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #include <linux/zalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) const char perf_usage_string[] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	"perf [--version] [--help] [OPTIONS] COMMAND [ARGS]";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) const char perf_more_info_string[] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	"See 'perf help COMMAND' for more information on a specific command.";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) static int use_pager = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) const char *input_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) struct cmd_struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	const char *cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	int (*fn)(int, const char **);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	int option;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) static struct cmd_struct commands[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	{ "buildid-cache", cmd_buildid_cache, 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	{ "buildid-list", cmd_buildid_list, 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	{ "config",	cmd_config,	0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	{ "c2c",	cmd_c2c,	0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	{ "diff",	cmd_diff,	0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	{ "evlist",	cmd_evlist,	0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	{ "help",	cmd_help,	0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	{ "kallsyms",	cmd_kallsyms,	0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	{ "list",	cmd_list,	0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	{ "record",	cmd_record,	0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	{ "report",	cmd_report,	0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	{ "bench",	cmd_bench,	0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	{ "stat",	cmd_stat,	0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	{ "timechart",	cmd_timechart,	0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	{ "top",	cmd_top,	0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	{ "annotate",	cmd_annotate,	0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	{ "version",	cmd_version,	0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	{ "script",	cmd_script,	0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	{ "sched",	cmd_sched,	0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) #ifdef HAVE_LIBELF_SUPPORT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	{ "probe",	cmd_probe,	0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	{ "kmem",	cmd_kmem,	0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	{ "lock",	cmd_lock,	0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	{ "kvm",	cmd_kvm,	0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	{ "test",	cmd_test,	0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) #if defined(HAVE_LIBAUDIT_SUPPORT) || defined(HAVE_SYSCALL_TABLE_SUPPORT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	{ "trace",	cmd_trace,	0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	{ "inject",	cmd_inject,	0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	{ "mem",	cmd_mem,	0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	{ "data",	cmd_data,	0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	{ "ftrace",	cmd_ftrace,	0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) struct pager_config {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	const char *cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) static int pager_command_config(const char *var, const char *value, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	struct pager_config *c = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	if (strstarts(var, "pager.") && !strcmp(var + 6, c->cmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		c->val = perf_config_bool(var, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static int check_pager_config(const char *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	struct pager_config c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	c.cmd = cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	c.val = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	err = perf_config(pager_command_config, &c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	return err ?: c.val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static int browser_command_config(const char *var, const char *value, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	struct pager_config *c = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	if (strstarts(var, "tui.") && !strcmp(var + 4, c->cmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		c->val = perf_config_bool(var, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	if (strstarts(var, "gtk.") && !strcmp(var + 4, c->cmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		c->val = perf_config_bool(var, value) ? 2 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)  * returns 0 for "no tui", 1 for "use tui", 2 for "use gtk",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)  * and -1 for "not specified"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static int check_browser_config(const char *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	struct pager_config c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	c.cmd = cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	c.val = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	err = perf_config(browser_command_config, &c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	return err ?: c.val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static void commit_pager_choice(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	switch (use_pager) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		setenv(PERF_PAGER_ENVIRONMENT, "cat", 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		/* setup_pager(); */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct option options[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	OPT_ARGUMENT("help", "help"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	OPT_ARGUMENT("version", "version"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	OPT_ARGUMENT("exec-path", "exec-path"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	OPT_ARGUMENT("html-path", "html-path"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	OPT_ARGUMENT("paginate", "paginate"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	OPT_ARGUMENT("no-pager", "no-pager"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	OPT_ARGUMENT("debugfs-dir", "debugfs-dir"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	OPT_ARGUMENT("buildid-dir", "buildid-dir"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	OPT_ARGUMENT("list-cmds", "list-cmds"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	OPT_ARGUMENT("list-opts", "list-opts"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	OPT_ARGUMENT("debug", "debug"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	OPT_END()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) static int handle_options(const char ***argv, int *argc, int *envchanged)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	int handled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	while (*argc > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		const char *cmd = (*argv)[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		if (cmd[0] != '-')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		 * For legacy reasons, the "version" and "help"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		 * commands can be written with "--" prepended
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		 * to make them look like flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		 * Shortcut for '-h' and '-v' options to invoke help
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		 * and version command.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		if (!strcmp(cmd, "-h")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 			(*argv)[0] = "--help";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		if (!strcmp(cmd, "-v")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 			(*argv)[0] = "--version";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		if (!strcmp(cmd, "-vv")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			(*argv)[0] = "version";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 			version_verbose = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		 * Check remaining flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		if (strstarts(cmd, CMD_EXEC_PATH)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 			cmd += strlen(CMD_EXEC_PATH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 			if (*cmd == '=')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 				set_argv_exec_path(cmd + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 				puts(get_argv_exec_path());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 				exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		} else if (!strcmp(cmd, "--html-path")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			puts(system_path(PERF_HTML_PATH));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 			exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		} else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 			use_pager = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		} else if (!strcmp(cmd, "--no-pager")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 			use_pager = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 			if (envchanged)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 				*envchanged = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		} else if (!strcmp(cmd, "--debugfs-dir")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 			if (*argc < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 				fprintf(stderr, "No directory given for --debugfs-dir.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 				usage(perf_usage_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 			tracing_path_set((*argv)[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 			if (envchanged)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 				*envchanged = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 			(*argv)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 			(*argc)--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		} else if (!strcmp(cmd, "--buildid-dir")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			if (*argc < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 				fprintf(stderr, "No directory given for --buildid-dir.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 				usage(perf_usage_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 			set_buildid_dir((*argv)[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 			if (envchanged)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 				*envchanged = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 			(*argv)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			(*argc)--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		} else if (strstarts(cmd, CMD_DEBUGFS_DIR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 			tracing_path_set(cmd + strlen(CMD_DEBUGFS_DIR));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			fprintf(stderr, "dir: %s\n", tracing_path_mount());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 			if (envchanged)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 				*envchanged = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		} else if (!strcmp(cmd, "--list-cmds")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 			unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 			for (i = 0; i < ARRAY_SIZE(commands); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 				struct cmd_struct *p = commands+i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 				printf("%s ", p->cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 			putchar('\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 			exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		} else if (!strcmp(cmd, "--list-opts")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			for (i = 0; i < ARRAY_SIZE(options)-1; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 				struct option *p = options+i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 				printf("--%s ", p->long_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 			putchar('\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 			exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		} else if (!strcmp(cmd, "--debug")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 			if (*argc < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 				fprintf(stderr, "No variable specified for --debug.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 				usage(perf_usage_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 			if (perf_debug_option((*argv)[1]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 				usage(perf_usage_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 			(*argv)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 			(*argc)--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 			fprintf(stderr, "Unknown option: %s\n", cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 			usage(perf_usage_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		(*argv)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		(*argc)--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		handled++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	return handled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) #define RUN_SETUP	(1<<0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) #define USE_PAGER	(1<<1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	struct stat st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	char sbuf[STRERR_BUFSIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	if (use_browser == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		use_browser = check_browser_config(p->cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	if (use_pager == -1 && p->option & RUN_SETUP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		use_pager = check_pager_config(p->cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	if (use_pager == -1 && p->option & USE_PAGER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		use_pager = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	commit_pager_choice();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	perf_env__init(&perf_env);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	perf_env__set_cmdline(&perf_env, argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	status = p->fn(argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	perf_config__exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	exit_browser(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	perf_env__exit(&perf_env);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	bpf__clear();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		return status & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	/* Somebody closed stdout? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	if (fstat(fileno(stdout), &st))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	/* Ignore write errors for pipes and sockets.. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	status = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	/* Check for ENOSPC and EIO errors.. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	if (fflush(stdout)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		fprintf(stderr, "write failure on standard output: %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 			str_error_r(errno, sbuf, sizeof(sbuf)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	if (ferror(stdout)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		fprintf(stderr, "unknown write failure on standard output");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	if (fclose(stdout)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		fprintf(stderr, "close failed on standard output: %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 			str_error_r(errno, sbuf, sizeof(sbuf)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) static void handle_internal_command(int argc, const char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	const char *cmd = argv[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	/* Turn "perf cmd --help" into "perf help cmd" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	if (argc > 1 && !strcmp(argv[1], "--help")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		argv[1] = argv[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		argv[0] = cmd = "help";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	for (i = 0; i < ARRAY_SIZE(commands); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		struct cmd_struct *p = commands+i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		if (strcmp(p->cmd, cmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		exit(run_builtin(p, argc, argv));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) static void execv_dashed_external(const char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	char *cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	const char *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	if (asprintf(&cmd, "perf-%s", argv[0]) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		goto do_die;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	 * argv[0] must be the perf command, but the argv array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	 * belongs to the caller, and may be reused in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	 * subsequent loop iterations. Save argv[0] and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	 * restore it on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	tmp = argv[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	argv[0] = cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	 * if we fail because the command is not found, it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	 * OK to return. Otherwise, we just pass along the status code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	status = run_command_v_opt(argv, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	if (status != -ERR_RUN_COMMAND_EXEC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		if (IS_RUN_COMMAND_ERR(status)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) do_die:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 			pr_err("FATAL: unable to run '%s'", argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 			status = -128;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		exit(-status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	errno = ENOENT; /* as if we called execvp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	argv[0] = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	zfree(&cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) static int run_argv(int *argcp, const char ***argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	/* See if it's an internal command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	handle_internal_command(*argcp, *argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	/* .. then try the external ones */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	execv_dashed_external(*argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) static void pthread__block_sigwinch(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	sigset_t set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	sigemptyset(&set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	sigaddset(&set, SIGWINCH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	pthread_sigmask(SIG_BLOCK, &set, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) void pthread__unblock_sigwinch(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	sigset_t set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	sigemptyset(&set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	sigaddset(&set, SIGWINCH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	pthread_sigmask(SIG_UNBLOCK, &set, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) static int libperf_print(enum libperf_print_level level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 			 const char *fmt, va_list ap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	return eprintf(level, verbose, fmt, ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) int main(int argc, const char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	const char *cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	char sbuf[STRERR_BUFSIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	/* libsubcmd init */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	exec_cmd_init("perf", PREFIX, PERF_EXEC_PATH, EXEC_PATH_ENVIRONMENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	pager_init(PERF_PAGER_ENVIRONMENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	libperf_init(libperf_print);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	cmd = extract_argv0_path(argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	if (!cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		cmd = "perf-help";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	srandom(time(NULL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	/* Setting $PERF_CONFIG makes perf read _only_ the given config file. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	config_exclusive_filename = getenv("PERF_CONFIG");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	err = perf_config(perf_default_config, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	set_buildid_dir(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	 *  - cannot take flags in between the "perf" and the "xxxx".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	 *  - cannot execute it externally (since it would just do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	 *    the same thing over again)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	 * So we just directly call the internal command handler. If that one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	 * fails to handle this, then maybe we just run a renamed perf binary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	 * that contains a dash in its name. To handle this scenario, we just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	 * fall through and ignore the "xxxx" part of the command string.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	if (strstarts(cmd, "perf-")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		cmd += 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		argv[0] = cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		handle_internal_command(argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		 * If the command is handled, the above function does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		 * return undo changes and fall through in such a case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		cmd -= 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		argv[0] = cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	if (strstarts(cmd, "trace")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) #if defined(HAVE_LIBAUDIT_SUPPORT) || defined(HAVE_SYSCALL_TABLE_SUPPORT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		setup_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		argv[0] = "trace";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 		return cmd_trace(argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 		fprintf(stderr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 			"trace command not available: missing audit-libs devel package at build time.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	/* Look for flags.. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	argv++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	argc--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	handle_options(&argv, &argc, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	commit_pager_choice();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	if (argc > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		if (strstarts(argv[0], "--"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 			argv[0] += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		/* The user didn't specify a command; give them help */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		printf("\n usage: %s\n\n", perf_usage_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		list_common_cmds_help();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 		printf("\n %s\n\n", perf_more_info_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	cmd = argv[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	test_attr__init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	 * We use PATH to find perf commands, but we prepend some higher
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	 * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	 * environment, and the $(perfexecdir) from the Makefile at build
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	 * time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	setup_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	 * Block SIGWINCH notifications so that the thread that wants it can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	 * unblock and get syscalls like select interrupted instead of waiting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	 * forever while the signal goes to some other non interested thread.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	pthread__block_sigwinch();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	perf_debug_setup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 		static int done_help;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 		run_argv(&argc, &argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 		if (errno != ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 		if (!done_help) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 			cmd = argv[0] = help_unknown_cmd(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 			done_help = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	fprintf(stderr, "Failed to run command '%s': %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 		cmd, str_error_r(errno, sbuf, sizeof(sbuf)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) }