36474ab
http://sourceware.org/gdb/wiki/ProjectArcher
36474ab
http://sourceware.org/gdb/wiki/ArcherBranchManagement
36474ab
36474ab
GIT snapshot:
Jan Kratochvil 213ecca
commit f0ee78c5ccefe388a64273353ecd5c99dae62558
36474ab
Jan Kratochvil 1054fa6
tromey/python
36474ab
36474ab
Jan Kratochvil 2f7f533
diff --git a/README.archer b/README.archer
Jan Kratochvil 2f7f533
new file mode 100644
Jan Kratochvil 2f7f533
index 0000000..173b8ea
Jan Kratochvil 2f7f533
--- /dev/null
Jan Kratochvil 2f7f533
+++ b/README.archer
Jan Kratochvil eb6cb2d
@@ -0,0 +1,2 @@
Jan Kratochvil 1054fa6
+This branch originally held the Python code for gdb.  It still exists
Jan Kratochvil 1054fa6
+because a small amount of code here has not yet been merged upstream.
Jan Kratochvil 2f7f533
diff --git a/gdb/Makefile.in b/gdb/Makefile.in
Jan Kratochvil 32f92b2
index 95104ef..a9b198a 100644
Jan Kratochvil 2f7f533
--- a/gdb/Makefile.in
Jan Kratochvil 2f7f533
+++ b/gdb/Makefile.in
Jan Kratochvil 32f92b2
@@ -1534,6 +1534,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
+
Jan Kratochvil eb6cb2d
 config.status: $(srcdir)/configure 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 32f92b2
index 30cfd17..e977b30 100644
Jan Kratochvil 2f7f533
--- a/gdb/data-directory/Makefile.in
Jan Kratochvil 2f7f533
+++ b/gdb/data-directory/Makefile.in
Jan Kratochvil 32f92b2
@@ -65,7 +65,10 @@ PYTHON_FILE_LIST = \
Jan Kratochvil 32f92b2
 	gdb/unwinder.py \
Jan Kratochvil 8a98352
 	gdb/prompt.py \
Jan Kratochvil eb6cb2d
 	gdb/xmethod.py \
Jan Kratochvil 2f7f533
+	gdb/types.py \
Jan Kratochvil 33ff709
 	gdb/command/__init__.py \
Jan Kratochvil 33ff709
+	gdb/command/ignore_errors.py \
Jan Kratochvil 33ff709
+	gdb/command/pahole.py \
Jan Kratochvil eb6cb2d
 	gdb/command/xmethods.py \
Jan Kratochvil 872aab0
 	gdb/command/frame_filters.py \
Jan Kratochvil 32f92b2
 	gdb/command/unwinders.py \
Jan Kratochvil 32f92b2
@@ -76,6 +79,8 @@ PYTHON_FILE_LIST = \
Jan Kratochvil 556378e
 	gdb/function/__init__.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 32f92b2
index 1665372..c4caffc 100644
Jan Kratochvil 2f7f533
--- a/gdb/doc/gdb.texinfo
Jan Kratochvil 2f7f533
+++ b/gdb/doc/gdb.texinfo
Jan Kratochvil eb6cb2d
@@ -1225,6 +1225,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 32f92b2
index 57ec22e..13beb05 100644
Jan Kratochvil 2f7f533
--- a/gdb/doc/python.texi
Jan Kratochvil 2f7f533
+++ b/gdb/doc/python.texi
Jan Kratochvil eb6cb2d
@@ -88,8 +88,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
index 05a38b2..9801fdf 100644
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 32f92b2
index aecd60a..d0f7834 100644
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"
Jan Kratochvil 32f92b2
@@ -453,6 +454,8 @@ captured_main (void *data)
Jan Kratochvil eb6cb2d
   char *cdarg = NULL;
Jan Kratochvil eb6cb2d
   char *ttyarg = NULL;
dd46ae6
 
Jan Kratochvil eb6cb2d
+  int python_script = 0;
ee681d3
+
Jan Kratochvil eb6cb2d
   /* These are static so that we can take their address in an
Jan Kratochvil eb6cb2d
      initializer.  */
