1 /* SPDX-License-Identifier: LGPL-2.1 */
2 /*
3 * Copyright (C) 2019, VMware, Tzvetomir Stoyanov <tz.stoyanov@gmail.com>
4 *
5 */
6 #ifndef _TRACE_FS_LOCAL_H
7 #define _TRACE_FS_LOCAL_H
8
9 #include <tracefs.h>
10 #include <pthread.h>
11
12 #define __hidden __attribute__((visibility ("hidden")))
13 #define __weak __attribute__((weak))
14
15 #define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
16
17 /* Will cause a division by zero warning if cond is true */
18 #define BUILD_BUG_ON(cond) \
19 do { if (!(1/!(cond))) { } } while (0)
20
21 #define HASH_BITS 10
22
23 struct tracefs_options_mask {
24 unsigned long long mask;
25 };
26
27 struct follow_event {
28 struct tep_event *event;
29 void *callback_data;
30 int (*callback)(struct tep_event *,
31 struct tep_record *,
32 int, void *);
33 };
34
35 struct tracefs_instance {
36 struct tracefs_options_mask supported_opts;
37 struct tracefs_options_mask enabled_opts;
38 struct follow_event *followers;
39 struct follow_event *missed_followers;
40 char *trace_dir;
41 char *name;
42 pthread_mutex_t lock;
43 int ref;
44 int flags;
45 int ftrace_filter_fd;
46 int ftrace_notrace_fd;
47 int ftrace_marker_fd;
48 int ftrace_marker_raw_fd;
49 int nr_followers;
50 int nr_missed_followers;
51 bool pipe_keep_going;
52 bool iterate_keep_going;
53 };
54
55 struct tracefs_buffer_stat {
56 ssize_t entries;
57 ssize_t overrun;
58 ssize_t commit_overrun;
59 ssize_t bytes;
60 long long oldest_ts;
61 long long now_ts;
62 ssize_t dropped_events;
63 ssize_t read_events;
64 };
65
66 extern const struct tep_format_field common_stacktrace;
67
68 extern pthread_mutex_t toplevel_lock;
69
trace_get_lock(struct tracefs_instance * instance)70 static inline pthread_mutex_t *trace_get_lock(struct tracefs_instance *instance)
71 {
72 return instance ? &instance->lock : &toplevel_lock;
73 }
74
75 void trace_put_instance(struct tracefs_instance *instance);
76 int trace_get_instance(struct tracefs_instance *instance);
77
78 /* Can be overridden */
79 void tracefs_warning(const char *fmt, ...);
80
81 char *strstrip(char *str);
82 int str_read_file(const char *file, char **buffer, bool warn);
83 char *trace_append_file(const char *dir, const char *name);
84 char *trace_find_tracing_dir(bool debugfs);
85
86 #ifndef ACCESSPERMS
87 #define ACCESSPERMS (S_IRWXU|S_IRWXG|S_IRWXO) /* 0777 */
88 #endif
89
90 #ifndef ALLPERMS
91 #define ALLPERMS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO) /* 07777 */
92 #endif
93
94 #ifndef DEFFILEMODE
95 #define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) /* 0666*/
96 #endif
97
98 struct tracefs_options_mask *
99 supported_opts_mask(struct tracefs_instance *instance);
100
101 struct tracefs_options_mask *
102 enabled_opts_mask(struct tracefs_instance *instance);
103
104 char **trace_list_create_empty(void);
105 int trace_list_pop(char **list);
106
107 char *append_string(char *str, const char *delim, const char *add);
108 int trace_test_state(int state);
109 bool trace_verify_event_field(struct tep_event *event,
110 const char *field_name,
111 const struct tep_format_field **ptr_field);
112 int trace_append_filter(char **filter, unsigned int *state,
113 unsigned int *open_parens,
114 struct tep_event *event,
115 enum tracefs_filter type,
116 const char *field_name,
117 enum tracefs_compare compare,
118 const char *val);
119
120 void *trace_mmap(int fd, struct kbuffer *kbuf);
121 void trace_unmap(void *mapping);
122 int trace_mmap_load_subbuf(void *mapping, struct kbuffer *kbuf);
123 int trace_mmap_read(void *mapping, void *buffer);
124
125 struct tracefs_synth *synth_init_from(struct tep_handle *tep,
126 const char *start_system,
127 const char *start_event);
128
129 #define HIST_COUNTER_TYPE (TRACEFS_HIST_KEY_MAX + 100)
130 int synth_add_start_field(struct tracefs_synth *synth,
131 const char *start_field,
132 const char *name,
133 enum tracefs_hist_key_type type, int cnt);
134
135 /* Internal interface for ftrace dynamic events */
136
137 struct tracefs_dynevent {
138 char *trace_file;
139 char *prefix;
140 char *system;
141 char *event;
142 char *address;
143 char *format;
144 enum tracefs_dynevent_type type;
145 };
146
147 struct tracefs_dynevent *
148 dynevent_alloc(enum tracefs_dynevent_type type, const char *system,
149 const char *event, const char *address, const char *format);
150 int dynevent_get_count(unsigned int types, const char *system);
151
152 int trace_load_events(struct tep_handle *tep,
153 const char *tracing_dir, const char *system);
154 int trace_rescan_events(struct tep_handle *tep,
155 const char *tracing_dir, const char *system);
156 struct tep_event *get_tep_event(struct tep_handle *tep,
157 const char *system, const char *name);
158
159 unsigned int quick_hash(const char *str);
160
161 #endif /* _TRACE_FS_LOCAL_H */
162