blob: e454a2c66cac5934165e012f03125d30f8fb0230 [file] [log] [blame]
Steven Rostedtf7d82352012-04-06 00:47:53 +02001/*
2 * Copyright (C) 2009 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
3 *
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!)
9 *
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.
14 *
15 * You should have received a copy of the GNU Lesser General Public
Jon Stanley7b9f6b42012-09-07 16:32:46 -040016 * License along with this program; if not, see <http://www.gnu.org/licenses>
Steven Rostedtf7d82352012-04-06 00:47:53 +020017 *
18 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19 */
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <stdarg.h>
24
Namhyung Kim3c6d8d82013-12-19 18:17:44 +090025#include <asm/bug.h>
Steven Rostedtf7d82352012-04-06 00:47:53 +020026#include "event-parse.h"
Steven Rostedt668fe012012-04-06 00:47:55 +020027#include "event-utils.h"
Steven Rostedtf7d82352012-04-06 00:47:53 +020028
29/*
30 * The TRACE_SEQ_POISON is to catch the use of using
31 * a trace_seq structure after it was destroyed.
32 */
33#define TRACE_SEQ_POISON ((void *)0xdeadbeef)
34#define TRACE_SEQ_CHECK(s) \
35do { \
Namhyung Kim3c6d8d82013-12-19 18:17:44 +090036 if (WARN_ONCE((s)->buffer == TRACE_SEQ_POISON, \
37 "Usage of trace_seq after it was destroyed")) \
38 (s)->state = TRACE_SEQ__BUFFER_POISONED; \
Steven Rostedtf7d82352012-04-06 00:47:53 +020039} while (0)
40
Namhyung Kim3c6d8d82013-12-19 18:17:44 +090041#define TRACE_SEQ_CHECK_RET_N(s, n) \
42do { \
43 TRACE_SEQ_CHECK(s); \
44 if ((s)->state != TRACE_SEQ__GOOD) \
45 return n; \
46} while (0)
47
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)
50
Steven Rostedtf7d82352012-04-06 00:47:53 +020051/**
52 * trace_seq_init - initialize the trace_seq structure
53 * @s: a pointer to the trace_seq structure to initialize
54 */
55void trace_seq_init(struct trace_seq *s)
56{
57 s->len = 0;
58 s->readpos = 0;
59 s->buffer_size = TRACE_SEQ_BUF_SIZE;
60 s->buffer = malloc_or_die(s->buffer_size);
Namhyung Kim3c6d8d82013-12-19 18:17:44 +090061 s->state = TRACE_SEQ__GOOD;
Steven Rostedtf7d82352012-04-06 00:47:53 +020062}
63
64/**
Namhyung Kim6a48aec2013-06-04 14:20:19 +090065 * trace_seq_reset - re-initialize the trace_seq structure
66 * @s: a pointer to the trace_seq structure to reset
67 */
68void trace_seq_reset(struct trace_seq *s)
69{
70 if (!s)
71 return;
72 TRACE_SEQ_CHECK(s);
73 s->len = 0;
74 s->readpos = 0;
75}
76
77/**
Steven Rostedtf7d82352012-04-06 00:47:53 +020078 * trace_seq_destroy - free up memory of a trace_seq
79 * @s: a pointer to the trace_seq to free the buffer
80 *
81 * Only frees the buffer, not the trace_seq struct itself.
82 */
83void trace_seq_destroy(struct trace_seq *s)
84{
85 if (!s)
86 return;
Namhyung Kim3c6d8d82013-12-19 18:17:44 +090087 TRACE_SEQ_CHECK_RET(s);
Steven Rostedtf7d82352012-04-06 00:47:53 +020088 free(s->buffer);
89 s->buffer = TRACE_SEQ_POISON;
90}
91
92static void expand_buffer(struct trace_seq *s)
93{
Namhyung Kim3026bba2014-01-15 10:45:25 +090094 char *buf;
95
96 buf = realloc(s->buffer, s->buffer_size + TRACE_SEQ_BUF_SIZE);
97 if (WARN_ONCE(!buf, "Can't allocate trace_seq buffer memory")) {
Namhyung Kim3c6d8d82013-12-19 18:17:44 +090098 s->state = TRACE_SEQ__MEM_ALLOC_FAILED;
Namhyung Kim3026bba2014-01-15 10:45:25 +090099 return;
100 }
101
102 s->buffer = buf;
103 s->buffer_size += TRACE_SEQ_BUF_SIZE;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200104}
105
106/**
107 * trace_seq_printf - sequence printing of trace information
108 * @s: trace sequence descriptor
109 * @fmt: printf format string
110 *
111 * It returns 0 if the trace oversizes the buffer's free
112 * space, 1 otherwise.
113 *
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.
119 */
120int
121trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
122{
123 va_list ap;
124 int len;
125 int ret;
126
Steven Rostedtf7d82352012-04-06 00:47:53 +0200127 try_again:
Namhyung Kim3c6d8d82013-12-19 18:17:44 +0900128 TRACE_SEQ_CHECK_RET0(s);
129
Steven Rostedtf7d82352012-04-06 00:47:53 +0200130 len = (s->buffer_size - 1) - s->len;
131
132 va_start(ap, fmt);
133 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
134 va_end(ap);
135
136 if (ret >= len) {
137 expand_buffer(s);
138 goto try_again;
139 }
140
141 s->len += ret;
142
143 return 1;
144}
145
146/**
147 * trace_seq_vprintf - sequence printing of trace information
148 * @s: trace sequence descriptor
149 * @fmt: printf format string
150 *
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.
156 */
157int
158trace_seq_vprintf(struct trace_seq *s, const char *fmt, va_list args)
159{
160 int len;
161 int ret;
162
Steven Rostedtf7d82352012-04-06 00:47:53 +0200163 try_again:
Namhyung Kim3c6d8d82013-12-19 18:17:44 +0900164 TRACE_SEQ_CHECK_RET0(s);
165
Steven Rostedtf7d82352012-04-06 00:47:53 +0200166 len = (s->buffer_size - 1) - s->len;
167
168 ret = vsnprintf(s->buffer + s->len, len, fmt, args);
169
170 if (ret >= len) {
171 expand_buffer(s);
172 goto try_again;
173 }
174
175 s->len += ret;
176
177 return len;
178}
179
180/**
181 * trace_seq_puts - trace sequence printing of simple string
182 * @s: trace sequence descriptor
183 * @str: simple string to record
184 *
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.
189 */
190int trace_seq_puts(struct trace_seq *s, const char *str)
191{
192 int len;
193
Namhyung Kim3c6d8d82013-12-19 18:17:44 +0900194 TRACE_SEQ_CHECK_RET0(s);
Steven Rostedtf7d82352012-04-06 00:47:53 +0200195
196 len = strlen(str);
197
198 while (len > ((s->buffer_size - 1) - s->len))
199 expand_buffer(s);
200
Namhyung Kim3c6d8d82013-12-19 18:17:44 +0900201 TRACE_SEQ_CHECK_RET0(s);
202
Steven Rostedtf7d82352012-04-06 00:47:53 +0200203 memcpy(s->buffer + s->len, str, len);
204 s->len += len;
205
206 return len;
207}
208
209int trace_seq_putc(struct trace_seq *s, unsigned char c)
210{
Namhyung Kim3c6d8d82013-12-19 18:17:44 +0900211 TRACE_SEQ_CHECK_RET0(s);
Steven Rostedtf7d82352012-04-06 00:47:53 +0200212
213 while (s->len >= (s->buffer_size - 1))
214 expand_buffer(s);
215
Namhyung Kim3c6d8d82013-12-19 18:17:44 +0900216 TRACE_SEQ_CHECK_RET0(s);
217
Steven Rostedtf7d82352012-04-06 00:47:53 +0200218 s->buffer[s->len++] = c;
219
220 return 1;
221}
222
223void trace_seq_terminate(struct trace_seq *s)
224{
Namhyung Kim3c6d8d82013-12-19 18:17:44 +0900225 TRACE_SEQ_CHECK_RET(s);
Steven Rostedtf7d82352012-04-06 00:47:53 +0200226
227 /* There's always one character left on the buffer */
228 s->buffer[s->len] = 0;
229}
230
231int trace_seq_do_printf(struct trace_seq *s)
232{
233 TRACE_SEQ_CHECK(s);
Namhyung Kim3c6d8d82013-12-19 18:17:44 +0900234
235 switch (s->state) {
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");
240 break;
241 case TRACE_SEQ__MEM_ALLOC_FAILED:
242 puts("Can't allocate trace_seq buffer memory");
243 break;
244 }
245 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200246}