trace-seq.c 5.4 KB

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