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 | |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 27 | |
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" |
José Fonseca | 2e93bcb | 2012-02-18 15:33:12 +0000 | [diff] [blame] | 49 | " -h, --help show this help message and exit\n" |
Imre Deak | 6f07a84 | 2012-05-08 15:20:43 +0300 | [diff] [blame] | 50 | " --calls=CALLSET only retain specified calls\n" |
| 51 | " --thread=THREAD_ID only retain calls from specified thread\n" |
José Fonseca | 2e93bcb | 2012-02-18 15:33:12 +0000 | [diff] [blame] | 52 | " -o, --output=TRACE_FILE output trace file\n" |
José Fonseca | d3c0013 | 2012-01-27 22:43:53 +0000 | [diff] [blame] | 53 | "\n" |
| 54 | ; |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 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, |
| 59 | THREAD_OPT, |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | const static char * |
Kenneth Graunke | 5245ded | 2012-04-05 10:51:26 -0700 | [diff] [blame] | 63 | shortOptions = "ho:"; |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 64 | |
| 65 | const static struct option |
| 66 | longOptions[] = { |
| 67 | {"help", no_argument, 0, 'h'}, |
| 68 | {"calls", required_argument, 0, CALLS_OPT}, |
Imre Deak | 6f07a84 | 2012-05-08 15:20:43 +0300 | [diff] [blame] | 69 | {"thread", required_argument, 0, THREAD_OPT}, |
| 70 | {"output", required_argument, 0, 'o'}, |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 71 | {0, 0, 0, 0} |
| 72 | }; |
| 73 | |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 74 | static int |
| 75 | command(int argc, char *argv[]) |
| 76 | { |
José Fonseca | d3c0013 | 2012-01-27 22:43:53 +0000 | [diff] [blame] | 77 | std::string output; |
| 78 | trace::CallSet calls(trace::FREQUENCY_ALL); |
Imre Deak | 6f07a84 | 2012-05-08 15:20:43 +0300 | [diff] [blame] | 79 | int thread = -1; |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 80 | int i; |
| 81 | |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 82 | int opt; |
| 83 | while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) { |
| 84 | switch (opt) { |
| 85 | case 'h': |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 86 | usage(); |
| 87 | return 0; |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 88 | case CALLS_OPT: |
| 89 | calls = trace::CallSet(optarg); |
| 90 | break; |
Imre Deak | 6f07a84 | 2012-05-08 15:20:43 +0300 | [diff] [blame] | 91 | case THREAD_OPT: |
| 92 | thread = atoi(optarg); |
| 93 | break; |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 94 | case 'o': |
| 95 | output = optarg; |
| 96 | break; |
| 97 | default: |
| 98 | std::cerr << "error: unexpected option `" << opt << "`\n"; |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 99 | usage(); |
| 100 | return 1; |
| 101 | } |
| 102 | } |
| 103 | |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 104 | if (optind >= argc) { |
| 105 | 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] | 106 | usage(); |
| 107 | return 1; |
| 108 | } |
| 109 | |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 110 | for (i = optind; i < argc; ++i) { |
José Fonseca | d3c0013 | 2012-01-27 22:43:53 +0000 | [diff] [blame] | 111 | trace::Parser p; |
| 112 | if (!p.open(argv[i])) { |
José Fonseca | d3c0013 | 2012-01-27 22:43:53 +0000 | [diff] [blame] | 113 | return 1; |
| 114 | } |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 115 | |
José Fonseca | d3c0013 | 2012-01-27 22:43:53 +0000 | [diff] [blame] | 116 | if (output.empty()) { |
| 117 | os::String base(argv[i]); |
| 118 | base.trimExtension(); |
| 119 | |
| 120 | output = std::string(base.str()) + std::string("-trim.trace"); |
| 121 | } |
| 122 | |
| 123 | trace::Writer writer; |
| 124 | if (!writer.open(output.c_str())) { |
| 125 | std::cerr << "error: failed to create " << argv[i] << "\n"; |
| 126 | return 1; |
| 127 | } |
| 128 | |
| 129 | trace::Call *call; |
| 130 | while ((call = p.parse_call())) { |
Imre Deak | 6f07a84 | 2012-05-08 15:20:43 +0300 | [diff] [blame] | 131 | if (calls.contains(*call) && |
| 132 | (thread == -1 || call->thread_id == thread)) { |
José Fonseca | d3c0013 | 2012-01-27 22:43:53 +0000 | [diff] [blame] | 133 | writer.writeCall(call); |
| 134 | } |
| 135 | delete call; |
| 136 | } |
| 137 | |
| 138 | std::cout << "Trimmed trace is available as " << output << "\n"; |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | const Command trim_command = { |
| 145 | "trim", |
| 146 | synopsis, |
| 147 | usage, |
| 148 | command |
| 149 | }; |