Subject: Re: bug#29548: 25.2; How to DEBUG get-device-terminal

From: Noam Postavsky <[email-masked]>
Date: Thu, 04 Jan 2018 21:12:28 -0500

Live System User <[email-masked]> writes:

>   Here's a new occurrance (see below):

Hmm, if you can reproduce this semi-regularly, then perhaps the
following patch can catch the reason for the problem.  It sets a
breakpoint in Fsignal while the terminal is in the process of being
deleted (requires reproducing the problem under gdb with the patched
.gdbinit loaded).

>From 3178fb78ba044efcbf6c986b64b82e4adb36d203 Mon Sep 17 00:00:00 2001
From: Noam Postavsky <[email-masked]>
Date: Thu, 4 Jan 2018 21:09:03 -0500
Subject: [PATCH] [TEMP] Debugging for Bug#29548 (terminal deletion)

---
 src/.gdbinit   | 22 ++++++++++++++++++++++
 src/terminal.c | 12 ++++++++++++
 2 files changed, 34 insertions(+)

diff --git a/src/.gdbinit b/src/.gdbinit
index 83341c795a..4cfd2ca88a 100644
--- a/src/.gdbinit
+++ b/src/.gdbinit
@@ -1279,3 +1279,25 @@ commands
   end
   continue
 end
+
+break signal_or_quit
+set $bp_signal_or_quit_num = $bpnum
+disable $bp_signal_or_quit_num
+
+break begin_delete_terminal
+set $bp_begin_delete_terminal_num = $bpnum
+commands $bp_begin_delete_terminal_num
+  if delete_terminal_count == 0
+    enable $bp_signal_or_quit_num
+  end
+  continue
+end
+
+break end_delete_terminal
+set $bp_end_delete_terminal_num = $bpnum
+commands $bp_end_delete_terminal_num
+  if delete_terminal_count == 1
+    disable $bp_signal_or_quit_num
+  end
+  continue
+end
diff --git a/src/terminal.c b/src/terminal.c
index 0b1cbe7b79..3bf18c562d 100644
--- a/src/terminal.c
+++ b/src/terminal.c
@@ -311,6 +311,16 @@ create_terminal (enum output_method type, struct redisplay_interface *rif)
 /* Low-level function to close all frames on a terminal, remove it
    from the terminal list and free its memory.  */
 
+static int delete_terminal_count = 0;
+static void begin_delete_terminal (void)
+{
+    delete_terminal_count++;
+}
+static void end_delete_terminal (void)
+{
+    delete_terminal_count--;
+}
+
 void
 delete_terminal (struct terminal *terminal)
 {
@@ -321,6 +331,7 @@ delete_terminal (struct terminal *terminal)
      delete_terminal_hook when we delete our last frame.  */
   if (!terminal->name)
     return;
+  begin_delete_terminal();
   xfree (terminal->name);
   terminal->name = NULL;
 
@@ -339,6 +350,7 @@ delete_terminal (struct terminal *terminal)
     if (! *tp)
       emacs_abort ();
   *tp = terminal->next_terminal;
+  end_delete_terminal();
 
   xfree (terminal->keyboard_coding);
   terminal->keyboard_coding = NULL;
-- 
2.11.0