Skip to content

Commit b86fd8c

Browse files
d-e-s-oinsearchoflosttime
authored andcommitted
Provide Debug impl for remaining public types
It is generally considered good practice to implement Debug on all public types of a crate [[0]][0] [[1]][1], in order to facilitate easier debugging by users. Let's do just that for our remaining types that don't do it yet. [0] https://rust-lang.github.io/api-guidelines/debuggability.html [1] https://rust-lang.github.io/api-guidelines/checklist.html Closes: #260 Signed-off-by: Daniel Müller <deso@posteo.net>
1 parent edfa059 commit b86fd8c

File tree

7 files changed

+61
-20
lines changed

7 files changed

+61
-20
lines changed

libbpf-rs/src/link.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::fmt::Debug;
12
use std::path::{Path, PathBuf};
23

34
use crate::*;
@@ -6,7 +7,7 @@ use crate::*;
67
///
78
/// This struct is used to model ownership. The underlying program will be detached
89
/// when this object is dropped if nothing else is holding a reference count.
9-
#[allow(missing_debug_implementations)]
10+
#[derive(Debug)]
1011
pub struct Link {
1112
ptr: *mut libbpf_sys::bpf_link,
1213
}
@@ -48,7 +49,7 @@ impl Link {
4849
/// Release "ownership" of underlying BPF resource (typically, a BPF program
4950
/// attached to some BPF hook, e.g., tracepoint, kprobe, etc). Disconnected
5051
/// links, when destructed through bpf_link__destroy() call won't attempt to
51-
/// detach/unregisted that BPF resource. This is useful in situations where,
52+
/// detach/unregistered that BPF resource. This is useful in situations where,
5253
/// say, attached BPF program has to outlive userspace program that attached it
5354
/// in the system. Depending on type of BPF program, though, there might be
5455
/// additional steps (like pinning BPF program in BPF FS) necessary to ensure

libbpf-rs/src/map.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use core::ffi::c_void;
22
use std::convert::TryFrom;
33
use std::ffi::CStr;
4+
use std::fmt::Debug;
45
use std::path::Path;
56
use std::ptr;
67
use std::ptr::null;
@@ -18,7 +19,7 @@ use crate::*;
1819
///
1920
/// Some methods require working with raw bytes. You may find libraries such as
2021
/// [`plain`](https://crates.io/crates/plain) helpful.
21-
#[allow(missing_debug_implementations)]
22+
#[derive(Debug)]
2223
pub struct OpenMap {
2324
ptr: *mut libbpf_sys::bpf_map,
2425
}
@@ -145,7 +146,7 @@ impl OpenMap {
145146
///
146147
/// Some methods require working with raw bytes. You may find libraries such as
147148
/// [`plain`](https://crates.io/crates/plain) helpful.
148-
#[allow(missing_debug_implementations)]
149+
#[derive(Debug)]
149150
pub struct Map {
150151
fd: i32,
151152
name: String,
@@ -668,7 +669,7 @@ impl MapType {
668669
}
669670
}
670671

671-
#[allow(missing_debug_implementations)]
672+
#[derive(Debug)]
672673
pub struct MapKeyIter<'a> {
673674
map: &'a Map,
674675
prev: Option<Vec<u8>>,

libbpf-rs/src/object.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl ObjectBuilder {
115115
/// Represents an opened (but not yet loaded) BPF object file.
116116
///
117117
/// Use this object to access [`OpenMap`]s and [`OpenProgram`]s.
118-
#[allow(missing_debug_implementations)]
118+
#[derive(Debug)]
119119
pub struct OpenObject {
120120
ptr: *mut libbpf_sys::bpf_object,
121121
maps: HashMap<String, OpenMap>,
@@ -290,7 +290,7 @@ impl Drop for OpenObject {
290290
///
291291
/// Note that this is an explanation of the motivation -- Rust's lifetime system should already be
292292
/// enforcing this invariant.
293-
#[allow(missing_debug_implementations)]
293+
#[derive(Debug)]
294294
pub struct Object {
295295
ptr: *mut libbpf_sys::bpf_object,
296296
maps: HashMap<String, Map>,

libbpf-rs/src/perf_buffer.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use core::ffi::c_void;
22
use std::boxed::Box;
3+
use std::fmt::Debug;
4+
use std::fmt::Formatter;
5+
use std::fmt::Result as FmtResult;
36
use std::slice;
47
use std::time::Duration;
58

@@ -14,14 +17,22 @@ impl<T> SampleCb for T where T: FnMut(i32, &[u8]) {}
1417
pub trait LostCb: FnMut(i32, u64) {}
1518
impl<T> LostCb for T where T: FnMut(i32, u64) {}
1619

17-
#[allow(missing_debug_implementations)]
1820
struct CbStruct<'b> {
1921
sample_cb: Option<Box<dyn SampleCb + 'b>>,
2022
lost_cb: Option<Box<dyn LostCb + 'b>>,
2123
}
2224

25+
impl Debug for CbStruct<'_> {
26+
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
27+
let Self { sample_cb, lost_cb } = self;
28+
f.debug_struct("CbStruct")
29+
.field("sample_cb", &sample_cb.as_ref().map(|cb| &cb as *const _))
30+
.field("lost_cb", &lost_cb.as_ref().map(|cb| &cb as *const _))
31+
.finish()
32+
}
33+
}
34+
2335
/// Builds [`PerfBuffer`] instances.
24-
#[allow(missing_debug_implementations)]
2536
pub struct PerfBufferBuilder<'a, 'b> {
2637
map: &'a Map,
2738
pages: usize,
@@ -148,9 +159,26 @@ impl<'a, 'b> PerfBufferBuilder<'a, 'b> {
148159
}
149160
}
150161

162+
impl Debug for PerfBufferBuilder<'_, '_> {
163+
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
164+
let Self {
165+
map,
166+
pages,
167+
sample_cb,
168+
lost_cb,
169+
} = self;
170+
f.debug_struct("PerfBufferBuilder")
171+
.field("map", map)
172+
.field("pages", pages)
173+
.field("sample_cb", &sample_cb.as_ref().map(|cb| &cb as *const _))
174+
.field("lost_cb", &lost_cb.as_ref().map(|cb| &cb as *const _))
175+
.finish()
176+
}
177+
}
178+
151179
/// Represents a special kind of [`Map`]. Typically used to transfer data between
152180
/// [`Program`]s and userspace.
153-
#[allow(missing_debug_implementations)]
181+
#[derive(Debug)]
154182
pub struct PerfBuffer<'b> {
155183
ptr: *mut libbpf_sys::perf_buffer,
156184
// Hold onto the box so it'll get dropped when PerfBuffer is dropped

libbpf-rs/src/program.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::*;
1212
/// Represents a parsed but not yet loaded BPF program.
1313
///
1414
/// This object exposes operations that need to happen before the program is loaded.
15-
#[allow(missing_debug_implementations)]
15+
#[derive(Debug)]
1616
pub struct OpenProgram {
1717
ptr: *mut libbpf_sys::bpf_program,
1818
section: String,
@@ -258,7 +258,7 @@ pub enum ProgramAttachType {
258258
///
259259
/// If you attempt to attach a `Program` with the wrong attach method, the `attach_*`
260260
/// method will fail with the appropriate error.
261-
#[allow(missing_debug_implementations)]
261+
#[derive(Debug)]
262262
pub struct Program {
263263
pub(crate) ptr: *mut libbpf_sys::bpf_program,
264264
name: String,

libbpf-rs/src/ringbuf.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
use core::ffi::c_void;
22
use std::boxed::Box;
3+
use std::fmt::Debug;
4+
use std::fmt::Formatter;
5+
use std::fmt::Result as FmtResult;
6+
use std::ops::Deref as _;
37
use std::os::raw::c_ulong;
48
use std::ptr;
59
use std::slice;
@@ -9,7 +13,6 @@ use crate::*;
913

1014
type Cb<'a> = Box<dyn FnMut(&[u8]) -> i32 + 'a>;
1115

12-
#[allow(missing_debug_implementations)]
1316
struct RingBufferCallback<'a> {
1417
cb: Cb<'a>,
1518
}
@@ -23,13 +26,21 @@ impl<'a> RingBufferCallback<'a> {
2326
}
2427
}
2528

29+
impl Debug for RingBufferCallback<'_> {
30+
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
31+
let Self { cb } = self;
32+
f.debug_struct("RingBufferCallback")
33+
.field("cb", &(cb.deref() as *const _))
34+
.finish()
35+
}
36+
}
37+
2638
/// Builds [`RingBuffer`] instances.
2739
///
2840
/// `ringbuf`s are a special kind of [`Map`], used to transfer data between
2941
/// [`Program`]s and userspace. As of Linux 5.8, the `ringbuf` map is now
3042
/// preferred over the `perf buffer`.
31-
#[derive(Default)]
32-
#[allow(missing_debug_implementations)]
43+
#[derive(Debug, Default)]
3344
pub struct RingBufferBuilder<'a> {
3445
fd_callbacks: Vec<(i32, RingBufferCallback<'a>)>,
3546
}
@@ -123,7 +134,7 @@ impl<'a> RingBufferBuilder<'a> {
123134
/// `ringbuf`s are a special kind of [`Map`], used to transfer data between
124135
/// [`Program`]s and userspace. As of Linux 5.8, the `ringbuf` map is now
125136
/// preferred over the `perf buffer`.
126-
#[allow(missing_debug_implementations)]
137+
#[derive(Debug)]
127138
pub struct RingBuffer<'a> {
128139
ptr: *mut libbpf_sys::ring_buffer,
129140
#[allow(clippy::vec_box)]

libbpf-rs/src/skeleton.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@ use libbpf_sys::{
1414
use crate::util;
1515
use crate::*;
1616

17-
#[allow(missing_debug_implementations)]
17+
#[derive(Debug)]
1818
struct MapSkelConfig {
1919
name: String,
2020
p: Box<*mut bpf_map>,
2121
mmaped: Option<Box<*mut c_void>>,
2222
}
2323

24-
#[allow(missing_debug_implementations)]
24+
#[derive(Debug)]
2525
struct ProgSkelConfig {
2626
name: String,
2727
p: Box<*mut bpf_program>,
2828
link: Box<*mut bpf_link>,
2929
}
3030

31-
#[allow(missing_debug_implementations)]
31+
#[derive(Debug)]
3232
pub struct ObjectSkeletonConfigBuilder<'a> {
3333
data: &'a [u8],
3434
p: Box<*mut bpf_object>,
@@ -206,7 +206,7 @@ impl<'a> ObjectSkeletonConfigBuilder<'a> {
206206
/// * free any allocated memory on drop
207207
///
208208
/// This struct can be moved around at will. Upon drop, all allocated resources will be freed
209-
#[allow(missing_debug_implementations)]
209+
#[derive(Debug)]
210210
pub struct ObjectSkeletonConfig<'a> {
211211
inner: bpf_object_skeleton,
212212
obj: Box<*mut bpf_object>,

0 commit comments

Comments
 (0)