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; |
Namhyung Kim | 504586e | 2014-01-15 10:45:26 +0900 | [diff] [blame] | 60 | s->buffer = malloc(s->buffer_size); |
| 61 | if (s->buffer != NULL) |
| 62 | s->state = TRACE_SEQ__GOOD; |
| 63 | else |
| 64 | s->state = TRACE_SEQ__MEM_ALLOC_FAILED; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** |
Namhyung Kim | 6a48aec | 2013-06-04 14:20:19 +0900 | [diff] [blame] | 68 | * trace_seq_reset - re-initialize the trace_seq structure |
| 69 | * @s: a pointer to the trace_seq structure to reset |
| 70 | */ |
| 71 | void trace_seq_reset(struct trace_seq *s) |
| 72 | { |
| 73 | if (!s) |
| 74 | return; |
| 75 | TRACE_SEQ_CHECK(s); |
| 76 | s->len = 0; |
| 77 | s->readpos = 0; |
| 78 | } |
| 79 | |
| 80 | /** |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 81 | * trace_seq_destroy - free up memory of a trace_seq |
| 82 | * @s: a pointer to the trace_seq to free the buffer |
| 83 | * |
| 84 | * Only frees the buffer, not the trace_seq struct itself. |
| 85 | */ |
| 86 | void trace_seq_destroy(struct trace_seq *s) |
| 87 | { |
| 88 | if (!s) |
| 89 | return; |
Namhyung Kim | 3c6d8d8 | 2013-12-19 18:17:44 +0900 | [diff] [blame] | 90 | TRACE_SEQ_CHECK_RET(s); |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 91 | free(s->buffer); |
| 92 | s->buffer = TRACE_SEQ_POISON; |
| 93 | } |
| 94 | |
| 95 | static void expand_buffer(struct trace_seq *s) |
| 96 | { |
Namhyung Kim | 3026bba | 2014-01-15 10:45:25 +0900 | [diff] [blame] | 97 | char *buf; |
| 98 | |
| 99 | buf = realloc(s->buffer, s->buffer_size + TRACE_SEQ_BUF_SIZE); |
| 100 | if (WARN_ONCE(!buf, "Can't allocate trace_seq buffer memory")) { |
Namhyung Kim | 3c6d8d8 | 2013-12-19 18:17:44 +0900 | [diff] [blame] | 101 | s->state = TRACE_SEQ__MEM_ALLOC_FAILED; |
Namhyung Kim | 3026bba | 2014-01-15 10:45:25 +0900 | [diff] [blame] | 102 | return; |
| 103 | } |
| 104 | |
| 105 | s->buffer = buf; |
| 106 | s->buffer_size += TRACE_SEQ_BUF_SIZE; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
| 110 | * trace_seq_printf - sequence printing of trace information |
| 111 | * @s: trace sequence descriptor |
| 112 | * @fmt: printf format string |
| 113 | * |
| 114 | * It returns 0 if the trace oversizes the buffer's free |
| 115 | * space, 1 otherwise. |
| 116 | * |
| 117 | * The tracer may use either sequence operations or its own |
| 118 | * copy to user routines. To simplify formating of a trace |
| 119 | * trace_seq_printf is used to store strings into a special |
| 120 | * buffer (@s). Then the output may be either used by |
| 121 | * the sequencer or pulled into another buffer. |
| 122 | */ |
| 123 | int |
| 124 | trace_seq_printf(struct trace_seq *s, const char *fmt, ...) |
| 125 | { |
| 126 | va_list ap; |
| 127 | int len; |
| 128 | int ret; |
| 129 | |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 130 | try_again: |
Namhyung Kim | 3c6d8d8 | 2013-12-19 18:17:44 +0900 | [diff] [blame] | 131 | TRACE_SEQ_CHECK_RET0(s); |
| 132 | |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 133 | len = (s->buffer_size - 1) - s->len; |
| 134 | |
| 135 | va_start(ap, fmt); |
| 136 | ret = vsnprintf(s->buffer + s->len, len, fmt, ap); |
| 137 | va_end(ap); |
| 138 | |
| 139 | if (ret >= len) { |
| 140 | expand_buffer(s); |
| 141 | goto try_again; |
| 142 | } |
| 143 | |
| 144 | s->len += ret; |
| 145 | |
| 146 | return 1; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * trace_seq_vprintf - sequence printing of trace information |
| 151 | * @s: trace sequence descriptor |
| 152 | * @fmt: printf format string |
| 153 | * |
| 154 | * The tracer may use either sequence operations or its own |
| 155 | * copy to user routines. To simplify formating of a trace |
| 156 | * trace_seq_printf is used to store strings into a special |
| 157 | * buffer (@s). Then the output may be either used by |
| 158 | * the sequencer or pulled into another buffer. |
| 159 | */ |
| 160 | int |
| 161 | trace_seq_vprintf(struct trace_seq *s, const char *fmt, va_list args) |
| 162 | { |
| 163 | int len; |
| 164 | int ret; |
| 165 | |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 166 | try_again: |
Namhyung Kim | 3c6d8d8 | 2013-12-19 18:17:44 +0900 | [diff] [blame] | 167 | TRACE_SEQ_CHECK_RET0(s); |
| 168 | |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 169 | len = (s->buffer_size - 1) - s->len; |
| 170 | |
| 171 | ret = vsnprintf(s->buffer + s->len, len, fmt, args); |
| 172 | |
| 173 | if (ret >= len) { |
| 174 | expand_buffer(s); |
| 175 | goto try_again; |
| 176 | } |
| 177 | |
| 178 | s->len += ret; |
| 179 | |
| 180 | return len; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * trace_seq_puts - trace sequence printing of simple string |
| 185 | * @s: trace sequence descriptor |
| 186 | * @str: simple string to record |
| 187 | * |
| 188 | * The tracer may use either the sequence operations or its own |
| 189 | * copy to user routines. This function records a simple string |
| 190 | * into a special buffer (@s) for later retrieval by a sequencer |
| 191 | * or other mechanism. |
| 192 | */ |
| 193 | int trace_seq_puts(struct trace_seq *s, const char *str) |
| 194 | { |
| 195 | int len; |
| 196 | |
Namhyung Kim | 3c6d8d8 | 2013-12-19 18:17:44 +0900 | [diff] [blame] | 197 | TRACE_SEQ_CHECK_RET0(s); |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 198 | |
| 199 | len = strlen(str); |
| 200 | |
| 201 | while (len > ((s->buffer_size - 1) - s->len)) |
| 202 | expand_buffer(s); |
| 203 | |
Namhyung Kim | 3c6d8d8 | 2013-12-19 18:17:44 +0900 | [diff] [blame] | 204 | TRACE_SEQ_CHECK_RET0(s); |
| 205 | |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 206 | memcpy(s->buffer + s->len, str, len); |
| 207 | s->len += len; |
| 208 | |
| 209 | return len; |
| 210 | } |
| 211 | |
| 212 | int trace_seq_putc(struct trace_seq *s, unsigned char c) |
| 213 | { |
Namhyung Kim | 3c6d8d8 | 2013-12-19 18:17:44 +0900 | [diff] [blame] | 214 | TRACE_SEQ_CHECK_RET0(s); |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 215 | |
| 216 | while (s->len >= (s->buffer_size - 1)) |
| 217 | expand_buffer(s); |
| 218 | |
Namhyung Kim | 3c6d8d8 | 2013-12-19 18:17:44 +0900 | [diff] [blame] | 219 | TRACE_SEQ_CHECK_RET0(s); |
| 220 | |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 221 | s->buffer[s->len++] = c; |
| 222 | |
| 223 | return 1; |
| 224 | } |
| 225 | |
| 226 | void trace_seq_terminate(struct trace_seq *s) |
| 227 | { |
Namhyung Kim | 3c6d8d8 | 2013-12-19 18:17:44 +0900 | [diff] [blame] | 228 | TRACE_SEQ_CHECK_RET(s); |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 229 | |
| 230 | /* There's always one character left on the buffer */ |
| 231 | s->buffer[s->len] = 0; |
| 232 | } |
| 233 | |
Arnaldo Carvalho de Melo | 402bb4e | 2015-02-03 12:44:09 -0300 | [diff] [blame] | 234 | int trace_seq_do_fprintf(struct trace_seq *s, FILE *fp) |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 235 | { |
| 236 | TRACE_SEQ_CHECK(s); |
Namhyung Kim | 3c6d8d8 | 2013-12-19 18:17:44 +0900 | [diff] [blame] | 237 | |
| 238 | switch (s->state) { |
| 239 | case TRACE_SEQ__GOOD: |
Arnaldo Carvalho de Melo | 402bb4e | 2015-02-03 12:44:09 -0300 | [diff] [blame] | 240 | return fprintf(fp, "%.*s", s->len, s->buffer); |
Namhyung Kim | 3c6d8d8 | 2013-12-19 18:17:44 +0900 | [diff] [blame] | 241 | case TRACE_SEQ__BUFFER_POISONED: |
Arnaldo Carvalho de Melo | 402bb4e | 2015-02-03 12:44:09 -0300 | [diff] [blame] | 242 | fprintf(fp, "%s\n", "Usage of trace_seq after it was destroyed"); |
Namhyung Kim | 3c6d8d8 | 2013-12-19 18:17:44 +0900 | [diff] [blame] | 243 | break; |
| 244 | case TRACE_SEQ__MEM_ALLOC_FAILED: |
Arnaldo Carvalho de Melo | 402bb4e | 2015-02-03 12:44:09 -0300 | [diff] [blame] | 245 | fprintf(fp, "%s\n", "Can't allocate trace_seq buffer memory"); |
Namhyung Kim | 3c6d8d8 | 2013-12-19 18:17:44 +0900 | [diff] [blame] | 246 | break; |
| 247 | } |
| 248 | return -1; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 249 | } |
Arnaldo Carvalho de Melo | 402bb4e | 2015-02-03 12:44:09 -0300 | [diff] [blame] | 250 | |
| 251 | int trace_seq_do_printf(struct trace_seq *s) |
| 252 | { |
| 253 | return trace_seq_do_fprintf(s, stdout); |
| 254 | } |