Blame SOURCES/gdb-gnat-dwarf-crash-3of3.patch

7d6eda
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
7d6eda
From: Fedora GDB patches <invalid@email.com>
7d6eda
Date: Fri, 27 Oct 2017 21:07:50 +0200
7d6eda
Subject: gdb-gnat-dwarf-crash-3of3.patch
7d6eda
7d6eda
;; Fix crash of -readnow /usr/lib/debug/usr/bin/gnatbind.debug (BZ 1069211).
7d6eda
;;=push+jan
7d6eda
7d6eda
http://sourceware.org/ml/gdb-patches/2014-02/msg00731.html
7d6eda
7d6eda
--6TrnltStXW4iwmi0
7d6eda
Content-Type: text/plain; charset=us-ascii
7d6eda
Content-Disposition: inline
7d6eda
7d6eda
Hi,
7d6eda
7d6eda
PR 16581:
7d6eda
	GDB crash on inherit_abstract_dies infinite recursion
7d6eda
	https://sourceware.org/bugzilla/show_bug.cgi?id=16581
7d6eda
7d6eda
fixed crash from an infinite recursion.  But in rare cases the new code can
7d6eda
now gdb_assert() due to weird DWARF file.
7d6eda
7d6eda
I do not yet fully understand why the DWARF is as it is but just GDB should
7d6eda
never crash due to invalid DWARF anyway.  The "invalid" DWARF I see only in
7d6eda
Fedora GCC build, not in FSF GCC build, more info at:
7d6eda
	https://bugzilla.redhat.com/show_bug.cgi?id=1069382
7d6eda
	http://people.redhat.com/jkratoch/gcc-debuginfo-4.8.2-7.fc20.x86_64-gnatbind.debug
7d6eda
7d6eda
Thanks,
7d6eda
Jan
7d6eda
7d6eda
--6TrnltStXW4iwmi0
7d6eda
Content-Type: text/plain; charset=us-ascii
7d6eda
Content-Disposition: inline; filename="complaint.patch"
7d6eda
7d6eda
gdb/
7d6eda
2014-02-24  Jan Kratochvil  <jan.kratochvil@redhat.com>
7d6eda
7d6eda
	* dwarf2read.c (process_die): Change gdb_assert to complaint.
7d6eda
7d6eda
diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c
7d6eda
--- a/gdb/dwarf2read.c
7d6eda
+++ b/gdb/dwarf2read.c
7d6eda
@@ -10657,6 +10657,13 @@ private:
7d6eda
 static void
7d6eda
 process_die (struct die_info *die, struct dwarf2_cu *cu)
7d6eda
 {
7d6eda
+  if (die->in_process)
7d6eda
+    {
7d6eda
+      complaint (_("DIE at 0x%s attempted to be processed twice"),
7d6eda
+		 sect_offset_str (die->sect_off));
7d6eda
+      return;
7d6eda
+    }
7d6eda
+
7d6eda
   process_die_scope scope (die, cu);
7d6eda
 
7d6eda
   switch (die->tag)
7d6eda
diff --git a/gdb/infrun.c b/gdb/infrun.c
7d6eda
--- a/gdb/infrun.c
7d6eda
+++ b/gdb/infrun.c
7d6eda
@@ -601,6 +601,13 @@ holding the child stopped.  Try \"set detach-on-fork\" or \
7d6eda
 				target_pid_to_str (process_ptid).c_str ());
7d6eda
 	    }
7d6eda
 
7d6eda
+#ifdef NEED_DETACH_SIGSTOP
7d6eda
+	  /* We should check PID_WAS_STOPPED and detach it stopped accordingly.
7d6eda
+	     In this point of code it cannot be 1 as we would not get FORK
7d6eda
+	     executed without CONTINUE first which resets PID_WAS_STOPPED.
7d6eda
+	     We would have to first TARGET_STOP and WAITPID it as with running
7d6eda
+	     inferior PTRACE_DETACH, SIGSTOP will ignore the signal.  */
7d6eda
+#endif
7d6eda
 	  target_detach (parent_inf, 0);
7d6eda
 	}
7d6eda
 
