]> git.sur5r.net Git - i3/i3/blob - i3-dump-log/main.c
137554a4f1fefc36539c5ca8c40b5357b32a7e2a
[i3/i3] / i3-dump-log / main.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * i3-dump-log/main.c: Dumps the i3 SHM log to stdout.
8  *
9  */
10 #include <stdio.h>
11 #include <stdbool.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <sys/un.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <err.h>
20 #include <stdint.h>
21 #include <getopt.h>
22 #include <limits.h>
23 #include <fcntl.h>
24 #include <sys/mman.h>
25 #include <sys/stat.h>
26
27 #include "libi3.h"
28 #include "shmlog.h"
29 #include <i3/ipc.h>
30
31 static uint32_t offset_next_write,
32     wrap_count;
33
34 static i3_shmlog_header *header;
35 static char *logbuffer,
36     *walk;
37
38 static int check_for_wrap(void) {
39     if (wrap_count == header->wrap_count)
40         return 0;
41
42     /* The log wrapped. Print the remaining content and reset walk to the top
43      * of the log. */
44     wrap_count = header->wrap_count;
45     const int len = (logbuffer + header->offset_last_wrap) - walk;
46     swrite(STDOUT_FILENO, walk, len);
47     walk = logbuffer + sizeof(i3_shmlog_header);
48     return 1;
49 }
50
51 static void print_till_end(void) {
52     check_for_wrap();
53     const int len = (logbuffer + header->offset_next_write) - walk;
54     swrite(STDOUT_FILENO, walk, len);
55     walk += len;
56 }
57
58 int main(int argc, char *argv[]) {
59     int o, option_index = 0;
60     bool verbose = false,
61          follow = false;
62
63     static struct option long_options[] = {
64         {"version", no_argument, 0, 'v'},
65         {"verbose", no_argument, 0, 'V'},
66         {"follow", no_argument, 0, 'f'},
67         {"help", no_argument, 0, 'h'},
68         {0, 0, 0, 0}};
69
70     char *options_string = "s:vfVh";
71
72     while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
73         if (o == 'v') {
74             printf("i3-dump-log " I3_VERSION "\n");
75             return 0;
76         } else if (o == 'V') {
77             verbose = true;
78         } else if (o == 'f') {
79             follow = true;
80         } else if (o == 'h') {
81             printf("i3-dump-log " I3_VERSION "\n");
82             printf("i3-dump-log [-f] [-s <socket>]\n");
83             return 0;
84         }
85     }
86
87     char *shmname = root_atom_contents("I3_SHMLOG_PATH", NULL, 0);
88     if (shmname == NULL) {
89         /* Something failed. Let’s invest a little effort to find out what it
90          * is. This is hugely helpful for users who want to debug i3 but are
91          * not used to the procedure yet. */
92         xcb_connection_t *conn;
93         int screen;
94         if ((conn = xcb_connect(NULL, &screen)) == NULL ||
95             xcb_connection_has_error(conn)) {
96             fprintf(stderr, "i3-dump-log: ERROR: Cannot connect to X11.\n\n");
97             if (getenv("DISPLAY") == NULL) {
98                 fprintf(stderr, "Your DISPLAY environment variable is not set.\n");
99                 fprintf(stderr, "Are you running i3-dump-log via SSH or on a virtual console?\n");
100                 fprintf(stderr, "Try DISPLAY=:0 i3-dump-log\n");
101                 exit(1);
102             }
103             fprintf(stderr, "FYI: The DISPLAY environment variable is set to \"%s\".\n", getenv("DISPLAY"));
104             exit(1);
105         }
106         if (root_atom_contents("I3_CONFIG_PATH", conn, screen) != NULL) {
107             fprintf(stderr, "i3-dump-log: ERROR: i3 is running, but SHM logging is not enabled.\n\n");
108             if (!is_debug_build()) {
109                 fprintf(stderr, "You seem to be using a release version of i3:\n  %s\n\n", I3_VERSION);
110                 fprintf(stderr, "Release versions do not use SHM logging by default,\ntherefore i3-dump-log does not work.\n\n");
111                 fprintf(stderr, "Please follow this guide instead:\nhttp://i3wm.org/docs/debugging-release-version.html\n");
112                 exit(1);
113             }
114         }
115         errx(EXIT_FAILURE, "Cannot get I3_SHMLOG_PATH atom contents. Is i3 running on this display?");
116     }
117
118     if (*shmname == '\0')
119         errx(EXIT_FAILURE, "Cannot dump log: SHM logging is disabled in i3.");
120
121     struct stat statbuf;
122
123     /* NB: While we must never write, we need O_RDWR for the pthread condvar. */
124     int logbuffer_shm = shm_open(shmname, O_RDWR, 0);
125     if (logbuffer_shm == -1)
126         err(EXIT_FAILURE, "Could not shm_open SHM segment for the i3 log (%s)", shmname);
127
128     if (fstat(logbuffer_shm, &statbuf) != 0)
129         err(EXIT_FAILURE, "stat(%s)", shmname);
130
131     /* NB: While we must never write, we need PROT_WRITE for the pthread condvar. */
132     logbuffer = mmap(NULL, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, logbuffer_shm, 0);
133     if (logbuffer == MAP_FAILED)
134         err(EXIT_FAILURE, "Could not mmap SHM segment for the i3 log");
135
136     header = (i3_shmlog_header *)logbuffer;
137
138     if (verbose)
139         printf("next_write = %d, last_wrap = %d, logbuffer_size = %d, shmname = %s\n",
140                header->offset_next_write, header->offset_last_wrap, header->size, shmname);
141     walk = logbuffer + header->offset_next_write;
142
143     /* We first need to print old content in case there was at least one
144      * wrapping already. */
145
146     if (*walk != '\0') {
147         /* In case there was a write to the buffer already, skip the first
148          * old line, it very likely is mangled. Not a problem, though, the log
149          * is chatty enough to have plenty lines left. */
150         while (*walk != '\n')
151             walk++;
152         walk++;
153     }
154
155     /* In case there was no wrapping, this is a no-op, otherwise it prints the
156      * old lines. */
157     wrap_count = 0;
158     check_for_wrap();
159
160     /* Then start from the beginning and print the newer lines */
161     walk = logbuffer + sizeof(i3_shmlog_header);
162     print_till_end();
163
164     if (follow) {
165         /* Since pthread_cond_wait() expects a mutex, we need to provide one.
166          * To not lock i3 (that’s bad, mhkay?) we just define one outside of
167          * the shared memory. */
168         pthread_mutex_t dummy_mutex = PTHREAD_MUTEX_INITIALIZER;
169         pthread_mutex_lock(&dummy_mutex);
170         while (1) {
171             pthread_cond_wait(&(header->condvar), &dummy_mutex);
172             /* If this was not a spurious wakeup, print the new lines. */
173             if (header->offset_next_write != offset_next_write) {
174                 offset_next_write = header->offset_next_write;
175                 print_till_end();
176             }
177         }
178     }
179
180     return 0;
181 }