control.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. static bool pattern_glob(const char *pat, const char *ev)
  90. {
  91. while (*pat != '\0' && *ev != '\0') {
  92. if (*pat == *ev) {
  93. pat++;
  94. ev++;
  95. }
  96. else if (*pat == '*') {
  97. if (pattern_glob(pat, ev+1)) {
  98. return true;
  99. } else if (pattern_glob(pat+1, ev)) {
  100. return true;
  101. } else {
  102. return false;
  103. }
  104. } else {
  105. return false;
  106. }
  107. }
  108. while (*pat == '*') {
  109. pat++;
  110. }
  111. if (*pat == '\0' && *ev == '\0') {
  112. return true;
  113. } else {
  114. return false;
  115. }
  116. }
  117. void trace_event_iter_init(TraceEventIter *iter, const char *pattern)
  118. {
  119. iter->event = 0;
  120. iter->group = 0;
  121. iter->pattern = pattern;
  122. }
  123. TraceEvent *trace_event_iter_next(TraceEventIter *iter)
  124. {
  125. while (iter->group < nevent_groups &&
  126. event_groups[iter->group].events[iter->event] != NULL) {
  127. TraceEvent *ev = event_groups[iter->group].events[iter->event];
  128. iter->event++;
  129. if (event_groups[iter->group].events[iter->event] == NULL) {
  130. iter->event = 0;
  131. iter->group++;
  132. }
  133. if (!iter->pattern ||
  134. pattern_glob(iter->pattern,
  135. trace_event_get_name(ev))) {
  136. return ev;
  137. }
  138. }
  139. return NULL;
  140. }
  141. void trace_list_events(void)
  142. {
  143. TraceEventIter iter;
  144. TraceEvent *ev;
  145. trace_event_iter_init(&iter, NULL);
  146. while ((ev = trace_event_iter_next(&iter)) != NULL) {
  147. fprintf(stderr, "%s\n", trace_event_get_name(ev));
  148. }
  149. #ifdef CONFIG_TRACE_DTRACE
  150. fprintf(stderr, "This list of names of trace points may be incomplete "
  151. "when using the DTrace/SystemTap backends.\n"
  152. "Run 'qemu-trace-stap list %s' to print the full list.\n",
  153. error_get_progname());
  154. #endif
  155. }
  156. static void do_trace_enable_events(const char *line_buf)
  157. {
  158. const bool enable = ('-' != line_buf[0]);
  159. const char *line_ptr = enable ? line_buf : line_buf + 1;
  160. TraceEventIter iter;
  161. TraceEvent *ev;
  162. bool is_pattern = trace_event_is_pattern(line_ptr);
  163. trace_event_iter_init(&iter, line_ptr);
  164. while ((ev = trace_event_iter_next(&iter)) != NULL) {
  165. if (!trace_event_get_state_static(ev)) {
  166. if (!is_pattern) {
  167. warn_report("trace event '%s' is not traceable",
  168. line_ptr);
  169. return;
  170. }
  171. continue;
  172. }
  173. /* start tracing */
  174. trace_event_set_state_dynamic(ev, enable);
  175. if (!is_pattern) {
  176. return;
  177. }
  178. }
  179. if (!is_pattern) {
  180. warn_report("trace event '%s' does not exist",
  181. line_ptr);
  182. }
  183. }
  184. void trace_enable_events(const char *line_buf)
  185. {
  186. if (is_help_option(line_buf)) {
  187. trace_list_events();
  188. if (cur_mon == NULL) {
  189. exit(0);
  190. }
  191. } else {
  192. do_trace_enable_events(line_buf);
  193. }
  194. }
  195. static void trace_init_events(const char *fname)
  196. {
  197. Location loc;
  198. FILE *fp;
  199. char line_buf[1024];
  200. size_t line_idx = 0;
  201. if (fname == NULL) {
  202. return;
  203. }
  204. loc_push_none(&loc);
  205. loc_set_file(fname, 0);
  206. fp = fopen(fname, "r");
  207. if (!fp) {
  208. error_report("%s", strerror(errno));
  209. exit(1);
  210. }
  211. while (fgets(line_buf, sizeof(line_buf), fp)) {
  212. loc_set_file(fname, ++line_idx);
  213. size_t len = strlen(line_buf);
  214. if (len > 1) { /* skip empty lines */
  215. line_buf[len - 1] = '\0';
  216. if ('#' == line_buf[0]) { /* skip commented lines */
  217. continue;
  218. }
  219. trace_enable_events(line_buf);
  220. }
  221. }
  222. if (fclose(fp) != 0) {
  223. loc_set_file(fname, 0);
  224. error_report("%s", strerror(errno));
  225. exit(1);
  226. }
  227. loc_pop(&loc);
  228. }
  229. void trace_init_file(const char *file)
  230. {
  231. #ifdef CONFIG_TRACE_SIMPLE
  232. st_set_trace_file(file);
  233. #elif defined CONFIG_TRACE_LOG
  234. /* If both the simple and the log backends are enabled, "--trace file"
  235. * only applies to the simple backend; use "-D" for the log backend.
  236. */
  237. if (file) {
  238. qemu_set_log_filename(file, &error_fatal);
  239. }
  240. #else
  241. if (file) {
  242. fprintf(stderr, "error: --trace file=...: "
  243. "option not supported by the selected tracing backends\n");
  244. exit(1);
  245. }
  246. #endif
  247. }
  248. void trace_fini_vcpu(CPUState *vcpu)
  249. {
  250. TraceEventIter iter;
  251. TraceEvent *ev;
  252. trace_guest_cpu_exit(vcpu);
  253. trace_event_iter_init(&iter, NULL);
  254. while ((ev = trace_event_iter_next(&iter)) != NULL) {
  255. if (trace_event_is_vcpu(ev) &&
  256. trace_event_get_state_static(ev) &&
  257. trace_event_get_vcpu_state_dynamic(vcpu, ev)) {
  258. /* must disable to affect the global counter */
  259. trace_event_set_vcpu_state_dynamic(vcpu, ev, false);
  260. }
  261. }
  262. }
  263. bool trace_init_backends(void)
  264. {
  265. #ifdef CONFIG_TRACE_SIMPLE
  266. if (!st_init()) {
  267. fprintf(stderr, "failed to initialize simple tracing backend.\n");
  268. return false;
  269. }
  270. #endif
  271. #ifdef CONFIG_TRACE_FTRACE
  272. if (!ftrace_init()) {
  273. fprintf(stderr, "failed to initialize ftrace backend.\n");
  274. return false;
  275. }
  276. #endif
  277. #ifdef CONFIG_TRACE_SYSLOG
  278. openlog(NULL, LOG_PID, LOG_DAEMON);
  279. #endif
  280. return true;
  281. }
  282. char *trace_opt_parse(const char *optarg)
  283. {
  284. char *trace_file;
  285. QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("trace"),
  286. optarg, true);
  287. if (!opts) {
  288. exit(1);
  289. }
  290. if (qemu_opt_get(opts, "enable")) {
  291. trace_enable_events(qemu_opt_get(opts, "enable"));
  292. }
  293. trace_init_events(qemu_opt_get(opts, "events"));
  294. trace_file = g_strdup(qemu_opt_get(opts, "file"));
  295. qemu_opts_del(opts);
  296. return trace_file;
  297. }
  298. uint32_t trace_get_vcpu_event_count(void)
  299. {
  300. return next_vcpu_id;
  301. }