tracing/filters: Support filtering for char * strings
authorLi Zefan <lizf@cn.fujitsu.com>
Fri, 7 Aug 2009 02:33:43 +0000 (10:33 +0800)
committerSteven Rostedt <rostedt@goodmis.org>
Wed, 26 Aug 2009 04:32:07 +0000 (00:32 -0400)
Usually, char * entries are dangerous in traces because the string
can be released whereas a pointer to it can still wait to be read from
the ring buffer.

But sometimes we can assume it's safe, like in case of RO data
(eg: __file__ or __line__, used in bkl trace event). If these RO data
are in a module and so is the call to the trace event, then it's safe,
because the ring buffer will be flushed once this module get unloaded.

To allow char * to be treated as a string:

TRACE_EVENT(...,

TP_STRUCT__entry(
__field_ext(const char *, name, FILTER_PTR_STRING)
...
)

...
);

The filtering will not dereference "char *" unless the developer
explicitly sets FILTER_PTR_STR in __field_ext.

Signed-off-by: Li Zefan <lizf@cn.fujitsu.com>
LKML-Reference: <4A7B9287.90205@cn.fujitsu.com>
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
include/linux/ftrace_event.h
kernel/trace/trace_events_filter.c

index 0440bea8f6bb678e620faa922ee43afa598d35ec..ace2da9e0a0d08d64ca1cd5ecc046452fd8f60db 100644 (file)
@@ -144,6 +144,7 @@ enum {
        FILTER_OTHER = 0,
        FILTER_STATIC_STRING,
        FILTER_DYN_STRING,
+       FILTER_PTR_STRING,
 };
 
 extern int trace_define_field(struct ftrace_event_call *call,
index 8a8e576733fc175b15511646b081d37f4785758d..9f03082c81d8db3643ff86f70b385076c00560c6 100644 (file)
@@ -163,6 +163,20 @@ static int filter_pred_string(struct filter_pred *pred, void *event,
        return match;
 }
 
+/* Filter predicate for char * pointers */
+static int filter_pred_pchar(struct filter_pred *pred, void *event,
+                            int val1, int val2)
+{
+       char **addr = (char **)(event + pred->offset);
+       int cmp, match;
+
+       cmp = strncmp(*addr, pred->str_val, pred->str_len);
+
+       match = (!cmp) ^ pred->not;
+
+       return match;
+}
+
 /*
  * Filter predicate for dynamic sized arrays of characters.
  * These are implemented through a list of strings at the end
@@ -489,7 +503,8 @@ int filter_assign_type(const char *type)
 static bool is_string_field(struct ftrace_event_field *field)
 {
        return field->filter_type == FILTER_DYN_STRING ||
-              field->filter_type == FILTER_STATIC_STRING;
+              field->filter_type == FILTER_STATIC_STRING ||
+              field->filter_type == FILTER_PTR_STRING;
 }
 
 static int is_legal_op(struct ftrace_event_field *field, int op)
@@ -579,11 +594,16 @@ static int filter_add_pred(struct filter_parse_state *ps,
        }
 
        if (is_string_field(field)) {
+               pred->str_len = field->size;
+
                if (field->filter_type == FILTER_STATIC_STRING)
                        fn = filter_pred_string;
-               else
+               else if (field->filter_type == FILTER_DYN_STRING)
                        fn = filter_pred_strloc;
-               pred->str_len = field->size;
+               else {
+                       fn = filter_pred_pchar;
+                       pred->str_len = strlen(pred->str_val);
+               }
        } else {
                if (field->is_signed)
                        ret = strict_strtoll(pred->str_val, 0, &val);