Files
i3/src/sighandler.c
Orestis Floros 38f28186ec Remove some includes from all.h
Also removes duplicates from other headers

All used std* imports are included once in all.h for easy use

- getopt: Only used in main.c and inject_randr1.5.c
- glob: Not used in i3, only in i3bar & libi3
- inttypes: Only used in util.c
- locale: A bit specific for all.h
- math: Slow according to #4022
- unistd: I feel it's good to explicitly include per file that needs it
- yajl: Specific to yajl_utils.h and ipc.h

Related to #4022

Timing before:

```
Analyzing build trace from ...
**** Time summary:
Compilation (81 times):
  Parsing (frontend):           51.3 s
  Codegen & opts (backend):      7.7 s

**** Files that took longest to parse (compiler frontend):
  1254 ms: build/src/i3-commands.o
   972 ms: build/src/i3-resize.o
   945 ms: build/src/i3-con.o
   921 ms: build/src/i3-scratchpad.o
   907 ms: build/src/i3-main.o
   904 ms: build/src/i3-handlers.o
   904 ms: build/src/i3-config_directives.o
   893 ms: build/src/i3-restore_layout.o
   875 ms: build/src/i3-x.o
   854 ms: build/src/i3-ipc.o

**** Files that took longest to codegen (compiler backend):
   863 ms: build/src/i3-commands.o
   471 ms: build/i3bar/src/i3bar-xcb.o
   377 ms: build/src/i3-con.o
   360 ms: build/src/i3-ipc.o
   306 ms: build/src/i3-x.o
   290 ms: build/src/i3-main.o
   238 ms: build/src/i3-config_parser.o
   237 ms: build/src/i3-handlers.o
   220 ms: build/i3-config-wizard/i3_config_wizard-main.o
   214 ms: build/src/i3-bindings.o

**** Functions that took longest to compile:
   209 ms: main (../../i3/src/main.c)
    95 ms: manage_window (../../i3/src/manage.c)
    57 ms: reconfig_windows (../../i3/i3bar/src/xcb.c)
    55 ms: x_draw_decoration (../../i3/src/x.c)
    49 ms: x_push_node (../../i3/src/x.c)
    48 ms: handle_client_message (../../i3/src/handlers.c)
    48 ms: dump_node (../../i3/src/ipc.c)
    47 ms: GENERATED_call (../../i3/src/config_parser.c)
    45 ms: config_string_cb (../../i3/i3bar/src/config.c)
    44 ms: GENERATED_call (../../i3/src/commands_parser.c)
    42 ms: floating_check_size (../../i3/src/floating.c)
    40 ms: con_swap (../../i3/src/con.c)
    40 ms: parse_config (../../i3/src/config_parser.c)
    39 ms: main (../../i3/i3-nagbar/main.c)
    39 ms: cmd_rename_workspace (../../i3/src/commands.c)
    38 ms: window_update_normal_hints (../../i3/src/window.c)
    38 ms: cmd_swap (../../i3/src/commands.c)
    37 ms: dump_bar_config (../../i3/src/ipc.c)
    36 ms: translate_keysyms (../../i3/src/bindings.c)
    35 ms: tree_close_internal (../../i3/src/tree.c)
    34 ms: match_matches_window (../../i3/src/match.c)
    34 ms: floating_enable (../../i3/src/floating.c)
    34 ms: json_string (../../i3/src/load_layout.c)
    33 ms: x_push_changes (../../i3/src/x.c)
    33 ms: main (../../i3/i3-config-wizard/main.c)
    31 ms: free_configuration (../../i3/src/config.c)
    30 ms: parse_file (../../i3/src/config_parser.c)
    28 ms: load_font (../../i3/libi3/font.c)
    28 ms: handle_configure_request (../../i3/src/handlers.c)
    28 ms: parse_command (../../i3/src/commands_parser.c)

**** Function sets that took longest to compile / optimize:

*** Expensive headers:
29287 ms: ../../i3/include/libi3.h (included 78 times, avg 375 ms), included via:
  i3-resize.o all.h data.h  (576 ms)
  a-g_utf8_make_valid.o  (491 ms)
  a-dpi.o  (491 ms)
  a-get_colorpixel.o  (483 ms)
  a-is_debug_build.o  (478 ms)
  a-strndup.o  (478 ms)
  ...

29221 ms: ../../i3/include/all.h (included 39 times, avg 749 ms), included via:
  i3-resize.o  (946 ms)
  i3-scratchpad.o  (895 ms)
  i3-restore_layout.o  (865 ms)
  i3-handlers.o  (839 ms)
  i3-drag.o  (830 ms)
  i3-config_directives.o  (793 ms)
  ...

5195 ms: /usr/include/xcb/xcb.h (included 79 times, avg 65 ms), included via:
  i3-handlers.o all.h  (113 ms)
  i3-restore_layout.o all.h  (108 ms)
  a-g_utf8_make_valid.o libi3.h  (108 ms)
  i3bar-xcb.o common.h  (105 ms)
  i3-scratchpad.o all.h  (95 ms)
  a-get_colorpixel.o libi3.h  (89 ms)
  ...

4100 ms: /usr/include/math.h (included 41 times, avg 100 ms), included via:
  i3-scratchpad.o all.h  (180 ms)
  i3-fake_outputs.o all.h  (138 ms)
  i3-regex.o all.h  (130 ms)
  i3-restore_layout.o all.h  (128 ms)
  i3-xcb.o all.h  (121 ms)
  i3-move.o all.h  (119 ms)
  ...

4046 ms: ../../i3/i3bar/include/common.h (included 9 times, avg 449 ms), included via:
  i3bar-main.o  (503 ms)
  i3bar-xcb.o  (501 ms)
  i3bar-workspaces.o  (472 ms)
  i3bar-parse_json_header.o  (446 ms)
  i3bar-child.o  (438 ms)
  i3bar-ipc.o  (434 ms)
  ...

2713 ms: ../../i3/include/i3.h (included 41 times, avg 66 ms), included via:
  i3-xcursor.o  (450 ms)
  i3-config_directives.o all.h ipc.h configuration.h  (87 ms)
  i3-config.o all.h ipc.h configuration.h  (71 ms)
  i3-manage.o all.h ipc.h configuration.h  (70 ms)
  i3-window.o all.h ipc.h configuration.h  (68 ms)
  i3-x.o all.h ipc.h configuration.h  (61 ms)
  ...

1492 ms: /usr/include/xcb/xkb.h (included 42 times, avg 35 ms), included via:
  i3-config_directives.o all.h ipc.h configuration.h i3.h  (50 ms)
  i3-config.o all.h ipc.h configuration.h i3.h  (45 ms)
  i3-window.o all.h ipc.h configuration.h i3.h  (43 ms)
  i3-x.o all.h ipc.h configuration.h i3.h  (42 ms)
  i3-manage.o all.h ipc.h configuration.h i3.h  (41 ms)
  i3-config_parser.o all.h ipc.h configuration.h i3.h  (38 ms)
  ...

1432 ms: /usr/include/stdlib.h (included 79 times, avg 18 ms), included via:
  i3-scratchpad.o all.h  (48 ms)
  i3-restore_layout.o all.h  (36 ms)
  i3-regex.o all.h  (32 ms)
  i3-key_press.o all.h  (28 ms)
  i3-commands.o all.h  (28 ms)
  i3-bindings.o all.h  (24 ms)
  ...

1349 ms: /usr/include/pthread.h (included 79 times, avg 17 ms), included via:
  i3bar-xcb.o common.h xcb.h  (33 ms)
  a-ucs2_conversion.o libi3.h xcb.h  (32 ms)
  i3-match.o all.h xcb.h  (27 ms)
  i3-scratchpad.o all.h xcb.h  (25 ms)
  a-g_utf8_make_valid.o libi3.h xcb.h  (25 ms)
  i3_config_wizard-main.o xcb.h  (24 ms)
  ...

1151 ms: /usr/include/X11/Xlib.h (included 45 times, avg 25 ms), included via:
  i3-output.o all.h data.h sn-launcher.h sn-common.h  (50 ms)
  i3-config_parser.o all.h data.h sn-launcher.h sn-common.h  (43 ms)
  i3-x.o all.h data.h sn-launcher.h sn-common.h  (34 ms)
  i3-config_directives.o all.h data.h sn-launcher.h sn-common.h  (32 ms)
  i3_config_wizard-main.o sn-launchee.h sn-common.h  (30 ms)
  i3-drag.o all.h data.h sn-launcher.h sn-common.h  (29 ms)
  ...
```

