Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
| 3 | * Copyright 2010 VMware, Inc. |
| 4 | * Copyright 2011 Intel corporation |
| 5 | * All Rights Reserved. |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | * of this software and associated documentation files (the "Software"), to deal |
| 9 | * in the Software without restriction, including without limitation the rights |
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | * copies of the Software, and to permit persons to whom the Software is |
| 12 | * furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included in |
| 15 | * all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | * THE SOFTWARE. |
| 24 | * |
| 25 | **************************************************************************/ |
| 26 | |
Carl Worth | 33b9263 | 2012-08-14 14:11:19 -0700 | [diff] [blame] | 27 | #include <sstream> |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 28 | #include <string.h> |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 29 | #include <limits.h> // for CHAR_MAX |
| 30 | #include <getopt.h> |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 31 | |
| 32 | #include "cli.hpp" |
| 33 | |
| 34 | #include "os_string.hpp" |
| 35 | |
José Fonseca | d3c0013 | 2012-01-27 22:43:53 +0000 | [diff] [blame] | 36 | #include "trace_callset.hpp" |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 37 | #include "trace_parser.hpp" |
José Fonseca | 630471a | 2012-01-27 22:06:51 +0000 | [diff] [blame] | 38 | #include "trace_writer.hpp" |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 39 | |
| 40 | static const char *synopsis = "Create a new trace by trimming an existing trace."; |
| 41 | |
| 42 | static void |
| 43 | usage(void) |
| 44 | { |
| 45 | std::cout |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 46 | << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n" |
José Fonseca | d3c0013 | 2012-01-27 22:43:53 +0000 | [diff] [blame] | 47 | << synopsis << "\n" |
| 48 | "\n" |
Carl Worth | 8646443 | 2012-08-11 11:46:54 -0700 | [diff] [blame] | 49 | " -h, --help Show detailed help for trim options and exit\n" |
| 50 | " --calls=CALLSET Include specified calls in the trimmed output.\n" |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 51 | " --frames=FRAMESET Include specified frames in the trimmed output.\n" |
Carl Worth | 8646443 | 2012-08-11 11:46:54 -0700 | [diff] [blame] | 52 | " --thread=THREAD_ID Only retain calls from specified thread\n" |
| 53 | " -o, --output=TRACE_FILE Output trace file\n" |
| 54 | ; |
| 55 | } |
| 56 | |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 57 | enum { |
Imre Deak | 6f07a84 | 2012-05-08 15:20:43 +0300 | [diff] [blame] | 58 | CALLS_OPT = CHAR_MAX + 1, |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 59 | FRAMES_OPT, |
Jose Fonseca | d9809aa | 2015-08-06 12:19:25 +0100 | [diff] [blame] | 60 | THREAD_OPT |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | const static char * |
Carl Worth | 54300eb | 2013-01-28 23:37:17 +1100 | [diff] [blame] | 64 | shortOptions = "aho:x"; |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 65 | |
| 66 | const static struct option |
| 67 | longOptions[] = { |
| 68 | {"help", no_argument, 0, 'h'}, |
| 69 | {"calls", required_argument, 0, CALLS_OPT}, |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 70 | {"frames", required_argument, 0, FRAMES_OPT}, |
Imre Deak | 6f07a84 | 2012-05-08 15:20:43 +0300 | [diff] [blame] | 71 | {"thread", required_argument, 0, THREAD_OPT}, |
| 72 | {"output", required_argument, 0, 'o'}, |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 73 | {0, 0, 0, 0} |
| 74 | }; |
| 75 | |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 76 | struct stringCompare { |
| 77 | bool operator() (const char *a, const char *b) const { |
| 78 | return strcmp(a, b) < 0; |
| 79 | } |
| 80 | }; |
| 81 | |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 82 | struct trim_options { |
| 83 | /* Calls to be included in trace. */ |
| 84 | trace::CallSet calls; |
| 85 | |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 86 | /* Frames to be included in trace. */ |
| 87 | trace::CallSet frames; |
| 88 | |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 89 | /* Output filename */ |
| 90 | std::string output; |
| 91 | |
| 92 | /* Emit only calls from this thread (-1 == all threads) */ |
| 93 | int thread; |
| 94 | }; |
| 95 | |
| 96 | static int |
| 97 | trim_trace(const char *filename, struct trim_options *options) |
| 98 | { |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 99 | trace::Parser p; |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 100 | unsigned frame; |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 101 | |
| 102 | if (!p.open(filename)) { |
| 103 | std::cerr << "error: failed to open " << filename << "\n"; |
| 104 | return 1; |
| 105 | } |
| 106 | |
Jose Fonseca | d9809aa | 2015-08-06 12:19:25 +0100 | [diff] [blame] | 107 | /* Prepare output file and writer for output. */ |
| 108 | if (options->output.empty()) { |
| 109 | os::String base(filename); |
| 110 | base.trimExtension(); |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 111 | |
Jose Fonseca | d9809aa | 2015-08-06 12:19:25 +0100 | [diff] [blame] | 112 | options->output = std::string(base.str()) + std::string("-trim.trace"); |
| 113 | } |
| 114 | |
| 115 | trace::Writer writer; |
| 116 | if (!writer.open(options->output.c_str())) { |
| 117 | std::cerr << "error: failed to create " << options->output << "\n"; |
| 118 | return 1; |
| 119 | } |
| 120 | |
| 121 | |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 122 | frame = 0; |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 123 | trace::Call *call; |
| 124 | while ((call = p.parse_call())) { |
Carl Worth | 5b827e1 | 2012-08-12 20:41:50 -0700 | [diff] [blame] | 125 | |
Carl Worth | 18198bc | 2013-03-11 15:49:17 -0700 | [diff] [blame] | 126 | /* There's no use doing any work past the last call and frame |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 127 | * requested by the user. */ |
Carl Worth | 18198bc | 2013-03-11 15:49:17 -0700 | [diff] [blame] | 128 | if ((options->calls.empty() || call->no > options->calls.getLast()) && |
| 129 | (options->frames.empty() || frame > options->frames.getLast())) { |
| 130 | |
Carl Worth | 4224901 | 2012-08-14 10:26:11 -0700 | [diff] [blame] | 131 | delete call; |
Carl Worth | 5b827e1 | 2012-08-12 20:41:50 -0700 | [diff] [blame] | 132 | break; |
Carl Worth | 4224901 | 2012-08-14 10:26:11 -0700 | [diff] [blame] | 133 | } |
Carl Worth | 5b827e1 | 2012-08-12 20:41:50 -0700 | [diff] [blame] | 134 | |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 135 | /* If requested, ignore all calls not belonging to the specified thread. */ |
Carl Worth | 4224901 | 2012-08-14 10:26:11 -0700 | [diff] [blame] | 136 | if (options->thread != -1 && call->thread_id != options->thread) { |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 137 | goto NEXT; |
Carl Worth | 4224901 | 2012-08-14 10:26:11 -0700 | [diff] [blame] | 138 | } |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 139 | |
Carl Worth | cc6e51c | 2012-08-13 14:35:43 -0700 | [diff] [blame] | 140 | /* If this call is included in the user-specified call set, |
| 141 | * then require it (and all dependencies) in the trimmed |
| 142 | * output. */ |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 143 | if (options->calls.contains(*call) || |
| 144 | options->frames.contains(frame, call->flags)) { |
| 145 | |
Jose Fonseca | d9809aa | 2015-08-06 12:19:25 +0100 | [diff] [blame] | 146 | writer.writeCall(call); |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 147 | } |
Carl Worth | 4224901 | 2012-08-14 10:26:11 -0700 | [diff] [blame] | 148 | |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 149 | NEXT: |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 150 | if (call->flags & trace::CALL_FLAG_END_FRAME) { |
| 151 | frame++; |
| 152 | } |
| 153 | |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 154 | delete call; |
| 155 | } |
| 156 | |
Carl Worth | 1a800a1 | 2012-08-17 14:23:13 -0700 | [diff] [blame] | 157 | std::cerr << "Trimmed trace is available as " << options->output << "\n"; |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 162 | static int |
| 163 | command(int argc, char *argv[]) |
| 164 | { |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 165 | struct trim_options options; |
| 166 | |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 167 | options.calls = trace::CallSet(trace::FREQUENCY_NONE); |
| 168 | options.frames = trace::CallSet(trace::FREQUENCY_NONE); |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 169 | options.output = ""; |
| 170 | options.thread = -1; |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 171 | |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 172 | int opt; |
| 173 | while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) { |
| 174 | switch (opt) { |
| 175 | case 'h': |
Jose Fonseca | d9809aa | 2015-08-06 12:19:25 +0100 | [diff] [blame] | 176 | usage(); |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 177 | return 0; |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 178 | case CALLS_OPT: |
Lawrence L Love | 5ab982d | 2013-12-05 11:31:16 -0800 | [diff] [blame] | 179 | options.calls.merge(optarg); |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 180 | break; |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 181 | case FRAMES_OPT: |
Lawrence L Love | 5ab982d | 2013-12-05 11:31:16 -0800 | [diff] [blame] | 182 | options.frames.merge(optarg); |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 183 | break; |
Imre Deak | 6f07a84 | 2012-05-08 15:20:43 +0300 | [diff] [blame] | 184 | case THREAD_OPT: |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 185 | options.thread = atoi(optarg); |
Imre Deak | 6f07a84 | 2012-05-08 15:20:43 +0300 | [diff] [blame] | 186 | break; |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 187 | case 'o': |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 188 | options.output = optarg; |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 189 | break; |
| 190 | default: |
José Fonseca | 4ce88b8 | 2013-10-11 17:24:47 -0700 | [diff] [blame] | 191 | std::cerr << "error: unexpected option `" << (char)opt << "`\n"; |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 192 | usage(); |
| 193 | return 1; |
| 194 | } |
| 195 | } |
| 196 | |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 197 | /* If neither of --calls nor --frames was set, default to the |
| 198 | * entire set of calls. */ |
| 199 | if (options.calls.empty() && options.frames.empty()) { |
| 200 | options.calls = trace::CallSet(trace::FREQUENCY_ALL); |
| 201 | } |
| 202 | |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 203 | if (optind >= argc) { |
| 204 | std::cerr << "error: apitrace trim requires a trace file as an argument.\n"; |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 205 | usage(); |
| 206 | return 1; |
| 207 | } |
| 208 | |
Carl Worth | f630d9d | 2012-09-04 16:48:00 -0700 | [diff] [blame] | 209 | if (argc > optind + 1) { |
| 210 | std::cerr << "error: extraneous arguments:"; |
| 211 | for (int i = optind + 1; i < argc; i++) { |
| 212 | std::cerr << " " << argv[i]; |
| 213 | } |
| 214 | std::cerr << "\n"; |
| 215 | usage(); |
| 216 | return 1; |
| 217 | } |
| 218 | |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 219 | return trim_trace(argv[optind], &options); |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | const Command trim_command = { |
| 223 | "trim", |
| 224 | synopsis, |
Jose Fonseca | d9809aa | 2015-08-06 12:19:25 +0100 | [diff] [blame] | 225 | usage, |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 226 | command |
| 227 | }; |