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