@@ -16,12 +16,9 @@ use aya_obj::generated::{
1616use libc:: pid_t;
1717
1818use super :: { PerfEventIoctlRequest , Syscall , syscall} ;
19- use crate :: programs:: {
20- Pid ,
21- perf_event:: {
22- BreakpointConfig , PerfEventConfig , PerfEventScope , SamplePolicy , WakeupPolicy ,
23- perf_type_id_to_u32,
24- } ,
19+ use crate :: programs:: perf_event:: {
20+ BreakpointConfig , PerfEventConfig , PerfEventScope , SamplePolicy , WakeupPolicy ,
21+ perf_type_id_to_u32,
2522} ;
2623
2724pub ( crate ) fn perf_event_open (
@@ -126,13 +123,7 @@ pub(crate) fn perf_event_open(
126123 }
127124 }
128125
129- let ( pid, cpu) = match scope {
130- PerfEventScope :: CallingProcess { cpu } => ( 0 , cpu. map_or ( -1 , |cpu| cpu as i32 ) ) ,
131- PerfEventScope :: OneProcess { pid, cpu } => {
132- ( pid. get ( ) as i32 , cpu. map_or ( -1 , |cpu| cpu as i32 ) )
133- }
134- PerfEventScope :: AllProcessesOneCpu { cpu } => ( -1 , cpu as i32 ) ,
135- } ;
126+ let ( pid, cpu) = perf_event_scope_pid_cpu ( scope) ;
136127
137128 perf_event_sys ( attr, pid, cpu, flags)
138129}
@@ -142,7 +133,7 @@ pub(crate) fn perf_event_open_probe(
142133 ret_bit : Option < u32 > ,
143134 name : & OsStr ,
144135 offset : u64 ,
145- pid : Option < u32 > ,
136+ scope : PerfEventScope ,
146137) -> io:: Result < crate :: MockableFd > {
147138 use std:: os:: unix:: ffi:: OsStrExt as _;
148139
@@ -159,26 +150,15 @@ pub(crate) fn perf_event_open_probe(
159150 attr. __bindgen_anon_3 . config1 = c_name. as_ptr ( ) as u64 ;
160151 attr. __bindgen_anon_4 . config2 = offset;
161152
162- let ( pid, cpu) = match pid {
163- Some ( pid) => ( pid as i32 , -1 ) ,
164- None => ( -1 , 0 ) ,
165- } ;
153+ let ( pid, cpu) = perf_event_scope_pid_cpu ( scope) ;
166154
167155 perf_event_sys ( attr, pid, cpu, PERF_FLAG_FD_CLOEXEC )
168156}
169157
170158pub ( crate ) fn perf_event_open_trace_point (
171159 event_id : u64 ,
172- pid : Option < u32 > ,
160+ scope : PerfEventScope ,
173161) -> io:: Result < crate :: MockableFd > {
174- let scope = match pid {
175- None => PerfEventScope :: AllProcessesOneCpu { cpu : 0 } ,
176- Some ( 0 ) => PerfEventScope :: CallingProcess { cpu : None } ,
177- Some ( pid) => PerfEventScope :: OneProcess {
178- pid : Pid :: new ( pid) . unwrap ( ) ,
179- cpu : None ,
180- } ,
181- } ;
182162 perf_event_open (
183163 PerfEventConfig :: TracePoint { event_id } ,
184164 scope,
@@ -189,6 +169,16 @@ pub(crate) fn perf_event_open_trace_point(
189169 )
190170}
191171
172+ fn perf_event_scope_pid_cpu ( scope : PerfEventScope ) -> ( pid_t , i32 ) {
173+ match scope {
174+ PerfEventScope :: CallingProcess { cpu } => ( 0 , cpu. map_or ( -1 , |cpu| cpu as i32 ) ) ,
175+ PerfEventScope :: OneProcess { pid, cpu } => {
176+ ( pid. get ( ) as i32 , cpu. map_or ( -1 , |cpu| cpu as i32 ) )
177+ }
178+ PerfEventScope :: AllProcessesOneCpu { cpu } => ( -1 , cpu as i32 ) ,
179+ }
180+ }
181+
192182pub ( crate ) fn perf_event_ioctl (
193183 fd : BorrowedFd < ' _ > ,
194184 request : PerfEventIoctlRequest < ' _ > ,
@@ -271,15 +261,36 @@ mod tests {
271261 use test_case:: test_case;
272262
273263 use super :: { PERF_FLAG_FD_CLOEXEC , perf_event_open_trace_point} ;
274- use crate :: sys:: { Syscall , override_syscall} ;
264+ use crate :: {
265+ programs:: { Pid , perf_event:: PerfEventScope } ,
266+ sys:: { Syscall , override_syscall} ,
267+ } ;
275268
276269 const EVENT_ID : u64 = 123 ;
277270
278- #[ test_case( None , -1 , 0 ; "all_processes" ) ]
279- #[ test_case( Some ( 0 ) , 0 , -1 ; "calling_process" ) ]
280- #[ test_case( Some ( 42 ) , 42 , -1 ; "one_process" ) ]
281- fn perf_event_open_trace_point_maps_pid_scope (
282- pid : Option < u32 > ,
271+ #[ test_case(
272+ PerfEventScope :: AllProcessesOneCpu { cpu: 0 } ,
273+ -1 ,
274+ 0 ;
275+ "all_processes"
276+ ) ]
277+ #[ test_case(
278+ PerfEventScope :: CallingProcess { cpu: None } ,
279+ 0 ,
280+ -1 ;
281+ "calling_process"
282+ ) ]
283+ #[ test_case(
284+ PerfEventScope :: OneProcess {
285+ pid: Pid :: new( 42 ) . unwrap( ) ,
286+ cpu: None ,
287+ } ,
288+ 42 ,
289+ -1 ;
290+ "one_process"
291+ ) ]
292+ fn perf_event_open_trace_point_maps_scope (
293+ scope : PerfEventScope ,
283294 expected_pid : pid_t ,
284295 expected_cpu : i32 ,
285296 ) {
@@ -302,7 +313,7 @@ mod tests {
302313 call => panic ! ( "unexpected syscall: {call:?}" ) ,
303314 } ) ;
304315
305- let fd = perf_event_open_trace_point ( EVENT_ID , pid ) . unwrap ( ) ;
316+ let fd = perf_event_open_trace_point ( EVENT_ID , scope ) . unwrap ( ) ;
306317 assert_eq ! ( fd. as_raw_fd( ) , crate :: MockableFd :: mock_signed_fd( ) ) ;
307318 }
308319}
0 commit comments