Blame SOURCES/gdb-archer.patch

190f2a
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
190f2a
From: Fedora GDB patches <invalid@email.com>
190f2a
Date: Fri, 27 Oct 2017 21:07:50 +0200
190f2a
Subject: gdb-archer.patch
190f2a
190f2a
;; Python patches of: http://sourceware.org/gdb/wiki/ProjectArcher
190f2a
;;=push
190f2a
190f2a
http://sourceware.org/gdb/wiki/ProjectArcher
190f2a
http://sourceware.org/gdb/wiki/ArcherBranchManagement
190f2a
190f2a
GIT snapshot:
190f2a
commit 718a1618b2f691a7f407213bb50f100ac59f91c3
190f2a
190f2a
tromey/python
190f2a
190f2a
diff --git a/gdb/Makefile.in b/gdb/Makefile.in
190f2a
--- a/gdb/Makefile.in
190f2a
+++ b/gdb/Makefile.in
190f2a
@@ -2112,6 +2112,12 @@ stamp-h: $(srcdir)/config.in config.status
190f2a
 	  CONFIG_LINKS= \
190f2a
 	  $(SHELL) config.status
190f2a
 
190f2a
+.gdbinit: $(srcdir)/gdbinit.in config.status
190f2a
+	CONFIG_FILES=".gdbinit:gdbinit.in" \
190f2a
+	  CONFIG_COMMANDS= \
190f2a
+	  CONFIG_HEADERS= \
190f2a
+	  $(SHELL) config.status
190f2a
+
190f2a
 config.status: $(srcdir)/configure configure.nat configure.tgt configure.host ../bfd/development.sh
190f2a
 	$(SHELL) config.status --recheck
190f2a
 
190f2a
diff --git a/gdb/data-directory/Makefile.in b/gdb/data-directory/Makefile.in
190f2a
--- a/gdb/data-directory/Makefile.in
190f2a
+++ b/gdb/data-directory/Makefile.in
190f2a
@@ -80,6 +80,7 @@ PYTHON_FILE_LIST = \
190f2a
 	gdb/unwinder.py \
190f2a
 	gdb/xmethod.py \
190f2a
 	gdb/command/__init__.py \
190f2a
+	gdb/command/ignore_errors.py \
190f2a
 	gdb/command/explore.py \
190f2a
 	gdb/command/backtrace.py \
190f2a
 	gdb/command/frame_filters.py \
190f2a
@@ -92,6 +93,8 @@ PYTHON_FILE_LIST = \
190f2a
 	gdb/function/as_string.py \
190f2a
 	gdb/function/caller_is.py \
190f2a
 	gdb/function/strfns.py \
190f2a
+	gdb/function/caller_is.py \
190f2a
+	gdb/function/in_scope.py \
190f2a
 	gdb/printer/__init__.py \
190f2a
 	gdb/printer/bound_registers.py
190f2a
 
190f2a
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
190f2a
--- a/gdb/doc/gdb.texinfo
190f2a
+++ b/gdb/doc/gdb.texinfo
190f2a
@@ -1245,6 +1245,16 @@ for remote debugging.
190f2a
 Run using @var{device} for your program's standard input and output.
190f2a
 @c FIXME: kingdon thinks there is more to -tty.  Investigate.
190f2a
 
190f2a
+@item -P
190f2a
+@cindex @code{-P}
190f2a
+@itemx --python
190f2a
+@cindex @code{--python}
190f2a
+Change interpretation of command line so that the argument immediately
190f2a
+following this switch is taken to be the name of a Python script file.
190f2a
+This option stops option processing; subsequent options are passed to
190f2a
+Python as @code{sys.argv}.  This option is only available if Python
190f2a
+scripting support was enabled when @value{GDBN} was configured.
190f2a
+
190f2a
 @c resolve the situation of these eventually
190f2a
 @item -tui
190f2a
 @cindex @code{--tui}
190f2a
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
190f2a
--- a/gdb/doc/python.texi
190f2a
+++ b/gdb/doc/python.texi
190f2a
@@ -88,8 +88,6 @@ containing @code{end}.  For example:
190f2a
 
190f2a
 @smallexample
190f2a
 (@value{GDBP}) python
