blob: 1aa3686032688f95609274918aab97bd7850e14b [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002
Jiri Olsa97978b32013-12-03 14:09:24 +01003#include <stdio.h>
4#include <unistd.h>
5#include <stdlib.h>
6#include <errno.h>
7#include <sys/types.h>
8#include <sys/stat.h>
9#include <fcntl.h>
10#include <linux/kernel.h>
Jiri Olsa8dd2a132015-09-07 10:38:06 +020011#include <linux/err.h>
Jiri Olsa29f5ffd2013-12-03 14:09:23 +010012#include <traceevent/event-parse.h>
Jiri Olsa592d5a62015-09-02 09:56:34 +020013#include <api/fs/tracing_path.h>
Jiri Olsa607bfbd2016-02-14 17:03:43 +010014#include <api/fs/fs.h>
Jiri Olsa29f5ffd2013-12-03 14:09:23 +010015#include "trace-event.h"
Arnaldo Carvalho de Meloc3168b02015-07-22 16:14:29 -030016#include "machine.h"
Jiri Olsa97978b32013-12-03 14:09:24 +010017#include "util.h"
18
19/*
20 * global trace_event object used by trace_event__tp_format
21 *
22 * TODO There's no cleanup call for this. Add some sort of
23 * __exit function support and call trace_event__cleanup
24 * there.
25 */
26static struct trace_event tevent;
Arnaldo Carvalho de Meloc3168b02015-07-22 16:14:29 -030027static bool tevent_initialized;
Jiri Olsa29f5ffd2013-12-03 14:09:23 +010028
29int trace_event__init(struct trace_event *t)
30{
31 struct pevent *pevent = pevent_alloc();
32
33 if (pevent) {
34 t->plugin_list = traceevent_load_plugins(pevent);
35 t->pevent = pevent;
36 }
37
38 return pevent ? 0 : -1;
39}
40
Arnaldo Carvalho de Meloc3168b02015-07-22 16:14:29 -030041static int trace_event__init2(void)
42{
43 int be = traceevent_host_bigendian();
44 struct pevent *pevent;
45
46 if (trace_event__init(&tevent))
47 return -1;
48
49 pevent = tevent.pevent;
50 pevent_set_flag(pevent, PEVENT_NSEC_OUTPUT);
51 pevent_set_file_bigendian(pevent, be);
52 pevent_set_host_bigendian(pevent, be);
53 tevent_initialized = true;
54 return 0;
55}
56
Arnaldo Carvalho de Melo959c2192015-07-24 12:13:05 -030057int trace_event__register_resolver(struct machine *machine,
58 pevent_func_resolver_t *func)
Arnaldo Carvalho de Meloc3168b02015-07-22 16:14:29 -030059{
60 if (!tevent_initialized && trace_event__init2())
61 return -1;
62
Arnaldo Carvalho de Melo959c2192015-07-24 12:13:05 -030063 return pevent_set_function_resolver(tevent.pevent, func, machine);
Arnaldo Carvalho de Meloc3168b02015-07-22 16:14:29 -030064}
65
Jiri Olsa29f5ffd2013-12-03 14:09:23 +010066void trace_event__cleanup(struct trace_event *t)
67{
Namhyung Kim8d0c2222014-01-15 10:45:28 +090068 traceevent_unload_plugins(t->plugin_list, t->pevent);
Jiri Olsa29f5ffd2013-12-03 14:09:23 +010069 pevent_free(t->pevent);
Jiri Olsa29f5ffd2013-12-03 14:09:23 +010070}
Jiri Olsa97978b32013-12-03 14:09:24 +010071
Jiri Olsa8dd2a132015-09-07 10:38:06 +020072/*
73 * Returns pointer with encoded error via <linux/err.h> interface.
74 */
Jiri Olsa97978b32013-12-03 14:09:24 +010075static struct event_format*
76tp_format(const char *sys, const char *name)
77{
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -030078 char *tp_dir = get_events_file(sys);
Jiri Olsa97978b32013-12-03 14:09:24 +010079 struct pevent *pevent = tevent.pevent;
80 struct event_format *event = NULL;
81 char path[PATH_MAX];
82 size_t size;
83 char *data;
Jiri Olsa8dd2a132015-09-07 10:38:06 +020084 int err;
Jiri Olsa97978b32013-12-03 14:09:24 +010085
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -030086 if (!tp_dir)
87 return ERR_PTR(-errno);
88
89 scnprintf(path, PATH_MAX, "%s/%s/format", tp_dir, name);
90 put_events_file(tp_dir);
Jiri Olsa97978b32013-12-03 14:09:24 +010091
Jiri Olsa8dd2a132015-09-07 10:38:06 +020092 err = filename__read_str(path, &data, &size);
93 if (err)
94 return ERR_PTR(err);
Jiri Olsa97978b32013-12-03 14:09:24 +010095
96 pevent_parse_format(pevent, &event, data, size, sys);
97
98 free(data);
99 return event;
100}
101
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200102/*
103 * Returns pointer with encoded error via <linux/err.h> interface.
104 */
Jiri Olsa97978b32013-12-03 14:09:24 +0100105struct event_format*
106trace_event__tp_format(const char *sys, const char *name)
107{
Arnaldo Carvalho de Meloc3168b02015-07-22 16:14:29 -0300108 if (!tevent_initialized && trace_event__init2())
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200109 return ERR_PTR(-ENOMEM);
Jiri Olsa97978b32013-12-03 14:09:24 +0100110
111 return tp_format(sys, name);
112}
Jiri Olsa71fe1052016-07-10 13:07:54 +0200113
114struct event_format *trace_event__tp_format_id(int id)
115{
116 if (!tevent_initialized && trace_event__init2())
117 return ERR_PTR(-ENOMEM);
118
119 return pevent_find_event(tevent.pevent, id);
120}