From 6aa6180bd716ed48c56526aae1102f24e2726c63 Mon Sep 17 00:00:00 2001 From: Gabriel Kim <0xgabriel.kim@gmail.com> Date: Sun, 30 Jul 2023 18:08:40 +0900 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: Gabriel Kim <0xgabriel.kim@gmail.com> --- cmds/record.c | 4 ++-- utils/utils.c | 44 ++++++++++++++++++++++---------------------- utils/utils.h | 3 +-- 3 files changed, 25 insertions(+), 26 deletions(-) diff --git a/cmds/record.c b/cmds/record.c index ec40ca179..26537df6a 100644 --- a/cmds/record.c +++ b/cmds/record.c @@ -1625,7 +1625,7 @@ static void check_binary(struct uftrace_opts *opts) if (!opts->force && !opts->patch) pr_err_ns(SCRIPT_MSG, opts->exename); - script = str_ltrim(script); + script = str_trim(script); /* ignore options */ p = strchr(script, ' '); @@ -2188,7 +2188,7 @@ int do_child_exec(int ready, struct uftrace_opts *opts, int argc, char *argv[]) if (strstr(shebang, "python")) is_python = true; #endif - s = str_ltrim(shebang); + s = str_trim(shebang); p = strchr(s, ' '); if (p != NULL) diff --git a/utils/utils.c b/utils/utils.c index cd49bb8da..ca12b6f56 100644 --- a/utils/utils.c +++ b/utils/utils.c @@ -595,33 +595,33 @@ char *strjoin(char *left, char *right, const char *delim) } /** - * 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_trim - to trim all spaces * @str: input string * * This function make @str to right trimmed @str */ -char *str_rtrim(char *str) +char *str_trim(char *str) { - char *p = strchr(str, '\0'); - while (--p >= str && isspace(*p)) - ; - *(p + 1) = '\0'; + int i = 0; + int j = 0; + bool spaceFound = false; + + if (!str) + return NULL; + + while (str[j]) { + if (str[j] != ' ' || !spaceFound) { + str[i] = str[j]; + i++; + spaceFound = (str[j] == ' '); + } + j++; + } + + if (i > 0 && str[i-1] == ' ') + i--; + + str[i] = '\0'; return str; } diff --git a/utils/utils.h b/utils/utils.h index 393def811..9dc03f8fc 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -389,8 +389,7 @@ 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 *str_trim(char *str); char **parse_cmdline(char *cmd, int *argc); void free_parsed_cmdline(char **argv);