123d15e86SLluís /* 223d15e86SLluís * Interface for configuring and controlling the state of tracing events. 323d15e86SLluís * 4*48151859SLluí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" 135b808275SLluís Vilanova #ifdef CONFIG_TRACE_SIMPLE 145b808275SLluís Vilanova #include "trace/simple.h" 155b808275SLluís Vilanova #endif 165b808275SLluís Vilanova #ifdef CONFIG_TRACE_FTRACE 175b808275SLluís Vilanova #include "trace/ftrace.h" 185b808275SLluís Vilanova #endif 19ed7f5f1dSPaolo Bonzini #ifdef CONFIG_TRACE_LOG 20ed7f5f1dSPaolo Bonzini #include "qemu/log.h" 21ed7f5f1dSPaolo Bonzini #endif 22daa76aa4SMarkus Armbruster #include "qapi/error.h" 23a35d9be6SAlexey Kardashevskiy #include "qemu/error-report.h" 24e9e0bb2aSDenis V. Lunev #include "qemu/config-file.h" 25acc6809dSDenis V. Lunev #include "monitor/monitor.h" 2623d15e86SLluís 2743b48cfcSPaolo Bonzini int trace_events_enabled_count; 28*48151859SLluís Vilanova /* 29*48151859SLluís Vilanova * Interpretation depends on wether the event has the 'vcpu' property: 30*48151859SLluís Vilanova * - false: Boolean value indicating whether the event is active. 31*48151859SLluís Vilanova * - true : Integral counting the number of vCPUs that have this event enabled. 32*48151859SLluís Vilanova */ 33*48151859SLluís Vilanova uint16_t trace_events_dstate[TRACE_EVENT_COUNT]; 34*48151859SLluís Vilanova /* Marks events for late vCPU state init */ 35*48151859SLluís Vilanova static bool trace_events_dstate_init[TRACE_EVENT_COUNT]; 3643b48cfcSPaolo Bonzini 37e9e0bb2aSDenis V. Lunev QemuOptsList qemu_trace_opts = { 38e9e0bb2aSDenis V. Lunev .name = "trace", 39e9e0bb2aSDenis V. Lunev .implied_opt_name = "enable", 40e9e0bb2aSDenis V. Lunev .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head), 41e9e0bb2aSDenis V. Lunev .desc = { 42e9e0bb2aSDenis V. Lunev { 43e9e0bb2aSDenis V. Lunev .name = "enable", 44e9e0bb2aSDenis V. Lunev .type = QEMU_OPT_STRING, 45e9e0bb2aSDenis V. Lunev }, 46e9e0bb2aSDenis V. Lunev { 47e9e0bb2aSDenis V. Lunev .name = "events", 48e9e0bb2aSDenis V. Lunev .type = QEMU_OPT_STRING, 49e9e0bb2aSDenis V. Lunev },{ 50e9e0bb2aSDenis V. Lunev .name = "file", 51e9e0bb2aSDenis V. Lunev .type = QEMU_OPT_STRING, 52e9e0bb2aSDenis V. Lunev }, 53e9e0bb2aSDenis V. Lunev { /* end of list */ } 54e9e0bb2aSDenis V. Lunev }, 55e9e0bb2aSDenis V. Lunev }; 56e9e0bb2aSDenis V. Lunev 57e9e0bb2aSDenis V. Lunev 58b1bae816SLluís Vilanova TraceEvent *trace_event_name(const char *name) 59b1bae816SLluís Vilanova { 60b1bae816SLluís Vilanova assert(name != NULL); 61b1bae816SLluís Vilanova 62b1bae816SLluís Vilanova TraceEventID i; 63b1bae816SLluís Vilanova for (i = 0; i < trace_event_count(); i++) { 64b1bae816SLluís Vilanova TraceEvent *ev = trace_event_id(i); 65b1bae816SLluís Vilanova if (strcmp(trace_event_get_name(ev), name) == 0) { 66b1bae816SLluís Vilanova return ev; 67b1bae816SLluís Vilanova } 68b1bae816SLluís Vilanova } 69b1bae816SLluís Vilanova return NULL; 70b1bae816SLluís Vilanova } 71b1bae816SLluís Vilanova 72b1bae816SLluís Vilanova static bool pattern_glob(const char *pat, const char *ev) 73b1bae816SLluís Vilanova { 74b1bae816SLluís Vilanova while (*pat != '\0' && *ev != '\0') { 75b1bae816SLluís Vilanova if (*pat == *ev) { 76b1bae816SLluís Vilanova pat++; 77b1bae816SLluís Vilanova ev++; 78b1bae816SLluís Vilanova } 79b1bae816SLluís Vilanova else if (*pat == '*') { 80b1bae816SLluís Vilanova if (pattern_glob(pat, ev+1)) { 81b1bae816SLluís Vilanova return true; 82b1bae816SLluís Vilanova } else if (pattern_glob(pat+1, ev)) { 83b1bae816SLluís Vilanova return true; 84b1bae816SLluís Vilanova } else { 85b1bae816SLluís Vilanova return false; 86b1bae816SLluís Vilanova } 87b1bae816SLluís Vilanova } else { 88b1bae816SLluís Vilanova return false; 89b1bae816SLluís Vilanova } 90b1bae816SLluís Vilanova } 91b1bae816SLluís Vilanova 92b1bae816SLluís Vilanova while (*pat == '*') { 93b1bae816SLluís Vilanova pat++; 94b1bae816SLluís Vilanova } 95b1bae816SLluís Vilanova 96b1bae816SLluís Vilanova if (*pat == '\0' && *ev == '\0') { 97b1bae816SLluís Vilanova return true; 98b1bae816SLluís Vilanova } else { 99b1bae816SLluís Vilanova return false; 100b1bae816SLluís Vilanova } 101b1bae816SLluís Vilanova } 102b1bae816SLluís Vilanova 103b1bae816SLluís Vilanova TraceEvent *trace_event_pattern(const char *pat, TraceEvent *ev) 104b1bae816SLluís Vilanova { 105b1bae816SLluís Vilanova assert(pat != NULL); 106b1bae816SLluís Vilanova 107b1bae816SLluís Vilanova TraceEventID i; 108b1bae816SLluís Vilanova 109b1bae816SLluís Vilanova if (ev == NULL) { 110b1bae816SLluís Vilanova i = -1; 111b1bae816SLluís Vilanova } else { 112b1bae816SLluís Vilanova i = trace_event_get_id(ev); 113b1bae816SLluís Vilanova } 114b1bae816SLluís Vilanova i++; 115b1bae816SLluís Vilanova 116b1bae816SLluís Vilanova while (i < trace_event_count()) { 117b1bae816SLluís Vilanova TraceEvent *res = trace_event_id(i); 118b1bae816SLluís Vilanova if (pattern_glob(pat, trace_event_get_name(res))) { 119b1bae816SLluís Vilanova return res; 120b1bae816SLluís Vilanova } 121b1bae816SLluís Vilanova i++; 122b1bae816SLluís Vilanova } 123b1bae816SLluís Vilanova 124b1bae816SLluís Vilanova return NULL; 125b1bae816SLluís Vilanova } 126b1bae816SLluís Vilanova 127e9527dd3SPaolo Bonzini void trace_list_events(void) 128e9527dd3SPaolo Bonzini { 129e9527dd3SPaolo Bonzini int i; 130e9527dd3SPaolo Bonzini for (i = 0; i < trace_event_count(); i++) { 131e9527dd3SPaolo Bonzini TraceEvent *res = trace_event_id(i); 132e9527dd3SPaolo Bonzini fprintf(stderr, "%s\n", trace_event_get_name(res)); 133e9527dd3SPaolo Bonzini } 134e9527dd3SPaolo Bonzini } 135e9527dd3SPaolo Bonzini 136e9527dd3SPaolo Bonzini static void do_trace_enable_events(const char *line_buf) 13710578a25SPaolo Bonzini { 13810578a25SPaolo Bonzini const bool enable = ('-' != line_buf[0]); 13910578a25SPaolo Bonzini const char *line_ptr = enable ? line_buf : line_buf + 1; 14010578a25SPaolo Bonzini 14110578a25SPaolo Bonzini if (trace_event_is_pattern(line_ptr)) { 14210578a25SPaolo Bonzini TraceEvent *ev = NULL; 14310578a25SPaolo Bonzini while ((ev = trace_event_pattern(line_ptr, ev)) != NULL) { 14410578a25SPaolo Bonzini if (trace_event_get_state_static(ev)) { 145*48151859SLluís Vilanova /* start tracing */ 14610578a25SPaolo Bonzini trace_event_set_state_dynamic(ev, enable); 147*48151859SLluís Vilanova /* mark for late vCPU init */ 148*48151859SLluís Vilanova trace_events_dstate_init[ev->id] = true; 14910578a25SPaolo Bonzini } 15010578a25SPaolo Bonzini } 15110578a25SPaolo Bonzini } else { 15210578a25SPaolo Bonzini TraceEvent *ev = trace_event_name(line_ptr); 15310578a25SPaolo Bonzini if (ev == NULL) { 15410578a25SPaolo Bonzini error_report("WARNING: trace event '%s' does not exist", 15510578a25SPaolo Bonzini line_ptr); 15610578a25SPaolo Bonzini } else if (!trace_event_get_state_static(ev)) { 15710578a25SPaolo Bonzini error_report("WARNING: trace event '%s' is not traceable", 15810578a25SPaolo Bonzini line_ptr); 15910578a25SPaolo Bonzini } else { 160*48151859SLluís Vilanova /* start tracing */ 16110578a25SPaolo Bonzini trace_event_set_state_dynamic(ev, enable); 162*48151859SLluís Vilanova /* mark for late vCPU init */ 163*48151859SLluís Vilanova trace_events_dstate_init[ev->id] = true; 16410578a25SPaolo Bonzini } 16510578a25SPaolo Bonzini } 16610578a25SPaolo Bonzini } 16710578a25SPaolo Bonzini 168e9527dd3SPaolo Bonzini void trace_enable_events(const char *line_buf) 169e9527dd3SPaolo Bonzini { 170e9527dd3SPaolo Bonzini if (is_help_option(line_buf)) { 171e9527dd3SPaolo Bonzini trace_list_events(); 172acc6809dSDenis V. Lunev if (cur_mon == NULL) { 173e9527dd3SPaolo Bonzini exit(0); 174acc6809dSDenis V. Lunev } 175e9527dd3SPaolo Bonzini } else { 176e9527dd3SPaolo Bonzini do_trace_enable_events(line_buf); 177e9527dd3SPaolo Bonzini } 178e9527dd3SPaolo Bonzini } 179e9527dd3SPaolo Bonzini 180e9e0bb2aSDenis V. Lunev static void trace_init_events(const char *fname) 18123d15e86SLluís { 182a35d9be6SAlexey Kardashevskiy Location loc; 183a35d9be6SAlexey Kardashevskiy FILE *fp; 184a35d9be6SAlexey Kardashevskiy char line_buf[1024]; 185a35d9be6SAlexey Kardashevskiy size_t line_idx = 0; 186a35d9be6SAlexey Kardashevskiy 18723d15e86SLluís if (fname == NULL) { 18823d15e86SLluís return; 18923d15e86SLluís } 19023d15e86SLluís 191a35d9be6SAlexey Kardashevskiy loc_push_none(&loc); 192a35d9be6SAlexey Kardashevskiy loc_set_file(fname, 0); 193a35d9be6SAlexey Kardashevskiy fp = fopen(fname, "r"); 19423d15e86SLluís if (!fp) { 195a35d9be6SAlexey Kardashevskiy error_report("%s", strerror(errno)); 19623d15e86SLluís exit(1); 19723d15e86SLluís } 19823d15e86SLluís while (fgets(line_buf, sizeof(line_buf), fp)) { 199a35d9be6SAlexey Kardashevskiy loc_set_file(fname, ++line_idx); 20023d15e86SLluís size_t len = strlen(line_buf); 20123d15e86SLluís if (len > 1) { /* skip empty lines */ 20223d15e86SLluís line_buf[len - 1] = '\0'; 203794b1f96SAlexey Kardashevskiy if ('#' == line_buf[0]) { /* skip commented lines */ 204794b1f96SAlexey Kardashevskiy continue; 205794b1f96SAlexey Kardashevskiy } 20610578a25SPaolo Bonzini trace_enable_events(line_buf); 20723d15e86SLluís } 20882432638SAlexey Kardashevskiy } 20923d15e86SLluís if (fclose(fp) != 0) { 210a35d9be6SAlexey Kardashevskiy loc_set_file(fname, 0); 211a35d9be6SAlexey Kardashevskiy error_report("%s", strerror(errno)); 21223d15e86SLluís exit(1); 21323d15e86SLluís } 214a35d9be6SAlexey Kardashevskiy loc_pop(&loc); 21523d15e86SLluís } 2165b808275SLluís Vilanova 21741fc57e4SPaolo Bonzini void trace_init_file(const char *file) 2185b808275SLluís Vilanova { 2195b808275SLluís Vilanova #ifdef CONFIG_TRACE_SIMPLE 22041fc57e4SPaolo Bonzini st_set_trace_file(file); 221ed7f5f1dSPaolo Bonzini #elif defined CONFIG_TRACE_LOG 222ed7f5f1dSPaolo Bonzini /* If both the simple and the log backends are enabled, "-trace file" 223ed7f5f1dSPaolo Bonzini * only applies to the simple backend; use "-D" for the log backend. 224ed7f5f1dSPaolo Bonzini */ 225ed7f5f1dSPaolo Bonzini if (file) { 226daa76aa4SMarkus Armbruster qemu_set_log_filename(file, &error_fatal); 227ed7f5f1dSPaolo Bonzini } 2285b808275SLluís Vilanova #else 2295b808275SLluís Vilanova if (file) { 2305b808275SLluís Vilanova fprintf(stderr, "error: -trace file=...: " 2315b808275SLluís Vilanova "option not supported by the selected tracing backends\n"); 23241fc57e4SPaolo Bonzini exit(1); 23341fc57e4SPaolo Bonzini } 23441fc57e4SPaolo Bonzini #endif 23541fc57e4SPaolo Bonzini } 23641fc57e4SPaolo Bonzini 23741fc57e4SPaolo Bonzini bool trace_init_backends(void) 23841fc57e4SPaolo Bonzini { 23941fc57e4SPaolo Bonzini #ifdef CONFIG_TRACE_SIMPLE 24041fc57e4SPaolo Bonzini if (!st_init()) { 24141fc57e4SPaolo Bonzini fprintf(stderr, "failed to initialize simple tracing backend.\n"); 2425b808275SLluís Vilanova return false; 2435b808275SLluís Vilanova } 2445b808275SLluís Vilanova #endif 2455b808275SLluís Vilanova 2465b808275SLluís Vilanova #ifdef CONFIG_TRACE_FTRACE 2475b808275SLluís Vilanova if (!ftrace_init()) { 2485b808275SLluís Vilanova fprintf(stderr, "failed to initialize ftrace backend.\n"); 2495b808275SLluís Vilanova return false; 2505b808275SLluís Vilanova } 2515b808275SLluís Vilanova #endif 2525b808275SLluís Vilanova 2535b808275SLluís Vilanova return true; 2545b808275SLluís Vilanova } 255e9e0bb2aSDenis V. Lunev 256e9e0bb2aSDenis V. Lunev char *trace_opt_parse(const char *optarg) 257e9e0bb2aSDenis V. Lunev { 258e9e0bb2aSDenis V. Lunev char *trace_file; 259e9e0bb2aSDenis V. Lunev QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("trace"), 260e9e0bb2aSDenis V. Lunev optarg, true); 261e9e0bb2aSDenis V. Lunev if (!opts) { 262e9e0bb2aSDenis V. Lunev exit(1); 263e9e0bb2aSDenis V. Lunev } 264e9e0bb2aSDenis V. Lunev if (qemu_opt_get(opts, "enable")) { 265e9e0bb2aSDenis V. Lunev trace_enable_events(qemu_opt_get(opts, "enable")); 266e9e0bb2aSDenis V. Lunev } 267e9e0bb2aSDenis V. Lunev trace_init_events(qemu_opt_get(opts, "events")); 268e9e0bb2aSDenis V. Lunev trace_file = g_strdup(qemu_opt_get(opts, "file")); 269e9e0bb2aSDenis V. Lunev qemu_opts_del(opts); 270e9e0bb2aSDenis V. Lunev 271e9e0bb2aSDenis V. Lunev return trace_file; 272e9e0bb2aSDenis V. Lunev } 273*48151859SLluís Vilanova 274*48151859SLluís Vilanova void trace_init_vcpu_events(void) 275*48151859SLluís Vilanova { 276*48151859SLluís Vilanova TraceEvent *ev = NULL; 277*48151859SLluís Vilanova while ((ev = trace_event_pattern("*", ev)) != NULL) { 278*48151859SLluís Vilanova if (trace_event_is_vcpu(ev) && 279*48151859SLluís Vilanova trace_event_get_state_static(ev) && 280*48151859SLluís Vilanova trace_events_dstate_init[ev->id]) { 281*48151859SLluís Vilanova trace_event_set_state_dynamic(ev, true); 282*48151859SLluís Vilanova } 283*48151859SLluís Vilanova } 284*48151859SLluís Vilanova } 285