123d15e86SLluís /* 223d15e86SLluís * Interface for configuring and controlling the state of tracing events. 323d15e86SLluís * 448151859SLluís Vilanova * Copyright (C) 2011-2016 Lluís Vilanova <vilanova@ac.upc.edu> 523d15e86SLluís * 6b1bae816SLluís Vilanova * This work is licensed under the terms of the GNU GPL, version 2 or later. 7b1bae816SLluís Vilanova * See the COPYING file in the top-level directory. 823d15e86SLluís */ 923d15e86SLluís 10d38ea87aSPeter Maydell #include "qemu/osdep.h" 1123d15e86SLluís #include "trace/control.h" 12f348b6d1SVeronia Bahaa #include "qemu/help_option.h" 13922a01a0SMarkus Armbruster #include "qemu/option.h" 145b808275SLluís Vilanova #ifdef CONFIG_TRACE_SIMPLE 155b808275SLluís Vilanova #include "trace/simple.h" 165b808275SLluís Vilanova #endif 175b808275SLluís Vilanova #ifdef CONFIG_TRACE_FTRACE 185b808275SLluís Vilanova #include "trace/ftrace.h" 195b808275SLluís Vilanova #endif 20ed7f5f1dSPaolo Bonzini #ifdef CONFIG_TRACE_LOG 21ed7f5f1dSPaolo Bonzini #include "qemu/log.h" 22ed7f5f1dSPaolo Bonzini #endif 230a852417SPaul Durrant #ifdef CONFIG_TRACE_SYSLOG 240a852417SPaul Durrant #include <syslog.h> 250a852417SPaul Durrant #endif 26daa76aa4SMarkus Armbruster #include "qapi/error.h" 27a35d9be6SAlexey Kardashevskiy #include "qemu/error-report.h" 28e9e0bb2aSDenis V. Lunev #include "qemu/config-file.h" 29acc6809dSDenis V. Lunev #include "monitor/monitor.h" 30243af022SPaolo Bonzini #include "trace/trace-root.h" 3123d15e86SLluís 3243b48cfcSPaolo Bonzini int trace_events_enabled_count; 3343b48cfcSPaolo Bonzini 34fe4db84dSDaniel P. Berrange typedef struct TraceEventGroup { 35fe4db84dSDaniel P. Berrange TraceEvent **events; 36fe4db84dSDaniel P. Berrange } TraceEventGroup; 37fe4db84dSDaniel P. Berrange 38fe4db84dSDaniel P. Berrange static TraceEventGroup *event_groups; 39fe4db84dSDaniel P. Berrange static size_t nevent_groups; 40ca3fa0e8SDaniel P. Berrange static uint32_t next_id; 41ca3fa0e8SDaniel P. Berrange static uint32_t next_vcpu_id; 42648b4823SJosh DuBois static bool init_trace_on_startup; 43fe4db84dSDaniel P. Berrange 44e9e0bb2aSDenis V. Lunev QemuOptsList qemu_trace_opts = { 45e9e0bb2aSDenis V. Lunev .name = "trace", 46e9e0bb2aSDenis V. Lunev .implied_opt_name = "enable", 47e9e0bb2aSDenis V. Lunev .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head), 48e9e0bb2aSDenis V. Lunev .desc = { 49e9e0bb2aSDenis V. Lunev { 50e9e0bb2aSDenis V. Lunev .name = "enable", 51e9e0bb2aSDenis V. Lunev .type = QEMU_OPT_STRING, 52e9e0bb2aSDenis V. Lunev }, 53e9e0bb2aSDenis V. Lunev { 54e9e0bb2aSDenis V. Lunev .name = "events", 55e9e0bb2aSDenis V. Lunev .type = QEMU_OPT_STRING, 56e9e0bb2aSDenis V. Lunev },{ 57e9e0bb2aSDenis V. Lunev .name = "file", 58e9e0bb2aSDenis V. Lunev .type = QEMU_OPT_STRING, 59e9e0bb2aSDenis V. Lunev }, 60e9e0bb2aSDenis V. Lunev { /* end of list */ } 61e9e0bb2aSDenis V. Lunev }, 62e9e0bb2aSDenis V. Lunev }; 63e9e0bb2aSDenis V. Lunev 64e9e0bb2aSDenis V. Lunev 65fe4db84dSDaniel P. Berrange void trace_event_register_group(TraceEvent **events) 66fe4db84dSDaniel P. Berrange { 67ca3fa0e8SDaniel P. Berrange size_t i; 68ca3fa0e8SDaniel P. Berrange for (i = 0; events[i] != NULL; i++) { 69ca3fa0e8SDaniel P. Berrange events[i]->id = next_id++; 70d01c05c9SLluís Vilanova if (events[i]->vcpu_id == TRACE_VCPU_EVENT_NONE) { 71d01c05c9SLluís Vilanova continue; 72d01c05c9SLluís Vilanova } 73d01c05c9SLluís Vilanova 74d01c05c9SLluís Vilanova if (likely(next_vcpu_id < CPU_TRACE_DSTATE_MAX_EVENTS)) { 75ca3fa0e8SDaniel P. Berrange events[i]->vcpu_id = next_vcpu_id++; 76d01c05c9SLluís Vilanova } else { 7755d527a9SAlistair Francis warn_report("too many vcpu trace events; dropping '%s'", 78d01c05c9SLluís Vilanova events[i]->name); 79ca3fa0e8SDaniel P. Berrange } 80ca3fa0e8SDaniel P. Berrange } 81fe4db84dSDaniel P. Berrange event_groups = g_renew(TraceEventGroup, event_groups, nevent_groups + 1); 82fe4db84dSDaniel P. Berrange event_groups[nevent_groups].events = events; 83fe4db84dSDaniel P. Berrange nevent_groups++; 84fe4db84dSDaniel P. Berrange } 85fe4db84dSDaniel P. Berrange 86fe4db84dSDaniel P. Berrange 87b1bae816SLluís Vilanova TraceEvent *trace_event_name(const char *name) 88b1bae816SLluís Vilanova { 89b1bae816SLluís Vilanova assert(name != NULL); 90b1bae816SLluís Vilanova 910d4e995cSDaniel P. Berrange TraceEventIter iter; 920d4e995cSDaniel P. Berrange TraceEvent *ev; 930d4e995cSDaniel P. Berrange trace_event_iter_init(&iter, NULL); 940d4e995cSDaniel P. Berrange while ((ev = trace_event_iter_next(&iter)) != NULL) { 95b1bae816SLluís Vilanova if (strcmp(trace_event_get_name(ev), name) == 0) { 96b1bae816SLluís Vilanova return ev; 97b1bae816SLluís Vilanova } 98b1bae816SLluís Vilanova } 99b1bae816SLluís Vilanova return NULL; 100b1bae816SLluís Vilanova } 101b1bae816SLluís Vilanova 1026a1b0f3aSDaniel P. Berrange void trace_event_iter_init(TraceEventIter *iter, const char *pattern) 1036a1b0f3aSDaniel P. Berrange { 1046a1b0f3aSDaniel P. Berrange iter->event = 0; 105fe4db84dSDaniel P. Berrange iter->group = 0; 1066a1b0f3aSDaniel P. Berrange iter->pattern = pattern; 1076a1b0f3aSDaniel P. Berrange } 1086a1b0f3aSDaniel P. Berrange 1096a1b0f3aSDaniel P. Berrange TraceEvent *trace_event_iter_next(TraceEventIter *iter) 1106a1b0f3aSDaniel P. Berrange { 111fe4db84dSDaniel P. Berrange while (iter->group < nevent_groups && 112fe4db84dSDaniel P. Berrange event_groups[iter->group].events[iter->event] != NULL) { 113fe4db84dSDaniel P. Berrange TraceEvent *ev = event_groups[iter->group].events[iter->event]; 1146a1b0f3aSDaniel P. Berrange iter->event++; 115fe4db84dSDaniel P. Berrange if (event_groups[iter->group].events[iter->event] == NULL) { 116fe4db84dSDaniel P. Berrange iter->event = 0; 117fe4db84dSDaniel P. Berrange iter->group++; 118fe4db84dSDaniel P. Berrange } 1196a1b0f3aSDaniel P. Berrange if (!iter->pattern || 120e66eae7aSAlex Bennée g_pattern_match_simple(iter->pattern, trace_event_get_name(ev))) { 1216a1b0f3aSDaniel P. Berrange return ev; 1226a1b0f3aSDaniel P. Berrange } 1236a1b0f3aSDaniel P. Berrange } 1246a1b0f3aSDaniel P. Berrange 1256a1b0f3aSDaniel P. Berrange return NULL; 1266a1b0f3aSDaniel P. Berrange } 1276a1b0f3aSDaniel P. Berrange 128e9527dd3SPaolo Bonzini void trace_list_events(void) 129e9527dd3SPaolo Bonzini { 1300d4e995cSDaniel P. Berrange TraceEventIter iter; 1310d4e995cSDaniel P. Berrange TraceEvent *ev; 1320d4e995cSDaniel P. Berrange trace_event_iter_init(&iter, NULL); 1330d4e995cSDaniel P. Berrange while ((ev = trace_event_iter_next(&iter)) != NULL) { 1340d4e995cSDaniel P. Berrange fprintf(stderr, "%s\n", trace_event_get_name(ev)); 135e9527dd3SPaolo Bonzini } 1369f591a5dSPhilippe Mathieu-Daudé #ifdef CONFIG_TRACE_DTRACE 1379f591a5dSPhilippe Mathieu-Daudé fprintf(stderr, "This list of names of trace points may be incomplete " 1389f591a5dSPhilippe Mathieu-Daudé "when using the DTrace/SystemTap backends.\n" 1399f591a5dSPhilippe Mathieu-Daudé "Run 'qemu-trace-stap list %s' to print the full list.\n", 1409f591a5dSPhilippe Mathieu-Daudé error_get_progname()); 1419f591a5dSPhilippe Mathieu-Daudé #endif 142e9527dd3SPaolo Bonzini } 143e9527dd3SPaolo Bonzini 144e9527dd3SPaolo Bonzini static void do_trace_enable_events(const char *line_buf) 14510578a25SPaolo Bonzini { 14610578a25SPaolo Bonzini const bool enable = ('-' != line_buf[0]); 14710578a25SPaolo Bonzini const char *line_ptr = enable ? line_buf : line_buf + 1; 1480d4e995cSDaniel P. Berrange TraceEventIter iter; 1490d4e995cSDaniel P. Berrange TraceEvent *ev; 1500d4e995cSDaniel P. Berrange bool is_pattern = trace_event_is_pattern(line_ptr); 15110578a25SPaolo Bonzini 1520d4e995cSDaniel P. Berrange trace_event_iter_init(&iter, line_ptr); 1530d4e995cSDaniel P. Berrange while ((ev = trace_event_iter_next(&iter)) != NULL) { 1540d4e995cSDaniel P. Berrange if (!trace_event_get_state_static(ev)) { 1550d4e995cSDaniel P. Berrange if (!is_pattern) { 1563dc6f869SAlistair Francis warn_report("trace event '%s' is not traceable", 15710578a25SPaolo Bonzini line_ptr); 1580d4e995cSDaniel P. Berrange return; 15910578a25SPaolo Bonzini } 1600d4e995cSDaniel P. Berrange continue; 1610d4e995cSDaniel P. Berrange } 1620d4e995cSDaniel P. Berrange 1630d4e995cSDaniel P. Berrange /* start tracing */ 1640d4e995cSDaniel P. Berrange trace_event_set_state_dynamic(ev, enable); 1650d4e995cSDaniel P. Berrange if (!is_pattern) { 1660d4e995cSDaniel P. Berrange return; 1670d4e995cSDaniel P. Berrange } 1680d4e995cSDaniel P. Berrange } 1690d4e995cSDaniel P. Berrange 1700d4e995cSDaniel P. Berrange if (!is_pattern) { 1713dc6f869SAlistair Francis warn_report("trace event '%s' does not exist", 1720d4e995cSDaniel P. Berrange line_ptr); 17310578a25SPaolo Bonzini } 17410578a25SPaolo Bonzini } 17510578a25SPaolo Bonzini 176e9527dd3SPaolo Bonzini void trace_enable_events(const char *line_buf) 177e9527dd3SPaolo Bonzini { 178e9527dd3SPaolo Bonzini if (is_help_option(line_buf)) { 179e9527dd3SPaolo Bonzini trace_list_events(); 180947e4744SKevin Wolf if (monitor_cur() == NULL) { 181e9527dd3SPaolo Bonzini exit(0); 182acc6809dSDenis V. Lunev } 183e9527dd3SPaolo Bonzini } else { 184e9527dd3SPaolo Bonzini do_trace_enable_events(line_buf); 185e9527dd3SPaolo Bonzini } 186e9527dd3SPaolo Bonzini } 187e9527dd3SPaolo Bonzini 188e9e0bb2aSDenis V. Lunev static void trace_init_events(const char *fname) 18923d15e86SLluís { 190a35d9be6SAlexey Kardashevskiy Location loc; 191a35d9be6SAlexey Kardashevskiy FILE *fp; 192a35d9be6SAlexey Kardashevskiy char line_buf[1024]; 193a35d9be6SAlexey Kardashevskiy size_t line_idx = 0; 194a35d9be6SAlexey Kardashevskiy 19523d15e86SLluís if (fname == NULL) { 19623d15e86SLluís return; 19723d15e86SLluís } 19823d15e86SLluís 199a35d9be6SAlexey Kardashevskiy loc_push_none(&loc); 200a35d9be6SAlexey Kardashevskiy loc_set_file(fname, 0); 201a35d9be6SAlexey Kardashevskiy fp = fopen(fname, "r"); 20223d15e86SLluís if (!fp) { 203a35d9be6SAlexey Kardashevskiy error_report("%s", strerror(errno)); 20423d15e86SLluís exit(1); 20523d15e86SLluís } 20623d15e86SLluís while (fgets(line_buf, sizeof(line_buf), fp)) { 207a35d9be6SAlexey Kardashevskiy loc_set_file(fname, ++line_idx); 20823d15e86SLluís size_t len = strlen(line_buf); 20923d15e86SLluís if (len > 1) { /* skip empty lines */ 21023d15e86SLluís line_buf[len - 1] = '\0'; 211794b1f96SAlexey Kardashevskiy if ('#' == line_buf[0]) { /* skip commented lines */ 212794b1f96SAlexey Kardashevskiy continue; 213794b1f96SAlexey Kardashevskiy } 21410578a25SPaolo Bonzini trace_enable_events(line_buf); 21523d15e86SLluís } 21682432638SAlexey Kardashevskiy } 21723d15e86SLluís if (fclose(fp) != 0) { 218a35d9be6SAlexey Kardashevskiy loc_set_file(fname, 0); 219a35d9be6SAlexey Kardashevskiy error_report("%s", strerror(errno)); 22023d15e86SLluís exit(1); 22123d15e86SLluís } 222a35d9be6SAlexey Kardashevskiy loc_pop(&loc); 22323d15e86SLluís } 2245b808275SLluís Vilanova 225*92eecfffSPaolo Bonzini void trace_init_file(void) 2265b808275SLluís Vilanova { 227*92eecfffSPaolo Bonzini QemuOpts *opts = qemu_find_opts_singleton("trace"); 228*92eecfffSPaolo Bonzini const char *file = qemu_opt_get(opts, "file"); 2295b808275SLluís Vilanova #ifdef CONFIG_TRACE_SIMPLE 23041fc57e4SPaolo Bonzini st_set_trace_file(file); 231648b4823SJosh DuBois if (init_trace_on_startup) { 2321b7157beSJosh DuBois st_set_trace_file_enabled(true); 233648b4823SJosh DuBois } 234ed7f5f1dSPaolo Bonzini #elif defined CONFIG_TRACE_LOG 2353d88754eSAlex Bennée /* 2363d88754eSAlex Bennée * If both the simple and the log backends are enabled, "--trace file" 2373d88754eSAlex Bennée * only applies to the simple backend; use "-D" for the log 2383d88754eSAlex Bennée * backend. However we should only override -D if we actually have 2393d88754eSAlex Bennée * something to override it with. 240ed7f5f1dSPaolo Bonzini */ 2413d88754eSAlex Bennée if (file) { 242daa76aa4SMarkus Armbruster qemu_set_log_filename(file, &error_fatal); 2433d88754eSAlex Bennée } 2445b808275SLluís Vilanova #else 2455b808275SLluís Vilanova if (file) { 246db817b8cSYaowei Bai fprintf(stderr, "error: --trace file=...: " 2475b808275SLluís Vilanova "option not supported by the selected tracing backends\n"); 24841fc57e4SPaolo Bonzini exit(1); 24941fc57e4SPaolo Bonzini } 25041fc57e4SPaolo Bonzini #endif 25141fc57e4SPaolo Bonzini } 25241fc57e4SPaolo Bonzini 25382e95ec8SLluís Vilanova void trace_fini_vcpu(CPUState *vcpu) 25482e95ec8SLluís Vilanova { 25582e95ec8SLluís Vilanova TraceEventIter iter; 25682e95ec8SLluís Vilanova TraceEvent *ev; 25782e95ec8SLluís Vilanova 258a47e8715SLluís Vilanova trace_guest_cpu_exit(vcpu); 259a47e8715SLluís Vilanova 26082e95ec8SLluís Vilanova trace_event_iter_init(&iter, NULL); 26182e95ec8SLluís Vilanova while ((ev = trace_event_iter_next(&iter)) != NULL) { 26282e95ec8SLluís Vilanova if (trace_event_is_vcpu(ev) && 26382e95ec8SLluís Vilanova trace_event_get_state_static(ev) && 26482e95ec8SLluís Vilanova trace_event_get_vcpu_state_dynamic(vcpu, ev)) { 26582e95ec8SLluís Vilanova /* must disable to affect the global counter */ 26682e95ec8SLluís Vilanova trace_event_set_vcpu_state_dynamic(vcpu, ev, false); 26782e95ec8SLluís Vilanova } 26882e95ec8SLluís Vilanova } 26982e95ec8SLluís Vilanova } 27082e95ec8SLluís Vilanova 27141fc57e4SPaolo Bonzini bool trace_init_backends(void) 27241fc57e4SPaolo Bonzini { 27341fc57e4SPaolo Bonzini #ifdef CONFIG_TRACE_SIMPLE 27441fc57e4SPaolo Bonzini if (!st_init()) { 27541fc57e4SPaolo Bonzini fprintf(stderr, "failed to initialize simple tracing backend.\n"); 2765b808275SLluís Vilanova return false; 2775b808275SLluís Vilanova } 2785b808275SLluís Vilanova #endif 2795b808275SLluís Vilanova 2805b808275SLluís Vilanova #ifdef CONFIG_TRACE_FTRACE 2815b808275SLluís Vilanova if (!ftrace_init()) { 2825b808275SLluís Vilanova fprintf(stderr, "failed to initialize ftrace backend.\n"); 2835b808275SLluís Vilanova return false; 2845b808275SLluís Vilanova } 2855b808275SLluís Vilanova #endif 2865b808275SLluís Vilanova 2870a852417SPaul Durrant #ifdef CONFIG_TRACE_SYSLOG 2880a852417SPaul Durrant openlog(NULL, LOG_PID, LOG_DAEMON); 2890a852417SPaul Durrant #endif 2900a852417SPaul Durrant 2915b808275SLluís Vilanova return true; 2925b808275SLluís Vilanova } 293e9e0bb2aSDenis V. Lunev 294*92eecfffSPaolo Bonzini void trace_opt_parse(const char *optarg) 295e9e0bb2aSDenis V. Lunev { 296e9e0bb2aSDenis V. Lunev QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("trace"), 297e9e0bb2aSDenis V. Lunev optarg, true); 298e9e0bb2aSDenis V. Lunev if (!opts) { 299e9e0bb2aSDenis V. Lunev exit(1); 300e9e0bb2aSDenis V. Lunev } 301e9e0bb2aSDenis V. Lunev if (qemu_opt_get(opts, "enable")) { 302e9e0bb2aSDenis V. Lunev trace_enable_events(qemu_opt_get(opts, "enable")); 303e9e0bb2aSDenis V. Lunev } 304e9e0bb2aSDenis V. Lunev trace_init_events(qemu_opt_get(opts, "events")); 305648b4823SJosh DuBois init_trace_on_startup = true; 306e9e0bb2aSDenis V. Lunev qemu_opts_del(opts); 307e9e0bb2aSDenis V. Lunev } 308b7d48952SDaniel P. Berrange 309b7d48952SDaniel P. Berrange uint32_t trace_get_vcpu_event_count(void) 310b7d48952SDaniel P. Berrange { 311ca3fa0e8SDaniel P. Berrange return next_vcpu_id; 312b7d48952SDaniel P. Berrange } 313