From: Joel Brobecker <brobecker@adacore.com>
To: Tom Tromey <tromey@redhat.com>
Cc: Thiago Jung Bauermann <bauerman@br.ibm.com>,
Eli Zaretskii <eliz@gnu.org>,
drow@false.org, pedro@codesourcery.com,
gdb-patches@sourceware.org
Subject: Re: RFC: add ability to "source" Python code
Date: Mon, 18 Jan 2010 06:33:00 -0000 [thread overview]
Message-ID: <20100118063303.GI17397@adacore.com> (raw)
In-Reply-To: <m3ska7ckly.fsf@fleche.redhat.com>
[-- Attachment #1: Type: text/plain, Size: 109 bytes --]
Hello,
Attached is the patch that I ended up checking in. I'll propose a NEWS
entry momentarily.
--
Joel
[-- Attachment #2: source-python.diff --]
[-- Type: text/x-diff, Size: 12006 bytes --]
commit 6a67114416d55f906d0b5aa05e11c3cfaa5b7adf
Author: Joel Brobecker <brobecker@adacore.com>
Date: Mon Jan 18 09:54:28 2010 +0400
Allow "source" to load python scripts.
gdb/ChangeLog:
Tom Tromey <tromey@redhat.com>
Thiago Jung Bauermann <bauerman@br.ibm.com>
* exceptions.h (enum errors): Add UNSUPPORTED_ERROR.
* python/python.c (source_python_script): New function.
* python/python.h (source_python_script): Add declaration.
* cli/cli-cmds.c: #include exceptions.h and python/python.h.
(script_ext_off, script_ext_soft, script_ext_strict)
(script_ext_enums, script_ext_mode): New static constants.
(show_script_ext_mode, find_and_open_script): New functions.
(source_script): Enhance to handle Python scripts.
(init_cli_cmds): Add set/show script-extension commands.
gdb/doc/ChangeLog:
Tom Tromey <tromey@redhat.com>
* gdb.texinfo (File Options): Document -x on .py files.
(Command Files): Document handling of Python scripts.
gdb/testsuite/ChangeLog:
Tom Tromey <tromey@redhat.com>
Thiago Jung Bauermann <bauerman@br.ibm.com>
* gdb.python/source2.py: New file.
* gdb.python/source1: New file.
* gdb.python/python.exp: Test "source" command.
diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c
index 4833898..7400967 100644
--- a/gdb/cli/cli-cmds.c
+++ b/gdb/cli/cli-cmds.c
@@ -19,6 +19,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "defs.h"
+#include "exceptions.h"
#include "arch-utils.h"
#include "readline/readline.h"
#include "readline/tilde.h"
@@ -47,6 +48,8 @@ extern void disconnect_or_stop_tracing (int from_tty);
#include "cli/cli-setshow.h"
#include "cli/cli-cmds.h"
+#include "python/python.h"
+
#ifdef TUI
#include "tui/tui.h" /* For tui_active et.al. */
#endif
@@ -187,6 +190,21 @@ struct cmd_list_element *showchecklist;
int source_verbose = 0;
int trace_commands = 0;
\f
+/* 'script-extension' option support. */
+
+static const char script_ext_off[] = "off";
+static const char script_ext_soft[] = "soft";
+static const char script_ext_strict[] = "strict";
+
+static const char *script_ext_enums[] = {
+ script_ext_off,
+ script_ext_soft,
+ script_ext_strict,
+ NULL
+};
+
+static const char *script_ext_mode = script_ext_soft;
+\f
/* Utility used everywhere when at least one argument is needed and
none is supplied. */
@@ -441,18 +459,25 @@ cd_command (char *dir, int from_tty)
pwd_command ((char *) 0, 1);
}
\f
-void
-source_script (char *file, int from_tty)
+/* Show the current value of the 'script-extension' option. */
+
+static void
+show_script_ext_mode (struct ui_file *file, int from_tty,
+ struct cmd_list_element *c, const char *value)
{
- FILE *stream;
- struct cleanup *old_cleanups;
+ fprintf_filtered (file, _("\
+Script filename extension recognition is \"%s\".\n"),
+ value);
+}
+
+static int
+find_and_open_script (int from_tty, char **filep, FILE **streamp,
+ struct cleanup **cleanupp)
+{
+ char *file = *filep;
char *full_pathname = NULL;
int fd;
-
- if (file == NULL || *file == 0)
- {
- error (_("source command requires file name of file to source."));
- }
+ struct cleanup *old_cleanups;
file = tilde_expand (file);
old_cleanups = make_cleanup (xfree, file);
@@ -476,12 +501,58 @@ source_script (char *file, int from_tty)
else
{
do_cleanups (old_cleanups);
- return;
+ return 0;
}
}
- stream = fdopen (fd, FOPEN_RT);
- script_from_file (stream, file);
+ *streamp = fdopen (fd, FOPEN_RT);
+ *filep = file;
+ *cleanupp = old_cleanups;
+
+ return 1;
+}
+
+void
+source_script (char *file, int from_tty)
+{
+ FILE *stream;
+ struct cleanup *old_cleanups;
+
+ if (file == NULL || *file == 0)
+ {
+ error (_("source command requires file name of file to source."));
+ }
+
+ if (!find_and_open_script (from_tty, &file, &stream, &old_cleanups))
+ return;
+
+ if (script_ext_mode != script_ext_off
+ && strlen (file) > 3 && !strcmp (&file[strlen (file) - 3], ".py"))
+ {
+ volatile struct gdb_exception e;
+
+ TRY_CATCH (e, RETURN_MASK_ERROR)
+ {
+ source_python_script (stream, file);
+ }
+ if (e.reason < 0)
+ {
+ /* Should we fallback to ye olde GDB script mode? */
+ if (script_ext_mode == script_ext_soft
+ && e.reason == RETURN_ERROR && e.error == UNSUPPORTED_ERROR)
+ {
+ if (!find_and_open_script (from_tty, &file, &stream, &old_cleanups))
+ return;
+
+ script_from_file (stream, file);
+ }
+ else
+ /* Nope, just punt. */
+ throw_exception (e);
+ }
+ }
+ else
+ script_from_file (stream, file);
do_cleanups (old_cleanups);
}
@@ -1314,6 +1385,19 @@ when GDB is started."), gdbinit);
source_help_text, &cmdlist);
set_cmd_completer (c, filename_completer);
+ add_setshow_enum_cmd ("script-extension", class_support,
+ script_ext_enums, &script_ext_mode, _("\
+Set mode for script filename extension recognition."), _("\
+Show mode for script filename extension recognition."), _("\
+off == no filename extension recognition (all sourced files are GDB scripts)\n\
+soft == evaluate script according to filename extension, fallback to GDB script"
+ "\n\
+strict == evaluate script according to filename extension, error if not supported"
+ ),
+ NULL,
+ show_script_ext_mode,
+ &setlist, &showlist);
+
add_com ("quit", class_support, quit_command, _("Exit gdb."));
c = add_com ("help", class_support, help_command,
_("Print list of commands."));
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 7cf1bb4..d37712b 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -963,8 +963,11 @@ Connect to process ID @var{number}, as with the @code{attach} command.
@itemx -x @var{file}
@cindex @code{--command}
@cindex @code{-x}
-Execute @value{GDBN} commands from file @var{file}. @xref{Command
-Files,, Command files}.
+Execute commands from file @var{file}. If @var{file} ends in
+@samp{.py}, then the file is evaluated as Python code. If Python
+support is not enabled in this @value{GDBN}, then the file is assumed to
+contain @value{GDBN} commands, regardless of its extension.
+@xref{Command Files,, Command files}.
@item -eval-command @var{command}
@itemx -ex @var{command}
@@ -19159,6 +19162,11 @@ If @code{-v}, for verbose mode, is given then @value{GDBN} displays
each command as it is executed. The option must be given before
@var{filename}, and is interpreted as part of the filename anywhere else.
+If @var{filename} ends in @samp{.py}, then @value{GDBN} evaluates the
+contents of the file as Python code. If Python support is not compiled
+in to @value{GDBN}, then the file is assumed to contain @value{GDBN}
+commands, regardless of its extension.
+
Commands that would ask for confirmation if used interactively proceed
without asking when used in a command file. Many @value{GDBN} commands that
normally print messages to say what they are doing omit the messages
diff --git a/gdb/exceptions.h b/gdb/exceptions.h
index 84a9f01..6b3cbeb 100644
--- a/gdb/exceptions.h
+++ b/gdb/exceptions.h
@@ -75,6 +75,9 @@ enum errors {
/* Error accessing memory. */
MEMORY_ERROR,
+ /* Feature is not supported in this copy of GDB. */
+ UNSUPPORTED_ERROR,
+
/* Add more errors here. */
NR_ERRORS
};
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 827372c..1f1ae72 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -343,6 +343,22 @@ gdbpy_parse_and_eval (PyObject *self, PyObject *args)
return value_to_value_object (result);
}
+/* Read a file as Python code. STREAM is the input file; FILE is the
+ name of the file. */
+
+void
+source_python_script (FILE *stream, char *file)
+{
+ PyGILState_STATE state;
+
+ state = PyGILState_Ensure ();
+
+ PyRun_SimpleFile (stream, file);
+
+ fclose (stream);
+ PyGILState_Release (state);
+}
+
\f
/* Printing. */
@@ -525,6 +541,14 @@ eval_python_from_control_command (struct command_line *cmd)
error (_("Python scripting is not supported in this copy of GDB."));
}
+void
+source_python_script (FILE *stream, char *file)
+{
+ fclose (stream);
+ throw_error (UNSUPPORTED_ERROR,
+ _("Python scripting is not supported in this copy of GDB."));
+}
+
#endif /* HAVE_PYTHON */
\f
diff --git a/gdb/python/python.h b/gdb/python/python.h
index f35827b..5d93f67 100644
--- a/gdb/python/python.h
+++ b/gdb/python/python.h
@@ -24,6 +24,8 @@
void eval_python_from_control_command (struct command_line *);
+void source_python_script (FILE *stream, char *file);
+
int apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr,
int embedded_offset, CORE_ADDR address,
struct ui_file *stream, int recurse,
diff --git a/gdb/testsuite/gdb.python/python.exp b/gdb/testsuite/gdb.python/python.exp
index 951c295..b345ad2 100644
--- a/gdb/testsuite/gdb.python/python.exp
+++ b/gdb/testsuite/gdb.python/python.exp
@@ -29,6 +29,10 @@ gdb_reinitialize_dir $srcdir/$subdir
gdb_test_multiple "python print 23" "verify python support" {
-re "not supported.*$gdb_prompt $" {
unsupported "python support is disabled"
+
+ # If Python is not supported, verify that sourcing a python script
+ # causes an error.
+ gdb_test "source $srcdir/$subdir/source2.py" "Error in sourced command file:.*"
return -1
}
-re "$gdb_prompt $" {}
@@ -72,5 +76,7 @@ gdb_py_test_multiple "indented multi-line python command" \
"foo ()" "" \
"end" "hello, world!"
+gdb_test "source $srcdir/$subdir/source2.py" "yes"
+
gdb_test "python print gdb.current_objfile()" "None"
gdb_test "python print gdb.objfiles()" "\\\[\\\]"
diff --git a/gdb/testsuite/gdb.python/source1 b/gdb/testsuite/gdb.python/source1
new file mode 100644
index 0000000..f9c19bd
--- /dev/null
+++ b/gdb/testsuite/gdb.python/source1
@@ -0,0 +1,19 @@
+# This testcase is part of GDB, the GNU debugger.
+
+# Copyright 2008, 2009, 2010 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# This is sourced as python; pick an expression that is not valid for gdb.
+print 'y%ss' % 'e'
diff --git a/gdb/testsuite/gdb.python/source2.py b/gdb/testsuite/gdb.python/source2.py
new file mode 100644
index 0000000..4e9a9e3
--- /dev/null
+++ b/gdb/testsuite/gdb.python/source2.py
@@ -0,0 +1,18 @@
+# This testcase is part of GDB, the GNU debugger.
+
+# Copyright 2008, 2009, 2010 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+print 'y%ss' % 'e'
next prev parent reply other threads:[~2010-01-18 6:33 UTC|newest]
Thread overview: 58+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-02-08 1:16 Tom Tromey
2009-02-08 1:34 ` Tom Tromey
2009-02-08 4:10 ` Eli Zaretskii
2009-02-08 4:08 ` Eli Zaretskii
2009-02-09 1:53 ` Doug Evans
2009-02-09 4:09 ` Eli Zaretskii
2009-02-10 1:37 ` Tom Tromey
2009-02-10 1:30 ` Tom Tromey
2009-02-09 1:35 ` Doug Evans
2009-02-10 0:00 ` Pedro Alves
2009-02-10 1:29 ` Tom Tromey
2009-02-10 2:36 ` Pedro Alves
2009-02-10 3:48 ` Daniel Jacobowitz
2009-02-10 9:34 ` Eli Zaretskii
2009-02-10 11:58 ` Thiago Jung Bauermann
2009-02-10 17:04 ` Tom Tromey
2009-02-11 2:25 ` Paul Pluzhnikov
2009-02-11 6:09 ` Joel Brobecker
2009-02-11 19:51 ` Tom Tromey
2009-02-11 20:21 ` Eli Zaretskii
2009-02-11 20:39 ` Joel Brobecker
2009-02-11 21:06 ` Eli Zaretskii
2009-02-11 21:26 ` Matt Rice
2009-02-11 21:49 ` Eli Zaretskii
2009-02-11 21:55 ` Eli Zaretskii
2009-02-11 22:01 ` Joel Brobecker
2009-02-12 3:59 ` Eli Zaretskii
2009-02-12 6:27 ` Joel Brobecker
2009-02-12 20:32 ` Thiago Jung Bauermann
2009-02-12 22:38 ` Eli Zaretskii
2009-02-13 8:42 ` Joel Brobecker
2009-02-13 15:23 ` Eli Zaretskii
2009-02-17 0:58 ` Joel Brobecker
2009-02-17 5:54 ` Eli Zaretskii
2009-02-17 20:37 ` Tom Tromey
2009-02-19 21:45 ` Joel Brobecker
2009-06-01 3:57 ` Thiago Jung Bauermann
2009-06-01 5:05 ` Paul Pluzhnikov
2009-06-01 15:33 ` Eli Zaretskii
2009-06-01 15:46 ` Eli Zaretskii
2009-06-01 17:54 ` Thiago Jung Bauermann
2009-06-10 23:10 ` Tom Tromey
2009-06-11 14:19 ` Joel Brobecker
2009-07-03 7:21 ` Paul Pluzhnikov
2010-01-15 7:21 ` Joel Brobecker
2010-01-15 9:13 ` Joel Brobecker
2010-01-15 18:03 ` Tom Tromey
2010-01-18 6:33 ` Joel Brobecker [this message]
2010-01-18 17:48 ` Eli Zaretskii
2010-01-19 10:32 ` Joel Brobecker
2009-02-11 20:43 ` Daniel Jacobowitz
2009-02-11 21:08 ` Eli Zaretskii
2009-02-11 21:16 ` Daniel Jacobowitz
2009-02-11 21:46 ` Eli Zaretskii
2009-02-11 20:54 ` Tom Tromey
2009-02-11 21:11 ` Eli Zaretskii
2009-02-11 20:46 ` Joel Brobecker
2009-02-11 20:58 ` Tom Tromey
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20100118063303.GI17397@adacore.com \
--to=brobecker@adacore.com \
--cc=bauerman@br.ibm.com \
--cc=drow@false.org \
--cc=eliz@gnu.org \
--cc=gdb-patches@sourceware.org \
--cc=pedro@codesourcery.com \
--cc=tromey@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox