trace-seq.c 5.9 KB

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