| 1 | |
| 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>
|
| 10 | #include <linux/err.h>
|
| 11 | #include <event-parse.h>
|
| 12 | #include <api/fs/tracing_path.h>
|
| 13 | #include <api/fs/fs.h>
|
| 14 | #include "trace-event.h"
|
| 15 | #include "machine.h"
|
| 16 |
|
| 17 | |
| 18 | * global trace_event object used by trace_event__tp_format
|
| 19 | *
|
| 20 | * TODO There's no cleanup call for this. Add some sort of
|
| 21 | * __exit function support and call trace_event__cleanup
|
| 22 | * there.
|
| 23 |
|
| 24 | static struct trace_event tevent;
|
| 25 | static bool tevent_initialized;
|
| 26 |
|
| 27 | int trace_event__init(struct trace_event *t)
|
| 28 | {
|
| 29 | struct tep_handle *pevent;
|
| 30 |
|
| 31 | trace_event__cleanup(t);
|
| 32 |
|
| 33 | pevent = tep_alloc();
|
| 34 |
|
| 35 | if (pevent) {
|
| 36 | t->plugin_list = tep_load_plugins(pevent);
|
| 37 | t->pevent = pevent;
|
| 38 | }
|
| 39 |
|
| 40 | return pevent ? 0 : -1;
|
| 41 | }
|
| 42 |
|
| 43 | static int trace_event__init2(void)
|
| 44 | {
|
| 45 | int be = tep_is_bigendian();
|
| 46 | struct tep_handle *pevent;
|
| 47 |
|
| 48 | if (trace_event__init(&tevent))
|
| 49 | return -1;
|
| 50 |
|
| 51 | pevent = tevent.pevent;
|
| 52 | tep_set_flag(pevent, TEP_NSEC_OUTPUT);
|
| 53 | tep_set_file_bigendian(pevent, be);
|
| 54 | tep_set_local_bigendian(pevent, be);
|
| 55 | tevent_initialized = true;
|
| 56 | return 0;
|
| 57 | }
|
| 58 |
|
| 59 | int trace_event__register_resolver(struct machine *machine,
|
| 60 | tep_func_resolver_t *func)
|
| 61 | {
|
| 62 | if (!tevent_initialized && trace_event__init2())
|
| 63 | return -1;
|
| 64 |
|
| 65 | return tep_set_function_resolver(tevent.pevent, func, machine);
|
| 66 | }
|
| 67 |
|
| 68 | void trace_event__cleanup(struct trace_event *t)
|
| 69 | {
|
| 70 | if (!t->pevent)
|
| 71 | return;
|
| 72 |
|
| 73 | tep_unload_plugins(t->plugin_list, t->pevent);
|
| 74 | tep_free(t->pevent);
|
| 75 | t->pevent = NULL;
|
| 76 | t->plugin_list = NULL;
|
| 77 | }
|
| 78 |
|
| 79 | |
| 80 | * Returns pointer with encoded error via <linux/err.h> interface.
|
| 81 |
|
| 82 | static struct tep_event*
|
| 83 | tp_format(const char *sys, const char *name)
|
| 84 | {
|
| 85 | char *tp_dir = get_events_file(sys);
|
| 86 | struct tep_handle *pevent = tevent.pevent;
|
| 87 | struct tep_event *event = NULL;
|
| 88 | char path[PATH_MAX];
|
| 89 | size_t size;
|
| 90 | char *data;
|
| 91 | int err;
|
| 92 |
|
| 93 | if (!tp_dir)
|
| 94 | return ERR_PTR(-errno);
|
| 95 |
|
| 96 | scnprintf(path, PATH_MAX, "%s/%s/format", tp_dir, name);
|
| 97 | put_events_file(tp_dir);
|
| 98 |
|
| 99 | err = filename__read_str(path, &data, &size);
|
| 100 | if (err)
|
| 101 | return ERR_PTR(err);
|
| 102 |
|
| 103 | tep_parse_format(pevent, &event, data, size, sys);
|
| 104 |
|
| 105 | free(data);
|
| 106 | return event;
|
| 107 | }
|
| 108 |
|
| 109 | |
| 110 | * Returns pointer with encoded error via <linux/err.h> interface.
|
| 111 |
|
| 112 | struct tep_event*
|
| 113 | trace_event__tp_format(const char *sys, const char *name)
|
| 114 | {
|
| 115 | if (!tevent_initialized && trace_event__init2())
|
| 116 | return ERR_PTR(-ENOMEM);
|
| 117 |
|
| 118 | return tp_format(sys, name);
|
| 119 | }
|
| 120 |
|
| 121 | struct tep_event *trace_event__tp_format_id(int id)
|
| 122 | {
|
| 123 | if (!tevent_initialized && trace_event__init2())
|
| 124 | return ERR_PTR(-ENOMEM);
|
| 125 |
|
| 126 | return tep_find_event(tevent.pevent, id);
|
| 127 | }
|