forked from namhyung/uftrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uftrace.h
601 lines (534 loc) · 14.5 KB
/
uftrace.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
#ifndef UFTRACE_H
#define UFTRACE_H
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <fcntl.h>
#include "utils/rbtree.h"
#include "utils/list.h"
#include "utils/symbol.h"
#include "utils/perf.h"
#include "utils/filter.h"
#include "utils/arch.h"
#define UFTRACE_MAGIC_LEN 8
#define UFTRACE_MAGIC_STR "Ftrace!"
#define UFTRACE_FILE_VERSION 4
#define UFTRACE_FILE_VERSION_MIN 3
#define UFTRACE_DIR_NAME "uftrace.data"
#define UFTRACE_DIR_OLD_NAME "ftrace.dir"
#define UFTRACE_RECV_PORT 8090
/* default option values */
#define OPT_RSTACK_MAX 65535
#define OPT_RSTACK_DEFAULT 1024
#define OPT_DEPTH_MAX OPT_RSTACK_MAX
#define OPT_DEPTH_DEFAULT OPT_RSTACK_DEFAULT
#define OPT_COLUMN_OFFSET 8
#define OPT_SORT_COLUMN 2
#define OPT_SORT_KEYS "total"
#define KB 1024
#define MB (KB * 1024)
struct uftrace_file_header {
char magic[UFTRACE_MAGIC_LEN];
uint32_t version;
uint16_t header_size;
uint8_t endian;
uint8_t elf_class;
uint64_t feat_mask;
uint64_t info_mask;
uint16_t max_stack;
uint16_t unused1;
uint32_t unused2;
};
enum uftrace_feat_bits {
/* bit index */
PLTHOOK_BIT,
TASK_SESSION_BIT,
KERNEL_BIT,
ARGUMENT_BIT,
RETVAL_BIT,
SYM_REL_ADDR_BIT,
MAX_STACK_BIT,
EVENT_BIT,
PERF_EVENT_BIT,
AUTO_ARGS_BIT,
DEBUG_INFO_BIT,
ESTIMATE_RETURN_BIT,
FEAT_BIT_MAX,
/* bit mask */
PLTHOOK = (1U << PLTHOOK_BIT),
TASK_SESSION = (1U << TASK_SESSION_BIT),
KERNEL = (1U << KERNEL_BIT),
ARGUMENT = (1U << ARGUMENT_BIT),
RETVAL = (1U << RETVAL_BIT),
SYM_REL_ADDR = (1U << SYM_REL_ADDR_BIT),
MAX_STACK = (1U << MAX_STACK_BIT),
EVENT = (1U << EVENT_BIT),
PERF_EVENT = (1U << PERF_EVENT_BIT),
AUTO_ARGS = (1U << AUTO_ARGS_BIT),
DEBUG_INFO = (1U << DEBUG_INFO_BIT),
ESTIMATE_RETURN = (1U << ESTIMATE_RETURN_BIT),
};
enum uftrace_info_bits {
EXE_NAME,
EXE_BUILD_ID,
EXIT_STATUS,
CMDLINE,
CPUINFO,
MEMINFO,
OSINFO,
TASKINFO,
USAGEINFO,
LOADINFO,
ARG_SPEC,
RECORD_DATE,
PATTERN_TYPE,
VERSION,
};
struct uftrace_info {
char *exename;
unsigned char build_id[20];
int exit_status;
char *cmdline;
int nr_cpus_online;
int nr_cpus_possible;
char *cpudesc;
char *meminfo;
char *kernel;
char *hostname;
char *distro;
char *argspec;
char *retspec;
char *autoarg;
char *autoret;
char *autoenum;
bool auto_args_enabled;
int nr_tid;
int *tids;
double stime;
double utime;
char *record_date;
char *elapsed_time;
long vctxsw;
long ictxsw;
long maxrss;
long major_fault;
long minor_fault;
long rblock;
long wblock;
float load1;
float load5;
float load15;
enum uftrace_pattern_type patt_type;
char *uftrace_version;
};
enum {
UFTRACE_EXIT_SUCCESS = 0,
UFTRACE_EXIT_FAILURE,
UFTRACE_EXIT_SIGNALED,
UFTRACE_EXIT_UNKNOWN,
UFTRACE_EXIT_FINISHED = 1 << 16,
};
struct kbuffer;
struct pevent;
struct uftrace_record;
struct uftrace_rstack_list;
struct uftrace_session;
struct uftrace_kernel_reader;
struct uftrace_perf_reader;
struct uftrace_extern_reader;
struct uftrace_module;
struct uftrace_session_link {
struct rb_root root;
struct rb_root tasks;
struct uftrace_session *first;
struct uftrace_task *first_task;
};
struct uftrace_data {
FILE *fp;
int sock;
const char *dirname;
enum uftrace_cpu_arch arch;
struct uftrace_file_header hdr;
struct uftrace_info info;
struct uftrace_kernel_reader *kernel;
struct uftrace_perf_reader *perf;
struct uftrace_extern_reader *extn;
struct uftrace_task_reader *tasks;
struct uftrace_session_link sessions;
int nr_tasks;
int nr_perf;
int last_perf_idx;
int depth;
bool needs_byte_swap;
bool needs_bit_swap;
bool perf_event_processed;
bool caller_filter;
uint64_t time_filter;
struct uftrace_time_range time_range;
struct list_head events;
};
bool data_is_lp64(struct uftrace_data *handle);
#define UFTRACE_MODE_INVALID 0
#define UFTRACE_MODE_RECORD 1
#define UFTRACE_MODE_REPLAY 2
#define UFTRACE_MODE_LIVE 3
#define UFTRACE_MODE_REPORT 4
#define UFTRACE_MODE_INFO 5
#define UFTRACE_MODE_RECV 6
#define UFTRACE_MODE_DUMP 7
#define UFTRACE_MODE_GRAPH 8
#define UFTRACE_MODE_SCRIPT 9
#define UFTRACE_MODE_TUI 10
#define UFTRACE_MODE_DEFAULT UFTRACE_MODE_LIVE
struct opts {
char *lib_path;
char *filter;
char *trigger;
char *sig_trigger;
char *tid;
char *exename;
char *dirname;
char *logfile;
char *host;
char *sort_keys;
char *args;
char *retval;
char *diff;
char *fields;
char *patch;
char *event;
char *watch;
char **run_cmd;
char *opt_file;
char *script_file;
char *diff_policy;
char *caller;
char *extern_data;
char *hide;
int mode;
int idx;
int depth;
int kernel_depth;
int max_stack;
int port;
int color;
int column_offset;
int sort_column;
int nr_thread;
int rt_prio;
int size_filter;
unsigned long bufsize;
unsigned long kernel_bufsize;
uint64_t threshold;
uint64_t sample_time;
bool flat;
bool libcall;
bool print_symtab;
bool force;
bool show_task;
bool no_merge;
bool nop;
bool time;
bool backtrace;
bool use_pager;
bool avg_total;
bool avg_self;
bool disabled;
bool report;
bool column_view;
bool want_bind_not;
bool task_newline;
bool chrome_trace;
bool comment;
bool flame_graph;
bool libmcount_single;
bool kernel;
bool kernel_skip_out; /* also affects VDSO filter */
bool kernel_only;
bool keep_pid;
bool list_event;
bool event_skip_out;
bool no_event;
bool no_sched;
bool nest_libcall;
bool record;
bool auto_args;
bool libname;
bool no_randomize_addr;
bool graphviz;
bool srcline;
bool estimate_return;
struct uftrace_time_range range;
enum uftrace_pattern_type patt_type;
};
extern struct strv default_opts;
static inline bool opts_has_filter(struct opts *opts)
{
return opts->filter || opts->trigger || opts->threshold ||
opts->depth != OPT_DEPTH_DEFAULT;
}
void parse_script_opt(struct opts *opts);
int command_record(int argc, char *argv[], struct opts *opts);
int command_replay(int argc, char *argv[], struct opts *opts);
int command_live(int argc, char *argv[], struct opts *opts);
int command_report(int argc, char *argv[], struct opts *opts);
int command_info(int argc, char *argv[], struct opts *opts);
int command_recv(int argc, char *argv[], struct opts *opts);
int command_dump(int argc, char *argv[], struct opts *opts);
int command_graph(int argc, char *argv[], struct opts *opts);
int command_script(int argc, char *argv[], struct opts *opts);
int command_tui(int argc, char *argv[], struct opts *opts);
extern volatile bool uftrace_done;
int open_data_file(struct opts *opts, struct uftrace_data *handle);
int open_info_file(struct opts *opts, struct uftrace_data *handle);
void __close_data_file(struct opts *opts, struct uftrace_data *handle,
bool unload_modules);
static inline void close_data_file(struct opts *opts, struct uftrace_data *handle)
{
__close_data_file(opts, handle, true);
}
int read_task_file(struct uftrace_session_link *sess, char *dirname,
bool needs_symtab, bool sym_rel_addr, bool needs_srcline);
int read_task_txt_file(struct uftrace_session_link *sess, char *dirname,
bool needs_symtab, bool sym_rel_addr, bool needs_srcline);
char * get_libmcount_path(struct opts *opts);
void put_libmcount_path(char *libpath);
#define SESSION_ID_LEN 16
#define TASK_COMM_LEN 16
#define TASK_COMM_LAST (TASK_COMM_LEN - 1)
struct uftrace_session {
struct rb_node node;
char sid[SESSION_ID_LEN];
uint64_t start_time;
int pid, tid;
struct symtabs symtabs;
struct rb_root filters;
struct rb_root fixups;
struct list_head dlopen_libs;
int namelen;
char exename[];
};
struct uftrace_sess_ref {
struct uftrace_sess_ref *next;
struct uftrace_session *sess;
uint64_t start, end;
};
struct uftrace_dlopen_list {
struct list_head list;
uint64_t time;
unsigned long base;
struct uftrace_module *mod;
};
struct uftrace_task {
int pid, tid, ppid;
char comm[TASK_COMM_LEN];
struct rb_node node;
struct uftrace_sess_ref sref;
struct uftrace_sess_ref *sref_last;
struct list_head children;
struct list_head siblings;
struct {
uint64_t run;
uint64_t idle;
uint64_t stamp;
} time;
};
#define UFTRACE_MSG_MAGIC 0xface
enum uftrace_msg_type {
UFTRACE_MSG_REC_START = 1,
UFTRACE_MSG_REC_END,
UFTRACE_MSG_TASK_START,
UFTRACE_MSG_TASK_END,
UFTRACE_MSG_FORK_START,
UFTRACE_MSG_FORK_END,
UFTRACE_MSG_SESSION,
UFTRACE_MSG_LOST,
UFTRACE_MSG_DLOPEN,
UFTRACE_MSG_FINISH,
UFTRACE_MSG_SEND_START = 100,
UFTRACE_MSG_SEND_DIR_NAME,
UFTRACE_MSG_SEND_DATA,
UFTRACE_MSG_SEND_KERNEL_DATA,
UFTRACE_MSG_SEND_PERF_DATA,
UFTRACE_MSG_SEND_INFO,
UFTRACE_MSG_SEND_META_DATA,
UFTRACE_MSG_SEND_END,
};
/* msg format for communicating by pipe */
struct uftrace_msg {
unsigned short magic; /* UFTRACE_MSG_MAGIC */
unsigned short type; /* UFTRACE_MSG_REC_* */
unsigned int len;
unsigned char data[];
};
struct uftrace_msg_task {
uint64_t time;
int32_t pid;
int32_t tid;
};
struct uftrace_msg_sess {
struct uftrace_msg_task task;
char sid[16];
int unused;
int namelen;
char exename[];
};
struct uftrace_msg_dlopen {
struct uftrace_msg_task task;
uint64_t base_addr;
char sid[16];
int unused;
int namelen;
char exename[];
};
extern struct uftrace_session *first_session;
void create_session(struct uftrace_session_link *sess,
struct uftrace_msg_sess *msg,
char *dirname, char *exename, bool sym_rel_addr,
bool needs_symtab, bool needs_srcline);
struct uftrace_session *find_task_session(struct uftrace_session_link *sess,
struct uftrace_task *task,
uint64_t timestamp);
void create_task(struct uftrace_session_link *sess, struct uftrace_msg_task *msg,
bool fork);
struct uftrace_task *find_task(struct uftrace_session_link *sess, int tid);
void read_session_map(char *dirname, struct symtabs *symtabs, char *sid);
void delete_session_map(struct symtabs *symtabs);
void update_session_map(const char *filename);
struct uftrace_session * get_session_from_sid(struct uftrace_session_link *sess,
char sid[]);
void session_add_dlopen(struct uftrace_session *sess, uint64_t timestamp,
unsigned long base_addr, const char *libname);
struct sym * session_find_dlsym(struct uftrace_session *sess, uint64_t timestamp,
unsigned long addr);
void delete_sessions(struct uftrace_session_link *sess);
struct uftrace_record;
struct sym * task_find_sym(struct uftrace_session_link *sess,
struct uftrace_task_reader *task,
struct uftrace_record *rec);
struct sym * task_find_sym_addr(struct uftrace_session_link *sess,
struct uftrace_task_reader *task,
uint64_t time, uint64_t addr);
struct debug_location * task_find_loc_addr(struct uftrace_session_link *sess,
struct uftrace_task_reader *task,
uint64_t time, uint64_t addr);
typedef int (*walk_sessions_cb_t)(struct uftrace_session *session, void *arg);
void walk_sessions(struct uftrace_session_link *sess,
walk_sessions_cb_t callback, void *arg);
typedef int (*walk_tasks_cb_t)(struct uftrace_task *task, void *arg);
void walk_tasks(struct uftrace_session_link *sess,
walk_tasks_cb_t callback, void *arg);
int setup_client_socket(struct opts *opts);
void send_trace_dir_name(int sock, char *name);
void send_trace_data(int sock, int tid, void *data, size_t len);
void send_trace_kernel_data(int sock, int cpu, void *data, size_t len);
void send_trace_perf_data(int sock, int cpu, void *data, size_t len);
void send_trace_metadata(int sock, const char *dirname, char *filename);
void send_trace_info(int sock, struct uftrace_file_header *hdr,
void *info, int len);
void send_trace_end(int sock);
void write_task_info(const char *dirname, struct uftrace_msg_task *tmsg);
void write_fork_info(const char *dirname, struct uftrace_msg_task *tmsg);
void write_session_info(const char *dirname, struct uftrace_msg_sess *smsg,
const char *exename);
void write_dlopen_info(const char *dirname, struct uftrace_msg_dlopen *dmsg,
const char *libname);
enum uftrace_record_type {
UFTRACE_ENTRY,
UFTRACE_EXIT,
UFTRACE_LOST,
UFTRACE_EVENT,
};
#define RECORD_MAGIC_V3 0xa
#define RECORD_MAGIC_V4 0x5
#define RECORD_MAGIC RECORD_MAGIC_V4
/* reduced version of mcount_ret_stack */
struct uftrace_record {
uint64_t time;
uint64_t type: 2;
uint64_t more: 1;
uint64_t magic: 3;
uint64_t depth: 10;
uint64_t addr: 48; /* child ip or uftrace_event_id */
};
static inline bool is_v3_compat(struct uftrace_record *urec)
{
/* (RECORD_MAGIC_V4 << 1 | more) == RECORD_MAGIC_V3 */
return urec->magic == RECORD_MAGIC && urec->more == 0;
}
struct fstack_arguments {
struct list_head *args;
unsigned len;
void *data;
};
struct uftrace_rstack_list {
struct list_head read;
struct list_head unused;
int count;
};
struct uftrace_rstack_list_node {
struct list_head list;
struct uftrace_record rstack;
struct fstack_arguments args;
};
void setup_rstack_list(struct uftrace_rstack_list *list);
void add_to_rstack_list(struct uftrace_rstack_list *list,
struct uftrace_record *rstack,
struct fstack_arguments *args);
struct uftrace_record * get_first_rstack_list(struct uftrace_rstack_list *);
void consume_first_rstack_list(struct uftrace_rstack_list *list);
void delete_last_rstack_list(struct uftrace_rstack_list *list);
void reset_rstack_list(struct uftrace_rstack_list *list);
enum ftrace_ext_type {
FTRACE_ARGUMENT = 1,
};
static inline bool has_perf_data(struct uftrace_data *handle)
{
return handle->perf != NULL;
}
static inline bool has_event_data(struct uftrace_data *handle)
{
return handle->perf_event_processed;
}
struct rusage;
void fill_uftrace_info(uint64_t *info_mask, int fd, struct opts *opts, int status,
struct rusage *rusage, char *elapsed_time);
int read_uftrace_info(uint64_t info_mask, struct uftrace_data *handle);
void process_uftrace_info(struct uftrace_data *handle, struct opts *opts,
void (*process)(void *data, const char *fmt, ...),
void *data);
void clear_uftrace_info(struct uftrace_info *info);
int arch_fill_cpuinfo_model(int fd);
enum uftrace_event_id {
EVENT_ID_KERNEL = 0U,
/* kernel IDs are read from tracefs */
EVENT_ID_BUILTIN = 100000U,
EVENT_ID_READ_PROC_STATM,
EVENT_ID_READ_PAGE_FAULT,
EVENT_ID_DIFF_PROC_STATM,
EVENT_ID_DIFF_PAGE_FAULT,
EVENT_ID_READ_PMU_CYCLE,
EVENT_ID_DIFF_PMU_CYCLE,
EVENT_ID_READ_PMU_CACHE,
EVENT_ID_DIFF_PMU_CACHE,
EVENT_ID_READ_PMU_BRANCH,
EVENT_ID_DIFF_PMU_BRANCH,
EVENT_ID_WATCH_CPU,
/* supported perf events */
EVENT_ID_PERF = 200000U,
EVENT_ID_PERF_SCHED_IN,
EVENT_ID_PERF_SCHED_OUT,
EVENT_ID_PERF_SCHED_BOTH,
EVENT_ID_PERF_TASK,
EVENT_ID_PERF_EXIT,
EVENT_ID_PERF_COMM,
EVENT_ID_USER = 1000000U,
EVENT_ID_EXTERN_DATA = 2000000U,
};
struct uftrace_event {
struct list_head list;
enum uftrace_event_id id;
char *provider;
char *event;
};
#endif /* UFTRACE_H */