7d6eda
diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
7d6eda
--- a/gdb/linux-nat.c
7d6eda
+++ b/gdb/linux-nat.c
7d6eda
@@ -189,6 +189,12 @@ struct linux_nat_target *linux_target;
7d6eda
 /* Does the current host support PTRACE_GETREGSET?  */
7d6eda
 enum tribool have_ptrace_getregset = TRIBOOL_UNKNOWN;
7d6eda
 
7d6eda
+#ifdef NEED_DETACH_SIGSTOP
7d6eda
+/* PID of the inferior stopped by SIGSTOP before attaching (or zero).  */
7d6eda
+static pid_t pid_was_stopped;
7d6eda
+
7d6eda
+#endif
7d6eda
+
7d6eda
 static unsigned int debug_linux_nat;
7d6eda
 static void
7d6eda
 show_debug_linux_nat (struct ui_file *file, int from_tty,
7d6eda
@@ -1030,6 +1036,9 @@ linux_nat_post_attach_wait (ptid_t ptid, int *signalled)
7d6eda
       if (debug_linux_nat)
7d6eda
 	fprintf_unfiltered (gdb_stdlog,
7d6eda
 			    "LNPAW: Attaching to a stopped process\n");
7d6eda
+#ifdef NEED_DETACH_SIGSTOP
7d6eda
+      pid_was_stopped = ptid.pid ();
7d6eda
+#endif
7d6eda
 
7d6eda
       /* The process is definitely stopped.  It is in a job control
7d6eda
 	 stop, unless the kernel predates the TASK_STOPPED /
7d6eda
@@ -1361,6 +1370,25 @@ get_detach_signal (struct lwp_info *lp)
7d6eda
       return gdb_signal_to_host (signo);
7d6eda
     }
7d6eda
 
7d6eda
+#ifdef NEED_DETACH_SIGSTOP
7d6eda
+  /* Workaround RHEL-5 kernel which has unreliable PTRACE_DETACH, SIGSTOP (that
7d6eda
+     many TIDs are left unstopped).  See RH Bug 496732.  */
7d6eda
+  if (lp->ptid.pid () == pid_was_stopped)
7d6eda
+    {
7d6eda
+      int err;
7d6eda
+
7d6eda
+      errno = 0;
7d6eda
+      err = kill_lwp (lp->ptid.lwp (), SIGSTOP);
7d6eda
+      if (debug_linux_nat)
7d6eda
+	{
7d6eda
+	  fprintf_unfiltered (gdb_stdlog,
7d6eda
+			      "SC:  lwp kill %d %s\n",
7d6eda
+			      err,
7d6eda
+			      errno ? safe_strerror (errno) : "ERRNO-OK");
7d6eda
+	}
7d6eda
+    }
7d6eda
+
7d6eda
+#endif
7d6eda
   return 0;
7d6eda
 }
7d6eda
 
7d6eda
@@ -1509,6 +1537,10 @@ linux_nat_target::detach (inferior *inf, int from_tty)
7d6eda
       detach_one_lwp (main_lwp, &signo);
7d6eda
 
7d6eda
       detach_success (inf);
7d6eda
+
7d6eda
+#ifdef NEED_DETACH_SIGSTOP
7d6eda
+      pid_was_stopped = 0;
7d6eda
+#endif
7d6eda
     }
7d6eda
 }
7d6eda
 
7d6eda
@@ -1766,6 +1798,16 @@ linux_nat_target::resume (ptid_t ptid, int step, enum gdb_signal signo)
7d6eda
       return;
7d6eda
     }
7d6eda
 
7d6eda
+#ifdef NEED_DETACH_SIGSTOP
7d6eda
+  /* At this point, we are going to resume the inferior and if we
7d6eda
+     have attached to a stopped process, we no longer should leave
7d6eda
+     it as stopped if the user detaches.  PTID variable has PID set to LWP
7d6eda
+     while we need to check the real PID here.  */
7d6eda
+
7d6eda
+  if (!step && lp && pid_was_stopped == lp->ptid.pid ())
7d6eda
+    pid_was_stopped = 0;
7d6eda
+
7d6eda
+#endif
7d6eda
   if (resume_many)
