From 516121a462666acefc8383a70ed74293d980a375 Mon Sep 17 00:00:00 2001
From: Dave Tucker <dave@dtucker.co.uk>
Date: Mon, 6 Jun 2022 18:48:05 +0100
Subject: [PATCH] ebpf: Add example

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

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
---
 ebpf/Cargo.toml                     | 12 +++++++++++-
 ebpf/aya-log-ebpf-macros/Cargo.toml |  2 +-
 ebpf/aya-log-ebpf/Cargo.toml        |  9 ---------
 ebpf/aya-log-ebpf/src/lib.rs        |  6 ++++--
 ebpf/example/Cargo.toml             | 13 +++++++++++++
 ebpf/example/src/main.rs            | 22 ++++++++++++++++++++++
 6 files changed, 51 insertions(+), 13 deletions(-)
 create mode 100644 ebpf/example/Cargo.toml
 create mode 100644 ebpf/example/src/main.rs

diff --git a/ebpf/Cargo.toml b/ebpf/Cargo.toml
index 4a122eef..d29c15e0 100644
--- a/ebpf/Cargo.toml
+++ b/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"
diff --git a/ebpf/aya-log-ebpf-macros/Cargo.toml b/ebpf/aya-log-ebpf-macros/Cargo.toml
index 8e0262b0..9247eb82 100644
--- a/ebpf/aya-log-ebpf-macros/Cargo.toml
+++ b/ebpf/aya-log-ebpf-macros/Cargo.toml
@@ -9,4 +9,4 @@ quote = "1.0"
 syn = "1.0"
 
 [lib]
-proc-macro = true
\ No newline at end of file
+proc-macro = true
diff --git a/ebpf/aya-log-ebpf/Cargo.toml b/ebpf/aya-log-ebpf/Cargo.toml
index 9ee4e548..5b716747 100644
--- a/ebpf/aya-log-ebpf/Cargo.toml
+++ b/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"
\ No newline at end of file
diff --git a/ebpf/aya-log-ebpf/src/lib.rs b/ebpf/aya-log-ebpf/src/lib.rs
index b0da7f7f..6faa949d 100644
--- a/ebpf/aya-log-ebpf/src/lib.rs
+++ b/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)]
diff --git a/ebpf/example/Cargo.toml b/ebpf/example/Cargo.toml
new file mode 100644
index 00000000..e78d7049
--- /dev/null
+++ b/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"
diff --git a/ebpf/example/src/main.rs b/ebpf/example/src/main.rs
new file mode 100644
index 00000000..b43b5364
--- /dev/null
+++ b/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() }
+}