Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logger callbacks #284

Merged
merged 3 commits into from
Feb 20, 2023
Merged

Conversation

geyslan
Copy link
Member

@geyslan geyslan commented Jan 10, 2023

commit 761bb03

selftest: add log-callbacks

commit 56803f4

logger: introduce logger callbacks

loggerCallback() calls callbacks, log() and []logFnFilters(),
which can be set by the libbpfgo consumer via SetLoggerCbs().

This moves output filtering from C libbpf_print_fn() to
Go filterOutput() which can be set as one of the slice of filter funcs.

This also introduces LogWarnLevel, LogInfoLevel and LogDebugLevel
constants.

Fixes: #276
Fixes: #6
Fixes: #55
Fixex: aquasecurity/tracee#2417

Related fix during the work:

Tests

libbpfgo/selftest/log-callbacks on  2417-logger-callback [$!?]
❯ make
make -C /home/gg/code/libbpfgo libbpfgo-static
make[1]: Entering directory '/home/gg/code/libbpfgo'
CC=clang \
        CGO_CFLAGS="-I/home/gg/code/libbpfgo/output" \
        CGO_LDFLAGS="-lelf -lz /home/gg/code/libbpfgo/output/libbpf.a" \
        GOOS=linux GOARCH=amd64 \
        go build \
        -tags netgo -ldflags '-w -extldflags "-static"' \
        .
...

libbpfgo/selftest/log-callbacks on  2417-logger-callback [$!?]
❯ sudo ./main-static                     

libbpfgo/selftest/log-callbacks on  2417-logger-callback [$!?]
❯ echo $?                                
0

Changing one filter callback for tests purpose only (== instead of !=):

func(libLevel int, msg string) bool {
    return libLevel == C.LIBBPF_WARN
},
tracee on  main [$!?⇡] via 🐹 v1.19.5 via ⍱ v2.3.4 took 3s 
❯ sudo ./dist/tracee -t uid=1000 -t comm=who --log warn 2>&1 | head -20
{"level":"warn","ts":1675175312.2060382,"msg":"libbpf: loading object 'embedded-core' from buffer"}
{"level":"warn","ts":1675175312.2061195,"msg":"libbpf: elf: section(3) raw_tracepoint/sys_enter, size 248, link 0, flags 6, type=1"}
{"level":"warn","ts":1675175312.2061718,"msg":"libbpf: sec 'raw_tracepoint/sys_enter': found program 'tracepoint__raw_syscalls__sys_enter' at insn offset 0 (0 bytes), code size 31 insns (248 bytes)"}
{"level":"warn","ts":1675175312.2062,"msg":"libbpf: elf: section(4) .relraw_tracepoint/sys_enter, size 32, link 223, flags 40, type=9"}
{"level":"warn","ts":1675175312.2062263,"msg":"libbpf: elf: section(5) raw_tracepoint/sys_enter_init, size 2368, link 0, flags 6, type=1"}
{"level":"warn","ts":1675175312.2062707,"msg":"libbpf: sec 'raw_tracepoint/sys_enter_init': found program 'sys_enter_init' at insn offset 0 (0 bytes), code size 296 insns (2368 bytes)"}
{"level":"warn","ts":1675175312.206299,"msg":"libbpf: elf: section(6) .relraw_tracepoint/sys_enter_init, size 160, link 223, flags 40, type=9"}
{"level":"warn","ts":1675175312.206323,"msg":"libbpf: elf: section(7) raw_tracepoint/sys_enter_submit, size 24904, link 0, flags 6, type=1"}
{"level":"warn","ts":1675175312.2063725,"msg":"libbpf: sec 'raw_tracepoint/sys_enter_submit': found program 'sys_enter_submit' at insn offset 0 (0 bytes), code size 3113 insns (24904 bytes)"}
{"level":"warn","ts":1675175312.2064219,"msg":"libbpf: elf: section(8) .relraw_tracepoint/sys_enter_submit, size 416, link 223, flags 40, type=9"}
{"level":"warn","ts":1675175312.2064548,"msg":"libbpf: elf: section(9) raw_tracepoint/sys_exit, size 312, link 0, flags 6, type=1"}
{"level":"warn","ts":1675175312.2065027,"msg":"libbpf: sec 'raw_tracepoint/sys_exit': found program 'tracepoint__raw_syscalls__sys_exit' at insn offset 0 (0 bytes), code size 39 insns (312 bytes)"}
{"level":"warn","ts":1675175312.2065287,"msg":"libbpf: elf: section(10) .relraw_tracepoint/sys_exit, size 32, link 223, flags 40, type=9"}
{"level":"warn","ts":1675175312.2065518,"msg":"libbpf: elf: section(11) raw_tracepoint/sys_exit_init, size 1200, link 0, flags 6, type=1"}
{"level":"warn","ts":1675175312.2065969,"msg":"libbpf: sec 'raw_tracepoint/sys_exit_init': found program 'sys_exit_init' at insn offset 0 (0 bytes), code size 150 insns (1200 bytes)"}
{"level":"warn","ts":1675175312.2066414,"msg":"libbpf: elf: section(12) .relraw_tracepoint/sys_exit_init, size 144, link 223, flags 40, type=9"}
{"level":"warn","ts":1675175312.2066822,"msg":"libbpf: elf: section(13) raw_tracepoint/sys_exit_submit, size 18976, link 0, flags 6, type=1"}
{"level":"warn","ts":1675175312.2067318,"msg":"libbpf: sec 'raw_tracepoint/sys_exit_submit': found program 'sys_exit_submit' at insn offset 0 (0 bytes), code size 2372 insns (18976 bytes)"}
{"level":"warn","ts":1675175312.2067814,"msg":"libbpf: elf: section(14) .relraw_tracepoint/sys_exit_submit, size 416, link 223, flags 40, type=9"}
{"level":"warn","ts":1675175312.206826,"msg":"libbpf: elf: section(15) raw_tracepoint/trace_sys_enter, size 8120, link 0, flags 6, type=1"}

