a8767b3
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
f637971
From: Fedora GDB patches <invalid@email.com>
f637971
Date: Fri, 27 Oct 2017 21:07:50 +0200
f637971
Subject: gdb-archer.patch
f637971
f637971
;; Python patches of: http://sourceware.org/gdb/wiki/ProjectArcher
f637971
;;=push
f637971
36474ab
http://sourceware.org/gdb/wiki/ProjectArcher
36474ab
http://sourceware.org/gdb/wiki/ArcherBranchManagement
36474ab
36474ab
GIT snapshot:
Jan Kratochvil 5bb0f3b
commit 718a1618b2f691a7f407213bb50f100ac59f91c3
36474ab
Jan Kratochvil 1054fa6
tromey/python
36474ab
Jan Kratochvil 2f7f533
diff --git a/gdb/Makefile.in b/gdb/Makefile.in
Jan Kratochvil 2f7f533
--- a/gdb/Makefile.in
Jan Kratochvil 2f7f533
+++ b/gdb/Makefile.in
e90857f
@@ -2084,6 +2084,12 @@ stamp-h: $(srcdir)/config.in config.status
Jan Kratochvil 33ff709
 	  CONFIG_LINKS= \
Jan Kratochvil 33ff709
 	  $(SHELL) config.status
Jan Kratochvil 33ff709
 
Jan Kratochvil 33ff709
+.gdbinit: $(srcdir)/gdbinit.in config.status
Jan Kratochvil 33ff709
+	CONFIG_FILES=".gdbinit:gdbinit.in" \
Jan Kratochvil 33ff709
+	  CONFIG_COMMANDS= \
Jan Kratochvil 33ff709
+	  CONFIG_HEADERS= \
Jan Kratochvil 33ff709
+	  $(SHELL) config.status
Jan Kratochvil 33ff709
+
c9daf93
 config.status: $(srcdir)/configure configure.nat configure.tgt configure.host ../bfd/development.sh
Jan Kratochvil 33ff709
 	$(SHELL) config.status --recheck
Jan Kratochvil 33ff709
 
Jan Kratochvil 2f7f533
diff --git a/gdb/data-directory/Makefile.in b/gdb/data-directory/Makefile.in
Jan Kratochvil 2f7f533
--- a/gdb/data-directory/Makefile.in
Jan Kratochvil 2f7f533
+++ b/gdb/data-directory/Makefile.in
bf87ff8
@@ -80,6 +80,7 @@ PYTHON_FILE_LIST = \
Jan Kratochvil 32f92b2
 	gdb/unwinder.py \
Jan Kratochvil eb6cb2d
 	gdb/xmethod.py \
Jan Kratochvil 33ff709
 	gdb/command/__init__.py \
Jan Kratochvil 33ff709
+	gdb/command/ignore_errors.py \
Jan Kratochvil af2c2a5
 	gdb/command/explore.py \
f637971
 	gdb/command/backtrace.py \
Jan Kratochvil 872aab0
 	gdb/command/frame_filters.py \
bf87ff8
@@ -92,6 +93,8 @@ PYTHON_FILE_LIST = \
Jan Kratochvil 7306e88
 	gdb/function/as_string.py \
Jan Kratochvil 2f7f533
 	gdb/function/caller_is.py \
Jan Kratochvil 8a98352
 	gdb/function/strfns.py \
Jan Kratochvil 33ff709
+	gdb/function/caller_is.py \
Jan Kratochvil 33ff709
+	gdb/function/in_scope.py \
Jan Kratochvil 2f7f533
 	gdb/printer/__init__.py \
Jan Kratochvil 2f7f533
 	gdb/printer/bound_registers.py
Jan Kratochvil 33ff709
 
Jan Kratochvil 2f7f533
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
Jan Kratochvil 2f7f533
--- a/gdb/doc/gdb.texinfo
Jan Kratochvil 2f7f533
+++ b/gdb/doc/gdb.texinfo
d827161
@@ -1251,6 +1251,16 @@ for remote debugging.
Jan Kratochvil 33ff709
 Run using @var{device} for your program's standard input and output.
Jan Kratochvil 33ff709
 @c FIXME: kingdon thinks there is more to -tty.  Investigate.
Jan Kratochvil 33ff709
 
Jan Kratochvil 33ff709
+@item -P
Jan Kratochvil 33ff709
+@cindex @code{-P}
Jan Kratochvil 33ff709
+@itemx --python
Jan Kratochvil 33ff709
+@cindex @code{--python}
Jan Kratochvil 33ff709
+Change interpretation of command line so that the argument immediately
Jan Kratochvil 33ff709
+following this switch is taken to be the name of a Python script file.
Jan Kratochvil 33ff709
+This option stops option processing; subsequent options are passed to
Jan Kratochvil 33ff709
+Python as @code{sys.argv}.  This option is only available if Python
Jan Kratochvil 33ff709
+scripting support was enabled when @value{GDBN} was configured.
Jan Kratochvil 33ff709
+
Jan Kratochvil 33ff709
 @c resolve the situation of these eventually