7d6eda
     iterate_over_lwps (ptid, [=] (struct lwp_info *info)
7d6eda
 			     {
7d6eda
@@ -3770,6 +3812,10 @@ linux_nat_target::mourn_inferior ()
7d6eda
 
7d6eda
   /* Let the arch-specific native code know this process is gone.  */
7d6eda
   linux_target->low_forget_process (pid);
7d6eda
+#ifdef NEED_DETACH_SIGSTOP
7d6eda
+
7d6eda
+  pid_was_stopped = 0;
7d6eda
+#endif
7d6eda
 }
7d6eda
 
7d6eda
 /* Convert a native/host siginfo object, into/from the siginfo in the
7d6eda
diff --git a/gdb/testsuite/gdb.threads/attach-stopped.exp b/gdb/testsuite/gdb.threads/attach-stopped.exp
7d6eda
--- a/gdb/testsuite/gdb.threads/attach-stopped.exp
7d6eda
+++ b/gdb/testsuite/gdb.threads/attach-stopped.exp
7d6eda
@@ -56,7 +56,65 @@ proc corefunc { threadtype } {
7d6eda
     gdb_reinitialize_dir $srcdir/$subdir
7d6eda
     gdb_load ${binfile}
7d6eda
 
7d6eda
-    # Verify that we can attach to the stopped process.
7d6eda
+    # Verify that we can attach to the process by first giving its
7d6eda
+    # executable name via the file command, and using attach with the
7d6eda
+    # process ID.
7d6eda
+
7d6eda
+    set test "$threadtype: set file, before attach1 to stopped process"
7d6eda
+    gdb_test_multiple "file $binfile" "$test" {
7d6eda
+       -re "Load new symbol table from.*y or n. $" {
7d6eda
+	    gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*done." \
7d6eda
+		    "$test (re-read)"
7d6eda
+	}
7d6eda
+	-re "Reading symbols from $escapedbinfile\.\.\.*done.*$gdb_prompt $" {
7d6eda
+	    pass "$test"
7d6eda
+	}
7d6eda
+    }
7d6eda
+
7d6eda
+    set test "$threadtype: attach1 to stopped, after setting file"
7d6eda
+    gdb_test_multiple "attach $testpid" "$test" {
7d6eda
+	-re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*$gdb_prompt $" {
7d6eda
+	    pass "$test"
7d6eda
+	}
7d6eda
+    }
7d6eda
+
7d6eda
+    # ".*sleep.*clone.*" would fail on s390x as bt stops at START_THREAD there.
7d6eda
+    if {[string equal $threadtype threaded]} {
7d6eda
+	gdb_test "thread apply all bt" ".*sleep.*start_thread.*" "$threadtype: attach1 to stopped bt"
7d6eda
+    } else {
7d6eda
+	gdb_test "bt" ".*sleep.*main.*" "$threadtype: attach1 to stopped bt"
7d6eda
+    }
7d6eda
+
7d6eda
+    # Exit and detach the process.
7d6eda
+       
7d6eda
+    gdb_exit
7d6eda
+
7d6eda
+    # Avoid some race:
7d6eda
+    sleep 2
7d6eda
+
7d6eda
+    if [catch {open /proc/${testpid}/status r} fileid] {
7d6eda
+	set line2 "NOTFOUND"
7d6eda
+    } else {
7d6eda
+	gets $fileid line1;
7d6eda
+	gets $fileid line2;
7d6eda
+	close $fileid;
7d6eda
+    }
7d6eda
+
7d6eda
+    set test "$threadtype: attach1, exit leaves process stopped"
7d6eda
+    if {[string match "*(stopped)*" $line2]} {
7d6eda
+      pass $test
7d6eda
+    } else {
7d6eda
+      fail $test
7d6eda
+    }
7d6eda
+
7d6eda
+    # At this point, the process should still be stopped
7d6eda
+
7d6eda
+    gdb_start
7d6eda
+    gdb_reinitialize_dir $srcdir/$subdir
7d6eda
+    gdb_load ${binfile}
7d6eda
+
7d6eda
+    # Verify that we can attach to the process just by giving the
7d6eda
+    # process ID.
7d6eda
        
7d6eda
     set test "$threadtype: attach2 to stopped, after setting file"
7d6eda
     gdb_test_multiple "attach $testpid" "$test" {