x86/xen: resume timer irqs early
[linux/fpc-iii.git] / tools / lib / traceevent / trace-seq.c
blobd7f2e68bc5b91d190e2ad5cd34388b45d957372a
1 /*
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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdarg.h>
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) \
34 do { \
35 if ((s)->buffer == TRACE_SEQ_POISON) \
36 die("Usage of trace_seq after it was destroyed"); \
37 } while (0)
39 /**
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)
45 s->len = 0;
46 s->readpos = 0;
47 s->buffer_size = TRACE_SEQ_BUF_SIZE;
48 s->buffer = malloc_or_die(s->buffer_size);
51 /**
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)
57 if (!s)
58 return;
59 TRACE_SEQ_CHECK(s);
60 s->len = 0;
61 s->readpos = 0;
64 /**
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)
72 if (!s)
73 return;
74 TRACE_SEQ_CHECK(s);
75 free(s->buffer);
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);
83 if (!s->buffer)
84 die("Can't allocate trace_seq buffer memory");
87 /**
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
93 * space, 1 otherwise.
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, ...)
104 va_list ap;
105 int len;
106 int ret;
108 TRACE_SEQ_CHECK(s);
110 try_again:
111 len = (s->buffer_size - 1) - s->len;
113 va_start(ap, fmt);
114 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
115 va_end(ap);
117 if (ret >= len) {
118 expand_buffer(s);
119 goto try_again;
122 s->len += ret;
124 return 1;
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)
141 int len;
142 int ret;
144 TRACE_SEQ_CHECK(s);
146 try_again:
147 len = (s->buffer_size - 1) - s->len;
149 ret = vsnprintf(s->buffer + s->len, len, fmt, args);
151 if (ret >= len) {
152 expand_buffer(s);
153 goto try_again;
156 s->len += ret;
158 return len;
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)
173 int len;
175 TRACE_SEQ_CHECK(s);
177 len = strlen(str);
179 while (len > ((s->buffer_size - 1) - s->len))
180 expand_buffer(s);
182 memcpy(s->buffer + s->len, str, len);
183 s->len += len;
185 return len;
188 int trace_seq_putc(struct trace_seq *s, unsigned char c)
190 TRACE_SEQ_CHECK(s);
192 while (s->len >= (s->buffer_size - 1))
193 expand_buffer(s);
195 s->buffer[s->len++] = c;
197 return 1;
200 void trace_seq_terminate(struct trace_seq *s)
202 TRACE_SEQ_CHECK(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)
210 TRACE_SEQ_CHECK(s);
211 return printf("%.*s", s->len, s->buffer);