Context

@geyslan geyslan added the feature New feature or request label Jan 10, 2023
@geyslan geyslan self-assigned this Jan 10, 2023
@geyslan geyslan marked this pull request as ready for review January 10, 2023 14:09
@geyslan geyslan marked this pull request as draft January 11, 2023 13:24
@geyslan geyslan force-pushed the 2417-logger-callback branch 2 times, most recently from 312bb98 to 945a88c Compare January 11, 2023 17:28
@geyslan geyslan marked this pull request as ready for review January 11, 2023 17:31
logger_cb.go Outdated Show resolved Hide resolved
logger_cb.go Outdated Show resolved Hide resolved
@rafaeldtinoco rafaeldtinoco self-requested a review January 18, 2023 12:38
logger_cb.go Outdated Show resolved Hide resolved
logger_cb.go Outdated Show resolved Hide resolved
rafaeldtinoco
rafaeldtinoco previously approved these changes Jan 18, 2023
Copy link
Contributor

@rafaeldtinoco rafaeldtinoco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM after the small changes I requested (if you agree).

@geyslan geyslan marked this pull request as draft January 27, 2023 14:28
@geyslan
Copy link
Member Author

geyslan commented Jan 27, 2023

LGTM after the small changes I requested (if you agree).

@rafaeldtinoco thanks for reviewing this.

As aquasecurity/tracee#2600 (review) concluded, we'll tackle this creating a libbpfgo.Logger interface.

Returing to this soon.

@geyslan
Copy link
Member Author

geyslan commented Jan 31, 2023

Despite having passed all the tests, I need to define the callbacks on them to make them silent.

logger_cb.go Outdated Show resolved Hide resolved
mozillazg
mozillazg previously approved these changes Feb 5, 2023
Copy link
Contributor

@mozillazg mozillazg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@NDStrahilevitz NDStrahilevitz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please give an example of how this is used with different settings and what they entail? Because i'm not sure how this looks work out in the end.
I had to look at the equivalent tracee PR to understand the first setting, and I have no idea how the filtering would work, maybe tests could help here.

logger_cb.go Outdated Show resolved Hide resolved
logger_cb.go Outdated Show resolved Hide resolved
logger_cb.go Outdated Show resolved Hide resolved
logger_cb.go Outdated Show resolved Hide resolved
logger_cb.go Outdated Show resolved Hide resolved
logger_cb_test.go Show resolved Hide resolved
logger_cb_test.go Show resolved Hide resolved
@rafaeldtinoco
Copy link
Contributor

rafaeldtinoco commented Feb 20, 2023

@geyslan minor nits and then we merge. LGTM. Please link the issue to the comment (about making the log filter function generic). BTW, make sure to address comment from @NDStrahilevitz about the regex compilation first.

@geyslan
Copy link
Member Author

geyslan commented Feb 20, 2023

After merging this and aquasecurity/tracee#2663, I'll open an issue in both projects to tackle the hard-coded filters move.

loggerCallback() calls callbacks, log() and []logFnFilters(),
which can be set by the libbpfgo consumer via SetLoggerCbs().

This moves output filtering from C libbpf_print_fn() to
Go filterOutput() which can be set as one of the slice of filter funcs.

This also introduces LogWarnLevel, LogInfoLevel and LogDebugLevel
constants.
Copy link
Contributor

@rafaeldtinoco rafaeldtinoco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@rafaeldtinoco rafaeldtinoco merged commit 8f83f25 into aquasecurity:main Feb 20, 2023
@rafaeldtinoco
Copy link
Contributor

Feel free to undraft tracee's change and move on there. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
5 participants