Binary ftrace_printk
Subsystems (8)
v3 cover Binary ftrace_printk
Changelog V3:
Rebase the whole against latest tip/master
Changelog V2:
This new iteration addresses Steven's reviews.
Notably:
- only build the ftrace_printk format section if CONFIG_TRACING is set
- be scheduler tracing safe (don't use preempt_enable directly from ftrace_printk to avoid tracing recursion)
- fix a loss of format string when a module is unloaded. Since we can loose it on the ring-buffer if it is in overwrite mode, we don't keep track of the format given by the modules to free them. We just copy their ftrace_printk string format forever. Note that it is safe against duplicate strings since we verify if the string is already present in our list before allocating a new one.
Changelog V1:
An new optimization is making its way to ftrace. Its purpose is to make ftrace_printk() consuming less memory and become faster.
Written by Lai Jiangshan, the approach is to delay the formatting job from tracing time to output time.
Currently, a call to ftrace_printk will format the whole string and insert it into the ring buffer. Then you can read it on /debug/tracing/trace file.
The new implementation stores the address of the format string and the binary parameters into the ring buffer, making the packet more compact and faster to insert.
Later, when the user exports the traces, the format string is retrieved with the binary parameters and the formatting job is eventually done.
Here is the result of a small comparative benchmark while putting the following ftrace_printk on the timer interrupt. ftrace_printk is the old implementation, ftrace_bprintk is a the new one:
ftrace_printk("This is the timer interrupt: %llu", jiffies_64);
After some time running on low load (no X, no really active processes):
ftrace_printk: duration average: 2044 ns, avg of bytes stored per entry: 39 ftrace_bprintk: duration average: 1426 ns, avg of bytes stored per entry: 16
Higher load (started X and launched a cat running on a X console looping on traces printing):
ftrace_printk: duration average: 8812 ns ftrace_bprintk: duration average: 2611 ns
Which means the new implementation can be 70 % faster on higher load. And it consumes lesser memory on the ring buffer.
The three first patches rebase against latest -tip the ftrace_bprintk work done by Lai few monthes ago.
The two others integrate ftrace_bprintk as a replacement for the old ftrace_printk implementation and factorize the printf style format decoding which is now used by three functions.
Frederic Weisbecker (2):
tracing/core: drop the old ftrace_printk implementation in favour of
ftrace_bprintk
vsprintf: unify the format decoding layer for its 3 users
Lai Jiangshan (3):
add binary printf
ftrace: infrastructure for supporting binary record
ftrace: add ftrace_bprintk()
include/asm-generic/vmlinux.lds.h | 9 + include/linux/ftrace.h | 1 - include/linux/kernel.h | 34 +- include/linux/module.h | 5 + include/linux/string.h | 7 + kernel/module.c | 6 + kernel/trace/Kconfig | 1 + kernel/trace/Makefile | 1 + kernel/trace/trace.c | 141 +++--- kernel/trace/trace.h | 8 +- kernel/trace/trace_functions_graph.c | 6 +- kernel/trace/trace_mmiotrace.c | 9 +- kernel/trace/trace_output.c | 41 ++- kernel/trace/trace_output.h | 2 + kernel/trace/trace_printk.c | 138 +++++ lib/Kconfig | 3 + lib/vsprintf.c | 1006 ++++++++++++++++++++++++++-------- 17 files changed, 1096 insertions(+), 322 deletions(-) create mode 100644 kernel/trace/trace_printk.c
PATCH v3 2/5 ftrace: infrastructure for supporting binary record
From: Lai Jiangshan <[email protected]>
From: Lai Jiangshan <[email protected]>
Impact: save on memory for tracing
Current tracers are typically using a struct(like struct ftrace_entry, struct ctx_switch_entry, struct special_entr etc...)to record a binary event. These structs can only record a their own kind of events. A new kind of tracer need a new struct and a lot of code too handle it.
So we need a generic binary record for events. This infrastructure is for this purpose.
[[email protected]: rebase against latest -tip, make it safe while sched tracing as reported by Steven Rostedt]
--- include/linux/ftrace.h | 3 + kernel/trace/Kconfig | 6 +++ kernel/trace/Makefile | 1 + kernel/trace/trace.c | 56 +++++++++++++++++++++++++++ kernel/trace/trace.h | 12 ++++++ kernel/trace/trace_bprintk.c | 87 ++++++++++++++++++++++++++++++++++++++++++ kernel/trace/trace_output.c | 75 ++++++++++++++++++++++++++++++++++++ 7 files changed, 240 insertions(+), 0 deletions(-) create mode 100644 kernel/trace/trace_bprintk.c
include/linux/ftrace.h +3 -0
@@ -223,6 +223,9 @@ extern int ftrace_make_nop(struct module *mod,*/ extern int ftrace_make_call (struct dyn_ftrace *rec,unsigned long addr); +#ifdef CONFIG_TRACE_BPRINTK +extern int trace_vbprintk (unsigned long ip,const char *fmt,va_list args); +#endif /* May be defined in arch */ extern int ftrace_arch_read_dyn_info (char *buf,int size);
kernel/trace/Kconfig +6 -0
@@ -97,6 +97,12 @@ config FUNCTION_GRAPH_TRACER This is done by setting the current return address on the current task structure into a stack of calls. +config TRACE_BPRINTK + bool "Binary printk for tracing" + default y + depends on TRACING + select BINARY_PRINTF + config IRQSOFF_TRACER bool "Interrupts-off Latency Tracer" default n
kernel/trace/Makefile +1 -0
@@ -22,6 +22,7 @@ obj-$(CONFIG_TRACING) += trace.o obj-$(CONFIG_TRACING) += trace_clock.o obj-$(CONFIG_TRACING) += trace_output.o obj-$(CONFIG_TRACING) += trace_stat.o +obj-$(CONFIG_TRACE_BPRINTK) += trace_bprintk.o obj-$(CONFIG_CONTEXT_SWITCH_TRACER) += trace_sched_switch.o obj-$(CONFIG_SYSPROF_TRACER) += trace_sysprof.o obj-$(CONFIG_FUNCTION_TRACER) += trace_functions.o
kernel/trace/trace.c +56 -0
62 lines not on this page · raw message
kernel/trace/trace.h +12 -0
@@ -20,6 +20,7 @@ enum trace_type {TRACE_WAKE ,TRACE_STACK ,TRACE_PRINT , +TRACE_BPRINTK ,TRACE_SPECIAL ,TRACE_MMIO_RW , TRACE_MMIO_MAP,
@@ -124,6 +125,16 @@ struct print_entry {char buf[]; }; +struct bprintk_entry { +struct trace_entry ent; +unsigned long ip; +const char *fmt; +u32 buf[]; +}; +#ifdef CONFIG_TRACE_BPRINTK +extern int trace_bprintk_enable; +#endif +#define TRACE_OLD_SIZE 88struct trace_field_cont {
@@ -285,6 +296,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 bprintk_entry, TRACE_BPRINTK);\ IF_ASSIGN(var, ent, struct special_entry, 0); \ IF_ASSIGN(var, ent, struct trace_mmiotrace_rw, \TRACE_MMIO_RW ); \
kernel/trace/trace_bprintk.c new file +87 -0
87 lines not on this page · raw message
kernel/trace/trace_output.c +75 -0
93 lines not on this page · raw message
Signature · 1 line
1.6.1
[tip:tracing/printk] tracing: infrastructure for supporting binary record
Commit-ID: 1427cdf0592368bdec57276edaf714040ee8744f Gitweb: http://git.kernel.org/tip/1427cdf0592368bdec57276edaf714040ee8744f Author: "Lai Jiangshan" <[email protected]> AuthorDate: Fri, 6 Mar 2009 17:21:47 +0100 Commit: Ingo Molnar <[email protected]> CommitDate: Fri, 6 Mar 2009 17:59:11 +0100
tracing: infrastructure for supporting binary record
Impact: save on memory for tracing
Current tracers are typically using a struct(like struct ftrace_entry, struct ctx_switch_entry, struct special_entr etc...)to record a binary event. These structs can only record a their own kind of events. A new kind of tracer need a new struct and a lot of code too handle it.
So we need a generic binary record for events. This infrastructure is for this purpose.
[[email protected]: rebase against latest -tip, make it safe while sched tracing as reported by Steven Rostedt]
LKML-Reference: <[email protected]>
--- include/linux/ftrace.h | 3 + kernel/trace/Kconfig | 6 +++ kernel/trace/Makefile | 1 + kernel/trace/trace.c | 56 +++++++++++++++++++++++++++ kernel/trace/trace.h | 12 ++++++ kernel/trace/trace_bprintk.c | 87 ++++++++++++++++++++++++++++++++++++++++++ kernel/trace/trace_output.c | 75 ++++++++++++++++++++++++++++++++++++ 7 files changed, 240 insertions(+), 0 deletions(-)
include/linux/ftrace.h +3 -0
@@ -223,6 +223,9 @@ extern int ftrace_make_nop(struct module *mod,*/ extern int ftrace_make_call (struct dyn_ftrace *rec,unsigned long addr); +#ifdef CONFIG_TRACE_BPRINTK +extern int trace_vbprintk (unsigned long ip,const char *fmt,va_list args); +#endif /* May be defined in arch */ extern int ftrace_arch_read_dyn_info (char *buf,int size);
kernel/trace/Kconfig +6 -0
@@ -97,6 +97,12 @@ config FUNCTION_GRAPH_TRACER This is done by setting the current return address on the current task structure into a stack of calls. +config TRACE_BPRINTK + bool "Binary printk for tracing" + default y + depends on TRACING + select BINARY_PRINTF + config IRQSOFF_TRACER bool "Interrupts-off Latency Tracer" default n
kernel/trace/Makefile +1 -0
@@ -22,6 +22,7 @@ obj-$(CONFIG_TRACING) += trace.o obj-$(CONFIG_TRACING) += trace_clock.o obj-$(CONFIG_TRACING) += trace_output.o obj-$(CONFIG_TRACING) += trace_stat.o +obj-$(CONFIG_TRACE_BPRINTK) += trace_bprintk.o obj-$(CONFIG_CONTEXT_SWITCH_TRACER) += trace_sched_switch.o obj-$(CONFIG_SYSPROF_TRACER) += trace_sysprof.o obj-$(CONFIG_FUNCTION_TRACER) += trace_functions.o
kernel/trace/trace.c +56 -0
62 lines not on this page · raw message
kernel/trace/trace.h +12 -0
@@ -20,6 +20,7 @@ enum trace_type {TRACE_WAKE ,TRACE_STACK ,TRACE_PRINT , +TRACE_BPRINTK ,TRACE_SPECIAL ,TRACE_MMIO_RW , TRACE_MMIO_MAP,
@@ -124,6 +125,16 @@ struct print_entry {char buf[]; }; +struct bprintk_entry { +struct trace_entry ent; +unsigned long ip; +const char *fmt; +u32 buf[]; +}; +#ifdef CONFIG_TRACE_BPRINTK +extern int trace_bprintk_enable; +#endif +#define TRACE_OLD_SIZE 88struct trace_field_cont {
@@ -285,6 +296,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 bprintk_entry, TRACE_BPRINTK);\ IF_ASSIGN(var, ent, struct special_entry, 0); \ IF_ASSIGN(var, ent, struct trace_mmiotrace_rw, \TRACE_MMIO_RW ); \
kernel/trace/trace_bprintk.c new file +87 -0
87 lines not on this page · raw message
kernel/trace/trace_output.c +75 -0
93 lines not on this page · raw message
PATCH v3 3/5 ftrace: add ftrace_bprintk()
In reply to Frederic WeisbeckerFrom: Lai Jiangshan <[email protected]>
From: Lai Jiangshan <[email protected]>
Impact: Add a generic printk() for ftrace, like ftrace_printk()
ftrace_bprintk() use the infrastructure to record events on ring_buffer.
[[email protected]: ported to latest -tip, made it work if !CONFIG_MODULES, never free the format strings from modules because we can't keep track of them and conditionnaly create the ftrace format strings section (reported by Steven Rostedt)]
--- include/asm-generic/vmlinux.lds.h | 9 ++++ include/linux/ftrace.h | 21 +++++++++ include/linux/module.h | 5 ++ kernel/module.c | 6 +++ kernel/trace/trace.c | 15 ++++++ kernel/trace/trace_bprintk.c | 87 ++++++++++++++++++++++++++++++++---- 6 files changed, 133 insertions(+), 10 deletions(-)
include/asm-generic/vmlinux.lds.h +9 -0
@@ -69,6 +69,14 @@#define FTRACE_EVENTS ()#endif +#ifdef CONFIG_TRACING +#define FTRACE_PRINTKS () VMLINUX_SYMBOL(__start___trace_bprintk_fmt) = .; \ + *(__trace_printk_fmt)/* Trace_printk fmt' pointer */ \ + VMLINUX_SYMBOL(__stop___trace_bprintk_fmt) = .; +#else +#define FTRACE_PRINTKS () +#endif +/* .data section */ #define DATA_DATA \ *(.data) \
@@ -100,6 +108,7 @@ *(__vermagic)/* Kernel version magic */ \ *(__markers_strings)/* Markers: strings */ \ *(__tracepoints_strings )/* Tracepoints: strings */ \ +FTRACE_PRINTKS () \ } \ \ .rodata1 : AT(ADDR(.rodata1) - LOAD_OFFSET) { \
include/linux/ftrace.h +21 -0
@@ -225,6 +225,27 @@ extern int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr);#ifdef CONFIG_TRACE_BPRINTK extern int trace_vbprintk (unsigned long ip,const char *fmt,va_list args); +extern int __ftrace_bprintk (unsigned long ip,const char *fmt, ...) + __attribute__ ((format (printf,2 ,3 ))); + +static inline void ____ftrace_bprintk_check_format (const char *fmt, ...) + __attribute__ ((format (printf,1 ,2 ))); +static inline void ____ftrace_bprintk_check_format (const char *fmt, ...) {} +#define __ftrace_bprintk_check_format(fmt, args...) \ +do { \ + if (0) \ + ____ftrace_bprintk_check_format(fmt, ##args); \ +} while (0) + +#define ftrace_bprintk(fmt, args...) \ +do { \ + static char *__attribute__((section("__trace_bprintk_fmt"))) \ + ftrace_bprintk_fmt = fmt; \ + __ftrace_bprintk_check_format(fmt, ##args); \ + __ftrace_bprintk(_THIS_IP_, ftrace_bprintk_fmt, ##args); \ +} while (0) +#else +#define ftrace_bprintk ftrace_printk#endif /* May be defined in arch */
include/linux/module.h +5 -0
@@ -347,6 +347,11 @@ struct moduleunsigned int num_tracepoints;#endif +#ifdef CONFIG_TRACE_BPRINTK +const char **trace_bprintk_fmt_start; +unsigned int num_trace_bprintk_fmt; +#endif +#ifdef CONFIG_MODULE_UNLOAD /* What modules depend on me? */ struct list_head modules_which_use_me;
kernel/module.c +6 -0
@@ -2192,6 +2192,12 @@ static noinline struct module *load_module(void __user *umod, &mod->num_tracepoints);#endif +#ifdef CONFIG_TRACE_BPRINTK + mod->trace_bprintk_fmt_start =section_objs (hdr, sechdrs, secstrings, +"__trace_bprintk_fmt" ,sizeof (char *), + &mod->num_trace_bprintk_fmt); +#endif +#ifdef CONFIG_MODVERSIONS if ((mod->num_syms && !mod->crcs) || (mod->num_gpl_syms && !mod->gpl_crcs)
kernel/trace/trace.c +15 -0
@@ -3848,6 +3848,21 @@ out: }EXPORT_SYMBOL_GPL (trace_vbprintk); +int __ftrace_bprintk (unsigned long ip,const char *fmt, ...) +{ +int ret; +va_list ap; + +if (!fmt) +return 0 ; + +va_start (ap, fmt); + ret =trace_vbprintk (ip, fmt, ap); +va_end (ap); +return ret; +} +EXPORT_SYMBOL_GPL (__ftrace_bprintk); +static int trace_panic_handler (struct notifier_block *this,unsigned long event,void *unused) {
kernel/trace/trace_bprintk.c +77 -10
126 lines not on this page · raw message
Signature · 1 line
1.6.1
[tip:tracing/printk] tracing: add trace_bprintk()
Commit-ID: 1ba28e02a18cbdbea123836f6c98efb09cbf59ec Gitweb: http://git.kernel.org/tip/1ba28e02a18cbdbea123836f6c98efb09cbf59ec Author: "Lai Jiangshan" <[email protected]> AuthorDate: Fri, 6 Mar 2009 17:21:48 +0100 Commit: Ingo Molnar <[email protected]> CommitDate: Fri, 6 Mar 2009 17:59:11 +0100
tracing: add trace_bprintk()
Impact: add a generic printk() for tracing, like trace_printk()
trace_bprintk() uses the infrastructure to record events on ring_buffer.
[ [email protected]: ported to latest -tip, made it work if
!CONFIG_MODULES, never free the format strings from modules because we can't keep track of them and conditionnaly create the ftrace format strings section (reported by Steven Rostedt) ]
LKML-Reference: <[email protected]>
--- include/asm-generic/vmlinux.lds.h | 9 ++++ include/linux/ftrace.h | 21 +++++++++ include/linux/module.h | 5 ++ kernel/module.c | 6 +++ kernel/trace/trace.c | 15 ++++++ kernel/trace/trace_bprintk.c | 87 ++++++++++++++++++++++++++++++++---- 6 files changed, 133 insertions(+), 10 deletions(-)
include/asm-generic/vmlinux.lds.h +9 -0
@@ -69,6 +69,14 @@#define FTRACE_EVENTS ()#endif +#ifdef CONFIG_TRACING +#define TRACE_PRINTKS () VMLINUX_SYMBOL(__start___trace_bprintk_fmt) = .; \ + *(__trace_printk_fmt)/* Trace_printk fmt' pointer */ \ + VMLINUX_SYMBOL(__stop___trace_bprintk_fmt) = .; +#else +#define TRACE_PRINTKS () +#endif +/* .data section */ #define DATA_DATA \ *(.data) \
@@ -100,6 +108,7 @@ *(__vermagic)/* Kernel version magic */ \ *(__markers_strings)/* Markers: strings */ \ *(__tracepoints_strings )/* Tracepoints: strings */ \ +TRACE_PRINTKS () \ } \ \ .rodata1 : AT(ADDR(.rodata1) - LOAD_OFFSET) { \
include/linux/ftrace.h +21 -0
@@ -225,6 +225,27 @@ extern int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr);#ifdef CONFIG_TRACE_BPRINTK extern int trace_vbprintk (unsigned long ip,const char *fmt,va_list args); +extern int __trace_bprintk (unsigned long ip,const char *fmt, ...) + __attribute__ ((format (printf,2 ,3 ))); + +static inline void ____trace_bprintk_check_format (const char *fmt, ...) + __attribute__ ((format (printf,1 ,2 ))); +static inline void ____trace_bprintk_check_format (const char *fmt, ...) {} +#define __trace_bprintk_check_format(fmt, args...) \ +do { \ + if (0) \ + ____trace_bprintk_check_format(fmt, ##args); \ +} while (0) + +#define trace_bprintk(fmt, args...) \ +do { \ + static char *__attribute__((section("__trace_bprintk_fmt"))) \ + trace_bprintk_fmt = fmt; \ + __trace_bprintk_check_format(fmt, ##args); \ + __trace_bprintk(_THIS_IP_, trace_bprintk_fmt, ##args); \ +} while (0) +#else +#define trace_bprintk trace_printk#endif /* May be defined in arch */
include/linux/module.h +5 -0
@@ -329,6 +329,11 @@ struct moduleunsigned int num_tracepoints;#endif +#ifdef CONFIG_TRACE_BPRINTK +const char **trace_bprintk_fmt_start; +unsigned int num_trace_bprintk_fmt; +#endif +#ifdef CONFIG_MODULE_UNLOAD /* What modules depend on me? */ struct list_head modules_which_use_me;
kernel/module.c +6 -0
@@ -2158,6 +2158,12 @@ static noinline struct module *load_module(void __user *umod, &mod->num_tracepoints);#endif +#ifdef CONFIG_TRACE_BPRINTK + mod->trace_bprintk_fmt_start =section_objs (hdr, sechdrs, secstrings, +"__trace_bprintk_fmt" ,sizeof (char *), + &mod->num_trace_bprintk_fmt); +#endif +#ifdef CONFIG_MODVERSIONS if ((mod->num_syms && !mod->crcs) || (mod->num_gpl_syms && !mod->gpl_crcs)
kernel/trace/trace.c +15 -0
@@ -3848,6 +3848,21 @@ out: }EXPORT_SYMBOL_GPL (trace_vbprintk); +int __trace_bprintk (unsigned long ip,const char *fmt, ...) +{ +int ret; +va_list ap; + +if (!fmt) +return 0 ; + +va_start (ap, fmt); + ret =trace_vbprintk (ip, fmt, ap); +va_end (ap); +return ret; +} +EXPORT_SYMBOL_GPL (__trace_bprintk); +static int trace_panic_handler (struct notifier_block *this,unsigned long event,void *unused) {
kernel/trace/trace_bprintk.c +77 -10
126 lines not on this page · raw message
PATCH v3 4/5 tracing/core: drop the old ftrace_printk implementation in favour of ftrace_bprintk
In reply to Frederic WeisbeckerImpact: faster and lighter tracing
Now that we have ftrace_bprintk() which is faster and consume lesser memory than ftrace_printk() and has the same purpose, we can now drop the old implementation in favour of the binary one from ftrace_bprintk(), which means we move all the implementation of ftrace_bprintk() to ftrace_printk(), so the Api doesn't change except that we must now use trace_seq_bprintk() to print the TRACE_PRINT entries.
Some changes result of this:
- Previously, ftrace_bprintk depended of a single tracer and couldn't work without. This tracer has been dropped and the whole implementation of ftrace_printk() (like the module formats management) is now integrated in the tracing core (comes with CONFIG_TRACING), though we keep the file trace_printk (previously trace_bprintk.c) where we can find the module management. Thus we don't overflow trace.c
- changes some parts to use trace_seq_bprintk() to print TRACE_PRINT entries.
- change a bit ftrace_printk/ftrace_vprintk macros to support non-builtin formats constants, and fix 'const' qualifiers warnings. But this is all transparent for developers.
- etc...
V2:
- Rebase against last changes
- Fix mispell on the changelog
V3:
- Rebase against last changes (moving ftrace_printk() to kernel.h)
--- include/linux/ftrace.h | 25 ---- include/linux/kernel.h | 34 +++++- include/linux/module.h | 2 +- kernel/trace/Kconfig | 7 +- kernel/trace/Makefile | 2 +- kernel/trace/trace.c | 212 ++++++++++------------------------ kernel/trace/trace.h | 14 +-- kernel/trace/trace_bprintk.c | 154 ------------------------ kernel/trace/trace_functions_graph.c | 6 +- kernel/trace/trace_mmiotrace.c | 9 +- kernel/trace/trace_output.c | 70 ++--------- kernel/trace/trace_output.h | 2 + kernel/trace/trace_printk.c | 138 ++++++++++++++++++++++ 13 files changed, 262 insertions(+), 413 deletions(-) delete mode 100644 kernel/trace/trace_bprintk.c create mode 100644 kernel/trace/trace_printk.c
include/linux/ftrace.h +0 -25
@@ -223,31 +223,6 @@ extern int ftrace_make_nop(struct module *mod,*/ extern int ftrace_make_call (struct dyn_ftrace *rec,unsigned long addr); -#ifdef CONFIG_TRACE_BPRINTK -extern int trace_vbprintk (unsigned long ip,const char *fmt,va_list args); -extern int __ftrace_bprintk (unsigned long ip,const char *fmt, ...) - __attribute__ ((format (printf,2 ,3 ))); - -static inline void ____ftrace_bprintk_check_format (const char *fmt, ...) - __attribute__ ((format (printf,1 ,2 ))); -static inline void ____ftrace_bprintk_check_format (const char *fmt, ...) {} -#define __ftrace_bprintk_check_format(fmt, args...) \ -do { \ - if (0) \ - ____ftrace_bprintk_check_format(fmt, ##args); \ -} while (0) - -#define ftrace_bprintk(fmt, args...) \ -do { \ - static char *__attribute__((section("__trace_bprintk_fmt"))) \ - ftrace_bprintk_fmt = fmt; \ - __ftrace_bprintk_check_format(fmt, ##args); \ - __ftrace_bprintk(_THIS_IP_, ftrace_bprintk_fmt, ##args); \ -} while (0) -#else -#define ftrace_bprintk ftrace_printk -#endif -/* May be defined in arch */ extern int ftrace_arch_read_dyn_info (char *buf,int size);
include/linux/kernel.h +31 -3
@@ -423,6 +423,16 @@ extern void ftrace_off_permanent(void);extern void ftrace_special (unsigned long arg1,unsigned long arg2,unsigned long arg3); +static inline void __attribute__ ((format (printf,1 ,2 ))) +____trace_printk_check_format (const char *fmt, ...) +{ +} +#define __trace_printk_check_format(fmt, args...) \ +do { \ + if (0) \ + ____trace_printk_check_format(fmt, ##args); \ +} while (0) +/** * trace_printk - printf formatting in the ftrace buffer * @fmt: the printf format for printing
@@ -439,13 +449,31 @@ ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3);* Please refrain from leaving trace_printks scattered around in * your code. */ -# define trace_printk(fmt...) __trace_printk(_THIS_IP_, fmt) + +#define trace_printk(fmt, args...) \ +do { \ + static const char *ftrace_printk_fmt \ + __attribute__((section("__trace_printk_fmt"))); \ + ftrace_printk_fmt = fmt; \ + __trace_printk_check_format(fmt, ##args); \ + __trace_printk(_THIS_IP_, ftrace_printk_fmt, ##args); \ +} while (0) +extern int __trace_printk (unsigned long ip,const char *fmt, ...) __attribute__ ((format (printf,2 ,3 ))); -# define ftrace_vprintk (fmt, ap) __trace_printk(_THIS_IP_, fmt, ap) + +#define ftrace_vprintk (fmt, vargs) \ +do { \ + static const char *ftrace_printk_fmt \ + __attribute__((section("__trace_printk_fmt"))); \ + ftrace_printk_fmt = fmt; \ + __ftrace_vprintk(_THIS_IP_, ftrace_printk_fmt, vargs); \ +} while (0) +extern int __ftrace_vprintk (unsigned long ip,const char *fmt,va_list ap); +extern void ftrace_dump (void );#else static inline void
@@ -467,7 +495,7 @@ ftrace_vprintk(const char *fmt, va_list ap) return 0; }static inline void ftrace_dump (void ) { } -#endif +#endif /* CONFIG_TRACING */ /* * Display an IP address in readable format.
include/linux/module.h +1 -1
@@ -347,7 +347,7 @@ struct moduleunsigned int num_tracepoints;#endif -#ifdef CONFIG_TRACE_BPRINTK +#ifdef CONFIG_TRACING const char **trace_bprintk_fmt_start;unsigned int num_trace_bprintk_fmt;#endif
kernel/trace/Kconfig +1 -6
@@ -52,6 +52,7 @@ config TRACING select STACKTRACE if STACKTRACE_SUPPORT select TRACEPOINTS select NOP_TRACER + select BINARY_PRINTF # # Minimum requirements an architecture has to meet for us to
@@ -97,12 +98,6 @@ config FUNCTION_GRAPH_TRACER This is done by setting the current return address on the current task structure into a stack of calls. -config TRACE_BPRINTK - bool "Binary printk for tracing" - default y - depends on TRACING - select BINARY_PRINTF - config IRQSOFF_TRACER bool "Interrupts-off Latency Tracer" default n
kernel/trace/Makefile +1 -1
@@ -22,7 +22,7 @@ obj-$(CONFIG_TRACING) += trace.o obj-$(CONFIG_TRACING) += trace_clock.o obj-$(CONFIG_TRACING) += trace_output.o obj-$(CONFIG_TRACING) += trace_stat.o -obj-$(CONFIG_TRACE_BPRINTK) += trace_bprintk.o +obj-$(CONFIG_TRACING) += trace_printk.o obj-$(CONFIG_CONTEXT_SWITCH_TRACER) += trace_sched_switch.o obj-$(CONFIG_SYSPROF_TRACER) += trace_sysprof.o obj-$(CONFIG_FUNCTION_TRACER) += trace_functions.o
kernel/trace/trace.c +62 -150
230 lines not on this page · raw message
kernel/trace/trace.h +3 -11
@@ -20,7 +20,6 @@ enum trace_type {TRACE_WAKE ,TRACE_STACK ,TRACE_PRINT , -TRACE_BPRINTK ,TRACE_SPECIAL ,TRACE_MMIO_RW , TRACE_MMIO_MAP,
@@ -120,16 +119,10 @@ struct userstack_entry {*/ struct print_entry {struct trace_entry ent; -unsigned long ip; +unsigned long ip;int depth; -char buf[]; -}; - -struct bprintk_entry { -struct trace_entry ent; -unsigned long ip; -const char *fmt; -u32 buf[]; +const char *fmt; +u32 buf[]; };#ifdef CONFIG_TRACE_BPRINTK extern int trace_bprintk_enable;
@@ -296,7 +289,6 @@ 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 bprintk_entry, TRACE_BPRINTK);\ IF_ASSIGN(var, ent, struct special_entry, 0); \ IF_ASSIGN(var, ent, struct trace_mmiotrace_rw, \TRACE_MMIO_RW ); \
kernel/trace/trace_bprintk.c deleted +0 -154
154 lines not on this page · raw message
kernel/trace/trace_functions_graph.c +5 -1
@@ -742,7 +742,11 @@ print_graph_comment(struct print_entry *trace, struct trace_seq *s, }/* The comment */ - ret =trace_seq_printf (s,"/* %s" , trace->buf); + ret =trace_seq_printf (s,"/* " ); +if (!ret) +return TRACE_TYPE_PARTIAL_LINE ; + + ret =trace_seq_bprintf (s, trace->fmt, trace->buf);if (!ret)return TRACE_TYPE_PARTIAL_LINE ;
kernel/trace/trace_mmiotrace.c +6 -3
@@ -254,15 +254,18 @@ 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,1000000ULL ); +unsigned long usec_rem =do_div (t,USEC_PER_SEC );unsigned secs = (unsigned long )t;int ret;/* The trailing newline must be in the message. */ - ret =trace_seq_printf (s,"MARK %u.%06lu %s" , secs, usec_rem, msg); + 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);if (!ret)return TRACE_TYPE_PARTIAL_LINE ;
kernel/trace/trace_output.c +12 -58
107 lines not on this page · raw message
kernel/trace/trace_output.h +2 -0
@@ -18,6 +18,8 @@ struct trace_event {extern int trace_seq_printf (struct trace_seq *s,const char *fmt, ...) __attribute__ ((format (printf,2 ,3 )));extern int +trace_seq_bprintf (struct trace_seq *s,const char *fmt,const u32 *binary); +extern int seq_print_ip_sym (struct trace_seq *s,unsigned long ip,unsigned long sym_flags); extern ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf,
kernel/trace/trace_printk.c new file +138 -0
138 lines not on this page · raw message
Signature · 1 line
1.6.1
Steven Rostedt
maintainerRe: [PATCH 4/5 v3] tracing/core: drop the old ftrace_printk implementation in favour of ftrace_bprintk
Quoted 28 lines · Frederic Weisbecker
Impact: faster and lighter tracing
Now that we have
ftrace_bprintk()which is faster and consume lesser memory thanftrace_printk()and has the same purpose, we can now drop the old implementation in favour of the binary one fromftrace_bprintk(), which means we move all the implementation offtrace_bprintk()toftrace_printk(), so the Api doesn't change except that we must now usetrace_seq_bprintk()to print theTRACE_PRINTentries.Some changes result of this:
- Previously,
ftrace_bprintkdepended of a single tracer and couldn't work without. This tracer has been dropped and the whole implementation offtrace_printk()(like the module formats management) is now integrated in the tracing core (comes withCONFIG_TRACING), though we keep the filetrace_printk(previouslytrace_bprintk.c) where we can find the module management. Thus we don't overflowtrace.c- changes some parts to use
trace_seq_bprintk()to printTRACE_PRINTentries.- change a bit ftrace_printk/ftrace_vprintk macros to support non-builtin formats constants, and fix 'const' qualifiers warnings. But this is all transparent for developers.
- etc...
V2:
Quoted 8 lines
- Rebase against last changes
- Fix mispell on the changelogV3:
- Rebase against last changes (moving
ftrace_printk()tokernel.h)Signed-off-by Frederic Weisbecker <[email protected]>
Very nice Frederic!
For some reason I'm still missing patch 1. Perhaps it decided to take the scenic route ;-)
Quoted 13 lines
kernel/trace/trace.h +0
-1@@ -20,7 +20,6 @@ enum trace_type {TRACE_WAKE ,TRACE_STACK ,TRACE_PRINT , -TRACE_BPRINTK ,TRACE_SPECIAL ,TRACE_MMIO_RW , TRACE_MMIO_MAP,
kernel/trace/trace.h struct userstack_entry { · line 127
Quoted 9 lines
kernel/trace/trace.h +1
-4@@ -120,16 +119,10 @@ struct userstack_entry {*/ struct print_entry {struct trace_entry ent; -unsigned long ip; +unsigned long ip;int depth; -char buf[]; -}; -
Note, this is going to break my recent patch that exports the internal ftrace events. That's OK, I'll fix it after Ingo pulls it in.
Ingo,
Would you prefer that I pull these patches in and then fix the code, and let you pull it from me?
-- Steve
Re: [PATCH 4/5 v3] tracing/core: drop the old ftrace_printk implementation in favour of ftrace_bprintk
Quoted 74 lines · Steven Rostedt
Quoted 28 lines · Frederic Weisbecker
Impact: faster and lighter tracing
Now that we have
ftrace_bprintk()which is faster and consume lesser memory thanftrace_printk()and has the same purpose, we can now drop the old implementation in favour of the binary one fromftrace_bprintk(), which means we move all the implementation offtrace_bprintk()toftrace_printk(), so the Api doesn't change except that we must now usetrace_seq_bprintk()to print theTRACE_PRINTentries.Some changes result of this:
- Previously,
ftrace_bprintkdepended of a single tracer and couldn't work without. This tracer has been dropped and the whole implementation offtrace_printk()(like the module formats management) is now integrated in the tracing core (comes withCONFIG_TRACING), though we keep the filetrace_printk(previouslytrace_bprintk.c) where we can find the module management. Thus we don't overflowtrace.c- changes some parts to use
trace_seq_bprintk()to printTRACE_PRINTentries.- change a bit ftrace_printk/ftrace_vprintk macros to support non-builtin formats constants, and fix 'const' qualifiers warnings. But this is all transparent for developers.
- etc...
V2:
Quoted 8 lines
- Rebase against last changes
- Fix mispell on the changelogV3:
- Rebase against last changes (moving
ftrace_printk()tokernel.h)Signed-off-by Frederic Weisbecker <[email protected]>Very nice Frederic!
For some reason I'm still missing patch 1. Perhaps it decided to take the scenic route ;-)
Quoted 13 lines
kernel/trace/trace.h +0
-1@@ -20,7 +20,6 @@ enum trace_type {TRACE_WAKE ,TRACE_STACK ,TRACE_PRINT , -TRACE_BPRINTK ,TRACE_SPECIAL ,TRACE_MMIO_RW , TRACE_MMIO_MAP,kernel/trace/trace.h struct userstack_entry { · line 127
Quoted 9 lines
kernel/trace/trace.h +1
-4@@ -120,16 +119,10 @@ struct userstack_entry {*/ struct print_entry {struct trace_entry ent; -unsigned long ip; +unsigned long ip;int depth; -char buf[]; -}; -Note, this is going to break my recent patch that exports the internal ftrace events. That's OK, I'll fix it after Ingo pulls it in.
Quoted 4 lines
Ingo,
Would you prefer that I pull these patches in and then fix the code, and let you pull it from me?
i've put them into a separate branch forked off tip:tracing/ftrace: tip:tracing/printk. The two vsprintf patches are in tip:core/printk and merged into tracing/ftrace.
You can thus apply your current enhancements without any interaction with me - and merge tracing/printk into the tip:tracing/ftrace branch on your own schedule, ok?
Ingo
Re: [PATCH 4/5 v3] tracing/core: drop the old ftrace_printk implementation in favour of ftrace_bprintk
Quoted 82 lines · Ingo Molnar
Quoted 74 lines · Steven Rostedt
Quoted 28 lines · Frederic Weisbecker
Impact: faster and lighter tracing
Now that we have
ftrace_bprintk()which is faster and consume lesser memory thanftrace_printk()and has the same purpose, we can now drop the old implementation in favour of the binary one fromftrace_bprintk(), which means we move all the implementation offtrace_bprintk()toftrace_printk(), so the Api doesn't change except that we must now usetrace_seq_bprintk()to print theTRACE_PRINTentries.Some changes result of this:
- Previously,
ftrace_bprintkdepended of a single tracer and couldn't work without. This tracer has been dropped and the whole implementation offtrace_printk()(like the module formats management) is now integrated in the tracing core (comes withCONFIG_TRACING), though we keep the filetrace_printk(previouslytrace_bprintk.c) where we can find the module management. Thus we don't overflowtrace.c- changes some parts to use
trace_seq_bprintk()to printTRACE_PRINTentries.- change a bit ftrace_printk/ftrace_vprintk macros to support non-builtin formats constants, and fix 'const' qualifiers warnings. But this is all transparent for developers.
- etc...
V2:
Quoted 8 lines
- Rebase against last changes
- Fix mispell on the changelogV3:
- Rebase against last changes (moving
ftrace_printk()tokernel.h)Signed-off-by Frederic Weisbecker <[email protected]>Very nice Frederic!
For some reason I'm still missing patch 1. Perhaps it decided to take the scenic route ;-)
Quoted 13 lines
kernel/trace/trace.h +0
-1@@ -20,7 +20,6 @@ enum trace_type {TRACE_WAKE ,TRACE_STACK ,TRACE_PRINT , -TRACE_BPRINTK ,TRACE_SPECIAL ,TRACE_MMIO_RW , TRACE_MMIO_MAP,kernel/trace/trace.h struct userstack_entry { · line 127
Quoted 9 lines
kernel/trace/trace.h +1
-4@@ -120,16 +119,10 @@ struct userstack_entry {*/ struct print_entry {struct trace_entry ent; -unsigned long ip; +unsigned long ip;int depth; -char buf[]; -}; -Note, this is going to break my recent patch that exports the internal ftrace events. That's OK, I'll fix it after Ingo pulls it in.
Quoted 4 lines
Ingo,
Would you prefer that I pull these patches in and then fix the code, and let you pull it from me?
Quoted 7 lines
i've put them into a separate branch forked off tip:tracing/ftrace: tip:tracing/printk. The two vsprintf patches are in tip:core/printk and merged into tracing/ftrace.
You can thus apply your current enhancements without any interaction with me - and merge tracing/printk into the tip:tracing/ftrace branch on your own schedule, ok?
ah - i misunderstood - i see that it interacts with your commits from last night.
I've excluded tip:tracing/printk from tip:master for now and will let you guys fix it up when you have time for it.
Ingo
Steven Rostedt
maintainerRe: [PATCH 4/5 v3] tracing/core: drop the old ftrace_printk implementation in favour of ftrace_bprintk
Quoted 9 lines · Ingo Molnar
Quoted 8 lines
i've put them into a separate branch forked off tip:tracing/ftrace: tip:tracing/printk. The two vsprintf patches are in tip:core/printk and merged into tracing/ftrace.
You can thus apply your current enhancements without any interaction with me - and merge tracing/printk into the tip:tracing/ftrace branch on your own schedule, ok?
Quoted 5 lines
ah - i misunderstood - i see that it interacts with your commits from last night.
I've excluded tip:tracing/printk from tip:master for now and will let you guys fix it up when you have time for it.
Ingo,
It compiles fine. Only a new item in the struct was added, nothing was removed. It just prints out a bit funny ;-)
You can go ahead and merge it into tracing/ftrace, and then I can work with that to fix up the formatting.
-- Steve
Re: [PATCH 4/5 v3] tracing/core: drop the old ftrace_printk implementation in favour of ftrace_bprintk
Quoted 17 lines · Steven Rostedt
Quoted 9 lines · Ingo Molnar
Quoted 8 lines
i've put them into a separate branch forked off tip:tracing/ftrace: tip:tracing/printk. The two vsprintf patches are in tip:core/printk and merged into tracing/ftrace.
You can thus apply your current enhancements without any interaction with me - and merge tracing/printk into the tip:tracing/ftrace branch on your own schedule, ok?
Quoted 5 lines
ah - i misunderstood - i see that it interacts with your commits from last night.
I've excluded tip:tracing/printk from tip:master for now and will let you guys fix it up when you have time for it.
Quoted 7 lines
Ingo,
It compiles fine. Only a new item in the struct was added, nothing was removed. It just prints out a bit funny ;-)
You can go ahead and merge it into tracing/ftrace, and then I can work with that to fix up the formatting.
triggers this build failure on certain configs:
kernel/trace/trace.c:1205: error: implicit declaration of function 'vbin_printf' kernel/trace/trace_output.c:64: error: implicit declaration of function 'bstr_printf'
so i've excluded it from tip:master again for now.
Ingo
Re: [PATCH 4/5 v3] tracing/core: drop the old ftrace_printk implementation in favour of ftrace_bprintk
Quoted 28 lines · Ingo Molnar
Quoted 17 lines · Steven Rostedt
Quoted 9 lines · Ingo Molnar
Quoted 8 lines
i've put them into a separate branch forked off tip:tracing/ftrace: tip:tracing/printk. The two vsprintf patches are in tip:core/printk and merged into tracing/ftrace.
You can thus apply your current enhancements without any interaction with me - and merge tracing/printk into the tip:tracing/ftrace branch on your own schedule, ok?
Quoted 5 lines
ah - i misunderstood - i see that it interacts with your commits from last night.
I've excluded tip:tracing/printk from tip:master for now and will let you guys fix it up when you have time for it.
Quoted 7 lines
Ingo,
It compiles fine. Only a new item in the struct was added, nothing was removed. It just prints out a bit funny ;-)
You can go ahead and merge it into tracing/ftrace, and then I can work with that to fix up the formatting.
Quoted 8 lines
triggers this build failure on certain configs:
kernel/trace/trace.c:1205: error: implicit declaration of function 'vbin_printf'kernel/trace/trace_output.c:64: error: implicit declaration of function 'bstr_printf'so i've excluded it from tip:master again for now.
Ingo
This is weird. Can you send me one of these configs please?
Re: [PATCH 4/5 v3] tracing/core: drop the old ftrace_printk implementation in favour of ftrace_bprintk
In reply to Ingo MolnarQuoted 9 lines · Ingo Molnar
Quoted 2 lines
You can go ahead and merge it into tracing/ftrace, and then I can work with that to fix up the formatting.
triggers this build failure on certain configs:
kernel/trace/trace.c:1205: error: implicit declaration of function 'vbin_printf'kernel/trace/trace_output.c:64: error: implicit declaration of function 'bstr_printf'so i've excluded it from tip:master again for now.
ah, disregard this - that's a testing fluke - i have put it back again.
(a test-system got hold of an intermediate merge and found a build bug in it.)
Ingo
Re: [PATCH 4/5 v3] tracing/core: drop the old ftrace_printk implementation in favour of ftrace_bprintk
In reply to Steven RostedtQuoted 35 lines · Steven Rostedt
Quoted 31 lines · Frederic Weisbecker
Impact: faster and lighter tracing
Now that we have
ftrace_bprintk()which is faster and consume lesser memory thanftrace_printk()and has the same purpose, we can now drop the old implementation in favour of the binary one fromftrace_bprintk(), which means we move all the implementation offtrace_bprintk()toftrace_printk(), so the Api doesn't change except that we must now usetrace_seq_bprintk()to print theTRACE_PRINTentries.Some changes result of this:
- Previously,
ftrace_bprintkdepended of a single tracer and couldn't work without. This tracer has been dropped and the whole implementation offtrace_printk()(like the module formats management) is now integrated in the tracing core (comes withCONFIG_TRACING), though we keep the filetrace_printk(previouslytrace_bprintk.c) where we can find the module management. Thus we don't overflowtrace.c- changes some parts to use
trace_seq_bprintk()to printTRACE_PRINTentries.- change a bit ftrace_printk/ftrace_vprintk macros to support non-builtin formats constants, and fix 'const' qualifiers warnings. But this is all transparent for developers.
- etc...
V2:
- Rebase against last changes
- Fix mispell on the changelog
Quoted 7 lines
Quoted 5 lines
V3:
- Rebase against last changes (moving
ftrace_printk()tokernel.h)Signed-off-by Frederic Weisbecker <[email protected]>Very nice Frederic!
Thanks!
Quoted 21 lines
For some reason I'm still missing patch 1. Perhaps it decided to take the scenic route ;-)
Quoted 18 lines
kernel/trace/trace.h +1
-2@@ -20,7 +20,6 @@ enum trace_type {TRACE_WAKE ,TRACE_STACK ,TRACE_PRINT , -TRACE_BPRINTK ,TRACE_SPECIAL ,TRACE_MMIO_RW , TRACE_MMIO_MAP,@@ -120,16 +119,10 @@ struct userstack_entry {*/ struct print_entry {struct trace_entry ent; -unsigned long ip; +unsigned long ip;
Quoted 8 lines
kernel/trace/trace.h struct userstack_entry { · line 127
Quoted 4 lines
kernel/trace/trace.h +0
-3int depth; -char buf[]; -}; -Note, this is going to break my recent patch that exports the internal ftrace events.
Argh, sorry I forgot these recent patches.
Quoted 1 line
That's OK, I'll fix it after Ingo pulls it in.
Thanks :-)
Quoted 7 lines
Ingo,
Would you prefer that I pull these patches in and then fix the code, and let you pull it from me?
-- Steve
[tip:tracing/printk] tracing/core: drop the old trace_printk() implementation in favour of trace_bprintk()
In reply to Frederic WeisbeckerCommit-ID: 769b0441f438c4bb4872cb8560eb6fe51bcc09ee Gitweb: http://git.kernel.org/tip/769b0441f438c4bb4872cb8560eb6fe51bcc09ee Author: "Frederic Weisbecker" <[email protected]> AuthorDate: Fri, 6 Mar 2009 17:21:49 +0100 Commit: Ingo Molnar <[email protected]> CommitDate: Fri, 6 Mar 2009 17:59:12 +0100
tracing/core: drop the old trace_printk() implementation in favour of trace_bprintk()
Impact: faster and lighter tracing
Now that we have trace_bprintk() which is faster and consume lesser memory than trace_printk() and has the same purpose, we can now drop the old implementation in favour of the binary one from trace_bprintk(), which means we move all the implementation of trace_bprintk() to trace_printk(), so the Api doesn't change except that we must now use trace_seq_bprintk() to print the TRACE_PRINT entries.
Some changes result of this:
- Previously, trace_bprintk depended of a single tracer and couldn't work without. This tracer has been dropped and the whole implementation of trace_printk() (like the module formats management) is now integrated in the tracing core (comes with CONFIG_TRACING), though we keep the file trace_printk (previously trace_bprintk.c) where we can find the module management. Thus we don't overflow trace.c
- changes some parts to use trace_seq_bprintk() to print TRACE_PRINT entries.
- change a bit trace_printk/trace_vprintk macros to support non-builtin formats constants, and fix 'const' qualifiers warnings. But this is all transparent for developers.
- etc...
V2:
- Rebase against last changes
- Fix mispell on the changelog
V3:
- Rebase against last changes (moving trace_printk() to kernel.h)
LKML-Reference: <[email protected]>
---
include/linux/ftrace.h | 25 ---
include/linux/kernel.h | 34 +++-
include/linux/module.h | 2 +-
kernel/trace/Kconfig | 7 +-
kernel/trace/Makefile | 2 +-
kernel/trace/trace.c | 212 +++++++---------------
kernel/trace/trace.h | 14 +-
kernel/trace/trace_functions_graph.c | 6 +-
kernel/trace/trace_mmiotrace.c | 9 +-
kernel/trace/trace_output.c | 70 ++------
kernel/trace/trace_output.h | 2 +
kernel/trace/{trace_bprintk.c => trace_printk.c} | 84 ++++-----
12 files changed, 158 insertions(+), 309 deletions(-)include/linux/ftrace.h +0 -25
@@ -223,31 +223,6 @@ extern int ftrace_make_nop(struct module *mod,*/ extern int ftrace_make_call (struct dyn_ftrace *rec,unsigned long addr); -#ifdef CONFIG_TRACE_BPRINTK -extern int trace_vbprintk (unsigned long ip,const char *fmt,va_list args); -extern int __trace_bprintk (unsigned long ip,const char *fmt, ...) - __attribute__ ((format (printf,2 ,3 ))); - -static inline void ____trace_bprintk_check_format (const char *fmt, ...) - __attribute__ ((format (printf,1 ,2 ))); -static inline void ____trace_bprintk_check_format (const char *fmt, ...) {} -#define __trace_bprintk_check_format(fmt, args...) \ -do { \ - if (0) \ - ____trace_bprintk_check_format(fmt, ##args); \ -} while (0) - -#define trace_bprintk(fmt, args...) \ -do { \ - static char *__attribute__((section("__trace_bprintk_fmt"))) \ - trace_bprintk_fmt = fmt; \ - __trace_bprintk_check_format(fmt, ##args); \ - __trace_bprintk(_THIS_IP_, trace_bprintk_fmt, ##args); \ -} while (0) -#else -#define trace_bprintk trace_printk -#endif -/* May be defined in arch */ extern int ftrace_arch_read_dyn_info (char *buf,int size);
include/linux/kernel.h +31 -3
@@ -423,6 +423,16 @@ extern void ftrace_off_permanent(void);extern void ftrace_special (unsigned long arg1,unsigned long arg2,unsigned long arg3); +static inline void __attribute__ ((format (printf,1 ,2 ))) +____trace_printk_check_format (const char *fmt, ...) +{ +} +#define __trace_printk_check_format(fmt, args...) \ +do { \ + if (0) \ + ____trace_printk_check_format(fmt, ##args); \ +} while (0) +/** * trace_printk - printf formatting in the ftrace buffer * @fmt: the printf format for printing
@@ -439,13 +449,31 @@ ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3);* Please refrain from leaving trace_printks scattered around in * your code. */ -# define trace_printk(fmt...) __trace_printk(_THIS_IP_, fmt) + +#define trace_printk(fmt, args...) \ +do { \ + static const char *trace_printk_fmt \ + __attribute__((section("__trace_printk_fmt"))); \ + trace_printk_fmt = fmt; \ + __trace_printk_check_format(fmt, ##args); \ + __trace_printk(_THIS_IP_, trace_printk_fmt, ##args); \ +} while (0) +extern int __trace_printk (unsigned long ip,const char *fmt, ...) __attribute__ ((format (printf,2 ,3 ))); -# define ftrace_vprintk (fmt, ap) __trace_printk(_THIS_IP_, fmt, ap) + +#define ftrace_vprintk (fmt, vargs) \ +do { \ + static const char *trace_printk_fmt \ + __attribute__((section("__trace_printk_fmt"))); \ + trace_printk_fmt = fmt; \ + __ftrace_vprintk(_THIS_IP_, trace_printk_fmt, vargs); \ +} while (0) +extern int __ftrace_vprintk (unsigned long ip,const char *fmt,va_list ap); +extern void ftrace_dump (void );#else static inline void
@@ -467,7 +495,7 @@ ftrace_vprintk(const char *fmt, va_list ap) return 0; }static inline void ftrace_dump (void ) { } -#endif +#endif /* CONFIG_TRACING */ /* * Display an IP address in readable format.
include/linux/module.h +1 -1
@@ -329,7 +329,7 @@ struct moduleunsigned int num_tracepoints;#endif -#ifdef CONFIG_TRACE_BPRINTK +#ifdef CONFIG_TRACING const char **trace_bprintk_fmt_start;unsigned int num_trace_bprintk_fmt;#endif
kernel/trace/Kconfig +1 -6
@@ -52,6 +52,7 @@ config TRACING select STACKTRACE if STACKTRACE_SUPPORT select TRACEPOINTS select NOP_TRACER + select BINARY_PRINTF # # Minimum requirements an architecture has to meet for us to
@@ -97,12 +98,6 @@ config FUNCTION_GRAPH_TRACER This is done by setting the current return address on the current task structure into a stack of calls. -config TRACE_BPRINTK - bool "Binary printk for tracing" - default y - depends on TRACING - select BINARY_PRINTF - config IRQSOFF_TRACER bool "Interrupts-off Latency Tracer" default n
kernel/trace/Makefile +1 -1
@@ -22,7 +22,7 @@ obj-$(CONFIG_TRACING) += trace.o obj-$(CONFIG_TRACING) += trace_clock.o obj-$(CONFIG_TRACING) += trace_output.o obj-$(CONFIG_TRACING) += trace_stat.o -obj-$(CONFIG_TRACE_BPRINTK) += trace_bprintk.o +obj-$(CONFIG_TRACING) += trace_printk.o obj-$(CONFIG_CONTEXT_SWITCH_TRACER) += trace_sched_switch.o obj-$(CONFIG_SYSPROF_TRACER) += trace_sysprof.o obj-$(CONFIG_FUNCTION_TRACER) += trace_functions.o
kernel/trace/trace.c +62 -150
230 lines not on this page · raw message
kernel/trace/trace.h +3 -11
@@ -20,7 +20,6 @@ enum trace_type {TRACE_WAKE ,TRACE_STACK ,TRACE_PRINT , -TRACE_BPRINTK ,TRACE_SPECIAL ,TRACE_MMIO_RW , TRACE_MMIO_MAP,
@@ -120,16 +119,10 @@ struct userstack_entry {*/ struct print_entry {struct trace_entry ent; -unsigned long ip; +unsigned long ip;int depth; -char buf[]; -}; - -struct bprintk_entry { -struct trace_entry ent; -unsigned long ip; -const char *fmt; -u32 buf[]; +const char *fmt; +u32 buf[]; };#ifdef CONFIG_TRACE_BPRINTK extern int trace_bprintk_enable;
@@ -296,7 +289,6 @@ 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 bprintk_entry, TRACE_BPRINTK);\ IF_ASSIGN(var, ent, struct special_entry, 0); \ IF_ASSIGN(var, ent, struct trace_mmiotrace_rw, \TRACE_MMIO_RW ); \
kernel/trace/trace_functions_graph.c +5 -1
@@ -742,7 +742,11 @@ print_graph_comment(struct print_entry *trace, struct trace_seq *s, }/* The comment */ - ret =trace_seq_printf (s,"/* %s" , trace->buf); + ret =trace_seq_printf (s,"/* " ); +if (!ret) +return TRACE_TYPE_PARTIAL_LINE ; + + ret =trace_seq_bprintf (s, trace->fmt, trace->buf);if (!ret)return TRACE_TYPE_PARTIAL_LINE ;
kernel/trace/trace_mmiotrace.c +6 -3
@@ -254,15 +254,18 @@ 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,1000000ULL ); +unsigned long usec_rem =do_div (t,USEC_PER_SEC );unsigned secs = (unsigned long )t;int ret;/* The trailing newline must be in the message. */ - ret =trace_seq_printf (s,"MARK %u.%06lu %s" , secs, usec_rem, msg); + 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);if (!ret)return TRACE_TYPE_PARTIAL_LINE ;
kernel/trace/trace_output.c +12 -58
107 lines not on this page · raw message
kernel/trace/trace_output.h +2 -0
@@ -18,6 +18,8 @@ struct trace_event {extern int trace_seq_printf (struct trace_seq *s,const char *fmt, ...) __attribute__ ((format (printf,2 ,3 )));extern int +trace_seq_bprintf (struct trace_seq *s,const char *fmt,const u32 *binary); +extern int seq_print_ip_sym (struct trace_seq *s,unsigned long ip,unsigned long sym_flags); extern ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf,
kernel/trace/trace_printk.c renamed from kernel/trace/trace_bprintk.c +34 -50
125 lines not on this page · raw message
[tip:tracing/printk] tracing: trace_bprintk() cleanups
In reply to Frederic WeisbeckerCommit-ID: 9de36825b321fe9fe9cf73260554251af579f4ca Gitweb: http://git.kernel.org/tip/9de36825b321fe9fe9cf73260554251af579f4ca Author: "Ingo Molnar" <[email protected]> AuthorDate: Fri, 6 Mar 2009 17:52:03 +0100 Commit: Ingo Molnar <[email protected]> CommitDate: Fri, 6 Mar 2009 17:59:12 +0100
tracing: trace_bprintk() cleanups
Impact: cleanup
Remove a few leftovers and clean up the code a bit.
LKML-Reference: <[email protected]>
--- kernel/module.c | 6 ------ kernel/trace/trace.h | 19 ++++++++----------- 2 files changed, 8 insertions(+), 17 deletions(-)
kernel/module.c +0 -6
@@ -2158,12 +2158,6 @@ static noinline struct module *load_module(void __user *umod, &mod->num_tracepoints);#endif -#ifdef CONFIG_TRACE_BPRINTK - mod->trace_bprintk_fmt_start =section_objs (hdr, sechdrs, secstrings, -"__trace_bprintk_fmt" ,sizeof (char *), - &mod->num_trace_bprintk_fmt); -#endif -#ifdef CONFIG_MODVERSIONS if ((mod->num_syms && !mod->crcs) || (mod->num_gpl_syms && !mod->gpl_crcs)
kernel/trace/trace.h +8 -11
@@ -119,14 +119,11 @@ struct userstack_entry {*/ struct print_entry {struct trace_entry ent; -unsigned long ip; +unsigned long ip;int depth;const char *fmt; -u32 buf[]; +u32 buf[]; }; -#ifdef CONFIG_TRACE_BPRINTK -extern int trace_bprintk_enable; -#endif #define TRACE_OLD_SIZE 88
@@ -199,7 +196,7 @@ struct kmemtrace_free_entry {* trace_flag_type is an enumeration that holds different * states when a trace occurs. These are: * IRQS_OFF - interrupts were disabled -* IRQS_NOSUPPORT - arch does not support irqs_disabled_flags +* IRQS_NOSUPPORT - arch does not support irqs_disabled_flags * NEED_RESCED - reschedule is requested * HARDIRQ - inside an interrupt handler * SOFTIRQ - inside a softirq handler
@@ -302,7 +299,7 @@ extern void __ftrace_bad_type(void); IF_ASSIGN(var, ent, struct ftrace_graph_ret_entry, \TRACE_GRAPH_RET ); \ IF_ASSIGN(var, ent, struct hw_branch_entry, TRACE_HW_BRANCHES);\ - IF_ASSIGN(var, ent, struct trace_power, TRACE_POWER); \ + IF_ASSIGN(var, ent, struct trace_power, TRACE_POWER); \ IF_ASSIGN(var, ent, struct kmemtrace_alloc_entry, \TRACE_KMEM_ALLOC ); \ IF_ASSIGN(var, ent, struct kmemtrace_free_entry, \
@@ -325,8 +322,8 @@ enum print_line_t {* flags value in struct tracer_flags. */ struct tracer_opt { -const char *name;/* Will appear on the trace_options file */ -u32 bit;/* Mask assigned in val field in tracer_flags */ +const char *name;/* Will appear on the trace_options file */ +u32 bit;/* Mask assigned in val field in tracer_flags */ };/*
@@ -335,7 +332,7 @@ struct tracer_opt {*/ struct tracer_flags {u32 val; -struct tracer_opt *opts; +struct tracer_opt *opts; };/* Makes more easy to define a tracer opt */
@@ -390,7 +387,7 @@ struct tracer {int (*set_flag)(u32 old_flags,u32 bit,int set);struct tracer *next;int print_max; -struct tracer_flags *flags; +struct tracer_flags *flags;struct tracer_stat *stats; };
[tip:tracing/printk] tracing: trace_printk() fix, move format array to data section
In reply to Frederic WeisbeckerCommit-ID: 8a20d84d09ab5d121f989cd99e4fc5f4b49f98ba Gitweb: http://git.kernel.org/tip/8a20d84d09ab5d121f989cd99e4fc5f4b49f98ba Author: "Ingo Molnar" <[email protected]> AuthorDate: Mon, 9 Mar 2009 10:09:06 +0100 Commit: Ingo Molnar <[email protected]> CommitDate: Mon, 9 Mar 2009 10:11:08 +0100
tracing: trace_printk() fix, move format array to data section
Impact: fix kernel crash when using trace_printk()
trace_printk_fmt section is defined into the readonly section. But we do:
trace_printk_fmt = fmt;
to fill in that table of format strings - which is not read-only. Under CONFIG_DEBUG_RODATA=y this crashes ...
Cc: Frederic Weisbecker <[email protected]>
Cc: Steven Rostedt <[email protected]>
Cc: Peter Zijlstra <[email protected]>
LKML-Reference: <[email protected]>
--- include/asm-generic/vmlinux.lds.h | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
include/asm-generic/vmlinux.lds.h +1 -1
@@ -98,6 +98,7 @@ VMLINUX_SYMBOL(__stop___tracepoints) = .; \ LIKELY_PROFILE() \ BRANCH_PROFILE() \ + TRACE_PRINTKS() \ FTRACE_EVENTS() #define RO_DATA(align) \
@@ -108,7 +109,6 @@ *(__vermagic)/* Kernel version magic */ \ *(__markers_strings)/* Markers: strings */ \ *(__tracepoints_strings)/* Tracepoints: strings */ \ -TRACE_PRINTKS () \ } \ \ .rodata1 : AT(ADDR(.rodata1) - LOAD_OFFSET) { \
[tip:tracing/printk] tracing: optimize trace_printk()
In reply to Frederic WeisbeckerCommit-ID: 7bffc23e56e92c14b787bf4d95249a32085bfed5 Gitweb: http://git.kernel.org/tip/7bffc23e56e92c14b787bf4d95249a32085bfed5 Author: "Ingo Molnar" <[email protected]> AuthorDate: Mon, 9 Mar 2009 10:11:36 +0100 Commit: Ingo Molnar <[email protected]> CommitDate: Mon, 9 Mar 2009 10:11:36 +0100
tracing: optimize trace_printk()
Impact: micro-optimization
trace_printk() does this unconditionally:
trace_printk_fmt = fmt;
Where trace_printk_fmt is an entry into a global array. This is very SMP-unfriendly.
So only write it once per bootup.
Cc: Frederic Weisbecker <[email protected]>
Cc: Steven Rostedt <[email protected]>
Cc: Peter Zijlstra <[email protected]>
LKML-Reference: <[email protected]>
--- include/linux/kernel.h | 10 ++++++++-- 1 files changed, 8 insertions(+), 2 deletions(-)
include/linux/kernel.h +8 -2
@@ -454,7 +454,10 @@ do { \ do { \ static const char *trace_printk_fmt \ __attribute__((section("__trace_printk_fmt"))); \ - trace_printk_fmt = fmt; \ + \ + if (!trace_printk_fmt) \ + trace_printk_fmt = fmt; \ + \ __trace_printk_check_format(fmt, ##args); \ __trace_printk(_THIS_IP_, trace_printk_fmt, ##args); \ } while (0)
@@ -467,7 +470,10 @@ __trace_printk(unsigned long ip, const char *fmt, ...)do { \ static const char *trace_printk_fmt \ __attribute__((section("__trace_printk_fmt"))); \ - trace_printk_fmt = fmt; \ + \ +if (!trace_printk_fmt) \ + trace_printk_fmt = fmt; \ + \__ftrace_vprintk (_THIS_IP_, trace_printk_fmt, vargs); \ }while (0 )
Steven Rostedt
maintainerRe: [tip:tracing/printk] tracing: optimize trace_printk()
Quoted 30 lines · Ingo Molnar
tracing: optimize
trace_printk()Impact: micro-optimization
trace_printk()does this unconditionally:trace_printk_fmt = fmt;Where
trace_printk_fmtis an entry into a global array. This is very SMP-unfriendly.So only write it once per bootup.
Cc: Frederic Weisbecker <[email protected]>
Cc: Steven Rostedt <[email protected]>
Cc: Peter Zijlstra <[email protected]>
LKML-Reference: <[email protected]>Signed-off-by Ingo Molnar <[email protected]>--- include/linux/kernel.h | 10 ++++++++-- 1 files changed, 8 insertions(+), 2 deletions(-)include/linux/kernel.h +0
-0
include/linux/kernel.h do { \ · line 460
Quoted 8 lines
include/linux/kernel.h +4
-1@@ -454,7 +454,10 @@ do { \ do { \ static const char *trace_printk_fmt \ __attribute__((section("__trace_printk_fmt"))); \ - trace_printk_fmt = fmt; \ + \ + if (!trace_printk_fmt) \ + trace_printk_fmt = fmt; \ + \
But this is a static init. That is, it is done at initialization and not every time.
The change actually slows down the system.
include/linux/kernel.h __trace_printk(unsigned long ip, const char *fmt, ...) · line 476
Quoted 12 lines
include/linux/kernel.h +4
-1__trace_printk_check_format(fmt, ##args); \ __trace_printk(_THIS_IP_, trace_printk_fmt, ##args); \ } while (0)@@ -467,7 +470,10 @@ __trace_printk(unsigned long ip, const char *fmt, ...) do { \ static const char *trace_printk_fmt \ __attribute__((section("__trace_printk_fmt"))); \ - trace_printk_fmt = fmt; \ + \ + if (!trace_printk_fmt) \ + trace_printk_fmt = fmt; \ + \
Same here.
-- Steve
Quoted 4 lines
include/linux/kernel.h +0
-0__ftrace_vprintk (_THIS_IP_, trace_printk_fmt, vargs); \ }while (0 )
Steven Rostedt
maintainerRe: [tip:tracing/printk] tracing: optimize trace_printk()
Quoted 27 lines · Steven Rostedt
Quoted 25 lines · Ingo Molnar
tracing: optimize
trace_printk()Impact: micro-optimization
trace_printk()does this unconditionally:trace_printk_fmt = fmt;Where
trace_printk_fmtis an entry into a global array. This is very SMP-unfriendly.So only write it once per bootup.
Cc: Frederic Weisbecker <[email protected]>
Cc: Steven Rostedt <[email protected]>
Cc: Peter Zijlstra <[email protected]>
LKML-Reference: <[email protected]>Signed-off-by Ingo Molnar <[email protected]>--- include/linux/kernel.h | 10 ++++++++-- 1 files changed, 8 insertions(+), 2 deletions(-)
Quoted 8 lines
include/linux/kernel.h do { \ · line 456
Quoted 8 lines
include/linux/kernel.h +0
-0@@ -454,7 +454,10 @@ do { \ do { \ static const char *trace_printk_fmt \ __attribute__((section("__trace_printk_fmt"))); \
A I missed the semicolon. I wonder if we could make it a static init?
-- Steve
Quoted 33 lines
include/linux/kernel.h do { \ · line 460
Quoted 5 lines
include/linux/kernel.h +4
-1- trace_printk_fmt = fmt; \ + \ +if (!trace_printk_fmt) \ + trace_printk_fmt = fmt; \ + \But this is a static init. That is, it is done at initialization and not every time.
The change actually slows down the system.
include/linux/kernel.h __trace_printk(unsigned long ip, const char *fmt, ...) · line 476
Quoted 12 lines
include/linux/kernel.h +4
-1__trace_printk_check_format(fmt, ##args); \ __trace_printk(_THIS_IP_, trace_printk_fmt, ##args); \ } while (0)@@ -467,7 +470,10 @@ __trace_printk(unsigned long ip, const char *fmt, ...) do { \ static const char *trace_printk_fmt \ __attribute__((section("__trace_printk_fmt"))); \ - trace_printk_fmt = fmt; \ + \ + if (!trace_printk_fmt) \ + trace_printk_fmt = fmt; \ + \Same here.
-- Steve
Quoted 4 lines
include/linux/kernel.h +0
-0__ftrace_vprintk (_THIS_IP_, trace_printk_fmt, vargs); \ }while (0 )
Re: [tip:tracing/printk] tracing: optimize trace_printk()
Quoted 43 lines · Steven Rostedt
Quoted 27 lines · Steven Rostedt
Quoted 25 lines · Ingo Molnar
tracing: optimize
trace_printk()Impact: micro-optimization
trace_printk()does this unconditionally:trace_printk_fmt = fmt;Where
trace_printk_fmtis an entry into a global array. This is very SMP-unfriendly.So only write it once per bootup.
Cc: Frederic Weisbecker <[email protected]>
Cc: Steven Rostedt <[email protected]>
Cc: Peter Zijlstra <[email protected]>
LKML-Reference: <[email protected]>Signed-off-by Ingo Molnar <[email protected]>--- include/linux/kernel.h | 10 ++++++++-- 1 files changed, 8 insertions(+), 2 deletions(-)Quoted 8 lines
include/linux/kernel.h do { \ · line 456
Quoted 8 lines
include/linux/kernel.h +0
-0@@ -454,7 +454,10 @@ do { \ do { \ static const char *trace_printk_fmt \ __attribute__((section("__trace_printk_fmt"))); \A I missed the semicolon. I wonder if we could make it a static init?
Quoted 1 line
-- Steve
Actually I first tried to make it like this:
static const char *trace_printk_fmt ___attribute__((section("__trace_printk_fmt"))) = fmt;
But gcc slapped me. That's why I moved it in the following line.
Quoted 33 lines
Quoted 33 lines
include/linux/kernel.h do { \ · line 460
Quoted 5 lines
include/linux/kernel.h +4
-1- trace_printk_fmt = fmt; \ + \ +if (!trace_printk_fmt) \ + trace_printk_fmt = fmt; \ + \But this is a static init. That is, it is done at initialization and not every time.
The change actually slows down the system.
include/linux/kernel.h __trace_printk(unsigned long ip, const char *fmt, ...) · line 476
Quoted 12 lines
include/linux/kernel.h +4
-1__trace_printk_check_format(fmt, ##args); \ __trace_printk(_THIS_IP_, trace_printk_fmt, ##args); \ } while (0)@@ -467,7 +470,10 @@ __trace_printk(unsigned long ip, const char *fmt, ...) do { \ static const char *trace_printk_fmt \ __attribute__((section("__trace_printk_fmt"))); \ - trace_printk_fmt = fmt; \ + \ + if (!trace_printk_fmt) \ + trace_printk_fmt = fmt; \ + \Same here.
-- Steve
Quoted 4 lines
include/linux/kernel.h +0
-0__ftrace_vprintk (_THIS_IP_, trace_printk_fmt, vargs); \ }while (0 )
PATCH v3 5/5 vsprintf: unify the format decoding layer for its 3 users
In reply to Frederic WeisbeckerAn new optimization is making its way to ftrace. Its purpose is to make ftrace_printk() consuming less memory and be faster.
Written by Lai Jiangshan, the approach is to delay the formatting job from tracing time to output time.
Currently, a call to ftrace_printk will format the whole string and insert it into the ring buffer. Then you can read it on /debug/tracing/trace file.
The new implementation stores the address of the format string and the binary parameters into the ring buffer, making the packet more compact and faster to insert.
Later, when the user exports the traces, the format string is retrieved with the binary parameters and the formatting job is eventually done.
The new implementation rewrites a lot of format decoding bits from vsnprintf() function, making now 3 differents functions to maintain in their duplicated parts of printf format decoding bits.
Suggested by Ingo Molnar, this patch tries to factorize the most possible common bits from these functions.
The real common part between them is the format decoding. Although they do somewhat similar jobs, their way to export or import the parameters is very different. Thus, only the decoding layer is extracted, unless you see other parts that could be worth factorized.
Changes in V2:
- Address a suggestion from Linus to group the format_decode() parameters inside a structure.
Changes in v3:
- Address other cleanups suggested by Ingo and Linus such as passing the printf_spec struct to the format helpers: pointer()/number()/string() Note that this struct is passed by copy and not by address. This is to avoid side effects because these functions often change these values and the changes shoudn't be persistant when a callee helper returns.
It would be too risky.
- Various cleanups (code alignement, switch/case instead of if/else fountains).
- Fix a bug that printed the first format specifier following a %p
Changes in v4:
- drop unapropriate const qualifier loss while casting fmt to a char * (thanks to Vegard Nossum for having pointed this out).
--- lib/vsprintf.c | 976 +++++++++++++++++++++++++++++++------------------------- 1 files changed, 543 insertions(+), 433 deletions(-)
lib/vsprintf.c +541 -431
@@ -396,7 +396,38 @@ static noinline char* put_dec(char *buf, unsigned long long num)#define SMALL 32/* Must be 32 == 0x20 */ #define SPECIAL 64/* 0x */ -static char *number (char *buf,char *end,unsigned long long num,int base,int size,int precision,int type) +enum format_type { +FORMAT_TYPE_NONE ,/* Just a string part */ +FORMAT_TYPE_WITDH , +FORMAT_TYPE_PRECISION , +FORMAT_TYPE_CHAR , +FORMAT_TYPE_STR , +FORMAT_TYPE_PTR , +FORMAT_TYPE_PERCENT_CHAR , +FORMAT_TYPE_INVALID , +FORMAT_TYPE_LONG_LONG , +FORMAT_TYPE_ULONG , +FORMAT_TYPE_LONG , +FORMAT_TYPE_USHORT , +FORMAT_TYPE_SHORT , +FORMAT_TYPE_UINT , +FORMAT_TYPE_INT , +FORMAT_TYPE_NRCHARS , +FORMAT_TYPE_SIZE_T , +FORMAT_TYPE_PTRDIFF +}; + +struct printf_spec { +enum format_type type; +int flags;/* flags to number() */ +int field_width;/* width of output field */ +int base; +int precision;/* # of digits/chars */ +int qualifier; +}; + +static char *number (char *buf,char *end,unsigned long long num, +struct printf_spec spec) {/* we are called with base 8, 10 or 16, only, thus don't need "G..." */ static const char digits[16 ] ="0123456789ABCDEF" ;/* "GHIJKLMNOPQRSTUVWXYZ"; */
@@ -404,32 +435,32 @@ static char *number(char *buf, char *end, unsigned long long num, int base, intchar tmp[66 ];char sign;char locase; -int need_pfx = ((type &SPECIAL ) && base !=10 ); +int need_pfx = ((spec.flags &SPECIAL ) && spec.base !=10 );int i;/* locase = 0 or 0x20. ORing digits or letters with 'locase' * produces same digits or (maybe lowercased) letters */ - locase = (type &SMALL ); -if (type &LEFT ) - type &= ~ZEROPAD ; + locase = (spec.flags &SMALL ); +if (spec.flags &LEFT ) + spec.flags &= ~ZEROPAD ; sign =0 ; -if (type &SIGN ) { +if (spec.flags &SIGN ) {if ((signed long long ) num <0 ) { sign ='-' ; num = - (signed long long ) num; - size--; - }else if (type &PLUS ) { + spec.field_width--; + }else if (spec.flags &PLUS ) { sign ='+' ; - size--; - }else if (type &SPACE ) { + spec.field_width--; + }else if (spec.flags &SPACE ) { sign =' ' ; - size--; + spec.field_width--; } }if (need_pfx) { - size--; -if (base ==16 ) - size--; + spec.field_width--; +if (spec.base ==16 ) + spec.field_width--; }/* generate full string in tmp[], in reverse order */
@@ -441,10 +472,10 @@ static char *number(char *buf, char *end, unsigned long long num, int base, inttmp[i++] = (digits[do_div(num,base)] | locase); } while (num != 0); */ - else if (base != 10) { /* 8 or 16 */ -int mask = base -1 ; + else if (spec.base != 10) { /* 8 or 16 */ +int mask = spec.base -1 ;int shift =3 ; -if (base ==16 ) shift =4 ; +if (spec.base ==16 ) shift =4 ; do { tmp[i++] = (digits[((unsigned char)num) & mask] | locase); num >>= shift;
@@ -454,12 +485,12 @@ static char *number(char *buf, char *end, unsigned long long num, int base, int }/* printing 100 using %2d gives "100", not "00" */ -if (i > precision) - precision = i; +if (i > spec.precision) + spec.precision = i;/* leading space padding */ - size -= precision; -if (!(type & (ZEROPAD +LEFT ))) { -while (--size >=0 ) { + spec.field_width -= spec.precision; +if (!(spec.flags & (ZEROPAD +LEFT ))) { +while (--spec.field_width >=0 ) {if (buf < end) *buf =' ' ; ++buf;
@@ -476,23 +507,23 @@ static char *number(char *buf, char *end, unsigned long long num, int base, intif (buf < end) *buf ='0' ; ++buf; -if (base ==16 ) { +if (spec.base ==16 ) {if (buf < end) *buf = ('X' | locase); ++buf; } }/* zero or space padding */ -if (!(type &LEFT )) { -char c = (type &ZEROPAD ) ?'0' :' ' ; -while (--size >=0 ) { +if (!(spec.flags &LEFT )) { +char c = (spec.flags &ZEROPAD ) ?'0' :' ' ; +while (--spec.field_width >=0 ) {if (buf < end) *buf = c; ++buf; } }/* hmm even more zero padding? */ -while (i <= --precision) { +while (i <= --spec.precision) {if (buf < end) *buf ='0' ; ++buf;
@@ -504,7 +535,7 @@ static char *number(char *buf, char *end, unsigned long long num, int base, int ++buf; }/* trailing space padding */ -while (--size >=0 ) { +while (--spec.field_width >=0 ) {if (buf < end) *buf =' ' ; ++buf;
@@ -512,17 +543,17 @@ static char *number(char *buf, char *end, unsigned long long num, int base, intreturn buf; } -static char *string (char *buf,char *end,char *s,int field_width,int precision,int flags) +static char *string (char *buf,char *end,char *s,struct printf_spec spec) {int len, i;if ((unsigned long )s <PAGE_SIZE ) s ="<NULL>" ; - len =strnlen (s, precision); + len =strnlen (s, spec.precision); -if (!(flags &LEFT )) { -while (len < field_width--) { +if (!(spec.flags &LEFT )) { +while (len < spec.field_width--) {if (buf < end) *buf =' ' ; ++buf;
@@ -533,7 +564,7 @@ static char *string(char *buf, char *end, char *s, int field_width, int precisio *buf = *s; ++buf; ++s; } -while (len < field_width--) { +while (len < spec.field_width--) {if (buf < end) *buf =' ' ; ++buf;
@@ -541,21 +572,24 @@ static char *string(char *buf, char *end, char *s, int field_width, int precisioreturn buf; } -static char *symbol_string (char *buf,char *end,void *ptr,int field_width,int precision,int flags) +static char *symbol_string (char *buf,char *end,void *ptr, +struct printf_spec spec) {unsigned long value = (unsigned long ) ptr;#ifdef CONFIG_KALLSYMS char sym[KSYM_SYMBOL_LEN ];sprint_symbol (sym, value); -return string (buf, end, sym, field_width, precision, flags); +return string (buf, end, sym, spec);#else - field_width =2 *sizeof (void *); - flags |=SPECIAL |SMALL |ZEROPAD ; -return number (buf, end, value,16 , field_width, precision, flags); + spec.field_width =2 *sizeof (void *); + spec.flags |=SPECIAL |SMALL |ZEROPAD ; + spec.base =16 ; +return number (buf, end, value, spec);#endif } -static char *resource_string (char *buf,char *end,struct resource *res,int field_width,int precision,int flags) +static char *resource_string (char *buf,char *end,struct resource *res, +struct printf_spec spec) {#ifndef IO_RSRC_PRINTK_SIZE #define IO_RSRC_PRINTK_SIZE 4
@@ -564,7 +598,11 @@ static char *resource_string(char *buf, char *end, struct resource *res, int fie#ifndef MEM_RSRC_PRINTK_SIZE #define MEM_RSRC_PRINTK_SIZE 8#endif - +struct printf_spec num_spec = { + .base =16 , + .precision =-1 , + .flags =SPECIAL |SMALL |ZEROPAD , + };/* room for the actual numbers, the two "0x", -, [, ] and the final zero */ char sym[4 *sizeof (resource_size_t) +8 ];char *p = sym, *pend = sym +sizeof (sym);
@@ -576,17 +614,18 @@ static char *resource_string(char *buf, char *end, struct resource *res, int fie size =MEM_RSRC_PRINTK_SIZE ; *p++ ='[' ; - p =number (p, pend, res->start,16 , size,-1 ,SPECIAL |SMALL |ZEROPAD ); + num_spec.field_width = size; + p =number (p, pend, res->start, num_spec); *p++ ='-' ; - p =number (p, pend, res->end,16 , size,-1 ,SPECIAL |SMALL |ZEROPAD ); + p =number (p, pend, res->end, num_spec); *p++ =']' ; *p =0 ; -return string (buf, end, sym, field_width, precision, flags); +return string (buf, end, sym, spec); } -static char *mac_address_string (char *buf,char *end,u8 *addr,int field_width, -int precision,int flags) +static char *mac_address_string (char *buf,char *end,u8 *addr, +struct printf_spec spec) {char mac_addr[6 *3 ];/* (6 * 2 hex digits), 5 colons and trailing zero */ char *p = mac_addr;
@@ -594,16 +633,17 @@ static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,for (i =0 ; i <6 ; i++) { p =pack_hex_byte (p, addr[i]); -if (!(flags &SPECIAL ) && i !=5 ) +if (!(spec.flags &SPECIAL ) && i !=5 ) *p++ =':' ; } *p ='\0' ; + spec.flags &= ~SPECIAL ; -return string (buf, end, mac_addr, field_width, precision, flags & ~SPECIAL ); +return string (buf, end, mac_addr, spec); } -static char *ip6_addr_string (char *buf,char *end,u8 *addr,int field_width, -int precision,int flags) +static char *ip6_addr_string (char *buf,char *end,u8 *addr, +struct printf_spec spec) {char ip6_addr[8 *5 ];/* (8 * 4 hex digits), 7 colons and trailing zero */ char *p = ip6_addr;
@@ -612,16 +652,17 @@ static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width,for (i =0 ; i <8 ; i++) { p =pack_hex_byte (p, addr[2 * i]); p =pack_hex_byte (p, addr[2 * i +1 ]); -if (!(flags &SPECIAL ) && i !=7 ) +if (!(spec.flags &SPECIAL ) && i !=7 ) *p++ =':' ; } *p ='\0' ; + spec.flags &= ~SPECIAL ; -return string (buf, end, ip6_addr, field_width, precision, flags & ~SPECIAL ); +return string (buf, end, ip6_addr, spec); } -static char *ip4_addr_string (char *buf,char *end,u8 *addr,int field_width, -int precision,int flags) +static char *ip4_addr_string (char *buf,char *end,u8 *addr, +struct printf_spec spec) {char ip4_addr[4 *4 ];/* (4 * 3 decimal digits), 3 dots and trailing zero */ char temp[3 ];/* hold each IP quad in reverse order */
@@ -637,8 +678,9 @@ static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width, *p++ ='.' ; } *p ='\0' ; + spec.flags &= ~SPECIAL ; -return string (buf, end, ip4_addr, field_width, precision, flags & ~SPECIAL ); +return string (buf, end, ip4_addr, spec); }/*
@@ -663,41 +705,234 @@ static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width,* function pointers are really function descriptors, which contain a * pointer to the real address. */ -static char *pointer (const char *fmt,char *buf,char *end,void *ptr,int field_width,int precision,int flags) +static char *pointer (const char *fmt,char *buf,char *end,void *ptr, +struct printf_spec spec) {if (!ptr) -return string (buf, end,"(null)" , field_width, precision, flags); +return string (buf, end,"(null)" , spec);switch (*fmt) {case 'F' : ptr =dereference_function_descriptor (ptr);/* Fallthrough */ case 'S' : -return symbol_string (buf, end, ptr, field_width, precision, flags); +return symbol_string (buf, end, ptr, spec);case 'R' : -return resource_string (buf, end, ptr, field_width, precision, flags); +return resource_string (buf, end, ptr, spec);case 'm' : - flags |=SPECIAL ; + spec.flags |=SPECIAL ;/* Fallthrough */ case 'M' : -return mac_address_string (buf, end, ptr, field_width, precision, flags); +return mac_address_string (buf, end, ptr, spec);case 'i' : - flags |=SPECIAL ; + spec.flags |=SPECIAL ;/* Fallthrough */ case 'I' :if (fmt[1 ] =='6' ) -return ip6_addr_string (buf, end, ptr, field_width, precision, flags); +return ip6_addr_string (buf, end, ptr, spec);if (fmt[1 ] =='4' ) -return ip4_addr_string (buf, end, ptr, field_width, precision, flags); - flags &= ~SPECIAL ; +return ip4_addr_string (buf, end, ptr, spec); + spec.flags &= ~SPECIAL ; +break ; + } + spec.flags |=SMALL ; +if (spec.field_width ==-1 ) { + spec.field_width =2 *sizeof (void *); + spec.flags |=ZEROPAD ; + } + spec.base =16 ; + +return number (buf, end, (unsigned long ) ptr, spec); +} + +/* +* Helper function to decode printf style format. +* Each call decode a token from the format and return the +* number of characters read (or likely the delta where it wants +* to go on the next call). +* The decoded token is returned through the parameters +* +* 'h', 'l', or 'L' for integer fields +* 'z' support added 23/7/1999 S.H. +* 'z' changed to 'Z' --davidm 1/25/99 +* 't' added for ptrdiff_t +* +* @fmt: the format string +* @type of the token returned +* @flags: various flags such as +, -, # tokens.. +* @field_width: overwritten width +* @base: base of the number (octal, hex, ...) +* @precision: precision of a number +* @qualifier: qualifier of a number (long, size_t, ...) +*/ +static int format_decode (const char *fmt,struct printf_spec *spec) +{ +const char *start = fmt; +bool sign = false; + +/* we finished early by reading the field width */ +if (spec->type ==FORMAT_TYPE_WITDH ) { +if (spec->field_width <0 ) { + spec->field_width = -spec->field_width; + spec->flags |=LEFT ; + } + spec->type =FORMAT_TYPE_NONE ; + goto precision; + } + +/* we finished early by reading the precision */ +if (spec->type ==FORMAT_TYPE_PRECISION ) { +if (spec->precision <0 ) + spec->precision =0 ; + + spec->type =FORMAT_TYPE_NONE ; + goto qualifier; + } + +/* By default */ + spec->type =FORMAT_TYPE_NONE ; + +for (; *fmt ; ++fmt) { +if (*fmt =='%' ) +break ; + } + +/* Return the current non-format string */ +if (fmt != start || !*fmt) +return fmt - start; + +/* Process flags */ + spec->flags =0 ; + +while (1 ) {/* this also skips first '%' */ +bool found = true; + + ++fmt; + +switch (*fmt) { +case '-' : spec->flags |=LEFT ;break ; +case '+' : spec->flags |=PLUS ;break ; +case ' ' : spec->flags |=SPACE ;break ; +case '#' : spec->flags |=SPECIAL ;break ; +case '0' : spec->flags |=ZEROPAD ;break ; +default : found = false; + } + +if (!found) +break ; + } + +/* get field width */ + spec->field_width =-1 ; + +if (isdigit (*fmt)) + spec->field_width =skip_atoi (&fmt); +else if (*fmt =='*' ) { +/* it's the next argument */ + spec->type =FORMAT_TYPE_WITDH ; +return ++fmt - start; + } + +precision: +/* get the precision */ + spec->precision =-1 ; +if (*fmt =='.' ) { + ++fmt; +if (isdigit (*fmt)) { + spec->precision =skip_atoi (&fmt); +if (spec->precision <0 ) + spec->precision =0 ; + }else if (*fmt =='*' ) { +/* it's the next argument */ + spec->type =FORMAT_TYPE_WITDH ; +return ++fmt - start; + } + } + +qualifier: +/* get the conversion qualifier */ + spec->qualifier =-1 ; +if (*fmt =='h' || *fmt =='l' || *fmt =='L' || + *fmt =='Z' || *fmt =='z' || *fmt =='t' ) { + spec->qualifier = *fmt; + ++fmt; +if (spec->qualifier =='l' && *fmt =='l' ) { + spec->qualifier ='L' ; + ++fmt; + } + } + +/* default base */ + spec->base =10 ; +switch (*fmt) { +case 'c' : + spec->type =FORMAT_TYPE_CHAR ; +return ++fmt - start; + +case 's' : + spec->type =FORMAT_TYPE_STR ; +return ++fmt - start; + +case 'p' : + spec->type =FORMAT_TYPE_PTR ; +return fmt - start; +/* skip alnum */ + +case 'n' : + spec->type =FORMAT_TYPE_NRCHARS ; +return ++fmt - start; + +case '%' : + spec->type =FORMAT_TYPE_PERCENT_CHAR ; +return ++fmt - start; + +/* integer number formats - set up the flags and "break" */ +case 'o' : + spec->base =8 ;break ; + +case 'x' : + spec->flags |=SMALL ; + +case 'X' : + spec->base =16 ; +break ; + +case 'd' : +case 'i' : + sign = true; +case 'u' : +break ; + +default : + spec->type =FORMAT_TYPE_INVALID ; +return fmt - start; } - flags |=SMALL ; -if (field_width ==-1 ) { - field_width =2 *sizeof (void *); - flags |=ZEROPAD ; + +if (spec->qualifier =='L' ) + spec->type =FORMAT_TYPE_LONG_LONG ; +else if (spec->qualifier =='l' ) { +if (sign) + spec->type =FORMAT_TYPE_LONG ; +else + spec->type =FORMAT_TYPE_ULONG ; + }else if (spec->qualifier =='Z' || spec->qualifier =='z' ) { + spec->type =FORMAT_TYPE_SIZE_T ; + }else if (spec->qualifier =='t' ) { + spec->type =FORMAT_TYPE_PTRDIFF ; + }else if (spec->qualifier =='h' ) { +if (sign) + spec->type =FORMAT_TYPE_SHORT ; +else + spec->type =FORMAT_TYPE_USHORT ; + }else { +if (sign) + spec->type =FORMAT_TYPE_INT ; +else + spec->type =FORMAT_TYPE_UINT ; } -return number (buf, end, (unsigned long ) ptr,16 , field_width, precision, flags); + +return ++fmt - start; }/**
@@ -726,18 +961,9 @@ static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int fieldint vsnprintf (char *buf,size_t size,const char *fmt,va_list args) {unsigned long long num; -int base;char *str, *end, c; - -int flags;/* flags to number() */ - -int field_width;/* width of output field */ -int precision;/* min. # of digits for integers; max -number of chars for from string */ -int qualifier;/* 'h', 'l', or 'L' for integer fields */ -/* 'z' support added 23/7/1999 S.H. */ -/* 'z' changed to 'Z' --davidm 1/25/99 */ -/* 't' added for ptrdiff_t */ +int read; +struct printf_spec spec = {0 };/* Reject out-of-range values early. Large positive sizes are used for unknown buffer sizes. */
@@ -758,184 +984,144 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) size = end - buf; } -for (; *fmt ; ++fmt) { -if (*fmt !='%' ) { -if (str < end) - *str = *fmt; - ++str; -continue ; - } +while (*fmt) { +const char *old_fmt = fmt; -/* process flags */ - flags =0 ; - repeat: - ++fmt;/* this also skips first '%' */ -switch (*fmt) { -case '-' : flags |=LEFT ; goto repeat; -case '+' : flags |=PLUS ; goto repeat; -case ' ' : flags |=SPACE ; goto repeat; -case '#' : flags |=SPECIAL ; goto repeat; -case '0' : flags |=ZEROPAD ; goto repeat; - } + read =format_decode (fmt, &spec); -/* get field width */ - field_width =-1 ; -if (isdigit (*fmt)) - field_width =skip_atoi (&fmt); -else if (*fmt =='*' ) { - ++fmt; -/* it's the next argument */ - field_width =va_arg (args, int); -if (field_width <0 ) { - field_width = -field_width; - flags |=LEFT ; - } - } + fmt += read; -/* get the precision */ - precision =-1 ; -if (*fmt =='.' ) { - ++fmt; -if (isdigit (*fmt)) - precision =skip_atoi (&fmt); -else if (*fmt =='*' ) { - ++fmt; -/* it's the next argument */ - precision =va_arg (args, int); +switch (spec.type) { +case FORMAT_TYPE_NONE : { +int copy = read; +if (str < end) { +if (copy > end - str) + copy = end - str; +memcpy (str, old_fmt, copy); } -if (precision <0 ) - precision =0 ; + str += read; +break ; } -/* get the conversion qualifier */ - qualifier =-1 ; -if (*fmt =='h' || *fmt =='l' || *fmt =='L' || - *fmt =='Z' || *fmt =='z' || *fmt =='t' ) { - qualifier = *fmt; - ++fmt; -if (qualifier =='l' && *fmt =='l' ) { - qualifier ='L' ; - ++fmt; - } - } +case FORMAT_TYPE_WITDH : + spec.field_width =va_arg (args, int); +break ; -/* default base */ - base =10 ; +case FORMAT_TYPE_PRECISION : + spec.precision =va_arg (args, int); +break ; -switch (*fmt) { -case 'c' : -if (!(flags &LEFT )) { -while (--field_width >0 ) { -if (str < end) - *str =' ' ; - ++str; - } - } - c = (unsigned char )va_arg (args, int); -if (str < end) - *str = c; - ++str; -while (--field_width >0 ) { +case FORMAT_TYPE_CHAR : +if (!(spec.flags &LEFT )) { +while (--spec.field_width >0 ) {if (str < end) *str =' ' ; ++str; - } -continue ; - -case 's' : - str = string(str, end, va_arg(args, char *), field_width, precision, flags); -continue ; - -case 'p' : - str =pointer (fmt+1 , str, end, - va_arg(args, void *), - field_width, precision, flags); -/* Skip all alphanumeric pointer suffixes */ -while (isalnum (fmt[1 ])) - fmt++; -continue ; - -case 'n' : -/* FIXME: -* What does C99 say about the overflow case here? */ -if (qualifier =='l' ) { - long * ip = va_arg(args, long *); - *ip = (str - buf); - }else if (qualifier =='Z' || qualifier =='z' ) { - size_t * ip = va_arg(args, size_t *); - *ip = (str - buf); - }else { - int * ip = va_arg(args, int *); - *ip = (str - buf); - } -continue ; -case '%' : + } + } + c = (unsigned char )va_arg (args, int); +if (str < end) + *str = c; + ++str; +while (--spec.field_width >0 ) {if (str < end) - *str ='%' ; + *str =' ' ; ++str; -continue ; + } +break ; -/* integer number formats - set up the flags and "break" */ -case 'o' : - base =8 ; -break ; +case FORMAT_TYPE_STR : + str = string(str, end, va_arg(args, char *), spec); +break ; -case 'x' : - flags |=SMALL ; -case 'X' : - base =16 ; -break ; +case FORMAT_TYPE_PTR : + str = pointer(fmt+1, str, end, va_arg(args, void *), + spec); +while (isalnum (*fmt)) + fmt++; +break ; -case 'd' : -case 'i' : - flags |=SIGN ; -case 'u' : -break ; +case FORMAT_TYPE_PERCENT_CHAR : +if (str < end) + *str ='%' ; + ++str; +break ; -default : +case FORMAT_TYPE_INVALID : +if (str < end) + *str ='%' ; + ++str; +if (*fmt) {if (str < end) - *str ='%' ; + *str = *fmt; ++str; -if (*fmt) { -if (str < end) - *str = *fmt; - ++str; - }else { - --fmt; - } -continue ; + }else { + --fmt; + } +break ; + +case FORMAT_TYPE_NRCHARS : { +int qualifier = spec.qualifier; + +if (qualifier =='l' ) { + long *ip = va_arg(args, long *); + *ip = (str - buf); + }else if (qualifier =='Z' || + qualifier =='z' ) { + size_t *ip = va_arg(args, size_t *); + *ip = (str - buf); + }else { + int *ip = va_arg(args, int *); + *ip = (str - buf); + } +break ; } -if (qualifier =='L' ) - num = va_arg(args, long long); -else if (qualifier =='l' ) { - num = va_arg(args, unsigned long); -if (flags &SIGN ) - num = (signed long ) num; - }else if (qualifier =='Z' || qualifier =='z' ) { - num =va_arg (args, size_t); - }else if (qualifier =='t' ) { - num =va_arg (args, ptrdiff_t); - }else if (qualifier =='h' ) { - num = (unsigned short )va_arg (args, int); -if (flags &SIGN ) - num = (signed short ) num; - }else { - num = va_arg(args, unsigned int); -if (flags &SIGN ) - num = (signed int ) num; + +default : +switch (spec.type) { +case FORMAT_TYPE_LONG_LONG : + num = va_arg(args, long long); +break ; +case FORMAT_TYPE_ULONG : + num = va_arg(args, unsigned long); +break ; +case FORMAT_TYPE_LONG : + num =va_arg (args, long); +break ; +case FORMAT_TYPE_SIZE_T : + num =va_arg (args, size_t); +break ; +case FORMAT_TYPE_PTRDIFF : + num =va_arg (args, ptrdiff_t); +break ; +case FORMAT_TYPE_USHORT : + num = (unsigned short )va_arg (args, int); +break ; +case FORMAT_TYPE_SHORT : + num = (short )va_arg (args, int); +break ; +case FORMAT_TYPE_UINT : + num = va_arg(args, unsigned int); +break ; +default : + num = va_arg(args, unsigned int); + } + + str =number (str, end, num, spec); } - str =number (str, end, num, base, - field_width, precision, flags); } +if (size >0 ) {if (str < end) *str ='\0' ;else end[-1 ] ='\0' ; } +/* the trailing null byte doesn't count towards the total */ return str-buf; + }EXPORT_SYMBOL (vsnprintf);
@@ -1084,8 +1270,9 @@ EXPORT_SYMBOL(sprintf);*/ int vbin_printf (u32 *bin_buf,size_t size,const char *fmt,va_list args) { +struct printf_spec spec = {0 };char *str, *end; -int qualifier; +int read; str = (char *)bin_buf; end = (char *)(bin_buf + size);
@@ -1110,57 +1297,26 @@ do { \ str +=sizeof (type); \ }while (0 ) -for (; *fmt ; ++fmt) { -if (*fmt !='%' ) -continue ; -repeat: -/* parse flags */ - ++fmt;/* this also skips first '%' */ -if (*fmt =='-' || *fmt =='+' || *fmt ==' ' - || *fmt =='#' || *fmt =='0' ) - goto repeat; +while (*fmt) { + read =format_decode (fmt, &spec); -/* parse field width */ -if (isdigit (*fmt)) -skip_atoi (&fmt); -else if (*fmt =='*' ) { - ++fmt; -/* it's the next argument */ -save_arg (int); - } + fmt += read; -/* parse the precision */ -if (*fmt =='.' ) { - ++fmt; -if (isdigit (*fmt)) -skip_atoi (&fmt); -else if (*fmt =='*' ) { - ++fmt; -/* it's the next argument */ -save_arg (int); - } - } +switch (spec.type) { +case FORMAT_TYPE_NONE : +break ; -/* parse the conversion qualifier */ - qualifier =-1 ; -if (*fmt =='h' || *fmt =='l' || *fmt =='L' || - *fmt =='Z' || *fmt =='z' || *fmt =='t' ) { - qualifier = *fmt; - ++fmt; -if (qualifier =='l' && *fmt =='l' ) { - qualifier ='L' ; - ++fmt; - } - } +case FORMAT_TYPE_WITDH : +case FORMAT_TYPE_PRECISION : +save_arg (int); +break ; -/* parse format type */ -switch (*fmt) { -case 'c' : +case FORMAT_TYPE_CHAR :save_arg (char); -continue ; -case 's' : { -/* save the string argument */ +break ; + +case FORMAT_TYPE_STR : { const char *save_str = va_arg(args, char *);size_t len; if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
@@ -1170,16 +1326,27 @@ repeat:if (str + len +1 < end)memcpy (str, save_str, len +1 ); str += len +1 ; -continue ; +break ; } -case 'p' : + +case FORMAT_TYPE_PTR : save_arg(void *);/* skip all alphanumeric pointer suffixes */ -while (isalnum (fmt[1 ])) +while (isalnum (*fmt)) fmt++; -continue ; -case 'n' : { +break ; + +case FORMAT_TYPE_PERCENT_CHAR : +break ; + +case FORMAT_TYPE_INVALID : +if (!*fmt) + --fmt; +break ; + +case FORMAT_TYPE_NRCHARS : {/* skip %n 's argument */ +int qualifier = spec.qualifier;void *skip_arg;if (qualifier =='l' ) skip_arg = va_arg(args, long *);
@@ -1187,37 +1354,37 @@ repeat: skip_arg = va_arg(args, size_t *); else skip_arg = va_arg(args, int *); -continue ; +break ; } -case 'o' : -case 'x' : -case 'X' : -case 'd' : -case 'i' : -case 'u' : -/* save arg for case: 'o', 'x', 'X', 'd', 'i', 'u' */ -if (qualifier =='L' ) + +default : +switch (spec.type) { + +case FORMAT_TYPE_LONG_LONG : save_arg(long long); -else if (qualifier =='l' ) +break ; +case FORMAT_TYPE_ULONG : +case FORMAT_TYPE_LONG : save_arg(unsigned long); -else if (qualifier =='Z' || qualifier =='z' ) +break ; +case FORMAT_TYPE_SIZE_T :save_arg (size_t); -else if (qualifier =='t' ) +break ; +case FORMAT_TYPE_PTRDIFF :save_arg (ptrdiff_t); -else if (qualifier =='h' ) +break ; +case FORMAT_TYPE_USHORT : +case FORMAT_TYPE_SHORT :save_arg (short); -else +break ; +default :save_arg (int); -continue ; -default : -if (!*fmt) - --fmt; -continue ; + } } } -#undef save_arg -return (u32 *)(PTR_ALIGN (str,sizeof (u32))) - bin_buf; + +#undef save_arg }EXPORT_SYMBOL_GPL (vbin_printf);
@@ -1249,14 +1416,10 @@ EXPORT_SYMBOL_GPL(vbin_printf);int bstr_printf (char *buf,size_t size,const char *fmt,const u32 *bin_buf) {unsigned long long num; -int base;char *str, *end, c;const char *args = (const char *)bin_buf; -int flags; -int field_width; -int precision; -int qualifier; +struct printf_spec spec = {0 };if (unlikely ((int ) size <0 )) {/* There can be only one.. */
@@ -1290,84 +1453,37 @@ int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) size = end - buf; } -for (; *fmt ; ++fmt) { -if (*fmt !='%' ) { -if (str < end) - *str = *fmt; - ++str; -continue ; - } +while (*fmt) { +int read; +const char *old_fmt = fmt; -/* process flags */ - flags =0 ; -repeat: - ++fmt;/* this also skips first '%' */ -switch (*fmt) { -case '-' : - flags |=LEFT ; - goto repeat; -case '+' : - flags |=PLUS ; - goto repeat; -case ' ' : - flags |=SPACE ; - goto repeat; -case '#' : - flags |=SPECIAL ; - goto repeat; -case '0' : - flags |=ZEROPAD ; - goto repeat; - } + read =format_decode (fmt, &spec); -/* get field width */ - field_width =-1 ; -if (isdigit (*fmt)) - field_width =skip_atoi (&fmt); -else if (*fmt =='*' ) { - ++fmt; -/* it's the next argument */ - field_width =get_arg (int); -if (field_width <0 ) { - field_width = -field_width; - flags |=LEFT ; - } - } + fmt += read; -/* get the precision */ - precision =-1 ; -if (*fmt =='.' ) { - ++fmt; -if (isdigit (*fmt)) - precision =skip_atoi (&fmt); -else if (*fmt =='*' ) { - ++fmt; -/* it's the next argument */ - precision =get_arg (int); +switch (spec.type) { +case FORMAT_TYPE_NONE : { +int copy = read; +if (str < end) { +if (copy > end - str) + copy = end - str; +memcpy (str, old_fmt, copy); } -if (precision <0 ) - precision =0 ; + str += read; +break ; } -/* get the conversion qualifier */ - qualifier =-1 ; -if (*fmt =='h' || *fmt =='l' || *fmt =='L' || - *fmt =='Z' || *fmt =='z' || *fmt =='t' ) { - qualifier = *fmt; - ++fmt; -if (qualifier =='l' && *fmt =='l' ) { - qualifier ='L' ; - ++fmt; - } - } +case FORMAT_TYPE_WITDH : + spec.field_width =get_arg (int); +break ; -/* default base */ - base =10 ; +case FORMAT_TYPE_PRECISION : + spec.precision =get_arg (int); +break ; -switch (*fmt) { -case 'c' : -if (!(flags &LEFT )) { -while (--field_width >0 ) { +case FORMAT_TYPE_CHAR : +if (!(spec.flags &LEFT )) { +while (--spec.field_width >0 ) {if (str < end) *str =' ' ; ++str;
@@ -1377,58 +1493,34 @@ repeat:if (str < end) *str = c; ++str; -while (--field_width >0 ) { +while (--spec.field_width >0 ) {if (str < end) *str =' ' ; ++str; } -continue ; +break ; -case 's' :{ +case FORMAT_TYPE_STR : {const char *str_arg = args;size_t len =strlen (str_arg); args += len +1 ; - str =string (str, end, (char *)str_arg, field_width, - precision, flags); -continue ; + str =string (str, end, (char *)str_arg, spec); +break ; } -case 'p' : - str = pointer(fmt+1, str, end, get_arg(void *), - field_width, precision, flags); -/* Skip all alphanumeric pointer suffixes */ -while (isalnum (fmt[1 ])) +case FORMAT_TYPE_PTR : + str = pointer(fmt+1, str, end, get_arg(void *), spec); +while (isalnum (*fmt)) fmt++; -continue ; - -case 'n' : -/* skip %n */ -continue ; +break ; -case '%' : +case FORMAT_TYPE_PERCENT_CHAR :if (str < end) *str ='%' ; ++str; -continue ; - -/* integer number formats - set up the flags and "break" */ -case 'o' : - base =8 ; -break ; - -case 'x' : - flags |=SMALL ; -case 'X' : - base =16 ;break ; -case 'd' : -case 'i' : - flags |=SIGN ; -case 'u' : -break ; - -default : +case FORMAT_TYPE_INVALID :if (str < end) *str ='%' ; ++str;
@@ -1439,36 +1531,54 @@ repeat: }else { --fmt; } -continue ; - } -if (qualifier =='L' ) - num = get_arg(long long); -else if (qualifier =='l' ) { - num = get_arg(unsigned long); -if (flags &SIGN ) - num = (signed long ) num; - }else if (qualifier =='Z' || qualifier =='z' ) { - num =get_arg (size_t); - }else if (qualifier =='t' ) { - num =get_arg (ptrdiff_t); - }else if (qualifier =='h' ) { - num = (unsigned short )get_arg (short); -if (flags &SIGN ) - num = (signed short ) num; - }else { - num = get_arg(unsigned int); -if (flags &SIGN ) - num = (signed int ) num; +break ; + +case FORMAT_TYPE_NRCHARS : +/* skip */ +break ; + +default : +switch (spec.type) { + +case FORMAT_TYPE_LONG_LONG : + num = get_arg(long long); +break ; +case FORMAT_TYPE_ULONG : + num = get_arg(unsigned long); +break ; +case FORMAT_TYPE_LONG : + num = get_arg(unsigned long); +break ; +case FORMAT_TYPE_SIZE_T : + num =get_arg (size_t); +break ; +case FORMAT_TYPE_PTRDIFF : + num =get_arg (ptrdiff_t); +break ; +case FORMAT_TYPE_USHORT : + num = get_arg(unsigned short); +break ; +case FORMAT_TYPE_SHORT : + num =get_arg (short); +break ; +case FORMAT_TYPE_UINT : + num = get_arg(unsigned int); +break ; +default : + num =get_arg (int); + } + + str =number (str, end, num, spec); } - str =number (str, end, num, base, - field_width, precision, flags); } +if (size >0 ) {if (str < end) *str ='\0' ;else end[-1 ] ='\0' ; } +#undef get_arg/* the trailing null byte doesn't count towards the total */
Signature · 1 line
1.6.1
[tip:core/printk] vsprintf: unify the format decoding layer for its 3 users
Commit-ID: fef20d9c1380f04ba9492d6463148db07b413708 Gitweb: http://git.kernel.org/tip/fef20d9c1380f04ba9492d6463148db07b413708 Author: "Frederic Weisbecker" <[email protected]> AuthorDate: Fri, 6 Mar 2009 17:21:50 +0100 Commit: Ingo Molnar <[email protected]> CommitDate: Fri, 6 Mar 2009 17:44:27 +0100
vsprintf: unify the format decoding layer for its 3 users
An new optimization is making its way to ftrace. Its purpose is to make trace_printk() consuming less memory and be faster.
Written by Lai Jiangshan, the approach is to delay the formatting job from tracing time to output time.
Currently, a call to trace_printk() will format the whole string and insert it into the ring buffer. Then you can read it on /debug/tracing/trace file.
The new implementation stores the address of the format string and the binary parameters into the ring buffer, making the packet more compact and faster to insert.
Later, when the user exports the traces, the format string is retrieved with the binary parameters and the formatting job is eventually done.
The new implementation rewrites a lot of format decoding bits from vsnprintf() function, making now 3 differents functions to maintain in their duplicated parts of printf format decoding bits.
Suggested by Ingo Molnar, this patch tries to factorize the most possible common bits from these functions.
The real common part between them is the format decoding. Although they do somewhat similar jobs, their way to export or import the parameters is very different. Thus, only the decoding layer is extracted, unless you see other parts that could be worth factorized.
Changes in V2:
- Address a suggestion from Linus to group the format_decode() parameters inside a structure.
Changes in v3:
- Address other cleanups suggested by Ingo and Linus such as passing the printf_spec struct to the format helpers: pointer()/number()/string() Note that this struct is passed by copy and not by address. This is to avoid side effects because these functions often change these values and the changes shoudn't be persistant when a callee helper returns.
It would be too risky.
- Various cleanups (code alignement, switch/case instead of if/else fountains).
- Fix a bug that printed the first format specifier following a %p
Changes in v4:
- drop unapropriate const qualifier loss while casting fmt to a char * (thanks to Vegard Nossum for having pointed this out).
LKML-Reference: <[email protected]>
--- lib/vsprintf.c | 976 +++++++++++++++++++++++++++++++------------------------- 1 files changed, 543 insertions(+), 433 deletions(-)
lib/vsprintf.c +541 -431
@@ -396,7 +396,38 @@ static noinline char* put_dec(char *buf, unsigned long long num)#define SMALL 32/* Must be 32 == 0x20 */ #define SPECIAL 64/* 0x */ -static char *number (char *buf,char *end,unsigned long long num,int base,int size,int precision,int type) +enum format_type { +FORMAT_TYPE_NONE ,/* Just a string part */ +FORMAT_TYPE_WITDH , +FORMAT_TYPE_PRECISION , +FORMAT_TYPE_CHAR , +FORMAT_TYPE_STR , +FORMAT_TYPE_PTR , +FORMAT_TYPE_PERCENT_CHAR , +FORMAT_TYPE_INVALID , +FORMAT_TYPE_LONG_LONG , +FORMAT_TYPE_ULONG , +FORMAT_TYPE_LONG , +FORMAT_TYPE_USHORT , +FORMAT_TYPE_SHORT , +FORMAT_TYPE_UINT , +FORMAT_TYPE_INT , +FORMAT_TYPE_NRCHARS , +FORMAT_TYPE_SIZE_T , +FORMAT_TYPE_PTRDIFF +}; + +struct printf_spec { +enum format_type type; +int flags;/* flags to number() */ +int field_width;/* width of output field */ +int base; +int precision;/* # of digits/chars */ +int qualifier; +}; + +static char *number (char *buf,char *end,unsigned long long num, +struct printf_spec spec) {/* we are called with base 8, 10 or 16, only, thus don't need "G..." */ static const char digits[16 ] ="0123456789ABCDEF" ;/* "GHIJKLMNOPQRSTUVWXYZ"; */
@@ -404,32 +435,32 @@ static char *number(char *buf, char *end, unsigned long long num, int base, intchar tmp[66 ];char sign;char locase; -int need_pfx = ((type &SPECIAL ) && base !=10 ); +int need_pfx = ((spec.flags &SPECIAL ) && spec.base !=10 );int i;/* locase = 0 or 0x20. ORing digits or letters with 'locase' * produces same digits or (maybe lowercased) letters */ - locase = (type &SMALL ); -if (type &LEFT ) - type &= ~ZEROPAD ; + locase = (spec.flags &SMALL ); +if (spec.flags &LEFT ) + spec.flags &= ~ZEROPAD ; sign =0 ; -if (type &SIGN ) { +if (spec.flags &SIGN ) {if ((signed long long ) num <0 ) { sign ='-' ; num = - (signed long long ) num; - size--; - }else if (type &PLUS ) { + spec.field_width--; + }else if (spec.flags &PLUS ) { sign ='+' ; - size--; - }else if (type &SPACE ) { + spec.field_width--; + }else if (spec.flags &SPACE ) { sign =' ' ; - size--; + spec.field_width--; } }if (need_pfx) { - size--; -if (base ==16 ) - size--; + spec.field_width--; +if (spec.base ==16 ) + spec.field_width--; }/* generate full string in tmp[], in reverse order */
@@ -441,10 +472,10 @@ static char *number(char *buf, char *end, unsigned long long num, int base, inttmp[i++] = (digits[do_div(num,base)] | locase); } while (num != 0); */ - else if (base != 10) { /* 8 or 16 */ -int mask = base -1 ; + else if (spec.base != 10) { /* 8 or 16 */ +int mask = spec.base -1 ;int shift =3 ; -if (base ==16 ) shift =4 ; +if (spec.base ==16 ) shift =4 ; do { tmp[i++] = (digits[((unsigned char)num) & mask] | locase); num >>= shift;
@@ -454,12 +485,12 @@ static char *number(char *buf, char *end, unsigned long long num, int base, int }/* printing 100 using %2d gives "100", not "00" */ -if (i > precision) - precision = i; +if (i > spec.precision) + spec.precision = i;/* leading space padding */ - size -= precision; -if (!(type & (ZEROPAD +LEFT ))) { -while (--size >=0 ) { + spec.field_width -= spec.precision; +if (!(spec.flags & (ZEROPAD +LEFT ))) { +while (--spec.field_width >=0 ) {if (buf < end) *buf =' ' ; ++buf;
@@ -476,23 +507,23 @@ static char *number(char *buf, char *end, unsigned long long num, int base, intif (buf < end) *buf ='0' ; ++buf; -if (base ==16 ) { +if (spec.base ==16 ) {if (buf < end) *buf = ('X' | locase); ++buf; } }/* zero or space padding */ -if (!(type &LEFT )) { -char c = (type &ZEROPAD ) ?'0' :' ' ; -while (--size >=0 ) { +if (!(spec.flags &LEFT )) { +char c = (spec.flags &ZEROPAD ) ?'0' :' ' ; +while (--spec.field_width >=0 ) {if (buf < end) *buf = c; ++buf; } }/* hmm even more zero padding? */ -while (i <= --precision) { +while (i <= --spec.precision) {if (buf < end) *buf ='0' ; ++buf;
@@ -504,7 +535,7 @@ static char *number(char *buf, char *end, unsigned long long num, int base, int ++buf; }/* trailing space padding */ -while (--size >=0 ) { +while (--spec.field_width >=0 ) {if (buf < end) *buf =' ' ; ++buf;
@@ -512,17 +543,17 @@ static char *number(char *buf, char *end, unsigned long long num, int base, intreturn buf; } -static char *string (char *buf,char *end,char *s,int field_width,int precision,int flags) +static char *string (char *buf,char *end,char *s,struct printf_spec spec) {int len, i;if ((unsigned long )s <PAGE_SIZE ) s ="<NULL>" ; - len =strnlen (s, precision); + len =strnlen (s, spec.precision); -if (!(flags &LEFT )) { -while (len < field_width--) { +if (!(spec.flags &LEFT )) { +while (len < spec.field_width--) {if (buf < end) *buf =' ' ; ++buf;
@@ -533,7 +564,7 @@ static char *string(char *buf, char *end, char *s, int field_width, int precisio *buf = *s; ++buf; ++s; } -while (len < field_width--) { +while (len < spec.field_width--) {if (buf < end) *buf =' ' ; ++buf;
@@ -541,21 +572,24 @@ static char *string(char *buf, char *end, char *s, int field_width, int precisioreturn buf; } -static char *symbol_string (char *buf,char *end,void *ptr,int field_width,int precision,int flags) +static char *symbol_string (char *buf,char *end,void *ptr, +struct printf_spec spec) {unsigned long value = (unsigned long ) ptr;#ifdef CONFIG_KALLSYMS char sym[KSYM_SYMBOL_LEN ];sprint_symbol (sym, value); -return string (buf, end, sym, field_width, precision, flags); +return string (buf, end, sym, spec);#else - field_width =2 *sizeof (void *); - flags |=SPECIAL |SMALL |ZEROPAD ; -return number (buf, end, value,16 , field_width, precision, flags); + spec.field_width =2 *sizeof (void *); + spec.flags |=SPECIAL |SMALL |ZEROPAD ; + spec.base =16 ; +return number (buf, end, value, spec);#endif } -static char *resource_string (char *buf,char *end,struct resource *res,int field_width,int precision,int flags) +static char *resource_string (char *buf,char *end,struct resource *res, +struct printf_spec spec) {#ifndef IO_RSRC_PRINTK_SIZE #define IO_RSRC_PRINTK_SIZE 4
@@ -564,7 +598,11 @@ static char *resource_string(char *buf, char *end, struct resource *res, int fie#ifndef MEM_RSRC_PRINTK_SIZE #define MEM_RSRC_PRINTK_SIZE 8#endif - +struct printf_spec num_spec = { + .base =16 , + .precision =-1 , + .flags =SPECIAL |SMALL |ZEROPAD , + };/* room for the actual numbers, the two "0x", -, [, ] and the final zero */ char sym[4 *sizeof (resource_size_t) +8 ];char *p = sym, *pend = sym +sizeof (sym);
@@ -576,17 +614,18 @@ static char *resource_string(char *buf, char *end, struct resource *res, int fie size =MEM_RSRC_PRINTK_SIZE ; *p++ ='[' ; - p =number (p, pend, res->start,16 , size,-1 ,SPECIAL |SMALL |ZEROPAD ); + num_spec.field_width = size; + p =number (p, pend, res->start, num_spec); *p++ ='-' ; - p =number (p, pend, res->end,16 , size,-1 ,SPECIAL |SMALL |ZEROPAD ); + p =number (p, pend, res->end, num_spec); *p++ =']' ; *p =0 ; -return string (buf, end, sym, field_width, precision, flags); +return string (buf, end, sym, spec); } -static char *mac_address_string (char *buf,char *end,u8 *addr,int field_width, -int precision,int flags) +static char *mac_address_string (char *buf,char *end,u8 *addr, +struct printf_spec spec) {char mac_addr[6 *3 ];/* (6 * 2 hex digits), 5 colons and trailing zero */ char *p = mac_addr;
@@ -594,16 +633,17 @@ static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,for (i =0 ; i <6 ; i++) { p =pack_hex_byte (p, addr[i]); -if (!(flags &SPECIAL ) && i !=5 ) +if (!(spec.flags &SPECIAL ) && i !=5 ) *p++ =':' ; } *p ='\0' ; + spec.flags &= ~SPECIAL ; -return string (buf, end, mac_addr, field_width, precision, flags & ~SPECIAL ); +return string (buf, end, mac_addr, spec); } -static char *ip6_addr_string (char *buf,char *end,u8 *addr,int field_width, -int precision,int flags) +static char *ip6_addr_string (char *buf,char *end,u8 *addr, +struct printf_spec spec) {char ip6_addr[8 *5 ];/* (8 * 4 hex digits), 7 colons and trailing zero */ char *p = ip6_addr;
@@ -612,16 +652,17 @@ static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width,for (i =0 ; i <8 ; i++) { p =pack_hex_byte (p, addr[2 * i]); p =pack_hex_byte (p, addr[2 * i +1 ]); -if (!(flags &SPECIAL ) && i !=7 ) +if (!(spec.flags &SPECIAL ) && i !=7 ) *p++ =':' ; } *p ='\0' ; + spec.flags &= ~SPECIAL ; -return string (buf, end, ip6_addr, field_width, precision, flags & ~SPECIAL ); +return string (buf, end, ip6_addr, spec); } -static char *ip4_addr_string (char *buf,char *end,u8 *addr,int field_width, -int precision,int flags) +static char *ip4_addr_string (char *buf,char *end,u8 *addr, +struct printf_spec spec) {char ip4_addr[4 *4 ];/* (4 * 3 decimal digits), 3 dots and trailing zero */ char temp[3 ];/* hold each IP quad in reverse order */
@@ -637,8 +678,9 @@ static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width, *p++ ='.' ; } *p ='\0' ; + spec.flags &= ~SPECIAL ; -return string (buf, end, ip4_addr, field_width, precision, flags & ~SPECIAL ); +return string (buf, end, ip4_addr, spec); }/*
@@ -663,41 +705,234 @@ static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width,* function pointers are really function descriptors, which contain a * pointer to the real address. */ -static char *pointer (const char *fmt,char *buf,char *end,void *ptr,int field_width,int precision,int flags) +static char *pointer (const char *fmt,char *buf,char *end,void *ptr, +struct printf_spec spec) {if (!ptr) -return string (buf, end,"(null)" , field_width, precision, flags); +return string (buf, end,"(null)" , spec);switch (*fmt) {case 'F' : ptr =dereference_function_descriptor (ptr);/* Fallthrough */ case 'S' : -return symbol_string (buf, end, ptr, field_width, precision, flags); +return symbol_string (buf, end, ptr, spec);case 'R' : -return resource_string (buf, end, ptr, field_width, precision, flags); +return resource_string (buf, end, ptr, spec);case 'm' : - flags |=SPECIAL ; + spec.flags |=SPECIAL ;/* Fallthrough */ case 'M' : -return mac_address_string (buf, end, ptr, field_width, precision, flags); +return mac_address_string (buf, end, ptr, spec);case 'i' : - flags |=SPECIAL ; + spec.flags |=SPECIAL ;/* Fallthrough */ case 'I' :if (fmt[1 ] =='6' ) -return ip6_addr_string (buf, end, ptr, field_width, precision, flags); +return ip6_addr_string (buf, end, ptr, spec);if (fmt[1 ] =='4' ) -return ip4_addr_string (buf, end, ptr, field_width, precision, flags); - flags &= ~SPECIAL ; +return ip4_addr_string (buf, end, ptr, spec); + spec.flags &= ~SPECIAL ; +break ; + } + spec.flags |=SMALL ; +if (spec.field_width ==-1 ) { + spec.field_width =2 *sizeof (void *); + spec.flags |=ZEROPAD ; + } + spec.base =16 ; + +return number (buf, end, (unsigned long ) ptr, spec); +} + +/* +* Helper function to decode printf style format. +* Each call decode a token from the format and return the +* number of characters read (or likely the delta where it wants +* to go on the next call). +* The decoded token is returned through the parameters +* +* 'h', 'l', or 'L' for integer fields +* 'z' support added 23/7/1999 S.H. +* 'z' changed to 'Z' --davidm 1/25/99 +* 't' added for ptrdiff_t +* +* @fmt: the format string +* @type of the token returned +* @flags: various flags such as +, -, # tokens.. +* @field_width: overwritten width +* @base: base of the number (octal, hex, ...) +* @precision: precision of a number +* @qualifier: qualifier of a number (long, size_t, ...) +*/ +static int format_decode (const char *fmt,struct printf_spec *spec) +{ +const char *start = fmt; +bool sign = false; + +/* we finished early by reading the field width */ +if (spec->type ==FORMAT_TYPE_WITDH ) { +if (spec->field_width <0 ) { + spec->field_width = -spec->field_width; + spec->flags |=LEFT ; + } + spec->type =FORMAT_TYPE_NONE ; + goto precision; + } + +/* we finished early by reading the precision */ +if (spec->type ==FORMAT_TYPE_PRECISION ) { +if (spec->precision <0 ) + spec->precision =0 ; + + spec->type =FORMAT_TYPE_NONE ; + goto qualifier; + } + +/* By default */ + spec->type =FORMAT_TYPE_NONE ; + +for (; *fmt ; ++fmt) { +if (*fmt =='%' ) +break ; + } + +/* Return the current non-format string */ +if (fmt != start || !*fmt) +return fmt - start; + +/* Process flags */ + spec->flags =0 ; + +while (1 ) {/* this also skips first '%' */ +bool found = true; + + ++fmt; + +switch (*fmt) { +case '-' : spec->flags |=LEFT ;break ; +case '+' : spec->flags |=PLUS ;break ; +case ' ' : spec->flags |=SPACE ;break ; +case '#' : spec->flags |=SPECIAL ;break ; +case '0' : spec->flags |=ZEROPAD ;break ; +default : found = false; + } + +if (!found) +break ; + } + +/* get field width */ + spec->field_width =-1 ; + +if (isdigit (*fmt)) + spec->field_width =skip_atoi (&fmt); +else if (*fmt =='*' ) { +/* it's the next argument */ + spec->type =FORMAT_TYPE_WITDH ; +return ++fmt - start; + } + +precision: +/* get the precision */ + spec->precision =-1 ; +if (*fmt =='.' ) { + ++fmt; +if (isdigit (*fmt)) { + spec->precision =skip_atoi (&fmt); +if (spec->precision <0 ) + spec->precision =0 ; + }else if (*fmt =='*' ) { +/* it's the next argument */ + spec->type =FORMAT_TYPE_WITDH ; +return ++fmt - start; + } + } + +qualifier: +/* get the conversion qualifier */ + spec->qualifier =-1 ; +if (*fmt =='h' || *fmt =='l' || *fmt =='L' || + *fmt =='Z' || *fmt =='z' || *fmt =='t' ) { + spec->qualifier = *fmt; + ++fmt; +if (spec->qualifier =='l' && *fmt =='l' ) { + spec->qualifier ='L' ; + ++fmt; + } + } + +/* default base */ + spec->base =10 ; +switch (*fmt) { +case 'c' : + spec->type =FORMAT_TYPE_CHAR ; +return ++fmt - start; + +case 's' : + spec->type =FORMAT_TYPE_STR ; +return ++fmt - start; + +case 'p' : + spec->type =FORMAT_TYPE_PTR ; +return fmt - start; +/* skip alnum */ + +case 'n' : + spec->type =FORMAT_TYPE_NRCHARS ; +return ++fmt - start; + +case '%' : + spec->type =FORMAT_TYPE_PERCENT_CHAR ; +return ++fmt - start; + +/* integer number formats - set up the flags and "break" */ +case 'o' : + spec->base =8 ;break ; + +case 'x' : + spec->flags |=SMALL ; + +case 'X' : + spec->base =16 ; +break ; + +case 'd' : +case 'i' : + sign = true; +case 'u' : +break ; + +default : + spec->type =FORMAT_TYPE_INVALID ; +return fmt - start; } - flags |=SMALL ; -if (field_width ==-1 ) { - field_width =2 *sizeof (void *); - flags |=ZEROPAD ; + +if (spec->qualifier =='L' ) + spec->type =FORMAT_TYPE_LONG_LONG ; +else if (spec->qualifier =='l' ) { +if (sign) + spec->type =FORMAT_TYPE_LONG ; +else + spec->type =FORMAT_TYPE_ULONG ; + }else if (spec->qualifier =='Z' || spec->qualifier =='z' ) { + spec->type =FORMAT_TYPE_SIZE_T ; + }else if (spec->qualifier =='t' ) { + spec->type =FORMAT_TYPE_PTRDIFF ; + }else if (spec->qualifier =='h' ) { +if (sign) + spec->type =FORMAT_TYPE_SHORT ; +else + spec->type =FORMAT_TYPE_USHORT ; + }else { +if (sign) + spec->type =FORMAT_TYPE_INT ; +else + spec->type =FORMAT_TYPE_UINT ; } -return number (buf, end, (unsigned long ) ptr,16 , field_width, precision, flags); + +return ++fmt - start; }/**
@@ -726,18 +961,9 @@ static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int fieldint vsnprintf (char *buf,size_t size,const char *fmt,va_list args) {unsigned long long num; -int base;char *str, *end, c; - -int flags;/* flags to number() */ - -int field_width;/* width of output field */ -int precision;/* min. # of digits for integers; max -number of chars for from string */ -int qualifier;/* 'h', 'l', or 'L' for integer fields */ -/* 'z' support added 23/7/1999 S.H. */ -/* 'z' changed to 'Z' --davidm 1/25/99 */ -/* 't' added for ptrdiff_t */ +int read; +struct printf_spec spec = {0 };/* Reject out-of-range values early. Large positive sizes are used for unknown buffer sizes. */
@@ -758,184 +984,144 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) size = end - buf; } -for (; *fmt ; ++fmt) { -if (*fmt !='%' ) { -if (str < end) - *str = *fmt; - ++str; -continue ; - } +while (*fmt) { +const char *old_fmt = fmt; -/* process flags */ - flags =0 ; - repeat: - ++fmt;/* this also skips first '%' */ -switch (*fmt) { -case '-' : flags |=LEFT ; goto repeat; -case '+' : flags |=PLUS ; goto repeat; -case ' ' : flags |=SPACE ; goto repeat; -case '#' : flags |=SPECIAL ; goto repeat; -case '0' : flags |=ZEROPAD ; goto repeat; - } + read =format_decode (fmt, &spec); -/* get field width */ - field_width =-1 ; -if (isdigit (*fmt)) - field_width =skip_atoi (&fmt); -else if (*fmt =='*' ) { - ++fmt; -/* it's the next argument */ - field_width =va_arg (args, int); -if (field_width <0 ) { - field_width = -field_width; - flags |=LEFT ; - } - } + fmt += read; -/* get the precision */ - precision =-1 ; -if (*fmt =='.' ) { - ++fmt; -if (isdigit (*fmt)) - precision =skip_atoi (&fmt); -else if (*fmt =='*' ) { - ++fmt; -/* it's the next argument */ - precision =va_arg (args, int); +switch (spec.type) { +case FORMAT_TYPE_NONE : { +int copy = read; +if (str < end) { +if (copy > end - str) + copy = end - str; +memcpy (str, old_fmt, copy); } -if (precision <0 ) - precision =0 ; + str += read; +break ; } -/* get the conversion qualifier */ - qualifier =-1 ; -if (*fmt =='h' || *fmt =='l' || *fmt =='L' || - *fmt =='Z' || *fmt =='z' || *fmt =='t' ) { - qualifier = *fmt; - ++fmt; -if (qualifier =='l' && *fmt =='l' ) { - qualifier ='L' ; - ++fmt; - } - } +case FORMAT_TYPE_WITDH : + spec.field_width =va_arg (args, int); +break ; -/* default base */ - base =10 ; +case FORMAT_TYPE_PRECISION : + spec.precision =va_arg (args, int); +break ; -switch (*fmt) { -case 'c' : -if (!(flags &LEFT )) { -while (--field_width >0 ) { -if (str < end) - *str =' ' ; - ++str; - } - } - c = (unsigned char )va_arg (args, int); -if (str < end) - *str = c; - ++str; -while (--field_width >0 ) { +case FORMAT_TYPE_CHAR : +if (!(spec.flags &LEFT )) { +while (--spec.field_width >0 ) {if (str < end) *str =' ' ; ++str; - } -continue ; - -case 's' : - str = string(str, end, va_arg(args, char *), field_width, precision, flags); -continue ; - -case 'p' : - str =pointer (fmt+1 , str, end, - va_arg(args, void *), - field_width, precision, flags); -/* Skip all alphanumeric pointer suffixes */ -while (isalnum (fmt[1 ])) - fmt++; -continue ; - -case 'n' : -/* FIXME: -* What does C99 say about the overflow case here? */ -if (qualifier =='l' ) { - long * ip = va_arg(args, long *); - *ip = (str - buf); - }else if (qualifier =='Z' || qualifier =='z' ) { - size_t * ip = va_arg(args, size_t *); - *ip = (str - buf); - }else { - int * ip = va_arg(args, int *); - *ip = (str - buf); - } -continue ; -case '%' : + } + } + c = (unsigned char )va_arg (args, int); +if (str < end) + *str = c; + ++str; +while (--spec.field_width >0 ) {if (str < end) - *str ='%' ; + *str =' ' ; ++str; -continue ; + } +break ; -/* integer number formats - set up the flags and "break" */ -case 'o' : - base =8 ; -break ; +case FORMAT_TYPE_STR : + str = string(str, end, va_arg(args, char *), spec); +break ; -case 'x' : - flags |=SMALL ; -case 'X' : - base =16 ; -break ; +case FORMAT_TYPE_PTR : + str = pointer(fmt+1, str, end, va_arg(args, void *), + spec); +while (isalnum (*fmt)) + fmt++; +break ; -case 'd' : -case 'i' : - flags |=SIGN ; -case 'u' : -break ; +case FORMAT_TYPE_PERCENT_CHAR : +if (str < end) + *str ='%' ; + ++str; +break ; -default : +case FORMAT_TYPE_INVALID : +if (str < end) + *str ='%' ; + ++str; +if (*fmt) {if (str < end) - *str ='%' ; + *str = *fmt; ++str; -if (*fmt) { -if (str < end) - *str = *fmt; - ++str; - }else { - --fmt; - } -continue ; + }else { + --fmt; + } +break ; + +case FORMAT_TYPE_NRCHARS : { +int qualifier = spec.qualifier; + +if (qualifier =='l' ) { + long *ip = va_arg(args, long *); + *ip = (str - buf); + }else if (qualifier =='Z' || + qualifier =='z' ) { + size_t *ip = va_arg(args, size_t *); + *ip = (str - buf); + }else { + int *ip = va_arg(args, int *); + *ip = (str - buf); + } +break ; } -if (qualifier =='L' ) - num = va_arg(args, long long); -else if (qualifier =='l' ) { - num = va_arg(args, unsigned long); -if (flags &SIGN ) - num = (signed long ) num; - }else if (qualifier =='Z' || qualifier =='z' ) { - num =va_arg (args, size_t); - }else if (qualifier =='t' ) { - num =va_arg (args, ptrdiff_t); - }else if (qualifier =='h' ) { - num = (unsigned short )va_arg (args, int); -if (flags &SIGN ) - num = (signed short ) num; - }else { - num = va_arg(args, unsigned int); -if (flags &SIGN ) - num = (signed int ) num; + +default : +switch (spec.type) { +case FORMAT_TYPE_LONG_LONG : + num = va_arg(args, long long); +break ; +case FORMAT_TYPE_ULONG : + num = va_arg(args, unsigned long); +break ; +case FORMAT_TYPE_LONG : + num =va_arg (args, long); +break ; +case FORMAT_TYPE_SIZE_T : + num =va_arg (args, size_t); +break ; +case FORMAT_TYPE_PTRDIFF : + num =va_arg (args, ptrdiff_t); +break ; +case FORMAT_TYPE_USHORT : + num = (unsigned short )va_arg (args, int); +break ; +case FORMAT_TYPE_SHORT : + num = (short )va_arg (args, int); +break ; +case FORMAT_TYPE_UINT : + num = va_arg(args, unsigned int); +break ; +default : + num = va_arg(args, unsigned int); + } + + str =number (str, end, num, spec); } - str =number (str, end, num, base, - field_width, precision, flags); } +if (size >0 ) {if (str < end) *str ='\0' ;else end[-1 ] ='\0' ; } +/* the trailing null byte doesn't count towards the total */ return str-buf; + }EXPORT_SYMBOL (vsnprintf);
@@ -1084,8 +1270,9 @@ EXPORT_SYMBOL(sprintf);*/ int vbin_printf (u32 *bin_buf,size_t size,const char *fmt,va_list args) { +struct printf_spec spec = {0 };char *str, *end; -int qualifier; +int read; str = (char *)bin_buf; end = (char *)(bin_buf + size);
@@ -1110,57 +1297,26 @@ do { \ str +=sizeof (type); \ }while (0 ) -for (; *fmt ; ++fmt) { -if (*fmt !='%' ) -continue ; -repeat: -/* parse flags */ - ++fmt;/* this also skips first '%' */ -if (*fmt =='-' || *fmt =='+' || *fmt ==' ' - || *fmt =='#' || *fmt =='0' ) - goto repeat; +while (*fmt) { + read =format_decode (fmt, &spec); -/* parse field width */ -if (isdigit (*fmt)) -skip_atoi (&fmt); -else if (*fmt =='*' ) { - ++fmt; -/* it's the next argument */ -save_arg (int); - } + fmt += read; -/* parse the precision */ -if (*fmt =='.' ) { - ++fmt; -if (isdigit (*fmt)) -skip_atoi (&fmt); -else if (*fmt =='*' ) { - ++fmt; -/* it's the next argument */ -save_arg (int); - } - } +switch (spec.type) { +case FORMAT_TYPE_NONE : +break ; -/* parse the conversion qualifier */ - qualifier =-1 ; -if (*fmt =='h' || *fmt =='l' || *fmt =='L' || - *fmt =='Z' || *fmt =='z' || *fmt =='t' ) { - qualifier = *fmt; - ++fmt; -if (qualifier =='l' && *fmt =='l' ) { - qualifier ='L' ; - ++fmt; - } - } +case FORMAT_TYPE_WITDH : +case FORMAT_TYPE_PRECISION : +save_arg (int); +break ; -/* parse format type */ -switch (*fmt) { -case 'c' : +case FORMAT_TYPE_CHAR :save_arg (char); -continue ; -case 's' : { -/* save the string argument */ +break ; + +case FORMAT_TYPE_STR : { const char *save_str = va_arg(args, char *);size_t len; if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
@@ -1170,16 +1326,27 @@ repeat:if (str + len +1 < end)memcpy (str, save_str, len +1 ); str += len +1 ; -continue ; +break ; } -case 'p' : + +case FORMAT_TYPE_PTR : save_arg(void *);/* skip all alphanumeric pointer suffixes */ -while (isalnum (fmt[1 ])) +while (isalnum (*fmt)) fmt++; -continue ; -case 'n' : { +break ; + +case FORMAT_TYPE_PERCENT_CHAR : +break ; + +case FORMAT_TYPE_INVALID : +if (!*fmt) + --fmt; +break ; + +case FORMAT_TYPE_NRCHARS : {/* skip %n 's argument */ +int qualifier = spec.qualifier;void *skip_arg;if (qualifier =='l' ) skip_arg = va_arg(args, long *);
@@ -1187,37 +1354,37 @@ repeat: skip_arg = va_arg(args, size_t *); else skip_arg = va_arg(args, int *); -continue ; +break ; } -case 'o' : -case 'x' : -case 'X' : -case 'd' : -case 'i' : -case 'u' : -/* save arg for case: 'o', 'x', 'X', 'd', 'i', 'u' */ -if (qualifier =='L' ) + +default : +switch (spec.type) { + +case FORMAT_TYPE_LONG_LONG : save_arg(long long); -else if (qualifier =='l' ) +break ; +case FORMAT_TYPE_ULONG : +case FORMAT_TYPE_LONG : save_arg(unsigned long); -else if (qualifier =='Z' || qualifier =='z' ) +break ; +case FORMAT_TYPE_SIZE_T :save_arg (size_t); -else if (qualifier =='t' ) +break ; +case FORMAT_TYPE_PTRDIFF :save_arg (ptrdiff_t); -else if (qualifier =='h' ) +break ; +case FORMAT_TYPE_USHORT : +case FORMAT_TYPE_SHORT :save_arg (short); -else +break ; +default :save_arg (int); -continue ; -default : -if (!*fmt) - --fmt; -continue ; + } } } -#undef save_arg -return (u32 *)(PTR_ALIGN (str,sizeof (u32))) - bin_buf; + +#undef save_arg }EXPORT_SYMBOL_GPL (vbin_printf);
@@ -1249,14 +1416,10 @@ EXPORT_SYMBOL_GPL(vbin_printf);int bstr_printf (char *buf,size_t size,const char *fmt,const u32 *bin_buf) {unsigned long long num; -int base;char *str, *end, c;const char *args = (const char *)bin_buf; -int flags; -int field_width; -int precision; -int qualifier; +struct printf_spec spec = {0 };if (unlikely ((int ) size <0 )) {/* There can be only one.. */
@@ -1290,84 +1453,37 @@ int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) size = end - buf; } -for (; *fmt ; ++fmt) { -if (*fmt !='%' ) { -if (str < end) - *str = *fmt; - ++str; -continue ; - } +while (*fmt) { +int read; +const char *old_fmt = fmt; -/* process flags */ - flags =0 ; -repeat: - ++fmt;/* this also skips first '%' */ -switch (*fmt) { -case '-' : - flags |=LEFT ; - goto repeat; -case '+' : - flags |=PLUS ; - goto repeat; -case ' ' : - flags |=SPACE ; - goto repeat; -case '#' : - flags |=SPECIAL ; - goto repeat; -case '0' : - flags |=ZEROPAD ; - goto repeat; - } + read =format_decode (fmt, &spec); -/* get field width */ - field_width =-1 ; -if (isdigit (*fmt)) - field_width =skip_atoi (&fmt); -else if (*fmt =='*' ) { - ++fmt; -/* it's the next argument */ - field_width =get_arg (int); -if (field_width <0 ) { - field_width = -field_width; - flags |=LEFT ; - } - } + fmt += read; -/* get the precision */ - precision =-1 ; -if (*fmt =='.' ) { - ++fmt; -if (isdigit (*fmt)) - precision =skip_atoi (&fmt); -else if (*fmt =='*' ) { - ++fmt; -/* it's the next argument */ - precision =get_arg (int); +switch (spec.type) { +case FORMAT_TYPE_NONE : { +int copy = read; +if (str < end) { +if (copy > end - str) + copy = end - str; +memcpy (str, old_fmt, copy); } -if (precision <0 ) - precision =0 ; + str += read; +break ; } -/* get the conversion qualifier */ - qualifier =-1 ; -if (*fmt =='h' || *fmt =='l' || *fmt =='L' || - *fmt =='Z' || *fmt =='z' || *fmt =='t' ) { - qualifier = *fmt; - ++fmt; -if (qualifier =='l' && *fmt =='l' ) { - qualifier ='L' ; - ++fmt; - } - } +case FORMAT_TYPE_WITDH : + spec.field_width =get_arg (int); +break ; -/* default base */ - base =10 ; +case FORMAT_TYPE_PRECISION : + spec.precision =get_arg (int); +break ; -switch (*fmt) { -case 'c' : -if (!(flags &LEFT )) { -while (--field_width >0 ) { +case FORMAT_TYPE_CHAR : +if (!(spec.flags &LEFT )) { +while (--spec.field_width >0 ) {if (str < end) *str =' ' ; ++str;
@@ -1377,58 +1493,34 @@ repeat:if (str < end) *str = c; ++str; -while (--field_width >0 ) { +while (--spec.field_width >0 ) {if (str < end) *str =' ' ; ++str; } -continue ; +break ; -case 's' :{ +case FORMAT_TYPE_STR : {const char *str_arg = args;size_t len =strlen (str_arg); args += len +1 ; - str =string (str, end, (char *)str_arg, field_width, - precision, flags); -continue ; + str =string (str, end, (char *)str_arg, spec); +break ; } -case 'p' : - str = pointer(fmt+1, str, end, get_arg(void *), - field_width, precision, flags); -/* Skip all alphanumeric pointer suffixes */ -while (isalnum (fmt[1 ])) +case FORMAT_TYPE_PTR : + str = pointer(fmt+1, str, end, get_arg(void *), spec); +while (isalnum (*fmt)) fmt++; -continue ; - -case 'n' : -/* skip %n */ -continue ; +break ; -case '%' : +case FORMAT_TYPE_PERCENT_CHAR :if (str < end) *str ='%' ; ++str; -continue ; - -/* integer number formats - set up the flags and "break" */ -case 'o' : - base =8 ; -break ; - -case 'x' : - flags |=SMALL ; -case 'X' : - base =16 ;break ; -case 'd' : -case 'i' : - flags |=SIGN ; -case 'u' : -break ; - -default : +case FORMAT_TYPE_INVALID :if (str < end) *str ='%' ; ++str;
@@ -1439,36 +1531,54 @@ repeat: }else { --fmt; } -continue ; - } -if (qualifier =='L' ) - num = get_arg(long long); -else if (qualifier =='l' ) { - num = get_arg(unsigned long); -if (flags &SIGN ) - num = (signed long ) num; - }else if (qualifier =='Z' || qualifier =='z' ) { - num =get_arg (size_t); - }else if (qualifier =='t' ) { - num =get_arg (ptrdiff_t); - }else if (qualifier =='h' ) { - num = (unsigned short )get_arg (short); -if (flags &SIGN ) - num = (signed short ) num; - }else { - num = get_arg(unsigned int); -if (flags &SIGN ) - num = (signed int ) num; +break ; + +case FORMAT_TYPE_NRCHARS : +/* skip */ +break ; + +default : +switch (spec.type) { + +case FORMAT_TYPE_LONG_LONG : + num = get_arg(long long); +break ; +case FORMAT_TYPE_ULONG : + num = get_arg(unsigned long); +break ; +case FORMAT_TYPE_LONG : + num = get_arg(unsigned long); +break ; +case FORMAT_TYPE_SIZE_T : + num =get_arg (size_t); +break ; +case FORMAT_TYPE_PTRDIFF : + num =get_arg (ptrdiff_t); +break ; +case FORMAT_TYPE_USHORT : + num = get_arg(unsigned short); +break ; +case FORMAT_TYPE_SHORT : + num =get_arg (short); +break ; +case FORMAT_TYPE_UINT : + num = get_arg(unsigned int); +break ; +default : + num =get_arg (int); + } + + str =number (str, end, num, spec); } - str =number (str, end, num, base, - field_width, precision, flags); } +if (size >0 ) {if (str < end) *str ='\0' ;else end[-1 ] ='\0' ; } +#undef get_arg/* the trailing null byte doesn't count towards the total */
PATCH v2 1/5 add binary printf
In reply to Frederic WeisbeckerFrom: Lai Jiangshan <[email protected]>
Impact: Add APIs for binary trace printk infrastructure
vbin_printf(): write args to binary buffer, string is copied when "%s" is occurred.
bstr_printf(): read from binary buffer for args and format a string
[[email protected]: rebase against latest -tip]
--- include/linux/string.h | 7 + lib/Kconfig | 3 + lib/vsprintf.c | 442 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 452 insertions(+), 0 deletions(-)
include/linux/string.h +7 -0
@@ -10,6 +10,7 @@#include <linux/compiler.h> /* for inline */ #include <linux/types.h> /* for size_t */ #include <linux/stddef.h> /* for NULL */ +#include <stdarg.h> extern char *strndup_user (const char __user *,long );
@@ -111,6 +112,12 @@ extern void argv_free(char **argv);extern bool sysfs_streq (const char *s1,const char *s2); +#ifdef CONFIG_BINARY_PRINTF +int vbin_printf (u32 *bin_buf,size_t size,const char *fmt,va_list args); +int bstr_printf (char *buf,size_t size,const char *fmt,const u32 *bin_buf); +int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) __printf(3, 4); +#endif +extern ssize_t memory_read_from_buffer (void *to,size_t count,loff_t *ppos,const void *from,size_t available);
lib/Kconfig +3 -0
@@ -2,6 +2,9 @@ # Library configuration # +config BINARY_PRINTF + def_bool n + menu "Library routines" config BITREVERSE
lib/vsprintf.c +442 -0
448 lines not on this page · raw message
Signature · 1 line
1.6.1
Re: [PATCH 1/5 v2] add binary printf
Quoted 7 lines · Frederic Weisbecker
From: Lai Jiangshan <[email protected]>
Impact: Add APIs for binary trace printk infrastructure
vbin_printf(): write args to binary buffer, string is copied when "%s" is occurred.
Quoted 6 lines
bstr_printf(): read from binary buffer for args and format a string[[email protected]: rebase against latest -tip]
Signed-off-by Lai Jiangshan <[email protected]>Signed-off-by Frederic Weisbecker <[email protected]>
Well it finally decided to show up.
for the whole series:
-- Steve
[tip:core/printk] vsprintf: add binary printf
In reply to Frederic WeisbeckerCommit-ID: 4370aa4aa75391a5e2e06bccb0919109f725ed8e Gitweb: http://git.kernel.org/tip/4370aa4aa75391a5e2e06bccb0919109f725ed8e Author: "Lai Jiangshan" <[email protected]> AuthorDate: Fri, 6 Mar 2009 17:21:46 +0100 Commit: Ingo Molnar <[email protected]> CommitDate: Fri, 6 Mar 2009 17:39:04 +0100
vsprintf: add binary printf
Impact: add new APIs for binary trace printk infrastructure
vbin_printf(): write args to binary buffer, string is copied when "%s" is occurred.
bstr_printf(): read from binary buffer for args and format a string
[[email protected]: rebase]
LKML-Reference: <[email protected]>
--- include/linux/string.h | 7 + lib/Kconfig | 3 + lib/vsprintf.c | 442 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 452 insertions(+), 0 deletions(-)
include/linux/string.h +7 -0
@@ -10,6 +10,7 @@#include <linux/compiler.h> /* for inline */ #include <linux/types.h> /* for size_t */ #include <linux/stddef.h> /* for NULL */ +#include <stdarg.h> extern char *strndup_user (const char __user *,long );
@@ -111,6 +112,12 @@ extern void argv_free(char **argv);extern bool sysfs_streq (const char *s1,const char *s2); +#ifdef CONFIG_BINARY_PRINTF +int vbin_printf (u32 *bin_buf,size_t size,const char *fmt,va_list args); +int bstr_printf (char *buf,size_t size,const char *fmt,const u32 *bin_buf); +int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) __printf(3, 4); +#endif +extern ssize_t memory_read_from_buffer (void *to,size_t count,loff_t *ppos,const void *from,size_t available);
lib/Kconfig +3 -0
@@ -2,6 +2,9 @@ # Library configuration # +config BINARY_PRINTF + def_bool n + menu "Library routines" config BITREVERSE
lib/vsprintf.c +442 -0
448 lines not on this page · raw message