Jiri Olsa | 29f5ffd | 2013-12-03 14:09:23 +0100 | [diff] [blame] | 1 | |
Jiri Olsa | 97978b3 | 2013-12-03 14:09:24 +0100 | [diff] [blame] | 2 | #include <stdio.h> |
| 3 | #include <unistd.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <errno.h> |
| 6 | #include <sys/types.h> |
| 7 | #include <sys/stat.h> |
| 8 | #include <fcntl.h> |
| 9 | #include <linux/kernel.h> |
Jiri Olsa | 29f5ffd | 2013-12-03 14:09:23 +0100 | [diff] [blame] | 10 | #include <traceevent/event-parse.h> |
Jiri Olsa | 592d5a6 | 2015-09-02 09:56:34 +0200 | [diff] [blame^] | 11 | #include <api/fs/tracing_path.h> |
Jiri Olsa | 29f5ffd | 2013-12-03 14:09:23 +0100 | [diff] [blame] | 12 | #include "trace-event.h" |
Arnaldo Carvalho de Melo | c3168b0 | 2015-07-22 16:14:29 -0300 | [diff] [blame] | 13 | #include "machine.h" |
Jiri Olsa | 97978b3 | 2013-12-03 14:09:24 +0100 | [diff] [blame] | 14 | #include "util.h" |
| 15 | |
| 16 | /* |
| 17 | * global trace_event object used by trace_event__tp_format |
| 18 | * |
| 19 | * TODO There's no cleanup call for this. Add some sort of |
| 20 | * __exit function support and call trace_event__cleanup |
| 21 | * there. |
| 22 | */ |
| 23 | static struct trace_event tevent; |
Arnaldo Carvalho de Melo | c3168b0 | 2015-07-22 16:14:29 -0300 | [diff] [blame] | 24 | static bool tevent_initialized; |
Jiri Olsa | 29f5ffd | 2013-12-03 14:09:23 +0100 | [diff] [blame] | 25 | |
| 26 | int trace_event__init(struct trace_event *t) |
| 27 | { |
| 28 | struct pevent *pevent = pevent_alloc(); |
| 29 | |
| 30 | if (pevent) { |
| 31 | t->plugin_list = traceevent_load_plugins(pevent); |
| 32 | t->pevent = pevent; |
| 33 | } |
| 34 | |
| 35 | return pevent ? 0 : -1; |
| 36 | } |
| 37 | |
Arnaldo Carvalho de Melo | c3168b0 | 2015-07-22 16:14:29 -0300 | [diff] [blame] | 38 | static int trace_event__init2(void) |
| 39 | { |
| 40 | int be = traceevent_host_bigendian(); |
| 41 | struct pevent *pevent; |
| 42 | |
| 43 | if (trace_event__init(&tevent)) |
| 44 | return -1; |
| 45 | |
| 46 | pevent = tevent.pevent; |
| 47 | pevent_set_flag(pevent, PEVENT_NSEC_OUTPUT); |
| 48 | pevent_set_file_bigendian(pevent, be); |
| 49 | pevent_set_host_bigendian(pevent, be); |
| 50 | tevent_initialized = true; |
| 51 | return 0; |
| 52 | } |
| 53 | |
Arnaldo Carvalho de Melo | 959c219 | 2015-07-24 12:13:05 -0300 | [diff] [blame] | 54 | int trace_event__register_resolver(struct machine *machine, |
| 55 | pevent_func_resolver_t *func) |
Arnaldo Carvalho de Melo | c3168b0 | 2015-07-22 16:14:29 -0300 | [diff] [blame] | 56 | { |
| 57 | if (!tevent_initialized && trace_event__init2()) |
| 58 | return -1; |
| 59 | |
Arnaldo Carvalho de Melo | 959c219 | 2015-07-24 12:13:05 -0300 | [diff] [blame] | 60 | return pevent_set_function_resolver(tevent.pevent, func, machine); |
Arnaldo Carvalho de Melo | c3168b0 | 2015-07-22 16:14:29 -0300 | [diff] [blame] | 61 | } |
| 62 | |
Jiri Olsa | 29f5ffd | 2013-12-03 14:09:23 +0100 | [diff] [blame] | 63 | void trace_event__cleanup(struct trace_event *t) |
| 64 | { |
Namhyung Kim | 8d0c222 | 2014-01-15 10:45:28 +0900 | [diff] [blame] | 65 | traceevent_unload_plugins(t->plugin_list, t->pevent); |
Jiri Olsa | 29f5ffd | 2013-12-03 14:09:23 +0100 | [diff] [blame] | 66 | pevent_free(t->pevent); |
Jiri Olsa | 29f5ffd | 2013-12-03 14:09:23 +0100 | [diff] [blame] | 67 | } |
Jiri Olsa | 97978b3 | 2013-12-03 14:09:24 +0100 | [diff] [blame] | 68 | |
| 69 | static struct event_format* |
| 70 | tp_format(const char *sys, const char *name) |
| 71 | { |
| 72 | struct pevent *pevent = tevent.pevent; |
| 73 | struct event_format *event = NULL; |
| 74 | char path[PATH_MAX]; |
| 75 | size_t size; |
| 76 | char *data; |
| 77 | |
| 78 | scnprintf(path, PATH_MAX, "%s/%s/%s/format", |
| 79 | tracing_events_path, sys, name); |
| 80 | |
| 81 | if (filename__read_str(path, &data, &size)) |
| 82 | return NULL; |
| 83 | |
| 84 | pevent_parse_format(pevent, &event, data, size, sys); |
| 85 | |
| 86 | free(data); |
| 87 | return event; |
| 88 | } |
| 89 | |
| 90 | struct event_format* |
| 91 | trace_event__tp_format(const char *sys, const char *name) |
| 92 | { |
Arnaldo Carvalho de Melo | c3168b0 | 2015-07-22 16:14:29 -0300 | [diff] [blame] | 93 | if (!tevent_initialized && trace_event__init2()) |
| 94 | return NULL; |
Jiri Olsa | 97978b3 | 2013-12-03 14:09:24 +0100 | [diff] [blame] | 95 | |
| 96 | return tp_format(sys, name); |
| 97 | } |