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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 #include "event-parse.h"
26 #include "event-utils.h"
29 * The TRACE_SEQ_POISON is to catch the use of using
30 * a trace_seq structure after it was destroyed.
32 #define TRACE_SEQ_POISON ((void *)0xdeadbeef)
33 #define TRACE_SEQ_CHECK(s) \
35 if ((s)->buffer == TRACE_SEQ_POISON) \
36 die("Usage of trace_seq after it was destroyed"); \
40 * trace_seq_init - initialize the trace_seq structure
41 * @s: a pointer to the trace_seq structure to initialize
43 void trace_seq_init(struct trace_seq *s)
47 s->buffer_size = TRACE_SEQ_BUF_SIZE;
48 s->buffer = malloc_or_die(s->buffer_size);
52 * trace_seq_reset - re-initialize the trace_seq structure
53 * @s: a pointer to the trace_seq structure to reset
55 void trace_seq_reset(struct trace_seq *s)
65 * trace_seq_destroy - free up memory of a trace_seq
66 * @s: a pointer to the trace_seq to free the buffer
68 * Only frees the buffer, not the trace_seq struct itself.
70 void trace_seq_destroy(struct trace_seq *s)
76 s->buffer = TRACE_SEQ_POISON;
79 static void expand_buffer(struct trace_seq *s)
81 s->buffer_size += TRACE_SEQ_BUF_SIZE;
82 s->buffer = realloc(s->buffer, s->buffer_size);
84 die("Can't allocate trace_seq buffer memory");
88 * trace_seq_printf - sequence printing of trace information
89 * @s: trace sequence descriptor
90 * @fmt: printf format string
92 * It returns 0 if the trace oversizes the buffer's free
95 * The tracer may use either sequence operations or its own
96 * copy to user routines. To simplify formating of a trace
97 * trace_seq_printf is used to store strings into a special
98 * buffer (@s). Then the output may be either used by
99 * the sequencer or pulled into another buffer.
102 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
111 len = (s->buffer_size - 1) - s->len;
114 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
128 * trace_seq_vprintf - sequence printing of trace information
129 * @s: trace sequence descriptor
130 * @fmt: printf format string
132 * The tracer may use either sequence operations or its own
133 * copy to user routines. To simplify formating of a trace
134 * trace_seq_printf is used to store strings into a special
135 * buffer (@s). Then the output may be either used by
136 * the sequencer or pulled into another buffer.
139 trace_seq_vprintf(struct trace_seq *s, const char *fmt, va_list args)
147 len = (s->buffer_size - 1) - s->len;
149 ret = vsnprintf(s->buffer + s->len, len, fmt, args);
162 * trace_seq_puts - trace sequence printing of simple string
163 * @s: trace sequence descriptor
164 * @str: simple string to record
166 * The tracer may use either the sequence operations or its own
167 * copy to user routines. This function records a simple string
168 * into a special buffer (@s) for later retrieval by a sequencer
169 * or other mechanism.
171 int trace_seq_puts(struct trace_seq *s, const char *str)
179 while (len > ((s->buffer_size - 1) - s->len))
182 memcpy(s->buffer + s->len, str, len);
188 int trace_seq_putc(struct trace_seq *s, unsigned char c)
192 while (s->len >= (s->buffer_size - 1))
195 s->buffer[s->len++] = c;
200 void trace_seq_terminate(struct trace_seq *s)
204 /* There's always one character left on the buffer */
205 s->buffer[s->len] = 0;
208 int trace_seq_do_printf(struct trace_seq *s)
211 return printf("%.*s", s->len, s->buffer);