Browse Source

ebpf: Add example

This ensures that macro expansion works properly and that expanded code
compiles

Signed-off-by: Dave Tucker <[email protected]>
Dave Tucker 2 years ago
parent
commit
5789585994

+ 11 - 1
aya-log/ebpf/Cargo.toml

@@ -1,2 +1,12 @@
 [workspace]
-members = ["aya-log-ebpf", "aya-log-ebpf-macros"]
+members = ["aya-log-ebpf", "aya-log-ebpf-macros", "example"]
+
+
+[profile.dev]
+panic = "abort"
+debug = 1
+opt-level = 2
+overflow-checks = false
+
+[profile.release]
+panic = "abort"

+ 1 - 1
aya-log/ebpf/aya-log-ebpf-macros/Cargo.toml

@@ -9,4 +9,4 @@ quote = "1.0"
 syn = "1.0"
 
 [lib]
-proc-macro = true
+proc-macro = true

+ 0 - 9
aya-log/ebpf/aya-log-ebpf/Cargo.toml

@@ -10,12 +10,3 @@ aya-log-ebpf-macros = { path = "../aya-log-ebpf-macros" }
 
 [lib]
 path = "src/lib.rs"
-
-[profile.dev]
-panic = "abort"
-debug = 1
-opt-level = 2
-overflow-checks = false
-
-[profile.release]
-panic = "abort"

+ 4 - 2
aya-log/ebpf/aya-log-ebpf/src/lib.rs

@@ -1,9 +1,11 @@
 #![no_std]
 use aya_bpf::{
     macros::map,
-    maps::{PerCpuArray, PerfEventByteArray}
+    maps::{PerCpuArray, PerfEventByteArray},
+};
+pub use aya_log_common::{
+    write_record_header, write_record_message, Level, WriteToBuf, LOG_BUF_CAPACITY,
 };
-pub use aya_log_common::{Level, LOG_BUF_CAPACITY, write_record_header, write_record_message, WriteToBuf};
 pub use aya_log_ebpf_macros::{debug, error, info, log, trace, warn};
 
 #[doc(hidden)]

+ 13 - 0
aya-log/ebpf/example/Cargo.toml

@@ -0,0 +1,13 @@
+[package]
+name = "example"
+version = "0.1.0"
+edition = "2018"
+publish = false
+
+[dependencies]
+aya-bpf = { git = "https://github.com/aya-rs/aya", branch = "main" }
+aya-log-ebpf = { path = "../aya-log-ebpf" }
+
+[[bin]]
+name = "example"
+path = "src/main.rs"

+ 22 - 0
aya-log/ebpf/example/src/main.rs

@@ -0,0 +1,22 @@
+#![no_std]
+#![no_main]
+
+use aya_bpf::{macros::tracepoint, programs::TracePointContext, BpfContext};
+use aya_log_ebpf::{debug, error, info, trace, warn};
+
+#[tracepoint]
+pub fn example(ctx: TracePointContext) -> u32 {
+    error!(&ctx, "this is an error message 🚨");
+    warn!(&ctx, "this is a warning message ⚠️");
+    info!(&ctx, "this is an info message ℹ️");
+    debug!(&ctx, "this is a debug message ️🐝");
+    trace!(&ctx, "this is a trace message 🔍");
+    let pid = ctx.pid();
+    info!(&ctx, "a message with args PID: {}", pid);
+    0
+}
+
+#[panic_handler]
+fn panic(_info: &core::panic::PanicInfo) -> ! {
+    unsafe { core::hint::unreachable_unchecked() }
+}