Timing after:

```
Analyzing build trace from ...
**** Time summary:
Compilation (81 times):
  Parsing (frontend):           47.6 s
  Codegen & opts (backend):      7.6 s

**** Files that took longest to parse (compiler frontend):
  1154 ms: build/src/i3-commands.o
   929 ms: build/src/i3-display_version.o
   852 ms: build/src/i3-bindings.o
   847 ms: build/src/i3-con.o
   806 ms: build/src/i3-ipc.o
   801 ms: build/src/i3-floating.o
   792 ms: build/src/i3-main.o
   792 ms: build/src/i3-drag.o
   792 ms: build/src/i3-window.o
   776 ms: build/src/i3-config_directives.o

**** Files that took longest to codegen (compiler backend):
   885 ms: build/src/i3-commands.o
   422 ms: build/i3bar/src/i3bar-xcb.o
   382 ms: build/src/i3-con.o
   348 ms: build/src/i3-x.o
   288 ms: build/src/i3-ipc.o
   268 ms: build/src/i3-handlers.o
   254 ms: build/src/i3-main.o
   251 ms: build/src/i3-floating.o
   249 ms: build/src/i3-config_parser.o
   194 ms: build/src/i3-randr.o

**** Functions that took longest to compile:
   186 ms: main (../../i3/src/main.c)
    95 ms: manage_window (../../i3/src/manage.c)
    65 ms: floating_check_size (../../i3/src/floating.c)
    63 ms: x_draw_decoration (../../i3/src/x.c)
    58 ms: handle_client_message (../../i3/src/handlers.c)
    55 ms: x_push_node (../../i3/src/x.c)
    54 ms: match_matches_window (../../i3/src/match.c)
    51 ms: parse_config (../../i3/src/config_parser.c)
    49 ms: dump_node (../../i3/src/ipc.c)
    47 ms: reconfig_windows (../../i3/i3bar/src/xcb.c)
    47 ms: config_string_cb (../../i3/i3bar/src/config.c)
    45 ms: GENERATED_call (../../i3/src/config_parser.c)
    45 ms: GENERATED_call (../../i3/src/commands_parser.c)
    43 ms: floating_enable (../../i3/src/floating.c)
    42 ms: handle_configure_request (../../i3/src/handlers.c)
    40 ms: con_swap (../../i3/src/con.c)
    36 ms: main (../../i3/i3-input/main.c)
    36 ms: main (../../i3/i3-msg/main.c)
    36 ms: main (../../i3/i3-nagbar/main.c)
    36 ms: cmd_move_con_to_workspace_number (../../i3/src/commands.c)
    35 ms: json_string (../../i3/src/load_layout.c)
    35 ms: tree_restore (../../i3/src/tree.c)
    35 ms: cmd_swap (../../i3/src/commands.c)
    34 ms: x_push_changes (../../i3/src/x.c)
    32 ms: main (../../i3/i3-config-wizard/main.c)
    32 ms: ewmh_setup_hints (../../i3/src/ewmh.c)
    31 ms: match_parse_property (../../i3/src/match.c)
    30 ms: cmd_mark (../../i3/src/commands.c)
    30 ms: translate_keysyms (../../i3/src/bindings.c)
    30 ms: window_update_normal_hints (../../i3/src/window.c)

**** Function sets that took longest to compile / optimize:

*** Expensive headers:
29596 ms: ../../i3/include/libi3.h (included 78 times, avg 379 ms), included via:
  a-get_config_path.o  (539 ms)
  i3_dump_log-main.o  (522 ms)
  i3_config_wizard-main.o  (501 ms)
  a-fake_configure_notify.o  (500 ms)
  a-root_atom_contents.o  (488 ms)
  i3-display_version.o all.h  (466 ms)
  ...

26054 ms: ../../i3/include/all.h (included 41 times, avg 635 ms), included via:
  i3-display_version.o  (901 ms)
  i3-drag.o  (775 ms)
  i3-ewmh.o  (703 ms)
  i3-startup.o  (693 ms)
  i3-commands.o  (687 ms)
  i3-xcb.o  (680 ms)
  ...

5345 ms: /usr/include/xcb/xcb.h (included 79 times, avg 67 ms), included via:
  i3-display_version.o all.h  (173 ms)
  i3_input-keysym2ucs.o keysym2ucs.h  (106 ms)
  i3-ewmh.o all.h  (106 ms)
  a-fake_configure_notify.o libi3.h  (103 ms)
  a-get_config_path.o libi3.h  (95 ms)
  i3bar-parse_json_header.o common.h  (93 ms)
  ...

4127 ms: ../../i3/i3bar/include/common.h (included 9 times, avg 458 ms), included via:
  i3bar-child.o  (524 ms)
  i3bar-mode.o  (486 ms)
  i3bar-outputs.o  (464 ms)
  i3bar-parse_json_header.o  (463 ms)
  i3bar-config.o  (457 ms)
  i3bar-ipc.o  (448 ms)
  ...

1542 ms: /usr/include/xcb/xkb.h (included 42 times, avg 36 ms), included via:
  i3-con.o all.h ipc.h configuration.h i3.h  (60 ms)
  i3-render.o all.h ipc.h configuration.h i3.h  (56 ms)
  i3-bindings.o all.h ipc.h configuration.h i3.h  (56 ms)
  i3-sighandler.o all.h ipc.h configuration.h i3.h  (48 ms)
  i3-xcb.o all.h ipc.h configuration.h i3.h  (47 ms)
  i3-resize.o all.h ipc.h configuration.h i3.h  (39 ms)
  ...

1456 ms: /usr/include/stdlib.h (included 79 times, avg 18 ms), included via:
  i3-drag.o all.h  (60 ms)
  i3-display_version.o all.h  (55 ms)
  i3-fake_outputs.o all.h  (39 ms)
  i3-config_directives.o all.h  (33 ms)
  i3-xcursor.o all.h  (30 ms)
  i3bar-mode.o common.h libi3.h pango.h pango-attributes.h pango-font.h pango-coverage.h glib-object.h gbinding.h glib.h gasyncqueue.h gthread.h gutils.h  (29 ms)
  ...

1136 ms: /usr/include/X11/Xlib.h (included 44 times, avg 25 ms), included via:
  i3-con.o all.h data.h sn-launcher.h sn-common.h  (35 ms)
  i3-resize.o all.h data.h sn-launcher.h sn-common.h  (34 ms)
  i3-util.o all.h data.h sn-launcher.h sn-common.h  (33 ms)
  i3-assignments.o all.h data.h sn-launcher.h sn-common.h  (33 ms)
  i3-sighandler.o all.h data.h sn-launcher.h sn-common.h  (31 ms)
  i3-xcb.o all.h data.h sn-launcher.h sn-common.h  (31 ms)
  ...

808 ms: /usr/include/stdio.h (included 79 times, avg 10 ms), included via:
  i3-drag.o all.h  (19 ms)
  i3-fake_outputs.o all.h  (18 ms)
  a-font.o libi3.h  (16 ms)
  i3bar-child.o common.h libi3.h  (15 ms)
  a-safewrappers.o libi3.h  (15 ms)
  a-ipc_send_message.o libi3.h  (13 ms)
  ...

770 ms: /usr/include/xcb/randr.h (included 42 times, avg 18 ms), included via:
  i3-click.o all.h data.h  (29 ms)
  i3-commands.o all.h data.h  (27 ms)
  i3-assignments.o all.h data.h  (25 ms)
  i3-xcb.o all.h data.h  (21 ms)
  i3-resize.o all.h data.h  (21 ms)
  i3-sighandler.o all.h data.h  (20 ms)
  ...

688 ms: /usr/include/math.h (included 6 times, avg 114 ms), included via:
  a-dpi.o  (145 ms)
  i3-render.o  (127 ms)
  i3-floating.o  (106 ms)
  a-root_atom_contents.o  (106 ms)
  i3-window.o  (102 ms)
  i3-bindings.o  (99 ms)
  ...
```
2020-04-20 05:54:17 +02:00

