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