1 /*
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"
45 #include <libweston/zalloc.h>
46 #include "weston-debug-client-protocol.h"
47
48 struct 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
64 struct 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 */
78 static struct debug_stream *
stream_alloc(struct debug_app * app,const char * name,const char * desc)79 stream_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 */
114 static struct debug_stream *
stream_find(struct debug_app * app,const char * name,const char * desc)115 stream_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
131 static void
stream_destroy(struct debug_stream * stream)132 stream_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);
138 free(stream->name);
139 free(stream);
140 }
141
142 static void
destroy_streams(struct debug_app * app)143 destroy_streams(struct debug_app *app)
144 {
145 struct debug_stream *stream;
146 struct debug_stream *tmp;
147
148 wl_list_for_each_safe(stream, tmp, &app->stream_list, link)
149 stream_destroy(stream);
150 }
151
152 static void
debug_advertise(void * data,struct weston_debug_v1 * debug,const char * name,const char * desc)153 debug_advertise(void *data, struct weston_debug_v1 *debug, const char *name,
154 const char *desc)
155 {
156 struct debug_app *app = data;
157 (void) stream_find(app, name, desc);
158 }
159
160 static const struct weston_debug_v1_listener debug_listener = {
161 debug_advertise,
162 };
163
164 static void
global_handler(void * data,struct wl_registry * registry,uint32_t id,const char * interface,uint32_t version)165 global_handler(void *data, struct wl_registry *registry, uint32_t id,
166 const char *interface, uint32_t version)
167 {
168 struct debug_app *app = data;
169 uint32_t myver;
170
171 assert(app->registry == registry);
172
173 if (!strcmp(interface, weston_debug_v1_interface.name)) {
174 if (app->debug_iface)
175 return;
176
177 myver = MIN(1, version);
178 app->debug_iface =
179 wl_registry_bind(registry, id,
180 &weston_debug_v1_interface, myver);
181 weston_debug_v1_add_listener(app->debug_iface, &debug_listener,
182 app);
183 }
184 }
185
186 static void
global_remove_handler(void * data,struct wl_registry * registry,uint32_t name)187 global_remove_handler(void *data, struct wl_registry *registry, uint32_t name)
188 {
189 }
190
191 static const struct wl_registry_listener registry_listener = {
192 global_handler,
193 global_remove_handler
194 };
195
196 static void
handle_stream_complete(void * data,struct weston_debug_stream_v1 * obj)197 handle_stream_complete(void *data, struct weston_debug_stream_v1 *obj)
198 {
199 struct debug_stream *stream = data;
200
201 assert(stream->obj == obj);
202
203 stream_destroy(stream);
204 }
205
206 static void
handle_stream_failure(void * data,struct weston_debug_stream_v1 * obj,const char * msg)207 handle_stream_failure(void *data, struct weston_debug_stream_v1 *obj,
208 const char *msg)
209 {
210 struct debug_stream *stream = data;
211
212 assert(stream->obj == obj);
213
214 fprintf(stderr, "Debug stream '%s' aborted: %s\n", stream->name, msg);
215
216 stream_destroy(stream);
217 }
218
219 static const struct weston_debug_stream_v1_listener stream_listener = {
220 handle_stream_complete,
221 handle_stream_failure
222 };
223
224 static void
start_streams(struct debug_app * app)225 start_streams(struct debug_app *app)
226 {
227 struct debug_stream *stream;
228
229 wl_list_for_each(stream, &app->stream_list, link) {
230 if (!stream->should_bind)
231 continue;
232
233 stream->obj = weston_debug_v1_subscribe(app->debug_iface,
234 stream->name,
235 app->out_fd);
236 weston_debug_stream_v1_add_listener(stream->obj,
237 &stream_listener, stream);
238 }
239 }
240
241 static void
list_streams(struct debug_app * app)242 list_streams(struct debug_app *app)
243 {
244 struct debug_stream *stream;
245
246 fprintf(stderr, "Available debug streams:\n");
247
248 wl_list_for_each(stream, &app->stream_list, link) {
249 if (stream->should_bind && stream->desc) {
250 fprintf(stderr, " %s [will bind]\n", stream->name);
251 fprintf(stderr, " %s\n", stream->desc);
252 } else if (stream->should_bind) {
253 fprintf(stderr, " %s [wanted but not found]\n",
254 stream->name);
255 } else {
256 fprintf(stderr, " %s [will not bind]\n",
257 stream->name);
258 fprintf(stderr, " %s\n", stream->desc);
259 }
260 }
261 }
262
263 static int
setup_out_fd(const char * output,const char * outfd)264 setup_out_fd(const char *output, const char *outfd)
265 {
266 int fd = -1;
267 int flags;
268
269 assert(!(output && outfd));
270
271 if (output) {
272 if (strcmp(output, "-") == 0) {
273 fd = STDOUT_FILENO;
274 } else {
275 fd = open(output,
276 O_WRONLY | O_APPEND | O_CREAT, 0644);
277 if (fd < 0) {
278 fprintf(stderr,
279 "Error: opening file '%s' failed: %s\n",
280 output, strerror(errno));
281 }
282 return fd;
283 }
284 } else if (outfd) {
285 fd = atoi(outfd);
286 } else {
287 fd = STDOUT_FILENO;
288 }
289
290 flags = fcntl(fd, F_GETFL);
291 if (flags == -1) {
292 fprintf(stderr,
293 "Error: cannot use file descriptor %d: %s\n", fd,
294 strerror(errno));
295 return -1;
296 }
297
298 if ((flags & O_ACCMODE) != O_WRONLY &&
299 (flags & O_ACCMODE) != O_RDWR) {
300 fprintf(stderr,
301 "Error: file descriptor %d is not writable.\n", fd);
302 return -1;
303 }
304
305 return fd;
306 }
307
308 static void
print_help(void)309 print_help(void)
310 {
311 fprintf(stderr,
312 "Usage: weston-debug [options] [names]\n"
313 "Where options may be:\n"
314 " -h, --help\n"
315 " This help text, and exit with success.\n"
316 " -l, --list\n"
317 " Print a list of available debug streams to stderr.\n"
318 " -a, --all-streams\n"
319 " Bind to all available streams.\n"
320 " -o FILE, --output FILE\n"
321 " Direct output to file named FILE. Use - for stdout.\n"
322 " Stdout is the default. Mutually exclusive with -f.\n"
323 " -f FD, --outfd FD\n"
324 " Direct output to the file descriptor FD.\n"
325 " Stdout (1) is the default. Mutually exclusive with -o.\n"
326 "Names are whatever debug stream names the compositor supports.\n"
327 );
328 }
329
330 static int
parse_cmdline(struct debug_app * app,int argc,char ** argv)331 parse_cmdline(struct debug_app *app, int argc, char **argv)
332 {
333 static const struct option opts[] = {
334 { "help", no_argument, NULL, 'h' },
335 { "list", no_argument, NULL, 'l' },
336 { "all-streams", no_argument, NULL, 'a' },
337 { "output", required_argument, NULL, 'o' },
338 { "outfd", required_argument, NULL, 'f' },
339 { 0 }
340 };
341 static const char optstr[] = "hlao:f:";
342 int c;
343 bool failed = false;
344
345 while (1) {
346 c = getopt_long(argc, argv, optstr, opts, NULL);
347 if (c == -1)
348 break;
349
350 switch (c) {
351 case 'h':
352 app->opt.help = true;
353 break;
354 case 'l':
355 app->opt.list = true;
356 break;
357 case 'a':
358 app->opt.bind_all = true;
359 break;
360 case 'o':
361 free(app->opt.output);
362 app->opt.output = strdup(optarg);
363 break;
364 case 'f':
365 free(app->opt.outfd);
366 app->opt.outfd = strdup(optarg);
367 break;
368 case '?':
369 failed = true;
370 break;
371 default:
372 fprintf(stderr, "huh? getopt => %c (%d)\n", c, c);
373 failed = true;
374 }
375 }
376
377 if (failed)
378 return -1;
379
380 while (optind < argc) {
381 struct debug_stream *stream =
382 stream_alloc(app, argv[optind++], NULL);
383 stream->should_bind = true;
384 }
385
386 return 0;
387 }
388
389 int
main(int argc,char ** argv)390 main(int argc, char **argv)
391 {
392 struct debug_app app = {};
393 int ret = 0;
394
395 wl_list_init(&app.stream_list);
396 app.out_fd = -1;
397
398 if (parse_cmdline(&app, argc, argv) < 0) {
399 ret = 1;
400 goto out_parse;
401 }
402
403 if (app.opt.help) {
404 print_help();
405 goto out_parse;
406 }
407
408 if (!app.opt.list && !app.opt.bind_all &&
409 wl_list_empty(&app.stream_list)) {
410 fprintf(stderr, "Error: no options given.\n\n");
411 ret = 1;
412 print_help();
413 goto out_parse;
414 }
415
416 if (app.opt.bind_all && !wl_list_empty(&app.stream_list)) {
417 fprintf(stderr, "Error: --all and specific stream names cannot be used simultaneously.\n");
418 ret = 1;
419 goto out_parse;
420 }
421
422 if (app.opt.output && app.opt.outfd) {
423 fprintf(stderr, "Error: options --output and --outfd cannot be used simultaneously.\n");
424 ret = 1;
425 goto out_parse;
426 }
427
428 app.out_fd = setup_out_fd(app.opt.output, app.opt.outfd);
429 if (app.out_fd < 0) {
430 ret = 1;
431 goto out_parse;
432 }
433
434 app.dpy = wl_display_connect(NULL);
435 if (!app.dpy) {
436 fprintf(stderr, "Error: Could not connect to Wayland display: %s\n",
437 strerror(errno));
438 ret = 1;
439 goto out_parse;
440 }
441
442 app.registry = wl_display_get_registry(app.dpy);
443 wl_registry_add_listener(app.registry, ®istry_listener, &app);
444 wl_display_roundtrip(app.dpy);
445
446 if (!app.debug_iface) {
447 ret = 1;
448 fprintf(stderr,
449 "The Wayland server does not support %s interface.\n",
450 weston_debug_v1_interface.name);
451 goto out_conn;
452 }
453
454 wl_display_roundtrip(app.dpy); /* for weston_debug_v1::advertise */
455
456 if (app.opt.list)
457 list_streams(&app);
458
459 start_streams(&app);
460
461 weston_debug_v1_destroy(app.debug_iface);
462
463 while (1) {
464 struct debug_stream *stream;
465 bool empty = true;
466
467 wl_list_for_each(stream, &app.stream_list, link) {
468 if (stream->obj) {
469 empty = false;
470 break;
471 }
472 }
473
474 if (empty)
475 break;
476
477 if (wl_display_dispatch(app.dpy) < 0) {
478 ret = 1;
479 break;
480 }
481 }
482
483 out_conn:
484 destroy_streams(&app);
485
486 /* Wait for server to close all files */
487 wl_display_roundtrip(app.dpy);
488
489 wl_registry_destroy(app.registry);
490 wl_display_disconnect(app.dpy);
491
492 out_parse:
493 if (app.out_fd != -1)
494 close(app.out_fd);
495
496 destroy_streams(&app);
497 free(app.opt.output);
498 free(app.opt.outfd);
499
500 return ret;
501 }
502