]> git.cworth.org Git - apitrace/blob - cli/cli_dump_images.cpp
cli: Fix invalid option message.
[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
33 #include <string>
34 #include <iostream>
35
36 #include "os_string.hpp"
37
38 #include "cli.hpp"
39 #include "cli_retrace.hpp"
40
41 static const char *synopsis = "Dump frame images obtained from a trace.";
42
43 static void
44 usage(void)
45 {
46     std::cout << "usage apitrace dump-images [OPTIONS] TRACE_FILE\n"
47               << synopsis << "\n"
48         "\n"
49         "    -h, --help             show this help message and exit\n"
50         "        --calls=CALLSET    dump images only for specified calls\n"
51         "                           (default value is \"*/frame\" which\n"
52         "                            which dumps an image for each frame)\n"
53         "         --call-nos[=BOOL] use call numbers in image filenames,\n"
54         "                           otherwise use sequental numbers (default=yes)\n"
55         "    -o, --output=PREFIX    prefix to use in naming output files\n"
56         "                           (default is trace filename without extension)\n"
57         "\n";
58 }
59
60 enum {
61     CALLS_OPT = CHAR_MAX + 1,
62     CALL_NOS_OPT,
63 };
64
65 const static char *
66 shortOptions = "ho:";
67
68 const static struct option
69 longOptions[] = {
70     {"help", no_argument, 0, 'h'},
71     {"calls", required_argument, 0, CALLS_OPT},
72     {"call-nos", optional_argument, 0, CALL_NOS_OPT},
73     {"output", required_argument, 0, 'o'},
74     {0, 0, 0, 0}
75 };
76
77 static int
78 command(int argc, char *argv[])
79 {
80     os::String prefix;
81     const char *calls = NULL;
82     const char *traceName = NULL;
83     const char *output = NULL;
84     std::string call_nos;
85
86     int opt;
87     while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
88         switch (opt) {
89         case 'h':
90             usage();
91             return 0;
92         case CALLS_OPT:
93             calls = optarg;
94             break;
95         case CALL_NOS_OPT:
96             call_nos = "--call-nos=";
97             call_nos.append(optarg);
98             break;
99         case 'o':
100             output = optarg;
101             break;
102         default:
103             std::cerr << "error: unexpected option `" << (char)opt << "`\n";
104             usage();
105             return 1;
106         }
107     }
108
109     if (optind >= argc) {
110         std::cerr << "error: apitrace dump-images requires a trace file as an argument.\n";
111         usage();
112         return 1;
113     }
114
115     if (optind < argc - 1) { 
116         std::cerr << "error: apitrace dump-images can accept only a single trace file argument.\n";
117         usage();
118         return 1;
119     }
120
121     traceName = argv[optind];
122
123     if (output == NULL) {
124         prefix = traceName;
125         prefix.trimDirectory();
126         prefix.trimExtension();
127         prefix.append('.');
128         output = prefix.str();
129     }
130
131     std::vector<const char *> opts;
132
133     opts.push_back("-s");
134     opts.push_back(output);
135     opts.push_back("-S");
136     if (calls)
137         opts.push_back(calls);
138     else
139         opts.push_back("*/frame");
140     if (!call_nos.empty()) {
141         opts.push_back(call_nos.c_str());
142     }
143
144     return executeRetrace(opts, traceName);
145 }
146
147 const Command dump_images_command = {
148     "dump-images",
149     synopsis,
150     usage,
151     command
152 };