123d15e86SLluís /* 223d15e86SLluís * Interface for configuring and controlling the state of tracing events. 323d15e86SLluís * 45b808275SLluís Vilanova * Copyright (C) 2011-2014 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 1023d15e86SLluís #include "trace/control.h" 115b808275SLluís Vilanova #ifdef CONFIG_TRACE_SIMPLE 125b808275SLluís Vilanova #include "trace/simple.h" 135b808275SLluís Vilanova #endif 145b808275SLluís Vilanova #ifdef CONFIG_TRACE_FTRACE 155b808275SLluís Vilanova #include "trace/ftrace.h" 165b808275SLluís Vilanova #endif 17a35d9be6SAlexey Kardashevskiy #include "qemu/error-report.h" 1823d15e86SLluís 1943b48cfcSPaolo Bonzini int trace_events_enabled_count; 20585ec727SPaolo Bonzini bool trace_events_dstate[TRACE_EVENT_COUNT]; 2143b48cfcSPaolo Bonzini 22b1bae816SLluís Vilanova TraceEvent *trace_event_name(const char *name) 23b1bae816SLluís Vilanova { 24b1bae816SLluís Vilanova assert(name != NULL); 25b1bae816SLluís Vilanova 26b1bae816SLluís Vilanova TraceEventID i; 27b1bae816SLluís Vilanova for (i = 0; i < trace_event_count(); i++) { 28b1bae816SLluís Vilanova TraceEvent *ev = trace_event_id(i); 29b1bae816SLluís Vilanova if (strcmp(trace_event_get_name(ev), name) == 0) { 30b1bae816SLluís Vilanova return ev; 31b1bae816SLluís Vilanova } 32b1bae816SLluís Vilanova } 33b1bae816SLluís Vilanova return NULL; 34b1bae816SLluís Vilanova } 35b1bae816SLluís Vilanova 36b1bae816SLluís Vilanova static bool pattern_glob(const char *pat, const char *ev) 37b1bae816SLluís Vilanova { 38b1bae816SLluís Vilanova while (*pat != '\0' && *ev != '\0') { 39b1bae816SLluís Vilanova if (*pat == *ev) { 40b1bae816SLluís Vilanova pat++; 41b1bae816SLluís Vilanova ev++; 42b1bae816SLluís Vilanova } 43b1bae816SLluís Vilanova else if (*pat == '*') { 44b1bae816SLluís Vilanova if (pattern_glob(pat, ev+1)) { 45b1bae816SLluís Vilanova return true; 46b1bae816SLluís Vilanova } else if (pattern_glob(pat+1, ev)) { 47b1bae816SLluís Vilanova return true; 48b1bae816SLluís Vilanova } else { 49b1bae816SLluís Vilanova return false; 50b1bae816SLluís Vilanova } 51b1bae816SLluís Vilanova } else { 52b1bae816SLluís Vilanova return false; 53b1bae816SLluís Vilanova } 54b1bae816SLluís Vilanova } 55b1bae816SLluís Vilanova 56b1bae816SLluís Vilanova while (*pat == '*') { 57b1bae816SLluís Vilanova pat++; 58b1bae816SLluís Vilanova } 59b1bae816SLluís Vilanova 60b1bae816SLluís Vilanova if (*pat == '\0' && *ev == '\0') { 61b1bae816SLluís Vilanova return true; 62b1bae816SLluís Vilanova } else { 63b1bae816SLluís Vilanova return false; 64b1bae816SLluís Vilanova } 65b1bae816SLluís Vilanova } 66b1bae816SLluís Vilanova 67b1bae816SLluís Vilanova TraceEvent *trace_event_pattern(const char *pat, TraceEvent *ev) 68b1bae816SLluís Vilanova { 69b1bae816SLluís Vilanova assert(pat != NULL); 70b1bae816SLluís Vilanova 71b1bae816SLluís Vilanova TraceEventID i; 72b1bae816SLluís Vilanova 73b1bae816SLluís Vilanova if (ev == NULL) { 74b1bae816SLluís Vilanova i = -1; 75b1bae816SLluís Vilanova } else { 76b1bae816SLluís Vilanova i = trace_event_get_id(ev); 77b1bae816SLluís Vilanova } 78b1bae816SLluís Vilanova i++; 79b1bae816SLluís Vilanova 80b1bae816SLluís Vilanova while (i < trace_event_count()) { 81b1bae816SLluís Vilanova TraceEvent *res = trace_event_id(i); 82b1bae816SLluís Vilanova if (pattern_glob(pat, trace_event_get_name(res))) { 83b1bae816SLluís Vilanova return res; 84b1bae816SLluís Vilanova } 85b1bae816SLluís Vilanova i++; 86b1bae816SLluís Vilanova } 87b1bae816SLluís Vilanova 88b1bae816SLluís Vilanova return NULL; 89b1bae816SLluís Vilanova } 90b1bae816SLluís Vilanova 9145bd0b41SPaolo Bonzini void trace_init_events(const char *fname) 9223d15e86SLluís { 93a35d9be6SAlexey Kardashevskiy Location loc; 94a35d9be6SAlexey Kardashevskiy FILE *fp; 95a35d9be6SAlexey Kardashevskiy char line_buf[1024]; 96a35d9be6SAlexey Kardashevskiy size_t line_idx = 0; 97a35d9be6SAlexey Kardashevskiy 9823d15e86SLluís if (fname == NULL) { 9923d15e86SLluís return; 10023d15e86SLluís } 10123d15e86SLluís 102a35d9be6SAlexey Kardashevskiy loc_push_none(&loc); 103a35d9be6SAlexey Kardashevskiy loc_set_file(fname, 0); 104a35d9be6SAlexey Kardashevskiy fp = fopen(fname, "r"); 10523d15e86SLluís if (!fp) { 106a35d9be6SAlexey Kardashevskiy error_report("%s", strerror(errno)); 10723d15e86SLluís exit(1); 10823d15e86SLluís } 10923d15e86SLluís while (fgets(line_buf, sizeof(line_buf), fp)) { 110a35d9be6SAlexey Kardashevskiy loc_set_file(fname, ++line_idx); 11123d15e86SLluís size_t len = strlen(line_buf); 11223d15e86SLluís if (len > 1) { /* skip empty lines */ 11323d15e86SLluís line_buf[len - 1] = '\0'; 114794b1f96SAlexey Kardashevskiy if ('#' == line_buf[0]) { /* skip commented lines */ 115794b1f96SAlexey Kardashevskiy continue; 116794b1f96SAlexey Kardashevskiy } 117b1bae816SLluís Vilanova const bool enable = ('-' != line_buf[0]); 118b1bae816SLluís Vilanova char *line_ptr = enable ? line_buf : line_buf + 1; 119b1bae816SLluís Vilanova if (trace_event_is_pattern(line_ptr)) { 120b1bae816SLluís Vilanova TraceEvent *ev = NULL; 121b1bae816SLluís Vilanova while ((ev = trace_event_pattern(line_ptr, ev)) != NULL) { 122b1bae816SLluís Vilanova if (trace_event_get_state_static(ev)) { 123b1bae816SLluís Vilanova trace_event_set_state_dynamic(ev, enable); 124ddde8accSGerd Hoffmann } 125b1bae816SLluís Vilanova } 126b1bae816SLluís Vilanova } else { 127b1bae816SLluís Vilanova TraceEvent *ev = trace_event_name(line_ptr); 128b1bae816SLluís Vilanova if (ev == NULL) { 129a35d9be6SAlexey Kardashevskiy error_report("WARNING: trace event '%s' does not exist", 13082432638SAlexey Kardashevskiy line_ptr); 13182432638SAlexey Kardashevskiy } else if (!trace_event_get_state_static(ev)) { 13281b07353SGonglei error_report("WARNING: trace event '%s' is not traceable", 13382432638SAlexey Kardashevskiy line_ptr); 13482432638SAlexey Kardashevskiy } else { 135b1bae816SLluís Vilanova trace_event_set_state_dynamic(ev, enable); 136b1bae816SLluís Vilanova } 13723d15e86SLluís } 13823d15e86SLluís } 13982432638SAlexey Kardashevskiy } 14023d15e86SLluís if (fclose(fp) != 0) { 141a35d9be6SAlexey Kardashevskiy loc_set_file(fname, 0); 142a35d9be6SAlexey Kardashevskiy error_report("%s", strerror(errno)); 14323d15e86SLluís exit(1); 14423d15e86SLluís } 145a35d9be6SAlexey Kardashevskiy loc_pop(&loc); 14623d15e86SLluís } 1475b808275SLluís Vilanova 148*41fc57e4SPaolo Bonzini void trace_init_file(const char *file) 1495b808275SLluís Vilanova { 1505b808275SLluís Vilanova #ifdef CONFIG_TRACE_SIMPLE 151*41fc57e4SPaolo Bonzini st_set_trace_file(file); 1525b808275SLluís Vilanova #else 1535b808275SLluís Vilanova if (file) { 1545b808275SLluís Vilanova fprintf(stderr, "error: -trace file=...: " 1555b808275SLluís Vilanova "option not supported by the selected tracing backends\n"); 156*41fc57e4SPaolo Bonzini exit(1); 157*41fc57e4SPaolo Bonzini } 158*41fc57e4SPaolo Bonzini #endif 159*41fc57e4SPaolo Bonzini } 160*41fc57e4SPaolo Bonzini 161*41fc57e4SPaolo Bonzini bool trace_init_backends(void) 162*41fc57e4SPaolo Bonzini { 163*41fc57e4SPaolo Bonzini #ifdef CONFIG_TRACE_SIMPLE 164*41fc57e4SPaolo Bonzini if (!st_init()) { 165*41fc57e4SPaolo Bonzini fprintf(stderr, "failed to initialize simple tracing backend.\n"); 1665b808275SLluís Vilanova return false; 1675b808275SLluís Vilanova } 1685b808275SLluís Vilanova #endif 1695b808275SLluís Vilanova 1705b808275SLluís Vilanova #ifdef CONFIG_TRACE_FTRACE 1715b808275SLluís Vilanova if (!ftrace_init()) { 1725b808275SLluís Vilanova fprintf(stderr, "failed to initialize ftrace backend.\n"); 1735b808275SLluís Vilanova return false; 1745b808275SLluís Vilanova } 1755b808275SLluís Vilanova #endif 1765b808275SLluís Vilanova 1775b808275SLluís Vilanova return true; 1785b808275SLluís Vilanova } 179