190f2a
-Type python script
190f2a
-End with a line saying just "end".
190f2a
 >print 23
190f2a
 >end
190f2a
 23
190f2a
diff --git a/gdb/gdb-gdb.gdb.in b/gdb/gdb-gdb.gdb.in
190f2a
--- a/gdb/gdb-gdb.gdb.in
190f2a
+++ b/gdb/gdb-gdb.gdb.in
190f2a
@@ -1,5 +1,15 @@
190f2a
 echo Setting up the environment for debugging gdb.\n
190f2a
 
190f2a
+# Set up the Python library and "require" command.
190f2a
+python
190f2a
+from os.path import abspath
190f2a
+gdb.datadir = abspath ('@srcdir@/python/lib')
190f2a
+gdb.pythonlibdir = gdb.datadir
190f2a
+gdb.__path__ = [gdb.datadir + '/gdb']
190f2a
+sys.path.insert(0, gdb.datadir)
190f2a
+end
190f2a
+source @srcdir@/python/lib/gdb/__init__.py
190f2a
+
190f2a
 if !$gdb_init_done
190f2a
   set variable $gdb_init_done = 1
190f2a
 
190f2a
diff --git a/gdb/main.c b/gdb/main.c
190f2a
--- a/gdb/main.c
190f2a
+++ b/gdb/main.c
190f2a
@@ -33,6 +33,7 @@
190f2a
 
190f2a
 #include "interps.h"
190f2a
 #include "main.h"
190f2a
+#include "python/python.h"
190f2a
 #include "source.h"
190f2a
 #include "cli/cli-cmds.h"
190f2a
 #include "objfiles.h"
190f2a
@@ -479,7 +480,7 @@ exec_or_core_file_attach (const char *filename, int from_tty)
190f2a
 }
190f2a
 
190f2a
 static void
190f2a
-captured_main_1 (struct captured_main_args *context)
190f2a
+captured_main_1 (struct captured_main_args *context, int &python_script)
190f2a
 {
190f2a
   int argc = context->argc;
190f2a
   char **argv = context->argv;
190f2a
@@ -695,10 +696,14 @@ captured_main_1 (struct captured_main_args *context)
190f2a
       {"args", no_argument, &set_args, 1},
190f2a
       {"l", required_argument, 0, 'l'},
190f2a
       {"return-child-result", no_argument, &return_child_result, 1},
190f2a
+#if HAVE_PYTHON
190f2a
+      {"python", no_argument, 0, 'P'},
190f2a
+      {"P", no_argument, 0, 'P'},
190f2a
+#endif
190f2a
       {0, no_argument, 0, 0}
190f2a
     };
190f2a
 