Jan Kratochvil eb6cb2d
   static int print_help;
Jan Kratochvil 32f92b2
@@ -663,10 +666,14 @@ captured_main (void *data)
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
 
Jan Kratochvil 32f92b2
@@ -684,6 +691,9 @@ captured_main (void *data)
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;
Jan Kratochvil 32f92b2
@@ -864,7 +874,31 @@ captured_main (void *data)
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
Jan Kratochvil 32f92b2
@@ -1137,7 +1171,8 @@ captured_main (void *data)
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
     {
Jan Kratochvil 32f92b2
@@ -1148,13 +1183,25 @@ captured_main (void *data)
Jan Kratochvil eb6cb2d
   /* Show time and/or space usage.  */
Jan Kratochvil eb6cb2d
   do_cleanups (pre_stat_chain);
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)
Jan Kratochvil eb6cb2d
     {
Jan Kratochvil eb6cb2d
-      catch_errors (captured_command_loop, 0, "", RETURN_MASK_ALL);
Jan Kratochvil eb6cb2d
+      extern int pagination_enabled;
Jan Kratochvil eb6cb2d
+      pagination_enabled = 0;
Jan Kratochvil eb6cb2d
+      run_python_script (argc - optind, &argv[optind]);
Jan Kratochvil eb6cb2d
+      return 1;
Jan Kratochvil eb6cb2d
+    }
Jan Kratochvil eb6cb2d
+  else
Jan Kratochvil eb6cb2d
+#endif
Jan Kratochvil eb6cb2d
+    {
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
+	{
Jan Kratochvil eb6cb2d
+	  catch_errors (captured_command_loop, 0, "", RETURN_MASK_ALL);
Jan Kratochvil eb6cb2d
+	}
Jan Kratochvil eb6cb2d
     }
Jan Kratochvil eb6cb2d
   /* No exit -- exit is through quit_command.  */
Jan Kratochvil eb6cb2d
 }
