Pekka Paalanen | b9fdc14 | 2017-10-12 13:13:44 +0200 | [diff] [blame] | 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" |
Pekka Paalanen | ecbdcfd | 2019-04-04 14:46:00 +0300 | [diff] [blame^] | 45 | #include <libweston/zalloc.h> |
Pekka Paalanen | b9fdc14 | 2017-10-12 13:13:44 +0200 | [diff] [blame] | 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 * |
| 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 * |
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |
| 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: %m\n", |
| 280 | output); |
| 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: %m\n", fd); |
| 294 | return -1; |
| 295 | } |
| 296 | |
| 297 | if ((flags & O_ACCMODE) != O_WRONLY && |
| 298 | (flags & O_ACCMODE) != O_RDWR) { |
| 299 | fprintf(stderr, |
| 300 | "Error: file descriptor %d is not writable.\n", fd); |
| 301 | return -1; |
| 302 | } |
| 303 | |
| 304 | return fd; |
| 305 | } |
| 306 | |
| 307 | static void |
| 308 | print_help(void) |
| 309 | { |
| 310 | fprintf(stderr, |
| 311 | "Usage: weston-debug [options] [names]\n" |
| 312 | "Where options may be:\n" |
| 313 | " -h, --help\n" |
| 314 | " This help text, and exit with success.\n" |
| 315 | " -l, --list\n" |
| 316 | " Print a list of available debug streams to stderr.\n" |
| 317 | " -a, --all-streams\n" |
| 318 | " Bind to all available streams.\n" |
| 319 | " -o FILE, --output FILE\n" |
| 320 | " Direct output to file named FILE. Use - for stdout.\n" |
| 321 | " Stdout is the default. Mutually exclusive with -f.\n" |
| 322 | " -f FD, --outfd FD\n" |
| 323 | " Direct output to the file descriptor FD.\n" |
| 324 | " Stdout (1) is the default. Mutually exclusive with -o.\n" |
| 325 | "Names are whatever debug stream names the compositor supports.\n" |
| 326 | ); |
| 327 | } |
| 328 | |
| 329 | static int |
| 330 | parse_cmdline(struct debug_app *app, int argc, char **argv) |
| 331 | { |
| 332 | static const struct option opts[] = { |
| 333 | { "help", no_argument, NULL, 'h' }, |
| 334 | { "list", no_argument, NULL, 'l' }, |
| 335 | { "all-streams", no_argument, NULL, 'a' }, |
| 336 | { "output", required_argument, NULL, 'o' }, |
| 337 | { "outfd", required_argument, NULL, 'f' }, |
| 338 | { 0 } |
| 339 | }; |
| 340 | static const char optstr[] = "hlao:f:"; |
| 341 | int c; |
| 342 | bool failed = false; |
| 343 | |
| 344 | while (1) { |
| 345 | c = getopt_long(argc, argv, optstr, opts, NULL); |
| 346 | if (c == -1) |
| 347 | break; |
| 348 | |
| 349 | switch (c) { |
| 350 | case 'h': |
| 351 | app->opt.help = true; |
| 352 | break; |
| 353 | case 'l': |
| 354 | app->opt.list = true; |
| 355 | break; |
| 356 | case 'a': |
| 357 | app->opt.bind_all = true; |
| 358 | break; |
| 359 | case 'o': |
| 360 | free(app->opt.output); |
| 361 | app->opt.output = strdup(optarg); |
| 362 | break; |
| 363 | case 'f': |
| 364 | free(app->opt.outfd); |
| 365 | app->opt.outfd = strdup(optarg); |
| 366 | break; |
| 367 | case '?': |
| 368 | failed = true; |
| 369 | break; |
| 370 | default: |
| 371 | fprintf(stderr, "huh? getopt => %c (%d)\n", c, c); |
| 372 | failed = true; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | if (failed) |
| 377 | return -1; |
| 378 | |
| 379 | while (optind < argc) { |
| 380 | struct debug_stream *stream = |
| 381 | stream_alloc(app, argv[optind++], NULL); |
| 382 | stream->should_bind = true; |
| 383 | } |
| 384 | |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | int |
| 389 | main(int argc, char **argv) |
| 390 | { |
| 391 | struct debug_app app = {}; |
| 392 | int ret = 0; |
| 393 | |
| 394 | wl_list_init(&app.stream_list); |
| 395 | app.out_fd = -1; |
| 396 | |
| 397 | if (parse_cmdline(&app, argc, argv) < 0) { |
| 398 | ret = 1; |
| 399 | goto out_parse; |
| 400 | } |
| 401 | |
| 402 | if (app.opt.help) { |
| 403 | print_help(); |
| 404 | goto out_parse; |
| 405 | } |
| 406 | |
| 407 | if (!app.opt.list && !app.opt.bind_all && |
| 408 | wl_list_empty(&app.stream_list)) { |
| 409 | fprintf(stderr, "Error: no options given.\n\n"); |
| 410 | ret = 1; |
| 411 | print_help(); |
| 412 | goto out_parse; |
| 413 | } |
| 414 | |
| 415 | if (app.opt.bind_all && !wl_list_empty(&app.stream_list)) { |
| 416 | fprintf(stderr, "Error: --all and specific stream names cannot be used simultaneously.\n"); |
| 417 | ret = 1; |
| 418 | goto out_parse; |
| 419 | } |
| 420 | |
| 421 | if (app.opt.output && app.opt.outfd) { |
| 422 | fprintf(stderr, "Error: options --output and --outfd cannot be used simultaneously.\n"); |
| 423 | ret = 1; |
| 424 | goto out_parse; |
| 425 | } |
| 426 | |
| 427 | app.out_fd = setup_out_fd(app.opt.output, app.opt.outfd); |
| 428 | if (app.out_fd < 0) { |
| 429 | ret = 1; |
| 430 | goto out_parse; |
| 431 | } |
| 432 | |
| 433 | app.dpy = wl_display_connect(NULL); |
| 434 | if (!app.dpy) { |
| 435 | fprintf(stderr, "Error: Could not connect to Wayland display: %m\n"); |
| 436 | ret = 1; |
| 437 | goto out_parse; |
| 438 | } |
| 439 | |
| 440 | app.registry = wl_display_get_registry(app.dpy); |
| 441 | wl_registry_add_listener(app.registry, ®istry_listener, &app); |
| 442 | wl_display_roundtrip(app.dpy); |
| 443 | |
| 444 | if (!app.debug_iface) { |
| 445 | ret = 1; |
| 446 | fprintf(stderr, |
| 447 | "The Wayland server does not support %s interface.\n", |
| 448 | weston_debug_v1_interface.name); |
| 449 | goto out_conn; |
| 450 | } |
| 451 | |
| 452 | wl_display_roundtrip(app.dpy); /* for weston_debug_v1::advertise */ |
| 453 | |
| 454 | if (app.opt.list) |
| 455 | list_streams(&app); |
| 456 | |
| 457 | start_streams(&app); |
| 458 | |
| 459 | weston_debug_v1_destroy(app.debug_iface); |
| 460 | |
| 461 | while (1) { |
| 462 | struct debug_stream *stream; |
| 463 | bool empty = true; |
| 464 | |
| 465 | wl_list_for_each(stream, &app.stream_list, link) { |
| 466 | if (stream->obj) { |
| 467 | empty = false; |
| 468 | break; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | if (empty) |
| 473 | break; |
| 474 | |
| 475 | if (wl_display_dispatch(app.dpy) < 0) { |
| 476 | ret = 1; |
| 477 | break; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | out_conn: |
| 482 | destroy_streams(&app); |
| 483 | |
| 484 | /* Wait for server to close all files */ |
| 485 | wl_display_roundtrip(app.dpy); |
| 486 | |
| 487 | wl_registry_destroy(app.registry); |
| 488 | wl_display_disconnect(app.dpy); |
| 489 | |
| 490 | out_parse: |
| 491 | if (app.out_fd != -1) |
| 492 | close(app.out_fd); |
| 493 | |
| 494 | destroy_streams(&app); |
| 495 | free(app.opt.output); |
| 496 | free(app.opt.outfd); |
| 497 | |
| 498 | return ret; |
| 499 | } |