diff mbox series

selftests/ftrace: Use printf instead of echo in kprobe syntax error tests

Message ID 20200304161435.23019-1-seth.forshee@canonical.com (mailing list archive)
State New
Headers show
Series selftests/ftrace: Use printf instead of echo in kprobe syntax error tests | expand

Commit Message

Seth Forshee March 4, 2020, 4:14 p.m. UTC
Test cases which use echo to write strings containing backslashes
fail with some shells, as echo's treatment of backslashes in
strings varies between shell implementations. Use printf instead,
as it should behave consistently across different shells. This
requires adjustments to the strings to escape \ and % characters.
ftrace_errlog_check() must also re-escape these characters after
processing them to remove ^ characters.

Signed-off-by: Seth Forshee <seth.forshee@canonical.com>
---
 tools/testing/selftests/ftrace/test.d/functions  |  6 +++---
 .../ftrace/test.d/kprobe/kprobe_syntax_errors.tc | 16 ++++++++--------
 2 files changed, 11 insertions(+), 11 deletions(-)

Comments

Steven Rostedt March 4, 2020, 8:50 p.m. UTC | #1
On Wed,  4 Mar 2020 10:14:35 -0600
Seth Forshee <seth.forshee@canonical.com> wrote:

> --- a/tools/testing/selftests/ftrace/test.d/kprobe/kprobe_syntax_errors.tc
> +++ b/tools/testing/selftests/ftrace/test.d/kprobe/kprobe_syntax_errors.tc
> @@ -37,7 +37,7 @@ fi
>  
>  check_error 'p vfs_read ^$none_var'	# BAD_VAR
>  
> -check_error 'p vfs_read ^%none_reg'	# BAD_REG_NAME
> +check_error 'p vfs_read ^%%none_reg'	# BAD_REG_NAME
>  check_error 'p vfs_read ^@12345678abcde'	# BAD_MEM_ADDR
>  check_error 'p vfs_read ^@+10'		# FILE_ON_KPROBE
>  
> @@ -80,7 +80,7 @@ check_error 'p vfs_read arg1=^'			# NO_ARG_BODY
>  # instruction boundary check is valid on x86 (at this moment)
>  case $(uname -m) in
>    x86_64|i[3456]86)
> -    echo 'p vfs_read' > kprobe_events
> +    printf 'p vfs_read' > kprobe_events
>      if grep -q FTRACE ../kprobes/list ; then
>  	check_error 'p ^vfs_read+3'		# BAD_INSN_BNDRY (only if function-tracer is enabled)
>      fi
> @@ -89,13 +89,13 @@ esac
>  
>  # multiprobe errors
>  if grep -q "Create/append/" README && grep -q "imm-value" README; then
> -echo 'p:kprobes/testevent _do_fork' > kprobe_events
> +printf 'p:kprobes/testevent _do_fork' > kprobe_events
>  check_error '^r:kprobes/testevent do_exit'	# DIFF_PROBE_TYPE
> -echo 'p:kprobes/testevent _do_fork abcd=\1' > kprobe_events
> -check_error 'p:kprobes/testevent _do_fork ^bcd=\1'	# DIFF_ARG_TYPE
> -check_error 'p:kprobes/testevent _do_fork ^abcd=\1:u8'	# DIFF_ARG_TYPE
> -check_error 'p:kprobes/testevent _do_fork ^abcd=\"foo"'	# DIFF_ARG_TYPE
> -check_error '^p:kprobes/testevent _do_fork abcd=\1'	# SAME_PROBE
> +printf 'p:kprobes/testevent _do_fork abcd=\\1' > kprobe_events
> +check_error 'p:kprobes/testevent _do_fork ^bcd=\\1'	# DIFF_ARG_TYPE
> +check_error 'p:kprobes/testevent _do_fork ^abcd=\\1:u8'	# DIFF_ARG_TYPE
> +check_error 'p:kprobes/testevent _do_fork ^abcd=\\"foo"'# DIFF_ARG_TYPE
> +check_error '^p:kprobes/testevent _do_fork abcd=\\1'	# SAME_PROBE
>  fi
>  
>  exit 0


This change causes my tests to fail:

