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