2011-12-10 12:27:40 +01:00
|
|
|
|
/*
|
|
|
|
|
* vim:ts=4:sw=4:expandtab
|
|
|
|
|
*
|
|
|
|
|
* i3 - an improved dynamic tiling window manager
|
2015-04-04 02:17:56 +02:00
|
|
|
|
* © 2009 Michael Stapelberg and contributors (see also: LICENSE)
|
2011-12-10 12:27:40 +01:00
|
|
|
|
*
|
|
|
|
|
* i3-dump-log/main.c: Dumps the i3 SHM log to stdout.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2016-10-11 09:13:35 +02:00
|
|
|
|
#include <config.h>
|
|
|
|
|
|
2011-12-10 12:27:40 +01:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <err.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <getopt.h>
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
|
|
#include "libi3.h"
|
2012-01-07 00:40:07 +01:00
|
|
|
|
#include "shmlog.h"
|
2011-12-10 12:27:40 +01:00
|
|
|
|
#include <i3/ipc.h>
|
|
|
|
|
|
2016-06-11 13:05:29 +02:00
|
|
|
|
#if !defined(__OpenBSD__)
|
|
|
|
|
static uint32_t offset_next_write;
|
|
|
|
|
#endif
|
|
|
|
|
static uint32_t wrap_count;
|
2012-08-13 00:57:57 +02:00
|
|
|
|
|
|
|
|
|
static i3_shmlog_header *header;
|
|
|
|
|
static char *logbuffer,
|
2014-06-15 19:07:02 +02:00
|
|
|
|
*walk;
|
2012-08-13 00:57:57 +02:00
|
|
|
|
|
|
|
|
|
static int check_for_wrap(void) {
|
|
|
|
|
if (wrap_count == header->wrap_count)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
/* The log wrapped. Print the remaining content and reset walk to the top
|
|
|
|
|
* of the log. */
|
|
|
|
|
wrap_count = header->wrap_count;
|
2012-12-10 17:45:14 +01:00
|
|
|
|
const int len = (logbuffer + header->offset_last_wrap) - walk;
|
2015-03-24 13:57:06 +01:00
|
|
|
|
swrite(STDOUT_FILENO, walk, len);
|
2012-08-13 00:57:57 +02:00
|
|
|
|
walk = logbuffer + sizeof(i3_shmlog_header);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void print_till_end(void) {
|
|
|
|
|
check_for_wrap();
|
2012-12-10 17:45:14 +01:00
|
|
|
|
const int len = (logbuffer + header->offset_next_write) - walk;
|
2015-03-24 13:57:06 +01:00
|
|
|
|
swrite(STDOUT_FILENO, walk, len);
|
|
|
|
|
walk += len;
|
2012-08-13 00:57:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-12-10 12:27:40 +01:00
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
|
int o, option_index = 0;
|
2016-06-11 13:05:29 +02:00
|
|
|
|
bool verbose = false;
|
|
|
|
|
#if !defined(__OpenBSD__)
|
|
|
|
|
bool follow = false;
|
|
|
|
|
#endif
|
2011-12-10 12:27:40 +01:00
|
|
|
|
|
|
|
|
|
static struct option long_options[] = {
|
|
|
|
|
{"version", no_argument, 0, 'v'},
|
|
|
|
|
{"verbose", no_argument, 0, 'V'},
|
2016-06-11 13:05:29 +02:00
|
|
|
|
#if !defined(__OpenBSD__)
|
2012-08-13 00:57:57 +02:00
|
|
|
|
{"follow", no_argument, 0, 'f'},
|
2016-06-11 13:05:29 +02:00
|
|
|
|
#endif
|
2011-12-10 12:27:40 +01:00
|
|
|
|
{"help", no_argument, 0, 'h'},
|
2016-06-11 13:05:29 +02:00
|
|
|
|
{0, 0, 0, 0}
|
|
|
|
|
};
|
2011-12-10 12:27:40 +01:00
|
|
|
|
|
2016-06-11 13:05:29 +02:00
|
|
|
|
#if !defined(__OpenBSD__)
|
2012-08-13 00:57:57 +02:00
|
|
|
|
char *options_string = "s:vfVh";
|
2016-06-11 13:05:29 +02:00
|
|
|
|
#else
|
|
|
|
|
char *options_string = "vVh";
|
|
|
|
|
#endif
|
2011-12-10 12:27:40 +01:00
|
|
|
|
|
|
|
|
|
while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
|
2012-01-07 00:40:07 +01:00
|
|
|
|
if (o == 'v') {
|
2011-12-10 12:27:40 +01:00
|
|
|
|
printf("i3-dump-log " I3_VERSION "\n");
|
|
|
|
|
return 0;
|
|
|
|
|
} else if (o == 'V') {
|
|
|
|
|
verbose = true;
|
2016-06-11 13:05:29 +02:00
|
|
|
|
#if !defined(__OpenBSD__)
|
2012-08-13 00:57:57 +02:00
|
|
|
|
} else if (o == 'f') {
|
|
|
|
|
follow = true;
|
2016-06-11 13:05:29 +02:00
|
|
|
|
#endif
|
2011-12-10 12:27:40 +01:00
|
|
|
|
} else if (o == 'h') {
|
|
|
|
|
printf("i3-dump-log " I3_VERSION "\n");
|
2016-06-11 13:05:29 +02:00
|
|
|
|
#if !defined(__OpenBSD__)
|
|
|
|
|
printf("i3-dump-log [-fhVv]\n");
|
|
|
|
|
#else
|
|
|
|
|
printf("i3-dump-log [-hVv]\n");
|
|
|
|
|
#endif
|
2011-12-10 12:27:40 +01:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-22 15:48:45 +01:00
|
|
|
|
char *shmname = root_atom_contents("I3_SHMLOG_PATH", NULL, 0);
|
2012-05-09 19:46:44 +02:00
|
|
|
|
if (shmname == NULL) {
|
|
|
|
|
/* Something failed. Let’s invest a little effort to find out what it
|
|
|
|
|
* is. This is hugely helpful for users who want to debug i3 but are
|
|
|
|
|
* not used to the procedure yet. */
|
|
|
|
|
xcb_connection_t *conn;
|
|
|
|
|
int screen;
|
|
|
|
|
if ((conn = xcb_connect(NULL, &screen)) == NULL ||
|
|
|
|
|
xcb_connection_has_error(conn)) {
|
|
|
|
|
fprintf(stderr, "i3-dump-log: ERROR: Cannot connect to X11.\n\n");
|
|
|
|
|
if (getenv("DISPLAY") == NULL) {
|
|
|
|
|
fprintf(stderr, "Your DISPLAY environment variable is not set.\n");
|
|
|
|
|
fprintf(stderr, "Are you running i3-dump-log via SSH or on a virtual console?\n");
|
|
|
|
|
fprintf(stderr, "Try DISPLAY=:0 i3-dump-log\n");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
fprintf(stderr, "FYI: The DISPLAY environment variable is set to \"%s\".\n", getenv("DISPLAY"));
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
2013-11-22 15:48:45 +01:00
|
|
|
|
if (root_atom_contents("I3_CONFIG_PATH", conn, screen) != NULL) {
|
2012-05-09 19:46:44 +02:00
|
|
|
|
fprintf(stderr, "i3-dump-log: ERROR: i3 is running, but SHM logging is not enabled.\n\n");
|
|
|
|
|
if (!is_debug_build()) {
|
|
|
|
|
fprintf(stderr, "You seem to be using a release version of i3:\n %s\n\n", I3_VERSION);
|
|
|
|
|
fprintf(stderr, "Release versions do not use SHM logging by default,\ntherefore i3-dump-log does not work.\n\n");
|
2017-09-24 10:19:07 +02:00
|
|
|
|
fprintf(stderr, "Please follow this guide instead:\nhttps://i3wm.org/docs/debugging-release-version.html\n");
|
2012-05-09 19:46:44 +02:00
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-01-07 00:40:07 +01:00
|
|
|
|
errx(EXIT_FAILURE, "Cannot get I3_SHMLOG_PATH atom contents. Is i3 running on this display?");
|
2012-05-09 19:46:44 +02:00
|
|
|
|
}
|
2011-12-10 12:27:40 +01:00
|
|
|
|
|
|
|
|
|
if (*shmname == '\0')
|
|
|
|
|
errx(EXIT_FAILURE, "Cannot dump log: SHM logging is disabled in i3.");
|
|
|
|
|
|
2012-01-07 00:40:07 +01:00
|
|
|
|
struct stat statbuf;
|
|
|
|
|
|
2012-12-14 01:27:34 +01:00
|
|
|
|
/* NB: While we must never write, we need O_RDWR for the pthread condvar. */
|
2012-08-13 00:57:57 +02:00
|
|
|
|
int logbuffer_shm = shm_open(shmname, O_RDWR, 0);
|
2011-12-10 12:27:40 +01:00
|
|
|
|
if (logbuffer_shm == -1)
|
|
|
|
|
err(EXIT_FAILURE, "Could not shm_open SHM segment for the i3 log (%s)", shmname);
|
|
|
|
|
|
2012-01-07 00:40:07 +01:00
|
|
|
|
if (fstat(logbuffer_shm, &statbuf) != 0)
|
|
|
|
|
err(EXIT_FAILURE, "stat(%s)", shmname);
|
|
|
|
|
|
2012-12-14 01:27:34 +01:00
|
|
|
|
/* NB: While we must never write, we need PROT_WRITE for the pthread condvar. */
|
2012-08-13 00:57:57 +02:00
|
|
|
|
logbuffer = mmap(NULL, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, logbuffer_shm, 0);
|
2011-12-10 12:27:40 +01:00
|
|
|
|
if (logbuffer == MAP_FAILED)
|
|
|
|
|
err(EXIT_FAILURE, "Could not mmap SHM segment for the i3 log");
|
|
|
|
|
|
2014-06-15 19:07:02 +02:00
|
|
|
|
header = (i3_shmlog_header *)logbuffer;
|
2012-01-07 00:40:07 +01:00
|
|
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
|
printf("next_write = %d, last_wrap = %d, logbuffer_size = %d, shmname = %s\n",
|
|
|
|
|
header->offset_next_write, header->offset_last_wrap, header->size, shmname);
|
2016-01-08 20:17:34 +01:00
|
|
|
|
free(shmname);
|
2012-08-13 00:57:57 +02:00
|
|
|
|
walk = logbuffer + header->offset_next_write;
|
2011-12-10 12:27:40 +01:00
|
|
|
|
|
2012-08-13 00:57:57 +02:00
|
|
|
|
/* We first need to print old content in case there was at least one
|
|
|
|
|
* wrapping already. */
|
|
|
|
|
|
|
|
|
|
if (*walk != '\0') {
|
|
|
|
|
/* In case there was a write to the buffer already, skip the first
|
|
|
|
|
* old line, it very likely is mangled. Not a problem, though, the log
|
|
|
|
|
* is chatty enough to have plenty lines left. */
|
|
|
|
|
while (*walk != '\n')
|
|
|
|
|
walk++;
|
|
|
|
|
walk++;
|
2011-12-10 12:27:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-13 00:57:57 +02:00
|
|
|
|
/* In case there was no wrapping, this is a no-op, otherwise it prints the
|
|
|
|
|
* old lines. */
|
|
|
|
|
wrap_count = 0;
|
|
|
|
|
check_for_wrap();
|
|
|
|
|
|
2011-12-10 12:27:40 +01:00
|
|
|
|
/* Then start from the beginning and print the newer lines */
|
2012-01-07 00:40:07 +01:00
|
|
|
|
walk = logbuffer + sizeof(i3_shmlog_header);
|
2012-08-13 00:57:57 +02:00
|
|
|
|
print_till_end();
|
|
|
|
|
|
2016-06-11 13:05:29 +02:00
|
|
|
|
#if !defined(__OpenBSD__)
|
2012-08-13 00:57:57 +02:00
|
|
|
|
if (follow) {
|
|
|
|
|
/* Since pthread_cond_wait() expects a mutex, we need to provide one.
|
|
|
|
|
* To not lock i3 (that’s bad, mhkay?) we just define one outside of
|
|
|
|
|
* the shared memory. */
|
|
|
|
|
pthread_mutex_t dummy_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
pthread_mutex_lock(&dummy_mutex);
|
|
|
|
|
while (1) {
|
|
|
|
|
pthread_cond_wait(&(header->condvar), &dummy_mutex);
|
|
|
|
|
/* If this was not a spurious wakeup, print the new lines. */
|
|
|
|
|
if (header->offset_next_write != offset_next_write) {
|
|
|
|
|
offset_next_write = header->offset_next_write;
|
|
|
|
|
print_till_end();
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-12-10 12:27:40 +01:00
|
|
|
|
}
|
2016-06-11 13:05:29 +02:00
|
|
|
|
#endif
|
2011-12-10 12:27:40 +01:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|