]> git.sur5r.net Git - i3/i3/blob - i3-dump-log/main.c
Merge branch 'release-4.16.1'
[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 #include <signal.h>
29
30 #include "libi3.h"
31 #include "shmlog.h"
32 #include <i3/ipc.h>
33
34 #if !defined(__OpenBSD__)
35 static uint32_t offset_next_write;
36 #endif
37 static uint32_t wrap_count;
38
39 static i3_shmlog_header *header;
40 static char *logbuffer,
41     *walk;
42 static int ipcfd = -1;
43
44 static volatile bool interrupted = false;
45
46 static void sighandler(int signal) {
47     interrupted = true;
48 }
49
50 static void disable_shmlog(void) {
51     const char *disablecmd = "debuglog off; shmlog off";
52     if (ipc_send_message(ipcfd, strlen(disablecmd),
53                          I3_IPC_MESSAGE_TYPE_COMMAND, (uint8_t *)disablecmd) != 0)
54         err(EXIT_FAILURE, "IPC send");
55
56     /* Ensure the command was sent by waiting for the reply: */
57     uint32_t reply_length = 0;
58     uint8_t *reply = NULL;
59     if (ipc_recv_message(ipcfd, I3_IPC_REPLY_TYPE_COMMAND,
60                          &reply_length, &reply) != 0) {
61         err(EXIT_FAILURE, "IPC recv");
62     }
63     free(reply);
64 }
65
66 static int check_for_wrap(void) {
67     if (wrap_count == header->wrap_count)
68         return 0;
69
70     /* The log wrapped. Print the remaining content and reset walk to the top
71      * of the log. */
72     wrap_count = header->wrap_count;
73     const int len = (logbuffer + header->offset_last_wrap) - walk;
74     swrite(STDOUT_FILENO, walk, len);
75     walk = logbuffer + sizeof(i3_shmlog_header);
76     return 1;
77 }
78
79 static void print_till_end(void) {
80     check_for_wrap();
81     const int len = (logbuffer + header->offset_next_write) - walk;
82     swrite(STDOUT_FILENO, walk, len);
83     walk += len;
84 }
85
86 void errorlog(char *fmt, ...) {
87     va_list args;
88
89     va_start(args, fmt);
90     vfprintf(stderr, fmt, args);
91     va_end(args);
92 }
93
94 int main(int argc, char *argv[]) {
95     int o, option_index = 0;
96     bool verbose = false;
97 #if !defined(__OpenBSD__)
98     bool follow = false;
99 #endif
100
101     static struct option long_options[] = {
102         {"version", no_argument, 0, 'v'},
103         {"verbose", no_argument, 0, 'V'},
104 #if !defined(__OpenBSD__)
105         {"follow", no_argument, 0, 'f'},
106 #endif
107         {"help", no_argument, 0, 'h'},
108         {0, 0, 0, 0}
109     };
110
111 #if !defined(__OpenBSD__)
112     char *options_string = "s:vfVh";
113 #else
114     char *options_string = "vVh";
115 #endif
116
117     while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
118         if (o == 'v') {
119             printf("i3-dump-log " I3_VERSION "\n");
120             return 0;
121         } else if (o == 'V') {
122             verbose = true;
123 #if !defined(__OpenBSD__)
124         } else if (o == 'f') {
125             follow = true;
126 #endif
127         } else if (o == 'h') {
128             printf("i3-dump-log " I3_VERSION "\n");
129 #if !defined(__OpenBSD__)
130             printf("i3-dump-log [-fhVv]\n");
131 #else
132             printf("i3-dump-log [-hVv]\n");
133 #endif
134             return 0;
135         }
136     }
137
138     char *shmname = root_atom_contents("I3_SHMLOG_PATH", NULL, 0);
139     if (shmname == NULL) {
140         /* Something failed. Let’s invest a little effort to find out what it
141          * is. This is hugely helpful for users who want to debug i3 but are
142          * not used to the procedure yet. */
143         xcb_connection_t *conn;
144         int screen;
145         if ((conn = xcb_connect(NULL, &screen)) == NULL ||
146             xcb_connection_has_error(conn)) {
147             fprintf(stderr, "i3-dump-log: ERROR: Cannot connect to X11.\n\n");
148             if (getenv("DISPLAY") == NULL) {
149                 fprintf(stderr, "Your DISPLAY environment variable is not set.\n");
150                 fprintf(stderr, "Are you running i3-dump-log via SSH or on a virtual console?\n");
151                 fprintf(stderr, "Try DISPLAY=:0 i3-dump-log\n");
152                 exit(1);
153             }
154             fprintf(stderr, "FYI: The DISPLAY environment variable is set to \"%s\".\n", getenv("DISPLAY"));
155             exit(1);
156         }
157         if (root_atom_contents("I3_CONFIG_PATH", conn, screen) != NULL) {
158             fprintf(stderr, "i3-dump-log: ERROR: i3 is running, but SHM logging is not enabled. Enabling SHM log until cancelled\n\n");
159             ipcfd = ipc_connect(NULL);
160             const char *enablecmd = "debuglog on; shmlog 5242880";
161             if (ipc_send_message(ipcfd, strlen(enablecmd),
162                                  I3_IPC_MESSAGE_TYPE_COMMAND, (uint8_t *)enablecmd) != 0)
163                 err(EXIT_FAILURE, "IPC send");
164             /* By the time we receive a reply, I3_SHMLOG_PATH is set: */
165             uint32_t reply_length = 0;
166             uint8_t *reply = NULL;
167             if (ipc_recv_message(ipcfd, I3_IPC_REPLY_TYPE_COMMAND,
168                                  &reply_length, &reply) != 0) {
169                 err(EXIT_FAILURE, "IPC recv");
170             }
171             free(reply);
172
173             atexit(disable_shmlog);
174
175             /* Retry: */
176             shmname = root_atom_contents("I3_SHMLOG_PATH", NULL, 0);
177             if (shmname == NULL && !is_debug_build()) {
178                 fprintf(stderr, "You seem to be using a release version of i3:\n  %s\n\n", I3_VERSION);
179                 fprintf(stderr, "Release versions do not use SHM logging by default,\ntherefore i3-dump-log does not work.\n\n");
180                 fprintf(stderr, "Please follow this guide instead:\nhttps://i3wm.org/docs/debugging-release-version.html\n");
181                 exit(1);
182             }
183         }
184         if (shmname == NULL) {
185             errx(EXIT_FAILURE, "Cannot get I3_SHMLOG_PATH atom contents. Is i3 running on this display?");
186         }
187     }
188
189     if (*shmname == '\0')
190         errx(EXIT_FAILURE, "Cannot dump log: SHM logging is disabled in i3.");
191
192     struct stat statbuf;
193
194     /* NB: While we must never write, we need O_RDWR for the pthread condvar. */
195     int logbuffer_shm = shm_open(shmname, O_RDWR, 0);
196     if (logbuffer_shm == -1)
197         err(EXIT_FAILURE, "Could not shm_open SHM segment for the i3 log (%s)", shmname);
198
199     if (fstat(logbuffer_shm, &statbuf) != 0)
200         err(EXIT_FAILURE, "stat(%s)", shmname);
201
202     /* NB: While we must never write, we need PROT_WRITE for the pthread condvar. */
203     logbuffer = mmap(NULL, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, logbuffer_shm, 0);
204     if (logbuffer == MAP_FAILED)
205         err(EXIT_FAILURE, "Could not mmap SHM segment for the i3 log");
206
207     header = (i3_shmlog_header *)logbuffer;
208
209     if (verbose)
210         printf("next_write = %d, last_wrap = %d, logbuffer_size = %d, shmname = %s\n",
211                header->offset_next_write, header->offset_last_wrap, header->size, shmname);
212     free(shmname);
213     walk = logbuffer + header->offset_next_write;
214
215     /* We first need to print old content in case there was at least one
216      * wrapping already. */
217
218     if (*walk != '\0') {
219         /* In case there was a write to the buffer already, skip the first
220          * old line, it very likely is mangled. Not a problem, though, the log
221          * is chatty enough to have plenty lines left. */
222         while (*walk != '\n')
223             walk++;
224         walk++;
225     }
226
227     /* In case there was no wrapping, this is a no-op, otherwise it prints the
228      * old lines. */
229     wrap_count = 0;
230     check_for_wrap();
231
232     /* Then start from the beginning and print the newer lines */
233     walk = logbuffer + sizeof(i3_shmlog_header);
234     print_till_end();
235
236 #if !defined(__OpenBSD__)
237     if (!follow) {
238         return 0;
239     }
240
241     /* Handle SIGINT gracefully to invoke atexit handlers, if any. */
242     struct sigaction action;
243     action.sa_handler = sighandler;
244     sigemptyset(&action.sa_mask);
245     action.sa_flags = 0;
246     sigaction(SIGINT, &action, NULL);
247
248     /* Since pthread_cond_wait() expects a mutex, we need to provide one.
249          * To not lock i3 (that’s bad, mhkay?) we just define one outside of
250          * the shared memory. */
251     pthread_mutex_t dummy_mutex = PTHREAD_MUTEX_INITIALIZER;
252     pthread_mutex_lock(&dummy_mutex);
253     while (!interrupted) {
254         pthread_cond_wait(&(header->condvar), &dummy_mutex);
255         /* If this was not a spurious wakeup, print the new lines. */
256         if (header->offset_next_write != offset_next_write) {
257             offset_next_write = header->offset_next_write;
258             print_till_end();
259         }
260     }
261
262 #endif
263     exit(0);
264     return 0;
265 }