Skip to content

Commit

Permalink
record: skip whitespaces after shebang for scripts
Browse files Browse the repository at this point in the history
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: namhyung#1690

Signed-off-by: Gabriel Kim <[email protected]>
  • Loading branch information
GabrielKimm committed Aug 31, 2023
1 parent 5897da8 commit 00300fd
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
4 changes: 2 additions & 2 deletions cmds/record.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, ' ');
Expand Down Expand Up @@ -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)
Expand Down
49 changes: 49 additions & 0 deletions utils/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 00300fd

Please sign in to comment.