36474ab
http://sourceware.org/gdb/wiki/ProjectArcher
36474ab
http://sourceware.org/gdb/wiki/ArcherBranchManagement
36474ab
36474ab
GIT snapshot:
213ecca
commit f0ee78c5ccefe388a64273353ecd5c99dae62558
36474ab
1054fa6
tromey/python
36474ab
36474ab
2f7f533
diff --git a/README.archer b/README.archer
2f7f533
new file mode 100644
2f7f533
index 0000000..173b8ea
2f7f533
--- /dev/null
2f7f533
+++ b/README.archer
eb6cb2d
@@ -0,0 +1,2 @@
1054fa6
+This branch originally held the Python code for gdb.  It still exists
1054fa6
+because a small amount of code here has not yet been merged upstream.
2f7f533
diff --git a/gdb/Makefile.in b/gdb/Makefile.in
32f92b2
index 95104ef..a9b198a 100644
2f7f533
--- a/gdb/Makefile.in
2f7f533
+++ b/gdb/Makefile.in
32f92b2
@@ -1534,6 +1534,12 @@ stamp-h: $(srcdir)/config.in config.status
33ff709
 	  CONFIG_LINKS= \
33ff709
 	  $(SHELL) config.status
33ff709
 
33ff709
+.gdbinit: $(srcdir)/gdbinit.in config.status
33ff709
+	CONFIG_FILES=".gdbinit:gdbinit.in" \
33ff709
+	  CONFIG_COMMANDS= \
33ff709
+	  CONFIG_HEADERS= \
33ff709
+	  $(SHELL) config.status
33ff709
+
eb6cb2d
 config.status: $(srcdir)/configure configure.tgt configure.host ../bfd/development.sh
33ff709
 	$(SHELL) config.status --recheck
33ff709
 
2f7f533
diff --git a/gdb/data-directory/Makefile.in b/gdb/data-directory/Makefile.in
32f92b2
index 30cfd17..e977b30 100644
2f7f533
--- a/gdb/data-directory/Makefile.in
2f7f533
+++ b/gdb/data-directory/Makefile.in
32f92b2
@@ -65,7 +65,10 @@ PYTHON_FILE_LIST = \
32f92b2
 	gdb/unwinder.py \
8a98352
 	gdb/prompt.py \
eb6cb2d
 	gdb/xmethod.py \
2f7f533
+	gdb/types.py \
33ff709
 	gdb/command/__init__.py \
33ff709
+	gdb/command/ignore_errors.py \
33ff709
+	gdb/command/pahole.py \
eb6cb2d
 	gdb/command/xmethods.py \
872aab0
 	gdb/command/frame_filters.py \
32f92b2
 	gdb/command/unwinders.py \
32f92b2
@@ -76,6 +79,8 @@ PYTHON_FILE_LIST = \
556378e
 	gdb/function/__init__.py \
2f7f533
 	gdb/function/caller_is.py \
8a98352
 	gdb/function/strfns.py \
33ff709
+	gdb/function/caller_is.py \
33ff709
+	gdb/function/in_scope.py \
2f7f533
 	gdb/printer/__init__.py \
2f7f533
 	gdb/printer/bound_registers.py
33ff709
 
2f7f533
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
32f92b2
index 1665372..c4caffc 100644
2f7f533
--- a/gdb/doc/gdb.texinfo
2f7f533
+++ b/gdb/doc/gdb.texinfo
eb6cb2d
@@ -1225,6 +1225,16 @@ for remote debugging.
33ff709
 Run using @var{device} for your program's standard input and output.
33ff709
 @c FIXME: kingdon thinks there is more to -tty.  Investigate.
33ff709
 
33ff709
+@item -P
33ff709
+@cindex @code{-P}
33ff709
+@itemx --python
33ff709
+@cindex @code{--python}
33ff709
+Change interpretation of command line so that the argument immediately
33ff709
+following this switch is taken to be the name of a Python script file.
33ff709
+This option stops option processing; subsequent options are passed to
33ff709
+Python as @code{sys.argv}.  This option is only available if Python
33ff709
+scripting support was enabled when @value{GDBN} was configured.
33ff709
+
33ff709
 @c resolve the situation of these eventually
