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