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