]> git.cworth.org Git - apitrace/blob - cli/cli_dump_images.cpp
34e4d05d130dcb5162ab0adcc793f77c73710d56
[apitrace] / cli / cli_dump_images.cpp
1 /*********************************************************************
2  *
3  * Copyright 2011 Jose Fonseca
4  * Copyright 2012 Intel Corporation
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person
8  * obtaining a copy of this software and associated documentation
9  * files (the "Software"), to deal in the Software without
10  * restriction, including without limitation the rights to use, copy,
11  * modify, merge, publish, distribute, sublicense, and/or sell copies
12  * of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25  * SOFTWARE.
26  *
27  *********************************************************************/
28
29 #include <string.h>
30 #include <limits.h> // for CHAR_MAX
31 #include <getopt.h>
32 #include <iostream>
33
34 #include "os_string.hpp"
35
36 #include "cli.hpp"
37 #include "cli_retrace.hpp"
38
39 static const char *synopsis = "Dump frame images obtained from a trace.";
40
41 static void
42 usage(void)
43 {
44     std::cout << "usage apitrace dump-images [OPTIONS] TRACE_FILE\n"
45               << synopsis << "\n"
46         "\n"
47         "    -h, --help             show this help message and exit\n"
48         "        --calls=CALLSET    dump images only for specified calls\n"
49         "                           (default value is \"*/frame\" which\n"
50         "                            which dumps an image for each frame)\n"
51         "         --call-nos[=BOOL] use call numbers in image filenames,\n"
52         "                           otherwise use sequental numbers (default=yes)\n"
53         "    -o, --output=PREFIX    prefix to use in naming output files\n"
54         "                           (default is trace filename without extension)\n"
55         "\n";
56 }
57
58 enum {
59     CALLS_OPT = CHAR_MAX + 1,
60     CALL_NOS_OPT,
61 };
62
63 const static char *
64 shortOptions = "ho:";
65
66 const static struct option
67 longOptions[] = {
68     {"help", no_argument, 0, 'h'},
69     {"calls", required_argument, 0, CALLS_OPT},
70     {"call-nos", optional_argument, 0, CALL_NOS_OPT},
71     {"output", required_argument, 0, 'o'},
72     {0, 0, 0, 0}
73 };
74
75 static int
76 command(int argc, char *argv[])
77 {
78     os::String prefix;
79     const char *calls = NULL;
80     const char *traceName = NULL;
81     const char *output = NULL;
82     std::string call_nos;
83
84     int opt;
85     while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
86         switch (opt) {
87         case 'h':
88             usage();
89             return 0;
90         case CALLS_OPT:
91             calls = optarg;
92             break;
93         case CALL_NOS_OPT:
94             call_nos = std::string("--call-nos=") + optarg;
95             break;
96         case 'o':
97             output = optarg;
98             break;
99         default:
100             std::cerr << "error: unexpected option `" << opt << "`\n";
101             usage();
102             return 1;
103         }
104     }
105
106     if (optind >= argc) {
107         std::cerr << "error: apitrace dump-images requires a trace file as an argument.\n";
108         usage();
109         return 1;
110     }
111
112     if (optind < argc - 1) { 
113         std::cerr << "error: apitrace dump-images can accept only a single trace file argument.\n";
114         usage();
115         return 1;
116     }
117
118     traceName = argv[optind];
119
120     if (output == NULL) {
121         prefix = traceName;
122         prefix.trimDirectory();
123         prefix.trimExtension();
124         prefix.append('.');
125         output = prefix.str();
126     }
127
128     std::vector<const char *> opts;
129
130     opts.push_back("-s");
131     opts.push_back(output);
132     opts.push_back("-S");
133     if (calls)
134         opts.push_back(calls);
135     else
136         opts.push_back("*/frame");
137     if (!call_nos.empty()) {
138         opts.push_back(call_nos.c_str());
139     }
140
141     return executeRetrace(opts, traceName);
142 }
143
144 const Command dump_images_command = {
145     "dump-images",
146     synopsis,
147     usage,
148     command
149 };