blob: b475bd3bfdc549c551bc55acfe38066e199377bd [file] [log] [blame]
Carl Worth6d543af2011-10-19 18:02:33 -07001/*********************************************************************
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 Worthefa42fa2011-11-01 13:48:55 -070030 * Top-level application for accessing almost all of apitrace
Carl Worth6d543af2011-10-19 18:02:33 -070031 * functionality.
32 */
33
José Fonseca71913ee2011-10-30 13:35:33 +000034#include <string.h>
35
36#include <iomanip>
37#include <iostream>
38
José Fonseca5cc28b02011-10-30 13:38:25 +000039#include "cli.hpp"
Carl Worth6d543af2011-10-19 18:02:33 -070040
41#define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
42
José Fonseca3c167fe2011-10-30 14:21:03 +000043static const char *help_synopsis = "Print detailed help for the given command.";
Carl Worthbd5e1642011-10-21 20:40:56 -070044
José Fonseca646a00d2011-10-30 14:07:20 +000045static void list_commands(void);
46
Carl Worthbd5e1642011-10-21 20:40:56 -070047static void
José Fonseca3c167fe2011-10-30 14:21:03 +000048help_usage()
Carl Worthbd5e1642011-10-21 20:40:56 -070049{
José Fonseca3c167fe2011-10-30 14:21:03 +000050 std::cout
51 << "usage: apitrace help [<command>]\n"
52 << help_synopsis << "\n"
José Fonseca646a00d2011-10-30 14:07:20 +000053 "\n";
54
55 list_commands();
Carl Worthbd5e1642011-10-21 20:40:56 -070056}
57
Carl Worth6d543af2011-10-19 18:02:33 -070058static int
Carl Worth68f7c982011-11-01 13:47:26 -070059do_help_command(int argc, char *argv[]);
Carl Worth6d543af2011-10-19 18:02:33 -070060
Carl Worth68f7c982011-11-01 13:47:26 -070061const Command help_command = {
José Fonseca3c167fe2011-10-30 14:21:03 +000062 "help",
63 help_synopsis,
64 help_usage,
Carl Worth68f7c982011-11-01 13:47:26 -070065 do_help_command
José Fonseca3c167fe2011-10-30 14:21:03 +000066};
67
68static const Command * commands[] = {
Carl Worth32420d22011-11-04 15:45:09 -070069 &diff_command,
Carl Worthca0c40f2011-11-04 17:04:15 -070070 &diff_state_command,
Carl Worth1cbf9182011-11-11 16:58:05 -080071 &diff_images_command,
Carl Worth68f7c982011-11-01 13:47:26 -070072 &dump_command,
Carl Worthe40a6ab2012-03-27 18:13:14 -070073 &dump_images_command,
Jose Fonsecac4343f12016-01-19 13:54:09 +000074 &leaks_command,
José Fonseca299a1b32012-01-26 20:32:59 +000075 &pickle_command,
José Fonseca084fe922013-07-09 16:35:14 +010076 &sed_command,
José Fonsecaa3285532011-11-27 12:32:00 +000077 &repack_command,
José Fonseca3320d022012-11-18 15:45:27 +000078 &retrace_command,
Carl Worthc987ebd2011-11-01 17:54:19 -070079 &trace_command,
Carl Worth4c5f6fa2011-11-14 14:50:07 -080080 &trim_command,
Jose Fonsecad9809aa2015-08-06 12:19:25 +010081 &trim_auto_command,
Carl Worth68f7c982011-11-01 13:47:26 -070082 &help_command
Carl Worth6d543af2011-10-19 18:02:33 -070083};
84
Carl Worthade96b22013-02-11 14:56:15 -080085/* Aliases provide a mechanism to allow compatibility with old command
86 * names (such as "retrace") for current commands (such as the replay
87 * command). */
88typedef struct {
89 const char *name;
90 const Command *command;
91} Alias;
92
93static const Alias aliases[] = {
94 { "retrace", &retrace_command }
95};
96
José Fonseca646a00d2011-10-30 14:07:20 +000097static void
Carl Worth6d543af2011-10-19 18:02:33 -070098usage(void)
99{
José Fonseca646a00d2011-10-30 14:07:20 +0000100 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
108static void
109list_commands(void) {
José Fonseca3c167fe2011-10-30 14:21:03 +0000110 const Command *command;
Carl Worth6d543af2011-10-19 18:02:33 -0700111 int i, max_width = 0;
112
José Fonseca646a00d2011-10-30 14:07:20 +0000113 std::cout << "The available commands are as follows:\n\n";
Carl Worth6d543af2011-10-19 18:02:33 -0700114
115 std::cout << std::setiosflags(std::ios::left);
116
117 for (i = 0; i < ARRAY_SIZE(commands); i++) {
José Fonseca3c167fe2011-10-30 14:21:03 +0000118 command = commands[i];
119 if (strlen(command->name) > max_width) {
Carl Worth6d543af2011-10-19 18:02:33 -0700120 max_width = strlen(command->name);
José Fonseca3c167fe2011-10-30 14:21:03 +0000121 }
Carl Worth6d543af2011-10-19 18:02:33 -0700122 }
123
124 for (i = 0; i < ARRAY_SIZE(commands); i++) {
José Fonseca3c167fe2011-10-30 14:21:03 +0000125 command = commands[i];
Carl Worth6d543af2011-10-19 18:02:33 -0700126
José Fonseca3c167fe2011-10-30 14:21:03 +0000127 std::cout << " " << std::setw(max_width + 2) << command->name
Carl Worthbd5e1642011-10-21 20:40:56 -0700128 << " " << command->synopsis << "\n";
Carl Worth6d543af2011-10-19 18:02:33 -0700129 }
130
131 std::cout << "\n"
132 "Use \"apitrace help <command>\" for more details on each command.\n";
133}
134
José Fonseca646a00d2011-10-30 14:07:20 +0000135
Carl Worth6d543af2011-10-19 18:02:33 -0700136static int
Carl Worth68f7c982011-11-01 13:47:26 -0700137do_help_command(int argc, char *argv[])
Carl Worth6d543af2011-10-19 18:02:33 -0700138{
José Fonseca3c167fe2011-10-30 14:21:03 +0000139 const Command *command;
Carl Worth6d543af2011-10-19 18:02:33 -0700140 int i;
141
José Fonseca54f752c2012-01-31 10:02:52 +0000142 if (argc != 2) {
José Fonseca3c167fe2011-10-30 14:21:03 +0000143 help_usage();
Carl Worth6d543af2011-10-19 18:02:33 -0700144 return 0;
145 }
146
José Fonseca54f752c2012-01-31 10:02:52 +0000147 char *command_name = argv[1];
Carl Worth6d543af2011-10-19 18:02:33 -0700148
149 for (i = 0; i < ARRAY_SIZE(commands); i++) {
José Fonseca3c167fe2011-10-30 14:21:03 +0000150 command = commands[i];
Carl Worth6d543af2011-10-19 18:02:33 -0700151
152 if (strcmp(command_name, command->name) == 0) {
José Fonseca646a00d2011-10-30 14:07:20 +0000153 (command->usage) ();
Carl Worth6d543af2011-10-19 18:02:33 -0700154 return 0;
155 }
156 }
157
158 std::cerr << "Error: Unknown command: " << command_name
159 << " (see \"apitrace help\").\n";
160
161 return 1;
162}
163
164int
165main(int argc, char **argv)
166{
José Fonsecaffba5972011-10-30 14:29:28 +0000167 const char *command_name;
José Fonseca3c167fe2011-10-30 14:21:03 +0000168 const Command *command;
Carl Worthade96b22013-02-11 14:56:15 -0800169 const Alias *alias;
José Fonsecaffba5972011-10-30 14:29:28 +0000170 int i;
Carl Worth6d543af2011-10-19 18:02:33 -0700171
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 Worth68f7c982011-11-01 13:47:26 -0700180 return do_help_command(0, NULL);
Carl Worth6d543af2011-10-19 18:02:33 -0700181 } else {
182 std::cerr << "Error: unknown option " << arg << "\n";
183 usage();
184 return 1;
185 }
186 }
Carl Worth6d543af2011-10-19 18:02:33 -0700187
José Fonsecaffba5972011-10-30 14:29:28 +0000188 if (i == argc) {
189 usage();
190 return 1;
Carl Worth6d543af2011-10-19 18:02:33 -0700191 }
192
José Fonseca54f752c2012-01-31 10:02:52 +0000193 command_name = argv[i];
José Fonsecaffba5972011-10-30 14:29:28 +0000194
195 argc -= i;
196 argv = &argv[i];
197
Carl Worth6d543af2011-10-19 18:02:33 -0700198 for (i = 0; i < ARRAY_SIZE(commands); i++) {
José Fonseca3c167fe2011-10-30 14:21:03 +0000199 command = commands[i];
Carl Worth6d543af2011-10-19 18:02:33 -0700200
201 if (strcmp(command_name, command->name) == 0)
José Fonsecaffba5972011-10-30 14:29:28 +0000202 return (command->function) (argc, argv);
Carl Worth6d543af2011-10-19 18:02:33 -0700203 }
204
Carl Worthade96b22013-02-11 14:56:15 -0800205 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 Worth6d543af2011-10-19 18:02:33 -0700212 std::cerr << "Error: unknown command " << command_name
213 << " (see \"apitrace help\").\n";
214
José Fonsecaa3285532011-11-27 12:32:00 +0000215 return 1;
Carl Worth6d543af2011-10-19 18:02:33 -0700216}