blob: 187fc8749141a7f3d352d6f53fc21334eef724b1 [file] [log] [blame]
Pekka Paalanenb9fdc142017-10-12 13:13:44 +02001/*
2 * Copyright © 2017 Pekka Paalanen <pq@iki.fi>
3 * Copyright © 2018 Zodiac Inflight Innovations
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27#include "config.h"
28
29#include <errno.h>
30#include <stdbool.h>
31#include <stdint.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <unistd.h>
36#include <getopt.h>
37#include <assert.h>
38#include <sys/types.h>
39#include <sys/stat.h>
40#include <fcntl.h>
41
42#include <wayland-client.h>
43
44#include "shared/helpers.h"
Pekka Paalanenecbdcfd2019-04-04 14:46:00 +030045#include <libweston/zalloc.h>
Pekka Paalanenb9fdc142017-10-12 13:13:44 +020046#include "weston-debug-client-protocol.h"
47
48struct debug_app {
49 struct {
50 bool help;
51 bool list;
52 bool bind_all;
53 char *output;
54 char *outfd;
55 } opt;
56
57 int out_fd;
58 struct wl_display *dpy;
59 struct wl_registry *registry;
60 struct weston_debug_v1 *debug_iface;
61 struct wl_list stream_list;
62};
63
64struct debug_stream {
65 struct wl_list link;
66 bool should_bind;
67 char *name;
68 char *desc;
69 struct weston_debug_stream_v1 *obj;
70};
71
72/**
73 * Called either through stream_find in response to an advertisement
74 * event (see comment on stream_find) when we have all the information,
75 * or directly from option parsing to make a placeholder entry when the
76 * stream was explicitly named on the command line to bind to.
77 */
78static struct debug_stream *
79stream_alloc(struct debug_app *app, const char *name, const char *desc)
80{
81 struct debug_stream *stream;
82
83 stream = zalloc(sizeof *stream);
84 if (!stream)
85 return NULL;
86
87 stream->name = strdup(name);
88 if (!stream->name) {
89 free(stream);
90 return NULL;
91 }
92
93 if (desc) {
94 stream->desc = strdup(desc);
95 if (!stream->desc) {
96 free(stream->name);
97 free(stream);
98 return NULL;
99 }
100 }
101
102 stream->should_bind = app->opt.bind_all;
103 wl_list_insert(app->stream_list.prev, &stream->link);
104
105 return stream;
106}
107
108/**
109 * Called in response to a stream advertisement event. If our stream was
110 * manually specified on the command line, then it will already have a
111 * dummy entry in stream_list: we fill in its description and return.
112 * If there's no entry in the list, we make a new one and return that.
113 */
114static struct debug_stream *
115stream_find(struct debug_app *app, const char *name, const char *desc)
116{
117 struct debug_stream *stream;
118
119 wl_list_for_each(stream, &app->stream_list, link) {
120 if (strcmp(stream->name, name) == 0) {
121 assert(stream->desc == NULL);
122 if (desc)
123 stream->desc = strdup(desc);
124 return stream;
125 }
126 }
127
128 return stream_alloc(app, name, desc);
129}
130
131static void
132stream_destroy(struct debug_stream *stream)
133{
134 if (stream->obj)
135 weston_debug_stream_v1_destroy(stream->obj);
136
137 wl_list_remove(&stream->link);
Marius Vlad568d04f2021-06-01 11:26:24 +0300138 free(stream->desc);
Pekka Paalanenb9fdc142017-10-12 13:13:44 +0200139 free(stream->name);
140 free(stream);
141}
142
143static void
144destroy_streams(struct debug_app *app)
145{
146 struct debug_stream *stream;
147 struct debug_stream *tmp;
148
149 wl_list_for_each_safe(stream, tmp, &app->stream_list, link)
150 stream_destroy(stream);
151}
152
153static void
154debug_advertise(void *data, struct weston_debug_v1 *debug, const char *name,
155 const char *desc)
156{
157 struct debug_app *app = data;
158 (void) stream_find(app, name, desc);
159}
160
161static const struct weston_debug_v1_listener debug_listener = {
162 debug_advertise,
163};
164
165static void
166global_handler(void *data, struct wl_registry *registry, uint32_t id,
167 const char *interface, uint32_t version)
168{
169 struct debug_app *app = data;
170 uint32_t myver;
171
172 assert(app->registry == registry);
173
174 if (!strcmp(interface, weston_debug_v1_interface.name)) {
175 if (app->debug_iface)
176 return;
177
178 myver = MIN(1, version);
179 app->debug_iface =
180 wl_registry_bind(registry, id,
181 &weston_debug_v1_interface, myver);
182 weston_debug_v1_add_listener(app->debug_iface, &debug_listener,
183 app);
184 }
185}
186
187static void
188global_remove_handler(void *data, struct wl_registry *registry, uint32_t name)
189{
190}
191
192static const struct wl_registry_listener registry_listener = {
193 global_handler,
194 global_remove_handler
195};
196
197static void
198handle_stream_complete(void *data, struct weston_debug_stream_v1 *obj)
199{
200 struct debug_stream *stream = data;
201
202 assert(stream->obj == obj);
203
204 stream_destroy(stream);
205}
206
207static void
208handle_stream_failure(void *data, struct weston_debug_stream_v1 *obj,
209 const char *msg)
210{
211 struct debug_stream *stream = data;
212
213 assert(stream->obj == obj);
214
215 fprintf(stderr, "Debug stream '%s' aborted: %s\n", stream->name, msg);
216
217 stream_destroy(stream);
218}
219
220static const struct weston_debug_stream_v1_listener stream_listener = {
221 handle_stream_complete,
222 handle_stream_failure
223};
224
225static void
226start_streams(struct debug_app *app)
227{
228 struct debug_stream *stream;
229
230 wl_list_for_each(stream, &app->stream_list, link) {
231 if (!stream->should_bind)
232 continue;
233
234 stream->obj = weston_debug_v1_subscribe(app->debug_iface,
235 stream->name,
236 app->out_fd);
237 weston_debug_stream_v1_add_listener(stream->obj,
238 &stream_listener, stream);
239 }
240}
241
242static void
243list_streams(struct debug_app *app)
244{
245 struct debug_stream *stream;
246
247 fprintf(stderr, "Available debug streams:\n");
248
249 wl_list_for_each(stream, &app->stream_list, link) {
250 if (stream->should_bind && stream->desc) {
251 fprintf(stderr, " %s [will bind]\n", stream->name);
252 fprintf(stderr, " %s\n", stream->desc);
253 } else if (stream->should_bind) {
254 fprintf(stderr, " %s [wanted but not found]\n",
255 stream->name);
256 } else {
257 fprintf(stderr, " %s [will not bind]\n",
258 stream->name);
259 fprintf(stderr, " %s\n", stream->desc);
260 }
261 }
262}
263
264static int
265setup_out_fd(const char *output, const char *outfd)
266{
267 int fd = -1;
268 int flags;
269
270 assert(!(output && outfd));
271
272 if (output) {
273 if (strcmp(output, "-") == 0) {
274 fd = STDOUT_FILENO;
275 } else {
276 fd = open(output,
277 O_WRONLY | O_APPEND | O_CREAT, 0644);
278 if (fd < 0) {
279 fprintf(stderr,
Antonio Borneo39578632019-04-26 23:57:31 +0200280 "Error: opening file '%s' failed: %s\n",
281 output, strerror(errno));
Pekka Paalanenb9fdc142017-10-12 13:13:44 +0200282 }
283 return fd;
284 }
285 } else if (outfd) {
286 fd = atoi(outfd);
287 } else {
288 fd = STDOUT_FILENO;
289 }
290
291 flags = fcntl(fd, F_GETFL);
292 if (flags == -1) {
293 fprintf(stderr,
Antonio Borneo39578632019-04-26 23:57:31 +0200294 "Error: cannot use file descriptor %d: %s\n", fd,
295 strerror(errno));
Pekka Paalanenb9fdc142017-10-12 13:13:44 +0200296 return -1;
297 }
298
299 if ((flags & O_ACCMODE) != O_WRONLY &&
300 (flags & O_ACCMODE) != O_RDWR) {
301 fprintf(stderr,
302 "Error: file descriptor %d is not writable.\n", fd);
303 return -1;
304 }
305
306 return fd;
307}
308
309static void
310print_help(void)
311{
312 fprintf(stderr,
313 "Usage: weston-debug [options] [names]\n"
314 "Where options may be:\n"
315 " -h, --help\n"
316 " This help text, and exit with success.\n"
317 " -l, --list\n"
318 " Print a list of available debug streams to stderr.\n"
319 " -a, --all-streams\n"
320 " Bind to all available streams.\n"
321 " -o FILE, --output FILE\n"
322 " Direct output to file named FILE. Use - for stdout.\n"
323 " Stdout is the default. Mutually exclusive with -f.\n"
324 " -f FD, --outfd FD\n"
325 " Direct output to the file descriptor FD.\n"
326 " Stdout (1) is the default. Mutually exclusive with -o.\n"
327 "Names are whatever debug stream names the compositor supports.\n"
328 );
329}
330
331static int
332parse_cmdline(struct debug_app *app, int argc, char **argv)
333{
334 static const struct option opts[] = {
335 { "help", no_argument, NULL, 'h' },
336 { "list", no_argument, NULL, 'l' },
337 { "all-streams", no_argument, NULL, 'a' },
338 { "output", required_argument, NULL, 'o' },
339 { "outfd", required_argument, NULL, 'f' },
340 { 0 }
341 };
342 static const char optstr[] = "hlao:f:";
343 int c;
344 bool failed = false;
345
346 while (1) {
347 c = getopt_long(argc, argv, optstr, opts, NULL);
348 if (c == -1)
349 break;
350
351 switch (c) {
352 case 'h':
353 app->opt.help = true;
354 break;
355 case 'l':
356 app->opt.list = true;
357 break;
358 case 'a':
359 app->opt.bind_all = true;
360 break;
361 case 'o':
362 free(app->opt.output);
363 app->opt.output = strdup(optarg);
364 break;
365 case 'f':
366 free(app->opt.outfd);
367 app->opt.outfd = strdup(optarg);
368 break;
369 case '?':
370 failed = true;
371 break;
372 default:
373 fprintf(stderr, "huh? getopt => %c (%d)\n", c, c);
374 failed = true;
375 }
376 }
377
378 if (failed)
379 return -1;
380
381 while (optind < argc) {
382 struct debug_stream *stream =
383 stream_alloc(app, argv[optind++], NULL);
384 stream->should_bind = true;
385 }
386
387 return 0;
388}
389
390int
391main(int argc, char **argv)
392{
393 struct debug_app app = {};
394 int ret = 0;
395
396 wl_list_init(&app.stream_list);
397 app.out_fd = -1;
398
399 if (parse_cmdline(&app, argc, argv) < 0) {
400 ret = 1;
401 goto out_parse;
402 }
403
404 if (app.opt.help) {
405 print_help();
406 goto out_parse;
407 }
408
409 if (!app.opt.list && !app.opt.bind_all &&
410 wl_list_empty(&app.stream_list)) {
411 fprintf(stderr, "Error: no options given.\n\n");
412 ret = 1;
413 print_help();
414 goto out_parse;
415 }
416
417 if (app.opt.bind_all && !wl_list_empty(&app.stream_list)) {
418 fprintf(stderr, "Error: --all and specific stream names cannot be used simultaneously.\n");
419 ret = 1;
420 goto out_parse;
421 }
422
423 if (app.opt.output && app.opt.outfd) {
424 fprintf(stderr, "Error: options --output and --outfd cannot be used simultaneously.\n");
425 ret = 1;
426 goto out_parse;
427 }
428
429 app.out_fd = setup_out_fd(app.opt.output, app.opt.outfd);
430 if (app.out_fd < 0) {
431 ret = 1;
432 goto out_parse;
433 }
434
435 app.dpy = wl_display_connect(NULL);
436 if (!app.dpy) {
Antonio Borneo39578632019-04-26 23:57:31 +0200437 fprintf(stderr, "Error: Could not connect to Wayland display: %s\n",
438 strerror(errno));
Pekka Paalanenb9fdc142017-10-12 13:13:44 +0200439 ret = 1;
440 goto out_parse;
441 }
442
443 app.registry = wl_display_get_registry(app.dpy);
444 wl_registry_add_listener(app.registry, &registry_listener, &app);
445 wl_display_roundtrip(app.dpy);
446
447 if (!app.debug_iface) {
448 ret = 1;
449 fprintf(stderr,
450 "The Wayland server does not support %s interface.\n",
451 weston_debug_v1_interface.name);
452 goto out_conn;
453 }
454
455 wl_display_roundtrip(app.dpy); /* for weston_debug_v1::advertise */
456
457 if (app.opt.list)
458 list_streams(&app);
459
460 start_streams(&app);
461
462 weston_debug_v1_destroy(app.debug_iface);
463
464 while (1) {
465 struct debug_stream *stream;
466 bool empty = true;
467
468 wl_list_for_each(stream, &app.stream_list, link) {
469 if (stream->obj) {
470 empty = false;
471 break;
472 }
473 }
474
475 if (empty)
476 break;
477
478 if (wl_display_dispatch(app.dpy) < 0) {
479 ret = 1;
480 break;
481 }
482 }
483
484out_conn:
485 destroy_streams(&app);
486
487 /* Wait for server to close all files */
488 wl_display_roundtrip(app.dpy);
489
490 wl_registry_destroy(app.registry);
491 wl_display_disconnect(app.dpy);
492
493out_parse:
494 if (app.out_fd != -1)
495 close(app.out_fd);
496
497 destroy_streams(&app);
498 free(app.opt.output);
499 free(app.opt.outfd);
500
501 return ret;
502}