summaryrefslogtreecommitdiff
path: root/apitrace.cpp
blob: d180e727663f1bfd517c41c4d4ba585080cdb1f7 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*********************************************************************
 *
 * 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.
 *
 *********************************************************************/


/*
 * Top-level application for accessing most all apitrace
 * functionality.
 */

#include <iostream>
#include <iomanip>

#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include <talloc.h>

#include "config.h"

#define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))

typedef int (*command_function_t) (int argc, char *argv[], int first_command_arg);

typedef struct command {
    const char *name;
    command_function_t function;
    const char *arguments;
    const char *summary;
    const char *documentation;
} command_t;

static int
apitrace_trace_command(int argc, char *argv[], int first_command_arg);

static int
apitrace_help_command(int argc, char *argv[], int first_command_arg);

static command_t commands[] = {
    { "trace", apitrace_trace_command,
      "<program> [<args> ...]",
      "Trace execution of <program>, writing to <program>.trace",
      "\tThe given program will be executed with the given arguments.\n"
      "\tDuring execution, all OpenGL calls will be captured to a trace\n"
      "\tfile named <program>.trace. That trace file can then be used\n"
      "\twith other apitrace utilities for replay or analysis." },
    { "help", apitrace_help_command,
      "[<command>]",
      "Print detailed help for the given command.",
      "\tExcept in this case, where this is all the help you will get." }
};

static void
usage(void)
{
    command_t *command;
    int i, max_width = 0;

    std::cout <<
        "Usage: apitrace <command> [<args> ...]\n\n"
	"The available commands are as follows:\n\n";

    std::cout << std::setiosflags(std::ios::left);

    for (i = 0; i < ARRAY_SIZE(commands); i++) {
	command = &commands[i];
	if (strlen(command->name) > max_width)
	    max_width = strlen(command->name);
    }

    for (i = 0; i < ARRAY_SIZE(commands); i++) {
	command = &commands[i];

	std::cout << " " << std::setw(max_width+2) << command->name
		  << " " << command->summary << "\n";
    }

    std::cout << "\n"
	"Use \"apitrace help <command>\" for more details on each command.\n";
}

static int
apitrace_help_command(int argc, char *argv[], int first_arg_command)
{
    command_t *command;
    int i;

    if(argc == 0) {
	usage();
	return 0;
    }

    for (i = 0; i < ARRAY_SIZE(commands); i++) {
	command = &commands[i];

	if (strcmp(argv[0], command->name) == 0) {
	    std::cout << "Help for \"apitrace " << argv[0] << "\":\n\n";
	    std::cout << command->name;
	    if (command->arguments)
		std::cout << " " << command->arguments
			  << "\n\n\t" << command->summary;
	    else
		std::cout << "\t" << command->summary;
	    std::cout << "\n\n" << command->documentation << "\n\n";

	    return 0;
	}
    }

    std::cerr << "Error: Unknown command: " << argv[0]
	      << " (see \"apitrace help\").\n";

    return 1;
}

/* Returns whether path exists as a regular file. */
static int
_executable_exists(const char *path)
{
    struct stat st;
    int err;

    err = stat(path, &st);
    if (err)
	return 0;

    if (! S_ISREG(st.st_mode))
	return 0;

    return 1;
}

/* Given an relative 'path', return a newly allocated string giving
 * the absolute version of path.
 *
 * Will return 'path' itself if it is already absolute, or if it is
 * NULL or an empty string. */
static char *
_make_absolute(void *ctx, char *path)
{
    char *cwd, *absolute;

    if (path == NULL || *path == '\0' || *path == '/')
	return path;

    cwd = get_current_dir_name();

    absolute = talloc_asprintf(ctx, "%s/%s", cwd, path);

    free (cwd);

    return absolute;
}

/* Given argv0 determine the directory from which the program was
 * run. If argv0 contains a '/' the directory is simply read from the
 * string itself. Otherwise, this function searches for the executable
 * by examining each directory within the PATH environment variable.
 *
 * Returns an empty string if argv0 is NULL or an empty string, or if
 * a path cannot be resolved.
 *
 * Note: This function may return a relative directory. Call
 * _make_absolute on the result if you need an absolute directory.
 */
char *
_find_argv0_directory(void *ctx, const char *argv0)
{
    const char *last_slash, *colon, *path;

    if (argv0 == NULL || argv0[0] == '\0')
	return talloc_strdup(ctx, "");

    last_slash = argv0 + strlen(argv0);

    while (last_slash > argv0 && *last_slash != '/')
	last_slash--;

    /* Contains a '/'. Copy argv0 to just before final '/'. */
    if (last_slash > argv0)
	return talloc_strndup(ctx, argv0, last_slash - argv0);

    /* Contains no '/'. Search PATH. */
    path = getenv("PATH");
    while (1) {
	char *dir;

	if (*path == '\0')
	    break;

	colon = strchr(path, ':');
	if (colon == NULL)
	    colon = path + strlen(path);

	dir = talloc_strndup(ctx, path, colon - path);
	if (dir && *dir) {
	    char *complete = talloc_asprintf(ctx, "%s/%s", dir, argv0);
	    if (_executable_exists(complete)) {
		talloc_free(complete);
		return dir;
	    }
	}
	talloc_free(dir);

	path = colon;
	while (*path == ':')
	    path++;
    }

    return talloc_strdup(ctx, "");
}

/* Find 'executable' by looking first in the apitrace private bin
 * directory (for example /usr/lib/apitrace), and then in the same
 * directory from which the current process was likely executed as
 * determined by 'argv0' and the PATH environment variable. */
static char *
_find_private_bin(void *ctx, const char *executable, const char *argv0)
{
    char *path, *dir;

    /* First look in our private bin directory. */
    path = talloc_asprintf(ctx, "%s/%s", APITRACE_PRIVATE_BIN_DIR, executable);

    if (_executable_exists(path))
	return path;

    talloc_free(path);

    /* Otherwise, look in the directory from which this executable was run. */
    dir = _find_argv0_directory(ctx, argv0);
    path = talloc_asprintf(ctx, "%s/%s",dir, executable);
    talloc_free(dir);

    if (_executable_exists(path))
	return path;

    std::cerr << "Error: Cannot find executable " << executable << ".\n";
    exit (1);
}

static int
apitrace_trace_command(int argc, char *argv[], int first_arg_command)
{
    char *path;
    void *ctx = talloc_new(NULL);

    path = _find_private_bin(ctx, "glxtrace.so", argv[0]);

    path = _make_absolute (ctx, path);

    setenv("LD_PRELOAD", path, 1);

    talloc_free(ctx);

    execvp(argv[first_arg_command], argv + first_arg_command);

    std::cerr << "Error: Failed to execvp " << argv[first_arg_command] << "\n";

    return 1;
}

int
main(int argc, char **argv)
{
    const char *command_name = "trace";
    command_t *command;
    int i, first_command_arg;

    if (argc == 1) {
	usage();
	return 1;
    }

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

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

	if (strcmp(arg, "--help") == 0) {
	    return apitrace_help_command (0, NULL, 0);
        } else {
            std::cerr << "Error: unknown option " << arg << "\n";
            usage();
            return 1;
        }
    }
    first_command_arg = i;

    if (first_command_arg < argc) {
	command_name = argv[first_command_arg];
	first_command_arg++;
    }

    for (i = 0; i < ARRAY_SIZE(commands); i++) {
	command = &commands[i];

	if (strcmp(command_name, command->name) == 0)
	    return (command->function) (argc, argv, first_command_arg);
    }

    std::cerr << "Error: unknown command " << command_name
	      << " (see \"apitrace help\").\n";

    return 1;
}