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