Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: LGPL-2.1 |
| 2 | /* |
| 3 | * Copyright (C) 2009 Red Hat Inc, Steven Rostedt <srostedt@redhat.com> |
| 4 | * |
| 5 | */ |
| 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
| 9 | #include <stdarg.h> |
| 10 | |
| 11 | #include <asm/bug.h> |
| 12 | #include "event-parse.h" |
| 13 | #include "event-utils.h" |
| 14 | |
| 15 | /* |
| 16 | * The TRACE_SEQ_POISON is to catch the use of using |
| 17 | * a trace_seq structure after it was destroyed. |
| 18 | */ |
| 19 | #define TRACE_SEQ_POISON ((void *)0xdeadbeef) |
| 20 | #define TRACE_SEQ_CHECK(s) \ |
| 21 | do { \ |
| 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; \ |
| 25 | } while (0) |
| 26 | |
| 27 | #define TRACE_SEQ_CHECK_RET_N(s, n) \ |
| 28 | do { \ |
| 29 | TRACE_SEQ_CHECK(s); \ |
| 30 | if ((s)->state != TRACE_SEQ__GOOD) \ |
| 31 | return n; \ |
| 32 | } while (0) |
| 33 | |
| 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) |
| 36 | |
| 37 | /** |
| 38 | * trace_seq_init - initialize the trace_seq structure |
| 39 | * @s: a pointer to the trace_seq structure to initialize |
| 40 | */ |
| 41 | void trace_seq_init(struct trace_seq *s) |
| 42 | { |
| 43 | s->len = 0; |
| 44 | s->readpos = 0; |
| 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; |
| 49 | else |
| 50 | s->state = TRACE_SEQ__MEM_ALLOC_FAILED; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * trace_seq_reset - re-initialize the trace_seq structure |
| 55 | * @s: a pointer to the trace_seq structure to reset |
| 56 | */ |
| 57 | void trace_seq_reset(struct trace_seq *s) |
| 58 | { |
| 59 | if (!s) |
| 60 | return; |
| 61 | TRACE_SEQ_CHECK(s); |
| 62 | s->len = 0; |
| 63 | s->readpos = 0; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * trace_seq_destroy - free up memory of a trace_seq |
| 68 | * @s: a pointer to the trace_seq to free the buffer |
| 69 | * |
| 70 | * Only frees the buffer, not the trace_seq struct itself. |
| 71 | */ |
| 72 | void trace_seq_destroy(struct trace_seq *s) |
| 73 | { |
| 74 | if (!s) |
| 75 | return; |
| 76 | TRACE_SEQ_CHECK_RET(s); |
| 77 | free(s->buffer); |
| 78 | s->buffer = TRACE_SEQ_POISON; |
| 79 | } |
| 80 | |
| 81 | static void expand_buffer(struct trace_seq *s) |
| 82 | { |
| 83 | char *buf; |
| 84 | |
| 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; |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | s->buffer = buf; |
| 92 | s->buffer_size += TRACE_SEQ_BUF_SIZE; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * trace_seq_printf - sequence printing of trace information |
| 97 | * @s: trace sequence descriptor |
| 98 | * @fmt: printf format string |
| 99 | * |
| 100 | * It returns 0 if the trace oversizes the buffer's free |
| 101 | * space, 1 otherwise. |
| 102 | * |
| 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. |
| 108 | */ |
| 109 | int |
| 110 | trace_seq_printf(struct trace_seq *s, const char *fmt, ...) |
| 111 | { |
| 112 | va_list ap; |
| 113 | int len; |
| 114 | int ret; |
| 115 | |
| 116 | try_again: |
| 117 | TRACE_SEQ_CHECK_RET0(s); |
| 118 | |
| 119 | len = (s->buffer_size - 1) - s->len; |
| 120 | |
| 121 | va_start(ap, fmt); |
| 122 | ret = vsnprintf(s->buffer + s->len, len, fmt, ap); |
| 123 | va_end(ap); |
| 124 | |
| 125 | if (ret >= len) { |
| 126 | expand_buffer(s); |
| 127 | goto try_again; |
| 128 | } |
| 129 | |
| 130 | s->len += ret; |
| 131 | |
| 132 | return 1; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * trace_seq_vprintf - sequence printing of trace information |
| 137 | * @s: trace sequence descriptor |
| 138 | * @fmt: printf format string |
| 139 | * |
| 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. |
| 145 | */ |
| 146 | int |
| 147 | trace_seq_vprintf(struct trace_seq *s, const char *fmt, va_list args) |
| 148 | { |
| 149 | int len; |
| 150 | int ret; |
| 151 | |
| 152 | try_again: |
| 153 | TRACE_SEQ_CHECK_RET0(s); |
| 154 | |
| 155 | len = (s->buffer_size - 1) - s->len; |
| 156 | |
| 157 | ret = vsnprintf(s->buffer + s->len, len, fmt, args); |
| 158 | |
| 159 | if (ret >= len) { |
| 160 | expand_buffer(s); |
| 161 | goto try_again; |
| 162 | } |
| 163 | |
| 164 | s->len += ret; |
| 165 | |
| 166 | return len; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * trace_seq_puts - trace sequence printing of simple string |
| 171 | * @s: trace sequence descriptor |
| 172 | * @str: simple string to record |
| 173 | * |
| 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. |
| 178 | */ |
| 179 | int trace_seq_puts(struct trace_seq *s, const char *str) |
| 180 | { |
| 181 | int len; |
| 182 | |
| 183 | TRACE_SEQ_CHECK_RET0(s); |
| 184 | |
| 185 | len = strlen(str); |
| 186 | |
| 187 | while (len > ((s->buffer_size - 1) - s->len)) |
| 188 | expand_buffer(s); |
| 189 | |
| 190 | TRACE_SEQ_CHECK_RET0(s); |
| 191 | |
| 192 | memcpy(s->buffer + s->len, str, len); |
| 193 | s->len += len; |
| 194 | |
| 195 | return len; |
| 196 | } |
| 197 | |
| 198 | int trace_seq_putc(struct trace_seq *s, unsigned char c) |
| 199 | { |
| 200 | TRACE_SEQ_CHECK_RET0(s); |
| 201 | |
| 202 | while (s->len >= (s->buffer_size - 1)) |
| 203 | expand_buffer(s); |
| 204 | |
| 205 | TRACE_SEQ_CHECK_RET0(s); |
| 206 | |
| 207 | s->buffer[s->len++] = c; |
| 208 | |
| 209 | return 1; |
| 210 | } |
| 211 | |
| 212 | void trace_seq_terminate(struct trace_seq *s) |
| 213 | { |
| 214 | TRACE_SEQ_CHECK_RET(s); |
| 215 | |
| 216 | /* There's always one character left on the buffer */ |
| 217 | s->buffer[s->len] = 0; |
| 218 | } |
| 219 | |
| 220 | int trace_seq_do_fprintf(struct trace_seq *s, FILE *fp) |
| 221 | { |
| 222 | TRACE_SEQ_CHECK(s); |
| 223 | |
| 224 | switch (s->state) { |
| 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"); |
| 229 | break; |
| 230 | case TRACE_SEQ__MEM_ALLOC_FAILED: |
| 231 | fprintf(fp, "%s\n", "Can't allocate trace_seq buffer memory"); |
| 232 | break; |
| 233 | } |
| 234 | return -1; |
| 235 | } |
| 236 | |
| 237 | int trace_seq_do_printf(struct trace_seq *s) |
| 238 | { |
| 239 | return trace_seq_do_fprintf(s, stdout); |
| 240 | } |