2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
7 * i3-dump-log/main.c: Dumps the i3 SHM log to stdout.
12 #include <sys/types.h>
13 #include <sys/socket.h>
31 static uint32_t offset_next_write,
34 static i3_shmlog_header *header;
35 static char *logbuffer,
38 static int check_for_wrap(void) {
39 if (wrap_count == header->wrap_count)
42 /* The log wrapped. Print the remaining content and reset walk to the top
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);
52 static void print_till_end(void) {
54 const int len = (logbuffer + header->offset_next_write) - walk;
55 const int n = write(STDOUT_FILENO, walk, len);
57 err(EXIT_FAILURE, "write()");
63 int main(int argc, char *argv[]) {
64 int o, option_index = 0;
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'},
76 char *options_string = "s:vfVh";
78 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
80 printf("i3-dump-log " I3_VERSION "\n");
82 } else if (o == 'V') {
84 } else if (o == 'f') {
86 } else if (o == 'h') {
87 printf("i3-dump-log " I3_VERSION "\n");
88 printf("i3-dump-log [-f] [-s <socket>]\n");
93 char *shmname = root_atom_contents("I3_SHMLOG_PATH");
94 if (shmname == NULL) {
95 /* Something failed. Let’s invest a little effort to find out what it
96 * is. This is hugely helpful for users who want to debug i3 but are
97 * not used to the procedure yet. */
98 xcb_connection_t *conn;
100 if ((conn = xcb_connect(NULL, &screen)) == NULL ||
101 xcb_connection_has_error(conn)) {
102 fprintf(stderr, "i3-dump-log: ERROR: Cannot connect to X11.\n\n");
103 if (getenv("DISPLAY") == NULL) {
104 fprintf(stderr, "Your DISPLAY environment variable is not set.\n");
105 fprintf(stderr, "Are you running i3-dump-log via SSH or on a virtual console?\n");
106 fprintf(stderr, "Try DISPLAY=:0 i3-dump-log\n");
109 fprintf(stderr, "FYI: The DISPLAY environment variable is set to \"%s\".\n", getenv("DISPLAY"));
112 if (root_atom_contents("I3_CONFIG_PATH") != NULL) {
113 fprintf(stderr, "i3-dump-log: ERROR: i3 is running, but SHM logging is not enabled.\n\n");
114 if (!is_debug_build()) {
115 fprintf(stderr, "You seem to be using a release version of i3:\n %s\n\n", I3_VERSION);
116 fprintf(stderr, "Release versions do not use SHM logging by default,\ntherefore i3-dump-log does not work.\n\n");
117 fprintf(stderr, "Please follow this guide instead:\nhttp://i3wm.org/docs/debugging-release-version.html\n");
121 errx(EXIT_FAILURE, "Cannot get I3_SHMLOG_PATH atom contents. Is i3 running on this display?");
124 if (*shmname == '\0')
125 errx(EXIT_FAILURE, "Cannot dump log: SHM logging is disabled in i3.");
129 /* NB: While we must never write, we need O_RDWR for the pthread condvar. */
130 int logbuffer_shm = shm_open(shmname, O_RDWR, 0);
131 if (logbuffer_shm == -1)
132 err(EXIT_FAILURE, "Could not shm_open SHM segment for the i3 log (%s)", shmname);
134 if (fstat(logbuffer_shm, &statbuf) != 0)
135 err(EXIT_FAILURE, "stat(%s)", shmname);
137 /* NB: While we must never write, we need PROT_WRITE for the pthread condvar. */
138 logbuffer = mmap(NULL, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, logbuffer_shm, 0);
139 if (logbuffer == MAP_FAILED)
140 err(EXIT_FAILURE, "Could not mmap SHM segment for the i3 log");
142 header = (i3_shmlog_header*)logbuffer;
145 printf("next_write = %d, last_wrap = %d, logbuffer_size = %d, shmname = %s\n",
146 header->offset_next_write, header->offset_last_wrap, header->size, shmname);
147 walk = logbuffer + header->offset_next_write;
149 /* We first need to print old content in case there was at least one
150 * wrapping already. */
153 /* In case there was a write to the buffer already, skip the first
154 * old line, it very likely is mangled. Not a problem, though, the log
155 * is chatty enough to have plenty lines left. */
156 while (*walk != '\n')
161 /* In case there was no wrapping, this is a no-op, otherwise it prints the
166 /* Then start from the beginning and print the newer lines */
167 walk = logbuffer + sizeof(i3_shmlog_header);
171 /* Since pthread_cond_wait() expects a mutex, we need to provide one.
172 * To not lock i3 (that’s bad, mhkay?) we just define one outside of
173 * the shared memory. */
174 pthread_mutex_t dummy_mutex = PTHREAD_MUTEX_INITIALIZER;
175 pthread_mutex_lock(&dummy_mutex);
177 pthread_cond_wait(&(header->condvar), &dummy_mutex);
178 /* If this was not a spurious wakeup, print the new lines. */
179 if (header->offset_next_write != offset_next_write) {
180 offset_next_write = header->offset_next_write;