1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * trace-event-scripting. Scripting engine common and initialization code. 4 * 5 * Copyright (C) 2009-2010 Tom Zanussi <tzanussi@gmail.com> 6 */ 7 8 #include <stdio.h> 9 #include <stdlib.h> 10 #include <string.h> 11 #include <errno.h> 12 #ifdef HAVE_LIBTRACEEVENT 13 #include <event-parse.h> 14 #endif 15 16 #include "debug.h" 17 #include "event.h" 18 #include "trace-event.h" 19 #include "evsel.h" 20 #include <linux/perf_event.h> 21 #include <linux/zalloc.h> 22 #include "util/sample.h" 23 24 unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH; 25 26 struct scripting_context *scripting_context; 27 28 struct script_spec { 29 struct list_head node; 30 struct scripting_ops *ops; 31 char spec[]; 32 }; 33 34 static LIST_HEAD(script_specs); 35 36 static struct script_spec *script_spec__new(const char *spec, 37 struct scripting_ops *ops) 38 { 39 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1); 40 41 if (s != NULL) { 42 strcpy(s->spec, spec); 43 s->ops = ops; 44 } 45 46 return s; 47 } 48 49 static void script_spec__add(struct script_spec *s) 50 { 51 list_add_tail(&s->node, &script_specs); 52 } 53 54 static struct script_spec *script_spec__find(const char *spec) 55 { 56 struct script_spec *s; 57 58 list_for_each_entry(s, &script_specs, node) 59 if (strcasecmp(s->spec, spec) == 0) 60 return s; 61 return NULL; 62 } 63 64 static int script_spec_register(const char *spec, struct scripting_ops *ops) 65 { 66 struct script_spec *s; 67 68 s = script_spec__find(spec); 69 if (s) 70 return -1; 71 72 s = script_spec__new(spec, ops); 73 if (!s) 74 return -1; 75 76 script_spec__add(s); 77 return 0; 78 } 79 80 struct scripting_ops *script_spec__lookup(const char *spec) 81 { 82 struct script_spec *s = script_spec__find(spec); 83 84 if (!s) 85 return NULL; 86 87 return s->ops; 88 } 89 90 int script_spec__for_each(int (*cb)(struct scripting_ops *ops, const char *spec)) 91 { 92 struct script_spec *s; 93 int ret = 0; 94 95 list_for_each_entry(s, &script_specs, node) { 96 ret = cb(s->ops, s->spec); 97 if (ret) 98 break; 99 } 100 return ret; 101 } 102 103 void scripting_context__update(struct scripting_context *c, 104 union perf_event *event, 105 struct perf_sample *sample, 106 struct evsel *evsel, 107 struct addr_location *al, 108 struct addr_location *addr_al) 109 { 110 #ifdef HAVE_LIBTRACEEVENT 111 const struct tep_event *tp_format = evsel__tp_format(evsel); 112 113 c->pevent = tp_format ? tp_format->tep : NULL; 114 #else 115 c->pevent = NULL; 116 #endif 117 c->event_data = sample->raw_data; 118 c->event = event; 119 c->sample = sample; 120 c->evsel = evsel; 121 c->al = al; 122 c->addr_al = addr_al; 123 } 124 125 static int flush_script_unsupported(void) 126 { 127 return 0; 128 } 129 130 static int stop_script_unsupported(void) 131 { 132 return 0; 133 } 134 135 static void process_event_unsupported(union perf_event *event __maybe_unused, 136 struct perf_sample *sample __maybe_unused, 137 struct evsel *evsel __maybe_unused, 138 struct addr_location *al __maybe_unused, 139 struct addr_location *addr_al __maybe_unused) 140 { 141 } 142 143 static void print_python_unsupported_msg(void) 144 { 145 fprintf(stderr, "Python scripting not supported." 146 " Install libpython and rebuild perf to enable it.\n" 147 "For example:\n # apt-get install python-dev (ubuntu)" 148 "\n # yum install python-devel (Fedora)" 149 "\n etc.\n"); 150 } 151 152 static int python_start_script_unsupported(const char *script __maybe_unused, 153 int argc __maybe_unused, 154 const char **argv __maybe_unused, 155 struct perf_session *session __maybe_unused) 156 { 157 print_python_unsupported_msg(); 158 159 return -1; 160 } 161 162 static int python_generate_script_unsupported(struct tep_handle *pevent 163 __maybe_unused, 164 const char *outfile 165 __maybe_unused) 166 { 167 print_python_unsupported_msg(); 168 169 return -1; 170 } 171 172 struct scripting_ops python_scripting_unsupported_ops = { 173 .name = "Python", 174 .dirname = "python", 175 .start_script = python_start_script_unsupported, 176 .flush_script = flush_script_unsupported, 177 .stop_script = stop_script_unsupported, 178 .process_event = process_event_unsupported, 179 .generate_script = python_generate_script_unsupported, 180 }; 181 182 static void register_python_scripting(struct scripting_ops *scripting_ops) 183 { 184 if (scripting_context == NULL) 185 scripting_context = malloc(sizeof(*scripting_context)); 186 187 if (scripting_context == NULL || 188 script_spec_register("Python", scripting_ops) || 189 script_spec_register("py", scripting_ops)) { 190 pr_err("Error registering Python script extension: disabling it\n"); 191 zfree(&scripting_context); 192 } 193 } 194 195 #ifndef HAVE_LIBPYTHON_SUPPORT 196 void setup_python_scripting(void) 197 { 198 register_python_scripting(&python_scripting_unsupported_ops); 199 } 200 #else 201 extern struct scripting_ops python_scripting_ops; 202 203 void setup_python_scripting(void) 204 { 205 register_python_scripting(&python_scripting_ops); 206 } 207 #endif 208 209 #ifdef HAVE_LIBTRACEEVENT 210 static void print_perl_unsupported_msg(void) 211 { 212 fprintf(stderr, "Perl scripting not supported." 213 " Install libperl and rebuild perf to enable it.\n" 214 "For example:\n # apt-get install libperl-dev (ubuntu)" 215 "\n # yum install 'perl(ExtUtils::Embed)' (Fedora)" 216 "\n etc.\n"); 217 } 218 219 static int perl_start_script_unsupported(const char *script __maybe_unused, 220 int argc __maybe_unused, 221 const char **argv __maybe_unused, 222 struct perf_session *session __maybe_unused) 223 { 224 print_perl_unsupported_msg(); 225 226 return -1; 227 } 228 229 static int perl_generate_script_unsupported(struct tep_handle *pevent 230 __maybe_unused, 231 const char *outfile __maybe_unused) 232 { 233 print_perl_unsupported_msg(); 234 235 return -1; 236 } 237 238 struct scripting_ops perl_scripting_unsupported_ops = { 239 .name = "Perl", 240 .dirname = "perl", 241 .start_script = perl_start_script_unsupported, 242 .flush_script = flush_script_unsupported, 243 .stop_script = stop_script_unsupported, 244 .process_event = process_event_unsupported, 245 .generate_script = perl_generate_script_unsupported, 246 }; 247 248 static void register_perl_scripting(struct scripting_ops *scripting_ops) 249 { 250 if (scripting_context == NULL) 251 scripting_context = malloc(sizeof(*scripting_context)); 252 253 if (scripting_context == NULL || 254 script_spec_register("Perl", scripting_ops) || 255 script_spec_register("pl", scripting_ops)) { 256 pr_err("Error registering Perl script extension: disabling it\n"); 257 zfree(&scripting_context); 258 } 259 } 260 261 #ifndef HAVE_LIBPERL_SUPPORT 262 void setup_perl_scripting(void) 263 { 264 register_perl_scripting(&perl_scripting_unsupported_ops); 265 } 266 #else 267 extern struct scripting_ops perl_scripting_ops; 268 269 void setup_perl_scripting(void) 270 { 271 register_perl_scripting(&perl_scripting_ops); 272 } 273 #endif 274 #endif 275 276 static const struct { 277 u32 flags; 278 const char *name; 279 } sample_flags[] = { 280 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"}, 281 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"}, 282 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"}, 283 {PERF_IP_FLAG_BRANCH, "jmp"}, 284 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"}, 285 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"}, 286 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"}, 287 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"}, 288 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"}, 289 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, 290 "hw int"}, 291 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"}, 292 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"}, 293 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"}, 294 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_VMENTRY, "vmentry"}, 295 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_VMEXIT, "vmexit"}, 296 {0, NULL} 297 }; 298 299 static const struct { 300 u32 flags; 301 const char *name; 302 } branch_events[] = { 303 {PERF_IP_FLAG_BRANCH_MISS, "miss"}, 304 {PERF_IP_FLAG_NOT_TAKEN, "not_taken"}, 305 {0, NULL} 306 }; 307 308 static int sample_flags_to_name(u32 flags, char *str, size_t size) 309 { 310 int i; 311 const char *prefix; 312 int pos = 0, ret, ev_idx = 0; 313 u32 xf = flags & PERF_ADDITIONAL_STATE_MASK; 314 u32 types, events; 315 char xs[16] = { 0 }; 316 317 /* Clear additional state bits */ 318 flags &= ~PERF_ADDITIONAL_STATE_MASK; 319 320 if (flags & PERF_IP_FLAG_TRACE_BEGIN) 321 prefix = "tr strt "; 322 else if (flags & PERF_IP_FLAG_TRACE_END) 323 prefix = "tr end "; 324 else 325 prefix = ""; 326 327 ret = snprintf(str + pos, size - pos, "%s", prefix); 328 if (ret < 0) 329 return ret; 330 pos += ret; 331 332 flags &= ~(PERF_IP_FLAG_TRACE_BEGIN | PERF_IP_FLAG_TRACE_END); 333 334 types = flags & ~PERF_IP_FLAG_BRANCH_EVENT_MASK; 335 for (i = 0; sample_flags[i].name; i++) { 336 if (sample_flags[i].flags != types) 337 continue; 338 339 ret = snprintf(str + pos, size - pos, "%s", sample_flags[i].name); 340 if (ret < 0) 341 return ret; 342 pos += ret; 343 break; 344 } 345 346 events = flags & PERF_IP_FLAG_BRANCH_EVENT_MASK; 347 for (i = 0; branch_events[i].name; i++) { 348 if (!(branch_events[i].flags & events)) 349 continue; 350 351 ret = snprintf(str + pos, size - pos, !ev_idx ? "/%s" : ",%s", 352 branch_events[i].name); 353 if (ret < 0) 354 return ret; 355 pos += ret; 356 ev_idx++; 357 } 358 359 /* Add an end character '/' for events */ 360 if (ev_idx) { 361 ret = snprintf(str + pos, size - pos, "/"); 362 if (ret < 0) 363 return ret; 364 pos += ret; 365 } 366 367 if (!xf) 368 return pos; 369 370 snprintf(xs, sizeof(xs), "(%s%s%s)", 371 flags & PERF_IP_FLAG_IN_TX ? "x" : "", 372 flags & PERF_IP_FLAG_INTR_DISABLE ? "D" : "", 373 flags & PERF_IP_FLAG_INTR_TOGGLE ? "t" : ""); 374 375 /* Right align the string if its length is less than the limit */ 376 if ((pos + strlen(xs)) < SAMPLE_FLAGS_STR_ALIGNED_SIZE) 377 ret = snprintf(str + pos, size - pos, "%*s", 378 (int)(SAMPLE_FLAGS_STR_ALIGNED_SIZE - ret), xs); 379 else 380 ret = snprintf(str + pos, size - pos, " %s", xs); 381 if (ret < 0) 382 return ret; 383 384 return pos + ret; 385 } 386 387 int perf_sample__sprintf_flags(u32 flags, char *str, size_t sz) 388 { 389 const char *chars = PERF_IP_FLAG_CHARS; 390 const size_t n = strlen(PERF_IP_FLAG_CHARS); 391 size_t i, pos = 0; 392 int ret; 393 394 ret = sample_flags_to_name(flags, str, sz); 395 if (ret > 0) 396 return ret; 397 398 for (i = 0; i < n; i++, flags >>= 1) { 399 if ((flags & 1) && pos < sz) 400 str[pos++] = chars[i]; 401 } 402 for (; i < 32; i++, flags >>= 1) { 403 if ((flags & 1) && pos < sz) 404 str[pos++] = '?'; 405 } 406 if (pos < sz) 407 str[pos] = 0; 408 409 return pos; 410 } 411