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 10 #include "qemu/osdep.h" 11 #include "trace/control.h" 12 #include "qemu/help_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 #include "qapi/error.h" 23 #include "qemu/error-report.h" 24 #include "qemu/config-file.h" 25 #include "monitor/monitor.h" 26 27 int trace_events_enabled_count; 28 /* 29 * Interpretation depends on wether the event has the 'vcpu' property: 30 * - false: Boolean value indicating whether the event is active. 31 * - true : Integral counting the number of vCPUs that have this event enabled. 32 */ 33 uint16_t trace_events_dstate[TRACE_EVENT_COUNT]; 34 /* Marks events for late vCPU state init */ 35 static bool trace_events_dstate_init[TRACE_EVENT_COUNT]; 36 37 QemuOptsList qemu_trace_opts = { 38 .name = "trace", 39 .implied_opt_name = "enable", 40 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head), 41 .desc = { 42 { 43 .name = "enable", 44 .type = QEMU_OPT_STRING, 45 }, 46 { 47 .name = "events", 48 .type = QEMU_OPT_STRING, 49 },{ 50 .name = "file", 51 .type = QEMU_OPT_STRING, 52 }, 53 { /* end of list */ } 54 }, 55 }; 56 57 58 TraceEvent *trace_event_name(const char *name) 59 { 60 assert(name != NULL); 61 62 TraceEventID i; 63 for (i = 0; i < trace_event_count(); i++) { 64 TraceEvent *ev = trace_event_id(i); 65 if (strcmp(trace_event_get_name(ev), name) == 0) { 66 return ev; 67 } 68 } 69 return NULL; 70 } 71 72 static bool pattern_glob(const char *pat, const char *ev) 73 { 74 while (*pat != '\0' && *ev != '\0') { 75 if (*pat == *ev) { 76 pat++; 77 ev++; 78 } 79 else if (*pat == '*') { 80 if (pattern_glob(pat, ev+1)) { 81 return true; 82 } else if (pattern_glob(pat+1, ev)) { 83 return true; 84 } else { 85 return false; 86 } 87 } else { 88 return false; 89 } 90 } 91 92 while (*pat == '*') { 93 pat++; 94 } 95 96 if (*pat == '\0' && *ev == '\0') { 97 return true; 98 } else { 99 return false; 100 } 101 } 102 103 TraceEvent *trace_event_pattern(const char *pat, TraceEvent *ev) 104 { 105 assert(pat != NULL); 106 107 TraceEventID i; 108 109 if (ev == NULL) { 110 i = -1; 111 } else { 112 i = trace_event_get_id(ev); 113 } 114 i++; 115 116 while (i < trace_event_count()) { 117 TraceEvent *res = trace_event_id(i); 118 if (pattern_glob(pat, trace_event_get_name(res))) { 119 return res; 120 } 121 i++; 122 } 123 124 return NULL; 125 } 126 127 void trace_list_events(void) 128 { 129 int i; 130 for (i = 0; i < trace_event_count(); i++) { 131 TraceEvent *res = trace_event_id(i); 132 fprintf(stderr, "%s\n", trace_event_get_name(res)); 133 } 134 } 135 136 static void do_trace_enable_events(const char *line_buf) 137 { 138 const bool enable = ('-' != line_buf[0]); 139 const char *line_ptr = enable ? line_buf : line_buf + 1; 140 141 if (trace_event_is_pattern(line_ptr)) { 142 TraceEvent *ev = NULL; 143 while ((ev = trace_event_pattern(line_ptr, ev)) != NULL) { 144 if (trace_event_get_state_static(ev)) { 145 /* start tracing */ 146 trace_event_set_state_dynamic(ev, enable); 147 /* mark for late vCPU init */ 148 trace_events_dstate_init[ev->id] = true; 149 } 150 } 151 } else { 152 TraceEvent *ev = trace_event_name(line_ptr); 153 if (ev == NULL) { 154 error_report("WARNING: trace event '%s' does not exist", 155 line_ptr); 156 } else if (!trace_event_get_state_static(ev)) { 157 error_report("WARNING: trace event '%s' is not traceable", 158 line_ptr); 159 } else { 160 /* start tracing */ 161 trace_event_set_state_dynamic(ev, enable); 162 /* mark for late vCPU init */ 163 trace_events_dstate_init[ev->id] = true; 164 } 165 } 166 } 167 168 void trace_enable_events(const char *line_buf) 169 { 170 if (is_help_option(line_buf)) { 171 trace_list_events(); 172 if (cur_mon == NULL) { 173 exit(0); 174 } 175 } else { 176 do_trace_enable_events(line_buf); 177 } 178 } 179 180 static void trace_init_events(const char *fname) 181 { 182 Location loc; 183 FILE *fp; 184 char line_buf[1024]; 185 size_t line_idx = 0; 186 187 if (fname == NULL) { 188 return; 189 } 190 191 loc_push_none(&loc); 192 loc_set_file(fname, 0); 193 fp = fopen(fname, "r"); 194 if (!fp) { 195 error_report("%s", strerror(errno)); 196 exit(1); 197 } 198 while (fgets(line_buf, sizeof(line_buf), fp)) { 199 loc_set_file(fname, ++line_idx); 200 size_t len = strlen(line_buf); 201 if (len > 1) { /* skip empty lines */ 202 line_buf[len - 1] = '\0'; 203 if ('#' == line_buf[0]) { /* skip commented lines */ 204 continue; 205 } 206 trace_enable_events(line_buf); 207 } 208 } 209 if (fclose(fp) != 0) { 210 loc_set_file(fname, 0); 211 error_report("%s", strerror(errno)); 212 exit(1); 213 } 214 loc_pop(&loc); 215 } 216 217 void trace_init_file(const char *file) 218 { 219 #ifdef CONFIG_TRACE_SIMPLE 220 st_set_trace_file(file); 221 #elif defined CONFIG_TRACE_LOG 222 /* If both the simple and the log backends are enabled, "-trace file" 223 * only applies to the simple backend; use "-D" for the log backend. 224 */ 225 if (file) { 226 qemu_set_log_filename(file, &error_fatal); 227 } 228 #else 229 if (file) { 230 fprintf(stderr, "error: -trace file=...: " 231 "option not supported by the selected tracing backends\n"); 232 exit(1); 233 } 234 #endif 235 } 236 237 bool trace_init_backends(void) 238 { 239 #ifdef CONFIG_TRACE_SIMPLE 240 if (!st_init()) { 241 fprintf(stderr, "failed to initialize simple tracing backend.\n"); 242 return false; 243 } 244 #endif 245 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 253 return true; 254 } 255 256 char *trace_opt_parse(const char *optarg) 257 { 258 char *trace_file; 259 QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("trace"), 260 optarg, true); 261 if (!opts) { 262 exit(1); 263 } 264 if (qemu_opt_get(opts, "enable")) { 265 trace_enable_events(qemu_opt_get(opts, "enable")); 266 } 267 trace_init_events(qemu_opt_get(opts, "events")); 268 trace_file = g_strdup(qemu_opt_get(opts, "file")); 269 qemu_opts_del(opts); 270 271 return trace_file; 272 } 273 274 void trace_init_vcpu_events(void) 275 { 276 TraceEvent *ev = NULL; 277 while ((ev = trace_event_pattern("*", ev)) != NULL) { 278 if (trace_event_is_vcpu(ev) && 279 trace_event_get_state_static(ev) && 280 trace_events_dstate_init[ev->id]) { 281 trace_event_set_state_dynamic(ev, true); 282 } 283 } 284 } 285