++ echo 'Test command: p vfs_read arg1="abcd'
Test command: p vfs_read arg1="abcd
++ echo
++ grep 'trace_kprobe: error:' -A 3 error_log
[61913.240093] trace_kprobe: error: Invalid fetch argument
  Command: p vfs_read arg1="abcd
                           ^
+++ tail -n 1 error_log
+++ wc -c
++ N=29
+++ expr 13 + 21
++ test 34 -eq 29


-- Steve
Seth Forshee March 4, 2020, 9:49 p.m. UTC | #2
On Wed, Mar 04, 2020 at 03:50:04PM -0500, Steven Rostedt wrote:
> On Wed,  4 Mar 2020 10:14:35 -0600
> Seth Forshee <seth.forshee@canonical.com> wrote:
> 
> > --- a/tools/testing/selftests/ftrace/test.d/kprobe/kprobe_syntax_errors.tc
> > +++ b/tools/testing/selftests/ftrace/test.d/kprobe/kprobe_syntax_errors.tc
> > @@ -37,7 +37,7 @@ fi
> >  
> >  check_error 'p vfs_read ^$none_var'	# BAD_VAR
> >  
> > -check_error 'p vfs_read ^%none_reg'	# BAD_REG_NAME
> > +check_error 'p vfs_read ^%%none_reg'	# BAD_REG_NAME
> >  check_error 'p vfs_read ^@12345678abcde'	# BAD_MEM_ADDR
> >  check_error 'p vfs_read ^@+10'		# FILE_ON_KPROBE
> >  
> > @@ -80,7 +80,7 @@ check_error 'p vfs_read arg1=^'			# NO_ARG_BODY
> >  # instruction boundary check is valid on x86 (at this moment)
> >  case $(uname -m) in
> >    x86_64|i[3456]86)
> > -    echo 'p vfs_read' > kprobe_events
> > +    printf 'p vfs_read' > kprobe_events
> >      if grep -q FTRACE ../kprobes/list ; then
> >  	check_error 'p ^vfs_read+3'		# BAD_INSN_BNDRY (only if function-tracer is enabled)
> >      fi
> > @@ -89,13 +89,13 @@ esac
> >  
> >  # multiprobe errors
> >  if grep -q "Create/append/" README && grep -q "imm-value" README; then
> > -echo 'p:kprobes/testevent _do_fork' > kprobe_events
> > +printf 'p:kprobes/testevent _do_fork' > kprobe_events
> >  check_error '^r:kprobes/testevent do_exit'	# DIFF_PROBE_TYPE
> > -echo 'p:kprobes/testevent _do_fork abcd=\1' > kprobe_events
> > -check_error 'p:kprobes/testevent _do_fork ^bcd=\1'	# DIFF_ARG_TYPE
> > -check_error 'p:kprobes/testevent _do_fork ^abcd=\1:u8'	# DIFF_ARG_TYPE
> > -check_error 'p:kprobes/testevent _do_fork ^abcd=\"foo"'	# DIFF_ARG_TYPE
> > -check_error '^p:kprobes/testevent _do_fork abcd=\1'	# SAME_PROBE
> > +printf 'p:kprobes/testevent _do_fork abcd=\\1' > kprobe_events
> > +check_error 'p:kprobes/testevent _do_fork ^bcd=\\1'	# DIFF_ARG_TYPE
> > +check_error 'p:kprobes/testevent _do_fork ^abcd=\\1:u8'	# DIFF_ARG_TYPE
> > +check_error 'p:kprobes/testevent _do_fork ^abcd=\\"foo"'# DIFF_ARG_TYPE
> > +check_error '^p:kprobes/testevent _do_fork abcd=\\1'	# SAME_PROBE
> >  fi
> >  
> >  exit 0
> 
> 
> This change causes my tests to fail:
> 
> ++ echo 'Test command: p vfs_read arg1="abcd'
> Test command: p vfs_read arg1="abcd
> ++ echo
> ++ grep 'trace_kprobe: error:' -A 3 error_log
> [61913.240093] trace_kprobe: error: Invalid fetch argument
>   Command: p vfs_read arg1="abcd
>                            ^
> +++ tail -n 1 error_log
> +++ wc -c
> ++ N=29
> +++ expr 13 + 21
> ++ test 34 -eq 29

Ah, I did miss a couple of backslashes that need to be escaped there.
The test passes for me without it though, so mabye printf behavior is
less consistent than I thought.

