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