1 // SPDX-License-Identifier: LGPL-2.1
3 * Copyright (C) 2009 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
12 #include "event-parse.h"
13 #include "event-utils.h"
16 * The TRACE_SEQ_POISON is to catch the use of using
17 * a trace_seq structure after it was destroyed.
19 #define TRACE_SEQ_POISON ((void *)0xdeadbeef)
20 #define TRACE_SEQ_CHECK(s) \
22 if (WARN_ONCE((s)->buffer == TRACE_SEQ_POISON, \
23 "Usage of trace_seq after it was destroyed")) \
24 (s)->state = TRACE_SEQ__BUFFER_POISONED; \
27 #define TRACE_SEQ_CHECK_RET_N(s, n) \
30 if ((s)->state != TRACE_SEQ__GOOD) \
34 #define TRACE_SEQ_CHECK_RET(s) TRACE_SEQ_CHECK_RET_N(s, )
35 #define TRACE_SEQ_CHECK_RET0(s) TRACE_SEQ_CHECK_RET_N(s, 0)
38 * trace_seq_init - initialize the trace_seq structure
39 * @s: a pointer to the trace_seq structure to initialize
41 void trace_seq_init(struct trace_seq *s)
45 s->buffer_size = TRACE_SEQ_BUF_SIZE;
46 s->buffer = malloc(s->buffer_size);
47 if (s->buffer != NULL)
48 s->state = TRACE_SEQ__GOOD;
50 s->state = TRACE_SEQ__MEM_ALLOC_FAILED;
54 * trace_seq_reset - re-initialize the trace_seq structure
55 * @s: a pointer to the trace_seq structure to reset
57 void trace_seq_reset(struct trace_seq *s)
67 * trace_seq_destroy - free up memory of a trace_seq
68 * @s: a pointer to the trace_seq to free the buffer
70 * Only frees the buffer, not the trace_seq struct itself.
72 void trace_seq_destroy(struct trace_seq *s)
76 TRACE_SEQ_CHECK_RET(s);
78 s->buffer = TRACE_SEQ_POISON;
81 static void expand_buffer(struct trace_seq *s)
85 buf = realloc(s->buffer, s->buffer_size + TRACE_SEQ_BUF_SIZE);
86 if (WARN_ONCE(!buf, "Can't allocate trace_seq buffer memory")) {
87 s->state = TRACE_SEQ__MEM_ALLOC_FAILED;
92 s->buffer_size += TRACE_SEQ_BUF_SIZE;
96 * trace_seq_printf - sequence printing of trace information
97 * @s: trace sequence descriptor
98 * @fmt: printf format string
100 * It returns 0 if the trace oversizes the buffer's free
101 * space, 1 otherwise.
103 * The tracer may use either sequence operations or its own
104 * copy to user routines. To simplify formating of a trace
105 * trace_seq_printf is used to store strings into a special
106 * buffer (@s). Then the output may be either used by
107 * the sequencer or pulled into another buffer.
110 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
117 TRACE_SEQ_CHECK_RET0(s);
119 len = (s->buffer_size - 1) - s->len;
122 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
136 * trace_seq_vprintf - sequence printing of trace information
137 * @s: trace sequence descriptor
138 * @fmt: printf format string
140 * The tracer may use either sequence operations or its own
141 * copy to user routines. To simplify formating of a trace
142 * trace_seq_printf is used to store strings into a special
143 * buffer (@s). Then the output may be either used by
144 * the sequencer or pulled into another buffer.
147 trace_seq_vprintf(struct trace_seq *s, const char *fmt, va_list args)
153 TRACE_SEQ_CHECK_RET0(s);
155 len = (s->buffer_size - 1) - s->len;
157 ret = vsnprintf(s->buffer + s->len, len, fmt, args);
170 * trace_seq_puts - trace sequence printing of simple string
171 * @s: trace sequence descriptor
172 * @str: simple string to record
174 * The tracer may use either the sequence operations or its own
175 * copy to user routines. This function records a simple string
176 * into a special buffer (@s) for later retrieval by a sequencer
177 * or other mechanism.
179 int trace_seq_puts(struct trace_seq *s, const char *str)
183 TRACE_SEQ_CHECK_RET0(s);
187 while (len > ((s->buffer_size - 1) - s->len))
190 TRACE_SEQ_CHECK_RET0(s);
192 memcpy(s->buffer + s->len, str, len);
198 int trace_seq_putc(struct trace_seq *s, unsigned char c)
200 TRACE_SEQ_CHECK_RET0(s);
202 while (s->len >= (s->buffer_size - 1))
205 TRACE_SEQ_CHECK_RET0(s);
207 s->buffer[s->len++] = c;
212 void trace_seq_terminate(struct trace_seq *s)
214 TRACE_SEQ_CHECK_RET(s);
216 /* There's always one character left on the buffer */
217 s->buffer[s->len] = 0;
220 int trace_seq_do_fprintf(struct trace_seq *s, FILE *fp)
225 case TRACE_SEQ__GOOD:
226 return fprintf(fp, "%.*s", s->len, s->buffer);
227 case TRACE_SEQ__BUFFER_POISONED:
228 fprintf(fp, "%s\n", "Usage of trace_seq after it was destroyed");
230 case TRACE_SEQ__MEM_ALLOC_FAILED:
231 fprintf(fp, "%s\n", "Can't allocate trace_seq buffer memory");
237 int trace_seq_do_printf(struct trace_seq *s)
239 return trace_seq_do_fprintf(s, stdout);