summaryrefslogtreecommitdiff
path: root/cli/cli_trace.cpp
blob: 68526cb4b01c69a5ce559f93a2227434d20cfe90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*********************************************************************
 *
 * Copyright 2011 Intel Corporation
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 *********************************************************************/


#include <assert.h>
#include <string.h>

#include <iostream>

#include "cli.hpp"

#include "trace_tools.hpp"


static const char *synopsis = "Generate a new trace by executing the given program.";

static void
usage(void)
{
    std::cout << "usage: apitrace trace PROGRAM [ARGS ...]\n"
        << synopsis << "\n"
        "\n"
        "    The given program will be executed with the given arguments.\n"
        "    During execution, all OpenGL calls will be captured to a trace\n"
        "    file. That trace file can then be used\n"
        "    with other apitrace utilities for replay or analysis.\n"
        "\n"
        "    -v, --verbose       verbose output\n"
        "    -a, --api API       specify API to trace (gl or egl);\n"
        "                        default is `gl`\n"
        "    -o, --output TRACE  specify output trace file;\n"
        "                        default is `PROGRAM.trace`\n";
}

static int
command(int argc, char *argv[])
{
    bool verbose = false;
    trace::API api = trace::API_GL;
    const char *output = NULL;
    int i;

    for (i = 1; i < argc; ) {
        const char *arg = argv[i];

        if (arg[0] != '-') {
            break;
        }

        ++i;

        if (strcmp(arg, "--") == 0) {
            break;
        } else if (strcmp(arg, "--help") == 0) {
            usage();
            return 0;
        } else if (strcmp(arg, "-v") == 0 ||
                   strcmp(arg, "--verbose") == 0) {
            verbose = true;
        } else if (strcmp(arg, "-a") == 0 ||
                   strcmp(arg, "--api") == 0) {
            arg = argv[i++];
            if (strcmp(arg, "gl") == 0) {
                api = trace::API_GL;
            } else if (strcmp(arg, "egl") == 0) {
                api = trace::API_EGL;
            } else {
                std::cerr << "error: unknown API `" << arg << "`\n";
                usage();
                return 1;
            }
        } else if (strcmp(arg, "-o") == 0 ||
                   strcmp(arg, "--output") == 0) {
            output = argv[i++];
        } else {
            std::cerr << "error: unknown option " << arg << "\n";
            usage();
            return 1;
        }
    }

    if (i == argc) {
        std::cerr << "error: no command specified\n";
        usage();
        return 1;
    }

    assert(argv[argc] == 0);
    return trace::traceProgram(api, argv + i, output, verbose);
}

const Command trace_command = {
    "trace",
    synopsis,
    usage,
    command
};