-
Notifications
You must be signed in to change notification settings - Fork 2
/
kprobe_queue.c
1532 lines (1363 loc) · 33.6 KB
/
kprobe_queue.c
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: Apache-2.0
/* Copyright (c) 2024 Elastic NV */
#include <linux/perf_event.h>
#include <linux/hw_breakpoint.h>
#include <sys/epoll.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/param.h>
#include <sys/syscall.h>
#include <sys/sysinfo.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <time.h>
#include <unistd.h>
#include "quark.h"
#define PERF_MMAP_PAGES 16 /* Must be power of 2 */
struct perf_sample_id {
u32 pid;
u32 tid;
u64 time; /* See raw_event_insert() */
u32 cpu;
u32 cpu_unused;
};
struct perf_record_fork {
struct perf_event_header header;
u32 pid;
u32 ppid;
u32 tid;
u32 ptid;
u64 time;
struct perf_sample_id sample_id;
};
struct perf_record_exit {
struct perf_event_header header;
u32 pid;
u32 ppid;
u32 tid;
u32 ptid;
u64 time;
struct perf_sample_id sample_id;
};
struct perf_record_comm {
struct perf_event_header header;
u32 pid;
u32 tid;
char comm[];
/* followed by sample_id */
};
/*
* Kernels might actually have a different common area, so far we only
* need common_type, so hold onto that
*/
struct perf_sample_data_hdr {
/* this is the actual id from tracefs eg: sched_process_exec/id */
u16 common_type;
/* ... */
};
struct perf_sample_data_loc {
u16 offset;
u16 size;
};
struct perf_record_sample {
struct perf_event_header header;
struct perf_sample_id sample_id;
u32 data_size;
char data[];
};
struct perf_record_lost {
struct perf_event_header header;
u64 id;
u64 lost;
struct perf_sample_id sample_id;
};
struct perf_event {
union {
struct perf_event_header header;
struct perf_record_fork fork;
struct perf_record_exit exit;
struct perf_record_comm comm;
struct perf_record_sample sample;
struct perf_record_lost lost;
};
};
struct perf_mmap {
struct perf_event_mmap_page *metadata;
size_t mapped_size;
size_t data_size;
size_t data_mask;
u8 *data_start;
u64 data_tmp_tail;
u8 wrapped_event_buf[4096] __aligned(8);
};
struct perf_group_leader {
TAILQ_ENTRY(perf_group_leader) entry;
int fd;
int cpu;
struct perf_event_attr attr;
struct perf_mmap mmap;
};
/*
* Forbid padding on samples/wire structures
*/
#ifndef NO_PUSH_PRAGMA
#pragma GCC diagnostic push
#pragma GCC diagnostic error "-Wpadded"
#endif /* NO_PUSH_PRAGMA */
struct exec_sample {
struct perf_sample_data_loc filename;
s32 pid;
s32 old_pid;
};
#define MAX_PWD 7
/* Sorted by alignment restriction, 64->32->16->8 */
struct task_sample {
/* 64bit */
u64 probe_ip;
u64 cap_inheritable;
u64 cap_permitted;
u64 cap_effective;
u64 cap_bset;
u64 cap_ambient;
u64 start_boottime;
u64 tty_addr;
u64 root_k;
u64 mnt_root_k;
u64 mnt_mountpoint_k;
u64 pwd_k[MAX_PWD];
/* 32bit */
struct perf_sample_data_loc root_s;
struct perf_sample_data_loc mnt_root_s;
struct perf_sample_data_loc mnt_mountpoint_s;
struct perf_sample_data_loc pwd_s[MAX_PWD];
struct perf_sample_data_loc comm;
u32 uid;
u32 gid;
u32 suid;
u32 sgid;
u32 euid;
u32 egid;
u32 pgid;
u32 sid;
u32 pid;
u32 tid;
u32 ppid;
s32 exit_code;
u32 tty_major;
u32 tty_minor_start;
u32 tty_minor_index;
u32 uts_inonum;
u32 ipc_inonum;
u32 mnt_inonum;
u32 net_inonum;
/* 16bit */
/* 8bit */
};
struct exec_connector_sample {
struct task_sample task_sample; /* must be 8 byte aligned */
/* 64bit */
u64 argc;
u64 stack[55]; /* sync with kprobe_defs */
};
#ifndef NO_PUSH_PRAGMA
#pragma GCC diagnostic pop
#endif /* NO_PUSH_PRAGMA */
/*
* End samples/wire/ structures
*/
struct kprobe_state {
TAILQ_ENTRY(kprobe_state) entry;
struct kprobe *k;
struct perf_event_attr attr;
int fd;
int cpu;
int group_fd;
};
struct kprobe_arg {
const char *name;
const char *reg;
const char *typ;
const char *arg_dsl;
};
struct kprobe {
const char *target;
int sample_kind;
int is_kret;
struct kprobe_arg args[];
};
struct path_ctx {
const char *root;
u64 root_k;
const char *mnt_root;
u64 mnt_root_k;
const char *mnt_mountpoint;
u64 mnt_mountpoint_k;
struct {
const char *pwd;
u64 pwd_k;
} pwd[MAX_PWD];
};
/*
* Kprobe sample formats
*/
enum sample_kinds {
INVALID_SAMPLE,
EXEC_SAMPLE,
WAKE_UP_NEW_TASK_SAMPLE,
EXIT_THREAD_SAMPLE,
EXEC_CONNECTOR_SAMPLE
};
/*
* Attributes of sample, maps id to kind and data_offset
*/
struct sample_attr {
int id;
int kind;
size_t data_offset;
};
/*
* The actual probe definitions, they're too big and ugly so they get a separate
* file
*/
#include "kprobe_defs.h"
/*
* Queue backend state
*/
TAILQ_HEAD(perf_group_leaders, perf_group_leader);
TAILQ_HEAD(kprobe_states, kprobe_state);
/* We only use 4, bump when needed */
#define MAX_SAMPLE_ATTR 8
struct kprobe_queue {
struct perf_group_leaders perf_group_leaders;
int num_perf_group_leaders;
struct kprobe_states kprobe_states;
int qid;
/* matches each sample event to a kind like EXEC_SAMPLE, FOO_SAMPLE */
struct sample_attr id_to_sample_attr[MAX_SAMPLE_ATTR];
};
static int kprobe_queue_populate(struct quark_queue *);
static int kprobe_queue_update_stats(struct quark_queue *);
static void kprobe_queue_close(struct quark_queue *);
struct quark_queue_ops queue_ops_kprobe = {
.open = kprobe_queue_open,
.populate = kprobe_queue_populate,
.update_stats = kprobe_queue_update_stats,
.close = kprobe_queue_close,
};
static ssize_t parse_data_offset(const char *);
static const char *
str_of_dataloc(struct perf_record_sample *sample,
const struct perf_sample_data_loc *data_loc)
{
return (sample->data + data_loc->offset);
}
static const struct sample_attr *
sample_attr_of_id(struct kprobe_queue *kqq, int id)
{
int i;
for (i = 0; i < MAX_SAMPLE_ATTR; i++) {
if (kqq->id_to_sample_attr[i].id == id)
return (&kqq->id_to_sample_attr[i]);
}
return (NULL);
}
static struct sample_attr *
sample_attr_prepare(struct kprobe_queue *kqq, int id, const char *format,
ssize_t *data_offset)
{
int i;
struct sample_attr *sattr;
if (sample_attr_of_id(kqq, id) != NULL) {
qwarnx("id already allocated");
return (NULL);
}
*data_offset = parse_data_offset(format);
if (*data_offset == -1) {
qwarnx("can't parse data offset");
return (NULL);
}
sattr = NULL;
for (i = 0; i < MAX_SAMPLE_ATTR; i++) {
if (kqq->id_to_sample_attr[i].kind == INVALID_SAMPLE) {
sattr = &kqq->id_to_sample_attr[i];
break;
}
}
if (sattr == NULL)
qwarnx("no more free sample attr slots");
return (sattr);
}
static inline int
sample_data_id(struct perf_record_sample *sample)
{
struct perf_sample_data_hdr *h = (struct perf_sample_data_hdr *)sample->data;
return (h->common_type);
}
static inline const void *
sample_data_body(struct perf_record_sample *sample, const struct sample_attr *sattr)
{
return (sample->data + sattr->data_offset);
}
static int
build_path(struct path_ctx *ctx, struct qstr *dst)
{
int i, done;
char *p, path[MAXPATHLEN];
const char *pwd, *ppwd;
u64 pwd_k;
p = &path[sizeof(path) - 1];
*p = 0;
done = 0;
for (i = 0; i < (int)nitems(ctx->pwd) && !done; i++) {
pwd_k = ctx->pwd[i].pwd_k;
pwd = ctx->pwd[i].pwd;
if (pwd_k == ctx->root_k)
break;
if (pwd_k == ctx->mnt_root_k) {
pwd = ctx->mnt_mountpoint;
done = 1;
}
/* XXX this strlen sucks as we had the length on the wire */
ppwd = pwd + strlen(pwd);
/* +1 is the / */
/* XXX this is way too dangerous XXX */
if (((ppwd - pwd) + 1) > (p - path))
return (errno = ENAMETOOLONG, -1);
while (ppwd != pwd)
*--p = *--ppwd;
*--p = '/';
}
if (*p == 0)
*--p = '/';
/* XXX double copy XXX */
return (qstr_strcpy(dst, p));
}
static int
qstr_copy_data_loc(struct qstr *qstr,
struct perf_record_sample *sample,
const struct perf_sample_data_loc *data_loc)
{
/* size includes NUL */
if (qstr_ensure(qstr, data_loc->size) == -1)
return (-1);
memcpy(qstr->p, sample->data + data_loc->offset, data_loc->size);
return (data_loc->size);
}
static void
task_sample_to_raw_task(struct kprobe_queue *kqq, const struct sample_attr *sattr,
struct perf_record_sample *sample, struct raw_task *task)
{
const struct task_sample *w = sample_data_body(sample, sattr);
struct path_ctx pctx;
int i;
task->cap_inheritable = w->cap_inheritable;
task->cap_permitted = w->cap_permitted;
task->cap_effective = w->cap_effective;
task->cap_bset = w->cap_bset;
task->cap_ambient = w->cap_ambient;
task->start_boottime = w->start_boottime;
task->uid = w->uid;
task->gid = w->gid;
task->suid = w->suid;
task->sgid = w->sgid;
task->euid = w->euid;
task->egid = w->egid;
task->pgid = w->pgid;
task->sid = w->sid;
task->ppid = w->ppid;
if (w->tty_addr) {
task->tty_major = w->tty_major;
task->tty_minor = w->tty_minor_start + w->tty_minor_index;
}
task->uts_inonum = w->uts_inonum;
task->ipc_inonum = w->ipc_inonum;
task->mnt_inonum = w->mnt_inonum;
task->net_inonum = w->net_inonum;
/* cwd below */
strlcpy(task->comm, str_of_dataloc(sample, &w->comm),
sizeof(task->comm));
if (sattr->kind == EXIT_THREAD_SAMPLE) {
task->exit_code = (w->exit_code >> 8) & 0xff;
task->exit_time_event = sample->sample_id.time;
qstr_strcpy(&task->cwd, "(exited)");
/* No cwd on exit */
return;
}
task->exit_code = -1;
task->exit_time_event = 0;
/* Consider moving all this inside build_path() */
pctx.root = str_of_dataloc(sample, &w->root_s);
pctx.root_k = w->root_k;
pctx.mnt_root = str_of_dataloc(sample, &w->mnt_root_s);
pctx.mnt_root_k = w->mnt_root_k;
pctx.mnt_mountpoint = str_of_dataloc(sample,
&w->mnt_mountpoint_s);
pctx.mnt_mountpoint_k = w->mnt_mountpoint_k;
for (i = 0; i < (int)nitems(pctx.pwd); i++) {
pctx.pwd[i].pwd = str_of_dataloc(sample,
&w->pwd_s[i]);
pctx.pwd[i].pwd_k = w->pwd_k[i];
}
if (build_path(&pctx, &task->cwd) == -1)
qwarn("can't build path");
}
static struct raw_event *
perf_sample_to_raw(struct quark_queue *qq, struct perf_record_sample *sample)
{
struct kprobe_queue *kqq = qq->queue_be;
const struct sample_attr *sattr;
int id, kind;
ssize_t n;
struct raw_event *raw = NULL;
id = sample_data_id(sample);
sattr = sample_attr_of_id(kqq, id);
if (sattr != NULL)
kind = sattr->kind;
else
kind = INVALID_SAMPLE;
switch (kind) {
case EXEC_SAMPLE: {
const struct exec_sample *exec = sample_data_body(sample, sattr);
if ((raw = raw_event_alloc(RAW_EXEC)) == NULL)
return (NULL);
n = qstr_copy_data_loc(&raw->exec.filename, sample, &exec->filename);
if (n == -1)
qwarnx("can't copy exec filename");
break;
}
case WAKE_UP_NEW_TASK_SAMPLE: /* FALLTHROUGH */
case EXIT_THREAD_SAMPLE: {
const struct task_sample *w = sample_data_body(sample, sattr);
int raw_type;
/*
* ev->sample.sample_id.pid is the parent, if the new task has
* the same pid as it, then this is a thread event
*/
if ((qq->flags & QQ_THREAD_EVENTS) == 0
&& w->pid != w->tid)
return (NULL);
raw_type = kind == WAKE_UP_NEW_TASK_SAMPLE ?
RAW_WAKE_UP_NEW_TASK : RAW_EXIT_THREAD;
if ((raw = raw_event_alloc(raw_type)) == NULL)
return (NULL);
/*
* Cheat, make it look like a child event
*/
if (raw_type == RAW_WAKE_UP_NEW_TASK) {
raw->pid = w->pid;
raw->tid = w->tid;
}
task_sample_to_raw_task(kqq, sattr, sample, &raw->task);
break;
}
case EXEC_CONNECTOR_SAMPLE: {
const char *start, *p, *end;
int i;
const struct exec_connector_sample *exec_sample;
struct raw_exec_connector *exec;
exec_sample = sample_data_body(sample, sattr);
if ((raw = raw_event_alloc(RAW_EXEC_CONNECTOR)) == NULL)
return (NULL);
exec = &raw->exec_connector;
start = p = (const char *)&exec_sample->stack[0];
end = start + sizeof(exec_sample->stack);
for (i = 0; i < (int)exec_sample->argc && p < end; i++)
p += strnlen(p, end - p) + 1;
if (p >= end)
p = end;
exec->args_len = p - start;
if (exec->args_len == 0)
exec->args.p[0] = 0;
else {
if (qstr_memcpy(&exec->args, start, exec->args_len) == -1)
qwarnx("can't copy args");
exec->args.p[exec->args_len - 1] = 0;
}
task_sample_to_raw_task(kqq, sattr, sample, &exec->task);
break;
}
default:
qwarnx("unknown or invalid sample id=%d", id);
return (NULL);
}
return (raw);
}
static struct raw_event *
perf_event_to_raw(struct quark_queue *qq, struct perf_event *ev)
{
struct raw_event *raw = NULL;
struct perf_sample_id *sid = NULL;
ssize_t n;
switch (ev->header.type) {
case PERF_RECORD_SAMPLE:
raw = perf_sample_to_raw(qq, &ev->sample);
if (raw != NULL)
sid = &ev->sample.sample_id;
break;
case PERF_RECORD_COMM:
/*
* Supress comm events due to exec as we can fetch comm
* directly from the task struct
*/
if (ev->header.misc & PERF_RECORD_MISC_COMM_EXEC)
return (NULL);
if ((qq->flags & QQ_THREAD_EVENTS) == 0 &&
ev->comm.pid != ev->comm.tid)
return (NULL);
if ((raw = raw_event_alloc(RAW_COMM)) == NULL)
return (NULL);
n = strlcpy(raw->comm.comm, ev->comm.comm,
sizeof(raw->comm.comm));
/*
* Yes, comm is variable length, maximum 16. The kernel
* guarantees alignment on an 8byte boundary for the sample_id,
* that means we have to calculate the next boundary.
*/
sid = (struct perf_sample_id *)
ALIGN_UP(ev->comm.comm + n + 1, 8);
break;
case PERF_RECORD_FORK:
case PERF_RECORD_EXIT:
/*
* As long as we are still using PERF_RECORD_COMM events, the
* kernel implies we want FORK and EXIT as well, see
* core.c:perf_event_task_match(), this is likely unintended
* behaviour.
*/
break;
case PERF_RECORD_LOST:
qq->stats.lost += ev->lost.lost;
break;
default:
qwarnx("unhandled type %d", ev->header.type);
return (NULL);
break;
}
if (sid != NULL) {
/* FORK/WAKE_UP_NEW_TASK overloads pid and tid */
if (raw->pid == 0)
raw->pid = sid->pid;
if (raw->tid == 0)
raw->tid = sid->tid;
raw->opid = sid->pid;
raw->tid = sid->tid;
raw->time = sid->time;
raw->cpu = sid->cpu;
}
return (raw);
}
static int
perf_mmap_init(struct perf_mmap *mm, int fd)
{
mm->mapped_size = (1 + PERF_MMAP_PAGES) * getpagesize();
mm->metadata = mmap(NULL, mm->mapped_size, PROT_READ|PROT_WRITE,
MAP_SHARED, fd, 0);
if (mm->metadata == MAP_FAILED)
return (-1);
mm->data_size = PERF_MMAP_PAGES * getpagesize();
mm->data_mask = mm->data_size - 1;
mm->data_start = (uint8_t *)mm->metadata + getpagesize();
mm->data_tmp_tail = mm->metadata->data_tail;
return (0);
}
static inline uint64_t
perf_mmap_load_head(struct perf_event_mmap_page *metadata)
{
return (__atomic_load_n(&metadata->data_head, __ATOMIC_ACQUIRE));
}
static inline void
perf_mmap_update_tail(struct perf_event_mmap_page *metadata, uint64_t tail)
{
return (__atomic_store_n(&metadata->data_tail, tail, __ATOMIC_RELEASE));
}
static struct perf_event *
perf_mmap_read(struct perf_mmap *mm)
{
struct perf_event_header *evh;
uint64_t data_head;
int diff;
ssize_t leftcont; /* contiguous size left */
data_head = perf_mmap_load_head(mm->metadata);
diff = data_head - mm->data_tmp_tail;
evh = (struct perf_event_header *)
(mm->data_start + (mm->data_tmp_tail & mm->data_mask));
/* Do we have at least one complete event */
if (diff < (int)sizeof(*evh) || diff < evh->size)
return (NULL);
/* Guard that we will always be able to fit a wrapped event */
if (unlikely(evh->size > sizeof(mm->wrapped_event_buf)))
errx(1, "getting an event larger than wrapped buf");
/* How much contiguous space there is left */
leftcont = mm->data_size - (mm->data_tmp_tail & mm->data_mask);
/* Everything fits without wrapping */
if (likely(evh->size <= leftcont)) {
mm->data_tmp_tail += evh->size;
return ((struct perf_event *)evh);
}
/*
* Slow path, we have to copy the event out in a linear buffer. Start
* from the remaining end
*/
memcpy(mm->wrapped_event_buf, evh, leftcont);
/* Copy the wrapped portion from the beginning */
memcpy(mm->wrapped_event_buf + leftcont, mm->data_start, evh->size - leftcont);
/* Record where our future tail will be on consume */
mm->data_tmp_tail += evh->size;
return ((struct perf_event *)mm->wrapped_event_buf);
}
static inline void
perf_mmap_consume(struct perf_mmap *mmap)
{
perf_mmap_update_tail(mmap->metadata, mmap->data_tmp_tail);
}
static int
perf_event_open(struct perf_event_attr *attr, pid_t pid, int cpu,
int group_fd, unsigned long flags)
{
return syscall(__NR_perf_event_open, attr, pid, cpu, group_fd, flags);
}
static int
perf_event_open_degradable(struct perf_event_attr *attr, pid_t pid, int cpu,
int group_fd, unsigned long flags)
{
int r;
again:
r = perf_event_open(attr, pid, cpu, group_fd, flags);
if (r >= 0)
return (r);
else if (r == -1 && errno != EINVAL)
return (-1);
/* start degrading until it works */
if (attr->comm_exec) {
attr->comm_exec = 0;
goto again;
}
if (attr->use_clockid) {
attr->use_clockid = 0;
attr->clockid = 0;
goto again;
}
return (r);
}
static int
open_tracing(int flags, const char *fmt, ...)
{
va_list ap;
int dfd, fd, i, r, saved_errno;
char tail[MAXPATHLEN];
char *paths[] = {
"/sys/kernel/tracing",
"/sys/kernel/debug/tracing",
};
va_start(ap, fmt);
r = vsnprintf(tail, sizeof(tail), fmt, ap);
va_end(ap);
if (r == -1 || r >= (int)sizeof(tail))
return (-1);
if (tail[0] == '/')
return (errno = EINVAL, -1);
saved_errno = 0;
for (i = 0; i < (int)nitems(paths); i++) {
if ((dfd = open(paths[i], O_PATH)) == -1) {
if (!saved_errno && errno != ENOENT)
saved_errno = errno;
qwarn("open: %s", paths[i]);
continue;
}
fd = openat(dfd, tail, flags);
close(dfd);
if (fd == -1) {
if (!saved_errno && errno != ENOENT)
saved_errno = errno;
qwarn("open: %s", tail);
continue;
}
return (fd);
}
if (saved_errno)
errno = saved_errno;
return (-1);
}
static int
fetch_tracing_id(const char *tail)
{
int id, fd;
char idbuf[16];
const char *errstr;
ssize_t n;
fd = open_tracing(O_RDONLY, "%s", tail);
if (fd == -1)
return (-1);
n = qread(fd, idbuf, sizeof(idbuf));
close(fd);
if (n <= 0)
return (-1);
idbuf[n - 1] = 0;
id = strtonum(idbuf, 1, INT_MAX, &errstr);
if (errstr != NULL) {
qwarnx("strtonum: %s", errstr);
return (errno = ERANGE, -1);
}
return (id);
}
static ssize_t
parse_data_offset(const char *path)
{
int fd;
FILE *f;
char *line, *s, *e;
const char *errstr;
ssize_t n, data_offset;
size_t line_len;
int past_common;
fd = open_tracing(O_RDONLY, path);
if (fd == -1)
return (-1);
f = fdopen(fd, "r");
if (f == NULL) {
close(fd);
return (-1);
}
past_common = 0;
line = NULL;
line_len = 0;
data_offset = -1;
while ((n = getline(&line, &line_len, f)) != -1) {
if (!past_common) {
past_common = !strcmp(line, "\n");
continue;
}
s = strstr(line, "offset:");
if (s == NULL)
break;
s += strlen("offset:");
e = strchr(s, ';');
if (e == NULL)
break;
*e = 0;
data_offset = strtonum(s, 0, SSIZE_MAX, &errstr);
if (errstr)
data_offset = -1;
break;
}
free(line);
fclose(f);
return (data_offset);
}
#define TOKSZ 256
static int
kprobe_exp_split(const char *exp1, char left[TOKSZ], char *op, char right[TOKSZ])
{
int paren_depth;
char exp[1024];
char *p, *start_left, *start_right;
if (strlcpy(exp, exp1, sizeof(exp)) >= sizeof(exp)) {
qwarnx("expression too long");
return (-1);
}
paren_depth = 0;
start_left = start_right = NULL;
*op = 0;
for (p = exp; *p != 0; p++) {
switch (*p) {
case '(':
if (++paren_depth == 1)
start_left = p + 1;
break;
case '+': /* FALLTHROUGH */
case '-': /* FALLTHROUGH */
case '*': /* FALLTHROUGH */
if (paren_depth > 1)
break;
if (*op != 0) {
qwarnx("multiple operators");
return (-1);
}
*op = *p;
*p = 0;
start_right = p + 1;
break;
case ')':
if (--paren_depth == 0)
*p = 0;
if (paren_depth < 0) {
qwarnx("unbalanced parenthesis");
return (-1);
}
break;
default:
break;
}
}
if (*op == 0) {
qwarnx("no operator");
return (-1);
}
if (start_left == NULL || start_right == NULL) {
qwarnx("syntax error");
return (-1);
}
if (strlcpy(left, start_left, TOKSZ) >= TOKSZ) {
qwarnx("left token overflow");
return (-1);
}
if (strlen(left) == 0) {
qwarnx("empty left token");
return (-1);
}
if (strlcpy(right, start_right, TOKSZ) >= TOKSZ) {
qwarnx("right token overflow");
return (-1);
}
if (strlen(right) == 0) {
qwarnx("empty right token");
return (-1);
}
return (0);
}
static int
kprobe_exp(const char *exp1, ssize_t *off1, struct quark_btf *qbtf)
{
ssize_t off, off_left, off_right;
char exp[1024], op, *end;
char left[TOKSZ], right[TOKSZ];
bzero(left, sizeof(left));
bzero(right, sizeof(right));
if (strlcpy(exp, exp1, sizeof(exp)) >= sizeof(exp)) {
qwarnx("expression too long");
return (-1);
}
switch (*exp) {
case '(': {
if ((end = strrchr(exp, ')')) == NULL) {
qwarnx("unclosed parenthesis: %s", exp);
return (-1);
}
if (kprobe_exp_split(exp, left, &op, right) == -1) {
qwarnx("can't split expression: %s", exp);
return (-1);
}
if (kprobe_exp(left, &off_left, qbtf) == -1) {
qwarnx("left expression is unresolved: %s", exp);
return (-1);
}
if (kprobe_exp(right, &off_right, qbtf) == -1) {
qwarnx("right expression is unresolved: %s", exp);
return (-1);
}
switch (op) {
case '+':
off = off_left + off_right;
break;
case '-':
off = off_left - off_right;
break;
case '*':
off = off_left * off_right;
break;
default:
qwarnx("invalid operator `%c`: %s", op, exp);
return (-1);
}
/* If there is a dot after `)`, recurse */
if (*(end + 1) == '.') {
if (kprobe_exp(end + 2, &off_left, qbtf) == -1) {
qwarnx("expression unresolved: %s", exp);
return (-1);
}
off += off_left;
}
break;
}
default: {
const char *errstr;
off = strtonum(exp, INT32_MIN, INT32_MAX, &errstr);
if (errstr == NULL)
break;
if ((off = quark_btf_offset(qbtf, exp)) == -1) {
qwarnx("expression unresolved: %s", exp);
return (-1);
}
break;
}}
*off1 = off;
return (0);
}