trace-seq.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. #include <asm/bug.h>
  11. #include "event-parse.h"
  12. #include "event-utils.h"
  13. /*
  14. * The TRACE_SEQ_POISON is to catch the use of using
  15. * a trace_seq structure after it was destroyed.
  16. */
  17. #define TRACE_SEQ_POISON ((void *)0xdeadbeef)
  18. #define TRACE_SEQ_CHECK(s) \
  19. do { \
  20. if (WARN_ONCE((s)->buffer == TRACE_SEQ_POISON, \
  21. "Usage of trace_seq after it was destroyed")) \
  22. (s)->state = TRACE_SEQ__BUFFER_POISONED; \
  23. } while (0)
  24. #define TRACE_SEQ_CHECK_RET_N(s, n) \
  25. do { \
  26. TRACE_SEQ_CHECK(s); \
  27. if ((s)->state != TRACE_SEQ__GOOD) \
  28. return n; \
  29. } while (0)
  30. #define TRACE_SEQ_CHECK_RET(s) TRACE_SEQ_CHECK_RET_N(s, )
  31. #define TRACE_SEQ_CHECK_RET0(s) TRACE_SEQ_CHECK_RET_N(s, 0)
  32. /**
  33. * trace_seq_init - initialize the trace_seq structure
  34. * @s: a pointer to the trace_seq structure to initialize
  35. */
  36. void trace_seq_init(struct trace_seq *s)
  37. {
  38. s->len = 0;
  39. s->readpos = 0;
  40. s->buffer_size = TRACE_SEQ_BUF_SIZE;
  41. s->buffer = malloc(s->buffer_size);
  42. if (s->buffer != NULL)
  43. s->state = TRACE_SEQ__GOOD;
  44. else
  45. s->state = TRACE_SEQ__MEM_ALLOC_FAILED;
  46. }
  47. /**
  48. * trace_seq_reset - re-initialize the trace_seq structure
  49. * @s: a pointer to the trace_seq structure to reset
  50. */
  51. void trace_seq_reset(struct trace_seq *s)
  52. {
  53. if (!s)
  54. return;
  55. TRACE_SEQ_CHECK(s);
  56. s->len = 0;
  57. s->readpos = 0;
  58. }
  59. /**
  60. * trace_seq_destroy - free up memory of a trace_seq
  61. * @s: a pointer to the trace_seq to free the buffer
  62. *
  63. * Only frees the buffer, not the trace_seq struct itself.
  64. */
  65. void trace_seq_destroy(struct trace_seq *s)
  66. {
  67. if (!s)
  68. return;
  69. TRACE_SEQ_CHECK_RET(s);
  70. free(s->buffer);
  71. s->buffer = TRACE_SEQ_POISON;
  72. }
  73. static void expand_buffer(struct trace_seq *s)
  74. {
  75. char *buf;
  76. buf = realloc(s->buffer, s->buffer_size + TRACE_SEQ_BUF_SIZE);
  77. if (WARN_ONCE(!buf, "Can't allocate trace_seq buffer memory")) {
  78. s->state = TRACE_SEQ__MEM_ALLOC_FAILED;
  79. return;
  80. }
  81. s->buffer = buf;
  82. s->buffer_size += TRACE_SEQ_BUF_SIZE;
  83. }
  84. /**
  85. * trace_seq_printf - sequence printing of trace information
  86. * @s: trace sequence descriptor
  87. * @fmt: printf format string
  88. *
  89. * It returns 0 if the trace oversizes the buffer's free
  90. * space, 1 otherwise.
  91. *
  92. * The tracer may use either sequence operations or its own
  93. * copy to user routines. To simplify formating of a trace
  94. * trace_seq_printf is used to store strings into a special
  95. * buffer (@s). Then the output may be either used by
  96. * the sequencer or pulled into another buffer.
  97. */
  98. int
  99. trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
  100. {
  101. va_list ap;
  102. int len;
  103. int ret;
  104. try_again:
  105. TRACE_SEQ_CHECK_RET0(s);
  106. len = (s->buffer_size - 1) - s->len;
  107. va_start(ap, fmt);
  108. ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
  109. va_end(ap);
  110. if (ret >= len) {
  111. expand_buffer(s);
  112. goto try_again;
  113. }
  114. s->len += ret;
  115. return 1;
  116. }
  117. /**
  118. * trace_seq_vprintf - sequence printing of trace information
  119. * @s: trace sequence descriptor
  120. * @fmt: printf format string
  121. *
  122. * The tracer may use either sequence operations or its own
  123. * copy to user routines. To simplify formating of a trace
  124. * trace_seq_printf is used to store strings into a special
  125. * buffer (@s). Then the output may be either used by
  126. * the sequencer or pulled into another buffer.
  127. */
  128. int
  129. trace_seq_vprintf(struct trace_seq *s, const char *fmt, va_list args)
  130. {
  131. int len;
  132. int ret;
  133. try_again:
  134. TRACE_SEQ_CHECK_RET0(s);
  135. len = (s->buffer_size - 1) - s->len;
  136. ret = vsnprintf(s->buffer + s->len, len, fmt, args);
  137. if (ret >= len) {
  138. expand_buffer(s);
  139. goto try_again;
  140. }
  141. s->len += ret;
  142. return len;
  143. }
  144. /**
  145. * trace_seq_puts - trace sequence printing of simple string
  146. * @s: trace sequence descriptor
  147. * @str: simple string to record
  148. *
  149. * The tracer may use either the sequence operations or its own
  150. * copy to user routines. This function records a simple string
  151. * into a special buffer (@s) for later retrieval by a sequencer
  152. * or other mechanism.
  153. */
  154. int trace_seq_puts(struct trace_seq *s, const char *str)
  155. {
  156. int len;
  157. TRACE_SEQ_CHECK_RET0(s);
  158. len = strlen(str);
  159. while (len > ((s->buffer_size - 1) - s->len))
  160. expand_buffer(s);
  161. TRACE_SEQ_CHECK_RET0(s);
  162. memcpy(s->buffer + s->len, str, len);
  163. s->len += len;
  164. return len;
  165. }
  166. int trace_seq_putc(struct trace_seq *s, unsigned char c)
  167. {
  168. TRACE_SEQ_CHECK_RET0(s);
  169. while (s->len >= (s->buffer_size - 1))
  170. expand_buffer(s);
  171. TRACE_SEQ_CHECK_RET0(s);
  172. s->buffer[s->len++] = c;
  173. return 1;
  174. }
  175. void trace_seq_terminate(struct trace_seq *s)
  176. {
  177. TRACE_SEQ_CHECK_RET(s);
  178. /* There's always one character left on the buffer */
  179. s->buffer[s->len] = 0;
  180. }
  181. int trace_seq_do_fprintf(struct trace_seq *s, FILE *fp)
  182. {
  183. TRACE_SEQ_CHECK(s);
  184. switch (s->state) {
  185. case TRACE_SEQ__GOOD:
  186. return fprintf(fp, "%.*s", s->len, s->buffer);
  187. case TRACE_SEQ__BUFFER_POISONED:
  188. fprintf(fp, "%s\n", "Usage of trace_seq after it was destroyed");
  189. break;
  190. case TRACE_SEQ__MEM_ALLOC_FAILED:
  191. fprintf(fp, "%s\n", "Can't allocate trace_seq buffer memory");
  192. break;
  193. }
  194. return -1;
  195. }
  196. int trace_seq_do_printf(struct trace_seq *s)
  197. {
  198. return trace_seq_do_fprintf(s, stdout);
  199. }