a8767b3
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
f524ac5
From: Andrew Cagney <cagney@gnu.org>
f524ac5
Date: Fri, 27 Oct 2017 21:07:50 +0200
f524ac5
Subject: gdb-6.3-gstack-20050411.patch
f524ac5
f637971
;; Add a wrapper script to GDB that implements pstack using the
f637971
;; --readnever option.
f637971
;;=push
f637971
434306b
2004-11-23  Andrew Cagney  <cagney@redhat.com>
434306b
434306b
	* Makefile.in (uninstall-gstack, install-gstack): New rules, add
434306b
	to install and uninstall.
434306b
	* gstack.sh, gstack.1: New files.
434306b
f637971
diff --git a/gdb/Makefile.in b/gdb/Makefile.in
f637971
--- a/gdb/Makefile.in
f637971
+++ b/gdb/Makefile.in
ed8730b
@@ -1726,7 +1726,7 @@ info install-info clean-info dvi pdf install-pdf html install-html: force
Jan Kratochvil e00e5ea
 install: all
Jan Kratochvil e00e5ea
 	@$(MAKE) $(FLAGS_TO_PASS) install-only
e93d347
 
Jan Kratochvil e00e5ea
-install-only: $(CONFIG_INSTALL)
Jan Kratochvil e00e5ea
+install-only: install-gstack $(CONFIG_INSTALL)
434306b
 	transformed_name=`t='$(program_transform_name)'; \
434306b
 			  echo gdb | sed -e "$$t"` ; \
434306b
 		if test "x$$transformed_name" = x; then \
ed8730b
@@ -1775,7 +1775,25 @@ install-guile:
Jan Kratochvil 1bcf9b6
 install-python:
Jan Kratochvil 1bcf9b6
 	$(SHELL) $(srcdir)/../mkinstalldirs $(DESTDIR)$(GDB_DATADIR)/python/gdb
Jan Kratochvil 1bcf9b6
 
Jan Kratochvil 1bcf9b6
-uninstall: force $(CONFIG_UNINSTALL)
434306b
+GSTACK=gstack
434306b
+.PHONY: install-gstack
434306b
+install-gstack:
434306b
+	transformed_name=`t='$(program_transform_name)'; \
434306b
+			  echo $(GSTACK) | sed -e "$$t"` ; \
434306b
+		if test "x$$transformed_name" = x; then \
434306b
+		  transformed_name=$(GSTACK) ; \
434306b
+		else \
434306b
+		  true ; \
434306b
+		fi ; \
434306b
+		$(SHELL) $(srcdir)/../mkinstalldirs $(DESTDIR)$(bindir) ; \
434306b
+		$(INSTALL_PROGRAM) $(srcdir)/$(GSTACK).sh \
434306b
+			$(DESTDIR)$(bindir)/$$transformed_name$(EXEEXT) ; \
434306b
+		: $(SHELL) $(srcdir)/../mkinstalldirs \
434306b
+			$(DESTDIR)$(man1dir) ; \
434306b
+		: $(INSTALL_DATA) $(srcdir)/gstack.1 \
434306b
+			$(DESTDIR)$(man1dir)/$$transformed_name.1
Jan Kratochvil 1bcf9b6
+
434306b
+uninstall: force uninstall-gstack $(CONFIG_UNINSTALL)
434306b
 	transformed_name=`t='$(program_transform_name)'; \
434306b
 			  echo gdb | sed -e $$t` ; \
434306b
 		if test "x$$transformed_name" = x; then \
ed8730b
@@ -1798,6 +1816,18 @@ uninstall: force $(CONFIG_UNINSTALL)
Jan Kratochvil cf0b61a
 	fi
Jan Kratochvil 254f0e9
 	@$(MAKE) DO=uninstall "DODIRS=$(SUBDIRS)" $(FLAGS_TO_PASS) subdir_do
Jan Kratochvil 254f0e9
 
434306b
+.PHONY: uninstall-gstack
434306b
+uninstall-gstack:
434306b
+	transformed_name=`t='$(program_transform_name)'; \
434306b
+			  echo $(GSTACK) | sed -e $$t` ; \
434306b
+		if test "x$$transformed_name" = x; then \
434306b
+		  transformed_name=$(GSTACK) ; \
434306b
+		else \
434306b
+		  true ; \
434306b
+		fi ; \
434306b
+		rm -f $(DESTDIR)$(bindir)/$$transformed_name$(EXEEXT) \
434306b
+		      $(DESTDIR)$(man1dir)/$$transformed_name.1
c77f117
+
8b1b3fd
 # The C++ name parser can be built standalone for testing.
