blob: ebb92d8606c49fa24004ccd928e4e700bb8fc551 [file] [log] [blame]
José Fonseca7e329022010-11-19 17:05:18 +00001/**************************************************************************
2 *
3 * Copyright 2010 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 **************************************************************************/
25
José Fonseca71913ee2011-10-30 13:35:33 +000026#include <string.h>
27
José Fonseca5cc28b02011-10-30 13:38:25 +000028#include "cli.hpp"
José Fonsecab122f502011-12-10 18:07:25 +000029#include "cli_pager.hpp"
José Fonseca822d20a2011-08-20 13:49:40 +010030
José Fonseca7e329022010-11-19 17:05:18 +000031#include "trace_parser.hpp"
32
Carl Worthe525e6f2011-10-20 15:22:09 -070033enum ColorOption {
34 COLOR_OPTION_NEVER = 0,
35 COLOR_OPTION_ALWAYS = 1,
36 COLOR_OPTION_AUTO = -1
37};
38
39static ColorOption color = COLOR_OPTION_AUTO;
José Fonseca822d20a2011-08-20 13:49:40 +010040
José Fonseca340f5692011-11-30 07:04:44 +000041static bool verbose = false;
42
José Fonseca3c167fe2011-10-30 14:21:03 +000043static const char *synopsis = "Dump given trace(s) to standard output.";
44
45static void
46usage(void)
Carl Worthbd5e1642011-10-21 20:40:56 -070047{
José Fonseca3c167fe2011-10-30 14:21:03 +000048 std::cout
49 << "usage: apitrace dump [OPTIONS] <trace-file>...\n"
50 << synopsis << "\n"
José Fonseca646a00d2011-10-30 14:07:20 +000051 "\n"
José Fonseca340f5692011-11-30 07:04:44 +000052 " -v, --verbose verbose output\n"
José Fonseca646a00d2011-10-30 14:07:20 +000053 " --color=<WHEN>\n"
54 " --colour=<WHEN> Colored syntax highlighting\n"
55 " WHEN is 'auto', 'always', or 'never'\n";
José Fonseca822d20a2011-08-20 13:49:40 +010056}
57
José Fonseca3c167fe2011-10-30 14:21:03 +000058static int
José Fonsecaffba5972011-10-30 14:29:28 +000059command(int argc, char *argv[])
José Fonseca7e329022010-11-19 17:05:18 +000060{
José Fonseca822d20a2011-08-20 13:49:40 +010061 int i;
62
José Fonsecaffba5972011-10-30 14:29:28 +000063 for (i = 0; i < argc; ++i) {
José Fonseca822d20a2011-08-20 13:49:40 +010064 const char *arg = argv[i];
65
66 if (arg[0] != '-') {
67 break;
68 }
69
70 if (!strcmp(arg, "--")) {
71 break;
José Fonseca646a00d2011-10-30 14:07:20 +000072 } else if (!strcmp(arg, "--help")) {
José Fonseca3c167fe2011-10-30 14:21:03 +000073 usage();
José Fonseca646a00d2011-10-30 14:07:20 +000074 return 0;
José Fonseca340f5692011-11-30 07:04:44 +000075 } else if (strcmp(arg, "-v") == 0 ||
76 strcmp(arg, "--verbose") == 0) {
77 verbose = true;
Carl Worthe525e6f2011-10-20 15:22:09 -070078 } else if (!strcmp(arg, "--color=auto") ||
79 !strcmp(arg, "--colour=auto")) {
80 color = COLOR_OPTION_AUTO;
81 } else if (!strcmp(arg, "--color") ||
82 !strcmp(arg, "--colour") ||
83 !strcmp(arg, "--color=always") ||
84 !strcmp(arg, "--colour=always")) {
85 color = COLOR_OPTION_ALWAYS;
86 } else if (!strcmp(arg, "--color=never") ||
87 !strcmp(arg, "--colour=never") ||
88 !strcmp(arg, "--no-color") ||
José Fonseca822d20a2011-08-20 13:49:40 +010089 !strcmp(arg, "--no-colour")) {
Carl Worthe525e6f2011-10-20 15:22:09 -070090 color = COLOR_OPTION_NEVER;
José Fonseca822d20a2011-08-20 13:49:40 +010091 } else {
92 std::cerr << "error: unknown option " << arg << "\n";
José Fonseca3c167fe2011-10-30 14:21:03 +000093 usage();
José Fonseca822d20a2011-08-20 13:49:40 +010094 return 1;
95 }
96 }
97
Carl Worthe525e6f2011-10-20 15:22:09 -070098 if (color == COLOR_OPTION_AUTO) {
99#ifdef _WIN32
100 color = COLOR_OPTION_ALWAYS;
101#else
102 color = isatty(1) ? COLOR_OPTION_ALWAYS : COLOR_OPTION_NEVER;
José Fonsecab122f502011-12-10 18:07:25 +0000103 pipepager();
Carl Worthe525e6f2011-10-20 15:22:09 -0700104#endif
105 }
106
José Fonseca822d20a2011-08-20 13:49:40 +0100107 for (; i < argc; ++i) {
José Fonsecab4a3d142011-10-27 07:43:19 +0100108 trace::Parser p;
José Fonseca610ed332011-06-04 22:55:42 +0100109
110 if (!p.open(argv[i])) {
111 std::cerr << "error: failed to open " << argv[i] << "\n";
112 return 1;
113 }
114
José Fonsecab4a3d142011-10-27 07:43:19 +0100115 trace::Call *call;
José Fonseca610ed332011-06-04 22:55:42 +0100116 while ((call = p.parse_call())) {
José Fonseca340f5692011-11-30 07:04:44 +0000117 if (verbose ||
118 !(call->flags & trace::CALL_FLAG_VERBOSE)) {
119 call->dump(std::cout, color);
120 }
José Fonseca610ed332011-06-04 22:55:42 +0100121 delete call;
José Fonseca19828972010-11-29 20:34:32 +0000122 }
123 }
José Fonseca822d20a2011-08-20 13:49:40 +0100124
José Fonseca19828972010-11-29 20:34:32 +0000125 return 0;
José Fonseca7e329022010-11-19 17:05:18 +0000126}
José Fonseca3c167fe2011-10-30 14:21:03 +0000127
Carl Worth68f7c982011-11-01 13:47:26 -0700128const Command dump_command = {
José Fonseca3c167fe2011-10-30 14:21:03 +0000129 "dump",
130 synopsis,
131 usage,
132 command
133};