|
11 | 11 | #include <linux/uaccess.h>
|
12 | 12 | #include <linux/ctype.h>
|
13 | 13 | #include <linux/kprobes.h>
|
| 14 | +#include <linux/spinlock.h> |
14 | 15 | #include <linux/syscalls.h>
|
15 | 16 | #include <linux/error-injection.h>
|
16 | 17 | #include <linux/btf_ids.h>
|
|
20 | 21 | #include "trace_probe.h"
|
21 | 22 | #include "trace.h"
|
22 | 23 |
|
| 24 | +#define CREATE_TRACE_POINTS |
| 25 | +#include "bpf_trace.h" |
| 26 | + |
23 | 27 | #define bpf_event_rcu_dereference(p) \
|
24 | 28 | rcu_dereference_protected(p, lockdep_is_held(&bpf_event_mutex))
|
25 | 29 |
|
@@ -375,6 +379,30 @@ static void bpf_trace_copy_string(char *buf, void *unsafe_ptr, char fmt_ptype,
|
375 | 379 | }
|
376 | 380 | }
|
377 | 381 |
|
| 382 | +static DEFINE_RAW_SPINLOCK(trace_printk_lock); |
| 383 | + |
| 384 | +#define BPF_TRACE_PRINTK_SIZE 1024 |
| 385 | + |
| 386 | +static inline __printf(1, 0) int bpf_do_trace_printk(const char *fmt, ...) |
| 387 | +{ |
| 388 | + static char buf[BPF_TRACE_PRINTK_SIZE]; |
| 389 | + unsigned long flags; |
| 390 | + va_list ap; |
| 391 | + int ret; |
| 392 | + |
| 393 | + raw_spin_lock_irqsave(&trace_printk_lock, flags); |
| 394 | + va_start(ap, fmt); |
| 395 | + ret = vsnprintf(buf, sizeof(buf), fmt, ap); |
| 396 | + va_end(ap); |
| 397 | + /* vsnprintf() will not append null for zero-length strings */ |
| 398 | + if (ret == 0) |
| 399 | + buf[0] = '\0'; |
| 400 | + trace_bpf_trace_printk(buf); |
| 401 | + raw_spin_unlock_irqrestore(&trace_printk_lock, flags); |
| 402 | + |
| 403 | + return ret; |
| 404 | +} |
| 405 | + |
378 | 406 | /*
|
379 | 407 | * Only limited trace_printk() conversion specifiers allowed:
|
380 | 408 | * %d %i %u %x %ld %li %lu %lx %lld %lli %llu %llx %p %pB %pks %pus %s
|
@@ -484,8 +512,7 @@ BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
|
484 | 512 | */
|
485 | 513 | #define __BPF_TP_EMIT() __BPF_ARG3_TP()
|
486 | 514 | #define __BPF_TP(...) \
|
487 |
| - __trace_printk(0 /* Fake ip */, \ |
488 |
| - fmt, ##__VA_ARGS__) |
| 515 | + bpf_do_trace_printk(fmt, ##__VA_ARGS__) |
489 | 516 |
|
490 | 517 | #define __BPF_ARG1_TP(...) \
|
491 | 518 | ((mod[0] == 2 || (mod[0] == 1 && __BITS_PER_LONG == 64)) \
|
@@ -522,10 +549,15 @@ static const struct bpf_func_proto bpf_trace_printk_proto = {
|
522 | 549 | const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
|
523 | 550 | {
|
524 | 551 | /*
|
525 |
| - * this program might be calling bpf_trace_printk, |
526 |
| - * so allocate per-cpu printk buffers |
| 552 | + * This program might be calling bpf_trace_printk, |
| 553 | + * so enable the associated bpf_trace/bpf_trace_printk event. |
| 554 | + * Repeat this each time as it is possible a user has |
| 555 | + * disabled bpf_trace_printk events. By loading a program |
| 556 | + * calling bpf_trace_printk() however the user has expressed |
| 557 | + * the intent to see such events. |
527 | 558 | */
|
528 |
| - trace_printk_init_buffers(); |
| 559 | + if (trace_set_clr_event("bpf_trace", "bpf_trace_printk", 1)) |
| 560 | + pr_warn_ratelimited("could not enable bpf_trace_printk events"); |
529 | 561 |
|
530 | 562 | return &bpf_trace_printk_proto;
|
531 | 563 | }
|
|
0 commit comments