control.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Interface for configuring and controlling the state of tracing events.
  3. *
  4. * Copyright (C) 2011-2016 Lluís Vilanova <vilanova@ac.upc.edu>
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  7. * See the COPYING file in the top-level directory.
  8. */
  9. #include "qemu/osdep.h"
  10. #include "trace/control.h"
  11. #include "qemu/help_option.h"
  12. #include "qemu/option.h"
  13. #ifdef CONFIG_TRACE_SIMPLE
  14. #include "trace/simple.h"
  15. #endif
  16. #ifdef CONFIG_TRACE_FTRACE
  17. #include "trace/ftrace.h"
  18. #endif
  19. #ifdef CONFIG_TRACE_LOG
  20. #include "qemu/log.h"
  21. #endif
  22. #ifdef CONFIG_TRACE_SYSLOG
  23. #include <syslog.h>
  24. #endif
  25. #include "qapi/error.h"
  26. #include "qemu/error-report.h"
  27. #include "qemu/config-file.h"
  28. #include "monitor/monitor.h"
  29. #include "trace-root.h"
  30. int trace_events_enabled_count;
  31. typedef struct TraceEventGroup {
  32. TraceEvent **events;
  33. } TraceEventGroup;
  34. static TraceEventGroup *event_groups;
  35. static size_t nevent_groups;
  36. static uint32_t next_id;
  37. static uint32_t next_vcpu_id;
  38. QemuOptsList qemu_trace_opts = {
  39. .name = "trace",
  40. .implied_opt_name = "enable",
  41. .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
  42. .desc = {
  43. {
  44. .name = "enable",
  45. .type = QEMU_OPT_STRING,
  46. },
  47. {
  48. .name = "events",
  49. .type = QEMU_OPT_STRING,
  50. },{
  51. .name = "file",
  52. .type = QEMU_OPT_STRING,
  53. },
  54. { /* end of list */ }
  55. },
  56. };
  57. void trace_event_register_group(TraceEvent **events)
  58. {
  59. size_t i;
  60. for (i = 0; events[i] != NULL; i++) {
  61. events[i]->id = next_id++;
  62. if (events[i]->vcpu_id == TRACE_VCPU_EVENT_NONE) {
  63. continue;
  64. }
  65. if (likely(next_vcpu_id < CPU_TRACE_DSTATE_MAX_EVENTS)) {
  66. events[i]->vcpu_id = next_vcpu_id++;
  67. } else {
  68. warn_report("too many vcpu trace events; dropping '%s'",
  69. events[i]->name);
  70. }
  71. }
  72. event_groups = g_renew(TraceEventGroup, event_groups, nevent_groups + 1);
  73. event_groups[nevent_groups].events = events;
  74. nevent_groups++;
  75. }
  76. TraceEvent *trace_event_name(const char *name)
  77. {
  78. assert(name != NULL);
  79. TraceEventIter iter;
  80. TraceEvent *ev;
  81. trace_event_iter_init(&iter, NULL);
  82. while ((ev = trace_event_iter_next(&iter)) != NULL) {
  83. if (strcmp(trace_event_get_name(ev), name) == 0) {
  84. return ev;
  85. }
  86. }
  87. return NULL;
  88. }
  89. void trace_event_iter_init(TraceEventIter *iter, const char *pattern)
  90. {
  91. iter->event = 0;
  92. iter->group = 0;
  93. iter->pattern = pattern;
  94. }
  95. TraceEvent *trace_event_iter_next(TraceEventIter *iter)
  96. {
  97. while (iter->group < nevent_groups &&
  98. event_groups[iter->group].events[iter->event] != NULL) {
  99. TraceEvent *ev = event_groups[iter->group].events[iter->event];
  100. iter->event++;
  101. if (event_groups[iter->group].events[iter->event] == NULL) {
  102. iter->event = 0;
  103. iter->group++;
  104. }
  105. if (!iter->pattern ||
  106. g_pattern_match_simple(iter->pattern, trace_event_get_name(ev))) {
  107. return ev;
  108. }
  109. }
  110. return NULL;
  111. }
  112. void trace_list_events(void)
  113. {
  114. TraceEventIter iter;
  115. TraceEvent *ev;
  116. trace_event_iter_init(&iter, NULL);
  117. while ((ev = trace_event_iter_next(&iter)) != NULL) {
  118. fprintf(stderr, "%s\n", trace_event_get_name(ev));
  119. }
  120. #ifdef CONFIG_TRACE_DTRACE
  121. fprintf(stderr, "This list of names of trace points may be incomplete "
  122. "when using the DTrace/SystemTap backends.\n"
  123. "Run 'qemu-trace-stap list %s' to print the full list.\n",
  124. error_get_progname());
  125. #endif
  126. }
  127. static void do_trace_enable_events(const char *line_buf)
  128. {
  129. const bool enable = ('-' != line_buf[0]);
  130. const char *line_ptr = enable ? line_buf : line_buf + 1;
  131. TraceEventIter iter;
  132. TraceEvent *ev;
  133. bool is_pattern = trace_event_is_pattern(line_ptr);
  134. trace_event_iter_init(&iter, line_ptr);
  135. while ((ev = trace_event_iter_next(&iter)) != NULL) {
  136. if (!trace_event_get_state_static(ev)) {
  137. if (!is_pattern) {
  138. warn_report("trace event '%s' is not traceable",
  139. line_ptr);
  140. return;
  141. }
  142. continue;
  143. }
  144. /* start tracing */
  145. trace_event_set_state_dynamic(ev, enable);
  146. if (!is_pattern) {
  147. return;
  148. }
  149. }
  150. if (!is_pattern) {
  151. warn_report("trace event '%s' does not exist",
  152. line_ptr);
  153. }
  154. }
  155. void trace_enable_events(const char *line_buf)
  156. {
  157. if (is_help_option(line_buf)) {
  158. trace_list_events();
  159. if (cur_mon == NULL) {
  160. exit(0);
  161. }
  162. } else {
  163. do_trace_enable_events(line_buf);
  164. }
  165. }
  166. static void trace_init_events(const char *fname)
  167. {
  168. Location loc;
  169. FILE *fp;
  170. char line_buf[1024];
  171. size_t line_idx = 0;
  172. if (fname == NULL) {
  173. return;
  174. }
  175. loc_push_none(&loc);
  176. loc_set_file(fname, 0);
  177. fp = fopen(fname, "r");
  178. if (!fp) {
  179. error_report("%s", strerror(errno));
  180. exit(1);
  181. }
  182. while (fgets(line_buf, sizeof(line_buf), fp)) {
  183. loc_set_file(fname, ++line_idx);
  184. size_t len = strlen(line_buf);
  185. if (len > 1) { /* skip empty lines */
  186. line_buf[len - 1] = '\0';
  187. if ('#' == line_buf[0]) { /* skip commented lines */
  188. continue;
  189. }
  190. trace_enable_events(line_buf);
  191. }
  192. }
  193. if (fclose(fp) != 0) {
  194. loc_set_file(fname, 0);
  195. error_report("%s", strerror(errno));
  196. exit(1);
  197. }
  198. loc_pop(&loc);
  199. }
  200. void trace_init_file(const char *file)
  201. {
  202. #ifdef CONFIG_TRACE_SIMPLE
  203. st_set_trace_file(file);
  204. st_set_trace_file_enabled(true);
  205. #elif defined CONFIG_TRACE_LOG
  206. /*
  207. * If both the simple and the log backends are enabled, "--trace file"
  208. * only applies to the simple backend; use "-D" for the log
  209. * backend. However we should only override -D if we actually have
  210. * something to override it with.
  211. */
  212. if (file) {
  213. qemu_set_log_filename(file, &error_fatal);
  214. }
  215. #else
  216. if (file) {
  217. fprintf(stderr, "error: --trace file=...: "
  218. "option not supported by the selected tracing backends\n");
  219. exit(1);
  220. }
  221. #endif
  222. }
  223. void trace_fini_vcpu(CPUState *vcpu)
  224. {
  225. TraceEventIter iter;
  226. TraceEvent *ev;
  227. trace_guest_cpu_exit(vcpu);
  228. trace_event_iter_init(&iter, NULL);
  229. while ((ev = trace_event_iter_next(&iter)) != NULL) {
  230. if (trace_event_is_vcpu(ev) &&
  231. trace_event_get_state_static(ev) &&
  232. trace_event_get_vcpu_state_dynamic(vcpu, ev)) {
  233. /* must disable to affect the global counter */
  234. trace_event_set_vcpu_state_dynamic(vcpu, ev, false);
  235. }
  236. }
  237. }
  238. bool trace_init_backends(void)
  239. {
  240. #ifdef CONFIG_TRACE_SIMPLE
  241. if (!st_init()) {
  242. fprintf(stderr, "failed to initialize simple tracing backend.\n");
  243. return false;
  244. }
  245. #endif
  246. #ifdef CONFIG_TRACE_FTRACE
  247. if (!ftrace_init()) {
  248. fprintf(stderr, "failed to initialize ftrace backend.\n");
  249. return false;
  250. }
  251. #endif
  252. #ifdef CONFIG_TRACE_SYSLOG
  253. openlog(NULL, LOG_PID, LOG_DAEMON);
  254. #endif
  255. return true;
  256. }
  257. char *trace_opt_parse(const char *optarg)
  258. {
  259. char *trace_file;
  260. QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("trace"),
  261. optarg, true);
  262. if (!opts) {
  263. exit(1);
  264. }
  265. if (qemu_opt_get(opts, "enable")) {
  266. trace_enable_events(qemu_opt_get(opts, "enable"));
  267. }
  268. trace_init_events(qemu_opt_get(opts, "events"));
  269. trace_file = g_strdup(qemu_opt_get(opts, "file"));
  270. qemu_opts_del(opts);
  271. return trace_file;
  272. }
  273. uint32_t trace_get_vcpu_event_count(void)
  274. {
  275. return next_vcpu_id;
  276. }