Skip to content

Commit

Permalink
tracing: Add trace_seq_has_overflowed() and trace_handle_return()
Browse files Browse the repository at this point in the history
Adding a trace_seq_has_overflowed() which returns true if the trace_seq
had too much written into it allows us to simplify the code.

Instead of checking the return value of every call to trace_seq_printf()
and friends, they can all be called normally, and at the end we can
return !trace_seq_has_overflowed() instead.

Several functions also return TRACE_TYPE_PARTIAL_LINE when the trace_seq
overflowed and TRACE_TYPE_HANDLED otherwise. Another helper function
was created called trace_handle_return() which takes a trace_seq and
returns these enums. Using this helper function also simplifies the
code.

This change also makes it possible to remove the return values of
trace_seq_printf() and friends. They should instead just be
void functions.

Link: http://lkml.kernel.org/r/20141114011410.365183157@goodmis.org

Reviewed-by: Petr Mladek <pmladek@suse.cz>
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
  • Loading branch information
Steven Rostedt (Red Hat) committed Nov 19, 2014
1 parent e400a40 commit 19a7fe2
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 300 deletions.
11 changes: 11 additions & 0 deletions include/linux/ftrace_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,17 @@ enum print_line_t {
TRACE_TYPE_NO_CONSUME = 3 /* Handled but ask to not consume */
};

/*
* Several functions return TRACE_TYPE_PARTIAL_LINE if the trace_seq
* overflowed, and TRACE_TYPE_HANDLED otherwise. This helper function
* simplifies those functions and keeps them in sync.
*/
static inline enum print_line_t trace_handle_return(struct trace_seq *s)
{
return trace_seq_has_overflowed(s) ?
TRACE_TYPE_PARTIAL_LINE : TRACE_TYPE_HANDLED;
}

void tracing_generic_entry_update(struct trace_entry *entry,
unsigned long flags,
int pc);
Expand Down
12 changes: 12 additions & 0 deletions include/linux/trace_seq.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ trace_seq_buffer_ptr(struct trace_seq *s)
return s->buffer + s->len;
}

/**
* trace_seq_has_overflowed - return true if the trace_seq took too much
* @s: trace sequence descriptor
*
* Returns true if too much data was added to the trace_seq and it is
* now full and will not take anymore.
*/
static inline bool trace_seq_has_overflowed(struct trace_seq *s)
{
return s->full || s->len > PAGE_SIZE - 1;
}

/*
* Currently only defined when tracing is enabled.
*/
Expand Down
6 changes: 2 additions & 4 deletions include/trace/ftrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,9 @@ ftrace_raw_output_##call(struct trace_iterator *iter, int flags, \
if (ret) \
return ret; \
\
ret = trace_seq_printf(s, print); \
if (!ret) \
return TRACE_TYPE_PARTIAL_LINE; \
trace_seq_printf(s, print); \
\
return TRACE_TYPE_HANDLED; \
return trace_handle_return(s); \
} \
static struct trace_event_functions ftrace_event_type_funcs_##call = { \
.trace = ftrace_raw_output_##call, \
Expand Down
69 changes: 35 additions & 34 deletions kernel/trace/trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -2649,24 +2649,21 @@ static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
event = ftrace_find_event(entry->type);

if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
if (!trace_print_lat_context(iter))
goto partial;
} else {
if (!trace_print_context(iter))
goto partial;
}
if (iter->iter_flags & TRACE_FILE_LAT_FMT)
trace_print_lat_context(iter);
else
trace_print_context(iter);
}

if (trace_seq_has_overflowed(s))
return TRACE_TYPE_PARTIAL_LINE;

if (event)
return event->funcs->trace(iter, sym_flags, event);

if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
goto partial;
trace_seq_printf(s, "Unknown type %d\n", entry->type);

return TRACE_TYPE_HANDLED;
partial:
return TRACE_TYPE_PARTIAL_LINE;
return trace_handle_return(s);
}

static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
Expand All @@ -2677,22 +2674,20 @@ static enum print_line_t print_raw_fmt(struct trace_iterator *iter)

entry = iter->ent;

if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
if (!trace_seq_printf(s, "%d %d %llu ",
entry->pid, iter->cpu, iter->ts))
goto partial;
}
if (trace_flags & TRACE_ITER_CONTEXT_INFO)
trace_seq_printf(s, "%d %d %llu ",
entry->pid, iter->cpu, iter->ts);

if (trace_seq_has_overflowed(s))
return TRACE_TYPE_PARTIAL_LINE;

event = ftrace_find_event(entry->type);
if (event)
return event->funcs->raw(iter, 0, event);

if (!trace_seq_printf(s, "%d ?\n", entry->type))
goto partial;
trace_seq_printf(s, "%d ?\n", entry->type);

return TRACE_TYPE_HANDLED;
partial:
return TRACE_TYPE_PARTIAL_LINE;
return trace_handle_return(s);
}

static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
Expand All @@ -2705,9 +2700,11 @@ static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
entry = iter->ent;

if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
SEQ_PUT_HEX_FIELD(s, entry->pid);
SEQ_PUT_HEX_FIELD(s, iter->cpu);
SEQ_PUT_HEX_FIELD(s, iter->ts);
if (trace_seq_has_overflowed(s))
return TRACE_TYPE_PARTIAL_LINE;
}

event = ftrace_find_event(entry->type);
Expand All @@ -2717,9 +2714,9 @@ static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
return ret;
}

SEQ_PUT_FIELD_RET(s, newline);
SEQ_PUT_FIELD(s, newline);

return TRACE_TYPE_HANDLED;
return trace_handle_return(s);
}

static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
Expand All @@ -2731,9 +2728,11 @@ static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
entry = iter->ent;

if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
SEQ_PUT_FIELD_RET(s, entry->pid);
SEQ_PUT_FIELD_RET(s, iter->cpu);
SEQ_PUT_FIELD_RET(s, iter->ts);
SEQ_PUT_FIELD(s, entry->pid);
SEQ_PUT_FIELD(s, iter->cpu);
SEQ_PUT_FIELD(s, iter->ts);
if (trace_seq_has_overflowed(s))
return TRACE_TYPE_PARTIAL_LINE;
}

event = ftrace_find_event(entry->type);
Expand Down Expand Up @@ -2779,10 +2778,12 @@ enum print_line_t print_trace_line(struct trace_iterator *iter)
{
enum print_line_t ret;

if (iter->lost_events &&
!trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
iter->cpu, iter->lost_events))
return TRACE_TYPE_PARTIAL_LINE;
if (iter->lost_events) {
trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
iter->cpu, iter->lost_events);
if (trace_seq_has_overflowed(&iter->seq))
return TRACE_TYPE_PARTIAL_LINE;
}

if (iter->trace && iter->trace->print_line) {
ret = iter->trace->print_line(iter);
Expand Down
1 change: 1 addition & 0 deletions kernel/trace/trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <linux/trace_seq.h>
#include <linux/ftrace_event.h>
#include <linux/compiler.h>
#include <linux/trace_seq.h>

#ifdef CONFIG_FTRACE_SYSCALLS
#include <asm/unistd.h> /* For NR_SYSCALLS */
Expand Down
Loading

0 comments on commit 19a7fe2

Please sign in to comment.