Jan Kratochvil 32f92b2
@@ -1188,6 +1235,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 (_("\
Jan Kratochvil 32f92b2
@@ -1232,6 +1285,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
index 0000000..6fa48ff
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/command/pahole.py b/gdb/python/lib/gdb/command/pahole.py
Jan Kratochvil 2f7f533
new file mode 100644
Jan Kratochvil 213ecca
index 0000000..e08eaf5
Jan Kratochvil 2f7f533
--- /dev/null
Jan Kratochvil 2f7f533
+++ b/gdb/python/lib/gdb/command/pahole.py
Jan Kratochvil eb6cb2d
@@ -0,0 +1,81 @@
Jan Kratochvil eb6cb2d
+# pahole command for gdb
dd46ae6
+
Jan Kratochvil eb6cb2d
+# Copyright (C) 2008, 2009, 2012 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 Pahole (gdb.Command):
Jan Kratochvil eb6cb2d
+    """Show the holes in a structure.
Jan Kratochvil eb6cb2d
+This command takes a single argument, a type name.
Jan Kratochvil eb6cb2d
+It prints the type and displays comments showing where holes are."""
ee681d3
+
Jan Kratochvil eb6cb2d
+    def __init__ (self):
Jan Kratochvil eb6cb2d
+        super (Pahole, self).__init__ ("pahole", gdb.COMMAND_NONE,
Jan Kratochvil eb6cb2d
+                                       gdb.COMPLETE_SYMBOL)
6370c17
+
Jan Kratochvil eb6cb2d
+    def maybe_print_hole(self, bitpos, field_bitpos):
Jan Kratochvil eb6cb2d
+        if bitpos != field_bitpos:
Jan Kratochvil eb6cb2d
+            hole = field_bitpos - bitpos
Jan Kratochvil 61676c0
+            print ('  /* XXX %d bit hole, try to pack */' % hole)
6370c17
+
Jan Kratochvil eb6cb2d
+    def pahole (self, type, level, name):
Jan Kratochvil eb6cb2d
+        if name is None:
Jan Kratochvil eb6cb2d
+            name = ''
Jan Kratochvil eb6cb2d
+        tag = type.tag
Jan Kratochvil eb6cb2d
+        if tag is None:
Jan Kratochvil eb6cb2d
+            tag = ''
Jan Kratochvil 61676c0
+        print ('%sstruct %s {' % (' ' * (2 * level), tag))
Jan Kratochvil eb6cb2d
+        bitpos = 0
Jan Kratochvil eb6cb2d
+        for field in type.fields ():
Jan Kratochvil eb6cb2d
+            # Skip static fields.
Jan Kratochvil eb6cb2d
+            if not hasattr (field, ('bitpos')):
Jan Kratochvil eb6cb2d
+                continue
6370c17
+
Jan Kratochvil eb6cb2d
+            ftype = field.type.strip_typedefs()
6370c17
+
Jan Kratochvil eb6cb2d
+            self.maybe_print_hole(bitpos, field.bitpos)
Jan Kratochvil eb6cb2d
+            bitpos = field.bitpos
Jan Kratochvil eb6cb2d
+            if field.bitsize > 0:
Jan Kratochvil eb6cb2d
+                fieldsize = field.bitsize
Jan Kratochvil eb6cb2d
+            else:
Jan Kratochvil eb6cb2d
+                # TARGET_CHAR_BIT here...
Jan Kratochvil eb6cb2d
+                fieldsize = 8 * ftype.sizeof
6370c17
+
Jan Kratochvil eb6cb2d
+            # TARGET_CHAR_BIT
Jan Kratochvil 213ecca
+            print (' /* %3d %3d */' % (int (bitpos / 8), int (fieldsize / 8)), end = "")
Jan Kratochvil eb6cb2d
+            bitpos = bitpos + fieldsize
6370c17
+
Jan Kratochvil eb6cb2d
+            if ftype.code == gdb.TYPE_CODE_STRUCT:
Jan Kratochvil eb6cb2d
+                self.pahole (ftype, level + 1, field.name)
Jan Kratochvil eb6cb2d
+            else:
Jan Kratochvil 213ecca
+                print (' ' * (2 + 2 * level), end = "")
Jan Kratochvil 61676c0
+                print ('%s %s' % (str (ftype), field.name))
6370c17
+
Jan Kratochvil eb6cb2d
+        if level == 0:
Jan Kratochvil eb6cb2d
+            self.maybe_print_hole(bitpos, 8 * type.sizeof)
6370c17
+
Jan Kratochvil 213ecca
+        print (' ' * (14 + 2 * level), end = "")
Jan Kratochvil 61676c0
+        print ('} %s' % name)
Jan Kratochvil f8eee05
+
Jan Kratochvil eb6cb2d
+    def invoke (self, arg, from_tty):
Jan Kratochvil eb6cb2d
+        type = gdb.lookup_type (arg)
Jan Kratochvil eb6cb2d
+        type = type.strip_typedefs ()
Jan Kratochvil eb6cb2d
+        if type.code != gdb.TYPE_CODE_STRUCT:
Jan Kratochvil 61676c0
+            raise (TypeError, '%s is not a struct type' % arg)
Jan Kratochvil 213ecca
+        print (' ' * 14, end = "")
Jan Kratochvil eb6cb2d
+        self.pahole (type, 0, '')
Jan Kratochvil 6fa2f55
+
Jan Kratochvil eb6cb2d
+Pahole()
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 61676c0
index 0000000..8742680
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 32f92b2
index 4f88b0e..870a1ba 100644
Jan Kratochvil 2f7f533
--- a/gdb/python/python.c
Jan Kratochvil 2f7f533
+++ b/gdb/python/python.c
Jan Kratochvil 2f7f533
@@ -93,6 +93,8 @@ const struct extension_language_defn extension_language_python =
Jan Kratochvil eb6cb2d
 #include "linespec.h"
Jan Kratochvil eb6cb2d
 #include "source.h"
Jan Kratochvil eb6cb2d
 #include "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"
Jan Kratochvil 32f92b2
@@ -1262,6 +1264,83 @@ gdbpy_print_stack (void)
Jan Kratochvil eb6cb2d
 
Jan Kratochvil eb6cb2d
 /* Return the current Progspace.
Jan Kratochvil eb6cb2d
    There always is one.  */
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 eb6cb2d
+  if (ui_out_is_mi_like_p (current_uiout))
Jan Kratochvil eb6cb2d
+    return PyErr_Format (PyExc_RuntimeError, _("Cannot invoke CLI from MI."));
Jan Kratochvil 46a1caf
+
Jan Kratochvil eb6cb2d
+  in_cli = 1;
Jan Kratochvil eb6cb2d
+  current_interp_command_loop ();
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.  */
Jan Kratochvil eb6cb2d
+  ensure_python_env (get_current_arch (), current_language);
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 61676c0
+    wchar_t **wargv = 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 61676c0
+	size_t len2;
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 61676c0
+	wargv[i] = 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
+
Jan Kratochvil eb6cb2d
 
Jan Kratochvil eb6cb2d
 static PyObject *
Jan Kratochvil eb6cb2d
 gdbpy_get_current_progspace (PyObject *unused1, PyObject *unused2)
Jan Kratochvil 32f92b2
@@ -1982,6 +2061,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 32f92b2
index e6bfcca..688daa3 100644
Jan Kratochvil 2f7f533
--- a/gdb/python/python.h
Jan Kratochvil 2f7f533
+++ b/gdb/python/python.h
Jan Kratochvil eb6cb2d
@@ -25,4 +25,6 @@
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
 
Jan Kratochvil eb6cb2d
+extern void run_python_script (int argc, char **argv);
Jan Kratochvil 46a1caf
+
Jan Kratochvil eb6cb2d
 #endif /* GDB_PYTHON_H */
Jan Kratochvil 2f7f533
diff --git a/gdb/testsuite/gdb.gdb/selftest.exp b/gdb/testsuite/gdb.gdb/selftest.exp
Jan Kratochvil 32f92b2
index 9f25a48..88b0842 100644
Jan Kratochvil 2f7f533
--- a/gdb/testsuite/gdb.gdb/selftest.exp
Jan Kratochvil 2f7f533
+++ b/gdb/testsuite/gdb.gdb/selftest.exp
Jan Kratochvil eb6cb2d
@@ -92,6 +92,10 @@ proc do_steps_and_nexts {} {
Jan Kratochvil eb6cb2d
 		set description "step over cmdarg_vec initialization"
Jan Kratochvil eb6cb2d
 		set command "step"
Jan Kratochvil eb6cb2d
 	    }
Jan Kratochvil eb6cb2d
+	    -re ".*python_script = 0.*$gdb_prompt $" {
Jan Kratochvil eb6cb2d
+		set description "step over python_script initialization"
Jan Kratochvil eb6cb2d
+		set command "step"
Jan Kratochvil eb6cb2d
+	    }
Jan Kratochvil eb6cb2d
 	    -re ".*pre_stat_chain = make_command_stats_cleanup.*$gdb_prompt $" {
Jan Kratochvil eb6cb2d
 		set description "next over make_command_stats_cleanup and everything it calls"
Jan Kratochvil eb6cb2d
 		set command "next"
Jan Kratochvil 2f7f533
diff --git a/gdb/testsuite/gdb.python/py-frame.exp b/gdb/testsuite/gdb.python/py-frame.exp
Jan Kratochvil 32f92b2
index 33fdbe5..264d00f 100644
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 32f92b2
index baa17b7..45ad67e 100644
Jan Kratochvil 2f7f533
--- a/gdb/testsuite/gdb.python/py-value.exp
Jan Kratochvil 2f7f533
+++ b/gdb/testsuite/gdb.python/py-value.exp
Jan Kratochvil 2f7f533
@@ -388,6 +388,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.
Jan Kratochvil 2f7f533
@@ -517,6 +526,7 @@ test_value_in_inferior
Jan Kratochvil f8eee05
 test_inferior_function_call
Jan Kratochvil f8eee05
 test_lazy_strings
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