1 // SPDX-License-Identifier: GPL-2.0
2 #include "../../builtin.h"
3 #include "../../perf.h"
4 #include "../../util/util.h" // perf_exe()
6 #include "../../util/hist.h"
7 #include "../../util/debug.h"
8 #include "../../util/symbol.h"
9 #include "../browser.h"
10 #include "../libslang.h"
12 #include <linux/string.h>
13 #include <linux/zalloc.h>
16 #define SCRIPT_NAMELEN 128
17 #define SCRIPT_MAX_NO 64
19 * Usually the full path for a script is:
20 * /home/username/libexec/perf-core/scripts/python/xxx.py
21 * /home/username/libexec/perf-core/scripts/perl/xxx.pl
22 * So 256 should be long enough to contain the full path.
24 #define SCRIPT_FULLPATH_LEN 256
26 struct script_config
{
31 char extra_format
[256];
34 void attr_to_script(char *extra_format
, struct perf_event_attr
*attr
)
37 if (attr
->read_format
& PERF_FORMAT_GROUP
)
38 strcat(extra_format
, " -F +metric");
39 if (attr
->sample_type
& PERF_SAMPLE_BRANCH_STACK
)
40 strcat(extra_format
, " -F +brstackinsn --xed");
41 if (attr
->sample_type
& PERF_SAMPLE_REGS_INTR
)
42 strcat(extra_format
, " -F +iregs");
43 if (attr
->sample_type
& PERF_SAMPLE_REGS_USER
)
44 strcat(extra_format
, " -F +uregs");
45 if (attr
->sample_type
& PERF_SAMPLE_PHYS_ADDR
)
46 strcat(extra_format
, " -F +phys_addr");
49 static int add_script_option(const char *name
, const char *opt
,
50 struct script_config
*c
)
52 c
->names
[c
->index
] = name
;
53 if (asprintf(&c
->paths
[c
->index
],
54 "%s script %s -F +metric %s %s",
55 c
->perf
, opt
, symbol_conf
.inline_name
? " --inline" : "",
62 static int scripts_config(const char *var
, const char *value
, void *data
)
64 struct script_config
*c
= data
;
66 if (!strstarts(var
, "scripts."))
68 if (c
->index
>= SCRIPT_MAX_NO
)
70 c
->names
[c
->index
] = strdup(var
+ 7);
71 if (!c
->names
[c
->index
])
73 if (asprintf(&c
->paths
[c
->index
], "%s %s", value
,
81 * When success, will copy the full path of the selected script
82 * into the buffer pointed by script_name, and return 0.
83 * Return -1 on failure.
85 static int list_scripts(char *script_name
, bool *custom
,
88 char *buf
, *paths
[SCRIPT_MAX_NO
], *names
[SCRIPT_MAX_NO
];
91 int max_std
, custom_perf
;
93 const char *perf
= perf_exe(pbuf
, sizeof pbuf
);
94 struct script_config scriptc
= {
95 .names
= (const char **)names
,
102 /* Preset the script name to SCRIPT_NAMELEN */
103 buf
= malloc(SCRIPT_MAX_NO
* (SCRIPT_NAMELEN
+ SCRIPT_FULLPATH_LEN
));
108 attr_to_script(scriptc
.extra_format
, &evsel
->core
.attr
);
109 add_script_option("Show individual samples", "", &scriptc
);
110 add_script_option("Show individual samples with assembler", "-F +insn --xed",
112 add_script_option("Show individual samples with source", "-F +srcline,+srccode",
114 perf_config(scripts_config
, &scriptc
);
115 custom_perf
= scriptc
.index
;
116 add_script_option("Show samples with custom perf script arguments", "", &scriptc
);
120 for (; i
< SCRIPT_MAX_NO
; i
++) {
121 names
[i
] = buf
+ (i
- max_std
) * (SCRIPT_NAMELEN
+ SCRIPT_FULLPATH_LEN
);
122 paths
[i
] = names
[i
] + SCRIPT_NAMELEN
;
125 num
= find_scripts(names
+ max_std
, paths
+ max_std
, SCRIPT_MAX_NO
- max_std
,
126 SCRIPT_FULLPATH_LEN
);
129 choice
= ui__popup_menu(num
+ max_std
, (char * const *)names
, NULL
);
134 if (choice
== custom_perf
) {
135 char script_args
[50];
136 int key
= ui_browser__input_window("perf script command",
137 "Enter perf script command line (without perf script prefix)",
139 if (key
!= K_ENTER
) {
143 sprintf(script_name
, "%s script %s", perf
, script_args
);
144 } else if (choice
< num
+ max_std
) {
145 strcpy(script_name
, paths
[choice
]);
147 *custom
= choice
>= max_std
;
151 for (i
= 0; i
< max_std
; i
++)
156 void run_script(char *cmd
)
158 pr_debug("Running %s\n", cmd
);
161 pr_warning("Cannot run %s\n", cmd
);
163 * SLang doesn't seem to reset the whole terminal, so be more
164 * forceful to get back to the original state.
166 printf("\033[c\033[H\033[J");
168 SLang_init_tty(0, 0, 0);
172 int script_browse(const char *script_opt
, struct evsel
*evsel
)
174 char *cmd
, script_name
[SCRIPT_FULLPATH_LEN
];
177 memset(script_name
, 0, SCRIPT_FULLPATH_LEN
);
178 if (list_scripts(script_name
, &custom
, evsel
))
181 if (asprintf(&cmd
, "%s%s %s %s%s 2>&1 | less",
182 custom
? "perf script -s " : "",
184 script_opt
? script_opt
: "",
185 input_name
? "-i " : "",
186 input_name
? input_name
: "") < 0)