trace-seq.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 "event-parse.h"
  25. #include "event-utils.h"
  26. /*
  27. * The TRACE_SEQ_POISON is to catch the use of using
  28. * a trace_seq structure after it was destroyed.
  29. */
  30. #define TRACE_SEQ_POISON ((void *)0xdeadbeef)
  31. #define TRACE_SEQ_CHECK(s) \
  32. do { \
  33. if ((s)->buffer == TRACE_SEQ_POISON) \
  34. die("Usage of trace_seq after it was destroyed"); \
  35. } while (0)
  36. /**
  37. * trace_seq_init - initialize the trace_seq structure
  38. * @s: a pointer to the trace_seq structure to initialize
  39. */
  40. void trace_seq_init(struct trace_seq *s)
  41. {
  42. s->len = 0;
  43. s->readpos = 0;
  44. s->buffer_size = TRACE_SEQ_BUF_SIZE;
  45. s->buffer = malloc_or_die(s->buffer_size);
  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(s);
  70. free(s->buffer);
  71. s->buffer = TRACE_SEQ_POISON;
  72. }
  73. static void expand_buffer(struct trace_seq *s)
  74. {
  75. s->buffer_size += TRACE_SEQ_BUF_SIZE;
  76. s->buffer = realloc(s->buffer, s->buffer_size);
  77. if (!s->buffer)
  78. die("Can't allocate trace_seq buffer memory");
  79. }
  80. /**
  81. * trace_seq_printf - sequence printing of trace information
  82. * @s: trace sequence descriptor
  83. * @fmt: printf format string
  84. *
  85. * It returns 0 if the trace oversizes the buffer's free
  86. * space, 1 otherwise.
  87. *
  88. * The tracer may use either sequence operations or its own
  89. * copy to user routines. To simplify formating of a trace
  90. * trace_seq_printf is used to store strings into a special
  91. * buffer (@s). Then the output may be either used by
  92. * the sequencer or pulled into another buffer.
  93. */
  94. int
  95. trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
  96. {
  97. va_list ap;
  98. int len;
  99. int ret;
  100. TRACE_SEQ_CHECK(s);
  101. try_again:
  102. len = (s->buffer_size - 1) - s->len;
  103. va_start(ap, fmt);
  104. ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
  105. va_end(ap);
  106. if (ret >= len) {
  107. expand_buffer(s);
  108. goto try_again;
  109. }
  110. s->len += ret;
  111. return 1;
  112. }
  113. /**
  114. * trace_seq_vprintf - sequence printing of trace information
  115. * @s: trace sequence descriptor
  116. * @fmt: printf format string
  117. *
  118. * The tracer may use either sequence operations or its own
  119. * copy to user routines. To simplify formating of a trace
  120. * trace_seq_printf is used to store strings into a special
  121. * buffer (@s). Then the output may be either used by
  122. * the sequencer or pulled into another buffer.
  123. */
  124. int
  125. trace_seq_vprintf(struct trace_seq *s, const char *fmt, va_list args)
  126. {
  127. int len;
  128. int ret;
  129. TRACE_SEQ_CHECK(s);
  130. try_again:
  131. len = (s->buffer_size - 1) - s->len;
  132. ret = vsnprintf(s->buffer + s->len, len, fmt, args);
  133. if (ret >= len) {
  134. expand_buffer(s);
  135. goto try_again;
  136. }
  137. s->len += ret;
  138. return len;
  139. }
  140. /**
  141. * trace_seq_puts - trace sequence printing of simple string
  142. * @s: trace sequence descriptor
  143. * @str: simple string to record
  144. *
  145. * The tracer may use either the sequence operations or its own
  146. * copy to user routines. This function records a simple string
  147. * into a special buffer (@s) for later retrieval by a sequencer
  148. * or other mechanism.
  149. */
  150. int trace_seq_puts(struct trace_seq *s, const char *str)
  151. {
  152. int len;
  153. TRACE_SEQ_CHECK(s);
  154. len = strlen(str);
  155. while (len > ((s->buffer_size - 1) - s->len))
  156. expand_buffer(s);
  157. memcpy(s->buffer + s->len, str, len);
  158. s->len += len;
  159. return len;
  160. }
  161. int trace_seq_putc(struct trace_seq *s, unsigned char c)
  162. {
  163. TRACE_SEQ_CHECK(s);
  164. while (s->len >= (s->buffer_size - 1))
  165. expand_buffer(s);
  166. s->buffer[s->len++] = c;
  167. return 1;
  168. }
  169. void trace_seq_terminate(struct trace_seq *s)
  170. {
  171. TRACE_SEQ_CHECK(s);
  172. /* There's always one character left on the buffer */
  173. s->buffer[s->len] = 0;
  174. }
  175. int trace_seq_do_printf(struct trace_seq *s)
  176. {
  177. TRACE_SEQ_CHECK(s);
  178. return printf("%.*s", s->len, s->buffer);
  179. }