Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 1 | /* |
| 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 Stanley | 7b9f6b4 | 2012-09-07 16:32:46 -0400 | [diff] [blame] | 16 | * License along with this program; if not, see <http://www.gnu.org/licenses> |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 17 | * |
| 18 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 19 | */ |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <stdarg.h> |
| 24 | |
Namhyung Kim | 3c6d8d8 | 2013-12-19 18:17:44 +0900 | [diff] [blame^] | 25 | #include <asm/bug.h> |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 26 | #include "event-parse.h" |
Steven Rostedt | 668fe01 | 2012-04-06 00:47:55 +0200 | [diff] [blame] | 27 | #include "event-utils.h" |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 28 | |
| 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) \ |
| 35 | do { \ |
Namhyung Kim | 3c6d8d8 | 2013-12-19 18:17:44 +0900 | [diff] [blame^] | 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; \ |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 39 | } while (0) |
| 40 | |
Namhyung Kim | 3c6d8d8 | 2013-12-19 18:17:44 +0900 | [diff] [blame^] | 41 | #define TRACE_SEQ_CHECK_RET_N(s, n) \ |
| 42 | do { \ |
| 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 Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 51 | /** |
| 52 | * trace_seq_init - initialize the trace_seq structure |
| 53 | * @s: a pointer to the trace_seq structure to initialize |
| 54 | */ |
| 55 | void 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 Kim | 3c6d8d8 | 2013-12-19 18:17:44 +0900 | [diff] [blame^] | 61 | s->state = TRACE_SEQ__GOOD; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /** |
Namhyung Kim | 6a48aec | 2013-06-04 14:20:19 +0900 | [diff] [blame] | 65 | * trace_seq_reset - re-initialize the trace_seq structure |
| 66 | * @s: a pointer to the trace_seq structure to reset |
| 67 | */ |
| 68 | void 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 Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 78 | * 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 | */ |
| 83 | void trace_seq_destroy(struct trace_seq *s) |
| 84 | { |
| 85 | if (!s) |
| 86 | return; |
Namhyung Kim | 3c6d8d8 | 2013-12-19 18:17:44 +0900 | [diff] [blame^] | 87 | TRACE_SEQ_CHECK_RET(s); |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 88 | free(s->buffer); |
| 89 | s->buffer = TRACE_SEQ_POISON; |
| 90 | } |
| 91 | |
| 92 | static void expand_buffer(struct trace_seq *s) |
| 93 | { |
| 94 | s->buffer_size += TRACE_SEQ_BUF_SIZE; |
| 95 | s->buffer = realloc(s->buffer, s->buffer_size); |
Namhyung Kim | 3c6d8d8 | 2013-12-19 18:17:44 +0900 | [diff] [blame^] | 96 | if (WARN_ONCE(!s->buffer, |
| 97 | "Can't allocate trace_seq buffer memory")) |
| 98 | s->state = TRACE_SEQ__MEM_ALLOC_FAILED; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /** |
| 102 | * trace_seq_printf - sequence printing of trace information |
| 103 | * @s: trace sequence descriptor |
| 104 | * @fmt: printf format string |
| 105 | * |
| 106 | * It returns 0 if the trace oversizes the buffer's free |
| 107 | * space, 1 otherwise. |
| 108 | * |
| 109 | * The tracer may use either sequence operations or its own |
| 110 | * copy to user routines. To simplify formating of a trace |
| 111 | * trace_seq_printf is used to store strings into a special |
| 112 | * buffer (@s). Then the output may be either used by |
| 113 | * the sequencer or pulled into another buffer. |
| 114 | */ |
| 115 | int |
| 116 | trace_seq_printf(struct trace_seq *s, const char *fmt, ...) |
| 117 | { |
| 118 | va_list ap; |
| 119 | int len; |
| 120 | int ret; |
| 121 | |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 122 | try_again: |
Namhyung Kim | 3c6d8d8 | 2013-12-19 18:17:44 +0900 | [diff] [blame^] | 123 | TRACE_SEQ_CHECK_RET0(s); |
| 124 | |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 125 | len = (s->buffer_size - 1) - s->len; |
| 126 | |
| 127 | va_start(ap, fmt); |
| 128 | ret = vsnprintf(s->buffer + s->len, len, fmt, ap); |
| 129 | va_end(ap); |
| 130 | |
| 131 | if (ret >= len) { |
| 132 | expand_buffer(s); |
| 133 | goto try_again; |
| 134 | } |
| 135 | |
| 136 | s->len += ret; |
| 137 | |
| 138 | return 1; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * trace_seq_vprintf - sequence printing of trace information |
| 143 | * @s: trace sequence descriptor |
| 144 | * @fmt: printf format string |
| 145 | * |
| 146 | * The tracer may use either sequence operations or its own |
| 147 | * copy to user routines. To simplify formating of a trace |
| 148 | * trace_seq_printf is used to store strings into a special |
| 149 | * buffer (@s). Then the output may be either used by |
| 150 | * the sequencer or pulled into another buffer. |
| 151 | */ |
| 152 | int |
| 153 | trace_seq_vprintf(struct trace_seq *s, const char *fmt, va_list args) |
| 154 | { |
| 155 | int len; |
| 156 | int ret; |
| 157 | |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 158 | try_again: |
Namhyung Kim | 3c6d8d8 | 2013-12-19 18:17:44 +0900 | [diff] [blame^] | 159 | TRACE_SEQ_CHECK_RET0(s); |
| 160 | |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 161 | len = (s->buffer_size - 1) - s->len; |
| 162 | |
| 163 | ret = vsnprintf(s->buffer + s->len, len, fmt, args); |
| 164 | |
| 165 | if (ret >= len) { |
| 166 | expand_buffer(s); |
| 167 | goto try_again; |
| 168 | } |
| 169 | |
| 170 | s->len += ret; |
| 171 | |
| 172 | return len; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * trace_seq_puts - trace sequence printing of simple string |
| 177 | * @s: trace sequence descriptor |
| 178 | * @str: simple string to record |
| 179 | * |
| 180 | * The tracer may use either the sequence operations or its own |
| 181 | * copy to user routines. This function records a simple string |
| 182 | * into a special buffer (@s) for later retrieval by a sequencer |
| 183 | * or other mechanism. |
| 184 | */ |
| 185 | int trace_seq_puts(struct trace_seq *s, const char *str) |
| 186 | { |
| 187 | int len; |
| 188 | |
Namhyung Kim | 3c6d8d8 | 2013-12-19 18:17:44 +0900 | [diff] [blame^] | 189 | TRACE_SEQ_CHECK_RET0(s); |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 190 | |
| 191 | len = strlen(str); |
| 192 | |
| 193 | while (len > ((s->buffer_size - 1) - s->len)) |
| 194 | expand_buffer(s); |
| 195 | |
Namhyung Kim | 3c6d8d8 | 2013-12-19 18:17:44 +0900 | [diff] [blame^] | 196 | TRACE_SEQ_CHECK_RET0(s); |
| 197 | |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 198 | memcpy(s->buffer + s->len, str, len); |
| 199 | s->len += len; |
| 200 | |
| 201 | return len; |
| 202 | } |
| 203 | |
| 204 | int trace_seq_putc(struct trace_seq *s, unsigned char c) |
| 205 | { |
Namhyung Kim | 3c6d8d8 | 2013-12-19 18:17:44 +0900 | [diff] [blame^] | 206 | TRACE_SEQ_CHECK_RET0(s); |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 207 | |
| 208 | while (s->len >= (s->buffer_size - 1)) |
| 209 | expand_buffer(s); |
| 210 | |
Namhyung Kim | 3c6d8d8 | 2013-12-19 18:17:44 +0900 | [diff] [blame^] | 211 | TRACE_SEQ_CHECK_RET0(s); |
| 212 | |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 213 | s->buffer[s->len++] = c; |
| 214 | |
| 215 | return 1; |
| 216 | } |
| 217 | |
| 218 | void trace_seq_terminate(struct trace_seq *s) |
| 219 | { |
Namhyung Kim | 3c6d8d8 | 2013-12-19 18:17:44 +0900 | [diff] [blame^] | 220 | TRACE_SEQ_CHECK_RET(s); |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 221 | |
| 222 | /* There's always one character left on the buffer */ |
| 223 | s->buffer[s->len] = 0; |
| 224 | } |
| 225 | |
| 226 | int trace_seq_do_printf(struct trace_seq *s) |
| 227 | { |
| 228 | TRACE_SEQ_CHECK(s); |
Namhyung Kim | 3c6d8d8 | 2013-12-19 18:17:44 +0900 | [diff] [blame^] | 229 | |
| 230 | switch (s->state) { |
| 231 | case TRACE_SEQ__GOOD: |
| 232 | return printf("%.*s", s->len, s->buffer); |
| 233 | case TRACE_SEQ__BUFFER_POISONED: |
| 234 | puts("Usage of trace_seq after it was destroyed"); |
| 235 | break; |
| 236 | case TRACE_SEQ__MEM_ALLOC_FAILED: |
| 237 | puts("Can't allocate trace_seq buffer memory"); |
| 238 | break; |
| 239 | } |
| 240 | return -1; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 241 | } |