Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 1 | /********************************************************************* |
| 2 | * |
| 3 | * Copyright 2011 Intel Corporation |
| 4 | * All Rights Reserved. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person |
| 7 | * obtaining a copy of this software and associated documentation |
| 8 | * files (the "Software"), to deal in the Software without |
| 9 | * restriction, including without limitation the rights to use, copy, |
| 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies |
| 11 | * 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 |
| 15 | * included in all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | * SOFTWARE. |
| 25 | * |
| 26 | *********************************************************************/ |
| 27 | |
| 28 | |
| 29 | /* |
Carl Worth | efa42fa | 2011-11-01 13:48:55 -0700 | [diff] [blame] | 30 | * Top-level application for accessing almost all of apitrace |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 31 | * functionality. |
| 32 | */ |
| 33 | |
José Fonseca | 71913ee | 2011-10-30 13:35:33 +0000 | [diff] [blame] | 34 | #include <string.h> |
| 35 | |
| 36 | #include <iomanip> |
| 37 | #include <iostream> |
| 38 | |
José Fonseca | 5cc28b0 | 2011-10-30 13:38:25 +0000 | [diff] [blame] | 39 | #include "cli.hpp" |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 40 | |
| 41 | #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0])) |
| 42 | |
José Fonseca | 3c167fe | 2011-10-30 14:21:03 +0000 | [diff] [blame] | 43 | static const char *help_synopsis = "Print detailed help for the given command."; |
Carl Worth | bd5e164 | 2011-10-21 20:40:56 -0700 | [diff] [blame] | 44 | |
José Fonseca | 646a00d | 2011-10-30 14:07:20 +0000 | [diff] [blame] | 45 | static void list_commands(void); |
| 46 | |
Carl Worth | bd5e164 | 2011-10-21 20:40:56 -0700 | [diff] [blame] | 47 | static void |
José Fonseca | 3c167fe | 2011-10-30 14:21:03 +0000 | [diff] [blame] | 48 | help_usage() |
Carl Worth | bd5e164 | 2011-10-21 20:40:56 -0700 | [diff] [blame] | 49 | { |
José Fonseca | 3c167fe | 2011-10-30 14:21:03 +0000 | [diff] [blame] | 50 | std::cout |
| 51 | << "usage: apitrace help [<command>]\n" |
| 52 | << help_synopsis << "\n" |
José Fonseca | 646a00d | 2011-10-30 14:07:20 +0000 | [diff] [blame] | 53 | "\n"; |
| 54 | |
| 55 | list_commands(); |
Carl Worth | bd5e164 | 2011-10-21 20:40:56 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 58 | static int |
Carl Worth | 68f7c98 | 2011-11-01 13:47:26 -0700 | [diff] [blame] | 59 | do_help_command(int argc, char *argv[]); |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 60 | |
Carl Worth | 68f7c98 | 2011-11-01 13:47:26 -0700 | [diff] [blame] | 61 | const Command help_command = { |
José Fonseca | 3c167fe | 2011-10-30 14:21:03 +0000 | [diff] [blame] | 62 | "help", |
| 63 | help_synopsis, |
| 64 | help_usage, |
Carl Worth | 68f7c98 | 2011-11-01 13:47:26 -0700 | [diff] [blame] | 65 | do_help_command |
José Fonseca | 3c167fe | 2011-10-30 14:21:03 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | static const Command * commands[] = { |
Carl Worth | 32420d2 | 2011-11-04 15:45:09 -0700 | [diff] [blame] | 69 | &diff_command, |
Carl Worth | ca0c40f | 2011-11-04 17:04:15 -0700 | [diff] [blame] | 70 | &diff_state_command, |
Carl Worth | 1cbf918 | 2011-11-11 16:58:05 -0800 | [diff] [blame] | 71 | &diff_images_command, |
Carl Worth | 68f7c98 | 2011-11-01 13:47:26 -0700 | [diff] [blame] | 72 | &dump_command, |
Carl Worth | e40a6ab | 2012-03-27 18:13:14 -0700 | [diff] [blame] | 73 | &dump_images_command, |
Jose Fonseca | c4343f1 | 2016-01-19 13:54:09 +0000 | [diff] [blame] | 74 | &leaks_command, |
José Fonseca | 299a1b3 | 2012-01-26 20:32:59 +0000 | [diff] [blame] | 75 | &pickle_command, |
José Fonseca | 084fe92 | 2013-07-09 16:35:14 +0100 | [diff] [blame] | 76 | &sed_command, |
José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 77 | &repack_command, |
José Fonseca | 3320d02 | 2012-11-18 15:45:27 +0000 | [diff] [blame] | 78 | &retrace_command, |
Carl Worth | c987ebd | 2011-11-01 17:54:19 -0700 | [diff] [blame] | 79 | &trace_command, |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 80 | &trim_command, |
Jose Fonseca | d9809aa | 2015-08-06 12:19:25 +0100 | [diff] [blame] | 81 | &trim_auto_command, |
Carl Worth | 68f7c98 | 2011-11-01 13:47:26 -0700 | [diff] [blame] | 82 | &help_command |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
Carl Worth | ade96b2 | 2013-02-11 14:56:15 -0800 | [diff] [blame] | 85 | /* Aliases provide a mechanism to allow compatibility with old command |
| 86 | * names (such as "retrace") for current commands (such as the replay |
| 87 | * command). */ |
| 88 | typedef struct { |
| 89 | const char *name; |
| 90 | const Command *command; |
| 91 | } Alias; |
| 92 | |
| 93 | static const Alias aliases[] = { |
| 94 | { "retrace", &retrace_command } |
| 95 | }; |
| 96 | |
José Fonseca | 646a00d | 2011-10-30 14:07:20 +0000 | [diff] [blame] | 97 | static void |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 98 | usage(void) |
| 99 | { |
José Fonseca | 646a00d | 2011-10-30 14:07:20 +0000 | [diff] [blame] | 100 | std::cout << |
| 101 | "Usage: apitrace <command> [<args> ...]\n" |
| 102 | "Top-level command line frontend to apitrace.\n" |
| 103 | "\n"; |
| 104 | |
| 105 | list_commands(); |
| 106 | } |
| 107 | |
| 108 | static void |
| 109 | list_commands(void) { |
José Fonseca | 3c167fe | 2011-10-30 14:21:03 +0000 | [diff] [blame] | 110 | const Command *command; |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 111 | int i, max_width = 0; |
| 112 | |
José Fonseca | 646a00d | 2011-10-30 14:07:20 +0000 | [diff] [blame] | 113 | std::cout << "The available commands are as follows:\n\n"; |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 114 | |
| 115 | std::cout << std::setiosflags(std::ios::left); |
| 116 | |
| 117 | for (i = 0; i < ARRAY_SIZE(commands); i++) { |
José Fonseca | 3c167fe | 2011-10-30 14:21:03 +0000 | [diff] [blame] | 118 | command = commands[i]; |
| 119 | if (strlen(command->name) > max_width) { |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 120 | max_width = strlen(command->name); |
José Fonseca | 3c167fe | 2011-10-30 14:21:03 +0000 | [diff] [blame] | 121 | } |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | for (i = 0; i < ARRAY_SIZE(commands); i++) { |
José Fonseca | 3c167fe | 2011-10-30 14:21:03 +0000 | [diff] [blame] | 125 | command = commands[i]; |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 126 | |
José Fonseca | 3c167fe | 2011-10-30 14:21:03 +0000 | [diff] [blame] | 127 | std::cout << " " << std::setw(max_width + 2) << command->name |
Carl Worth | bd5e164 | 2011-10-21 20:40:56 -0700 | [diff] [blame] | 128 | << " " << command->synopsis << "\n"; |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | std::cout << "\n" |
| 132 | "Use \"apitrace help <command>\" for more details on each command.\n"; |
| 133 | } |
| 134 | |
José Fonseca | 646a00d | 2011-10-30 14:07:20 +0000 | [diff] [blame] | 135 | |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 136 | static int |
Carl Worth | 68f7c98 | 2011-11-01 13:47:26 -0700 | [diff] [blame] | 137 | do_help_command(int argc, char *argv[]) |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 138 | { |
José Fonseca | 3c167fe | 2011-10-30 14:21:03 +0000 | [diff] [blame] | 139 | const Command *command; |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 140 | int i; |
| 141 | |
José Fonseca | 54f752c | 2012-01-31 10:02:52 +0000 | [diff] [blame] | 142 | if (argc != 2) { |
José Fonseca | 3c167fe | 2011-10-30 14:21:03 +0000 | [diff] [blame] | 143 | help_usage(); |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 144 | return 0; |
| 145 | } |
| 146 | |
José Fonseca | 54f752c | 2012-01-31 10:02:52 +0000 | [diff] [blame] | 147 | char *command_name = argv[1]; |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 148 | |
| 149 | for (i = 0; i < ARRAY_SIZE(commands); i++) { |
José Fonseca | 3c167fe | 2011-10-30 14:21:03 +0000 | [diff] [blame] | 150 | command = commands[i]; |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 151 | |
| 152 | if (strcmp(command_name, command->name) == 0) { |
José Fonseca | 646a00d | 2011-10-30 14:07:20 +0000 | [diff] [blame] | 153 | (command->usage) (); |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 154 | return 0; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | std::cerr << "Error: Unknown command: " << command_name |
| 159 | << " (see \"apitrace help\").\n"; |
| 160 | |
| 161 | return 1; |
| 162 | } |
| 163 | |
| 164 | int |
| 165 | main(int argc, char **argv) |
| 166 | { |
José Fonseca | ffba597 | 2011-10-30 14:29:28 +0000 | [diff] [blame] | 167 | const char *command_name; |
José Fonseca | 3c167fe | 2011-10-30 14:21:03 +0000 | [diff] [blame] | 168 | const Command *command; |
Carl Worth | ade96b2 | 2013-02-11 14:56:15 -0800 | [diff] [blame] | 169 | const Alias *alias; |
José Fonseca | ffba597 | 2011-10-30 14:29:28 +0000 | [diff] [blame] | 170 | int i; |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 171 | |
| 172 | for (i = 1; i < argc; ++i) { |
| 173 | const char *arg = argv[i]; |
| 174 | |
| 175 | if (arg[0] != '-') { |
| 176 | break; |
| 177 | } |
| 178 | |
| 179 | if (strcmp(arg, "--help") == 0) { |
Carl Worth | 68f7c98 | 2011-11-01 13:47:26 -0700 | [diff] [blame] | 180 | return do_help_command(0, NULL); |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 181 | } else { |
| 182 | std::cerr << "Error: unknown option " << arg << "\n"; |
| 183 | usage(); |
| 184 | return 1; |
| 185 | } |
| 186 | } |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 187 | |
José Fonseca | ffba597 | 2011-10-30 14:29:28 +0000 | [diff] [blame] | 188 | if (i == argc) { |
| 189 | usage(); |
| 190 | return 1; |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 191 | } |
| 192 | |
José Fonseca | 54f752c | 2012-01-31 10:02:52 +0000 | [diff] [blame] | 193 | command_name = argv[i]; |
José Fonseca | ffba597 | 2011-10-30 14:29:28 +0000 | [diff] [blame] | 194 | |
| 195 | argc -= i; |
| 196 | argv = &argv[i]; |
| 197 | |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 198 | for (i = 0; i < ARRAY_SIZE(commands); i++) { |
José Fonseca | 3c167fe | 2011-10-30 14:21:03 +0000 | [diff] [blame] | 199 | command = commands[i]; |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 200 | |
| 201 | if (strcmp(command_name, command->name) == 0) |
José Fonseca | ffba597 | 2011-10-30 14:29:28 +0000 | [diff] [blame] | 202 | return (command->function) (argc, argv); |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Carl Worth | ade96b2 | 2013-02-11 14:56:15 -0800 | [diff] [blame] | 205 | for (i = 0; i < ARRAY_SIZE(aliases); i++) { |
| 206 | alias = &aliases[i]; |
| 207 | |
| 208 | if (strcmp(command_name, alias->name) == 0) |
| 209 | return (alias->command->function) (argc, argv); |
| 210 | } |
| 211 | |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 212 | std::cerr << "Error: unknown command " << command_name |
| 213 | << " (see \"apitrace help\").\n"; |
| 214 | |
José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 215 | return 1; |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 216 | } |