@@ -12,9 +12,9 @@ mod shared;
12
12
13
13
use clap:: { Args , Parser , Subcommand } ;
14
14
use profile_json_preparse:: parse_libinfo_map_from_profile_file;
15
- use shared:: recording_props:: { ProfileCreationProps , RecordingProps } ;
15
+ use shared:: recording_props:: { ProcessLaunchProps , ProfileCreationProps , RecordingProps } ;
16
16
17
- use std:: ffi:: { OsStr , OsString } ;
17
+ use std:: ffi:: OsStr ;
18
18
use std:: fs:: File ;
19
19
use std:: io:: { BufReader , BufWriter } ;
20
20
use std:: path:: { Path , PathBuf } ;
@@ -237,6 +237,7 @@ fn main() {
237
237
238
238
#[ cfg( any( target_os = "android" , target_os = "macos" , target_os = "linux" ) ) ]
239
239
Action :: Record ( record_args) => {
240
+ let process_launch_props = record_args. process_launch_props ( ) ;
240
241
let recording_props = record_args. recording_props ( ) ;
241
242
let profile_creation_props = record_args. profile_creation_props ( ) ;
242
243
let server_props = record_args. server_props ( ) ;
@@ -249,12 +250,8 @@ fn main() {
249
250
server_props,
250
251
) ;
251
252
} else {
252
- let ( env_vars, command_name, args) = parse_command ( & record_args. command ) ;
253
253
let exit_status = match profiler:: start_recording (
254
- command_name,
255
- args,
256
- & env_vars,
257
- record_args. iteration_count ,
254
+ process_launch_props,
258
255
recording_props,
259
256
profile_creation_props,
260
257
server_props,
@@ -330,13 +327,45 @@ impl RecordArgs {
330
327
}
331
328
}
332
329
330
+ pub fn process_launch_props ( & self ) -> ProcessLaunchProps {
331
+ let command = & self . command ;
332
+ let iteration_count = self . iteration_count ;
333
+ assert ! (
334
+ !command. is_empty( ) ,
335
+ "CLI parsing should have ensured that we have at least one command name"
336
+ ) ;
337
+
338
+ let mut env_vars = Vec :: new ( ) ;
339
+ let mut i = 0 ;
340
+ while let Some ( ( var_name, var_val) ) = command. get ( i) . and_then ( |s| split_at_first_equals ( s) )
341
+ {
342
+ env_vars. push ( ( var_name. to_owned ( ) , var_val. to_owned ( ) ) ) ;
343
+ i += 1 ;
344
+ }
345
+ if i == command. len ( ) {
346
+ eprintln ! ( "Error: No command name found. Every item looks like an environment variable (contains '='): {command:?}" ) ;
347
+ std:: process:: exit ( 1 ) ;
348
+ }
349
+ let command_name = command[ i] . clone ( ) ;
350
+ let args = command[ ( i + 1 ) ..] . to_owned ( ) ;
351
+ ProcessLaunchProps {
352
+ env_vars,
353
+ command_name,
354
+ args,
355
+ iteration_count,
356
+ }
357
+ }
358
+
333
359
#[ allow( unused) ]
334
360
pub fn profile_creation_props ( & self ) -> ProfileCreationProps {
335
- let profile_name = match ( self . profile_creation_args . profile_name . clone ( ) , self . pid , self . command . first ( ) ) {
336
- ( Some ( profile_name) , _, _) => profile_name,
337
- ( None , Some ( pid) , _) => format ! ( "PID {pid}" ) ,
338
- ( None , None , Some ( command) ) => command. to_string_lossy ( ) . to_string ( ) ,
339
- ( None , None , None ) => panic ! ( "Either pid or command is guaranteed to be present (clap should have done the validation)" ) ,
361
+ let profile_name = match ( self . profile_creation_args . profile_name . clone ( ) , self . pid ) {
362
+ ( Some ( profile_name) , _) => profile_name,
363
+ ( None , Some ( pid) ) => format ! ( "PID {pid}" ) ,
364
+ _ => self
365
+ . process_launch_props ( )
366
+ . command_name
367
+ . to_string_lossy ( )
368
+ . to_string ( ) ,
340
369
} ;
341
370
ProfileCreationProps {
342
371
profile_name,
@@ -384,28 +413,6 @@ fn split_at_first_equals(s: &OsStr) -> Option<(&OsStr, &OsStr)> {
384
413
Some ( ( name, val) )
385
414
}
386
415
387
- #[ allow( unused) ]
388
- fn parse_command ( command : & [ OsString ] ) -> ( Vec < ( OsString , OsString ) > , OsString , & [ OsString ] ) {
389
- assert ! (
390
- !command. is_empty( ) ,
391
- "CLI parsing should have ensured that we have at least one command name"
392
- ) ;
393
-
394
- let mut env_vars = Vec :: new ( ) ;
395
- let mut i = 0 ;
396
- while let Some ( ( var_name, var_val) ) = command. get ( i) . and_then ( |s| split_at_first_equals ( s) ) {
397
- env_vars. push ( ( var_name. to_owned ( ) , var_val. to_owned ( ) ) ) ;
398
- i += 1 ;
399
- }
400
- if i == command. len ( ) {
401
- eprintln ! ( "Error: No command name found. Every item looks like an environment variable (contains '='): {command:?}" ) ;
402
- std:: process:: exit ( 1 ) ;
403
- }
404
- let command_name = command[ i] . clone ( ) ;
405
- let args = & command[ ( i + 1 ) ..] ;
406
- ( env_vars, command_name, args)
407
- }
408
-
409
416
fn convert_file_to_profile (
410
417
filename : & Path ,
411
418
input_file : & File ,
0 commit comments