From 00300fd78c93f54672e1929d576a680ec91ebf66 Mon Sep 17 00:00:00 2001 From: Gabriel Kim <0xgabriel.kim@gmail.com> Date: Thu, 31 Aug 2023 19:18:50 +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 | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ utils/utils.h | 2 ++ 3 files changed, 53 insertions(+), 2 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..0d41c8056 100644 --- a/utils/utils.c +++ b/utils/utils.c @@ -594,6 +594,22 @@ char *strjoin(char *left, char *right, const char *delim) return new; } +/** + * str_trim - to trim all spaces + * @str: input string + * + * This function make @str to all trimmed @str + */ +char *str_trim(char *str) +{ + if (!str) + return NULL; + str = str_ltrim(str); + str = str_rtrim(str); + str = str_mtrim(str); + return str; +} + /** * str_ltrim - to trim left spaces * @str: input string @@ -625,6 +641,39 @@ char *str_rtrim(char *str) return str; } +/** + * str_mtrim - to trim middle spaces + * @str: input string + * + * This function make @str to middle trimmed @str + */ +char *str_mtrim(char *str) +{ + if (!str) + return NULL; + + char *read_ptr = str; + char *write_ptr = str; + int space_flag = 0; + + while (*read_ptr != '\0') { + if (isspace((unsigned char)*read_ptr)) { + if (!space_flag) { + *write_ptr++ = ' '; + space_flag = 1; + } + } + else { + *write_ptr++ = *read_ptr; + space_flag = 0; + } + read_ptr++; + } + + *write_ptr = '\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 393def811..5f4be8024 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -389,8 +389,10 @@ 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_trim(char *str); char *str_ltrim(char *str); char *str_rtrim(char *str); +char *str_mtrim(char *str); char **parse_cmdline(char *cmd, int *argc); void free_parsed_cmdline(char **argv);