2 #define I3__FILE__ "sighandler.c"
21 #include <xcb/xcb_event.h>
23 #include <X11/keysym.h>
33 "To debug this problem, either attach gdb now",
35 "- 'b' to save a backtrace (needs GDB),",
36 "- 'r' to restart i3 in-place or",
37 "- 'f' to forget the current layout and restart"};
47 char *tmpdir = getenv(
"TMPDIR");
51 pid_t pid_parent = getpid();
53 char *filename = NULL;
60 sasprintf(&filename,
"%s/i3-backtrace.%d.%d.txt", tmpdir, pid_parent, suffix);
62 }
while (stat(filename, &bt) == 0);
64 pid_t pid_gdb = fork();
66 DLOG(
"Failed to fork for GDB\n");
68 }
else if (pid_gdb == 0) {
86 dup2(stdin_pipe[0], STDIN_FILENO);
87 dup2(stdout_pipe[1], STDOUT_FILENO);
89 char *pid_s, *gdb_log_cmd;
91 sasprintf(&gdb_log_cmd,
"set logging file %s", filename);
101 "-ex",
"set logging on",
105 execvp(args[0], args);
106 DLOG(
"Failed to exec GDB\n");
111 waitpid(pid_gdb, &status, 0);
114 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
115 DLOG(
"GDB did not run properly\n");
117 }
else if (stat(filename, &bt) == -1) {
118 DLOG(
"GDB executed successfully, but no backtrace was generated\n");
130 xcb_rectangle_t border = {0, 0,
width, height},
131 inner = {2, 2, width - 4, height - 4};
140 char *bt_colour =
"#FFFFFF";
142 bt_colour =
"#AA0000";
144 bt_colour =
"#00AA00";
146 for (
int i = 0; crash_text_i3strings[i] != NULL; ++i) {
152 8, 5 + i * font_height, width - 16);
171 uint16_t
state =
event->state;
178 xcb_keysym_t sym = xcb_key_press_lookup_keysym(
keysyms, event, state);
181 DLOG(
"User issued core-dump command.\n");
205 xcb_window_t win = xcb_generate_id(conn);
210 mask |= XCB_CW_BACK_PIXEL;
213 mask |= XCB_CW_OVERRIDE_REDIRECT;
217 uint32_t
x = screen_rect.
x + ((screen_rect.
width / 2) - (width / 2)),
218 y = screen_rect.
y + ((screen_rect.
height / 2) - (height / 2));
220 xcb_create_window(conn,
221 XCB_COPY_FROM_PARENT,
226 XCB_WINDOW_CLASS_INPUT_OUTPUT,
227 XCB_WINDOW_CLASS_COPY_FROM_PARENT,
232 xcb_map_window(conn, win);
239 int crash_text_num =
sizeof(
crash_text) /
sizeof(
char *);
242 int crash_text_length =
sizeof(
crash_text) /
sizeof(
char *);
245 for (
int i = 0; i < crash_text_length; ++i) {
248 crash_text_i3strings[crash_text_length] = NULL;
251 int width = font_width + 20;
268 xcb_grab_keyboard(
conn,
false, win, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
271 xcb_grab_pointer(
conn,
false, win, XCB_NONE, XCB_GRAB_MODE_ASYNC,
272 XCB_GRAB_MODE_ASYNC, win, XCB_NONE, XCB_CURRENT_TIME);
286 DLOG(
"i3 crashed. SIG: %d\n", sig);
288 struct sigaction action;
289 action.sa_handler = SIG_DFL;
290 sigaction(sig, &action, NULL);
295 xcb_generic_event_t *event;
297 while ((event = xcb_wait_for_event(
conn))) {
299 int type = (
event->response_type & 0x7F);
300 if (type == XCB_KEY_PRESS) {
312 struct sigaction action;
315 action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
316 sigemptyset(&action.sa_mask);
319 if (sigaction(SIGQUIT, &action, NULL) == -1 ||
320 sigaction(SIGILL, &action, NULL) == -1 ||
321 sigaction(SIGABRT, &action, NULL) == -1 ||
322 sigaction(SIGFPE, &action, NULL) == -1 ||
323 sigaction(SIGSEGV, &action, NULL) == -1)
324 ELOG(
"Could not setup signal handler");
int predict_text_width(i3String *text)
Predict the text width in pixels for the given text.
Stores a rectangle, for example the size of a window, the child window etc.
static int sig_handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event)
Rect rect
x, y, width, height
void handle_signal(int sig, siginfo_t *info, void *data)
An Output is a physical output on your graphics driver.
static xcb_gcontext_t pixmap_gc
i3String * i3string_from_utf8(const char *from_utf8)
Build an i3String from an UTF-8 encoded string.
void i3_restart(bool forget_layout)
Restart i3 in-place appends -a to argument list to disable autostart.
static xcb_pixmap_t pixmap
int sasprintf(char **strp, const char *fmt,...)
Safe-wrapper around asprintf which exits if it returns -1 (meaning that there is no more memory avail...
static int backtrace(void)
uint32_t get_colorpixel(const char *hex) __attribute__((const ))
Returns the colorpixel to use for the given hex color (think of HTML).
struct _i3String i3String
Opaque data structure for storing strings.
struct outputs_head outputs
int height
The height of the font, built from font_ascent + font_descent.
void draw_text(i3String *text, xcb_drawable_t drawable, xcb_gcontext_t gc, int x, int y, int max_width)
Draws text onto the specified X drawable (normally a pixmap) at the specified coordinates (from the t...
void setup_signal_handler(void)
Setup signal handlers to safely handle SIGSEGV and SIGFPE.
static int sig_draw_window(xcb_window_t win, int width, int height, int font_height, i3String **crash_text_i3strings)
static void open_popups(void)
bool active
Whether the output is currently active (has a CRTC attached with a valid mode)
static int crash_text_longest
static int backtrace_string_index
unsigned int xcb_numlock_mask
void set_font_colors(xcb_gcontext_t gc, uint32_t foreground, uint32_t background)
Defines the colors to be used for the forthcoming draw_text calls.
static char * crash_text[]
static xcb_window_t open_input_window(xcb_connection_t *conn, Rect screen_rect, uint32_t width, uint32_t height)
void * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...
xcb_key_symbols_t * keysyms
#define TAILQ_FOREACH(var, head, field)
static int backtrace_done