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: #1690

Signed-off-by: Gabriel Kim <0xgabriel.kim@gmail.com>
  • Loading branch information
GabrielKimm committed Jul 30, 2023
1 parent b2ab34d commit 6aa6180
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 26 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
44 changes: 22 additions & 22 deletions utils/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
3 changes: 1 addition & 2 deletions utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 6aa6180

Please sign in to comment.