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

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