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 | |
| 27 | #include <string.h> |
| 28 | |
| 29 | #include "cli.hpp" |
| 30 | |
| 31 | #include "os_string.hpp" |
| 32 | |
José Fonseca | d3c0013 | 2012-01-27 22:43:53 +0000 | [diff] [blame] | 33 | #include "trace_callset.hpp" |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 34 | #include "trace_parser.hpp" |
José Fonseca | 630471a | 2012-01-27 22:06:51 +0000 | [diff] [blame] | 35 | #include "trace_writer.hpp" |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 36 | |
| 37 | static const char *synopsis = "Create a new trace by trimming an existing trace."; |
| 38 | |
| 39 | static void |
| 40 | usage(void) |
| 41 | { |
| 42 | std::cout |
José Fonseca | d3c0013 | 2012-01-27 22:43:53 +0000 | [diff] [blame] | 43 | << "usage: apitrace trim [OPTIONS] <trace-file>...\n" |
| 44 | << synopsis << "\n" |
| 45 | "\n" |
| 46 | " --calls <CALLSET> Only trim specified calls\n" |
| 47 | " -o --output <TRACEFILE> Output trace file\n" |
| 48 | "\n" |
| 49 | ; |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static int |
| 53 | command(int argc, char *argv[]) |
| 54 | { |
José Fonseca | d3c0013 | 2012-01-27 22:43:53 +0000 | [diff] [blame] | 55 | std::string output; |
| 56 | trace::CallSet calls(trace::FREQUENCY_ALL); |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 57 | int i; |
| 58 | |
José Fonseca | d3c0013 | 2012-01-27 22:43:53 +0000 | [diff] [blame] | 59 | for (i = 0; i < argc;) { |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 60 | const char *arg = argv[i]; |
| 61 | |
| 62 | if (arg[0] != '-') { |
| 63 | break; |
| 64 | } |
| 65 | |
José Fonseca | d3c0013 | 2012-01-27 22:43:53 +0000 | [diff] [blame] | 66 | ++i; |
| 67 | |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 68 | if (!strcmp(arg, "--")) { |
| 69 | break; |
| 70 | } else if (!strcmp(arg, "--help")) { |
| 71 | usage(); |
| 72 | return 0; |
José Fonseca | d3c0013 | 2012-01-27 22:43:53 +0000 | [diff] [blame] | 73 | } else if (!strcmp(arg, "--calls")) { |
| 74 | calls = trace::CallSet(argv[i++]); |
| 75 | } else if (!strcmp(arg, "-o") || |
| 76 | !strcmp(arg, "--output")) { |
| 77 | output = argv[i++]; |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 78 | } else { |
| 79 | std::cerr << "error: unknown option " << arg << "\n"; |
| 80 | usage(); |
| 81 | return 1; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if (i >= argc) { |
| 86 | std::cerr << "Error: apitrace trim requires a trace file as an argument.\n"; |
| 87 | usage(); |
| 88 | return 1; |
| 89 | } |
| 90 | |
José Fonseca | d3c0013 | 2012-01-27 22:43:53 +0000 | [diff] [blame] | 91 | for ( ; i < argc; ++i) { |
| 92 | trace::Parser p; |
| 93 | if (!p.open(argv[i])) { |
| 94 | std::cerr << "error: failed to open " << argv[i] << "\n"; |
| 95 | return 1; |
| 96 | } |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 97 | |
José Fonseca | d3c0013 | 2012-01-27 22:43:53 +0000 | [diff] [blame] | 98 | if (output.empty()) { |
| 99 | os::String base(argv[i]); |
| 100 | base.trimExtension(); |
| 101 | |
| 102 | output = std::string(base.str()) + std::string("-trim.trace"); |
| 103 | } |
| 104 | |
| 105 | trace::Writer writer; |
| 106 | if (!writer.open(output.c_str())) { |
| 107 | std::cerr << "error: failed to create " << argv[i] << "\n"; |
| 108 | return 1; |
| 109 | } |
| 110 | |
| 111 | trace::Call *call; |
| 112 | while ((call = p.parse_call())) { |
| 113 | if (calls.contains(*call)) { |
| 114 | writer.writeCall(call); |
| 115 | } |
| 116 | delete call; |
| 117 | } |
| 118 | |
| 119 | std::cout << "Trimmed trace is available as " << output << "\n"; |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | const Command trim_command = { |
| 126 | "trim", |
| 127 | synopsis, |
| 128 | usage, |
| 129 | command |
| 130 | }; |