1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
4 *
5 */
6 #ifndef __TRACE_LOCAL_H
7 #define __TRACE_LOCAL_H
8
9 #include <sys/types.h>
10 #include <dirent.h> /* for DIR */
11 #include <ctype.h> /* for isdigit() */
12 #include <errno.h>
13 #include <limits.h>
14
15 #include "trace-cmd-private.h"
16 #include "event-utils.h"
17
18 #define TRACE_AGENT_DEFAULT_PORT 823
19
20 #define DEFAULT_INPUT_FILE "trace.dat"
21 #define GUEST_PIPE_NAME "trace-pipe-cpu"
22 #define GUEST_DIR_FMT "/var/lib/trace-cmd/virt/%s"
23 #define GUEST_FIFO_FMT GUEST_DIR_FMT "/" GUEST_PIPE_NAME "%d"
24 #define VIRTIO_FIFO_FMT "/dev/virtio-ports/" GUEST_PIPE_NAME "%d"
25
26 /* fix stupid glib guint64 typecasts and printf formats */
27 typedef unsigned long long u64;
28
29 struct buffer_instance;
30
31 #define __printf(a, b) __attribute__((format(printf,a,b)))
32
33 __printf(1,2)
34 void warning(const char *fmt, ...);
35
36 /* for local shared information with trace-cmd executable */
37
38 void usage(char **argv);
39
40 extern int silence_warnings;
41 extern int show_status;
42
43 int trace_set_verbose(char *level);
44
45 enum port_type {
46 USE_UDP = 0, /* Default setting */
47 USE_TCP,
48 USE_VSOCK
49 };
50
51 struct pid_record_data {
52 int pid;
53 int brass[2];
54 int cpu;
55 int closed;
56 struct tracecmd_input *stream;
57 struct buffer_instance *instance;
58 struct tep_record *record;
59 };
60
61 void show_file(const char *name);
62
63 struct tracecmd_input *read_trace_header(const char *file, int flags);
64 int read_trace_files(void);
65
66 void trace_record(int argc, char **argv);
67
68 void trace_stop(int argc, char **argv);
69
70 void trace_restart(int argc, char **argv);
71
72 void trace_reset(int argc, char **argv);
73
74 void trace_start(int argc, char **argv);
75
76 void trace_set(int argc, char **argv);
77
78 void trace_extract(int argc, char **argv);
79
80 void trace_stream(int argc, char **argv);
81
82 void trace_profile(int argc, char **argv);
83
84 void trace_report(int argc, char **argv);
85
86 void trace_split(int argc, char **argv);
87
88 void trace_listen(int argc, char **argv);
89
90 void trace_agent(int argc, char **argv);
91
92 void trace_setup_guest(int argc, char **argv);
93
94 void trace_restore(int argc, char **argv);
95
96 void trace_clear(int argc, char **argv);
97
98 void trace_check_events(int argc, char **argv);
99
100 void trace_stack(int argc, char **argv);
101
102 void trace_option(int argc, char **argv);
103
104 void trace_hist(int argc, char **argv);
105
106 void trace_snapshot(int argc, char **argv);
107
108 void trace_mem(int argc, char **argv);
109
110 void trace_stat(int argc, char **argv);
111
112 void trace_show(int argc, char **argv);
113
114 void trace_list(int argc, char **argv);
115
116 void trace_usage(int argc, char **argv);
117
118 void trace_dump(int argc, char **argv);
119
120 void trace_convert(int argc, char **argv);
121
122 int trace_record_agent(struct tracecmd_msg_handle *msg_handle,
123 int cpus, int *fds,
124 int argc, char **argv, bool use_fifos,
125 unsigned long long trace_id, const char *host);
126
127 struct hook_list;
128
129 void trace_init_profile(struct tracecmd_input *handle, struct hook_list *hooks,
130 int global);
131 int do_trace_profile(void);
132 void trace_profile_set_merge_like_comms(void);
133
134 struct tracecmd_input *
135 trace_stream_init(struct buffer_instance *instance, int cpu, int fd, int cpus,
136 struct hook_list *hooks,
137 tracecmd_handle_init_func handle_init, int global);
138 int trace_stream_read(struct pid_record_data *pids, int nr_pids, struct timeval *tv);
139
140 void trace_show_data(struct tracecmd_input *handle, struct tep_record *record);
141
142 /* --- event interation --- */
143
144 /*
145 * Use this to iterate through the event directories
146 */
147
148
149 enum event_process {
150 PROCESSED_NONE,
151 PROCESSED_EVENT,
152 PROCESSED_SYSTEM
153 };
154
155 enum process_type {
156 PROCESS_EVENT,
157 PROCESS_SYSTEM
158 };
159
160 struct event_iter {
161 DIR *system_dir;
162 DIR *event_dir;
163 struct dirent *system_dent;
164 struct dirent *event_dent;
165 };
166
167 enum event_iter_type {
168 EVENT_ITER_NONE,
169 EVENT_ITER_SYSTEM,
170 EVENT_ITER_EVENT
171 };
172
173 struct event_iter *trace_event_iter_alloc(const char *path);
174 enum event_iter_type trace_event_iter_next(struct event_iter *iter,
175 const char *path, const char *system);
176 void trace_event_iter_free(struct event_iter *iter);
177
178 char *append_file(const char *dir, const char *name);
179 char *get_file_content(const char *file);
180
181 char *strstrip(char *str);
182
183 /* --- instance manipulation --- */
184
185 enum buffer_instance_flags {
186 BUFFER_FL_KEEP = 1 << 0,
187 BUFFER_FL_PROFILE = 1 << 1,
188 BUFFER_FL_GUEST = 1 << 2,
189 BUFFER_FL_AGENT = 1 << 3,
190 BUFFER_FL_HAS_CLOCK = 1 << 4,
191 BUFFER_FL_TSC2NSEC = 1 << 5,
192 BUFFER_FL_NETWORK = 1 << 6,
193 };
194
195 struct func_list {
196 struct func_list *next;
197 const char *func;
198 const char *mod;
199 };
200
201 struct pid_addr_maps {
202 struct pid_addr_maps *next;
203 struct tracecmd_proc_addr_map *lib_maps;
204 unsigned int nr_lib_maps;
205 char *proc_name;
206 int pid;
207 };
208
209 struct opt_list {
210 struct opt_list *next;
211 const char *option;
212 };
213
214 struct filter_pids {
215 struct filter_pids *next;
216 int pid;
217 int exclude;
218 };
219
220 struct tsc_nsec {
221 int mult;
222 int shift;
223 unsigned long long offset;
224 };
225
226 struct buffer_instance {
227 struct buffer_instance *next;
228 char *name;
229 struct tracefs_instance *tracefs;
230 unsigned long long trace_id;
231 char *cpumask;
232 char *output_file;
233 struct event_list *events;
234 struct event_list **event_next;
235 bool delete;
236
237 struct event_list *sched_switch_event;
238 struct event_list *sched_wakeup_event;
239 struct event_list *sched_wakeup_new_event;
240
241 const char *plugin;
242 char *filter_mod;
243 struct func_list *filter_funcs;
244 struct func_list *notrace_funcs;
245
246 struct opt_list *options;
247 struct filter_pids *filter_pids;
248 struct filter_pids *process_pids;
249 char *common_pid_filter;
250 int nr_filter_pids;
251 int len_filter_pids;
252 int nr_process_pids;
253 bool ptrace_child;
254
255 int have_set_event_pid;
256 int have_event_fork;
257 int have_func_fork;
258 int get_procmap;
259
260 const char *clock;
261 unsigned int *client_ports;
262
263 struct trace_seq *s_save;
264 struct trace_seq *s_print;
265
266 struct tracecmd_input *handle;
267
268 struct tracecmd_msg_handle *msg_handle;
269 struct tracecmd_output *network_handle;
270 const char *host;
271
272 struct pid_addr_maps *pid_maps;
273
274 char *max_graph_depth;
275
276 int flags;
277 int tracing_on_init_val;
278 int tracing_on_fd;
279 int buffer_size;
280 int cpu_count;
281
282 int argc;
283 char **argv;
284
285 struct addrinfo *result;
286 unsigned int cid;
287 unsigned int port;
288 int *fds;
289 bool use_fifos;
290
291 enum port_type port_type; /* Default to USE_UDP (zero) */
292 int tsync_loop_interval;
293 struct tracecmd_time_sync *tsync;
294 };
295
296 void init_top_instance(void);
297
298 extern struct buffer_instance top_instance;
299 extern struct buffer_instance *buffer_instances;
300 extern struct buffer_instance *first_instance;
301
302 #define for_each_instance(i) for (i = buffer_instances; i; i = (i)->next)
303 #define for_all_instances(i) for (i = first_instance; i; \
304 i = i == &top_instance ? buffer_instances : (i)->next)
305
306 #define is_agent(instance) ((instance)->flags & BUFFER_FL_AGENT)
307 #define is_guest(instance) ((instance)->flags & BUFFER_FL_GUEST)
308 #define is_network(instance) ((instance)->flags & BUFFER_FL_NETWORK)
309
310 #define START_PORT_SEARCH 1500
311 #define MAX_PORT_SEARCH 6000
312
313 struct sockaddr_storage;
314
315 int trace_net_make(int port, enum port_type type);
316 int trace_net_search(int start_port, int *sfd, enum port_type type);
317 int trace_net_print_connection(int fd);
318 bool trace_net_cmp_connection(struct sockaddr_storage *addr, const char *name);
319 bool trace_net_cmp_connection_fd(int fd, const char *name);
320
321 struct buffer_instance *allocate_instance(const char *name);
322 void add_instance(struct buffer_instance *instance, int cpu_count);
323 void update_first_instance(struct buffer_instance *instance, int topt);
324
325 void show_instance_file(struct buffer_instance *instance, const char *name);
326 void show_options(const char *prefix, struct buffer_instance *buffer);
327
328 struct trace_guest {
329 struct tracefs_instance *instance;
330 char *name;
331 int cid;
332 int pid;
333 int cpu_max;
334 int *cpu_pid;
335 int *task_pids;
336 };
337 struct trace_guest *trace_get_guest(unsigned int cid, const char *name);
338 bool trace_have_guests_pid(void);
339 void read_qemu_guests(void);
340 int get_guest_pid(unsigned int guest_cid);
341 int get_guest_vcpu_pid(unsigned int guest_cid, unsigned int guest_vcpu);
342
343 /* moved from trace-cmd.h */
344 void tracecmd_remove_instances(void);
345 int tracecmd_add_event(const char *event_str, int stack);
346 void tracecmd_enable_events(void);
347 void tracecmd_disable_all_tracing(int disable_tracer);
348 void tracecmd_disable_tracing(void);
349 void tracecmd_enable_tracing(void);
350 void tracecmd_stat_cpu(struct trace_seq *s, int cpu);
351
352 int tracecmd_host_tsync(struct buffer_instance *instance,
353 unsigned int tsync_port);
354 void tracecmd_host_tsync_complete(struct buffer_instance *instance);
355 const char *tracecmd_guest_tsync(struct tracecmd_tsync_protos *tsync_protos,
356 char *clock, unsigned int *tsync_port,
357 pthread_t *thr_id);
358
359 int trace_make_vsock(unsigned int port);
360 int trace_get_vsock_port(int sd, unsigned int *port);
361 int trace_open_vsock(unsigned int cid, unsigned int port);
362
363 int get_local_cid(unsigned int *cid);
364
365 char *trace_get_guest_file(const char *file, const char *guest);
366
367 #ifdef VSOCK
368 int trace_vsock_open(unsigned int cid, unsigned int port);
369 int trace_vsock_make(unsigned int port);
370 int trace_vsock_make_any(void);
371 int get_vsocket_params(int fd, unsigned int *lcid, unsigned int *rcid);
372 int trace_vsock_get_port(int sd, unsigned int *port);
373 bool trace_vsock_can_splice_read(void);
374 int trace_vsock_local_cid(void);
375 int trace_vsock_print_connection(int fd);
376 #else
trace_vsock_open(unsigned int cid,unsigned int port)377 static inline int trace_vsock_open(unsigned int cid, unsigned int port)
378 {
379 return -ENOTSUP;
380 }
381
trace_vsock_make(unsigned int port)382 static inline int trace_vsock_make(unsigned int port)
383 {
384 return -ENOTSUP;
385
386 }
387
trace_vsock_make_any(void)388 static inline int trace_vsock_make_any(void)
389 {
390 return -ENOTSUP;
391
392 }
393
get_vsocket_params(int fd,unsigned int * lcid,unsigned int * rcid)394 static inline int get_vsocket_params(int fd, unsigned int *lcid, unsigned int *rcid)
395 {
396 return -ENOTSUP;
397 }
398
trace_vsock_get_port(int sd,unsigned int * port)399 static inline int trace_vsock_get_port(int sd, unsigned int *port)
400 {
401 return -ENOTSUP;
402 }
403
trace_vsock_can_splice_read(void)404 static inline bool trace_vsock_can_splice_read(void)
405 {
406 return false;
407 }
408
trace_vsock_local_cid(void)409 static inline int trace_vsock_local_cid(void)
410 {
411 return -ENOTSUP;
412 }
trace_vsock_print_connection(int fd)413 static inline int trace_vsock_print_connection(int fd)
414 {
415 return -1;
416 }
417 #endif /* VSOCK */
418
419 /* No longer in event-utils.h */
420 __printf(1,2)
421 void __noreturn die(const char *fmt, ...); /* Can be overriden */
422 void *malloc_or_die(unsigned int size); /* Can be overridden */
423 __printf(1,2)
424 void __noreturn __die(const char *fmt, ...);
425 void __noreturn _vdie(const char *fmt, va_list ap);
426
is_digits(const char * s)427 static inline bool is_digits(const char *s)
428 {
429 for (; *s; s++)
430 if (!isdigit(*s))
431 return false;
432 return true;
433 }
434
435 bool trace_tsc2nsec_is_supported(void);
436
437 #endif /* __TRACE_LOCAL_H */
438