]> git.cworth.org Git - apitrace/blob - cli/cli_dump.cpp
Bundle BSD 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
30 #include "cli.hpp"
31 #include "cli_pager.hpp"
32
33 #include "trace_parser.hpp"
34 #include "trace_dump.hpp"
35 #include "trace_callset.hpp"
36
37
38 enum ColorOption {
39     COLOR_OPTION_NEVER = 0,
40     COLOR_OPTION_ALWAYS = 1,
41     COLOR_OPTION_AUTO = -1
42 };
43
44 static ColorOption color = COLOR_OPTION_AUTO;
45
46 static bool verbose = false;
47
48 static trace::CallSet calls(trace::FREQUENCY_ALL);
49
50 static const char *synopsis = "Dump given trace(s) to standard output.";
51
52 static void
53 usage(void)
54 {
55     std::cout
56         << "usage: apitrace dump [OPTIONS] <trace-file>...\n"
57         << synopsis << "\n"
58         "\n"
59         "    -v, --verbose       verbose output\n"
60         "    --calls <CALLSET>   Only dump specified calls\n"
61         "    --color=<WHEN>\n"
62         "    --colour=<WHEN>     Colored syntax highlighting\n"
63         "                        WHEN is 'auto', 'always', or 'never'\n"
64         "    --no-arg-names      Don't dump argument names\n"
65         "    --thread-ids        Dump thread ids\n"
66     ;
67 }
68
69 static int
70 command(int argc, char *argv[])
71 {
72     trace::DumpFlags dumpFlags = 0;
73     bool dumpThreadIds = false;
74
75     int i;
76
77     for (i = 0; i < argc;) {
78         const char *arg = argv[i];
79
80         if (arg[0] != '-') {
81             break;
82         }
83
84         ++i;
85
86         if (!strcmp(arg, "--")) {
87             break;
88         } else if (!strcmp(arg, "--help")) {
89             usage();
90             return 0;
91         } else if (strcmp(arg, "-v") == 0 ||
92                    strcmp(arg, "--verbose") == 0) {
93             verbose = true;
94         } else if (!strcmp(arg, "--calls")) {
95             calls = trace::CallSet(argv[i++]);
96         } else if (!strcmp(arg, "--color=auto") ||
97                    !strcmp(arg, "--colour=auto")) {
98             color = COLOR_OPTION_AUTO;
99         } else if (!strcmp(arg, "--color") ||
100                    !strcmp(arg, "--colour") ||
101                    !strcmp(arg, "--color=always") ||
102                    !strcmp(arg, "--colour=always")) {
103             color = COLOR_OPTION_ALWAYS;
104         } else if (!strcmp(arg, "--color=never") ||
105                    !strcmp(arg, "--colour=never") ||
106                    !strcmp(arg, "--no-color") ||
107                    !strcmp(arg, "--no-colour")) {
108             color = COLOR_OPTION_NEVER;
109         } else if (!strcmp(arg, "--no-arg-names")) {
110             dumpFlags |= trace::DUMP_FLAG_NO_ARG_NAMES;
111         } else if (!strcmp(arg, "--thread-ids")) {
112             dumpThreadIds = true;
113         } else {
114             std::cerr << "error: unknown option " << arg << "\n";
115             usage();
116             return 1;
117         }
118     }
119
120     if (color == COLOR_OPTION_AUTO) {
121 #ifdef _WIN32
122         color = COLOR_OPTION_ALWAYS;
123 #else
124         color = isatty(1) ? COLOR_OPTION_ALWAYS : COLOR_OPTION_NEVER;
125         pipepager();
126 #endif
127     }
128
129     if (color == COLOR_OPTION_NEVER) {
130         dumpFlags |= trace::DUMP_FLAG_NO_COLOR;
131     }
132
133     for (; i < argc; ++i) {
134         trace::Parser p;
135
136         if (!p.open(argv[i])) {
137             std::cerr << "error: failed to open " << argv[i] << "\n";
138             return 1;
139         }
140
141         trace::Call *call;
142         while ((call = p.parse_call())) {
143             if (calls.contains(*call)) {
144                 if (verbose ||
145                     !(call->flags & trace::CALL_FLAG_VERBOSE)) {
146                     if (dumpThreadIds) {
147                         std::cout << std::hex << call->thread_id << std::dec << " ";
148                     }
149                     trace::dump(*call, std::cout, dumpFlags);
150                 }
151             }
152             delete call;
153         }
154     }
155
156     return 0;
157 }
158
159 const Command dump_command = {
160     "dump",
161     synopsis,
162     usage,
163     command
164 };