trace-seq.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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_destroy - free up memory of a trace_seq
  49. * @s: a pointer to the trace_seq to free the buffer
  50. *
  51. * Only frees the buffer, not the trace_seq struct itself.
  52. */
  53. void trace_seq_destroy(struct trace_seq *s)
  54. {
  55. if (!s)
  56. return;
  57. TRACE_SEQ_CHECK(s);
  58. free(s->buffer);
  59. s->buffer = TRACE_SEQ_POISON;
  60. }
  61. static void expand_buffer(struct trace_seq *s)
  62. {
  63. s->buffer_size += TRACE_SEQ_BUF_SIZE;
  64. s->buffer = realloc(s->buffer, s->buffer_size);
  65. if (!s->buffer)
  66. die("Can't allocate trace_seq buffer memory");
  67. }
  68. /**
  69. * trace_seq_printf - sequence printing of trace information
  70. * @s: trace sequence descriptor
  71. * @fmt: printf format string
  72. *
  73. * It returns 0 if the trace oversizes the buffer's free
  74. * space, 1 otherwise.
  75. *
  76. * The tracer may use either sequence operations or its own
  77. * copy to user routines. To simplify formating of a trace
  78. * trace_seq_printf is used to store strings into a special
  79. * buffer (@s). Then the output may be either used by
  80. * the sequencer or pulled into another buffer.
  81. */
  82. int
  83. trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
  84. {
  85. va_list ap;
  86. int len;
  87. int ret;
  88. TRACE_SEQ_CHECK(s);
  89. try_again:
  90. len = (s->buffer_size - 1) - s->len;
  91. va_start(ap, fmt);
  92. ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
  93. va_end(ap);
  94. if (ret >= len) {
  95. expand_buffer(s);
  96. goto try_again;
  97. }
  98. s->len += ret;
  99. return 1;
  100. }
  101. /**
  102. * trace_seq_vprintf - sequence printing of trace information
  103. * @s: trace sequence descriptor
  104. * @fmt: printf format string
  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_vprintf(struct trace_seq *s, const char *fmt, va_list args)
  114. {
  115. int len;
  116. int ret;
  117. TRACE_SEQ_CHECK(s);
  118. try_again:
  119. len = (s->buffer_size - 1) - s->len;
  120. ret = vsnprintf(s->buffer + s->len, len, fmt, args);
  121. if (ret >= len) {
  122. expand_buffer(s);
  123. goto try_again;
  124. }
  125. s->len += ret;
  126. return len;
  127. }
  128. /**
  129. * trace_seq_puts - trace sequence printing of simple string
  130. * @s: trace sequence descriptor
  131. * @str: simple string to record
  132. *
  133. * The tracer may use either the sequence operations or its own
  134. * copy to user routines. This function records a simple string
  135. * into a special buffer (@s) for later retrieval by a sequencer
  136. * or other mechanism.
  137. */
  138. int trace_seq_puts(struct trace_seq *s, const char *str)
  139. {
  140. int len;
  141. TRACE_SEQ_CHECK(s);
  142. len = strlen(str);
  143. while (len > ((s->buffer_size - 1) - s->len))
  144. expand_buffer(s);
  145. memcpy(s->buffer + s->len, str, len);
  146. s->len += len;
  147. return len;
  148. }
  149. int trace_seq_putc(struct trace_seq *s, unsigned char c)
  150. {
  151. TRACE_SEQ_CHECK(s);
  152. while (s->len >= (s->buffer_size - 1))
  153. expand_buffer(s);
  154. s->buffer[s->len++] = c;
  155. return 1;
  156. }
  157. void trace_seq_terminate(struct trace_seq *s)
  158. {
  159. TRACE_SEQ_CHECK(s);
  160. /* There's always one character left on the buffer */
  161. s->buffer[s->len] = 0;
  162. }
  163. int trace_seq_do_printf(struct trace_seq *s)
  164. {
  165. TRACE_SEQ_CHECK(s);
  166. return printf("%.*s", s->len, s->buffer);
  167. }