Jan Kratochvil 33ff709
 @item -tui
Jan Kratochvil 33ff709
 @cindex @code{--tui}
Jan Kratochvil 2f7f533
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
Jan Kratochvil 2f7f533
--- a/gdb/doc/python.texi
Jan Kratochvil 2f7f533
+++ b/gdb/doc/python.texi
0702d0d
@@ -90,8 +90,6 @@ containing @code{end}.  For example:
Jan Kratochvil 33ff709
 
Jan Kratochvil 33ff709
 @smallexample
Jan Kratochvil 33ff709
 (@value{GDBP}) python
Jan Kratochvil 33ff709
-Type python script
Jan Kratochvil 33ff709
-End with a line saying just "end".
Jan Kratochvil 33ff709
 >print 23
Jan Kratochvil 33ff709
 >end
Jan Kratochvil 33ff709
 23
Jan Kratochvil 2f7f533
diff --git a/gdb/gdb-gdb.gdb.in b/gdb/gdb-gdb.gdb.in
Jan Kratochvil 2f7f533
--- a/gdb/gdb-gdb.gdb.in
Jan Kratochvil 2f7f533
+++ b/gdb/gdb-gdb.gdb.in
Jan Kratochvil eb6cb2d
@@ -1,5 +1,15 @@
Jan Kratochvil eb6cb2d
 echo Setting up the environment for debugging gdb.\n
Jan Kratochvil eb6cb2d
 
Jan Kratochvil eb6cb2d
+# Set up the Python library and "require" command.
Jan Kratochvil eb6cb2d
+python
Jan Kratochvil eb6cb2d
+from os.path import abspath
Jan Kratochvil eb6cb2d
+gdb.datadir = abspath ('@srcdir@/python/lib')
Jan Kratochvil eb6cb2d
+gdb.pythonlibdir = gdb.datadir
Jan Kratochvil eb6cb2d
+gdb.__path__ = [gdb.datadir + '/gdb']
Jan Kratochvil eb6cb2d
+sys.path.insert(0, gdb.datadir)
Jan Kratochvil eb6cb2d
+end
Jan Kratochvil eb6cb2d
+source @srcdir@/python/lib/gdb/__init__.py
Jan Kratochvil 33ff709
+
Jan Kratochvil eb6cb2d
 if !$gdb_init_done
Jan Kratochvil eb6cb2d
   set variable $gdb_init_done = 1
Jan Kratochvil 33ff709
 
Jan Kratochvil 2f7f533
diff --git a/gdb/main.c b/gdb/main.c
Jan Kratochvil 2f7f533
--- a/gdb/main.c
Jan Kratochvil 2f7f533
+++ b/gdb/main.c
Jan Kratochvil 2f7f533
@@ -33,6 +33,7 @@
45f7971
 
Jan Kratochvil eb6cb2d
 #include "interps.h"
Jan Kratochvil eb6cb2d
 #include "main.h"
Jan Kratochvil eb6cb2d
+#include "python/python.h"
Jan Kratochvil eb6cb2d
 #include "source.h"
Jan Kratochvil eb6cb2d
 #include "cli/cli-cmds.h"
Jan Kratochvil eb6cb2d
 #include "objfiles.h"
e90857f
@@ -468,7 +469,7 @@ exec_or_core_file_attach (const char *filename, int from_tty)
f637971
 }
dd46ae6
 
Jan Kratochvil af2c2a5
 static void
Jan Kratochvil af2c2a5
-captured_main_1 (struct captured_main_args *context)
Jan Kratochvil af2c2a5
+captured_main_1 (struct captured_main_args *context, int &python_script)
Jan Kratochvil af2c2a5
 {
Jan Kratochvil af2c2a5
   int argc = context->argc;
Jan Kratochvil af2c2a5
   char **argv = context->argv;
cb6c609
@@ -686,10 +687,14 @@ captured_main_1 (struct captured_main_args *context)
Jan Kratochvil eb6cb2d
       {"args", no_argument, &set_args, 1},
Jan Kratochvil eb6cb2d
       {"l", required_argument, 0, 'l'},
Jan Kratochvil eb6cb2d
       {"return-child-result", no_argument, &return_child_result, 1},
Jan Kratochvil eb6cb2d
+#if HAVE_PYTHON
Jan Kratochvil eb6cb2d
+      {"python", no_argument, 0, 'P'},
Jan Kratochvil eb6cb2d
+      {"P", no_argument, 0, 'P'},
Jan Kratochvil eb6cb2d
+#endif
Jan Kratochvil eb6cb2d
       {0, no_argument, 0, 0}
Jan Kratochvil eb6cb2d
     };
Jan Kratochvil 3b55f78
 
