From 8df0f582a3a589afd155137685b410a69267ff9c Mon Sep 17 00:00:00 2001 From: Yi Hong Date: Wed, 10 May 2023 22:08:21 +0800 Subject: [PATCH] record: skip whitespaces after shebang for scripts Python tracing won't work when the shebang line has a space like below: #! /usr/bin/env python3 This patch makes uftrace to understand the above shebang as well. Fixed: #1690 Signed-off-by: Yi Hong --- cmds/record.c | 2 +- utils/utils.c | 31 +++++++++++++++++++++++++++++++ utils/utils.h | 2 ++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/cmds/record.c b/cmds/record.c index 453a6c584..c80013994 100644 --- a/cmds/record.c +++ b/cmds/record.c @@ -1608,7 +1608,7 @@ static void check_binary(struct uftrace_opts *opts) pr_err("Cannot read '%s'", opts->exename); if (memcmp(elf_ident, ELFMAG, SELFMAG)) { - char *script = check_script_file(opts->exename); + char *script = str_ltrim(check_script_file(opts->exename)); char *p; if (script == NULL) diff --git a/utils/utils.c b/utils/utils.c index 592b3df40..cd49bb8da 100644 --- a/utils/utils.c +++ b/utils/utils.c @@ -594,6 +594,37 @@ char *strjoin(char *left, char *right, const char *delim) return new; } +/** + * str_ltrim - to trim left spaces + * @str: input string + * + * This function make @str to left trimmed @str + */ +char *str_ltrim(char *str) +{ + if (!str) + return NULL; + while (isspace((unsigned char)*str)) { + str++; + } + return str; +} + +/** + * str_rtrim - to trim right spaces + * @str: input string + * + * This function make @str to right trimmed @str + */ +char *str_rtrim(char *str) +{ + char *p = strchr(str, '\0'); + while (--p >= str && isspace(*p)) + ; + *(p + 1) = '\0'; + return str; +} + /** * strv_split - split given string and construct a string vector * @strv: string vector diff --git a/utils/utils.h b/utils/utils.h index fb0d8c4ed..393def811 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -389,6 +389,8 @@ void strv_append(struct strv *strv, const char *str); void strv_replace(struct strv *strv, int idx, const char *str); char *strv_join(struct strv *strv, const char *delim); void strv_free(struct strv *strv); +char *str_ltrim(char *str); +char *str_rtrim(char *str); char **parse_cmdline(char *cmd, int *argc); void free_parsed_cmdline(char **argv);