2 * Copyright (C) 2009 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation;
8 * version 2.1 of the License (not later!)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, see <http://www.gnu.org/licenses>
18 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 #include "event-parse.h"
27 #include "event-utils.h"
30 * The TRACE_SEQ_POISON is to catch the use of using
31 * a trace_seq structure after it was destroyed.
33 #define TRACE_SEQ_POISON ((void *)0xdeadbeef)
34 #define TRACE_SEQ_CHECK(s) \
36 if (WARN_ONCE((s)->buffer == TRACE_SEQ_POISON, \
37 "Usage of trace_seq after it was destroyed")) \
38 (s)->state = TRACE_SEQ__BUFFER_POISONED; \
41 #define TRACE_SEQ_CHECK_RET_N(s, n) \
44 if ((s)->state != TRACE_SEQ__GOOD) \
48 #define TRACE_SEQ_CHECK_RET(s) TRACE_SEQ_CHECK_RET_N(s, )
49 #define TRACE_SEQ_CHECK_RET0(s) TRACE_SEQ_CHECK_RET_N(s, 0)
52 * trace_seq_init - initialize the trace_seq structure
53 * @s: a pointer to the trace_seq structure to initialize
55 void trace_seq_init(struct trace_seq *s)
59 s->buffer_size = TRACE_SEQ_BUF_SIZE;
60 s->buffer = malloc_or_die(s->buffer_size);
61 s->state = TRACE_SEQ__GOOD;
65 * trace_seq_reset - re-initialize the trace_seq structure
66 * @s: a pointer to the trace_seq structure to reset
68 void trace_seq_reset(struct trace_seq *s)
78 * trace_seq_destroy - free up memory of a trace_seq
79 * @s: a pointer to the trace_seq to free the buffer
81 * Only frees the buffer, not the trace_seq struct itself.
83 void trace_seq_destroy(struct trace_seq *s)
87 TRACE_SEQ_CHECK_RET(s);
89 s->buffer = TRACE_SEQ_POISON;
92 static void expand_buffer(struct trace_seq *s)
96 buf = realloc(s->buffer, s->buffer_size + TRACE_SEQ_BUF_SIZE);
97 if (WARN_ONCE(!buf, "Can't allocate trace_seq buffer memory")) {
98 s->state = TRACE_SEQ__MEM_ALLOC_FAILED;
103 s->buffer_size += TRACE_SEQ_BUF_SIZE;
107 * trace_seq_printf - sequence printing of trace information
108 * @s: trace sequence descriptor
109 * @fmt: printf format string
111 * It returns 0 if the trace oversizes the buffer's free
112 * space, 1 otherwise.
114 * The tracer may use either sequence operations or its own
115 * copy to user routines. To simplify formating of a trace
116 * trace_seq_printf is used to store strings into a special
117 * buffer (@s). Then the output may be either used by
118 * the sequencer or pulled into another buffer.
121 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
128 TRACE_SEQ_CHECK_RET0(s);
130 len = (s->buffer_size - 1) - s->len;
133 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
147 * trace_seq_vprintf - sequence printing of trace information
148 * @s: trace sequence descriptor
149 * @fmt: printf format string
151 * The tracer may use either sequence operations or its own
152 * copy to user routines. To simplify formating of a trace
153 * trace_seq_printf is used to store strings into a special
154 * buffer (@s). Then the output may be either used by
155 * the sequencer or pulled into another buffer.
158 trace_seq_vprintf(struct trace_seq *s, const char *fmt, va_list args)
164 TRACE_SEQ_CHECK_RET0(s);
166 len = (s->buffer_size - 1) - s->len;
168 ret = vsnprintf(s->buffer + s->len, len, fmt, args);
181 * trace_seq_puts - trace sequence printing of simple string
182 * @s: trace sequence descriptor
183 * @str: simple string to record
185 * The tracer may use either the sequence operations or its own
186 * copy to user routines. This function records a simple string
187 * into a special buffer (@s) for later retrieval by a sequencer
188 * or other mechanism.
190 int trace_seq_puts(struct trace_seq *s, const char *str)
194 TRACE_SEQ_CHECK_RET0(s);
198 while (len > ((s->buffer_size - 1) - s->len))
201 TRACE_SEQ_CHECK_RET0(s);
203 memcpy(s->buffer + s->len, str, len);
209 int trace_seq_putc(struct trace_seq *s, unsigned char c)
211 TRACE_SEQ_CHECK_RET0(s);
213 while (s->len >= (s->buffer_size - 1))
216 TRACE_SEQ_CHECK_RET0(s);
218 s->buffer[s->len++] = c;
223 void trace_seq_terminate(struct trace_seq *s)
225 TRACE_SEQ_CHECK_RET(s);
227 /* There's always one character left on the buffer */
228 s->buffer[s->len] = 0;
231 int trace_seq_do_printf(struct trace_seq *s)
236 case TRACE_SEQ__GOOD:
237 return printf("%.*s", s->len, s->buffer);
238 case TRACE_SEQ__BUFFER_POISONED:
239 puts("Usage of trace_seq after it was destroyed");
241 case TRACE_SEQ__MEM_ALLOC_FAILED:
242 puts("Can't allocate trace_seq buffer memory");