tracing: Use common error_log with probe events
Masami Hiramatsu
maintainerv2 cover tracing: Use common error_log with probe events
Hi,
Here is the 2nd version of using common error_log with probe events. Previous version is here.
http://lkml.kernel.org/r/155248005229.10815.334731901778152247.stgit@devnote2
In this version, I've updated some error messages according to Steve's comment, adjust some error position, and update testcase to simplify a bit.
- [4/7]: Update error message according to Steve's comment (Thanks!)
- [6/7]: Update error message, adjust error positions, and add uprobe errors
- [7/7]: Specify error position in command string by "^".
Clear error_log right before writing command.
Add uprobe syntax error checker
Thank you,
---
Masami Hiramatsu (7):
tracing/probe: Check maxactive error cases tracing/probe: Check event name length correctly tracing/probe: Check the size of argument name and body tracing/probe: Check event/group naming rule at parsing tracing/probe: Verify alloc_trace_*probe() result
tracing: Use tracing error_log with probe events selftests/ftrace: Add error_log testcase for probe errors
kernel/trace/trace_kprobe.c | 90 ++++-- kernel/trace/trace_probe.c | 282 +++++++++++++++----- kernel/trace/trace_probe.h | 78 +++++- kernel/trace/trace_uprobe.c | 52 ++-- .../ftrace/test.d/kprobe/kprobe_syntax_errors.tc | 93 +++++++ .../ftrace/test.d/kprobe/uprobe_syntax_errors.tc | 31 ++ 6 files changed, 494 insertions(+), 132 deletions(-) create mode 100644 tools/testing/selftests/ftrace/test.d/kprobe/kprobe_syntax_errors.tc create mode 100644 tools/testing/selftests/ftrace/test.d/kprobe/uprobe_syntax_errors.tc
--
Masami Hiramatsu <[email protected]>
Masami Hiramatsu
maintainerRFC PATCH v2 1/7 tracing/probe: Check maxactive error cases
Check maxactive on kprobe error case, because maxactive is only for kretprobe, not for kprobe. Also, maxactive should not be 0, it should be at least 1.
--- kernel/trace/trace_kprobe.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
kernel/trace/trace_kprobe.c +7 -3
@@ -624,7 +624,11 @@ static int trace_kprobe_create(int argc, const char *argv[])if (event) event++; -if (is_return &&isdigit (argv[0 ][1 ])) { +if (isdigit (argv[0 ][1 ])) { +if (!is_return) { +pr_info ("Maxactive is not for kprobe" ); +return -EINVAL ; + }if (event) len = event - &argv[0 ][1 ] -1 ;else
@@ -634,8 +638,8 @@ static int trace_kprobe_create(int argc, const char *argv[])memcpy (buf, &argv[0 ][1 ], len); buf[len] ='\0' ; ret =kstrtouint (buf,0 , &maxactive); -if (ret) { -pr_info ("Failed to parse maxactive.\n" ); +if (ret || !maxactive) { +pr_info ("Invalid maxactive number\n" );return ret; }/* kretprobes instances are iterated over via a list. The
Masami Hiramatsu
maintainerRFC PATCH v2 2/7 tracing/probe: Check event name length correctly
In reply to Masami HiramatsuEnsure given name of event is not too long when parsing it, and fix to update event name offset correctly when the group name is given. For example, this makes probe event to check the "p:foo/" error case correctly.
--- kernel/trace/trace_probe.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
kernel/trace/trace_probe.c +7 -1
@@ -159,6 +159,7 @@ int traceprobe_parse_event_name(const char **pevent, const char **pgroup, char *buf) {const char *slash, *event = *pevent; +int len; slash =strchr (event,'/' );if (slash) {
@@ -173,10 +174,15 @@ int traceprobe_parse_event_name(const char **pevent, const char **pgroup,strlcpy (buf, event, slash - event +1 ); *pgroup = buf; *pevent = slash +1 ; + event = *pevent; } -if (strlen (event) ==0 ) { + len =strlen (event); +if (len ==0 ) {pr_info ("Event name is not specified\n" );return -EINVAL ; + }else if (len >MAX_EVENT_NAME_LEN ) { +pr_info ("Event name is too long\n" ); +return -E2BIG ; }return 0 ; }
Masami Hiramatsu
maintainerRFC PATCH v2 3/7 tracing/probe: Check the size of argument name and body
In reply to Masami HiramatsuCheck the size of argument name and expression is not 0 and smaller than maximum length.
--- kernel/trace/trace_probe.c | 2 ++ kernel/trace/trace_probe.h | 1 + 2 files changed, 3 insertions(+)
kernel/trace/trace_probe.c +2 -0
@@ -554,6 +554,8 @@ int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, char *arg, body =strchr (arg,'=' );if (body) { +if (body - arg >MAX_ARG_NAME_LEN || body == arg) +return -EINVAL ; parg->name =kmemdup_nul (arg, body - arg,GFP_KERNEL ); body++; }else {
kernel/trace/trace_probe.h +1 -0
@@ -32,6 +32,7 @@#define MAX_TRACE_ARGS 128#define MAX_ARGSTR_LEN 63#define MAX_ARRAY_LEN 64 +#define MAX_ARG_NAME_LEN 32#define MAX_STRING_SIZE PATH_MAX/* Reserved field names */
Masami Hiramatsu
maintainerRFC PATCH v2 4/7 tracing/probe: Check event/group naming rule at parsing
In reply to Masami HiramatsuCheck event and group naming rule at parsing it instead of allocating probes.
---
Changes in v2:
- Update error message according to Steve's comment.
--- kernel/trace/trace_kprobe.c | 7 +------ kernel/trace/trace_probe.c | 8 ++++++++ kernel/trace/trace_uprobe.c | 5 +---- 3 files changed, 10 insertions(+), 10 deletions(-)
kernel/trace/trace_kprobe.c +1 -6
@@ -221,7 +221,7 @@ static struct trace_kprobe *alloc_trace_kprobe(const char *group, tk->rp.maxactive = maxactive; -if (!event || !is_good_name (event)) { +if (!event || !group) { ret = -EINVAL ; goto error; }
@@ -231,11 +231,6 @@ static struct trace_kprobe *alloc_trace_kprobe(const char *group,if (!tk->tp.call.name) goto error; -if (!group || !is_good_name (group)) { - ret = -EINVAL ; - goto error; - } - tk->tp.class.system =kstrdup (group,GFP_KERNEL );if (!tk->tp.class.system) goto error;
kernel/trace/trace_probe.c +8 -0
@@ -172,6 +172,10 @@ int traceprobe_parse_event_name(const char **pevent, const char **pgroup,return -E2BIG ; }strlcpy (buf, event, slash - event +1 ); +if (!is_good_name (buf)) { +pr_info ("Group name must follow the same rules as C identifiers\n" ); +return -EINVAL ; + } *pgroup = buf; *pevent = slash +1 ; event = *pevent;
@@ -184,6 +188,10 @@ int traceprobe_parse_event_name(const char **pevent, const char **pgroup,pr_info ("Event name is too long\n" );return -E2BIG ; } +if (!is_good_name (event)) { +pr_info ("Event name must follow the same rules as C identifiers\n" ); +return -EINVAL ; + }return 0 ; }
kernel/trace/trace_uprobe.c +1 -4
@@ -266,10 +266,7 @@ alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret) {struct trace_uprobe *tu; -if (!event || !is_good_name (event)) -return ERR_PTR (-EINVAL ); - -if (!group || !is_good_name (group)) +if (!event || !group)return ERR_PTR (-EINVAL ); tu =kzalloc (SIZEOF_TRACE_UPROBE (nargs),GFP_KERNEL );
Masami Hiramatsu
maintainerRFC PATCH v2 5/7 tracing/probe: Verify alloc_trace_*probe() result
In reply to Masami HiramatsuSince alloc_trace_*probe() returns -EINVAL only if !event && !group, it should not happen in trace_*probe_create(). If we catch that case there is a bug. So use WARN_ON_ONCE() instead of pr_info().
--- kernel/trace/trace_kprobe.c | 4 ++-- kernel/trace/trace_uprobe.c | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-)
kernel/trace/trace_kprobe.c +2 -2
@@ -693,9 +693,9 @@ static int trace_kprobe_create(int argc, const char *argv[]) tk =alloc_trace_kprobe (group, event, addr, symbol, offset, maxactive, argc, is_return);if (IS_ERR (tk)) { -pr_info ("Failed to allocate trace_probe.(%d)\n" , - (int )PTR_ERR (tk)); ret =PTR_ERR (tk); +/* This must return -ENOMEM otherwise there is a bug */ +WARN_ON_ONCE (ret != -ENOMEM ); goto out; }
kernel/trace/trace_uprobe.c +2 -1
@@ -514,8 +514,9 @@ static int trace_uprobe_create(int argc, const char **argv) tu =alloc_trace_uprobe (group, event, argc, is_return);if (IS_ERR (tu)) { -pr_info ("Failed to allocate trace_uprobe.(%d)\n" , (int )PTR_ERR (tu)); ret =PTR_ERR (tu); +/* This must return -ENOMEM otherwise there is a bug */ +WARN_ON_ONCE (ret != -ENOMEM ); goto fail_address_parse; } tu->offset = offset;
Masami Hiramatsu
maintainerRFC PATCH v2 6/7 tracing: Use tracing error_log with probe events
In reply to Masami HiramatsuUse tracing error_log with probe events for logging error more precisely. This also makes all parse error returns -EINVAL (except for -ENOMEM), because user can see better error message in error_log file now.
---
Changes in v2:
- Update error message according to Steve's comment
- Add uprobe reference counter errors
- Adjust some error positions
--- kernel/trace/trace_kprobe.c | 77 +++++++----- kernel/trace/trace_probe.c | 274 +++++++++++++++++++++++++++++++------------ kernel/trace/trace_probe.h | 77 ++++++++++++ kernel/trace/trace_uprobe.c | 44 +++++-- 4 files changed, 348 insertions(+), 124 deletions(-)
kernel/trace/trace_kprobe.c +46 -31
@@ -441,13 +441,8 @@ static int __register_trace_kprobe(struct trace_kprobe *tk)else ret =register_kprobe (&tk->rp.kp); -if (ret ==0 ) { +if (ret ==0 ) tk->tp.flags |=TP_FLAG_REGISTERED ; - }else if (ret == -EILSEQ ) { -pr_warn ("Probing address(0x%p) is not an instruction boundary.\n" , - tk->rp.kp.addr); - ret = -EINVAL ; - }return ret; }
@@ -591,7 +586,7 @@ static int trace_kprobe_create(int argc, const char *argv[])* Type of args: * FETCHARG:TYPE : use TYPE instead of unsigned long. */ -struct trace_kprobe *tk; +struct trace_kprobe *tk =NULL ;int i, len, ret =0 ;bool is_return = false;char *symbol =NULL , *tmp =NULL ;
@@ -615,44 +610,50 @@ static int trace_kprobe_create(int argc, const char *argv[])if (argc <2 )return -ECANCELED ; +trace_probe_log_init ("trace_kprobe" , argc, argv); + event =strchr (&argv[0 ][1 ],':' );if (event) event++;if (isdigit (argv[0 ][1 ])) {if (!is_return) { -pr_info ("Maxactive is not for kprobe" ); -return -EINVAL ; +trace_probe_log_err (1 ,MAXACT_NO_KPROBE ); + goto parse_error; }if (event) len = event - &argv[0 ][1 ] -1 ;else len =strlen (&argv[0 ][1 ]); -if (len >MAX_EVENT_NAME_LEN -1 ) -return -E2BIG ; +if (len >MAX_EVENT_NAME_LEN -1 ) { +trace_probe_log_err (1 ,BAD_MAXACT ); + goto parse_error; + }memcpy (buf, &argv[0 ][1 ], len); buf[len] ='\0' ; ret =kstrtouint (buf,0 , &maxactive);if (ret || !maxactive) { -pr_info ("Invalid maxactive number\n" ); -return ret; +trace_probe_log_err (1 ,BAD_MAXACT ); + goto parse_error; }/* kretprobes instances are iterated over via a list. The * maximum should stay reasonable. */ if (maxactive >KRETPROBE_MAXACTIVE_MAX ) { -pr_info ("Maxactive is too big (%d > %d).\n" , - maxactive,KRETPROBE_MAXACTIVE_MAX ); -return -E2BIG ; +trace_probe_log_err (1 ,MAXACT_TOO_BIG ); + goto parse_error; } }/* try to parse an address. if that fails, try to read the * input as a symbol. */ if (kstrtoul (argv[1 ],0 , (unsigned long *)&addr)) { +trace_probe_log_set_index (1 );/* Check whether uprobe event specified */ -if (strchr (argv[1 ],'/' ) &&strchr (argv[1 ],':' )) -return -ECANCELED ; +if (strchr (argv[1 ],'/' ) &&strchr (argv[1 ],':' )) { + ret = -ECANCELED ; + goto error; + }/* a symbol specified */ symbol =kstrdup (argv[1 ],GFP_KERNEL );if (!symbol)
@@ -660,23 +661,23 @@ static int trace_kprobe_create(int argc, const char *argv[])/* TODO: support .init module functions */ ret =traceprobe_split_symbol_offset (symbol, &offset);if (ret || offset <0 || offset >UINT_MAX ) { -pr_info ("Failed to parse either an address or a symbol.\n" ); - goto out; +trace_probe_log_err (0 ,BAD_PROBE_ADDR ); + goto parse_error; }if (kprobe_on_func_entry (NULL , symbol, offset)) flags |=TPARG_FL_FENTRY ;if (offset && is_return && !(flags &TPARG_FL_FENTRY )) { -pr_info ("Given offset is not valid for return probe.\n" ); - ret = -EINVAL ; - goto out; +trace_probe_log_err (0 ,BAD_RETPROBE ); + goto parse_error; } } - argc -=2 ; argv +=2 ; +trace_probe_log_set_index (0 );if (event) { - ret =traceprobe_parse_event_name (&event, &group, buf); + ret =traceprobe_parse_event_name (&event, &group, buf, + event - argv[0 ]);if (ret) - goto out; + goto parse_error; }else {/* Make a new event name */ if (symbol)
@@ -691,13 +692,14 @@ static int trace_kprobe_create(int argc, const char *argv[])/* setup a probe */ tk =alloc_trace_kprobe (group, event, addr, symbol, offset, maxactive, - argc, is_return); + argc -2 , is_return);if (IS_ERR (tk)) { ret =PTR_ERR (tk); -/* This must return -ENOMEM otherwise there is a bug */ +/* This must return -ENOMEM, else there is a bug */ WARN_ON_ONCE (ret != -ENOMEM ); - goto out; + goto out;/* We know tk is not allocated */ } + argc -=2 ; argv +=2 ;/* parse arguments */ for (i =0 ; i < argc && i <MAX_TRACE_ARGS ; i++) {
@@ -707,19 +709,32 @@ static int trace_kprobe_create(int argc, const char *argv[]) goto error; } +trace_probe_log_set_index (i +2 ); ret =traceprobe_parse_probe_arg (&tk->tp, i, tmp, flags);kfree (tmp);if (ret) - goto error; + goto error;/* This can be -ENOMEM */ } ret =register_trace_kprobe (tk); -if (ret) +if (ret) { +trace_probe_log_set_index (1 ); +if (ret == -EILSEQ ) +trace_probe_log_err (0 ,BAD_INSN_BNDRY ); +else if (ret == -ENOENT ) +trace_probe_log_err (0 ,BAD_PROBE_ADDR ); +else if (ret != -ENOMEM ) +trace_probe_log_err (0 ,FAIL_REG_PROBE ); goto error; + } + out: +trace_probe_log_clear ();kfree (symbol);return ret; +parse_error: + ret = -EINVAL ; error:free_trace_kprobe (tk); goto out;
kernel/trace/trace_probe.c +196 -78
522 lines not on this page · raw message
kernel/trace/trace_probe.h +75 -2
86 lines not on this page · raw message
kernel/trace/trace_uprobe.c +31 -13
107 lines not on this page · raw message
Masami Hiramatsu
maintainerRFC PATCH v2 7/7 selftests/ftrace: Add error_log testcase for probe errors
In reply to Masami HiramatsuAdd error_log testcase for error logs on probe events. This tests most of error cases and checks the error position is correct.
---
Changes in v2:
- Specify error position in command string by "^"
- Clear error_log right before writing command
- Add uprobe syntax error testcase
--- .../ftrace/test.d/kprobe/kprobe_syntax_errors.tc | 93 ++++++++++++++++++++ .../ftrace/test.d/kprobe/uprobe_syntax_errors.tc | 31 +++++++ 2 files changed, 124 insertions(+) create mode 100644 tools/testing/selftests/ftrace/test.d/kprobe/kprobe_syntax_errors.tc create mode 100644 tools/testing/selftests/ftrace/test.d/kprobe/uprobe_syntax_errors.tc
tools/testing/selftests/ftrace/test.d/kprobe/kprobe_syntax_errors.tc new file +93 -0
@@ -0,0 +1,93 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# description: Kprobe event parser error log check + +[ -f kprobe_events ] || exit_unsupported # this is configurable + +[ -f error_log ] || exit_unsupported + +check_error() { # command-with-error-pos-by-^ +pos=$(echo -n "${1%^*}" | wc -c) # error position +command=$(echo "$1" | tr -d ^) +echo "Test command: $command" +echo > error_log +(! echo "$command" > kprobe_events ) >& /dev/null +grep "trace_kprobe: error:" -A 3 error_log +N=$(tail -n 1 error_log | wc -c) +# " Command: " and "^\n" => 13 +test $(expr 13 + $pos) -eq $N +} + +if grep -q 'r\[maxactive\]' README; then +check_error 'p^100 vfs_read' # MAXACT_NO_KPROBE +check_error 'r^1a111 vfs_read' # BAD_MAXACT +check_error 'r^100000 vfs_read' # MAXACT_TOO_BIG +fi + +check_error 'p ^non_exist_func' # BAD_PROBE_ADDR (enoent) +check_error 'p ^hoge-fuga' # BAD_PROBE_ADDR (bad syntax) +check_error 'p ^hoge+1000-1000' # BAD_PROBE_ADDR (bad syntax) +check_error 'r ^vfs_read+10' # BAD_RETPROBE +check_error 'p:^/bar vfs_read' # NO_GROUP_NAME +check_error 'p:^12345678901234567890123456789012345678901234567890123456789012345/bar vfs_read' # GROUP_TOO_LONG + +check_error 'p:^foo.1/bar vfs_read' # BAD_GROUP_NAME +check_error 'p:foo/^ vfs_read' # NO_EVENT_NAME +check_error 'p:foo/^12345678901234567890123456789012345678901234567890123456789012345 vfs_read' # EVENT_TOO_LONG +check_error 'p:foo/^bar.1 vfs_read' # BAD_EVENT_NAME + +check_error 'p vfs_read ^$retval' # RETVAL_ON_PROBE +check_error 'p vfs_read ^$stack10000' # BAD_STACK_NUM + +if grep -q '$arg<N>' README; then +check_error 'p vfs_read ^$arg10000' # BAD_ARG_NUM +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 ^@12345678abcde' # BAD_MEM_ADDR +check_error 'p vfs_read ^@+10' # FILE_ON_KPROBE + +check_error 'p vfs_read ^+0@0)' # DEREF_NEED_BRACE +check_error 'p vfs_read ^+0ab1(@0)' # BAD_DEREF_OFFS +check_error 'p vfs_read +0(+0(@0^)' # DEREF_OPEN_BRACE + +if grep -A1 "fetcharg:" README | grep -q '\$comm' ; then +check_error 'p vfs_read +0(^$comm)' # COMM_CANT_DEREF +fi + +check_error 'p vfs_read ^&1' # BAD_FETCH_ARG + + +# We've introduced this limitation with array support +if grep -q ' <type>\\\[<array-size>\\\]' README; then +check_error 'p vfs_read +0(^+0(+0(+0(+0(+0(+0(+0(+0(+0(+0(+0(+0(+0(@0))))))))))))))' # TOO_MANY_OPS? +check_error 'p vfs_read +0(@11):u8[10^' # ARRAY_NO_CLOSE +check_error 'p vfs_read +0(@11):u8[10]^a' # BAD_ARRAY_SUFFIX +check_error 'p vfs_read +0(@11):u8[^10a]' # BAD_ARRAY_NUM +check_error 'p vfs_read +0(@11):u8[^256]' # ARRAY_TOO_BIG +fi + +check_error 'p vfs_read @11:^unknown_type' # BAD_TYPE +check_error 'p vfs_read $stack0:^string' # BAD_STRING +check_error 'p vfs_read @11:^b10@a/16' # BAD_BITFIELD + +check_error 'p vfs_read ^arg123456789012345678901234567890=@11' # ARG_NAME_TOO_LOG +check_error 'p vfs_read ^=@11' # NO_ARG_NAME +check_error 'p vfs_read ^var.1=@11' # BAD_ARG_NAME +check_error 'p vfs_read var1=@11 ^var1=@12' # USED_ARG_NAME +check_error 'p vfs_read ^+1234567(+1234567(+1234567(+1234567(+1234567(+1234567(@1234))))))' # ARG_TOO_LONG +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 + if grep -q FTRACE ../kprobes/list ; then + check_error 'p ^vfs_read+3' # BAD_INSN_BNDRY (only if function-tracer is enabled) + fi + ;; +esac + +exit 0
tools/testing/selftests/ftrace/test.d/kprobe/uprobe_syntax_errors.tc new file +31 -0
@@ -0,0 +1,31 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# description: Uprobe event parser error log check + +[ -f uprobe_events ] || exit_unsupported # this is configurable + +[ -f error_log ] || exit_unsupported + +check_error() { # command-with-error-pos-by-^ +pos=$(echo -n "${1%^*}" | wc -c) # error position +command=$(echo "$1" | tr -d ^) +echo "Test command: $command" +echo > error_log +(! echo "$command" > uprobe_events ) >& /dev/null +grep "trace_uprobe: error:" -A 3 error_log +N=$(tail -n 1 error_log | wc -c) +# " Command: " and "^\n" => 13 +test $(expr 13 + $pos) -eq $N +} + +check_error 'p ^/non_exist_file:100' # FILE_NOT_FOUND +check_error 'p ^/sys:100' # NO_REGULAR_FILE +check_error 'p /bin/sh:^10a' # BAD_UPROBE_OFFS +check_error 'p /bin/sh:10(^1a)' # BAD_REFCNT +check_error 'p /bin/sh:10(10^' # REFCNT_OPEN_BRACE +check_error 'p /bin/sh:10(10)^a' # BAD_REFCNT_SUFFIX + +check_error 'p /bin/sh:10 ^@+ab' # BAD_FILE_OFFS +check_error 'p /bin/sh:10 ^@symbol' # SYM_ON_UPROBE + +exit 0