Jan Kratochvil eb6cb2d
-    while (1)
Jan Kratochvil eb6cb2d
+    while (!python_script)
Jan Kratochvil eb6cb2d
       {
Jan Kratochvil eb6cb2d
 	int option_index;
79563d6
 
cb6c609
@@ -707,6 +712,9 @@ captured_main_1 (struct captured_main_args *context)
Jan Kratochvil eb6cb2d
 	  case 0:
Jan Kratochvil eb6cb2d
 	    /* Long option that just sets a flag.  */
Jan Kratochvil eb6cb2d
 	    break;
Jan Kratochvil eb6cb2d
+	  case 'P':
Jan Kratochvil eb6cb2d
+	    python_script = 1;
Jan Kratochvil eb6cb2d
+	    break;
Jan Kratochvil eb6cb2d
 	  case OPT_SE:
Jan Kratochvil eb6cb2d
 	    symarg = optarg;
Jan Kratochvil eb6cb2d
 	    execarg = optarg;
cb6c609
@@ -886,7 +894,31 @@ captured_main_1 (struct captured_main_args *context)
79563d6
 
Jan Kratochvil eb6cb2d
   /* Now that gdb_init has created the initial inferior, we're in
Jan Kratochvil eb6cb2d
      position to set args for that inferior.  */
Jan Kratochvil eb6cb2d
-  if (set_args)
Jan Kratochvil eb6cb2d
+  if (python_script)
Jan Kratochvil eb6cb2d
+    {
Jan Kratochvil eb6cb2d
+      /* The first argument is a python script to evaluate, and
Jan Kratochvil eb6cb2d
+	 subsequent arguments are passed to the script for
Jan Kratochvil eb6cb2d
+	 processing there.  */
Jan Kratochvil eb6cb2d
+      if (optind >= argc)
Jan Kratochvil eb6cb2d
+	{
Jan Kratochvil eb6cb2d
+	  fprintf_unfiltered (gdb_stderr,
Jan Kratochvil eb6cb2d
+			      _("%s: Python script file name required\n"),
Jan Kratochvil eb6cb2d
+			      argv[0]);
Jan Kratochvil eb6cb2d
+	  exit (1);
Jan Kratochvil eb6cb2d
+	}
Jan Kratochvil eb6cb2d
+
Jan Kratochvil eb6cb2d
+      /* FIXME: should handle inferior I/O intelligently here.
Jan Kratochvil eb6cb2d
+	 E.g., should be possible to run gdb in pipeline and have
Jan Kratochvil eb6cb2d
+	 Python (and gdb) output go to stderr or file; and if a
Jan Kratochvil eb6cb2d
+	 prompt is needed, open the tty.  */
Jan Kratochvil eb6cb2d
+      quiet = 1;
Jan Kratochvil eb6cb2d
+      /* FIXME: should read .gdbinit if, and only if, a prompt is
Jan Kratochvil eb6cb2d
+	 requested by the script.  Though... maybe this is not
Jan Kratochvil eb6cb2d
+	 ideal?  */
Jan Kratochvil eb6cb2d
+      /* FIXME: likewise, reading in history.  */
Jan Kratochvil eb6cb2d
+      inhibit_gdbinit = 1;
Jan Kratochvil eb6cb2d
+    }
Jan Kratochvil eb6cb2d
+  else if (set_args)
Jan Kratochvil eb6cb2d
     {
Jan Kratochvil eb6cb2d
       /* The remaining options are the command-line options for the
Jan Kratochvil eb6cb2d
 	 inferior.  The first one is the sym/exec file, and the rest
cb6c609
@@ -1187,7 +1219,8 @@ captured_main_1 (struct captured_main_args *context)
79563d6
 
Jan Kratochvil eb6cb2d
   /* Read in the old history after all the command files have been
Jan Kratochvil eb6cb2d
      read.  */
Jan Kratochvil eb6cb2d
-  init_history ();
Jan Kratochvil eb6cb2d
+  if (!python_script)
Jan Kratochvil eb6cb2d
+    init_history ();
79563d6
 
Jan Kratochvil eb6cb2d
   if (batch_flag)
Jan Kratochvil eb6cb2d
     {
cb6c609
@@ -1203,22 +1236,35 @@ static void
Jan Kratochvil af2c2a5
 captured_main (void *data)
Jan Kratochvil af2c2a5
 {
Jan Kratochvil af2c2a5
   struct captured_main_args *context = (struct captured_main_args *) data;
Jan Kratochvil af2c2a5
+  int python_script = 0;
Jan Kratochvil af2c2a5
 
Jan Kratochvil af2c2a5
-  captured_main_1 (context);
Jan Kratochvil af2c2a5
+  captured_main_1 (context, python_script);
dd46ae6
 
Jan Kratochvil eb6cb2d
-  /* NOTE: cagney/1999-11-07: There is probably no reason for not
Jan Kratochvil eb6cb2d
-     moving this loop and the code found in captured_command_loop()
Jan Kratochvil eb6cb2d
-     into the command_loop() proper.  The main thing holding back that
Jan Kratochvil eb6cb2d
-     change - SET_TOP_LEVEL() - has been eliminated.  */
Jan Kratochvil eb6cb2d
-  while (1)
Jan Kratochvil eb6cb2d
+#if HAVE_PYTHON
Jan Kratochvil eb6cb2d
+  if (python_script)
f637971
     {
e90857f
-      try
f637971
-	{
f637971
-	  captured_command_loop ();
f637971
-	}
e90857f
-      catch (const gdb_exception &ex)
Jan Kratochvil eb6cb2d
+      extern int pagination_enabled;
Jan Kratochvil eb6cb2d
+      pagination_enabled = 0;
Jan Kratochvil af2c2a5
+      run_python_script (context->argc - optind, &context->argv[optind]);
Jan Kratochvil af2c2a5
+      return;
Jan Kratochvil eb6cb2d
+    }
Jan Kratochvil eb6cb2d
+  else
Jan Kratochvil eb6cb2d
+#endif
f637971
+    {
Jan Kratochvil eb6cb2d
+      /* NOTE: cagney/1999-11-07: There is probably no reason for not
Jan Kratochvil eb6cb2d
+	 moving this loop and the code found in captured_command_loop()
Jan Kratochvil eb6cb2d
+	 into the command_loop() proper.  The main thing holding back that
Jan Kratochvil eb6cb2d
+	 change - SET_TOP_LEVEL() - has been eliminated. */
Jan Kratochvil eb6cb2d
+      while (1)
f637971
 	{
f637971
-	  exception_print (gdb_stderr, ex);
e90857f
+	  try
f637971
+	    {
f637971
+	      captured_command_loop ();
f637971
+	    }
e90857f
+	  catch (const gdb_exception &ex)
f637971
+	    {
f637971
+	      exception_print (gdb_stderr, ex);
f637971
+	    }
f637971
 	}
Jan Kratochvil eb6cb2d
     }
Jan Kratochvil eb6cb2d
   /* No exit -- exit is through quit_command.  */
cb6c609
@@ -1261,6 +1307,12 @@ print_gdb_help (struct ui_file *stream)
Jan Kratochvil eb6cb2d
   fputs_unfiltered (_("\
Jan Kratochvil eb6cb2d
 This is the GNU debugger.  Usage:\n\n\
Jan Kratochvil eb6cb2d
     gdb [options] [executable-file [core-file or process-id]]\n\
Jan Kratochvil eb6cb2d
+    gdb [options] --args executable-file [inferior-arguments ...]\n"), stream);
Jan Kratochvil eb6cb2d
+#if HAVE_PYTHON
Jan Kratochvil eb6cb2d
+  fputs_unfiltered (_("\
Jan Kratochvil eb6cb2d
+    gdb [options] [--python|-P] script-file [script-arguments ...]\n"), stream);
Jan Kratochvil eb6cb2d
+#endif
Jan Kratochvil eb6cb2d
+  fputs_unfiltered (_("\n\
Jan Kratochvil eb6cb2d
     gdb [options] --args executable-file [inferior-arguments ...]\n\n\
Jan Kratochvil eb6cb2d
 "), stream);
Jan Kratochvil eb6cb2d
   fputs_unfiltered (_("\
cb6c609
@@ -1306,6 +1358,13 @@ Output and user interface control:\n\n\
Jan Kratochvil 32f92b2
 #endif
Jan Kratochvil eb6cb2d
   fputs_unfiltered (_("\
Jan Kratochvil eb6cb2d
   --dbx              DBX compatibility mode.\n\
Jan Kratochvil eb6cb2d
+"), stream);
Jan Kratochvil eb6cb2d
+#if HAVE_PYTHON
Jan Kratochvil eb6cb2d
+  fputs_unfiltered (_("\
Jan Kratochvil eb6cb2d
+  --python, -P       Following argument is Python script file; remaining\n\
Jan Kratochvil eb6cb2d
+                     arguments are passed to script.\n"), stream);
Jan Kratochvil eb6cb2d
+#endif
Jan Kratochvil eb6cb2d
+  fputs_unfiltered (_("\
Jan Kratochvil eb6cb2d
   -q, --quiet, --silent\n\
Jan Kratochvil eb6cb2d
                      Do not print version number on startup.\n\n\
Jan Kratochvil eb6cb2d
 "), stream);
Jan Kratochvil 2f7f533
diff --git a/gdb/python/lib/gdb/command/ignore_errors.py b/gdb/python/lib/gdb/command/ignore_errors.py
Jan Kratochvil 2f7f533
new file mode 100644
Jan Kratochvil 2f7f533
--- /dev/null
Jan Kratochvil 2f7f533
+++ b/gdb/python/lib/gdb/command/ignore_errors.py
Jan Kratochvil eb6cb2d
@@ -0,0 +1,37 @@
Jan Kratochvil eb6cb2d
+# Ignore errors in user commands.
dd46ae6
+
Jan Kratochvil eb6cb2d
+# Copyright (C) 2008 Free Software Foundation, Inc.
dd46ae6
+
Jan Kratochvil eb6cb2d
+# This program is free software; you can redistribute it and/or modify
Jan Kratochvil eb6cb2d
+# it under the terms of the GNU General Public License as published by
Jan Kratochvil eb6cb2d
+# the Free Software Foundation; either version 3 of the License, or
Jan Kratochvil eb6cb2d
+# (at your option) any later version.
Jan Kratochvil eb6cb2d
+#
Jan Kratochvil eb6cb2d
+# This program is distributed in the hope that it will be useful,
Jan Kratochvil eb6cb2d
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
Jan Kratochvil eb6cb2d
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Jan Kratochvil eb6cb2d
+# GNU General Public License for more details.
Jan Kratochvil eb6cb2d
+#
Jan Kratochvil eb6cb2d
+# You should have received a copy of the GNU General Public License
Jan Kratochvil eb6cb2d
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
dd46ae6
+
Jan Kratochvil eb6cb2d
+import gdb
dd46ae6
+
Jan Kratochvil eb6cb2d
+class IgnoreErrorsCommand (gdb.Command):
Jan Kratochvil eb6cb2d
+    """Execute a single command, ignoring all errors.
Jan Kratochvil eb6cb2d
+Only one-line commands are supported.
Jan Kratochvil eb6cb2d
+This is primarily useful in scripts."""
ee681d3
+
Jan Kratochvil eb6cb2d
+    def __init__ (self):
Jan Kratochvil eb6cb2d
+        super (IgnoreErrorsCommand, self).__init__ ("ignore-errors",
Jan Kratochvil eb6cb2d
+                                                    gdb.COMMAND_OBSCURE,
Jan Kratochvil eb6cb2d
+                                                    # FIXME...
Jan Kratochvil eb6cb2d
+                                                    gdb.COMPLETE_COMMAND)
ee681d3
+
Jan Kratochvil eb6cb2d
+    def invoke (self, arg, from_tty):
Jan Kratochvil eb6cb2d
+        try:
Jan Kratochvil eb6cb2d
+            gdb.execute (arg, from_tty)
Jan Kratochvil eb6cb2d
+        except:
Jan Kratochvil eb6cb2d
+            pass
dd46ae6
+
Jan Kratochvil eb6cb2d
+IgnoreErrorsCommand ()
Jan Kratochvil 2f7f533
diff --git a/gdb/python/lib/gdb/function/in_scope.py b/gdb/python/lib/gdb/function/in_scope.py
Jan Kratochvil 2f7f533
new file mode 100644
Jan Kratochvil 2f7f533
--- /dev/null
Jan Kratochvil 2f7f533
+++ b/gdb/python/lib/gdb/function/in_scope.py
Jan Kratochvil eb6cb2d
@@ -0,0 +1,47 @@
Jan Kratochvil eb6cb2d
+# In-scope function.
Jan Kratochvil 46a1caf
+
Jan Kratochvil eb6cb2d
+# Copyright (C) 2008 Free Software Foundation, Inc.
Jan Kratochvil 46a1caf
+
Jan Kratochvil 46a1caf
+# This program is free software; you can redistribute it and/or modify
Jan Kratochvil 46a1caf
+# it under the terms of the GNU General Public License as published by
Jan Kratochvil 46a1caf
+# the Free Software Foundation; either version 3 of the License, or
Jan Kratochvil 46a1caf
+# (at your option) any later version.
Jan Kratochvil 46a1caf
+#
Jan Kratochvil 46a1caf
+# This program is distributed in the hope that it will be useful,
Jan Kratochvil 46a1caf
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
Jan Kratochvil 46a1caf
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Jan Kratochvil 46a1caf
+# GNU General Public License for more details.
Jan Kratochvil 46a1caf
+#
Jan Kratochvil 46a1caf
+# You should have received a copy of the GNU General Public License
Jan Kratochvil 46a1caf
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Jan Kratochvil 46a1caf
+
Jan Kratochvil eb6cb2d
+import gdb
Jan Kratochvil f8eee05
+
Jan Kratochvil eb6cb2d
+class InScope (gdb.Function):
Jan Kratochvil eb6cb2d
+    """Return True if all the given variables or macros are in scope.
Jan Kratochvil eb6cb2d
+Takes one argument for each variable name to be checked."""
Jan Kratochvil f8eee05
+
Jan Kratochvil eb6cb2d
+    def __init__ (self):
Jan Kratochvil 61676c0
+        super (InScope, self).__init__ ("in_scope")
Jan Kratochvil 46a1caf
+
Jan Kratochvil eb6cb2d
+    def invoke (self, *vars):
Jan Kratochvil eb6cb2d
+        if len (vars) == 0:
Jan Kratochvil 61676c0
+            raise (TypeError, "in_scope takes at least one argument")
Jan Kratochvil f8eee05
+
Jan Kratochvil eb6cb2d
+        # gdb.Value isn't hashable so it can't be put in a map.
Jan Kratochvil 61676c0
+        # Convert to string first.
Jan Kratochvil 61676c0
+        wanted = set (map (lambda x: x.string (), vars))
Jan Kratochvil 61676c0
+        found = set ()
Jan Kratochvil 61676c0
+        block = gdb.selected_frame ().block ()
Jan Kratochvil 61676c0
+        while block:
Jan Kratochvil 61676c0
+            for sym in block:
Jan Kratochvil 61676c0
+                if (sym.is_argument or sym.is_constant
Jan Kratochvil 61676c0
+                      or sym.is_function or sym.is_variable):
Jan Kratochvil 61676c0
+                    if sym.name in wanted:
Jan Kratochvil 61676c0
+                        found.add (sym.name)
Jan Kratochvil f8eee05
+
Jan Kratochvil 61676c0
+            block = block.superblock
Jan Kratochvil f8eee05
+
Jan Kratochvil 61676c0
+        return wanted == found
Jan Kratochvil f8eee05
+
Jan Kratochvil eb6cb2d
+InScope ()
Jan Kratochvil 2f7f533
diff --git a/gdb/python/python.c b/gdb/python/python.c
Jan Kratochvil 2f7f533
--- a/gdb/python/python.c
Jan Kratochvil 2f7f533
+++ b/gdb/python/python.c
0702d0d
@@ -94,6 +94,8 @@ const struct extension_language_defn extension_language_python =
Jan Kratochvil eb6cb2d
 #include "linespec.h"
Jan Kratochvil eb6cb2d
 #include "source.h"
b1bf7bc
 #include "common/version.h"
Jan Kratochvil eb6cb2d
+#include "inferior.h"
Jan Kratochvil eb6cb2d
+#include "gdbthread.h"
Jan Kratochvil eb6cb2d
 #include "target.h"
Jan Kratochvil eb6cb2d
 #include "gdbthread.h"
Jan Kratochvil eb6cb2d
 #include "interps.h"
e90857f
@@ -1276,6 +1278,94 @@ gdbpy_print_stack_or_quit ()
0702d0d
 
0702d0d
 
Jan Kratochvil eb6cb2d
 
Jan Kratochvil eb6cb2d
+/* True if 'gdb -P' was used, false otherwise.  */
Jan Kratochvil eb6cb2d
+static int running_python_script;
Jan Kratochvil f8eee05
+
Jan Kratochvil eb6cb2d
+/* True if we are currently in a call to 'gdb.cli', false otherwise.  */
Jan Kratochvil eb6cb2d
+static int in_cli;
Jan Kratochvil 46a1caf
+
Jan Kratochvil eb6cb2d
+/* Enter the command loop.  */
Jan Kratochvil 46a1caf
+
Jan Kratochvil eb6cb2d
+static PyObject *
Jan Kratochvil eb6cb2d
+gdbpy_cli (PyObject *unused1, PyObject *unused2)
Jan Kratochvil eb6cb2d
+{
Jan Kratochvil eb6cb2d
+  if (! running_python_script || in_cli)
Jan Kratochvil eb6cb2d
+    return PyErr_Format (PyExc_RuntimeError, "cannot invoke CLI recursively");
Jan Kratochvil eb6cb2d
+  
Jan Kratochvil af2c2a5
+  if (current_uiout->is_mi_like_p ())
Jan Kratochvil eb6cb2d
+    return PyErr_Format (PyExc_RuntimeError, _("Cannot invoke CLI from MI."));
Jan Kratochvil 46a1caf
+
Jan Kratochvil eb6cb2d
+  in_cli = 1;
Jan Kratochvil 7306e88
+  /* See captured_command_loop.  */
Jan Kratochvil 7306e88
+
Jan Kratochvil 7306e88
+  /* Give the interpreter a chance to print a prompt.  */
Jan Kratochvil 7306e88
+  interp_pre_command_loop (top_level_interpreter ());
Jan Kratochvil 7306e88
+
Jan Kratochvil 7306e88
+  /* Now it's time to start the event loop.  */
Jan Kratochvil 7306e88
+  start_event_loop ();
Jan Kratochvil 7306e88
+
Jan Kratochvil eb6cb2d
+  in_cli = 0;
Jan Kratochvil 46a1caf
+
Jan Kratochvil eb6cb2d
+  Py_RETURN_NONE;
Jan Kratochvil f8eee05
+}
Jan Kratochvil 46a1caf
+
Jan Kratochvil eb6cb2d
+/* Set up the Python argument vector and evaluate a script.  This is
Jan Kratochvil eb6cb2d
+   used to implement 'gdb -P'.  */
Jan Kratochvil 46a1caf
+
Jan Kratochvil eb6cb2d
+void
Jan Kratochvil eb6cb2d
+run_python_script (int argc, char **argv)
Jan Kratochvil f8eee05
+{
Jan Kratochvil eb6cb2d
+  FILE *input;
Jan Kratochvil 46a1caf
+
Jan Kratochvil eb6cb2d
+  /* We never free this, since we plan to exit at the end.  */
96770ac
+  gdbpy_enter *env = new gdbpy_enter (get_current_arch (), current_language);
96770ac
+  /* "Use" env, to suppress GCC warnings.  */
96770ac
+  (void) env;
Jan Kratochvil 46a1caf
+
Jan Kratochvil eb6cb2d
+  running_python_script = 1;
Jan Kratochvil 61676c0
+
Jan Kratochvil 61676c0
+#if PYTHON_ABI_VERSION < 3
Jan Kratochvil eb6cb2d
+  PySys_SetArgv (argc - 1, argv + 1);
Jan Kratochvil 61676c0
+#else
Jan Kratochvil 61676c0
+  {
Jan Kratochvil 7306e88
+    wchar_t **wargv = (wchar_t **) alloca (sizeof (*wargv) * (argc + 1));
Jan Kratochvil 61676c0
+    int i;
Jan Kratochvil 61676c0
+
Jan Kratochvil 61676c0
+    for (i = 1; i < argc; i++)
Jan Kratochvil 61676c0
+      {
Jan Kratochvil 61676c0
+	size_t len = mbstowcs (NULL, argv[i], 0);
Jan Kratochvil 9faadbe
+	/* Python-related GDB sources are built with -DNDEBUG
Jan Kratochvil 9faadbe
+	   https://sourceware.org/bugzilla/show_bug.cgi?id=20445 */
Jan Kratochvil 9faadbe
+	size_t len2 ATTRIBUTE_UNUSED;
Jan Kratochvil 61676c0
+
Jan Kratochvil 61676c0
+	if (len == (size_t) -1)
Jan Kratochvil 61676c0
+	  {
Jan Kratochvil 61676c0
+	    fprintf (stderr, "Invalid multibyte argument #%d \"%s\"\n",
Jan Kratochvil 61676c0
+		     i, argv[i]);
Jan Kratochvil 61676c0
+	    exit (1);
Jan Kratochvil 61676c0
+	  }
Jan Kratochvil 7306e88
+	wargv[i] = (wchar_t *) alloca (sizeof (**wargv) * (len + 1));
Jan Kratochvil 61676c0
+	len2 = mbstowcs (wargv[i], argv[i], len + 1);
Jan Kratochvil 61676c0
+	assert (len2 == len);
Jan Kratochvil 61676c0
+      }
Jan Kratochvil 61676c0
+    wargv[argc] = NULL;
Jan Kratochvil 61676c0
+    PySys_SetArgv (argc - 1, wargv + 1);
Jan Kratochvil 61676c0
+  }
Jan Kratochvil 61676c0
+#endif
Jan Kratochvil 61676c0
+
Jan Kratochvil eb6cb2d
+  input = fopen (argv[0], "r");
Jan Kratochvil eb6cb2d
+  if (! input)
Jan Kratochvil eb6cb2d
+    {
Jan Kratochvil eb6cb2d
+      fprintf (stderr, "could not open %s: %s\n", argv[0], strerror (errno));
Jan Kratochvil eb6cb2d
+      exit (1);
Jan Kratochvil eb6cb2d
+    }
Jan Kratochvil eb6cb2d
+  PyRun_SimpleFile (input, argv[0]);
Jan Kratochvil eb6cb2d
+  fclose (input);
Jan Kratochvil eb6cb2d
+  exit (0);
Jan Kratochvil 46a1caf
+}
Jan Kratochvil 46a1caf
+
Jan Kratochvil eb6cb2d
+
Jan Kratochvil 46a1caf
+
0702d0d
 /* Return a sequence holding all the Progspaces.  */
Jan Kratochvil eb6cb2d
 
Jan Kratochvil eb6cb2d
 static PyObject *
e90857f
@@ -1928,6 +2018,8 @@ PyMethodDef python_GdbMethods[] =
Jan Kratochvil eb6cb2d
 Evaluate command, a string, as a gdb CLI command.  Optionally returns\n\
Jan Kratochvil eb6cb2d
 a Python String containing the output of the command if to_string is\n\
Jan Kratochvil eb6cb2d
 set to True." },
Jan Kratochvil eb6cb2d
+  { "cli", gdbpy_cli, METH_NOARGS,
Jan Kratochvil eb6cb2d
+    "Enter the gdb CLI" },
Jan Kratochvil eb6cb2d
   { "parameter", gdbpy_parameter, METH_VARARGS,
Jan Kratochvil eb6cb2d
     "Return a gdb parameter's value" },
Jan Kratochvil eb6cb2d
 
Jan Kratochvil 2f7f533
diff --git a/gdb/python/python.h b/gdb/python/python.h
Jan Kratochvil 2f7f533
--- a/gdb/python/python.h
Jan Kratochvil 2f7f533
+++ b/gdb/python/python.h
a3b5cf4
@@ -25,7 +25,10 @@
Jan Kratochvil eb6cb2d
 /* This is all that python exports to gdb.  */
Jan Kratochvil eb6cb2d
 extern const struct extension_language_defn extension_language_python;
Jan Kratochvil eb6cb2d
 
a3b5cf4
+
a3b5cf4
 /* Command element for the 'python' command.  */
a3b5cf4
 extern cmd_list_element *python_cmd_element;
a3b5cf4
 
Jan Kratochvil eb6cb2d
+extern void run_python_script (int argc, char **argv);
Jan Kratochvil 46a1caf
+
b1bf7bc
 #endif /* PYTHON_PYTHON_H */
Jan Kratochvil 2f7f533
diff --git a/gdb/testsuite/gdb.python/py-frame.exp b/gdb/testsuite/gdb.python/py-frame.exp
Jan Kratochvil 2f7f533
--- a/gdb/testsuite/gdb.python/py-frame.exp
Jan Kratochvil 2f7f533
+++ b/gdb/testsuite/gdb.python/py-frame.exp
Jan Kratochvil 2f7f533
@@ -95,6 +95,8 @@ gdb_test "python print ('result = %s' % f0.read_var ('a'))" " = 1" "test Frame.r
Jan Kratochvil f8eee05
 
Jan Kratochvil 556378e
 gdb_test "python print ('result = %s' % (gdb.selected_frame () == f1))" " = True" "test gdb.selected_frame"
Jan Kratochvil 2f7f533
 
Jan Kratochvil 556378e
+gdb_test "python print ('result = %s' % (f0.block ()))" "<gdb.Block object at 0x\[\[:xdigit:\]\]+>" "test Frame.block"
Jan Kratochvil 2f7f533
+
Jan Kratochvil 2f7f533
 # Can read SP register.
Jan Kratochvil 2f7f533
 gdb_test "python print ('result = %s' % (gdb.selected_frame ().read_register ('sp') == gdb.parse_and_eval ('\$sp')))" \
Jan Kratochvil 2f7f533
   " = True" \
Jan Kratochvil 2f7f533
diff --git a/gdb/testsuite/gdb.python/py-value.exp b/gdb/testsuite/gdb.python/py-value.exp
Jan Kratochvil 2f7f533
--- a/gdb/testsuite/gdb.python/py-value.exp
Jan Kratochvil 2f7f533
+++ b/gdb/testsuite/gdb.python/py-value.exp
a17be6a
@@ -412,6 +412,15 @@ proc test_value_after_death {} {
Jan Kratochvil f8eee05
     "print value's type"
Jan Kratochvil f8eee05
 }
Jan Kratochvil f8eee05
 
Jan Kratochvil f8eee05
+# Regression test for a cast failure.  The bug was that if we cast a
Jan Kratochvil f8eee05
+# value to its own type, gdb could crash.  This happened because we
Jan Kratochvil f8eee05
+# could end up double-freeing a struct value.
Jan Kratochvil f8eee05
+proc test_cast_regression {} {
Jan Kratochvil f8eee05
+  gdb_test "python v = gdb.Value(5)" "" "create value for cast test"
Jan Kratochvil f8eee05
+  gdb_test "python v = v.cast(v.type)" "" "cast value for cast test"
Jan Kratochvil 61676c0
+  gdb_test "python print(v)" "5" "print value for cast test"
Jan Kratochvil f8eee05
+}
Jan Kratochvil 46a1caf
+
Jan Kratochvil f8eee05
 # Regression test for invalid subscript operations.  The bug was that
Jan Kratochvil f8eee05
 # the type of the value was not being checked before allowing a
Jan Kratochvil f8eee05
 # subscript operation to proceed.
a17be6a
@@ -598,6 +607,7 @@ test_value_in_inferior
29841d1
 test_value_from_buffer
Jan Kratochvil f8eee05
 test_inferior_function_call
Jan Kratochvil f8eee05
 test_value_after_death
Jan Kratochvil f8eee05
+test_cast_regression
Jan Kratochvil f8eee05
 
Jan Kratochvil f8eee05
 # Test either C or C++ values. 
Jan Kratochvil 2c55a54
 
Jan Kratochvil af2c2a5
diff --git a/gdb/varobj.c b/gdb/varobj.c
Jan Kratochvil af2c2a5
--- a/gdb/varobj.c
Jan Kratochvil af2c2a5
+++ b/gdb/varobj.c
96770ac
@@ -217,7 +217,6 @@ is_root_p (const struct varobj *var)
Jan Kratochvil af2c2a5
 }
Jan Kratochvil af2c2a5
 
Jan Kratochvil af2c2a5
 #ifdef HAVE_PYTHON
96770ac
-
Jan Kratochvil af2c2a5
 /* See python-internal.h.  */
Jan Kratochvil af2c2a5
 gdbpy_enter_varobj::gdbpy_enter_varobj (const struct varobj *var)
96770ac
 : gdbpy_enter (var->root->exp->gdbarch, var->root->exp->language_defn)