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