trace-seq.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // SPDX-License-Identifier: LGPL-2.1
  2. /*
  3. * Copyright (C) 2009 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
  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, 1 otherwise.
  92. *
  93. * The tracer may use either sequence operations or its own
  94. * copy to user routines. To simplify formating of a trace
  95. * trace_seq_printf is used to store strings into a special
  96. * buffer (@s). Then the output may be either used by
  97. * the sequencer or pulled into another buffer.
  98. */
  99. int
  100. trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
  101. {
  102. va_list ap;
  103. int len;
  104. int ret;
  105. try_again:
  106. TRACE_SEQ_CHECK_RET0(s);
  107. len = (s->buffer_size - 1) - s->len;
  108. va_start(ap, fmt);
  109. ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
  110. va_end(ap);
  111. if (ret >= len) {
  112. expand_buffer(s);
  113. goto try_again;
  114. }
  115. s->len += ret;
  116. return 1;
  117. }
  118. /**
  119. * trace_seq_vprintf - sequence printing of trace information
  120. * @s: trace sequence descriptor
  121. * @fmt: printf format string
  122. *
  123. * The tracer may use either sequence operations or its own
  124. * copy to user routines. To simplify formating of a trace
  125. * trace_seq_printf is used to store strings into a special
  126. * buffer (@s). Then the output may be either used by
  127. * the sequencer or pulled into another buffer.
  128. */
  129. int
  130. trace_seq_vprintf(struct trace_seq *s, const char *fmt, va_list args)
  131. {
  132. int len;
  133. int ret;
  134. try_again:
  135. TRACE_SEQ_CHECK_RET0(s);
  136. len = (s->buffer_size - 1) - s->len;
  137. ret = vsnprintf(s->buffer + s->len, len, fmt, args);
  138. if (ret >= len) {
  139. expand_buffer(s);
  140. goto try_again;
  141. }
  142. s->len += ret;
  143. return len;
  144. }
  145. /**
  146. * trace_seq_puts - trace sequence printing of simple string
  147. * @s: trace sequence descriptor
  148. * @str: simple string to record
  149. *
  150. * The tracer may use either the sequence operations or its own
  151. * copy to user routines. This function records a simple string
  152. * into a special buffer (@s) for later retrieval by a sequencer
  153. * or other mechanism.
  154. */
  155. int trace_seq_puts(struct trace_seq *s, const char *str)
  156. {
  157. int len;
  158. TRACE_SEQ_CHECK_RET0(s);
  159. len = strlen(str);
  160. while (len > ((s->buffer_size - 1) - s->len))
  161. expand_buffer(s);
  162. TRACE_SEQ_CHECK_RET0(s);
  163. memcpy(s->buffer + s->len, str, len);
  164. s->len += len;
  165. return len;
  166. }
  167. int trace_seq_putc(struct trace_seq *s, unsigned char c)
  168. {
  169. TRACE_SEQ_CHECK_RET0(s);
  170. while (s->len >= (s->buffer_size - 1))
  171. expand_buffer(s);
  172. TRACE_SEQ_CHECK_RET0(s);
  173. s->buffer[s->len++] = c;
  174. return 1;
  175. }
  176. void trace_seq_terminate(struct trace_seq *s)
  177. {
  178. TRACE_SEQ_CHECK_RET(s);
  179. /* There's always one character left on the buffer */
  180. s->buffer[s->len] = 0;
  181. }
  182. int trace_seq_do_fprintf(struct trace_seq *s, FILE *fp)
  183. {
  184. TRACE_SEQ_CHECK(s);
  185. switch (s->state) {
  186. case TRACE_SEQ__GOOD:
  187. return fprintf(fp, "%.*s", s->len, s->buffer);
  188. case TRACE_SEQ__BUFFER_POISONED:
  189. fprintf(fp, "%s\n", "Usage of trace_seq after it was destroyed");
  190. break;
  191. case TRACE_SEQ__MEM_ALLOC_FAILED:
  192. fprintf(fp, "%s\n", "Can't allocate trace_seq buffer memory");
  193. break;
  194. }
  195. return -1;
  196. }
  197. int trace_seq_do_printf(struct trace_seq *s)
  198. {
  199. return trace_seq_do_fprintf(s, stdout);
  200. }