2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2011 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>
30 int main(int argc, char *argv[]) {
31 char *socket_path = getenv("I3SOCK");
32 int o, option_index = 0;
33 int message_type = I3_IPC_MESSAGE_TYPE_GET_LOG_MARKERS;
36 static struct option long_options[] = {
37 {"socket", required_argument, 0, 's'},
38 {"version", no_argument, 0, 'v'},
39 {"verbose", no_argument, 0, 'V'},
40 {"help", no_argument, 0, 'h'},
44 char *options_string = "s:vVh";
46 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
48 if (socket_path != NULL)
50 socket_path = sstrdup(optarg);
51 } else if (o == 'v') {
52 printf("i3-dump-log " I3_VERSION "\n");
54 } else if (o == 'V') {
56 } else if (o == 'h') {
57 printf("i3-dump-log " I3_VERSION "\n");
58 printf("i3-dump-log [-s <socket>]\n");
63 if (socket_path == NULL)
64 socket_path = socket_path_from_x11();
66 /* Fall back to the default socket path */
67 if (socket_path == NULL)
68 socket_path = sstrdup("/tmp/i3-ipc.sock");
70 int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
72 err(EXIT_FAILURE, "Could not create socket");
74 struct sockaddr_un addr;
75 memset(&addr, 0, sizeof(struct sockaddr_un));
76 addr.sun_family = AF_LOCAL;
77 strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
78 if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0)
79 err(EXIT_FAILURE, "Could not connect to i3");
81 if (ipc_send_message(sockfd, 0, message_type, NULL) == -1)
82 err(EXIT_FAILURE, "IPC: write()");
84 uint32_t reply_length;
87 if ((ret = ipc_recv_message(sockfd, message_type, &reply_length, &reply)) != 0) {
89 err(EXIT_FAILURE, "IPC: read()");
93 sasprintf(&buffer, "%.*s", reply_length, reply);
94 /* The reply will look like this:
95 * {"offset_next_write":1729,"offset_last_wrap":1996,"size":2048,"shmname":"/i3-log-399"}
96 * IMO, it’s not worth linking a JSON parser in just for this. If the
97 * structure changes in the future, this decision needs to be re-evaluated
99 int offset_next_write, offset_last_wrap, logbuffer_size;
100 char *next_write_str = strstr(buffer, "offset_next_write"),
101 *last_wrap_str = strstr(buffer, "offset_last_wrap"),
102 *size_str = strstr(buffer, "size"),
103 *shmname = strstr(buffer, "shmname");
104 if (!next_write_str ||
108 sscanf(next_write_str, "offset_next_write\":%d", &offset_next_write) != 1 ||
109 sscanf(last_wrap_str, "offset_last_wrap\":%d", &offset_last_wrap) != 1 ||
110 sscanf(size_str, "size\":%d", &logbuffer_size) != 1)
111 errx(EXIT_FAILURE, "invalid IPC reply: %s\n", buffer);
113 shmname += strlen("shmname\":\"");
114 char *quote = strchr(shmname, '"');
116 errx(EXIT_FAILURE, "invalid IPC reply: %s\n", buffer);
120 printf("next_write = %d, last_wrap = %d, logbuffer_size = %d, shmname = %s\n",
121 offset_next_write, offset_last_wrap, logbuffer_size, shmname);
123 if (*shmname == '\0')
124 errx(EXIT_FAILURE, "Cannot dump log: SHM logging is disabled in i3.");
126 int logbuffer_shm = shm_open(shmname, O_RDONLY, 0);
127 if (logbuffer_shm == -1)
128 err(EXIT_FAILURE, "Could not shm_open SHM segment for the i3 log (%s)", shmname);
130 char *logbuffer = mmap(NULL, logbuffer_size, PROT_READ, MAP_SHARED, logbuffer_shm, 0);
131 if (logbuffer == MAP_FAILED)
132 err(EXIT_FAILURE, "Could not mmap SHM segment for the i3 log");
135 char *walk = logbuffer + offset_next_write;
136 /* Skip the first line, it very likely is mangled. Not a problem, though,
137 * the log is chatty enough to have plenty lines left. */
138 while (*walk != '\0')
141 /* Print the oldest log lines. We use printf("%s") to stop on \0. */
142 while (walk < (logbuffer + offset_last_wrap)) {
143 chars = printf("%s", walk);
144 /* Shortcut: If there are two consecutive \0 bytes, this part of the
145 * buffer was never touched. To not call printf() for every byte of the
146 * buffer, we directly exit the loop. */
147 if (*walk == '\0' && *(walk+1) == '\0')
149 walk += (chars > 0 ? chars : 1);
152 /* Then start from the beginning and print the newer lines */
154 while (walk < (logbuffer + offset_next_write)) {
155 chars = printf("%s", walk);
156 walk += (chars > 0 ? chars : 1);