Skip to content

Commit 0e9aafa

Browse files
committed
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 <[email protected]>
1 parent 5897da8 commit 0e9aafa

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

cmds/record.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1625,7 +1625,7 @@ static void check_binary(struct uftrace_opts *opts)
16251625
if (!opts->force && !opts->patch)
16261626
pr_err_ns(SCRIPT_MSG, opts->exename);
16271627

1628-
script = str_ltrim(script);
1628+
script = str_trim(script);
16291629

16301630
/* ignore options */
16311631
p = strchr(script, ' ');
@@ -2188,7 +2188,7 @@ int do_child_exec(int ready, struct uftrace_opts *opts, int argc, char *argv[])
21882188
if (strstr(shebang, "python"))
21892189
is_python = true;
21902190
#endif
2191-
s = str_ltrim(shebang);
2191+
s = str_trim(shebang);
21922192

21932193
p = strchr(s, ' ');
21942194
if (p != NULL)

utils/utils.c

+49
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,22 @@ char *strjoin(char *left, char *right, const char *delim)
594594
return new;
595595
}
596596

597+
/**
598+
* str_trim - to trim all spaces
599+
* @str: input string
600+
*
601+
* This function make @str to all trimmed @str
602+
*/
603+
char *str_trim(char *str)
604+
{
605+
if (!str)
606+
return NULL;
607+
str = str_ltrim(str);
608+
str = str_rtrim(str);
609+
str = str_mtrim(str);
610+
return str;
611+
}
612+
597613
/**
598614
* str_ltrim - to trim left spaces
599615
* @str: input string
@@ -625,6 +641,39 @@ char *str_rtrim(char *str)
625641
return str;
626642
}
627643

644+
/**
645+
* str_mtrim - to trim middle spaces
646+
* @str: input string
647+
*
648+
* This function make @str to middle trimmed @str
649+
*/
650+
char *str_mtrim(char *str)
651+
{
652+
if (!str)
653+
return NULL;
654+
655+
char *read_ptr = str;
656+
char *write_ptr = str;
657+
int space_flag = 0;
658+
659+
while (*read_ptr != '\0') {
660+
if (isspace((unsigned char)*read_ptr)) {
661+
if (!space_flag) {
662+
*write_ptr++ = ' ';
663+
space_flag = 1;
664+
}
665+
}
666+
else {
667+
*write_ptr++ = *read_ptr;
668+
space_flag = 0;
669+
}
670+
read_ptr++;
671+
}
672+
673+
*write_ptr = '\0';
674+
return str;
675+
}
676+
628677
/**
629678
* strv_split - split given string and construct a string vector
630679
* @strv: string vector

utils/utils.h

+2
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,10 @@ void strv_append(struct strv *strv, const char *str);
389389
void strv_replace(struct strv *strv, int idx, const char *str);
390390
char *strv_join(struct strv *strv, const char *delim);
391391
void strv_free(struct strv *strv);
392+
char *str_trim(char *str);
392393
char *str_ltrim(char *str);
393394
char *str_rtrim(char *str);
395+
char *str_mtrim(char *str);
394396

395397
char **parse_cmdline(char *cmd, int *argc);
396398
void free_parsed_cmdline(char **argv);

0 commit comments

Comments
 (0)