Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python: improve python trace user experience #1891

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions cmds/record.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "utils/kernel.h"
#include "utils/list.h"
#include "utils/perf.h"
#include "utils/script.h"
#include "utils/shmem.h"
#include "utils/symbol.h"
#include "utils/utils.h"
Expand Down Expand Up @@ -1590,13 +1591,17 @@ static void check_binary(struct uftrace_opts *opts)
size_t i;
char elf_ident[EI_NIDENT];
static char altname[PATH_MAX]; // for opts->exename to be persistent
bool is_python_exe;
uint16_t e_type;
uint16_t e_machine;
uint16_t supported_machines[] = { EM_X86_64, EM_ARM, EM_AARCH64, EM_386, EM_RISCV };

again:

if (get_script_type(opts->exename) == SCRIPT_PYTHON)
is_python_exe = true;
/* if it cannot be found in PATH, then fails inside */
if (!is_regular_executable(opts->exename)) {
if (!is_python_exe && !is_regular_executable(opts->exename)) {
find_in_path(opts->exename, altname, sizeof(altname));
opts->exename = altname;
}
Expand All @@ -1614,11 +1619,11 @@ static void check_binary(struct uftrace_opts *opts)
char *script = altname;
char *p;

if (!check_script_file(opts->exename, altname, sizeof(altname)))
if (!is_python_exe && !check_script_file(opts->exename, altname, sizeof(altname)))
pr_err_ns(UFTRACE_ELF_MSG, opts->exename);

#if defined(HAVE_LIBPYTHON2) || defined(HAVE_LIBPYTHON3)
if (strstr(script, "python")) {
if (is_python_exe || strstr(script, "python")) {
opts->force = true;
/* TODO: disable sched event until it can merge subsequent events */
opts->no_sched = true;
Expand All @@ -1635,7 +1640,8 @@ static void check_binary(struct uftrace_opts *opts)
if (p)
*p = '\0';

opts->exename = script;
if (is_python_exe)
opts->exename = "/usr/bin/env";
close(fd);
goto again;
}
Expand Down Expand Up @@ -2163,8 +2169,10 @@ static int do_child_exec(int ready[], struct uftrace_opts *opts, int argc, char
opts->dirname = dirpath;

if (access(argv[0], F_OK) == 0) {
if (get_script_type(argv[0]) == SCRIPT_PYTHON)
shebang = "/usr/bin/env python3";
/* prefer current directory over PATH */
if (check_script_file(argv[0], exepath, sizeof(exepath)))
else if (check_script_file(argv[0], exepath, sizeof(exepath)))
shebang = exepath;
}
else {
Expand All @@ -2176,7 +2184,9 @@ static int do_child_exec(int ready[], struct uftrace_opts *opts, int argc, char
strv_for_each(&path_names, dir, i) {
xasprintf(&path, "%s/%s", dir, argv[0]);
ret = access(path, F_OK);
if (ret == 0 && check_script_file(path, exepath, sizeof(exepath)))
if (ret == 0 && get_script_type(path) == SCRIPT_PYTHON)
shebang = "/usr/bin/env python3";
else if (ret == 0 && check_script_file(path, exepath, sizeof(exepath)))
shebang = exepath;
free(path);
if (ret == 0)
Expand All @@ -2193,7 +2203,7 @@ static int do_child_exec(int ready[], struct uftrace_opts *opts, int argc, char
if (strstr(shebang, "python"))
is_python = true;
#endif
s = str_ltrim(shebang);
s = strdup(str_ltrim(shebang));

p = strchr(s, ' ');
if (p != NULL)
Expand Down