1717struct perf_inject {
1818 struct perf_tool tool ;
1919 bool build_ids ;
20+ const char * input_name ;
21+ int pipe_output ,
22+ output ;
23+ u64 bytes_written ;
2024};
2125
22- static int perf_event__repipe_synth (struct perf_tool * tool __maybe_unused ,
26+ static int perf_event__repipe_synth (struct perf_tool * tool ,
2327 union perf_event * event ,
2428 struct machine * machine __maybe_unused )
2529{
30+ struct perf_inject * inject = container_of (tool , struct perf_inject , tool );
2631 uint32_t size ;
2732 void * buf = event ;
2833
2934 size = event -> header .size ;
3035
3136 while (size ) {
32- int ret = write (STDOUT_FILENO , buf , size );
37+ int ret = write (inject -> output , buf , size );
3338 if (ret < 0 )
3439 return - errno ;
3540
3641 size -= ret ;
3742 buf += ret ;
43+ inject -> bytes_written += ret ;
3844 }
3945
4046 return 0 ;
@@ -231,12 +237,20 @@ static int __cmd_inject(struct perf_inject *inject)
231237 inject -> tool .tracing_data = perf_event__repipe_tracing_data ;
232238 }
233239
234- session = perf_session__new ("-" , O_RDONLY , false, true, & inject -> tool );
240+ session = perf_session__new (inject -> input_name , O_RDONLY , false, true, & inject -> tool );
235241 if (session == NULL )
236242 return - ENOMEM ;
237243
244+ if (!inject -> pipe_output )
245+ lseek (inject -> output , session -> header .data_offset , SEEK_SET );
246+
238247 ret = perf_session__process_events (session , & inject -> tool );
239248
249+ if (!inject -> pipe_output ) {
250+ session -> header .data_size = inject -> bytes_written ;
251+ perf_session__write_header (session , session -> evlist , inject -> output , true);
252+ }
253+
240254 perf_session__delete (session );
241255
242256 return ret ;
@@ -260,10 +274,16 @@ int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused)
260274 .tracing_data = perf_event__repipe_tracing_data_synth ,
261275 .build_id = perf_event__repipe_op2_synth ,
262276 },
277+ .input_name = "-" ,
263278 };
279+ const char * output_name = "-" ;
264280 const struct option options [] = {
265281 OPT_BOOLEAN ('b' , "build-ids" , & inject .build_ids ,
266282 "Inject build-ids into the output stream" ),
283+ OPT_STRING ('i' , "input" , & inject .input_name , "file" ,
284+ "input file name" ),
285+ OPT_STRING ('o' , "output" , & output_name , "file" ,
286+ "output file name" ),
267287 OPT_INCR ('v' , "verbose" , & verbose ,
268288 "be more verbose (show build ids, etc)" ),
269289 OPT_END ()
@@ -281,6 +301,18 @@ int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused)
281301 if (argc )
282302 usage_with_options (inject_usage , options );
283303
304+ if (!strcmp (output_name , "-" )) {
305+ inject .pipe_output = 1 ;
306+ inject .output = STDOUT_FILENO ;
307+ } else {
308+ inject .output = open (output_name , O_CREAT | O_WRONLY | O_TRUNC ,
309+ S_IRUSR | S_IWUSR );
310+ if (inject .output < 0 ) {
311+ perror ("failed to create output file" );
312+ return -1 ;
313+ }
314+ }
315+
284316 if (symbol__init () < 0 )
285317 return -1 ;
286318
0 commit comments