]> git.cworth.org Git - apitrace/blob - cli/cli_dump.cpp
cli: Fix invalid option message.
[apitrace] / cli / cli_dump.cpp
1 /**************************************************************************
2  *
3  * Copyright 2011 Jose Fonseca
4  * Copyright 2010 VMware, Inc.
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
28 #include <string.h>
29 #include <limits.h> // for CHAR_MAX
30 #include <getopt.h>
31 #ifndef _WIN32
32 #include <unistd.h> // for isatty()
33 #endif
34
35 #include "cli.hpp"
36 #include "cli_pager.hpp"
37
38 #include "trace_parser.hpp"
39 #include "trace_dump.hpp"
40 #include "trace_callset.hpp"
41 #include "trace_option.hpp"
42
43
44 enum ColorOption {
45     COLOR_OPTION_NEVER = 0,
46     COLOR_OPTION_ALWAYS = 1,
47     COLOR_OPTION_AUTO = -1
48 };
49
50 static ColorOption color = COLOR_OPTION_AUTO;
51
52 static bool verbose = false;
53
54 static trace::CallSet calls(trace::FREQUENCY_ALL);
55
56 static const char *synopsis = "Dump given trace(s) to standard output.";
57
58 static void
59 usage(void)
60 {
61     std::cout
62         << "usage: apitrace dump [OPTIONS] TRACE_FILE...\n"
63         << synopsis << "\n"
64         "\n"
65         "    -h, --help           show this help message and exit\n"
66         "    -v, --verbose        verbose output\n"
67         "    --calls=CALLSET      only dump specified calls\n"
68         "    --color[=WHEN]\n"
69         "    --colour[=WHEN]      colored syntax highlighting\n"
70         "                         WHEN is 'auto', 'always', or 'never'\n"
71         "    --thread-ids=[=BOOL] dump thread ids [default: no]\n"
72         "    --call-nos[=BOOL]    dump call numbers[default: yes]\n"
73         "    --arg-names[=BOOL]   dump argument names [default: yes]\n"
74         "\n"
75     ;
76 }
77
78 enum {
79         CALLS_OPT = CHAR_MAX + 1,
80         COLOR_OPT,
81     THREAD_IDS_OPT,
82     CALL_NOS_OPT,
83     ARG_NAMES_OPT,
84 };
85
86 const static char *
87 shortOptions = "hv";
88
89 const static struct option
90 longOptions[] = {
91     {"help", no_argument, 0, 'h'},
92     {"verbose", no_argument, 0, 'v'},
93     {"calls", required_argument, 0, CALLS_OPT},
94     {"colour", optional_argument, 0, COLOR_OPT},
95     {"color", optional_argument, 0, COLOR_OPT},
96     {"thread-ids", optional_argument, 0, THREAD_IDS_OPT},
97     {"call-nos", optional_argument, 0, CALL_NOS_OPT},
98     {"arg-names", optional_argument, 0, ARG_NAMES_OPT},
99     {0, 0, 0, 0}
100 };
101
102 static int
103 command(int argc, char *argv[])
104 {
105     trace::DumpFlags dumpFlags = 0;
106     bool dumpThreadIds = false;
107     
108     int opt;
109     while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
110         switch (opt) {
111         case 'h':
112             usage();
113             return 0;
114         case 'v':
115             verbose = true;
116             break;
117         case CALLS_OPT:
118             calls = trace::CallSet(optarg);
119             break;
120         case COLOR_OPT:
121             if (!optarg ||
122                 !strcmp(optarg, "always")) {
123                 color = COLOR_OPTION_ALWAYS;
124             } else if (!strcmp(optarg, "auto")) {
125                 color = COLOR_OPTION_AUTO;
126             } else if (!strcmp(optarg, "never")) {
127                 color = COLOR_OPTION_NEVER;
128             } else {
129                 std::cerr << "error: unknown color argument " << optarg << "\n";
130                 return 1;
131             }
132             break;
133         case THREAD_IDS_OPT:
134             dumpThreadIds = trace::boolOption(optarg);
135             break;
136         case CALL_NOS_OPT:
137             if (trace::boolOption(optarg)) {
138                 dumpFlags &= ~trace::DUMP_FLAG_NO_CALL_NO;
139             } else {
140                 dumpFlags |= trace::DUMP_FLAG_NO_CALL_NO;
141             }
142             break;
143         case ARG_NAMES_OPT:
144             if (trace::boolOption(optarg)) {
145                 dumpFlags &= ~trace::DUMP_FLAG_NO_ARG_NAMES;
146             } else {
147                 dumpFlags |= trace::DUMP_FLAG_NO_ARG_NAMES;
148             }
149             break;
150         default:
151             std::cerr << "error: unexpected option `" << (char)opt << "`\n";
152             usage();
153             return 1;
154         }
155     }
156
157     if (color == COLOR_OPTION_AUTO) {
158 #ifdef _WIN32
159         color = COLOR_OPTION_ALWAYS;
160 #else
161         color = isatty(STDOUT_FILENO) ? COLOR_OPTION_ALWAYS : COLOR_OPTION_NEVER;
162         pipepager();
163 #endif
164     }
165
166     if (color == COLOR_OPTION_NEVER) {
167         dumpFlags |= trace::DUMP_FLAG_NO_COLOR;
168     }
169
170     for (int i = optind; i < argc; ++i) {
171         trace::Parser p;
172
173         if (!p.open(argv[i])) {
174             return 1;
175         }
176
177         trace::Call *call;
178         while ((call = p.parse_call())) {
179             if (calls.contains(*call)) {
180                 if (verbose ||
181                     !(call->flags & trace::CALL_FLAG_VERBOSE)) {
182                     if (dumpThreadIds) {
183                         std::cout << std::hex << call->thread_id << std::dec << " ";
184                     }
185                     trace::dump(*call, std::cout, dumpFlags);
186                 }
187             }
188             delete call;
189         }
190     }
191
192     return 0;
193 }
194
195 const Command dump_command = {
196     "dump",
197     synopsis,
198     usage,
199     command
200 };