blob: 667bd109d16fcae8d2d7d00d3a769cd442d32b47 [file] [log] [blame]
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01001
Jiri Olsa97978b32013-12-03 14:09:24 +01002#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 Olsa29f5ffd2013-12-03 14:09:23 +010010#include <traceevent/event-parse.h>
11#include "trace-event.h"
Arnaldo Carvalho de Meloc3168b02015-07-22 16:14:29 -030012#include "machine.h"
Jiri Olsa97978b32013-12-03 14:09:24 +010013#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 */
22static struct trace_event tevent;
Arnaldo Carvalho de Meloc3168b02015-07-22 16:14:29 -030023static bool tevent_initialized;
Jiri Olsa29f5ffd2013-12-03 14:09:23 +010024
25int 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 Meloc3168b02015-07-22 16:14:29 -030037static 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
53int trace_event__register_resolver(struct machine *machine)
54{
55 if (!tevent_initialized && trace_event__init2())
56 return -1;
57
58 return pevent_set_function_resolver(tevent.pevent,
59 machine__resolve_kernel_addr,
60 machine);
61}
62
Jiri Olsa29f5ffd2013-12-03 14:09:23 +010063void trace_event__cleanup(struct trace_event *t)
64{
Namhyung Kim8d0c2222014-01-15 10:45:28 +090065 traceevent_unload_plugins(t->plugin_list, t->pevent);
Jiri Olsa29f5ffd2013-12-03 14:09:23 +010066 pevent_free(t->pevent);
Jiri Olsa29f5ffd2013-12-03 14:09:23 +010067}
Jiri Olsa97978b32013-12-03 14:09:24 +010068
69static struct event_format*
70tp_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
90struct event_format*
91trace_event__tp_format(const char *sys, const char *name)
92{
Arnaldo Carvalho de Meloc3168b02015-07-22 16:14:29 -030093 if (!tevent_initialized && trace_event__init2())
94 return NULL;
Jiri Olsa97978b32013-12-03 14:09:24 +010095
96 return tp_format(sys, name);
97}