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

bpf_printk: breaking skeleton generation #155

Closed
mimullin-bbry opened this issue Dec 7, 2021 · 2 comments
Closed

bpf_printk: breaking skeleton generation #155

mimullin-bbry opened this issue Dec 7, 2021 · 2 comments

Comments

@mimullin-bbry
Copy link
Contributor

mimullin-bbry commented Dec 7, 2021

The following patch breaks compilation of examples

diff --git a/examples/runqslower/src/bpf/runqslower.bpf.c b/examples/runqslower/src/bpf/runqslower.bpf.c
index cf821da..eca67ff 100644
--- a/examples/runqslower/src/bpf/runqslower.bpf.c
+++ b/examples/runqslower/src/bpf/runqslower.bpf.c
@@ -56,6 +56,7 @@ int handle__sched_wakeup(u64 *ctx)
        /* TP_PROTO(struct task_struct *p) */
        struct task_struct *p = (void *)ctx[0];

+       bpf_printk("this causes an error, but shouldn't");
        return trace_enqueue(p->tgid, p->pid);
 }

The following is observed during a cargo build


   Compiling capable v0.1.0 (/code/libbpf-rs-mainline/examples/capable)
error: failed to run custom build command for `runqslower v0.1.0 (/code/libbpf-rs-mainline/examples/runqslower)`

Caused by:
  process didn't exit successfully: `/code/libbpf-rs-mainline/target/debug/build/runqslower-b81ac9d4cca6e1bc/build-script-build` (exit status: 101)
  --- stderr
  Warning: unrecognized map: .maps
  Warning: unrecognized map: license
  error: expected `:`, found `.`
     --> <stdin>:167:29
      |
  167 |     pub handle__sched_wakeup.____fmt: [i8; 36],
      |                             ^ expected `:`

  thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Generate("Failed to generate skeleton for /tmp/.tmpizMHT9/runqslower.o: Failed to rustfmt: exit status: 1")', examples/runqslower/build.rs:19:47
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...
error: build failed
@mimullin-bbry
Copy link
Contributor Author

Additional Info.

Adding the following patch solves the problem.

diff --git a/examples/runqslower/build.rs b/examples/runqslower/build.rs
index 59f3ddb..b164db3 100644
--- a/examples/runqslower/build.rs
+++ b/examples/runqslower/build.rs
@@ -16,6 +16,6 @@ fn main() {
     // all up.
     create_dir_all("./src/bpf/.output").unwrap();
     let skel = Path::new("./src/bpf/.output/runqslower.skel.rs");
-    SkeletonBuilder::new(SRC).generate(&skel).unwrap();
+    SkeletonBuilder::new(SRC).clang_args("-DBPF_NO_GLOBAL_DATA").generate(&skel).unwrap();
     println!("cargo:rerun-if-changed={}", SRC);
 }

Using BPF_NO_GLOBAL_DATA as default when doing a SkeletonBuilder, or clearly documenting that users must enable BPF_NO_GLOBAL_DATA when using bpf_printk (and limiting to printing three variables). Might be a temporary work around.

A naive solution is to apply the following diff.


diff --git a/libbpf-cargo/src/btf/btf.rs b/libbpf-cargo/src/btf/btf.rs
index ae86bff..2b783ce 100644
--- a/libbpf-cargo/src/btf/btf.rs
+++ b/libbpf-cargo/src/btf/btf.rs
@@ -604,7 +604,10 @@ impl<'a> Btf<'a> {
                     for datasec_var in &t.vars {
                         let var = match self.type_by_id(datasec_var.type_id)? {
                             BtfType::Var(v) => {
-                                if let Some(next_ty_id) = next_type(v.type_id)? {
+                                if v.name.contains(".____fmt") || v.name.contains(".___fmt") {
+                                    println!("skip the bpf_printk error");
+                                    continue;
+                                } else if let Some(next_ty_id) = next_type(v.type_id)? {
                                     dependent_types.push(next_ty_id);
                                 }

But this is super kludgy.

@anakryiko
Copy link
Member

libbpf-rs should skip generating fields for static variables. That's what we did for libbpf skeletons a while ago and mandate that only global variables make it to the skeleton.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants