6
6
//!
7
7
//! let mut iter = ProgInfoIter::default();
8
8
//! for prog in iter {
9
- //! println!("{}", prog.name);
9
+ //! println!("{}", prog.name.to_string_lossy() );
10
10
//! }
11
11
//! ```
12
12
13
- use core:: ffi:: c_void;
14
13
use std:: convert:: TryFrom ;
14
+ use std:: ffi:: c_void;
15
+ use std:: ffi:: CString ;
15
16
use std:: mem:: size_of_val;
16
17
use std:: os:: raw:: c_char;
17
- use std:: string:: String ;
18
18
use std:: time:: Duration ;
19
19
20
20
use nix:: errno;
@@ -91,19 +91,6 @@ macro_rules! gen_info_impl {
91
91
} ;
92
92
}
93
93
94
- fn name_arr_to_string ( a : & [ c_char ] , default : & str ) -> String {
95
- let converted_arr: Vec < u8 > = a
96
- . iter ( )
97
- . take_while ( |x| * * x != 0 )
98
- . map ( |x| * x as u8 )
99
- . collect ( ) ;
100
- if !converted_arr. is_empty ( ) {
101
- String :: from_utf8 ( converted_arr) . unwrap_or_else ( |_| default. to_string ( ) )
102
- } else {
103
- default. to_string ( )
104
- }
105
- }
106
-
107
94
/// BTF Line information
108
95
#[ derive( Clone , Debug ) ]
109
96
pub struct LineInfo {
@@ -131,17 +118,17 @@ impl From<&libbpf_sys::bpf_line_info> for LineInfo {
131
118
}
132
119
}
133
120
121
+ /// Bpf identifier tag
134
122
#[ derive( Debug , Clone , Default ) ]
135
123
#[ repr( C ) ]
136
- /// Bpf identifier tag
137
124
pub struct Tag ( [ u8 ; 8 ] ) ;
138
125
139
126
/// Information about a BPF program
140
127
#[ derive( Debug , Clone ) ]
141
128
// TODO: Document members.
142
129
#[ allow( missing_docs) ]
143
130
pub struct ProgramInfo {
144
- pub name : String ,
131
+ pub name : CString ,
145
132
pub ty : ProgramType ,
146
133
pub tag : Tag ,
147
134
pub id : u32 ,
@@ -169,15 +156,15 @@ pub struct ProgramInfo {
169
156
pub run_cnt : u64 ,
170
157
}
171
158
172
- #[ derive( Default , Debug ) ]
173
159
/// An iterator for the information of loaded bpf programs
160
+ #[ derive( Default , Debug ) ]
174
161
pub struct ProgInfoIter {
175
162
cur_id : u32 ,
176
163
opts : ProgInfoQueryOptions ,
177
164
}
178
165
179
- #[ derive( Clone , Default , Debug ) ]
180
166
/// Options to query the program info currently loaded
167
+ #[ derive( Clone , Default , Debug ) ]
181
168
pub struct ProgInfoQueryOptions {
182
169
/// Include the vector of bpf instructions in the result
183
170
include_xlated_prog_insns : bool ,
@@ -264,17 +251,19 @@ impl ProgInfoQueryOptions {
264
251
self
265
252
}
266
253
267
- /// Include all
254
+ /// Include everything there is in the query results
268
255
pub fn include_all ( self ) -> Self {
269
- self . include_xlated_prog_insns ( true )
270
- . include_jited_prog_insns ( true )
271
- . include_map_ids ( true )
272
- . include_line_info ( true )
273
- . include_func_info ( true )
274
- . include_jited_line_info ( true )
275
- . include_jited_func_lens ( true )
276
- . include_prog_tags ( true )
277
- . include_jited_ksyms ( true )
256
+ Self {
257
+ include_xlated_prog_insns : true ,
258
+ include_jited_prog_insns : true ,
259
+ include_map_ids : true ,
260
+ include_line_info : true ,
261
+ include_func_info : true ,
262
+ include_jited_line_info : true ,
263
+ include_jited_func_lens : true ,
264
+ include_prog_tags : true ,
265
+ include_jited_ksyms : true ,
266
+ }
278
267
}
279
268
}
280
269
@@ -299,7 +288,8 @@ impl ProgramInfo {
299
288
unsafe { libbpf_sys:: bpf_obj_get_info_by_fd ( fd, item_ptr as * mut c_void , & mut len) } ;
300
289
util:: parse_ret ( ret) ?;
301
290
302
- let name = name_arr_to_string ( & item. name , "(?)" ) ;
291
+ // SANITY: `libbpf` should guarantee NUL termination.
292
+ let name = util:: c_char_slice_to_cstr ( & item. name ) . unwrap ( ) ;
303
293
let ty = match ProgramType :: try_from ( item. type_ ) {
304
294
Ok ( ty) => ty,
305
295
Err ( _) => ProgramType :: Unknown ,
@@ -379,7 +369,7 @@ impl ProgramInfo {
379
369
util:: parse_ret ( ret) ?;
380
370
381
371
return Ok ( ProgramInfo {
382
- name,
372
+ name : name . to_owned ( ) ,
383
373
ty,
384
374
tag : Tag ( item. tag ) ,
385
375
id : item. id ,
@@ -430,7 +420,8 @@ impl Iterator for ProgInfoIter {
430
420
431
421
match prog {
432
422
Ok ( p) => return Some ( p) ,
433
- Err ( e) => eprintln ! ( "Failed to load program: {}" , e) ,
423
+ // TODO: We should consider bubbling up errors properly.
424
+ Err ( _err) => ( ) ,
434
425
}
435
426
}
436
427
}
@@ -441,7 +432,7 @@ impl Iterator for ProgInfoIter {
441
432
// TODO: Document members.
442
433
#[ allow( missing_docs) ]
443
434
pub struct MapInfo {
444
- pub name : String ,
435
+ pub name : CString ,
445
436
pub ty : MapType ,
446
437
pub id : u32 ,
447
438
pub key_size : u32 ,
@@ -459,14 +450,15 @@ pub struct MapInfo {
459
450
460
451
impl MapInfo {
461
452
fn from_uapi ( _fd : i32 , s : libbpf_sys:: bpf_map_info ) -> Option < Self > {
462
- let name = name_arr_to_string ( & s. name , "(?)" ) ;
453
+ // SANITY: `libbpf` should guarantee NUL termination.
454
+ let name = util:: c_char_slice_to_cstr ( & s. name ) . unwrap ( ) ;
463
455
let ty = match MapType :: try_from ( s. type_ ) {
464
456
Ok ( ty) => ty,
465
457
Err ( _) => MapType :: Unknown ,
466
458
} ;
467
459
468
460
Some ( Self {
469
- name,
461
+ name : name . to_owned ( ) ,
470
462
ty,
471
463
id : s. id ,
472
464
key_size : s. key_size ,
@@ -495,10 +487,9 @@ gen_info_impl!(
495
487
496
488
/// Information about BPF type format
497
489
#[ derive( Debug , Clone ) ]
498
- #[ allow( missing_docs) ]
499
490
pub struct BtfInfo {
500
491
/// The name associated with this btf information in the kernel
501
- pub name : String ,
492
+ pub name : CString ,
502
493
/// The raw btf bytes from the kernel
503
494
pub btf : Vec < u8 > ,
504
495
/// The btf id associated with this btf information in the kernel
@@ -532,7 +523,10 @@ impl BtfInfo {
532
523
util:: parse_ret ( ret) ?;
533
524
534
525
Ok ( BtfInfo {
535
- name : String :: from_utf8 ( name) . unwrap_or_else ( |_| "(?)" . to_string ( ) ) ,
526
+ // SANITY: Our buffer contained space for a NUL byte and we set its
527
+ // contents to 0. Barring a `libbpf` bug a NUL byte will be
528
+ // present.
529
+ name : CString :: from_vec_with_nul ( name) . unwrap ( ) ,
536
530
btf,
537
531
id : item. id ,
538
532
} )
@@ -568,7 +562,8 @@ impl Iterator for BtfInfoIter {
568
562
569
563
match info {
570
564
Ok ( i) => return Some ( i) ,
571
- Err ( e) => eprintln ! ( "Failed to load btf information: {}" , e) ,
565
+ // TODO: We should consider bubbling up errors properly.
566
+ Err ( _err) => ( ) ,
572
567
}
573
568
}
574
569
}
0 commit comments