33ff709
 @item -tui
33ff709
 @cindex @code{--tui}
2f7f533
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
32f92b2
index 57ec22e..13beb05 100644
2f7f533
--- a/gdb/doc/python.texi
2f7f533
+++ b/gdb/doc/python.texi
eb6cb2d
@@ -88,8 +88,6 @@ containing @code{end}.  For example:
33ff709
 
33ff709
 @smallexample
33ff709
 (@value{GDBP}) python
33ff709
-Type python script
33ff709
-End with a line saying just "end".
33ff709
 >print 23
33ff709
 >end
33ff709
 23
2f7f533
diff --git a/gdb/gdb-gdb.gdb.in b/gdb/gdb-gdb.gdb.in
2f7f533
index 05a38b2..9801fdf 100644
2f7f533
--- a/gdb/gdb-gdb.gdb.in
2f7f533
+++ b/gdb/gdb-gdb.gdb.in
eb6cb2d
@@ -1,5 +1,15 @@
eb6cb2d
 echo Setting up the environment for debugging gdb.\n
eb6cb2d
 
eb6cb2d
+# Set up the Python library and "require" command.
eb6cb2d
+python
eb6cb2d
+from os.path import abspath
eb6cb2d
+gdb.datadir = abspath ('@srcdir@/python/lib')
eb6cb2d
+gdb.pythonlibdir = gdb.datadir
eb6cb2d
+gdb.__path__ = [gdb.datadir + '/gdb']
eb6cb2d
+sys.path.insert(0, gdb.datadir)
eb6cb2d
+end
eb6cb2d
+source @srcdir@/python/lib/gdb/__init__.py
33ff709
+
eb6cb2d
 if !$gdb_init_done
eb6cb2d
   set variable $gdb_init_done = 1
33ff709
 
2f7f533
diff --git a/gdb/main.c b/gdb/main.c
32f92b2
index aecd60a..d0f7834 100644
2f7f533
--- a/gdb/main.c
2f7f533
+++ b/gdb/main.c
2f7f533
@@ -33,6 +33,7 @@
45f7971
 
eb6cb2d
 #include "interps.h"
eb6cb2d
 #include "main.h"
eb6cb2d
+#include "python/python.h"
eb6cb2d
 #include "source.h"
eb6cb2d
 #include "cli/cli-cmds.h"
eb6cb2d
 #include "objfiles.h"
32f92b2
@@ -453,6 +454,8 @@ captured_main (void *data)
eb6cb2d
   char *cdarg = NULL;
eb6cb2d
   char *ttyarg = NULL;
dd46ae6
 
eb6cb2d
+  int python_script = 0;
ee681d3
+
eb6cb2d
   /* These are static so that we can take their address in an
eb6cb2d
      initializer.  */
eb6cb2d
   static int print_help;
32f92b2
@@ -663,10 +666,14 @@ captured_main (void *data)
eb6cb2d
       {"args", no_argument, &set_args, 1},
eb6cb2d
       {"l", required_argument, 0, 'l'},
eb6cb2d
       {"return-child-result", no_argument, &return_child_result, 1},
eb6cb2d
+#if HAVE_PYTHON
eb6cb2d
+      {"python", no_argument, 0, 'P'},
eb6cb2d
+      {"P", no_argument, 0, 'P'},
eb6cb2d
+#endif
eb6cb2d
       {0, no_argument, 0, 0}
eb6cb2d
     };
3b55f78
 
