Commit 3c6d8d84 authored by Namhyung Kim's avatar Namhyung Kim Committed by Arnaldo Carvalho de Melo

tools lib traceevent: Add state member to struct trace_seq

The trace_seq->state is for tracking errors during the use of trace_seq
APIs and getting rid of die() in it.
Signed-off-by: default avatarNamhyung Kim <namhyung@kernel.org>
Reviewed-by: default avatarJiri Olsa <jolsa@redhat.com>
Acked-by: default avatarSteven Rostedt <rostedt@goodmis.org>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung.kim@lge.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Steven Rostedt <rostedt@goodmis.org>
Link: http://lkml.kernel.org/r/87fvopalbb.fsf@sejong.aot.lge.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 7d16c634
...@@ -136,7 +136,7 @@ export Q VERBOSE ...@@ -136,7 +136,7 @@ export Q VERBOSE
EVENT_PARSE_VERSION = $(EP_VERSION).$(EP_PATCHLEVEL).$(EP_EXTRAVERSION) EVENT_PARSE_VERSION = $(EP_VERSION).$(EP_PATCHLEVEL).$(EP_EXTRAVERSION)
INCLUDES = -I. $(CONFIG_INCLUDES) INCLUDES = -I. -I $(srctree)/../../include $(CONFIG_INCLUDES)
# Set compile option CFLAGS if not set elsewhere # Set compile option CFLAGS if not set elsewhere
CFLAGS ?= -g -Wall CFLAGS ?= -g -Wall
......
...@@ -58,6 +58,12 @@ struct pevent_record { ...@@ -58,6 +58,12 @@ struct pevent_record {
#endif #endif
}; };
enum trace_seq_fail {
TRACE_SEQ__GOOD,
TRACE_SEQ__BUFFER_POISONED,
TRACE_SEQ__MEM_ALLOC_FAILED,
};
/* /*
* Trace sequences are used to allow a function to call several other functions * Trace sequences are used to allow a function to call several other functions
* to create a string of data to use (up to a max of PAGE_SIZE). * to create a string of data to use (up to a max of PAGE_SIZE).
...@@ -68,6 +74,7 @@ struct trace_seq { ...@@ -68,6 +74,7 @@ struct trace_seq {
unsigned int buffer_size; unsigned int buffer_size;
unsigned int len; unsigned int len;
unsigned int readpos; unsigned int readpos;
enum trace_seq_fail state;
}; };
void trace_seq_init(struct trace_seq *s); void trace_seq_init(struct trace_seq *s);
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include <string.h> #include <string.h>
#include <stdarg.h> #include <stdarg.h>
#include <asm/bug.h>
#include "event-parse.h" #include "event-parse.h"
#include "event-utils.h" #include "event-utils.h"
...@@ -32,10 +33,21 @@ ...@@ -32,10 +33,21 @@
#define TRACE_SEQ_POISON ((void *)0xdeadbeef) #define TRACE_SEQ_POISON ((void *)0xdeadbeef)
#define TRACE_SEQ_CHECK(s) \ #define TRACE_SEQ_CHECK(s) \
do { \ do { \
if ((s)->buffer == TRACE_SEQ_POISON) \ if (WARN_ONCE((s)->buffer == TRACE_SEQ_POISON, \
die("Usage of trace_seq after it was destroyed"); \ "Usage of trace_seq after it was destroyed")) \
(s)->state = TRACE_SEQ__BUFFER_POISONED; \
} while (0) } while (0)
#define TRACE_SEQ_CHECK_RET_N(s, n) \
do { \
TRACE_SEQ_CHECK(s); \
if ((s)->state != TRACE_SEQ__GOOD) \
return n; \
} while (0)
#define TRACE_SEQ_CHECK_RET(s) TRACE_SEQ_CHECK_RET_N(s, )
#define TRACE_SEQ_CHECK_RET0(s) TRACE_SEQ_CHECK_RET_N(s, 0)
/** /**
* trace_seq_init - initialize the trace_seq structure * trace_seq_init - initialize the trace_seq structure
* @s: a pointer to the trace_seq structure to initialize * @s: a pointer to the trace_seq structure to initialize
...@@ -46,6 +58,7 @@ void trace_seq_init(struct trace_seq *s) ...@@ -46,6 +58,7 @@ void trace_seq_init(struct trace_seq *s)
s->readpos = 0; s->readpos = 0;
s->buffer_size = TRACE_SEQ_BUF_SIZE; s->buffer_size = TRACE_SEQ_BUF_SIZE;
s->buffer = malloc_or_die(s->buffer_size); s->buffer = malloc_or_die(s->buffer_size);
s->state = TRACE_SEQ__GOOD;
} }
/** /**
...@@ -71,7 +84,7 @@ void trace_seq_destroy(struct trace_seq *s) ...@@ -71,7 +84,7 @@ void trace_seq_destroy(struct trace_seq *s)
{ {
if (!s) if (!s)
return; return;
TRACE_SEQ_CHECK(s); TRACE_SEQ_CHECK_RET(s);
free(s->buffer); free(s->buffer);
s->buffer = TRACE_SEQ_POISON; s->buffer = TRACE_SEQ_POISON;
} }
...@@ -80,8 +93,9 @@ static void expand_buffer(struct trace_seq *s) ...@@ -80,8 +93,9 @@ static void expand_buffer(struct trace_seq *s)
{ {
s->buffer_size += TRACE_SEQ_BUF_SIZE; s->buffer_size += TRACE_SEQ_BUF_SIZE;
s->buffer = realloc(s->buffer, s->buffer_size); s->buffer = realloc(s->buffer, s->buffer_size);
if (!s->buffer) if (WARN_ONCE(!s->buffer,
die("Can't allocate trace_seq buffer memory"); "Can't allocate trace_seq buffer memory"))
s->state = TRACE_SEQ__MEM_ALLOC_FAILED;
} }
/** /**
...@@ -105,9 +119,9 @@ trace_seq_printf(struct trace_seq *s, const char *fmt, ...) ...@@ -105,9 +119,9 @@ trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
int len; int len;
int ret; int ret;
TRACE_SEQ_CHECK(s);
try_again: try_again:
TRACE_SEQ_CHECK_RET0(s);
len = (s->buffer_size - 1) - s->len; len = (s->buffer_size - 1) - s->len;
va_start(ap, fmt); va_start(ap, fmt);
...@@ -141,9 +155,9 @@ trace_seq_vprintf(struct trace_seq *s, const char *fmt, va_list args) ...@@ -141,9 +155,9 @@ trace_seq_vprintf(struct trace_seq *s, const char *fmt, va_list args)
int len; int len;
int ret; int ret;
TRACE_SEQ_CHECK(s);
try_again: try_again:
TRACE_SEQ_CHECK_RET0(s);
len = (s->buffer_size - 1) - s->len; len = (s->buffer_size - 1) - s->len;
ret = vsnprintf(s->buffer + s->len, len, fmt, args); ret = vsnprintf(s->buffer + s->len, len, fmt, args);
...@@ -172,13 +186,15 @@ int trace_seq_puts(struct trace_seq *s, const char *str) ...@@ -172,13 +186,15 @@ int trace_seq_puts(struct trace_seq *s, const char *str)
{ {
int len; int len;
TRACE_SEQ_CHECK(s); TRACE_SEQ_CHECK_RET0(s);
len = strlen(str); len = strlen(str);
while (len > ((s->buffer_size - 1) - s->len)) while (len > ((s->buffer_size - 1) - s->len))
expand_buffer(s); expand_buffer(s);
TRACE_SEQ_CHECK_RET0(s);
memcpy(s->buffer + s->len, str, len); memcpy(s->buffer + s->len, str, len);
s->len += len; s->len += len;
...@@ -187,11 +203,13 @@ int trace_seq_puts(struct trace_seq *s, const char *str) ...@@ -187,11 +203,13 @@ int trace_seq_puts(struct trace_seq *s, const char *str)
int trace_seq_putc(struct trace_seq *s, unsigned char c) int trace_seq_putc(struct trace_seq *s, unsigned char c)
{ {
TRACE_SEQ_CHECK(s); TRACE_SEQ_CHECK_RET0(s);
while (s->len >= (s->buffer_size - 1)) while (s->len >= (s->buffer_size - 1))
expand_buffer(s); expand_buffer(s);
TRACE_SEQ_CHECK_RET0(s);
s->buffer[s->len++] = c; s->buffer[s->len++] = c;
return 1; return 1;
...@@ -199,7 +217,7 @@ int trace_seq_putc(struct trace_seq *s, unsigned char c) ...@@ -199,7 +217,7 @@ int trace_seq_putc(struct trace_seq *s, unsigned char c)
void trace_seq_terminate(struct trace_seq *s) void trace_seq_terminate(struct trace_seq *s)
{ {
TRACE_SEQ_CHECK(s); TRACE_SEQ_CHECK_RET(s);
/* There's always one character left on the buffer */ /* There's always one character left on the buffer */
s->buffer[s->len] = 0; s->buffer[s->len] = 0;
...@@ -208,5 +226,16 @@ void trace_seq_terminate(struct trace_seq *s) ...@@ -208,5 +226,16 @@ void trace_seq_terminate(struct trace_seq *s)
int trace_seq_do_printf(struct trace_seq *s) int trace_seq_do_printf(struct trace_seq *s)
{ {
TRACE_SEQ_CHECK(s); TRACE_SEQ_CHECK(s);
switch (s->state) {
case TRACE_SEQ__GOOD:
return printf("%.*s", s->len, s->buffer); return printf("%.*s", s->len, s->buffer);
case TRACE_SEQ__BUFFER_POISONED:
puts("Usage of trace_seq after it was destroyed");
break;
case TRACE_SEQ__MEM_ALLOC_FAILED:
puts("Can't allocate trace_seq buffer memory");
break;
}
return -1;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment