Skip to content

Commit 973817a

Browse files
author
Joel Brobecker
committed
Allow "source" to load python scripts.
* 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.
1 parent 23db03a commit 973817a

5 files changed

Lines changed: 139 additions & 12 deletions

File tree

‎gdb/ChangeLog‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
2010-01-18 Tom Tromey <tromey@redhat.com>
2+
Thiago Jung Bauermann <bauerman@br.ibm.com>
3+
4+
Allow "source" to load python scripts.
5+
* exceptions.h (enum errors): Add UNSUPPORTED_ERROR.
6+
* python/python.c (source_python_script): New function.
7+
* python/python.h (source_python_script): Add declaration.
8+
* cli/cli-cmds.c: #include exceptions.h and python/python.h.
9+
(script_ext_off, script_ext_soft, script_ext_strict)
10+
(script_ext_enums, script_ext_mode): New static constants.
11+
(show_script_ext_mode, find_and_open_script): New functions.
12+
(source_script): Enhance to handle Python scripts.
13+
(init_cli_cmds): Add set/show script-extension commands.
14+
115
2010-01-16 Stan Shebs <stan@codesourcery.com>
216

317
* tracepoint.h (struct trace_status): Use unsigned long long

‎gdb/cli/cli-cmds.c‎

Lines changed: 96 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
along with this program. If not, see <http://www.gnu.org/licenses/>. */
2020

2121
#include "defs.h"
22+
#include "exceptions.h"
2223
#include "arch-utils.h"
2324
#include "readline/readline.h"
2425
#include "readline/tilde.h"
@@ -47,6 +48,8 @@ extern void disconnect_or_stop_tracing (int from_tty);
4748
#include "cli/cli-setshow.h"
4849
#include "cli/cli-cmds.h"
4950

51+
#include "python/python.h"
52+
5053
#ifdef TUI
5154
#include "tui/tui.h" /* For tui_active et.al. */
5255
#endif
@@ -187,6 +190,21 @@ struct cmd_list_element *showchecklist;
187190
int source_verbose = 0;
188191
int trace_commands = 0;
189192

193+
/* 'script-extension' option support. */
194+
195+
static const char script_ext_off[] = "off";
196+
static const char script_ext_soft[] = "soft";
197+
static const char script_ext_strict[] = "strict";
198+
199+
static const char *script_ext_enums[] = {
200+
script_ext_off,
201+
script_ext_soft,
202+
script_ext_strict,
203+
NULL
204+
};
205+
206+
static const char *script_ext_mode = script_ext_soft;
207+
190208
/* Utility used everywhere when at least one argument is needed and
191209
none is supplied. */
192210

@@ -441,18 +459,25 @@ cd_command (char *dir, int from_tty)
441459
pwd_command ((char *) 0, 1);
442460
}
443461

444-
void
445-
source_script (char *file, int from_tty)
462+
/* Show the current value of the 'script-extension' option. */
463+
464+
static void
465+
show_script_ext_mode (struct ui_file *file, int from_tty,
466+
struct cmd_list_element *c, const char *value)
446467
{
447-
FILE *stream;
448-
struct cleanup *old_cleanups;
468+
fprintf_filtered (file, _("\
469+
Script filename extension recognition is \"%s\".\n"),
470+
value);
471+
}
472+
473+
static int
474+
find_and_open_script (int from_tty, char **filep, FILE **streamp,
475+
struct cleanup **cleanupp)
476+
{
477+
char *file = *filep;
449478
char *full_pathname = NULL;
450479
int fd;
451-
452-
if (file == NULL || *file == 0)
453-
{
454-
error (_("source command requires file name of file to source."));
455-
}
480+
struct cleanup *old_cleanups;
456481

457482
file = tilde_expand (file);
458483
old_cleanups = make_cleanup (xfree, file);
@@ -476,12 +501,58 @@ source_script (char *file, int from_tty)
476501
else
477502
{
478503
do_cleanups (old_cleanups);
479-
return;
504+
return 0;
480505
}
481506
}
482507

