trace-seq.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <stdarg.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 ((s)->buffer == TRACE_SEQ_POISON) \
  35. die("Usage of trace_seq after it was destroyed"); \
  36. } while (0)
  37. /**
  38. * trace_seq_init - initialize the trace_seq structure
  39. * @s: a pointer to the trace_seq structure to initialize
  40. */
  41. void trace_seq_init(struct trace_seq *s)
  42. {
  43. s->len = 0;
  44. s->readpos = 0;
  45. s->buffer_size = TRACE_SEQ_BUF_SIZE;
  46. s->buffer = malloc_or_die(s->buffer_size);
  47. }
  48. /**
  49. * trace_seq_destroy - free up memory of a trace_seq
  50. * @s: a pointer to the trace_seq to free the buffer
  51. *
  52. * Only frees the buffer, not the trace_seq struct itself.
  53. */
  54. void trace_seq_destroy(struct trace_seq *s)
  55. {
  56. if (!s)
  57. return;
  58. TRACE_SEQ_CHECK(s);
  59. free(s->buffer);
  60. s->buffer = TRACE_SEQ_POISON;
  61. }
  62. static void expand_buffer(struct trace_seq *s)
  63. {
  64. s->buffer_size += TRACE_SEQ_BUF_SIZE;
  65. s->buffer = realloc(s->buffer, s->buffer_size);
  66. if (!s->buffer)
  67. die("Can't allocate trace_seq buffer memory");
  68. }
  69. /**
  70. * trace_seq_printf - sequence printing of trace information
  71. * @s: trace sequence descriptor
  72. * @fmt: printf format string
  73. *
  74. * It returns 0 if the trace oversizes the buffer's free
  75. * space, 1 otherwise.
  76. *
  77. * The tracer may use either sequence operations or its own
  78. * copy to user routines. To simplify formating of a trace
  79. * trace_seq_printf is used to store strings into a special
  80. * buffer (@s). Then the output may be either used by
  81. * the sequencer or pulled into another buffer.
  82. */
  83. int
  84. trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
  85. {
  86. va_list ap;
  87. int len;
  88. int ret;
  89. TRACE_SEQ_CHECK(s);
  90. try_again:
  91. len = (s->buffer_size - 1) - s->len;
  92. va_start(ap, fmt);
  93. ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
  94. va_end(ap);
  95. if (ret >= len) {
  96. expand_buffer(s);
  97. goto try_again;
  98. }
  99. s->len += ret;
  100. return 1;
  101. }
  102. /**
  103. * trace_seq_vprintf - sequence printing of trace information
  104. * @s: trace sequence descriptor
  105. * @fmt: printf format string
  106. *
  107. * The tracer may use either sequence operations or its own
  108. * copy to user routines. To simplify formating of a trace
  109. * trace_seq_printf is used to store strings into a special
  110. * buffer (@s). Then the output may be either used by
  111. * the sequencer or pulled into another buffer.
  112. */
  113. int
  114. trace_seq_vprintf(struct trace_seq *s, const char *fmt, va_list args)
  115. {
  116. int len;
  117. int ret;
  118. TRACE_SEQ_CHECK(s);
  119. try_again:
  120. len = (s->buffer_size - 1) - s->len;
  121. ret = vsnprintf(s->buffer + s->len, len, fmt, args);
  122. if (ret >= len) {
  123. expand_buffer(s);
  124. goto try_again;
  125. }
  126. s->len += ret;
  127. return len;
  128. }
  129. /**
  130. * trace_seq_puts - trace sequence printing of simple string
  131. * @s: trace sequence descriptor
  132. * @str: simple string to record
  133. *
  134. * The tracer may use either the sequence operations or its own
  135. * copy to user routines. This function records a simple string
  136. * into a special buffer (@s) for later retrieval by a sequencer
  137. * or other mechanism.
  138. */
  139. int trace_seq_puts(struct trace_seq *s, const char *str)
  140. {
  141. int len;
  142. TRACE_SEQ_CHECK(s);
  143. len = strlen(str);
  144. while (len > ((s->buffer_size - 1) - s->len))
  145. expand_buffer(s);
  146. memcpy(s->buffer + s->len, str, len);
  147. s->len += len;
  148. return len;
  149. }
  150. int trace_seq_putc(struct trace_seq *s, unsigned char c)
  151. {
  152. TRACE_SEQ_CHECK(s);
  153. while (s->len >= (s->buffer_size - 1))
  154. expand_buffer(s);
  155. s->buffer[s->len++] = c;
  156. return 1;
  157. }
  158. void trace_seq_terminate(struct trace_seq *s)
  159. {
  160. TRACE_SEQ_CHECK(s);
  161. /* There's always one character left on the buffer */
  162. s->buffer[s->len] = 0;
  163. }
  164. int trace_seq_do_printf(struct trace_seq *s)
  165. {
  166. TRACE_SEQ_CHECK(s);
  167. return printf("%.*s", s->len, s->buffer);
  168. }