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