From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 26121 invoked by alias); 1 Jun 2009 03:57:10 -0000 Received: (qmail 26110 invoked by uid 22791); 1 Jun 2009 03:57:08 -0000 X-SWARE-Spam-Status: No, hits=-1.4 required=5.0 tests=AWL,BAYES_00,J_CHICKENPOX_15,J_CHICKENPOX_16,J_CHICKENPOX_22,J_CHICKENPOX_37,SPF_PASS X-Spam-Check-By: sourceware.org Received: from e24smtp02.br.ibm.com (HELO e24smtp02.br.ibm.com) (32.104.18.86) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 01 Jun 2009 03:57:01 +0000 Received: from d24relay01.br.ibm.com (d24relay01.br.ibm.com [9.8.31.16]) by e24smtp02.br.ibm.com (8.13.1/8.13.1) with ESMTP id n514Msve010467 for ; Mon, 1 Jun 2009 01:22:54 -0300 Received: from d24av01.br.ibm.com (d24av01.br.ibm.com [9.18.232.46]) by d24relay01.br.ibm.com (8.13.8/8.13.8/NCO v9.2) with ESMTP id n514uBiN3854504 for ; Mon, 1 Jun 2009 01:56:11 -0300 Received: from d24av01.br.ibm.com (loopback [127.0.0.1]) by d24av01.br.ibm.com (8.12.11.20060308/8.13.3) with ESMTP id n513uuIh003033 for ; Mon, 1 Jun 2009 00:56:57 -0300 Received: from [9.18.201.83] ([9.18.201.83]) by d24av01.br.ibm.com (8.12.11.20060308/8.12.11) with ESMTP id n513uuvJ003020; Mon, 1 Jun 2009 00:56:56 -0300 Subject: Re: RFC: add ability to "source" Python code From: Thiago Jung Bauermann To: Joel Brobecker Cc: Tom Tromey , Eli Zaretskii , drow@false.org, pedro@codesourcery.com, gdb-patches@sourceware.org In-Reply-To: <20090219200536.GA25486@adacore.com> References: <20090211220118.GE13021@adacore.com> <20090212062654.GG13021@adacore.com> <20090213022246.GA5401@adacore.com> <20090217000746.GA3812@adacore.com> <20090219200536.GA25486@adacore.com> Content-Type: text/plain; charset="UTF-8" Date: Mon, 01 Jun 2009 03:57:00 -0000 Message-Id: <1243828612.9045.9.camel@hactar> Mime-Version: 1.0 Content-Transfer-Encoding: 8bit X-IsSubscribed: yes Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2009-06/txt/msg00001.txt.bz2 Hi folks, I'm resurrecting this old thread. I believe it is important that we settle this discussion before 7.0. Em Qui, 2009-02-19 às 12:05 -0800, Joel Brobecker escreveu: > > Eli> I'm okay with this, but I think Tom had some valid reasons for > > Eli> having Python scripts that don't have a certain extension. > > > > It is a convenience when using scratch files. > > In practice I don't use -p much. I don't mind removing it. > > Ok, we can try without, and see if we get any request? Agreed. I don't care much about -p either. > So, where do you think we should go from now? > a tri-state setting: > > 1. Use filename extension and evaluate accordingly. > Error if the language was not compiled in. > > 2, Use filename extension and evaluate accordingly. > Fallback to GDB scripting if the language was not compiled in. > > 3. No filename extension recognition, always treat sourced files > as GDB scripts. > > Would that be acceptable to everyone? This patch implements such tri-state setting: (gdb) help set script-extension Set mode for script filename extension recognition. off == no filename extension recognition (all sourced files are GDB scripts) soft == evaluate script according to filename extension, fallback to GDB script hard == evaluate script according to filename extension, error if not supported The default is 'soft'. When Python support is not compiled in, the code always assumes a GDB script. Is this good enough for everybody? Please ignore the documentation part of the patch, I'll only update it when we have the code part ready to commit. -- []'s Thiago Jung Bauermann IBM Linux Technology Center 2009-06-01 Tom Tromey Thiago Jung Bauermann * python/python.c (source_python_script): New function. * python/python.h (source_python_script): Declare. * cli/cli-cmds.c (find_argument): New function. (source_command): Use find_argument. Handle -p argument. Use make_cleanup_restore_integer. (source_verbose_cleanup): Remove. (source_python): New global. Include python.h. (init_cli_cmds): Update. 2009-06-01 Tom Tromey * gdb.texinfo (File Options): Document -x on .py files. (Command Files): Document source -p. 2009-06-01 Tom Tromey Thiago Jung Bauermann * gdb.base/help.exp: Update "source" help text. * gdb.python/source2.py: New file. * gdb.python/source1: New file. * gdb.python/python.exp: Test "source" command. Index: gdb.git/gdb/cli/cli-cmds.c =================================================================== --- gdb.git.orig/gdb/cli/cli-cmds.c 2009-06-01 00:51:11.000000000 -0300 +++ gdb.git/gdb/cli/cli-cmds.c 2009-06-01 00:52:37.000000000 -0300 @@ -19,6 +19,7 @@ along with this program. If not, see . */ #include "defs.h" +#include "exceptions.h" #include "readline/readline.h" #include "readline/tilde.h" #include "completer.h" @@ -45,6 +46,8 @@ #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 @@ -181,6 +184,21 @@ struct cmd_list_element *showchecklist; int source_verbose = 0; int trace_commands = 0; +/* 'script-extension' option support. */ + +static const char script_ext_off[] = "off"; +static const char script_ext_soft[] = "soft"; +static const char script_ext_hard[] = "hard"; + +static const char *script_ext_enums[] = { + script_ext_off, + script_ext_soft, + script_ext_hard, + NULL +}; + +static const char *script_ext_mode = script_ext_soft; + /* Utility used everywhere when at least one argument is needed and none is supplied. */ @@ -432,18 +450,25 @@ cd_command (char *dir, int from_tty) pwd_command ((char *) 0, 1); } -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); @@ -467,12 +492,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); } @@ -1286,6 +1357,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\ +hard == 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.")); Index: gdb.git/gdb/doc/gdb.texinfo =================================================================== --- gdb.git.orig/gdb/doc/gdb.texinfo 2009-06-01 00:51:11.000000000 -0300 +++ gdb.git/gdb/doc/gdb.texinfo 2009-06-01 00:52:37.000000000 -0300 @@ -957,8 +957,11 @@ Connect to process ID @var{number}, as w @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} @@ -18227,6 +18230,11 @@ If @code{-v}, for verbose mode, is given 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 Index: gdb.git/gdb/python/python.c =================================================================== --- gdb.git.orig/gdb/python/python.c 2009-06-01 00:51:11.000000000 -0300 +++ gdb.git/gdb/python/python.c 2009-06-01 00:52:37.000000000 -0300 @@ -18,6 +18,7 @@ along with this program. If not, see . */ #include "defs.h" +#include "exceptions.h" #include "command.h" #include "ui-out.h" #include "cli/cli-script.h" @@ -275,6 +276,22 @@ execute_gdb_command (PyObject *self, PyO Py_RETURN_NONE; } +/* 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); +} + /* Printing. */ @@ -460,6 +477,14 @@ eval_python_from_control_command (struct 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 */ Index: gdb.git/gdb/python/python.h =================================================================== --- gdb.git.orig/gdb/python/python.h 2009-06-01 00:51:12.000000000 -0300 +++ gdb.git/gdb/python/python.h 2009-06-01 00:53:48.000000000 -0300 @@ -26,6 +26,8 @@ extern struct value *values_in_python; 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, Index: gdb.git/gdb/testsuite/gdb.python/python.exp =================================================================== --- gdb.git.orig/gdb/testsuite/gdb.python/python.exp 2009-06-01 00:52:37.000000000 -0300 +++ gdb.git/gdb/testsuite/gdb.python/python.exp 2009-06-01 00:55:50.000000000 -0300 @@ -27,8 +27,8 @@ gdb_start gdb_reinitialize_dir $srcdir/$subdir if ![python_supported] then { - unsupported "python support is disabled" - return -1 + gdb_test "source $srcdir/$subdir/source2.py" "Error in sourced command file:.*" + return 0 } gdb_py_test_multiple "multi-line python command" \ @@ -51,5 +51,7 @@ gdb_py_test_multiple "indented multi-lin "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()" "\\\[\\\]" Index: gdb.git/gdb/testsuite/gdb.python/source1 =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ gdb.git/gdb/testsuite/gdb.python/source1 2009-06-01 00:52:37.000000000 -0300 @@ -0,0 +1,19 @@ +# This testcase is part of GDB, the GNU debugger. + +# Copyright 2008, 2009 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 . + +# This is sourced as python; pick an expression that is not valid for gdb. +print 'y%ss' % 'e' Index: gdb.git/gdb/testsuite/gdb.python/source2.py =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ gdb.git/gdb/testsuite/gdb.python/source2.py 2009-06-01 00:52:37.000000000 -0300 @@ -0,0 +1,18 @@ +# This testcase is part of GDB, the GNU debugger. + +# Copyright 2008, 2009 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 . + +print 'y%ss' % 'e' Index: gdb.git/gdb/exceptions.h =================================================================== --- gdb.git.orig/gdb/exceptions.h 2009-06-01 00:51:12.000000000 -0300 +++ gdb.git/gdb/exceptions.h 2009-06-01 00:52:37.000000000 -0300 @@ -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 };