407ebe9
 test-cp-name-parser.o: cp-name-parser.c
Jan Kratochvil 254f0e9
 	$(COMPILE) -DTEST_CPNAMES cp-name-parser.c
f637971
diff --git a/gdb/gstack.sh b/gdb/gstack.sh
f637971
new file mode 100644
f637971
--- /dev/null
f637971
+++ b/gdb/gstack.sh
Jan Kratochvil 67644b5
@@ -0,0 +1,43 @@
434306b
+#!/bin/sh
434306b
+
434306b
+if test $# -ne 1; then
434306b
+    echo "Usage: `basename $0 .sh` <process-id>" 1>&2
434306b
+    exit 1
434306b
+fi
434306b
+
434306b
+if test ! -r /proc/$1; then
434306b
+    echo "Process $1 not found." 1>&2
434306b
+    exit 1
434306b
+fi
434306b
+
434306b
+# GDB doesn't allow "thread apply all bt" when the process isn't
434306b
+# threaded; need to peek at the process to determine if that or the
434306b
+# simpler "bt" should be used.
434306b
+
434306b
+backtrace="bt"
434306b
+if test -d /proc/$1/task ; then
434306b
+    # Newer kernel; has a task/ directory.
ee681d3
+    if test `/bin/ls /proc/$1/task | /usr/bin/wc -l` -gt 1 2>/dev/null ; then
434306b
+	backtrace="thread apply all bt"
434306b
+    fi
434306b
+elif test -f /proc/$1/maps ; then
434306b
+    # Older kernel; go by it loading libpthread.
ee681d3
+    if /bin/grep -e libpthread /proc/$1/maps > /dev/null 2>&1 ; then
434306b
+	backtrace="thread apply all bt"
434306b
+    fi
434306b
+fi
434306b
+
Jan Kratochvil 44ca5e4
+GDB=${GDB:-gdb}
434306b
+
434306b
+# Run GDB, strip out unwanted noise.
Jan Kratochvil 67644b5
+# --readnever is no longer used since .gdb_index is now in use.
c77f117
+$GDB --quiet -nx $GDBARGS /proc/$1/exe $1 <<EOF 2>&1 |
06e575d
+set width 0
06e575d
+set height 0
06e575d
+set pagination no
434306b
+$backtrace
434306b
+EOF
ee681d3
+/bin/sed -n \
ee681d3
+    -e 's/^\((gdb) \)*//' \
434306b
+    -e '/^#/p' \
434306b
+    -e '/^Thread/p'
f637971
diff --git a/gdb/testsuite/gdb.base/gstack.c b/gdb/testsuite/gdb.base/gstack.c
f637971
new file mode 100644
f637971
--- /dev/null
f637971
+++ b/gdb/testsuite/gdb.base/gstack.c
f637971
@@ -0,0 +1,43 @@
f637971
+/* This testcase is part of GDB, the GNU debugger.
f637971
+
f637971
+   Copyright 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
f637971
+
f637971
+   This program is free software; you can redistribute it and/or modify
f637971
+   it under the terms of the GNU General Public License as published by
f637971
+   the Free Software Foundation; either version 3 of the License, or
f637971
+   (at your option) any later version.
f637971
+
f637971
+   This program is distributed in the hope that it will be useful,
f637971
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
f637971
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f637971
+   GNU General Public License for more details.
f637971
+
f637971
+   You should have received a copy of the GNU General Public License
f637971
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
f637971
+
f637971
+#include <stdio.h>
f637971
+#include <unistd.h>
f637971
+#include <string.h>
f637971
+
f637971
+void
f637971
+func (void)
f637971
+{
f637971
+  const char msg[] = "looping\n";
f637971
+
f637971
+  /* Use the most simple notification not to get caught by attach on exiting
f637971
+     the function.  */
f637971
+  write (1, msg, strlen (msg));
c77f117
+
f637971
+  for (;;);
f637971
+}
f637971
+
f637971
+int
f637971
+main (void)
f637971
+{
f637971
+  alarm (60);
f637971
+  nice (100);
f637971
+
f637971
+  func ();
f637971
+
f637971
+  return 0;
f637971
+}
f637971
diff --git a/gdb/testsuite/gdb.base/gstack.exp b/gdb/testsuite/gdb.base/gstack.exp
f637971
new file mode 100644
f637971
--- /dev/null
f637971
+++ b/gdb/testsuite/gdb.base/gstack.exp
83f9b09
@@ -0,0 +1,84 @@
Jan Kratochvil 67644b5
+# Copyright (C) 2012 Free Software Foundation, Inc.
ee681d3
+
ee681d3
+# This program is free software; you can redistribute it and/or modify
ee681d3
+# it under the terms of the GNU General Public License as published by
ee681d3
+# the Free Software Foundation; either version 3 of the License, or
ee681d3
+# (at your option) any later version.
ee681d3
+#
ee681d3
+# This program is distributed in the hope that it will be useful,
ee681d3
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
ee681d3
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ee681d3
+# GNU General Public License for more details.
ee681d3
+#
ee681d3
+# You should have received a copy of the GNU General Public License
ee681d3
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
ee681d3
+
ee681d3
+set testfile gstack
ee681d3
+set executable ${testfile}
Jan Kratochvil 2c55a54
+set binfile [standard_output_file $executable]
ee681d3
+if {[build_executable ${testfile} ${executable} "" {debug}] == -1} {
ee681d3
+    return -1
ee681d3
+}
ee681d3
+
ee681d3
+set test "spawn inferior"
ee681d3
+set command "${binfile}"
ee681d3
+set res [remote_spawn host $command];
ee681d3
+if { $res < 0 || $res == "" } {
ee681d3
+    perror "Spawning $command failed."
ee681d3
+    fail $test
ee681d3
+    return
ee681d3
+}
83f9b09
+
83f9b09
+# The spawn id of the test inferior.
83f9b09
+set test_spawn_id $res
83f9b09
+
Jan Kratochvil 67644b5
+set use_gdb_stub 1
ee681d3
+set pid [exp_pid -i $res]
ee681d3
+gdb_expect {
ee681d3
+    -re "looping\r\n" {
ee681d3
+	pass $test
ee681d3
+    }
ee681d3
+    eof {
ee681d3
+	fail "$test (eof)"
ee681d3
+	return
ee681d3
+    }
ee681d3
+    timeout {
ee681d3
+	fail "$test (timeout)"
ee681d3
+	return
ee681d3
+    }
ee681d3
+}
ee681d3
+
ee681d3
+# Testcase uses the most simple notification not to get caught by attach on
ee681d3
+# exiting the function.  Still we could retry the gstack command if we fail.
ee681d3
+
ee681d3
+set test "spawn gstack"
Jan Kratochvil 89fbbfc
+set command "sh -c GDB=$GDB\\ GDBARGS=-data-directory\\\\\\ $BUILD_DATA_DIRECTORY\\ sh\\ ${srcdir}/../gstack.sh\\ $pid\\;echo\\ GSTACK-END"
ee681d3
+set res [remote_spawn host $command];
ee681d3
+if { $res < 0 || $res == "" } {
ee681d3
+    perror "Spawning $command failed."
ee681d3
+    fail $test
ee681d3
+}
83f9b09
+
83f9b09
+set gdb_spawn_id $res
83f9b09
+
Jan Kratochvil 67644b5
+gdb_test_multiple "" $test {
Jan Kratochvil 67644b5
+    -re "^#0 +(0x\[0-9a-f\]+ in )?\\.?func \\(\\) at \[^\r\n\]*\r\n#1 +0x\[0-9a-f\]+ in \\.?main \\(\\) at \[^\r\n\]*\r\nGSTACK-END\r\n\$" {
ee681d3
+	pass $test
ee681d3
+    }
ee681d3
+}
ee681d3
+
83f9b09
+gdb_test_multiple "" "gstack exits" {
83f9b09
+    eof {
83f9b09
+	set result [wait -i $gdb_spawn_id]
83f9b09
+	verbose $result
83f9b09
+
83f9b09
+	gdb_assert { [lindex $result 2] == 0 } "gstack exits with no error"
83f9b09
+	gdb_assert { [lindex $result 3] == 0 } "gstack's exit status is 0"
83f9b09
+
83f9b09
+	remote_close host
83f9b09
+	clear_gdb_spawn_id
83f9b09
+    }
83f9b09
+}
83f9b09
+
83f9b09
+# Kill the test inferior.
83f9b09
+kill_wait_spawned_process $test_spawn_id