I'll send a v2, hopefully it will work better for you.

Seth
diff mbox series

Patch

diff --git a/tools/testing/selftests/ftrace/test.d/functions b/tools/testing/selftests/ftrace/test.d/functions
index 5d4550591ff9..b38c6eb029e8 100644
--- a/tools/testing/selftests/ftrace/test.d/functions
+++ b/tools/testing/selftests/ftrace/test.d/functions
@@ -114,11 +114,11 @@  yield() {
 }
 
 ftrace_errlog_check() { # err-prefix command-with-error-pos-by-^ command-file
-    pos=$(echo -n "${2%^*}" | wc -c) # error position
-    command=$(echo "$2" | tr -d ^)
+    pos=$(printf "${2%^*}" | wc -c) # error position
+    command=$(printf "$2" | sed -e 's/\^//g' -e 's/%/%%/g' -e 's/\\/\\\\/g')
     echo "Test command: $command"
     echo > error_log
-    (! echo "$command" >> "$3" ) 2> /dev/null
+    (! printf "$command" >> "$3" ) 2> /dev/null
     grep "$1: error:" -A 3 error_log
     N=$(tail -n 1 error_log | wc -c)
     # "  Command: " and "^\n" => 13
diff --git a/tools/testing/selftests/ftrace/test.d/kprobe/kprobe_syntax_errors.tc b/tools/testing/selftests/ftrace/test.d/kprobe/kprobe_syntax_errors.tc
index ef1e9bafb098..adef694eb740 100644
--- a/tools/testing/selftests/ftrace/test.d/kprobe/kprobe_syntax_errors.tc
+++ b/tools/testing/selftests/ftrace/test.d/kprobe/kprobe_syntax_errors.tc
@@ -37,7 +37,7 @@  fi
 
 check_error 'p vfs_read ^$none_var'	# BAD_VAR
 
-check_error 'p vfs_read ^%none_reg'	# BAD_REG_NAME
+check_error 'p vfs_read ^%%none_reg'	# BAD_REG_NAME
 check_error 'p vfs_read ^@12345678abcde'	# BAD_MEM_ADDR
 check_error 'p vfs_read ^@+10'		# FILE_ON_KPROBE
 
@@ -80,7 +80,7 @@  check_error 'p vfs_read arg1=^'			# NO_ARG_BODY
 # instruction boundary check is valid on x86 (at this moment)
 case $(uname -m) in
   x86_64|i[3456]86)
-    echo 'p vfs_read' > kprobe_events
+    printf 'p vfs_read' > kprobe_events
     if grep -q FTRACE ../kprobes/list ; then
 	check_error 'p ^vfs_read+3'		# BAD_INSN_BNDRY (only if function-tracer is enabled)
     fi
@@ -89,13 +89,13 @@  esac
 
 # multiprobe errors
 if grep -q "Create/append/" README && grep -q "imm-value" README; then
-echo 'p:kprobes/testevent _do_fork' > kprobe_events
+printf 'p:kprobes/testevent _do_fork' > kprobe_events
 check_error '^r:kprobes/testevent do_exit'	# DIFF_PROBE_TYPE
-echo 'p:kprobes/testevent _do_fork abcd=\1' > kprobe_events
-check_error 'p:kprobes/testevent _do_fork ^bcd=\1'	# DIFF_ARG_TYPE
-check_error 'p:kprobes/testevent _do_fork ^abcd=\1:u8'	# DIFF_ARG_TYPE
-check_error 'p:kprobes/testevent _do_fork ^abcd=\"foo"'	# DIFF_ARG_TYPE
-check_error '^p:kprobes/testevent _do_fork abcd=\1'	# SAME_PROBE
+printf 'p:kprobes/testevent _do_fork abcd=\\1' > kprobe_events
+check_error 'p:kprobes/testevent _do_fork ^bcd=\\1'	# DIFF_ARG_TYPE
+check_error 'p:kprobes/testevent _do_fork ^abcd=\\1:u8'	# DIFF_ARG_TYPE
+check_error 'p:kprobes/testevent _do_fork ^abcd=\\"foo"'# DIFF_ARG_TYPE
+check_error '^p:kprobes/testevent _do_fork abcd=\\1'	# SAME_PROBE
 fi
 
 exit 0