2011-12-10 15:27:40 +04:00
|
|
|
|
/*
|
|
|
|
|
* vim:ts=4:sw=4:expandtab
|
|
|
|
|
*
|
2024-08-05 19:23:55 +03:00
|
|
|
|
* i3 - an improved tiling window manager
|
2015-04-04 03:17:56 +03:00
|
|
|
|
* © 2009 Michael Stapelberg and contributors (see also: LICENSE)
|
2011-12-10 15:27:40 +04:00
|
|
|
|
*
|
|
|
|
|
* i3-dump-log/main.c: Dumps the i3 SHM log to stdout.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2016-10-11 10:13:35 +03:00
|
|
|
|
#include <config.h>
|
|
|
|
|
|
2020-04-19 10:43:48 +03:00
|
|
|
|
#include "libi3.h"
|
|
|
|
|
#include "shmlog.h"
|
|
|
|
|
|
2011-12-10 15:27:40 +04:00
|
|
|
|
#include <err.h>
|
2020-04-19 10:43:48 +03:00
|
|
|
|
#include <fcntl.h>
|
2011-12-10 15:27:40 +04:00
|
|
|
|
#include <getopt.h>
|
2020-04-19 10:43:48 +03:00
|
|
|
|
#include <i3/ipc.h>
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
2011-12-10 15:27:40 +04:00
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
#include <sys/stat.h>
|
2020-04-19 10:43:48 +03:00
|
|
|
|
#include <unistd.h>
|
2021-01-15 18:12:55 +03:00
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <sys/un.h>
|
2011-12-10 15:27:40 +04:00
|
|
|
|
|
2016-06-11 14:05:29 +03:00
|
|
|
|
static uint32_t wrap_count;
|
2012-08-13 02:57:57 +04:00
|
|
|
|
|
|
|
|
|
static i3_shmlog_header *header;
|
|
|
|
|
static char *logbuffer,
|
2014-06-15 21:07:02 +04:00
|
|
|
|
*walk;
|
2017-11-26 19:26:40 +03:00
|
|
|
|
static int ipcfd = -1;
|
|
|
|
|
|
|
|
|
|
static void disable_shmlog(void) {
|
|
|
|
|
const char *disablecmd = "debuglog off; shmlog off";
|
|
|
|
|
if (ipc_send_message(ipcfd, strlen(disablecmd),
|
2024-01-27 13:37:05 +03:00
|
|
|
|
I3_IPC_MESSAGE_TYPE_COMMAND, (uint8_t *)disablecmd) != 0) {
|
2017-11-26 19:26:40 +03:00
|
|
|
|
err(EXIT_FAILURE, "IPC send");
|
2024-01-27 13:37:05 +03:00
|
|
|
|
}
|
2017-11-26 19:26:40 +03:00
|
|
|
|
|
|
|
|
|
/* Ensure the command was sent by waiting for the reply: */
|
|
|
|
|
uint32_t reply_length = 0;
|
|
|
|
|
uint8_t *reply = NULL;
|
|
|
|
|
if (ipc_recv_message(ipcfd, I3_IPC_REPLY_TYPE_COMMAND,
|
|
|
|
|
&reply_length, &reply) != 0) {
|
|
|
|
|
err(EXIT_FAILURE, "IPC recv");
|
|
|
|
|
}
|
|
|
|
|
free(reply);
|
|
|
|
|
}
|
2012-08-13 02:57:57 +04:00
|
|
|
|
|
|
|
|
|
static int check_for_wrap(void) {
|
2024-01-27 13:37:05 +03:00
|
|
|
|
if (wrap_count == header->wrap_count) {
|
2012-08-13 02:57:57 +04:00
|
|
|
|
return 0;
|
2024-01-27 13:37:05 +03:00
|
|
|
|
}
|
2012-08-13 02:57:57 +04:00
|
|
|
|
|
|
|
|
|
/* The log wrapped. Print the remaining content and reset walk to the top
|
|
|
|
|
* of the log. */
|
|
|
|
|
wrap_count = header->wrap_count;
|
2012-12-10 20:45:14 +04:00
|
|
|
|
const int len = (logbuffer + header->offset_last_wrap) - walk;
|
2015-03-24 15:57:06 +03:00
|
|
|
|
swrite(STDOUT_FILENO, walk, len);
|
2012-08-13 02:57:57 +04:00
|
|
|
|
walk = logbuffer + sizeof(i3_shmlog_header);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void print_till_end(void) {
|
|
|
|
|
check_for_wrap();
|
2012-12-10 20:45:14 +04:00
|
|
|
|
const int len = (logbuffer + header->offset_next_write) - walk;
|
2015-03-24 15:57:06 +03:00
|
|
|
|
swrite(STDOUT_FILENO, walk, len);
|
|
|
|
|
walk += len;
|
2012-08-13 02:57:57 +04:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-26 19:26:40 +03:00
|
|
|
|
void errorlog(char *fmt, ...) {
|
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
|
vfprintf(stderr, fmt, args);
|
|
|
|
|
va_end(args);
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-10 15:27:40 +04:00
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
|
int o, option_index = 0;
|
2016-06-11 14:05:29 +03:00
|
|
|
|
bool verbose = false;
|
|
|
|
|
#if !defined(__OpenBSD__)
|
|
|
|
|
bool follow = false;
|
|
|
|
|
#endif
|
2011-12-10 15:27:40 +04:00
|
|
|
|
|
|
|
|
|
static struct option long_options[] = {
|
|
|
|
|
{"version", no_argument, 0, 'v'},
|
|
|
|
|
{"verbose", no_argument, 0, 'V'},
|
2016-06-11 14:05:29 +03:00
|
|
|
|
#if !defined(__OpenBSD__)
|
2012-08-13 02:57:57 +04:00
|
|
|
|
{"follow", no_argument, 0, 'f'},
|
2016-06-11 14:05:29 +03:00
|
|
|
|
#endif
|
2011-12-10 15:27:40 +04:00
|
|
|
|
{"help", no_argument, 0, 'h'},
|
2016-06-11 14:05:29 +03:00
|
|
|
|
{0, 0, 0, 0}
|
|
|
|
|
};
|
2011-12-10 15:27:40 +04:00
|
|
|
|
|
2016-06-11 14:05:29 +03:00
|
|
|
|
#if !defined(__OpenBSD__)
|
2012-08-13 02:57:57 +04:00
|
|
|
|
char *options_string = "s:vfVh";
|
2016-06-11 14:05:29 +03:00
|
|
|
|
#else
|
|
|
|
|
char *options_string = "vVh";
|
|
|
|
|
#endif
|
2011-12-10 15:27:40 +04:00
|
|
|
|
|
|
|
|
|
while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
|
2012-01-07 03:40:07 +04:00
|
|
|
|
if (o == 'v') {
|
2011-12-10 15:27:40 +04:00
|
|
|
|
printf("i3-dump-log " I3_VERSION "\n");
|
|
|
|
|
return 0;
|
|
|
|
|
} else if (o == 'V') {
|
|
|
|
|
verbose = true;
|
2016-06-11 14:05:29 +03:00
|
|
|
|
#if !defined(__OpenBSD__)
|
2012-08-13 02:57:57 +04:00
|
|
|
|
} else if (o == 'f') {
|
|
|
|
|
follow = true;
|
2016-06-11 14:05:29 +03:00
|
|
|
|
#endif
|
2011-12-10 15:27:40 +04:00
|
|
|
|
} else if (o == 'h') {
|
|
|
|
|
printf("i3-dump-log " I3_VERSION "\n");
|
2016-06-11 14:05:29 +03:00
|
|
|
|
#if !defined(__OpenBSD__)
|
|
|
|
|
printf("i3-dump-log [-fhVv]\n");
|
|
|
|
|
#else
|
|
|
|
|
printf("i3-dump-log [-hVv]\n");
|
|
|
|
|
#endif
|
2011-12-10 15:27:40 +04:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-22 18:48:45 +04:00
|
|
|
|
char *shmname = root_atom_contents("I3_SHMLOG_PATH", NULL, 0);
|
2012-05-09 21:46:44 +04: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 18:48:45 +04:00
|
|
|
|
if (root_atom_contents("I3_CONFIG_PATH", conn, screen) != NULL) {
|
2019-02-12 11:22:26 +03:00
|
|
|
|
fprintf(stderr, "i3-dump-log: i3 is running, but SHM logging is not enabled. Enabling SHM log now while i3-dump-log is running\n\n");
|
2017-11-26 19:26:40 +03:00
|
|
|
|
ipcfd = ipc_connect(NULL);
|
|
|
|
|
const char *enablecmd = "debuglog on; shmlog 5242880";
|
|
|
|
|
if (ipc_send_message(ipcfd, strlen(enablecmd),
|
2024-01-27 13:37:05 +03:00
|
|
|
|
I3_IPC_MESSAGE_TYPE_COMMAND, (uint8_t *)enablecmd) != 0) {
|
2017-11-26 19:26:40 +03:00
|
|
|
|
err(EXIT_FAILURE, "IPC send");
|
2024-01-27 13:37:05 +03:00
|
|
|
|
}
|
2017-11-26 19:26:40 +03:00
|
|
|
|
/* By the time we receive a reply, I3_SHMLOG_PATH is set: */
|
|
|
|
|
uint32_t reply_length = 0;
|
|
|
|
|
uint8_t *reply = NULL;
|
|
|
|
|
if (ipc_recv_message(ipcfd, I3_IPC_REPLY_TYPE_COMMAND,
|
|
|
|
|
&reply_length, &reply) != 0) {
|
|
|
|
|
err(EXIT_FAILURE, "IPC recv");
|
|
|
|
|
}
|
|
|
|
|
free(reply);
|
|
|
|
|
|
|
|
|
|
atexit(disable_shmlog);
|
|
|
|
|
|
|
|
|
|
/* Retry: */
|
|
|
|
|
shmname = root_atom_contents("I3_SHMLOG_PATH", NULL, 0);
|
|
|
|
|
if (shmname == NULL && !is_debug_build()) {
|
2012-05-09 21:46:44 +04:00
|
|
|
|
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 11:19:07 +03:00
|
|
|
|
fprintf(stderr, "Please follow this guide instead:\nhttps://i3wm.org/docs/debugging-release-version.html\n");
|
2012-05-09 21:46:44 +04:00
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-26 19:26:40 +03:00
|
|
|
|
if (shmname == NULL) {
|
|
|
|
|
errx(EXIT_FAILURE, "Cannot get I3_SHMLOG_PATH atom contents. Is i3 running on this display?");
|
|
|
|
|
}
|
2012-05-09 21:46:44 +04:00
|
|
|
|
}
|
2011-12-10 15:27:40 +04:00
|
|
|
|
|
2024-01-27 13:37:05 +03:00
|
|
|
|
if (*shmname == '\0') {
|
2011-12-10 15:27:40 +04:00
|
|
|
|
errx(EXIT_FAILURE, "Cannot dump log: SHM logging is disabled in i3.");
|
2024-01-27 13:37:05 +03:00
|
|
|
|
}
|
2011-12-10 15:27:40 +04:00
|
|
|
|
|
2012-01-07 03:40:07 +04:00
|
|
|
|
struct stat statbuf;
|
|
|
|
|
|
2012-12-14 04:27:34 +04:00
|
|
|
|
/* NB: While we must never write, we need O_RDWR for the pthread condvar. */
|
2012-08-13 02:57:57 +04:00
|
|
|
|
int logbuffer_shm = shm_open(shmname, O_RDWR, 0);
|
2021-01-15 18:12:55 +03:00
|
|
|
|
if (logbuffer_shm == -1) {
|
2011-12-10 15:27:40 +04:00
|
|
|
|
err(EXIT_FAILURE, "Could not shm_open SHM segment for the i3 log (%s)", shmname);
|
2021-01-15 18:12:55 +03:00
|
|
|
|
}
|
2011-12-10 15:27:40 +04:00
|
|
|
|
|
2021-01-15 18:12:55 +03:00
|
|
|
|
if (fstat(logbuffer_shm, &statbuf) != 0) {
|
2012-01-07 03:40:07 +04:00
|
|
|
|
err(EXIT_FAILURE, "stat(%s)", shmname);
|
2021-01-15 18:12:55 +03:00
|
|
|
|
}
|
2012-01-07 03:40:07 +04:00
|
|
|
|
|
2021-01-15 18:12:55 +03:00
|
|
|
|
logbuffer = mmap(NULL, statbuf.st_size, PROT_READ, MAP_SHARED, logbuffer_shm, 0);
|
|
|
|
|
if (logbuffer == MAP_FAILED) {
|
2011-12-10 15:27:40 +04:00
|
|
|
|
err(EXIT_FAILURE, "Could not mmap SHM segment for the i3 log");
|
2021-01-15 18:12:55 +03:00
|
|
|
|
}
|
2011-12-10 15:27:40 +04:00
|
|
|
|
|
2014-06-15 21:07:02 +04:00
|
|
|
|
header = (i3_shmlog_header *)logbuffer;
|
2012-01-07 03:40:07 +04:00
|
|
|
|
|
2021-01-15 18:12:55 +03:00
|
|
|
|
if (verbose) {
|
2012-01-07 03:40:07 +04:00
|
|
|
|
printf("next_write = %d, last_wrap = %d, logbuffer_size = %d, shmname = %s\n",
|
|
|
|
|
header->offset_next_write, header->offset_last_wrap, header->size, shmname);
|
2021-01-15 18:12:55 +03:00
|
|
|
|
}
|
2016-01-08 22:17:34 +03:00
|
|
|
|
free(shmname);
|
2012-08-13 02:57:57 +04:00
|
|
|
|
walk = logbuffer + header->offset_next_write;
|
2011-12-10 15:27:40 +04:00
|
|
|
|
|
2012-08-13 02:57:57 +04: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. */
|
2024-01-27 13:37:05 +03:00
|
|
|
|
while (*walk != '\n') {
|
2012-08-13 02:57:57 +04:00
|
|
|
|
walk++;
|
2024-01-27 13:37:05 +03:00
|
|
|
|
}
|
2012-08-13 02:57:57 +04:00
|
|
|
|
walk++;
|
2011-12-10 15:27:40 +04:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-13 02:57:57 +04: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 15:27:40 +04:00
|
|
|
|
/* Then start from the beginning and print the newer lines */
|
2012-01-07 03:40:07 +04:00
|
|
|
|
walk = logbuffer + sizeof(i3_shmlog_header);
|
2012-08-13 02:57:57 +04:00
|
|
|
|
print_till_end();
|
|
|
|
|
|
2016-06-11 14:05:29 +03:00
|
|
|
|
#if !defined(__OpenBSD__)
|
2017-11-26 19:26:40 +03:00
|
|
|
|
if (!follow) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-15 18:12:55 +03:00
|
|
|
|
char *log_stream_socket_path = root_atom_contents("I3_LOG_STREAM_SOCKET_PATH", NULL, 0);
|
|
|
|
|
if (log_stream_socket_path == NULL) {
|
|
|
|
|
errx(EXIT_FAILURE, "could not determine i3 log stream socket path: possible i3-dump-log and i3 version mismatch");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
|
|
|
|
|
if (sockfd == -1) {
|
|
|
|
|
err(EXIT_FAILURE, "Could not create socket");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(void)fcntl(sockfd, F_SETFD, FD_CLOEXEC);
|
|
|
|
|
|
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
|
memset(&addr, 0, sizeof(struct sockaddr_un));
|
|
|
|
|
addr.sun_family = AF_LOCAL;
|
|
|
|
|
strncpy(addr.sun_path, log_stream_socket_path, sizeof(addr.sun_path) - 1);
|
|
|
|
|
if (connect(sockfd, (const struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) {
|
|
|
|
|
err(EXIT_FAILURE, "Could not connect to i3 on socket %s", log_stream_socket_path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Same size as the buffer used in log.c vlog(): */
|
|
|
|
|
char buf[4096];
|
|
|
|
|
for (;;) {
|
|
|
|
|
const int n = read(sockfd, buf, sizeof(buf));
|
|
|
|
|
if (n == -1) {
|
|
|
|
|
err(EXIT_FAILURE, "read(log-stream-socket):");
|
|
|
|
|
}
|
|
|
|
|
if (n == 0) {
|
|
|
|
|
exit(0); /* i3 closed the socket */
|
2012-08-13 02:57:57 +04:00
|
|
|
|
}
|
2021-01-15 18:12:55 +03:00
|
|
|
|
buf[n] = '\0';
|
|
|
|
|
swrite(STDOUT_FILENO, buf, n);
|
2011-12-10 15:27:40 +04:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-26 19:26:40 +03:00
|
|
|
|
#endif
|
|
|
|
|
exit(0);
|
2011-12-10 15:27:40 +04:00
|
|
|
|
return 0;
|
|
|
|
|
}
|