483-
stream = fdopen (fd, FOPEN_RT);
484-
script_from_file (stream, file);
508+
*streamp = fdopen (fd, FOPEN_RT);
509+
*filep = file;
510+
*cleanupp = old_cleanups;
511+
512+
return 1;
513+
}
514+
515+
void
516+
source_script (char *file, int from_tty)
517+
{
518+
FILE *stream;
519+
struct cleanup *old_cleanups;
520+
521+
if (file == NULL || *file == 0)
522+
{
523+
error (_("source command requires file name of file to source."));
524+
}
525+
526+
if (!find_and_open_script (from_tty, &file, &stream, &old_cleanups))
527+
return;
528+
529+
if (script_ext_mode != script_ext_off
530+
&& strlen (file) > 3 && !strcmp (&file[strlen (file) - 3], ".py"))
531+
{
532+
volatile struct gdb_exception e;
533+
534+
TRY_CATCH (e, RETURN_MASK_ERROR)
535+
{
536+
source_python_script (stream, file);
537+
}
538+
if (e.reason < 0)
539+
{
540+
/* Should we fallback to ye olde GDB script mode? */
541+
if (script_ext_mode == script_ext_soft
542+
&& e.reason == RETURN_ERROR && e.error == UNSUPPORTED_ERROR)
543+
{
544+
if (!find_and_open_script (from_tty, &file, &stream, &old_cleanups))
545+
return;
546+
547+
script_from_file (stream, file);
548+
}
549+
else
550+
/* Nope, just punt. */
551+
throw_exception (e);
552+
}
553+
}
554+
else
555+
script_from_file (stream, file);
485556

486557
do_cleanups (old_cleanups);
487558
}
@@ -1314,6 +1385,19 @@ when GDB is started."), gdbinit);
13141385
source_help_text, &cmdlist);
13151386
set_cmd_completer (c, filename_completer);
13161387

1388+
add_setshow_enum_cmd ("script-extension", class_support,
1389+
script_ext_enums, &script_ext_mode, _("\
1390+
Set mode for script filename extension recognition."), _("\
1391+
Show mode for script filename extension recognition."), _("\
1392+
off == no filename extension recognition (all sourced files are GDB scripts)\n\
1393+
soft == evaluate script according to filename extension, fallback to GDB script"
1394+
"\n\
1395+
strict == evaluate script according to filename extension, error if not supported"
1396+
),
1397+
NULL,
1398+
show_script_ext_mode,
1399+
&setlist, &showlist);
1400+
13171401
add_com ("quit", class_support, quit_command, _("Exit gdb."));
13181402
c = add_com ("help", class_support, help_command,
13191403
_("Print list of commands."));

‎gdb/exceptions.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ enum errors {
7575
/* Error accessing memory. */
7676
MEMORY_ERROR,
7777

78+
/* Feature is not supported in this copy of GDB. */
79+
UNSUPPORTED_ERROR,
80+
7881
/* Add more errors here. */
7982
NR_ERRORS
8083
};

‎gdb/python/python.c‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,22 @@ gdbpy_parse_and_eval (PyObject *self, PyObject *args)
343343
return value_to_value_object (result);
344344
}
345345

346+
/* Read a file as Python code. STREAM is the input file; FILE is the
347+
name of the file. */
348+
349+
void
350+
source_python_script (FILE *stream, char *file)
351+
{
352+
PyGILState_STATE state;
353+
354+
state = PyGILState_Ensure ();
355+
356+
PyRun_SimpleFile (stream, file);
357+
358+
fclose (stream);
359+
PyGILState_Release (state);
360+
}
361+
346362

347363

348364
/* Printing. */
@@ -525,6 +541,14 @@ eval_python_from_control_command (struct command_line *cmd)
525541
error (_("Python scripting is not supported in this copy of GDB."));
526542
}
527543

544+
void
545+
source_python_script (FILE *stream, char *file)
546+
{
547+
fclose (stream);
548+
throw_error (UNSUPPORTED_ERROR,
549+
_("Python scripting is not supported in this copy of GDB."));
550+
}
551+
528552
#endif /* HAVE_PYTHON */
529553

530554

‎gdb/python/python.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
void eval_python_from_control_command (struct command_line *);
2626

27+
void source_python_script (FILE *stream, char *file);
28+
2729
int apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr,
2830
int embedded_offset, CORE_ADDR address,
2931
struct ui_file *stream, int recurse,

0 commit comments

Comments
 (0)