eb6cb2d
-    while (1)
eb6cb2d
+    while (!python_script)
eb6cb2d
       {
eb6cb2d
 	int option_index;
79563d6
 
32f92b2
@@ -684,6 +691,9 @@ captured_main (void *data)
eb6cb2d
 	  case 0:
eb6cb2d
 	    /* Long option that just sets a flag.  */
eb6cb2d
 	    break;
eb6cb2d
+	  case 'P':
eb6cb2d
+	    python_script = 1;
eb6cb2d
+	    break;
eb6cb2d
 	  case OPT_SE:
eb6cb2d
 	    symarg = optarg;
eb6cb2d
 	    execarg = optarg;
32f92b2
@@ -864,7 +874,31 @@ captured_main (void *data)
79563d6
 
eb6cb2d
   /* Now that gdb_init has created the initial inferior, we're in
eb6cb2d
      position to set args for that inferior.  */
eb6cb2d
-  if (set_args)
eb6cb2d
+  if (python_script)
eb6cb2d
+    {
eb6cb2d
+      /* The first argument is a python script to evaluate, and
eb6cb2d
+	 subsequent arguments are passed to the script for
eb6cb2d
+	 processing there.  */
eb6cb2d
+      if (optind >= argc)
eb6cb2d
+	{
eb6cb2d
+	  fprintf_unfiltered (gdb_stderr,
eb6cb2d
+			      _("%s: Python script file name required\n"),
eb6cb2d
+			      argv[0]);
eb6cb2d
+	  exit (1);
eb6cb2d
+	}
eb6cb2d
+
eb6cb2d
+      /* FIXME: should handle inferior I/O intelligently here.
eb6cb2d
+	 E.g., should be possible to run gdb in pipeline and have
eb6cb2d
+	 Python (and gdb) output go to stderr or file; and if a
eb6cb2d
+	 prompt is needed, open the tty.  */
eb6cb2d
+      quiet = 1;
eb6cb2d
+      /* FIXME: should read .gdbinit if, and only if, a prompt is
eb6cb2d
+	 requested by the script.  Though... maybe this is not
eb6cb2d
+	 ideal?  */
eb6cb2d
+      /* FIXME: likewise, reading in history.  */
eb6cb2d
+      inhibit_gdbinit = 1;
eb6cb2d
+    }
eb6cb2d
+  else if (set_args)
eb6cb2d
     {
eb6cb2d
       /* The remaining options are the command-line options for the
eb6cb2d
 	 inferior.  The first one is the sym/exec file, and the rest
32f92b2
@@ -1137,7 +1171,8 @@ captured_main (void *data)
79563d6
 
eb6cb2d
   /* Read in the old history after all the command files have been
eb6cb2d
      read.  */
eb6cb2d
-  init_history ();
eb6cb2d
+  if (!python_script)
eb6cb2d
+    init_history ();
79563d6
 
eb6cb2d
   if (batch_flag)
eb6cb2d
     {
32f92b2
@@ -1148,13 +1183,25 @@ captured_main (void *data)
eb6cb2d
   /* Show time and/or space usage.  */
eb6cb2d
   do_cleanups (pre_stat_chain);
dd46ae6
 
eb6cb2d
-  /* NOTE: cagney/1999-11-07: There is probably no reason for not
eb6cb2d
-     moving this loop and the code found in captured_command_loop()
eb6cb2d
-     into the command_loop() proper.  The main thing holding back that
eb6cb2d
-     change - SET_TOP_LEVEL() - has been eliminated.  */
eb6cb2d
-  while (1)
eb6cb2d
+#if HAVE_PYTHON
eb6cb2d
+  if (python_script)
eb6cb2d
     {
eb6cb2d
-      catch_errors (captured_command_loop, 0, "", RETURN_MASK_ALL);
eb6cb2d
+      extern int pagination_enabled;
eb6cb2d
+      pagination_enabled = 0;
eb6cb2d
+      run_python_script (argc - optind, &argv[optind]);
eb6cb2d
+      return 1;
eb6cb2d
+    }
eb6cb2d
+  else
eb6cb2d
+#endif
eb6cb2d
+    {
eb6cb2d
+      /* NOTE: cagney/1999-11-07: There is probably no reason for not
eb6cb2d
+	 moving this loop and the code found in captured_command_loop()
eb6cb2d
+	 into the command_loop() proper.  The main thing holding back that
eb6cb2d
+	 change - SET_TOP_LEVEL() - has been eliminated. */
eb6cb2d
+      while (1)
eb6cb2d
+	{
eb6cb2d
+	  catch_errors (captured_command_loop, 0, "", RETURN_MASK_ALL);
eb6cb2d
+	}
eb6cb2d
     }
eb6cb2d
   /* No exit -- exit is through quit_command.  */
eb6cb2d
 }
32f92b2
@@ -1188,6 +1235,12 @@ print_gdb_help (struct ui_file *stream)
eb6cb2d
   fputs_unfiltered (_("\
eb6cb2d
 This is the GNU debugger.  Usage:\n\n\
eb6cb2d
     gdb [options] [executable-file [core-file or process-id]]\n\
eb6cb2d
+    gdb [options] --args executable-file [inferior-arguments ...]\n"), stream);
eb6cb2d
+#if HAVE_PYTHON
eb6cb2d
+  fputs_unfiltered (_("\
eb6cb2d
+    gdb [options] [--python|-P] script-file [script-arguments ...]\n"), stream);
eb6cb2d
+#endif
eb6cb2d
+  fputs_unfiltered (_("\n\
eb6cb2d
     gdb [options] --args executable-file [inferior-arguments ...]\n\n\
eb6cb2d
 "), stream);
eb6cb2d
   fputs_unfiltered (_("\
32f92b2
@@ -1232,6 +1285,13 @@ Output and user interface control:\n\n\
32f92b2
 #endif
eb6cb2d
   fputs_unfiltered (_("\
eb6cb2d
   --dbx              DBX compatibility mode.\n\
eb6cb2d
+"), stream);
eb6cb2d
+#if HAVE_PYTHON
eb6cb2d
+  fputs_unfiltered (_("\
eb6cb2d
+  --python, -P       Following argument is Python script file; remaining\n\
eb6cb2d
+                     arguments are passed to script.\n"), stream);
eb6cb2d
+#endif
eb6cb2d
+  fputs_unfiltered (_("\
eb6cb2d
   -q, --quiet, --silent\n\
eb6cb2d
                      Do not print version number on startup.\n\n\
eb6cb2d
 "), stream);
2f7f533
diff --git a/gdb/python/lib/gdb/command/ignore_errors.py b/gdb/python/lib/gdb/command/ignore_errors.py
2f7f533
new file mode 100644
2f7f533
index 0000000..6fa48ff
2f7f533
--- /dev/null
2f7f533
+++ b/gdb/python/lib/gdb/command/ignore_errors.py
eb6cb2d
@@ -0,0 +1,37 @@
eb6cb2d
+# Ignore errors in user commands.
dd46ae6
+
eb6cb2d
+# Copyright (C) 2008 Free Software Foundation, Inc.
dd46ae6
+
eb6cb2d
+# This program is free software; you can redistribute it and/or modify
eb6cb2d
+# it under the terms of the GNU General Public License as published by
eb6cb2d
+# the Free Software Foundation; either version 3 of the License, or
eb6cb2d
+# (at your option) any later version.
eb6cb2d
+#
eb6cb2d
+# This program is distributed in the hope that it will be useful,
eb6cb2d
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
eb6cb2d
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
eb6cb2d
+# GNU General Public License for more details.
eb6cb2d
+#
eb6cb2d
+# You should have received a copy of the GNU General Public License
eb6cb2d
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
dd46ae6
+
eb6cb2d
+import gdb
dd46ae6
+
eb6cb2d
+class IgnoreErrorsCommand (gdb.Command):
eb6cb2d
+    """Execute a single command, ignoring all errors.
eb6cb2d
+Only one-line commands are supported.
eb6cb2d
+This is primarily useful in scripts."""
ee681d3
+
eb6cb2d
+    def __init__ (self):
eb6cb2d
+        super (IgnoreErrorsCommand, self).__init__ ("ignore-errors",
eb6cb2d
+                                                    gdb.COMMAND_OBSCURE,
eb6cb2d
+                                                    # FIXME...
eb6cb2d
+                                                    gdb.COMPLETE_COMMAND)
ee681d3
+
eb6cb2d
+    def invoke (self, arg, from_tty):
eb6cb2d
+        try:
eb6cb2d
+            gdb.execute (arg, from_tty)
eb6cb2d
+        except:
eb6cb2d
+            pass
dd46ae6
+
eb6cb2d
+IgnoreErrorsCommand ()
2f7f533
diff --git a/gdb/python/lib/gdb/command/pahole.py b/gdb/python/lib/gdb/command/pahole.py
2f7f533
new file mode 100644
213ecca
index 0000000..e08eaf5
2f7f533
--- /dev/null
2f7f533
+++ b/gdb/python/lib/gdb/command/pahole.py
eb6cb2d
@@ -0,0 +1,81 @@
eb6cb2d
+# pahole command for gdb
dd46ae6
+
eb6cb2d
+# Copyright (C) 2008, 2009, 2012 Free Software Foundation, Inc.
dd46ae6
+
eb6cb2d
+# This program is free software; you can redistribute it and/or modify
eb6cb2d
+# it under the terms of the GNU General Public License as published by
eb6cb2d
+# the Free Software Foundation; either version 3 of the License, or
eb6cb2d
+# (at your option) any later version.
eb6cb2d
+#
eb6cb2d
+# This program is distributed in the hope that it will be useful,
eb6cb2d
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
eb6cb2d
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
eb6cb2d
+# GNU General Public License for more details.
eb6cb2d
+#
eb6cb2d
+# You should have received a copy of the GNU General Public License
eb6cb2d
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
dd46ae6
+
eb6cb2d
+import gdb
dd46ae6
+
eb6cb2d
+class Pahole (gdb.Command):
eb6cb2d
+    """Show the holes in a structure.
eb6cb2d
+This command takes a single argument, a type name.
eb6cb2d
+It prints the type and displays comments showing where holes are."""
ee681d3
+
eb6cb2d
+    def __init__ (self):
eb6cb2d
+        super (Pahole, self).__init__ ("pahole", gdb.COMMAND_NONE,
eb6cb2d
+                                       gdb.COMPLETE_SYMBOL)
6370c17
+
eb6cb2d
+    def maybe_print_hole(self, bitpos, field_bitpos):
eb6cb2d
+        if bitpos != field_bitpos:
eb6cb2d
+            hole = field_bitpos - bitpos
61676c0
+            print ('  /* XXX %d bit hole, try to pack */' % hole)
6370c17
+
eb6cb2d
+    def pahole (self, type, level, name):
eb6cb2d
+        if name is None:
eb6cb2d
+            name = ''
eb6cb2d
+        tag = type.tag
eb6cb2d
+        if tag is None:
eb6cb2d
+            tag = ''
61676c0
+        print ('%sstruct %s {' % (' ' * (2 * level), tag))
eb6cb2d
+        bitpos = 0
eb6cb2d
+        for field in type.fields ():
eb6cb2d
+            # Skip static fields.
eb6cb2d
+            if not hasattr (field, ('bitpos')):
eb6cb2d
+                continue
6370c17
+
eb6cb2d
+            ftype = field.type.strip_typedefs()
6370c17
+
eb6cb2d
+            self.maybe_print_hole(bitpos, field.bitpos)
eb6cb2d
+            bitpos = field.bitpos
eb6cb2d
+            if field.bitsize > 0:
eb6cb2d
+                fieldsize = field.bitsize
eb6cb2d
+            else:
eb6cb2d
+                # TARGET_CHAR_BIT here...
eb6cb2d
+                fieldsize = 8 * ftype.sizeof
6370c17
+
eb6cb2d
+            # TARGET_CHAR_BIT
213ecca
+            print (' /* %3d %3d */' % (int (bitpos / 8), int (fieldsize / 8)), end = "")
eb6cb2d
+            bitpos = bitpos + fieldsize
6370c17
+
eb6cb2d
+            if ftype.code == gdb.TYPE_CODE_STRUCT:
eb6cb2d
+                self.pahole (ftype, level + 1, field.name)
eb6cb2d
+            else:
213ecca
+                print (' ' * (2 + 2 * level), end = "")
61676c0
+                print ('%s %s' % (str (ftype), field.name))
6370c17
+
eb6cb2d
+        if level == 0:
eb6cb2d
+            self.maybe_print_hole(bitpos, 8 * type.sizeof)
6370c17
+
213ecca
+        print (' ' * (14 + 2 * level), end = "")
61676c0
+        print ('} %s' % name)
f8eee05
+
eb6cb2d
+    def invoke (self, arg, from_tty):
eb6cb2d
+        type = gdb.lookup_type (arg)
eb6cb2d
+        type = type.strip_typedefs ()
eb6cb2d
+        if type.code != gdb.TYPE_CODE_STRUCT:
61676c0
+            raise (TypeError, '%s is not a struct type' % arg)
213ecca
+        print (' ' * 14, end = "")
eb6cb2d
+        self.pahole (type, 0, '')
6fa2f55
+
eb6cb2d
+Pahole()
2f7f533
diff --git a/gdb/python/lib/gdb/function/in_scope.py b/gdb/python/lib/gdb/function/in_scope.py
2f7f533
new file mode 100644
61676c0
index 0000000..8742680
2f7f533
--- /dev/null
2f7f533
+++ b/gdb/python/lib/gdb/function/in_scope.py
eb6cb2d
@@ -0,0 +1,47 @@
eb6cb2d
+# In-scope function.
46a1caf
+
eb6cb2d
+# Copyright (C) 2008 Free Software Foundation, Inc.
46a1caf
+
46a1caf
+# This program is free software; you can redistribute it and/or modify
46a1caf
+# it under the terms of the GNU General Public License as published by
46a1caf
+# the Free Software Foundation; either version 3 of the License, or
46a1caf
+# (at your option) any later version.
46a1caf
+#
46a1caf
+# This program is distributed in the hope that it will be useful,
46a1caf
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
46a1caf
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
46a1caf
+# GNU General Public License for more details.
46a1caf
+#
46a1caf
+# You should have received a copy of the GNU General Public License
46a1caf
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
46a1caf
+
eb6cb2d
+import gdb
f8eee05
+
eb6cb2d
+class InScope (gdb.Function):
eb6cb2d
+    """Return True if all the given variables or macros are in scope.
eb6cb2d
+Takes one argument for each variable name to be checked."""
f8eee05
+
eb6cb2d
+    def __init__ (self):
61676c0
+        super (InScope, self).__init__ ("in_scope")
46a1caf
+
eb6cb2d
+    def invoke (self, *vars):
eb6cb2d
+        if len (vars) == 0:
61676c0
+            raise (TypeError, "in_scope takes at least one argument")
f8eee05
+
eb6cb2d
+        # gdb.Value isn't hashable so it can't be put in a map.
61676c0
+        # Convert to string first.
61676c0
+        wanted = set (map (lambda x: x.string (), vars))
61676c0
+        found = set ()
61676c0
+        block = gdb.selected_frame ().block ()
61676c0
+        while block:
61676c0
+            for sym in block:
61676c0
+                if (sym.is_argument or sym.is_constant
61676c0
+                      or sym.is_function or sym.is_variable):
61676c0
+                    if sym.name in wanted:
61676c0
+                        found.add (sym.name)
f8eee05
+
61676c0
+            block = block.superblock
f8eee05
+
61676c0
+        return wanted == found
f8eee05
+
eb6cb2d
+InScope ()
2f7f533
diff --git a/gdb/python/python.c b/gdb/python/python.c
32f92b2
index 4f88b0e..870a1ba 100644
2f7f533
--- a/gdb/python/python.c
2f7f533
+++ b/gdb/python/python.c
2f7f533
@@ -93,6 +93,8 @@ const struct extension_language_defn extension_language_python =
eb6cb2d
 #include "linespec.h"
eb6cb2d
 #include "source.h"
eb6cb2d
 #include "version.h"
eb6cb2d
+#include "inferior.h"
eb6cb2d
+#include "gdbthread.h"
eb6cb2d
 #include "target.h"
eb6cb2d
 #include "gdbthread.h"
eb6cb2d
 #include "interps.h"
32f92b2
@@ -1262,6 +1264,83 @@ gdbpy_print_stack (void)
eb6cb2d
 
eb6cb2d
 /* Return the current Progspace.
eb6cb2d
    There always is one.  */
eb6cb2d
+/* True if 'gdb -P' was used, false otherwise.  */
eb6cb2d
+static int running_python_script;
f8eee05
+
eb6cb2d
+/* True if we are currently in a call to 'gdb.cli', false otherwise.  */
eb6cb2d
+static int in_cli;
46a1caf
+
eb6cb2d
+/* Enter the command loop.  */
46a1caf
+
eb6cb2d
+static PyObject *
eb6cb2d
+gdbpy_cli (PyObject *unused1, PyObject *unused2)
eb6cb2d
+{
eb6cb2d
+  if (! running_python_script || in_cli)
eb6cb2d
+    return PyErr_Format (PyExc_RuntimeError, "cannot invoke CLI recursively");
eb6cb2d
+  
eb6cb2d
+  if (ui_out_is_mi_like_p (current_uiout))
eb6cb2d
+    return PyErr_Format (PyExc_RuntimeError, _("Cannot invoke CLI from MI."));
46a1caf
+
eb6cb2d
+  in_cli = 1;
eb6cb2d
+  current_interp_command_loop ();
eb6cb2d
+  in_cli = 0;
46a1caf
+
eb6cb2d
+  Py_RETURN_NONE;
f8eee05
+}
46a1caf
+
eb6cb2d
+/* Set up the Python argument vector and evaluate a script.  This is
eb6cb2d
+   used to implement 'gdb -P'.  */
46a1caf
+
eb6cb2d
+void
eb6cb2d
+run_python_script (int argc, char **argv)
f8eee05
+{
eb6cb2d
+  FILE *input;
46a1caf
+
eb6cb2d
+  /* We never free this, since we plan to exit at the end.  */
eb6cb2d
+  ensure_python_env (get_current_arch (), current_language);
46a1caf
+
eb6cb2d
+  running_python_script = 1;
61676c0
+
61676c0
+#if PYTHON_ABI_VERSION < 3
eb6cb2d
+  PySys_SetArgv (argc - 1, argv + 1);
61676c0
+#else
61676c0
+  {
61676c0
+    wchar_t **wargv = alloca (sizeof (*wargv) * (argc + 1));
61676c0
+    int i;
61676c0
+
61676c0
+    for (i = 1; i < argc; i++)
61676c0
+      {
61676c0
+	size_t len = mbstowcs (NULL, argv[i], 0);
61676c0
+	size_t len2;
61676c0
+
61676c0
+	if (len == (size_t) -1)
61676c0
+	  {
61676c0
+	    fprintf (stderr, "Invalid multibyte argument #%d \"%s\"\n",
61676c0
+		     i, argv[i]);
61676c0
+	    exit (1);
61676c0
+	  }
61676c0
+	wargv[i] = alloca (sizeof (**wargv) * (len + 1));
61676c0
+	len2 = mbstowcs (wargv[i], argv[i], len + 1);
61676c0
+	assert (len2 == len);
61676c0
+      }
61676c0
+    wargv[argc] = NULL;
61676c0
+    PySys_SetArgv (argc - 1, wargv + 1);
61676c0
+  }
61676c0
+#endif
61676c0
+
eb6cb2d
+  input = fopen (argv[0], "r");
eb6cb2d
+  if (! input)
eb6cb2d
+    {
eb6cb2d
+      fprintf (stderr, "could not open %s: %s\n", argv[0], strerror (errno));
eb6cb2d
+      exit (1);
eb6cb2d
+    }
eb6cb2d
+  PyRun_SimpleFile (input, argv[0]);
eb6cb2d
+  fclose (input);
eb6cb2d
+  exit (0);
46a1caf
+}
46a1caf
+
eb6cb2d
+
46a1caf
+
eb6cb2d
 
eb6cb2d
 static PyObject *
eb6cb2d
 gdbpy_get_current_progspace (PyObject *unused1, PyObject *unused2)
32f92b2
@@ -1982,6 +2061,8 @@ PyMethodDef python_GdbMethods[] =
eb6cb2d
 Evaluate command, a string, as a gdb CLI command.  Optionally returns\n\
eb6cb2d
 a Python String containing the output of the command if to_string is\n\
eb6cb2d
 set to True." },
eb6cb2d
+  { "cli", gdbpy_cli, METH_NOARGS,
eb6cb2d
+    "Enter the gdb CLI" },
eb6cb2d
   { "parameter", gdbpy_parameter, METH_VARARGS,
eb6cb2d
     "Return a gdb parameter's value" },
eb6cb2d
 
2f7f533
diff --git a/gdb/python/python.h b/gdb/python/python.h
32f92b2
index e6bfcca..688daa3 100644
2f7f533
--- a/gdb/python/python.h
2f7f533
+++ b/gdb/python/python.h
eb6cb2d
@@ -25,4 +25,6 @@
eb6cb2d
 /* This is all that python exports to gdb.  */
eb6cb2d
 extern const struct extension_language_defn extension_language_python;
eb6cb2d
 
eb6cb2d
+extern void run_python_script (int argc, char **argv);
46a1caf
+
eb6cb2d
 #endif /* GDB_PYTHON_H */
2f7f533
diff --git a/gdb/testsuite/gdb.gdb/selftest.exp b/gdb/testsuite/gdb.gdb/selftest.exp
32f92b2
index 9f25a48..88b0842 100644
2f7f533
--- a/gdb/testsuite/gdb.gdb/selftest.exp
2f7f533
+++ b/gdb/testsuite/gdb.gdb/selftest.exp
eb6cb2d
@@ -92,6 +92,10 @@ proc do_steps_and_nexts {} {
eb6cb2d
 		set description "step over cmdarg_vec initialization"
eb6cb2d
 		set command "step"
eb6cb2d
 	    }
eb6cb2d
+	    -re ".*python_script = 0.*$gdb_prompt $" {
eb6cb2d
+		set description "step over python_script initialization"
eb6cb2d
+		set command "step"
eb6cb2d
+	    }
eb6cb2d
 	    -re ".*pre_stat_chain = make_command_stats_cleanup.*$gdb_prompt $" {
eb6cb2d
 		set description "next over make_command_stats_cleanup and everything it calls"
eb6cb2d
 		set command "next"
2f7f533
diff --git a/gdb/testsuite/gdb.python/py-frame.exp b/gdb/testsuite/gdb.python/py-frame.exp
32f92b2
index 33fdbe5..264d00f 100644
2f7f533
--- a/gdb/testsuite/gdb.python/py-frame.exp
2f7f533
+++ b/gdb/testsuite/gdb.python/py-frame.exp
2f7f533
@@ -95,6 +95,8 @@ gdb_test "python print ('result = %s' % f0.read_var ('a'))" " = 1" "test Frame.r
f8eee05
 
556378e
 gdb_test "python print ('result = %s' % (gdb.selected_frame () == f1))" " = True" "test gdb.selected_frame"
2f7f533
 
556378e
+gdb_test "python print ('result = %s' % (f0.block ()))" "<gdb.Block object at 0x\[\[:xdigit:\]\]+>" "test Frame.block"
2f7f533
+
2f7f533
 # Can read SP register.
2f7f533
 gdb_test "python print ('result = %s' % (gdb.selected_frame ().read_register ('sp') == gdb.parse_and_eval ('\$sp')))" \
2f7f533
   " = True" \
2f7f533
diff --git a/gdb/testsuite/gdb.python/py-value.exp b/gdb/testsuite/gdb.python/py-value.exp
32f92b2
index baa17b7..45ad67e 100644
2f7f533
--- a/gdb/testsuite/gdb.python/py-value.exp
2f7f533
+++ b/gdb/testsuite/gdb.python/py-value.exp
2f7f533
@@ -388,6 +388,15 @@ proc test_value_after_death {} {
f8eee05
     "print value's type"
f8eee05
 }
f8eee05
 
f8eee05
+# Regression test for a cast failure.  The bug was that if we cast a
f8eee05
+# value to its own type, gdb could crash.  This happened because we
f8eee05
+# could end up double-freeing a struct value.
f8eee05
+proc test_cast_regression {} {
f8eee05
+  gdb_test "python v = gdb.Value(5)" "" "create value for cast test"
f8eee05
+  gdb_test "python v = v.cast(v.type)" "" "cast value for cast test"
61676c0
+  gdb_test "python print(v)" "5" "print value for cast test"
f8eee05
+}
46a1caf
+
f8eee05
 # Regression test for invalid subscript operations.  The bug was that
f8eee05
 # the type of the value was not being checked before allowing a
f8eee05
 # subscript operation to proceed.
2f7f533
@@ -517,6 +526,7 @@ test_value_in_inferior
f8eee05
 test_inferior_function_call
f8eee05
 test_lazy_strings
f8eee05
 test_value_after_death
f8eee05
+test_cast_regression
f8eee05
 
f8eee05
 # Test either C or C++ values. 
2c55a54