190f2a
-    while (1)
190f2a
+    while (!python_script)
190f2a
       {
190f2a
 	int option_index;
190f2a
 
190f2a
@@ -716,6 +721,9 @@ captured_main_1 (struct captured_main_args *context)
190f2a
 	  case 0:
190f2a
 	    /* Long option that just sets a flag.  */
190f2a
 	    break;
190f2a
+	  case 'P':
190f2a
+	    python_script = 1;
190f2a
+	    break;
190f2a
 	  case OPT_SE:
190f2a
 	    symarg = optarg;
190f2a
 	    execarg = optarg;
190f2a
@@ -890,7 +898,31 @@ captured_main_1 (struct captured_main_args *context)
190f2a
 
190f2a
   /* Now that gdb_init has created the initial inferior, we're in
190f2a
      position to set args for that inferior.  */
190f2a
-  if (set_args)
190f2a
+  if (python_script)
190f2a
+    {
190f2a
+      /* The first argument is a python script to evaluate, and
190f2a
+	 subsequent arguments are passed to the script for
190f2a
+	 processing there.  */
190f2a
+      if (optind >= argc)
190f2a
+	{
190f2a
+	  fprintf_unfiltered (gdb_stderr,
190f2a
+			      _("%s: Python script file name required\n"),
190f2a
+			      argv[0]);
190f2a
+	  exit (1);
190f2a
+	}
190f2a
+
190f2a
+      /* FIXME: should handle inferior I/O intelligently here.
190f2a
+	 E.g., should be possible to run gdb in pipeline and have
190f2a
+	 Python (and gdb) output go to stderr or file; and if a
190f2a
+	 prompt is needed, open the tty.  */
190f2a
+      quiet = 1;
190f2a
+      /* FIXME: should read .gdbinit if, and only if, a prompt is
190f2a
+	 requested by the script.  Though... maybe this is not
190f2a
+	 ideal?  */
190f2a
+      /* FIXME: likewise, reading in history.  */
190f2a
+      inhibit_gdbinit = 1;
190f2a
+    }
190f2a
+  else if (set_args)
190f2a
     {
190f2a
       /* The remaining options are the command-line options for the
190f2a
 	 inferior.  The first one is the sym/exec file, and the rest
190f2a
@@ -1180,7 +1212,8 @@ captured_main_1 (struct captured_main_args *context)
190f2a
 
190f2a
   /* Read in the old history after all the command files have been
190f2a
      read.  */
190f2a
-  init_history ();
190f2a
+  if (!python_script)
190f2a
+    init_history ();
190f2a
 
190f2a
   if (batch_flag)
190f2a
     {
190f2a
@@ -1193,24 +1226,37 @@ static void
190f2a
 captured_main (void *data)
190f2a
 {
190f2a
   struct captured_main_args *context = (struct captured_main_args *) data;
190f2a
+  int python_script = 0;
190f2a
 
190f2a
-  captured_main_1 (context);
190f2a
+  captured_main_1 (context, python_script);
190f2a
 
190f2a
-  /* NOTE: cagney/1999-11-07: There is probably no reason for not
190f2a
-     moving this loop and the code found in captured_command_loop()
190f2a
-     into the command_loop() proper.  The main thing holding back that
190f2a
-     change - SET_TOP_LEVEL() - has been eliminated.  */
190f2a
-  while (1)
190f2a
+#if HAVE_PYTHON
190f2a
+  if (python_script)
190f2a
     {
190f2a
-      TRY
190f2a
-	{
190f2a
-	  captured_command_loop ();
190f2a
-	}
190f2a
-      CATCH (ex, RETURN_MASK_ALL)
190f2a
+      extern int pagination_enabled;
190f2a
+      pagination_enabled = 0;
190f2a
+      run_python_script (context->argc - optind, &context->argv[optind]);
190f2a
+      return;
190f2a
+    }
190f2a
+  else
190f2a
+#endif
190f2a
+    {
190f2a
+      /* NOTE: cagney/1999-11-07: There is probably no reason for not
190f2a
+	 moving this loop and the code found in captured_command_loop()
190f2a
+	 into the command_loop() proper.  The main thing holding back that
190f2a
+	 change - SET_TOP_LEVEL() - has been eliminated. */
190f2a
+      while (1)
190f2a
 	{
190f2a
-	  exception_print (gdb_stderr, ex);
190f2a
+	  TRY
190f2a
+	    {
190f2a
+	      captured_command_loop ();
190f2a
+	    }
190f2a
+	  CATCH (ex, RETURN_MASK_ALL)
190f2a
+	    {
190f2a
+	      exception_print (gdb_stderr, ex);
190f2a
+	    }
190f2a
+	  END_CATCH
190f2a
 	}
190f2a
-      END_CATCH
190f2a
     }
190f2a
   /* No exit -- exit is through quit_command.  */
190f2a
 }
190f2a
@@ -1253,6 +1299,12 @@ print_gdb_help (struct ui_file *stream)
190f2a
   fputs_unfiltered (_("\
190f2a
 This is the GNU debugger.  Usage:\n\n\
190f2a
     gdb [options] [executable-file [core-file or process-id]]\n\
190f2a
+    gdb [options] --args executable-file [inferior-arguments ...]\n"), stream);
190f2a
+#if HAVE_PYTHON
190f2a
+  fputs_unfiltered (_("\
190f2a
+    gdb [options] [--python|-P] script-file [script-arguments ...]\n"), stream);
190f2a
+#endif
190f2a
+  fputs_unfiltered (_("\n\
190f2a
     gdb [options] --args executable-file [inferior-arguments ...]\n\n\
190f2a
 "), stream);
190f2a
   fputs_unfiltered (_("\
190f2a
@@ -1298,6 +1350,13 @@ Output and user interface control:\n\n\
190f2a
 #endif
190f2a
   fputs_unfiltered (_("\
190f2a
   --dbx              DBX compatibility mode.\n\
190f2a
+"), stream);
190f2a
+#if HAVE_PYTHON
190f2a
+  fputs_unfiltered (_("\
190f2a
+  --python, -P       Following argument is Python script file; remaining\n\
190f2a
+                     arguments are passed to script.\n"), stream);
190f2a
+#endif
190f2a
+  fputs_unfiltered (_("\
190f2a
   -q, --quiet, --silent\n\
190f2a
                      Do not print version number on startup.\n\n\
190f2a
 "), stream);
190f2a
diff --git a/gdb/python/lib/gdb/command/ignore_errors.py b/gdb/python/lib/gdb/command/ignore_errors.py
190f2a
new file mode 100644
190f2a
--- /dev/null
190f2a
+++ b/gdb/python/lib/gdb/command/ignore_errors.py
190f2a
@@ -0,0 +1,37 @@
190f2a
+# Ignore errors in user commands.
190f2a
+
190f2a
+# Copyright (C) 2008 Free Software Foundation, Inc.
190f2a
+
190f2a
+# This program is free software; you can redistribute it and/or modify
190f2a
+# it under the terms of the GNU General Public License as published by
190f2a
+# the Free Software Foundation; either version 3 of the License, or
190f2a
+# (at your option) any later version.
190f2a
+#
190f2a
+# This program is distributed in the hope that it will be useful,
190f2a
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
190f2a
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
190f2a
+# GNU General Public License for more details.
190f2a
+#
190f2a
+# You should have received a copy of the GNU General Public License
190f2a
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
190f2a
+
190f2a
+import gdb
190f2a
+
190f2a
+class IgnoreErrorsCommand (gdb.Command):
190f2a
+    """Execute a single command, ignoring all errors.
190f2a
+Only one-line commands are supported.
190f2a
+This is primarily useful in scripts."""
190f2a
+
190f2a
+    def __init__ (self):
190f2a
+        super (IgnoreErrorsCommand, self).__init__ ("ignore-errors",
190f2a
+                                                    gdb.COMMAND_OBSCURE,
190f2a
+                                                    # FIXME...
190f2a
+                                                    gdb.COMPLETE_COMMAND)
190f2a
+
190f2a
+    def invoke (self, arg, from_tty):
190f2a
+        try:
190f2a
+            gdb.execute (arg, from_tty)
190f2a
+        except:
190f2a
+            pass
190f2a
+
190f2a
+IgnoreErrorsCommand ()
190f2a
diff --git a/gdb/python/lib/gdb/function/in_scope.py b/gdb/python/lib/gdb/function/in_scope.py
190f2a
new file mode 100644
190f2a
--- /dev/null
190f2a
+++ b/gdb/python/lib/gdb/function/in_scope.py
190f2a
@@ -0,0 +1,47 @@
190f2a
+# In-scope function.
190f2a
+
190f2a
+# Copyright (C) 2008 Free Software Foundation, Inc.
190f2a
+
190f2a
+# This program is free software; you can redistribute it and/or modify
190f2a
+# it under the terms of the GNU General Public License as published by
190f2a
+# the Free Software Foundation; either version 3 of the License, or
190f2a
+# (at your option) any later version.
190f2a
+#
190f2a
+# This program is distributed in the hope that it will be useful,
190f2a
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
190f2a
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
190f2a
+# GNU General Public License for more details.
190f2a
+#
190f2a
+# You should have received a copy of the GNU General Public License
190f2a
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
190f2a
+
190f2a
+import gdb
190f2a
+
190f2a
+class InScope (gdb.Function):
190f2a
+    """Return True if all the given variables or macros are in scope.
190f2a
+Takes one argument for each variable name to be checked."""
190f2a
+
190f2a
+    def __init__ (self):
190f2a
+        super (InScope, self).__init__ ("in_scope")
190f2a
+
190f2a
+    def invoke (self, *vars):
190f2a
+        if len (vars) == 0:
190f2a
+            raise (TypeError, "in_scope takes at least one argument")
190f2a
+
190f2a
+        # gdb.Value isn't hashable so it can't be put in a map.
190f2a
+        # Convert to string first.
190f2a
+        wanted = set (map (lambda x: x.string (), vars))
190f2a
+        found = set ()
190f2a
+        block = gdb.selected_frame ().block ()
190f2a
+        while block:
190f2a
+            for sym in block:
190f2a
+                if (sym.is_argument or sym.is_constant
190f2a
+                      or sym.is_function or sym.is_variable):
190f2a
+                    if sym.name in wanted:
190f2a
+                        found.add (sym.name)
190f2a
+
190f2a
+            block = block.superblock
190f2a
+
190f2a
+        return wanted == found
190f2a
+
190f2a
+InScope ()
190f2a
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
190f2a
--- a/gdb/python/python-internal.h
190f2a
+++ b/gdb/python/python-internal.h
190f2a
@@ -644,6 +644,9 @@ class gdbpy_enter_varobj : public gdbpy_enter
190f2a
 
190f2a
 };
190f2a
 
190f2a
+struct cleanup *ensure_python_env (struct gdbarch *gdbarch,
190f2a
+				   const struct language_defn *language);
190f2a
+
190f2a
 extern struct gdbarch *python_gdbarch;
190f2a
 extern const struct language_defn *python_language;
190f2a
 
190f2a
diff --git a/gdb/python/python.c b/gdb/python/python.c
190f2a
--- a/gdb/python/python.c
190f2a
+++ b/gdb/python/python.c
190f2a
@@ -95,6 +95,8 @@ const struct extension_language_defn extension_language_python =
190f2a
 #include "linespec.h"
190f2a
 #include "source.h"
190f2a
 #include "version.h"
190f2a
+#include "inferior.h"
190f2a
+#include "gdbthread.h"
190f2a
 #include "target.h"
190f2a
 #include "gdbthread.h"
190f2a
 #include "interps.h"
190f2a
@@ -237,6 +239,29 @@ gdbpy_enter::~gdbpy_enter ()
190f2a
   restore_active_ext_lang (m_previous_active);
190f2a
 }
190f2a
 
190f2a
+static void
190f2a
+restore_python_env (void *p)
190f2a
+{
190f2a
+  gdbpy_enter *env = (gdbpy_enter *) p;
190f2a
+
190f2a
+  delete env;
190f2a
+}
190f2a
+
190f2a
+/* Called before entering the Python interpreter to install the
190f2a
+   current language and architecture to be used for Python values.
190f2a
+   Also set the active extension language for GDB so that SIGINT's
190f2a
+   are directed our way, and if necessary install the right SIGINT
190f2a
+   handler.  */
190f2a
+
190f2a
+struct cleanup *
190f2a
+ensure_python_env (struct gdbarch *gdbarch,
190f2a
+                   const struct language_defn *language)
190f2a
+{
190f2a
+  gdbpy_enter *env = new gdbpy_enter (gdbarch, language);
190f2a
+
190f2a
+  return make_cleanup (restore_python_env, env);
190f2a
+}
190f2a
+
190f2a
 /* Set the quit flag.  */
190f2a
 
190f2a
 static void
190f2a
@@ -1367,6 +1392,92 @@ gdbpy_print_stack (void)
190f2a
 
190f2a
 /* Return the current Progspace.
190f2a
    There always is one.  */
190f2a
+/* True if 'gdb -P' was used, false otherwise.  */
190f2a
+static int running_python_script;
190f2a
+
190f2a
+/* True if we are currently in a call to 'gdb.cli', false otherwise.  */
190f2a
+static int in_cli;
190f2a
+
190f2a
+/* Enter the command loop.  */
190f2a
+
190f2a
+static PyObject *
190f2a
+gdbpy_cli (PyObject *unused1, PyObject *unused2)
190f2a
+{
190f2a
+  if (! running_python_script || in_cli)
190f2a
+    return PyErr_Format (PyExc_RuntimeError, "cannot invoke CLI recursively");
190f2a
+  
190f2a
+  if (current_uiout->is_mi_like_p ())
190f2a
+    return PyErr_Format (PyExc_RuntimeError, _("Cannot invoke CLI from MI."));
190f2a
+
190f2a
+  in_cli = 1;
190f2a
+  /* See captured_command_loop.  */
190f2a
+
190f2a
+  /* Give the interpreter a chance to print a prompt.  */
190f2a
+  interp_pre_command_loop (top_level_interpreter ());
190f2a
+
190f2a
+  /* Now it's time to start the event loop.  */
190f2a
+  start_event_loop ();
190f2a
+
190f2a
+  in_cli = 0;
190f2a
+
190f2a
+  Py_RETURN_NONE;
190f2a
+}
190f2a
+
190f2a
+/* Set up the Python argument vector and evaluate a script.  This is
190f2a
+   used to implement 'gdb -P'.  */
190f2a
+
190f2a
+void
190f2a
+run_python_script (int argc, char **argv)
190f2a
+{
190f2a
+  FILE *input;
190f2a
+
190f2a
+  /* We never free this, since we plan to exit at the end.  */
190f2a
+  ensure_python_env (get_current_arch (), current_language);
190f2a
+
190f2a
+  running_python_script = 1;
190f2a
+
190f2a
+#if PYTHON_ABI_VERSION < 3
190f2a
+  PySys_SetArgv (argc - 1, argv + 1);
190f2a
+#else
190f2a
+  {
190f2a
+    wchar_t **wargv = (wchar_t **) alloca (sizeof (*wargv) * (argc + 1));
190f2a
+    int i;
190f2a
+
190f2a
+    for (i = 1; i < argc; i++)
190f2a
+      {
190f2a
+	size_t len = mbstowcs (NULL, argv[i], 0);
190f2a
+	/* Python-related GDB sources are built with -DNDEBUG
190f2a
+	   https://sourceware.org/bugzilla/show_bug.cgi?id=20445 */
190f2a
+	size_t len2 ATTRIBUTE_UNUSED;
190f2a
+
190f2a
+	if (len == (size_t) -1)
190f2a
+	  {
190f2a
+	    fprintf (stderr, "Invalid multibyte argument #%d \"%s\"\n",
190f2a
+		     i, argv[i]);
190f2a
+	    exit (1);
190f2a
+	  }
190f2a
+	wargv[i] = (wchar_t *) alloca (sizeof (**wargv) * (len + 1));
190f2a
+	len2 = mbstowcs (wargv[i], argv[i], len + 1);
190f2a
+	assert (len2 == len);
190f2a
+      }
190f2a
+    wargv[argc] = NULL;
190f2a
+    PySys_SetArgv (argc - 1, wargv + 1);
190f2a
+  }
190f2a
+#endif
190f2a
+
190f2a
+  input = fopen (argv[0], "r");
190f2a
+  if (! input)
190f2a
+    {
190f2a
+      fprintf (stderr, "could not open %s: %s\n", argv[0], strerror (errno));
190f2a
+      exit (1);
190f2a
+    }
190f2a
+  PyRun_SimpleFile (input, argv[0]);
190f2a
+  fclose (input);
190f2a
+  exit (0);
190f2a
+}
190f2a
+
190f2a
+
190f2a
+
190f2a
 
190f2a
 static PyObject *
190f2a
 gdbpy_get_current_progspace (PyObject *unused1, PyObject *unused2)
190f2a
@@ -2057,6 +2168,8 @@ PyMethodDef python_GdbMethods[] =
190f2a
 Evaluate command, a string, as a gdb CLI command.  Optionally returns\n\
190f2a
 a Python String containing the output of the command if to_string is\n\
190f2a
 set to True." },
190f2a
+  { "cli", gdbpy_cli, METH_NOARGS,
190f2a
+    "Enter the gdb CLI" },
190f2a
   { "parameter", gdbpy_parameter, METH_VARARGS,
190f2a
     "Return a gdb parameter's value" },
190f2a
 
190f2a
diff --git a/gdb/python/python.h b/gdb/python/python.h
190f2a
--- a/gdb/python/python.h
190f2a
+++ b/gdb/python/python.h
190f2a
@@ -25,4 +25,6 @@
190f2a
 /* This is all that python exports to gdb.  */
190f2a
 extern const struct extension_language_defn extension_language_python;
190f2a
 
190f2a
+extern void run_python_script (int argc, char **argv);
190f2a
+
190f2a
 #endif /* GDB_PYTHON_H */
190f2a
diff --git a/gdb/testsuite/gdb.python/py-frame.exp b/gdb/testsuite/gdb.python/py-frame.exp
190f2a
--- a/gdb/testsuite/gdb.python/py-frame.exp
190f2a
+++ b/gdb/testsuite/gdb.python/py-frame.exp
190f2a
@@ -95,6 +95,8 @@ gdb_test "python print ('result = %s' % f0.read_var ('a'))" " = 1" "test Frame.r
190f2a
 
190f2a
 gdb_test "python print ('result = %s' % (gdb.selected_frame () == f1))" " = True" "test gdb.selected_frame"
190f2a
 
190f2a
+gdb_test "python print ('result = %s' % (f0.block ()))" "<gdb.Block object at 0x\[\[:xdigit:\]\]+>" "test Frame.block"
190f2a
+
190f2a
 # Can read SP register.
190f2a
 gdb_test "python print ('result = %s' % (gdb.selected_frame ().read_register ('sp') == gdb.parse_and_eval ('\$sp')))" \
190f2a
   " = True" \
190f2a
diff --git a/gdb/testsuite/gdb.python/py-value.exp b/gdb/testsuite/gdb.python/py-value.exp
190f2a
--- a/gdb/testsuite/gdb.python/py-value.exp
190f2a
+++ b/gdb/testsuite/gdb.python/py-value.exp
190f2a
@@ -384,6 +384,15 @@ proc test_value_after_death {} {
190f2a
     "print value's type"
190f2a
 }
190f2a
 
190f2a
+# Regression test for a cast failure.  The bug was that if we cast a
190f2a
+# value to its own type, gdb could crash.  This happened because we
190f2a
+# could end up double-freeing a struct value.
190f2a
+proc test_cast_regression {} {
190f2a
+  gdb_test "python v = gdb.Value(5)" "" "create value for cast test"
190f2a
+  gdb_test "python v = v.cast(v.type)" "" "cast value for cast test"
190f2a
+  gdb_test "python print(v)" "5" "print value for cast test"
190f2a
+}
190f2a
+
190f2a
 # Regression test for invalid subscript operations.  The bug was that
190f2a
 # the type of the value was not being checked before allowing a
190f2a
 # subscript operation to proceed.
190f2a
@@ -512,6 +521,7 @@ if ![runto_main] then {
190f2a
 test_value_in_inferior
190f2a
 test_inferior_function_call
190f2a
 test_value_after_death
190f2a
+test_cast_regression
190f2a
 
190f2a
 # Test either C or C++ values. 
190f2a
 
190f2a
diff --git a/gdb/varobj.c b/gdb/varobj.c
190f2a
--- a/gdb/varobj.c
190f2a
+++ b/gdb/varobj.c
190f2a
@@ -218,6 +218,14 @@ is_root_p (const struct varobj *var)
190f2a
 }
190f2a
 
190f2a
 #ifdef HAVE_PYTHON
190f2a
+/* Helper function to install a Python environment suitable for
190f2a
+   use during operations on VAR.  */
190f2a
+struct cleanup *
190f2a
+varobj_ensure_python_env (const struct varobj *var)
190f2a
+{
190f2a
+  return ensure_python_env (var->root->exp->gdbarch,
190f2a
+			    var->root->exp->language_defn);
190f2a
+}
190f2a
 
190f2a
 /* See python-internal.h.  */
190f2a
 gdbpy_enter_varobj::gdbpy_enter_varobj (const struct varobj *var)
190f2a
diff --git a/gdb/varobj.h b/gdb/varobj.h
190f2a
--- a/gdb/varobj.h
190f2a
+++ b/gdb/varobj.h
190f2a
@@ -328,6 +328,8 @@ extern bool varobj_has_more (const struct varobj *var, int to);
190f2a
 
190f2a
 extern bool varobj_is_dynamic_p (const struct varobj *var);
190f2a
 
190f2a
+extern struct cleanup *varobj_ensure_python_env (const struct varobj *var);
190f2a
+
190f2a
 extern bool varobj_default_value_is_changeable_p (const struct varobj *var);
190f2a
 extern bool varobj_value_is_changeable_p (const struct varobj *var);
190f2a