351 lines
11 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* vim:ts=4:sw=4:expandtab
*
* i3 - an improved dynamic tiling window manager
* © 2009 Michael Stapelberg and contributors (see also: LICENSE)
*
*/
#include "all.h"
#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>
typedef struct dialog_t {
xcb_window_t id;
xcb_colormap_t colormap;
Rect dims;
surface_t surface;
TAILQ_ENTRY(dialog_t)
dialogs;
} dialog_t;
static TAILQ_HEAD(dialogs_head, dialog_t) dialogs = TAILQ_HEAD_INITIALIZER(dialogs);
static int raised_signal;
static int backtrace_done = 0;
static int sighandler_backtrace(void);
static void sighandler_setup(void);
static void sighandler_create_dialogs(void);
static void sighandler_destroy_dialogs(void);
static void sighandler_handle_expose(void);
static void sighandler_draw_dialog(dialog_t *dialog);
static void sighandler_handle_key_press(xcb_key_press_event_t *event);
static i3String *message_intro;
static i3String *message_intro2;
static i3String *message_option_backtrace;
static i3String *message_option_restart;
static i3String *message_option_forget;
static int dialog_width;
static int dialog_height;
static int border_width = 2;
static int margin = 4;
/*
* Attach gdb to pid_parent and dump a backtrace to i3-backtrace.$pid in the
* tmpdir
*/
static int sighandler_backtrace(void) {
char *tmpdir = getenv("TMPDIR");
if (tmpdir == NULL)
tmpdir = "/tmp";
pid_t pid_parent = getpid();
char *filename = NULL;
int suffix = 0;
/* Find a unique filename for the backtrace (since the PID of i3 stays the
* same), so that we dont overwrite earlier backtraces. */
do {
FREE(filename);
sasprintf(&filename, "%s/i3-backtrace.%d.%d.txt", tmpdir, pid_parent, suffix);
suffix++;
} while (path_exists(filename));
pid_t pid_gdb = fork();
if (pid_gdb < 0) {
DLOG("Failed to fork for GDB\n");
return -1;
} else if (pid_gdb == 0) {
/* child */
int stdin_pipe[2],
stdout_pipe[2];
if (pipe(stdin_pipe) == -1) {
ELOG("Failed to init stdin_pipe\n");
return -1;
}
if (pipe(stdout_pipe) == -1) {
ELOG("Failed to init stdout_pipe\n");
return -1;
}
/* close standard streams in case i3 is started from a terminal; gdb
* needs to run without controlling terminal for it to work properly in
* this situation */
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
/* We provide pipe file descriptors for stdin/stdout because gdb < 7.5
* crashes otherwise, see
* https://sourceware.org/bugzilla/show_bug.cgi?id=14114 */
dup2(stdin_pipe[0], STDIN_FILENO);
dup2(stdout_pipe[1], STDOUT_FILENO);
char *pid_s, *gdb_log_cmd;
sasprintf(&pid_s, "%d", pid_parent);
sasprintf(&gdb_log_cmd, "set logging file %s", filename);
char *args[] = {
"gdb",
start_argv[0],
"-p",
pid_s,
"-batch",
"-nx",
"-ex", gdb_log_cmd,
"-ex", "set logging on",
"-ex", "bt full",
"-ex", "quit",
NULL};
execvp(args[0], args);
DLOG("Failed to exec GDB\n");
exit(EXIT_FAILURE);
}
int status = 0;
waitpid(pid_gdb, &status, 0);
/* see if the backtrace was successful or not */
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
DLOG("GDB did not run properly\n");
return -1;
} else if (!path_exists(filename)) {
DLOG("GDB executed successfully, but no backtrace was generated\n");
return -1;
}
return 1;
}
static void sighandler_setup(void) {
border_width = logical_px(border_width);
margin = logical_px(margin);
int num_lines = 5;
message_intro = i3string_from_utf8("i3 has just crashed. Please report a bug for this.");
message_intro2 = i3string_from_utf8("To debug this problem, you can either attach gdb or choose from the following options:");
message_option_backtrace = i3string_from_utf8("- 'b' to save a backtrace (requires gdb)");
message_option_restart = i3string_from_utf8("- 'r' to restart i3 in-place");
message_option_forget = i3string_from_utf8("- 'f' to forget the previous layout and restart i3");
int width_longest_message = predict_text_width(message_intro2);
dialog_width = width_longest_message + 2 * border_width + 2 * margin;
dialog_height = num_lines * config.font.height + 2 * border_width + 2 * margin;
}
static void sighandler_create_dialogs(void) {
Output *output;
TAILQ_FOREACH (output, &outputs, outputs) {
if (!output->active) {
continue;
}
dialog_t *dialog = scalloc(1, sizeof(struct dialog_t));
TAILQ_INSERT_TAIL(&dialogs, dialog, dialogs);
xcb_visualid_t visual = get_visualid_by_depth(root_depth);
dialog->colormap = xcb_generate_id(conn);
xcb_create_colormap(conn, XCB_COLORMAP_ALLOC_NONE, dialog->colormap, root, visual);
uint32_t mask = 0;
uint32_t values[4];
int i = 0;
/* Needs to be set in the case of a 32-bit root depth. */
mask |= XCB_CW_BACK_PIXEL;
values[i++] = root_screen->black_pixel;
/* Needs to be set in the case of a 32-bit root depth. */
mask |= XCB_CW_BORDER_PIXEL;
values[i++] = root_screen->black_pixel;
mask |= XCB_CW_OVERRIDE_REDIRECT;
values[i++] = 1;
/* Needs to be set in the case of a 32-bit root depth. */
mask |= XCB_CW_COLORMAP;
values[i++] = dialog->colormap;
dialog->dims.x = output->rect.x + (output->rect.width / 2);
dialog->dims.y = output->rect.y + (output->rect.height / 2);
dialog->dims.width = dialog_width;
dialog->dims.height = dialog_height;
/* Make sure the dialog is centered. */
dialog->dims.x -= dialog->dims.width / 2;
dialog->dims.y -= dialog->dims.height / 2;
dialog->id = create_window(conn, dialog->dims, root_depth, visual,
XCB_WINDOW_CLASS_INPUT_OUTPUT, XCURSOR_CURSOR_POINTER,
true, mask, values);
draw_util_surface_init(conn, &(dialog->surface), dialog->id, get_visualtype_by_id(visual),
dialog->dims.width, dialog->dims.height);
xcb_grab_keyboard(conn, false, dialog->id, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
/* Confine the pointer to the crash dialog. */
xcb_grab_pointer(conn, false, dialog->id, XCB_NONE, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, dialog->id,
XCB_NONE, XCB_CURRENT_TIME);
}
sighandler_handle_expose();
xcb_flush(conn);
}
static void sighandler_destroy_dialogs(void) {
while (!TAILQ_EMPTY(&dialogs)) {
dialog_t *dialog = TAILQ_FIRST(&dialogs);
xcb_free_colormap(conn, dialog->colormap);
draw_util_surface_free(conn, &(dialog->surface));
xcb_destroy_window(conn, dialog->id);
TAILQ_REMOVE(&dialogs, dialog, dialogs);
free(dialog);
}
xcb_flush(conn);
}
static void sighandler_handle_expose(void) {
dialog_t *current;
TAILQ_FOREACH (current, &dialogs, dialogs) {
sighandler_draw_dialog(current);
}
xcb_flush(conn);
}
static void sighandler_draw_dialog(dialog_t *dialog) {
const color_t black = draw_util_hex_to_color("#000000");
const color_t white = draw_util_hex_to_color("#FFFFFF");
const color_t red = draw_util_hex_to_color("#FF0000");
/* Start with a clean slate and draw a red border. */
draw_util_clear_surface(&(dialog->surface), red);
draw_util_rectangle(&(dialog->surface), black, border_width, border_width,
dialog->dims.width - 2 * border_width, dialog->dims.height - 2 * border_width);
int y = border_width + margin;
const int x = border_width + margin;
const int max_width = dialog->dims.width - 2 * x;
draw_util_text(message_intro, &(dialog->surface), white, black, x, y, max_width);
y += config.font.height;
draw_util_text(message_intro2, &(dialog->surface), white, black, x, y, max_width);
y += config.font.height;
char *bt_color = "#FFFFFF";
if (backtrace_done < 0) {
bt_color = "#AA0000";
} else if (backtrace_done > 0) {
bt_color = "#00AA00";
}
draw_util_text(message_option_backtrace, &(dialog->surface), draw_util_hex_to_color(bt_color), black, x, y, max_width);
y += config.font.height;
draw_util_text(message_option_restart, &(dialog->surface), white, black, x, y, max_width);
y += config.font.height;
draw_util_text(message_option_forget, &(dialog->surface), white, black, x, y, max_width);
y += config.font.height;
}
static void sighandler_handle_key_press(xcb_key_press_event_t *event) {
uint16_t state = event->state;
/* Apparently, after activating numlock once, the numlock modifier
* stays turned on (use xev(1) to verify). So, to resolve useful
* keysyms, we remove the numlock flag from the event state */
state &= ~xcb_numlock_mask;
xcb_keysym_t sym = xcb_key_press_lookup_keysym(keysyms, event, state);
if (sym == 'b') {
DLOG("User issued core-dump command.\n");
/* fork and exec/attach GDB to the parent to get a backtrace in the
* tmpdir */
backtrace_done = sighandler_backtrace();
sighandler_handle_expose();
} else if (sym == 'r') {
sighandler_destroy_dialogs();
i3_restart(false);
} else if (sym == 'f') {
sighandler_destroy_dialogs();
i3_restart(true);
}
}
static void handle_signal(int sig, siginfo_t *info, void *data) {
DLOG("i3 crashed. SIG: %d\n", sig);
struct sigaction action;
action.sa_handler = SIG_DFL;
action.sa_flags = 0;
sigemptyset(&action.sa_mask);
sigaction(sig, &action, NULL);
raised_signal = sig;
sighandler_setup();
sighandler_create_dialogs();
xcb_generic_event_t *event;
/* Yay, more own eventhandlers… */
while ((event = xcb_wait_for_event(conn))) {
/* Strip off the highest bit (set if the event is generated) */
int type = (event->response_type & 0x7F);
switch (type) {
case XCB_KEY_PRESS:
sighandler_handle_key_press((xcb_key_press_event_t *)event);
break;
case XCB_EXPOSE:
if (((xcb_expose_event_t *)event)->count == 0) {
sighandler_handle_expose();
}
break;
}
free(event);
}
}
/*
* Configured a signal handler to gracefully handle crashes and allow the user
* to generate a backtrace and rescue their session.
*
*/
void setup_signal_handler(void) {
struct sigaction action;
action.sa_sigaction = handle_signal;
action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
sigemptyset(&action.sa_mask);
/* Catch all signals with default action "Core", see signal(7) */
if (sigaction(SIGQUIT, &action, NULL) == -1 ||
sigaction(SIGILL, &action, NULL) == -1 ||
sigaction(SIGABRT, &action, NULL) == -1 ||
sigaction(SIGFPE, &action, NULL) == -1 ||
sigaction(SIGSEGV, &action, NULL) == -1)
ELOG("Could not setup signal handler.\n");
}