• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
4  *
5  */
6 #define _LARGEFILE64_SOURCE
7 #include <dirent.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <getopt.h>
12 #include <stdarg.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <sys/wait.h>
16 #include <sys/mman.h>
17 #include <fcntl.h>
18 #include <signal.h>
19 #include <unistd.h>
20 #include <ctype.h>
21 #include <errno.h>
22 
23 #include "trace-local.h"
24 
create_output(const char * file,const char * tracing_dir,const char * kallsyms)25 static struct tracecmd_output *create_output(const char *file,
26 					     const char *tracing_dir, const char *kallsyms)
27 {
28 	struct tracecmd_output *out;
29 
30 	out = tracecmd_output_create(file);
31 	if (!out)
32 		goto error;
33 
34 	if (tracing_dir && tracecmd_output_set_trace_dir(out, tracing_dir))
35 		goto error;
36 	if (kallsyms && tracecmd_output_set_kallsyms(out, kallsyms))
37 		goto error;
38 	if (tracecmd_output_write_headers(out, NULL))
39 		goto error;
40 	return out;
41 error:
42 	if (out)
43 		tracecmd_output_close(out);
44 	unlink(file);
45 	return NULL;
46 }
47 
trace_restore(int argc,char ** argv)48 void trace_restore (int argc, char **argv)
49 {
50 	struct tracecmd_output *handle;
51 	const char *output_file = DEFAULT_INPUT_FILE;
52 	const char *output = NULL;
53 	const char *input = NULL;
54 	const char *tracing_dir = NULL;
55 	const char *kallsyms = NULL;
56 	struct stat st1;
57 	struct stat st2;
58 	int first_arg;
59 	int create_only = 0;
60 	int args;
61 	int c;
62 
63 	if (argc < 2)
64 		usage(argv);
65 
66 	if (strcmp(argv[1], "restore") != 0)
67 		usage(argv);
68 
69 	while ((c = getopt(argc-1, argv+1, "+hco:i:t:k:")) >= 0) {
70 		switch (c) {
71 		case 'h':
72 			usage(argv);
73 			break;
74 		case 'c':
75 			if (input)
76 				die("-c and -i are incompatible");
77 			create_only = 1;
78 			/* make output default to partial */
79 			output_file = "trace-partial.dat";
80 			break;
81 
82 		case 't':
83 			tracing_dir = optarg;
84 			break;
85 		case 'k':
86 			kallsyms = optarg;
87 			break;
88 		case 'o':
89 			if (output)
90 				die("only one output file allowed");
91 			output = optarg;
92 			break;
93 
94 		case 'i':
95 			if (input)
96 				die("only one input file allowed");
97 			if (create_only)
98 				die("-c and -i are incompatible");
99 			input = optarg;
100 			break;
101 
102 		default:
103 			usage(argv);
104 		}
105 	}
106 
107 	if (!output)
108 		output = output_file;
109 
110 	if ((argc - optind) <= 1) {
111 		if (!create_only) {
112 			warning("No data files found");
113 			usage(argv);
114 		}
115 
116 		handle = create_output(output, tracing_dir, kallsyms);
117 		if (!handle)
118 			die("Unabled to create output file %s", output);
119 		if (tracecmd_write_cmdlines(handle) < 0)
120 			die("Failed to write command lines");
121 		tracecmd_output_close(handle);
122 		exit(0);
123 	}
124 	first_arg = optind + 1;
125 	args = argc - first_arg;
126 	printf("first = %d %s args=%d\n", first_arg, argv[first_arg], args);
127 
128 	/* Make sure input and output are not the same file */
129 	if (input && output) {
130 		if (stat(input, &st1) < 0)
131 			die("%s:", input);
132 		/* output exists? otherwise we don't care */
133 		if (stat(output, &st2) == 0) {
134 			if (st1.st_ino == st2.st_ino &&
135 			    st1.st_dev == st2.st_dev)
136 				die("input and output file are the same");
137 		}
138 	}
139 
140 	if (input) {
141 		struct tracecmd_input *ihandle;
142 
143 		ihandle = tracecmd_alloc(input, 0);
144 		if (!ihandle)
145 			die("error reading file %s", input);
146 		/* make sure headers are ok */
147 		if (tracecmd_read_headers(ihandle, TRACECMD_FILE_CMD_LINES) < 0)
148 			die("error reading file %s headers", input);
149 
150 		handle = tracecmd_copy(ihandle, output, TRACECMD_FILE_CMD_LINES, 0, NULL);
151 		tracecmd_close(ihandle);
152 	} else {
153 		handle = tracecmd_output_create(output);
154 		tracecmd_output_write_headers(handle, NULL);
155 	}
156 
157 	if (!handle)
158 		die("error writing to %s", output);
159 
160 	if (tracecmd_append_cpu_data(handle, args, &argv[first_arg]) < 0)
161 		die("failed to append data");
162 
163 	return;
164 }
165