diff options
Diffstat (limited to 'trace-restore.c')
-rw-r--r-- | trace-restore.c | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/trace-restore.c b/trace-restore.c new file mode 100644 index 0000000..cf8c14f --- /dev/null +++ b/trace-restore.c | |||
@@ -0,0 +1,146 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com> | ||
3 | * | ||
4 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License as published by | ||
8 | * the Free Software Foundation; version 2 of the License (not later!) | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with this program; if not, write to the Free Software | ||
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
18 | * | ||
19 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
20 | */ | ||
21 | #define _LARGEFILE64_SOURCE | ||
22 | #define _GNU_SOURCE | ||
23 | #include <dirent.h> | ||
24 | #include <stdio.h> | ||
25 | #include <stdlib.h> | ||
26 | #include <string.h> | ||
27 | #include <getopt.h> | ||
28 | #include <stdarg.h> | ||
29 | #include <sys/types.h> | ||
30 | #include <sys/stat.h> | ||
31 | #include <sys/wait.h> | ||
32 | #include <sys/mman.h> | ||
33 | #include <pthread.h> | ||
34 | #include <fcntl.h> | ||
35 | #include <signal.h> | ||
36 | #include <unistd.h> | ||
37 | #include <ctype.h> | ||
38 | #include <errno.h> | ||
39 | |||
40 | #include "trace-local.h" | ||
41 | |||
42 | void trace_restore (int argc, char **argv) | ||
43 | { | ||
44 | struct tracecmd_output *handle; | ||
45 | const char *output_file = "trace.dat"; | ||
46 | const char *output = NULL; | ||
47 | const char *input = NULL; | ||
48 | struct stat st1; | ||
49 | struct stat st2; | ||
50 | int first_arg; | ||
51 | int create_only = 0; | ||
52 | int args; | ||
53 | int c; | ||
54 | |||
55 | if (argc < 2) | ||
56 | usage(argv); | ||
57 | |||
58 | if (strcmp(argv[1], "restore") != 0) | ||
59 | usage(argv); | ||
60 | |||
61 | while ((c = getopt(argc-1, argv+1, "+hco:i:")) >= 0) { | ||
62 | switch (c) { | ||
63 | case 'h': | ||
64 | usage(argv); | ||
65 | break; | ||
66 | case 'c': | ||
67 | if (input) | ||
68 | die("-c and -i are incompatible"); | ||
69 | create_only = 1; | ||
70 | /* make output default to partial */ | ||
71 | output_file = "trace-partial.dat"; | ||
72 | break; | ||
73 | |||
74 | case 'o': | ||
75 | if (output) | ||
76 | die("only one output file allowed"); | ||
77 | output = optarg; | ||
78 | break; | ||
79 | |||
80 | case 'i': | ||
81 | if (input) | ||
82 | die("only one input file allowed"); | ||
83 | if (create_only) | ||
84 | die("-c and -i are incompatible"); | ||
85 | input = optarg; | ||
86 | break; | ||
87 | |||
88 | default: | ||
89 | usage(argv); | ||
90 | } | ||
91 | } | ||
92 | |||
93 | if (!output) | ||
94 | output = output_file; | ||
95 | |||
96 | if ((argc - optind) <= 1) { | ||
97 | if (!create_only) { | ||
98 | warning("No data files found"); | ||
99 | usage(argv); | ||
100 | } | ||
101 | |||
102 | handle = tracecmd_create_init_file(output); | ||
103 | if (!handle) | ||
104 | die("Unabled to create output file %s", output); | ||
105 | tracecmd_output_close(handle); | ||
106 | exit(0); | ||
107 | } | ||
108 | first_arg = optind + 1; | ||
109 | args = argc - first_arg; | ||
110 | printf("first = %d %s args=%d\n", first_arg, argv[first_arg], args); | ||
111 | |||
112 | /* Make sure input and output are not the same file */ | ||
113 | if (input && output) { | ||
114 | if (stat(input, &st1) < 0) | ||
115 | die("%s:", input); | ||
116 | /* output exists? otherwise we don't care */ | ||
117 | if (stat(output, &st2) == 0) { | ||
118 | if (st1.st_ino == st2.st_ino && | ||
119 | st1.st_dev == st2.st_dev) | ||
120 | die("input and output file are the same"); | ||
121 | } | ||
122 | } | ||
123 | |||
124 | if (input) { | ||
125 | struct tracecmd_input *ihandle; | ||
126 | |||
127 | ihandle = tracecmd_alloc(input); | ||
128 | if (!ihandle) | ||
129 | die("error reading file %s", input); | ||
130 | /* make sure headers are ok */ | ||
131 | if (tracecmd_read_headers(ihandle) < 0) | ||
132 | die("error reading file %s headers", input); | ||
133 | |||
134 | handle = tracecmd_copy(ihandle, output); | ||
135 | tracecmd_close(ihandle); | ||
136 | } else | ||
137 | handle = tracecmd_create_init_file(output); | ||
138 | |||
139 | if (!handle) | ||
140 | die("error writing to %s", output); | ||
141 | |||
142 | if (tracecmd_append_cpu_data(handle, args, &argv[first_arg]) < 0) | ||
143 | die("failed to append data"); | ||
144 | |||
145 | return; | ||
146 | } | ||