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