1 /* General GDB/Guile code.
3 Copyright (C) 2014 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* See README file in this directory for implementation notes, coding
21 conventions, et.al. */
25 #include "breakpoint.h"
26 #include "cli/cli-cmds.h"
27 #include "cli/cli-script.h"
28 #include "cli/cli-utils.h"
32 #include "extension-priv.h"
37 #include "guile-internal.h"
40 /* Declared constants and enum for guile exception printing. */
41 const char gdbscm_print_excp_none
[] = "none";
42 const char gdbscm_print_excp_full
[] = "full";
43 const char gdbscm_print_excp_message
[] = "message";
45 /* "set guile print-stack" choices. */
46 static const char *const guile_print_excp_enums
[] =
48 gdbscm_print_excp_none
,
49 gdbscm_print_excp_full
,
50 gdbscm_print_excp_message
,
54 /* The exception printing variable. 'full' if we want to print the
55 error message and stack, 'none' if we want to print nothing, and
56 'message' if we only want to print the error message. 'message' is
58 const char *gdbscm_print_excp
= gdbscm_print_excp_message
;
61 /* Forward decls, these are defined later. */
62 static const struct extension_language_script_ops guile_extension_script_ops
;
63 static const struct extension_language_ops guile_extension_ops
;
66 /* The main struct describing GDB's interface to the Guile
67 extension language. */
68 const struct extension_language_defn extension_language_guile
=
80 &guile_extension_script_ops
,
90 static void gdbscm_finish_initialization
91 (const struct extension_language_defn
*);
92 static int gdbscm_initialized (const struct extension_language_defn
*);
93 static void gdbscm_eval_from_control_command
94 (const struct extension_language_defn
*, struct command_line
*);
95 static script_sourcer_func gdbscm_source_script
;
97 int gdb_scheme_initialized
;
99 /* Symbol for setting documentation strings. */
100 SCM gdbscm_documentation_symbol
;
102 /* Keywords used by various functions. */
103 static SCM from_tty_keyword
;
104 static SCM to_string_keyword
;
106 /* The name of the various modules (without the surrounding parens). */
107 const char gdbscm_module_name
[] = "gdb";
108 const char gdbscm_init_module_name
[] = "gdb init";
110 /* The name of the bootstrap file. */
111 static const char boot_scm_filename
[] = "boot.scm";
113 /* The interface between gdb proper and loading of python scripts. */
115 static const struct extension_language_script_ops guile_extension_script_ops
=
117 gdbscm_source_script
,
118 gdbscm_source_objfile_script
,
119 gdbscm_auto_load_enabled
122 /* The interface between gdb proper and guile scripting. */
124 static const struct extension_language_ops guile_extension_ops
=
126 gdbscm_finish_initialization
,
129 gdbscm_eval_from_control_command
,
131 NULL
, /* gdbscm_start_type_printers, */
132 NULL
, /* gdbscm_apply_type_printers, */
133 NULL
, /* gdbscm_free_type_printers, */
135 gdbscm_apply_val_pretty_printer
,
137 NULL
, /* gdbscm_apply_frame_filter, */
139 gdbscm_preserve_values
,
141 gdbscm_breakpoint_has_cond
,
142 gdbscm_breakpoint_cond_says_stop
,
144 NULL
, /* gdbscm_check_quit_flag, */
145 NULL
, /* gdbscm_clear_quit_flag, */
146 NULL
, /* gdbscm_set_quit_flag, */
149 /* Implementation of the gdb "guile-repl" command. */
152 guile_repl_command (char *arg
, int from_tty
)
154 struct cleanup
*cleanup
;
156 cleanup
= make_cleanup_restore_integer (&interpreter_async
);
157 interpreter_async
= 0;
159 arg
= skip_spaces (arg
);
161 /* This explicitly rejects any arguments for now.
162 "It is easier to relax a restriction than impose one after the fact."
163 We would *like* to be able to pass arguments to the interactive shell
164 but that's not what python-interactive does. Until there is time to
165 sort it out, we forbid arguments. */
168 error (_("guile-repl currently does not take any arguments."));
172 gdbscm_enter_repl ();
175 do_cleanups (cleanup
);
178 /* Implementation of the gdb "guile" command.
179 Note: Contrary to the Python version this displays the result.
180 Have to see which is better.
182 TODO: Add the result to Guile's history? */
185 guile_command (char *arg
, int from_tty
)
187 struct cleanup
*cleanup
;
189 cleanup
= make_cleanup_restore_integer (&interpreter_async
);
190 interpreter_async
= 0;
192 arg
= skip_spaces (arg
);
196 char *msg
= gdbscm_safe_eval_string (arg
, 1);
200 make_cleanup (xfree
, msg
);
206 struct command_line
*l
= get_command_line (guile_control
, "");
208 make_cleanup_free_command_lines (&l
);
209 execute_control_command_untraced (l
);
212 do_cleanups (cleanup
);
215 /* Given a command_line, return a command string suitable for passing
216 to Guile. Lines in the string are separated by newlines. The return
217 value is allocated using xmalloc and the caller is responsible for
221 compute_scheme_string (struct command_line
*l
)
223 struct command_line
*iter
;
228 for (iter
= l
; iter
; iter
= iter
->next
)
229 size
+= strlen (iter
->line
) + 1;
231 script
= xmalloc (size
+ 1);
233 for (iter
= l
; iter
; iter
= iter
->next
)
235 int len
= strlen (iter
->line
);
237 strcpy (&script
[here
], iter
->line
);
239 script
[here
++] = '\n';
245 /* Take a command line structure representing a "guile" command, and
246 evaluate its body using the Guile interpreter.
247 This is the extension_language_ops.eval_from_control_command "method". */
250 gdbscm_eval_from_control_command
251 (const struct extension_language_defn
*extlang
, struct command_line
*cmd
)
254 struct cleanup
*cleanup
;
256 if (cmd
->body_count
!= 1)
257 error (_("Invalid \"guile\" block structure."));
259 cleanup
= make_cleanup (null_cleanup
, NULL
);
261 script
= compute_scheme_string (cmd
->body_list
[0]);
262 msg
= gdbscm_safe_eval_string (script
, 0);
266 make_cleanup (xfree
, msg
);
270 do_cleanups (cleanup
);
273 /* Read a file as Scheme code.
274 This is the extension_language_script_ops.script_sourcer "method".
275 FILE is the file to run. FILENAME is name of the file FILE.
276 This does not throw any errors. If an exception occurs an error message
280 gdbscm_source_script (const struct extension_language_defn
*extlang
,
281 FILE *file
, const char *filename
)
283 char *msg
= gdbscm_safe_source_script (filename
);
287 fprintf_filtered (gdb_stderr
, "%s\n", msg
);
292 /* (execute string [#:from-tty boolean] [#:to-string boolean\
293 A Scheme function which evaluates a string using the gdb CLI. */
296 gdbscm_execute_gdb_command (SCM command_scm
, SCM rest
)
298 int from_tty_arg_pos
= -1, to_string_arg_pos
= -1;
299 int from_tty
= 0, to_string
= 0;
300 volatile struct gdb_exception except
;
301 const SCM keywords
[] = { from_tty_keyword
, to_string_keyword
, SCM_BOOL_F
};
304 struct cleanup
*cleanups
;
306 gdbscm_parse_function_args (FUNC_NAME
, SCM_ARG1
, keywords
, "s#tt",
307 command_scm
, &command
, rest
,
308 &from_tty_arg_pos
, &from_tty
,
309 &to_string_arg_pos
, &to_string
);
311 /* Note: The contents of "command" may get modified while it is
313 cleanups
= make_cleanup (xfree
, command
);
315 TRY_CATCH (except
, RETURN_MASK_ALL
)
317 struct cleanup
*inner_cleanups
;
319 inner_cleanups
= make_cleanup_restore_integer (&interpreter_async
);
320 interpreter_async
= 0;
322 prevent_dont_repeat ();
324 result
= execute_command_to_string (command
, from_tty
);
327 execute_command (command
, from_tty
);
331 /* Do any commands attached to breakpoint we stopped at. */
332 bpstat_do_actions ();
334 do_cleanups (inner_cleanups
);
336 do_cleanups (cleanups
);
337 GDBSCM_HANDLE_GDB_EXCEPTION (except
);
341 SCM r
= gdbscm_scm_from_c_string (result
);
345 return SCM_UNSPECIFIED
;
348 /* (data-directory) -> string */
351 gdbscm_data_directory (void)
353 return gdbscm_scm_from_c_string (gdb_datadir
);
356 /* (gdb-version) -> string */
359 gdbscm_gdb_version (void)
361 return gdbscm_scm_from_c_string (version
);
364 /* (host-config) -> string */
367 gdbscm_host_config (void)
369 return gdbscm_scm_from_c_string (host_name
);
372 /* (target-config) -> string */
375 gdbscm_target_config (void)
377 return gdbscm_scm_from_c_string (target_name
);
380 #else /* ! HAVE_GUILE */
382 /* Dummy implementation of the gdb "guile-repl" and "guile"
386 guile_repl_command (char *arg
, int from_tty
)
388 arg
= skip_spaces (arg
);
390 error (_("guile-repl currently does not take any arguments."));
391 error (_("Guile scripting is not supported in this copy of GDB."));
395 guile_command (char *arg
, int from_tty
)
397 arg
= skip_spaces (arg
);
399 error (_("Guile scripting is not supported in this copy of GDB."));
402 /* Even if Guile isn't enabled, we still have to slurp the
403 command list to the corresponding "end". */
404 struct command_line
*l
= get_command_line (guile_control
, "");
405 struct cleanup
*cleanups
= make_cleanup_free_command_lines (&l
);
407 execute_control_command_untraced (l
);
408 do_cleanups (cleanups
);
412 #endif /* ! HAVE_GUILE */
414 /* Lists for 'set,show,info guile' commands. */
416 static struct cmd_list_element
*set_guile_list
;
417 static struct cmd_list_element
*show_guile_list
;
418 static struct cmd_list_element
*info_guile_list
;
420 /* Function for use by 'set guile' prefix command. */
423 set_guile_command (char *args
, int from_tty
)
425 help_list (set_guile_list
, "set guile ", all_commands
, gdb_stdout
);
428 /* Function for use by 'show guile' prefix command. */
431 show_guile_command (char *args
, int from_tty
)
433 cmd_show_list (show_guile_list
, from_tty
, "");
436 /* The "info scheme" command is defined as a prefix, with
437 allow_unknown 0. Therefore, its own definition is called only for
438 "info scheme" with no args. */
441 info_guile_command (char *args
, int from_tty
)
443 printf_unfiltered (_("\"info guile\" must be followed"
444 " by the name of an info command.\n"));
445 help_list (info_guile_list
, "info guile ", -1, gdb_stdout
);
448 /* Initialization. */
452 static const scheme_function misc_guile_functions
[] =
454 { "execute", 1, 0, 1, gdbscm_execute_gdb_command
,
456 Execute the given GDB command.\n\
458 Arguments: string [#:to-string boolean] [#:from-tty boolean]\n\
459 If #:from-tty is true then the command executes as if entered\n\
460 from the keyboard. The default is false (#f).\n\
461 If #:to-string is true then the result is returned as a string.\n\
462 Otherwise output is sent to the current output port,\n\
463 which is the default.\n\
464 Returns: The result of the command if #:to-string is true.\n\
465 Otherwise returns unspecified." },
467 { "data-directory", 0, 0, 0, gdbscm_data_directory
,
469 Return the name of GDB's data directory." },
471 { "gdb-version", 0, 0, 0, gdbscm_gdb_version
,
473 Return GDB's version string." },
475 { "host-config", 0, 0, 0, gdbscm_host_config
,
477 Return the name of the host configuration." },
479 { "target-config", 0, 0, 0, gdbscm_target_config
,
481 Return the name of the target configuration." },
486 /* Load gdb/boot.scm, the Scheme side of GDB/Guile support.
487 Note: This function assumes it's called within the gdb module. */
490 initialize_scheme_side (void)
492 char *gdb_guile_dir
= concat (gdb_datadir
, SLASH_STRING
, "guile", NULL
);
493 char *boot_scm_path
= concat (gdb_guile_dir
, SLASH_STRING
, "gdb",
494 SLASH_STRING
, boot_scm_filename
, NULL
);
497 /* While scm_c_primitive_load works, the loaded code is not compiled,
498 instead it is left to be interpreted. Eh?
499 Anyways, this causes a ~100x slowdown, so we only use it to load
500 gdb/boot.scm, and then let boot.scm do the rest. */
501 msg
= gdbscm_safe_source_script (boot_scm_path
);
505 fprintf_filtered (gdb_stderr
, "%s", msg
);
508 "Could not complete Guile gdb module initialization from:\n"
510 "Limited Guile support is available.\n"
511 "Suggest passing --data-directory=/path/to/gdb/data-directory.\n"),
515 xfree (gdb_guile_dir
);
516 xfree (boot_scm_path
);
519 /* Install the gdb scheme module.
520 The result is a boolean indicating success.
521 If initializing the gdb module fails an error message is printed.
522 Note: This function runs in the context of the gdb module. */
525 initialize_gdb_module (void *data
)
527 /* The documentation symbol needs to be defined before any calls to
528 gdbscm_define_{variables,functions}. */
529 gdbscm_documentation_symbol
= scm_from_latin1_symbol ("documentation");
531 /* The smob and exception support must be initialized early. */
532 gdbscm_initialize_smobs ();
533 gdbscm_initialize_exceptions ();
535 /* The rest are initialized in alphabetical order. */
536 gdbscm_initialize_arches ();
537 gdbscm_initialize_auto_load ();
538 gdbscm_initialize_blocks ();
539 gdbscm_initialize_breakpoints ();
540 gdbscm_initialize_commands ();
541 gdbscm_initialize_disasm ();
542 gdbscm_initialize_frames ();
543 gdbscm_initialize_iterators ();
544 gdbscm_initialize_lazy_strings ();
545 gdbscm_initialize_math ();
546 gdbscm_initialize_objfiles ();
547 gdbscm_initialize_ports ();
548 gdbscm_initialize_pretty_printers ();
549 gdbscm_initialize_pspaces ();
550 gdbscm_initialize_strings ();
551 gdbscm_initialize_symbols ();
552 gdbscm_initialize_symtabs ();
553 gdbscm_initialize_types ();
554 gdbscm_initialize_values ();
556 gdbscm_define_functions (misc_guile_functions
, 1);
558 from_tty_keyword
= scm_from_latin1_keyword ("from-tty");
559 to_string_keyword
= scm_from_latin1_keyword ("to-string");
561 initialize_scheme_side ();
563 gdb_scheme_initialized
= 1;
566 /* Utility to call scm_c_define_module+initialize_gdb_module from
567 within scm_with_guile. */
570 call_initialize_gdb_module (void *data
)
572 /* Most of the initialization is done by initialize_gdb_module.
573 It is called via scm_c_define_module so that the initialization is
574 performed within the desired module. */
575 scm_c_define_module (gdbscm_module_name
, initialize_gdb_module
, NULL
);
580 /* A callback to finish Guile initialization after gdb has finished all its
582 This is the extension_language_ops.finish_initialization "method". */
585 gdbscm_finish_initialization (const struct extension_language_defn
*extlang
)
587 /* Restore the environment to the user interaction one. */
588 scm_set_current_module (scm_interaction_environment ());
591 /* The extension_language_ops.initialized "method". */
594 gdbscm_initialized (const struct extension_language_defn
*extlang
)
596 return gdb_scheme_initialized
;
599 /* Enable or disable Guile backtraces. */
602 gdbscm_set_backtrace (int enable
)
604 static const char disable_bt
[] = "(debug-disable 'backtrace)";
605 static const char enable_bt
[] = "(debug-enable 'backtrace)";
608 gdbscm_safe_eval_string (enable_bt
, 0);
610 gdbscm_safe_eval_string (disable_bt
, 0);
613 #endif /* HAVE_GUILE */
615 /* Install the various gdb commands used by Guile. */
618 install_gdb_commands (void)
620 add_com ("guile-repl", class_obscure
,
624 Start an interactive Guile prompt.\n\
626 To return to GDB, type the EOF character (e.g., Ctrl-D on an empty\n\
628 #else /* HAVE_GUILE */
630 Start a Guile interactive prompt.\n\
632 Guile scripting is not supported in this copy of GDB.\n\
633 This command is only a placeholder.")
634 #endif /* HAVE_GUILE */
636 add_com_alias ("gr", "guile-repl", class_obscure
, 1);
638 /* Since "help guile" is easy to type, and intuitive, we add general help
639 in using GDB+Guile to this command. */
640 add_com ("guile", class_obscure
, guile_command
,
643 Evaluate one or more Guile expressions.\n\
645 The expression(s) can be given as an argument, for instance:\n\
647 guile (display 23)\n\
649 The result of evaluating the last expression is printed.\n\
651 If no argument is given, the following lines are read and passed\n\
652 to Guile for evaluation. Type a line containing \"end\" to indicate\n\
653 the end of the set of expressions.\n\
655 The Guile GDB module must first be imported before it can be used.\n\
657 (gdb) guile (use-modules (gdb))\n\
658 or if you want to import the (gdb) module with a prefix, use:\n\
659 (gdb) guile (use-modules ((gdb) #:renamer (symbol-prefix-proc 'gdb:)))\n\
661 The Guile interactive session, started with the \"guile-repl\"\n\
662 command, provides extensive help and apropos capabilities.\n\
663 Type \",help\" once in a Guile interactive session.")
664 #else /* HAVE_GUILE */
666 Evaluate a Guile expression.\n\
668 Guile scripting is not supported in this copy of GDB.\n\
669 This command is only a placeholder.")
670 #endif /* HAVE_GUILE */
672 add_com_alias ("gu", "guile", class_obscure
, 1);
674 add_prefix_cmd ("guile", class_obscure
, set_guile_command
,
675 _("Prefix command for Guile preference settings."),
676 &set_guile_list
, "set guile ", 0,
678 add_alias_cmd ("gu", "guile", class_obscure
, 1, &setlist
);
680 add_prefix_cmd ("guile", class_obscure
, show_guile_command
,
681 _("Prefix command for Guile preference settings."),
682 &show_guile_list
, "show guile ", 0,
684 add_alias_cmd ("gu", "guile", class_obscure
, 1, &showlist
);
686 add_prefix_cmd ("guile", class_obscure
, info_guile_command
,
687 _("Prefix command for Guile info displays."),
688 &info_guile_list
, "info guile ", 0,
690 add_info_alias ("gu", "guile", 1);
692 /* The name "print-stack" is carried over from Python.
693 A better name is "print-exception". */
694 add_setshow_enum_cmd ("print-stack", no_class
, guile_print_excp_enums
,
695 &gdbscm_print_excp
, _("\
696 Set mode for Guile exception printing on error."), _("\
697 Show the mode of Guile exception printing on error."), _("\
698 none == no stack or message will be printed.\n\
699 full == a message and a stack will be printed.\n\
700 message == an error message without a stack will be printed."),
702 &set_guile_list
, &show_guile_list
);
705 /* Provide a prototype to silence -Wmissing-prototypes. */
706 extern initialize_file_ftype _initialize_guile
;
709 _initialize_guile (void)
713 install_gdb_commands ();
716 /* The Python support puts the C side in module "_gdb", leaving the Python
717 side to define module "gdb" which imports "_gdb". There is evidently no
718 similar convention in Guile so we skip this. */
720 /* scm_with_guile is the most portable way to initialize Guile.
721 Plus we need to initialize the Guile support while in Guile mode
722 (e.g., called from within a call to scm_with_guile). */
723 scm_with_guile (call_initialize_gdb_module
, NULL
);
725 /* Set Guile's backtrace to match the "set guile print-stack" default.
726 [N.B. The two settings are still separate.]
727 But only do this after we've initialized Guile, it's nice to see a
728 backtrace if there's an error during initialization.
729 OTOH, if the error is that gdb/init.scm wasn't found because gdb is being
730 run from the build tree, the backtrace is more noise than signal.
732 gdbscm_set_backtrace (0);