tracing/core: bring back raw trace_printk for dynamic formats strings
Subsystems (2)
PATCH 1/1 tracing/core: bring back raw trace_printk for dynamic formats strings
Impact: fix callsites with dynamic format strings
Since its new binary implementation, trace_printk() internally uses static containers for the format strings on each callsites.
But the value is assigned once at build time, which means that it can't take dynamic formats.
So this patch unearthes the raw trace_printk implementation for the callers that will need trace_printk to be able to carry these dynamic format strings. The trace_printk() macro will use the appropriate implementation for each callsite.
Most of the time however, the binary implementation will still be used.
The other impact of this patch is that mmiotrace_printk() will use the old implementation because it calls the low level trace_vprintk and we can't guess here whether the format passed in it is dynamic or not.
Some parts of this patch have been written by Steven Rostedt (most notably the part that chooses the appropriate implementation for each callsites).
--- include/linux/kernel.h | 40 +++++++++++----- kernel/trace/trace.c | 85 ++++++++++++++++++++++++++++++++-- kernel/trace/trace.h | 13 +++++- kernel/trace/trace_event_types.h | 11 ++++- kernel/trace/trace_functions_graph.c | 6 +- kernel/trace/trace_mmiotrace.c | 7 +-- kernel/trace/trace_output.c | 57 ++++++++++++++++++++-- kernel/trace/trace_printk.c | 33 +++++++++++-- 8 files changed, 213 insertions(+), 39 deletions(-)
include/linux/kernel.h +27 -13
@@ -452,32 +452,46 @@ do { \ #define trace_printk(fmt, args...) \ do { \ - static const char *trace_printk_fmt \ - __attribute__((section("__trace_printk_fmt"))); \ - \ - if (!trace_printk_fmt) \ - trace_printk_fmt = fmt; \ - \ __trace_printk_check_format(fmt, ##args); \ - __trace_printk(_THIS_IP_, trace_printk_fmt, ##args); \ + if (__builtin_constant_p(fmt)) { \ + static const char *trace_printk_fmt \ + __attribute__((section("__trace_printk_fmt"))) = \ + __builtin_constant_p(fmt) ? fmt : NULL; \ + \ + __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \ + } else \ + __trace_printk(_THIS_IP_, fmt, ##args); \ } while (0)extern int +__trace_bprintk (unsigned long ip,const char *fmt, ...) + __attribute__ ((format (printf,2 ,3 ))); + +extern int __trace_printk (unsigned long ip,const char *fmt, ...) __attribute__ ((format (printf,2 ,3 ))); +/* +* The double __builtin_constant_p is because gcc will give us an error +* if we try to allocate the static variable to fmt if it is not a +* constant. Even with the outer if statement. +*/ #define ftrace_vprintk (fmt, vargs) \ do { \ - static const char *trace_printk_fmt \ - __attribute__((section("__trace_printk_fmt"))); \ - \ - if (!trace_printk_fmt) \ - trace_printk_fmt = fmt; \ + if (__builtin_constant_p(fmt)) { \ + static const char *trace_printk_fmt \ + __attribute__((section("__trace_printk_fmt"))) = \ + __builtin_constant_p(fmt) ? fmt : NULL; \ \ - __ftrace_vprintk(_THIS_IP_, trace_printk_fmt, vargs); \ + __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \ + } else \ + __ftrace_vprintk(_THIS_IP_, fmt, vargs); \ } while (0)extern int +__ftrace_vbprintk (unsigned long ip,const char *fmt,va_list ap); + +extern int __ftrace_vprintk (unsigned long ip,const char *fmt,va_list ap);extern void ftrace_dump (void );
kernel/trace/trace.c +80 -5
129 lines not on this page · raw message
kernel/trace/trace.h +12 -1
@@ -20,6 +20,7 @@ enum trace_type {TRACE_WAKE ,TRACE_STACK ,TRACE_PRINT , +TRACE_BPRINT ,TRACE_SPECIAL ,TRACE_MMIO_RW , TRACE_MMIO_MAP,
@@ -117,7 +118,7 @@ struct userstack_entry {/* * trace_printk entry: */ -struct print_entry { +struct bprint_entry {struct trace_entry ent;unsigned long ip;int depth;
@@ -125,6 +126,13 @@ struct print_entry {u32 buf[]; }; +struct print_entry { +struct trace_entry ent; +unsigned long ip; +int depth; +char buf[]; +}; +#define TRACE_OLD_SIZE 88struct trace_field_cont {
@@ -286,6 +294,7 @@ extern void __ftrace_bad_type(void); IF_ASSIGN(var, ent, struct stack_entry, TRACE_STACK); \ IF_ASSIGN(var, ent, struct userstack_entry, TRACE_USER_STACK);\ IF_ASSIGN(var, ent, struct print_entry, TRACE_PRINT); \ + IF_ASSIGN(var, ent, struct bprint_entry, TRACE_BPRINT); \ IF_ASSIGN(var, ent, struct special_entry, 0); \ IF_ASSIGN(var, ent, struct trace_mmiotrace_rw, \TRACE_MMIO_RW ); \
@@ -570,6 +579,8 @@ extern int trace_selftest_startup_branch(struct tracer *trace,extern void *head_page (struct trace_array_cpu *data);extern long ns2usecs (cycle_t nsec);extern int +trace_vbprintk (unsigned long ip,int depth,const char *fmt,va_list args); +extern int trace_vprintk (unsigned long ip,int depth,const char *fmt,va_list args);extern unsigned long trace_flags;
kernel/trace/trace_event_types.h +10 -1
@@ -102,7 +102,7 @@ TRACE_EVENT_FORMAT(user_stack, TRACE_USER_STACK, userstack_entry, ignore, "\t=> (%08lx)\n\t=> (%08lx)\n\t=> (%08lx)\n\t=> (%08lx)\n") ); -TRACE_EVENT_FORMAT(print, TRACE_PRINT, print_entry, ignore, +TRACE_EVENT_FORMAT(bprint, TRACE_PRINT, bprint_entry, ignore, TRACE_STRUCT( TRACE_FIELD(unsigned long, ip, ip) TRACE_FIELD(unsigned int, depth, depth)
@@ -112,6 +112,15 @@ TRACE_EVENT_FORMAT(print, TRACE_PRINT, print_entry, ignore,TP_RAW_FMT ("%08lx (%d) fmt:%p %s" ) ); +TRACE_EVENT_FORMAT (print,TRACE_PRINT , print_entry, ignore, +TRACE_STRUCT ( + TRACE_FIELD(unsigned long, ip, ip) + TRACE_FIELD(unsigned int, depth, depth) +TRACE_FIELD_ZERO_CHAR (buf) + ), +TP_RAW_FMT ("%08lx (%d) fmt:%p %s" ) +); + TRACE_EVENT_FORMAT(branch, TRACE_BRANCH, trace_branch, ignore, TRACE_STRUCT( TRACE_FIELD(unsigned int, line, line)
kernel/trace/trace_functions_graph.c +3 -3
@@ -684,7 +684,7 @@ print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s, }static enum print_line_t -print_graph_comment (struct print_entry *trace,struct trace_seq *s, +print_graph_comment (struct bprint_entry *trace,struct trace_seq *s,struct trace_entry *ent,struct trace_iterator *iter) {int i;
@@ -781,8 +781,8 @@ print_graph_function(struct trace_iterator *iter)trace_assign_type (field, entry);return print_graph_return (&field->ret, s, entry, iter); } -case TRACE_PRINT : { -struct print_entry *field; +case TRACE_BPRINT : { +struct bprint_entry *field;trace_assign_type (field, entry);return print_graph_comment (field, s, entry, iter); }
kernel/trace/trace_mmiotrace.c +2 -5
@@ -254,6 +254,7 @@ static enum print_line_t mmio_print_mark(struct trace_iterator *iter) {struct trace_entry *entry = iter->ent;struct print_entry *print = (struct print_entry *)entry; +const char *msg = print->buf;struct trace_seq *s = &iter->seq;unsigned long long t =ns2usecs (iter->ts);unsigned long usec_rem =do_div (t,USEC_PER_SEC );
@@ -261,11 +262,7 @@ static enum print_line_t mmio_print_mark(struct trace_iterator *iter)int ret;/* The trailing newline must be in the message. */ - ret =trace_seq_printf (s,"MARK %u.%06lu " , secs, usec_rem); -if (!ret) -return TRACE_TYPE_PARTIAL_LINE ; - - ret =trace_seq_bprintf (s, print->fmt, print->buf); + ret =trace_seq_printf (s,"MARK %u.%06lu %s" , secs, usec_rem, msg);if (!ret)return TRACE_TYPE_PARTIAL_LINE ;
kernel/trace/trace_output.c +51 -6
91 lines not on this page · raw message
kernel/trace/trace_printk.c +28 -5
@@ -99,7 +99,7 @@ struct notifier_block module_trace_bprintk_format_nb = { .notifier_call = module_trace_bprintk_format_notify, }; -int __trace_printk (unsigned long ip,const char *fmt, ...) +int __trace_bprintk (unsigned long ip,const char *fmt, ...) {int ret;va_list ap;
@@ -111,13 +111,13 @@ int __trace_printk(unsigned long ip, const char *fmt, ...)return 0 ;va_start (ap, fmt); - ret =trace_vprintk (ip,task_curr_ret_stack (current), fmt, ap); + ret =trace_vbprintk (ip,task_curr_ret_stack (current), fmt, ap);va_end (ap);return ret; } -EXPORT_SYMBOL_GPL (__trace_printk); +EXPORT_SYMBOL_GPL (__trace_bprintk); -int __ftrace_vprintk (unsigned long ip,const char *fmt,va_list ap) +int __ftrace_vbprintk (unsigned long ip,const char *fmt,va_list ap) {if (unlikely (!fmt))return 0 ;
@@ -125,11 +125,34 @@ int __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap)if (!(trace_flags &TRACE_ITER_PRINTK ))return 0 ; +return trace_vbprintk (ip,task_curr_ret_stack (current), fmt, ap); +} +EXPORT_SYMBOL_GPL (__ftrace_vbprintk); + +int __trace_printk (unsigned long ip,const char *fmt, ...) +{ +int ret; +va_list ap; + +if (!(trace_flags &TRACE_ITER_PRINTK )) +return 0 ; + +va_start (ap, fmt); + ret =trace_vprintk (ip,task_curr_ret_stack (current), fmt, ap); +va_end (ap); +return ret; +} +EXPORT_SYMBOL_GPL (__trace_printk); + +int __ftrace_vprintk (unsigned long ip,const char *fmt,va_list ap) +{ +if (!(trace_flags &TRACE_ITER_PRINTK )) +return 0 ; +return trace_vprintk (ip,task_curr_ret_stack (current), fmt, ap); }EXPORT_SYMBOL_GPL (__ftrace_vprintk); -static __init int init_trace_printk (void ) {return register_module_notifier (&module_trace_bprintk_format_nb);
Signature · 1 line
1.6.1
Steven Rostedt
maintainerQuoted 13 lines · Frederic Weisbecker
Impact: fix callsites with dynamic format strings
Since its new binary implementation,
trace_printk()internally uses static containers for the format strings on each callsites.
But the value is assigned once at build time, which means that it can't take dynamic formats.So this patch unearthes the raw
trace_printkimplementation for the callers that will needtrace_printkto be able to carry these dynamic format strings. Thetrace_printk()macro will use the appropriate implementation for each callsite.
Most of the time however, the binary implementation will still be used.
Quoted 8 lines
The other impact of this patch is that
mmiotrace_printk()will use the old implementation because it calls the low leveltrace_vprintkand we can't guess here whether the format passed in it is dynamic or not.Some parts of this patch have been written by Steven Rostedt (most notably the part that chooses the appropriate implementation for each callsites).
Signed-off-by Frederic Weisbecker <[email protected]>
Thanks Frederic!
Ingo, I'm going to pull this in my tree, since I have some patches dependent on it.
-- Steve