Skip to content
Snippets Groups Projects
builtin-script.c 26.1 KiB
Newer Older
  • Learn to ignore specific revisions
  • 			perror("failed to create pipe");
    			exit(-1);
    		}
    
    		pid = fork();
    		if (pid < 0) {
    			perror("failed to fork");
    			exit(-1);
    		}
    
    		if (!pid) {
    
    			system_wide = true;
    			j = 0;
    
    
    			if (!is_top_script(argv[0]))
    				system_wide = !have_cmd(argc - rep_args,
    							&argv[rep_args]);
    
    			__argv = malloc((argc + 6) * sizeof(const char *));
    
    			__argv[j++] = "/bin/sh";
    			__argv[j++] = rec_script_path;
    			if (system_wide)
    				__argv[j++] = "-a";
    			__argv[j++] = "-q";
    			__argv[j++] = "-o";
    			__argv[j++] = "-";
    			for (i = rep_args + 1; i < argc; i++)
    				__argv[j++] = argv[i];
    			__argv[j++] = NULL;
    
    		__argv = malloc((argc + 4) * sizeof(const char *));
    
    		j = 0;
    		__argv[j++] = "/bin/sh";
    		__argv[j++] = rep_script_path;
    		for (i = 1; i < rep_args + 1; i++)
    			__argv[j++] = argv[i];
    		__argv[j++] = "-i";
    		__argv[j++] = "-";
    		__argv[j++] = NULL;
    
    	if (rec_script_path)
    		script_path = rec_script_path;
    	if (rep_script_path)
    		script_path = rep_script_path;
    
    	if (script_path) {
    		system_wide = false;
    		j = 0;
    
    		if (rec_script_path)
    			system_wide = !have_cmd(argc - 1, &argv[1]);
    
    		__argv = malloc((argc + 2) * sizeof(const char *));
    
    		__argv[j++] = "/bin/sh";
    		__argv[j++] = script_path;
    		if (system_wide)
    			__argv[j++] = "-a";
    
    		for (i = 2; i < argc; i++)
    
    			__argv[j++] = argv[i];
    		__argv[j++] = NULL;
    
    
    		execvp("/bin/sh", (char **)__argv);
    
    	if (symbol__init() < 0)
    		return -1;
    
    	if (!script_name)
    		setup_pager();
    
    	session = perf_session__new(input_name, O_RDONLY, 0, false, &event_ops);
    
    	if (!no_callchain)
    
    		symbol_conf.use_callchain = true;
    	else
    		symbol_conf.use_callchain = false;
    
    
    	if (generate_script_lang) {
    		struct stat perf_stat;
    
    		if (output_set_by_user()) {
    
    			fprintf(stderr,
    				"custom fields not supported for generated scripts");
    			return -1;
    		}
    
    		input = open(input_name, O_RDONLY);
    
    		if (input < 0) {
    			perror("failed to open file");
    			exit(-1);
    		}
    
    		err = fstat(input, &perf_stat);
    		if (err < 0) {
    			perror("failed to stat file");
    			exit(-1);
    		}
    
    		if (!perf_stat.st_size) {
    			fprintf(stderr, "zero-sized file, nothing to do!\n");
    			exit(0);
    		}
    
    		scripting_ops = script_spec__lookup(generate_script_lang);
    		if (!scripting_ops) {
    			fprintf(stderr, "invalid language specifier");
    			return -1;
    		}
    
    
    		err = scripting_ops->generate_script("perf-script");
    
    		goto out;
    	}
    
    	if (script_name) {
    
    		err = scripting_ops->start_script(script_name, argc, argv);
    
    		if (err)
    			goto out;
    
    		pr_debug("perf script started with script %s\n\n", script_name);
    
    	err = __cmd_script(session);
    
    	cleanup_scripting();
    out:
    	return err;