]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/guile/guile.c
Add progspace support for Guile.
[thirdparty/binutils-gdb.git] / gdb / guile / guile.c
1 /* General GDB/Guile code.
2
3 Copyright (C) 2014 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 /* See README file in this directory for implementation notes, coding
21 conventions, et.al. */
22
23 #include "defs.h"
24 #include <string.h>
25 #include "breakpoint.h"
26 #include "cli/cli-cmds.h"
27 #include "cli/cli-script.h"
28 #include "cli/cli-utils.h"
29 #include "command.h"
30 #include "gdbcmd.h"
31 #include "interps.h"
32 #include "extension-priv.h"
33 #include "utils.h"
34 #include "version.h"
35 #ifdef HAVE_GUILE
36 #include "guile.h"
37 #include "guile-internal.h"
38 #endif
39
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";
44
45 /* "set guile print-stack" choices. */
46 static const char *const guile_print_excp_enums[] =
47 {
48 gdbscm_print_excp_none,
49 gdbscm_print_excp_full,
50 gdbscm_print_excp_message,
51 NULL
52 };
53
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
57 the default. */
58 const char *gdbscm_print_excp = gdbscm_print_excp_message;
59
60 #ifdef HAVE_GUILE
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;
64 #endif
65
66 /* The main struct describing GDB's interface to the Guile
67 extension language. */
68 const struct extension_language_defn extension_language_guile =
69 {
70 EXT_LANG_GUILE,
71 "guile",
72 "Guile",
73
74 ".scm",
75 "-gdb.scm",
76
77 guile_control,
78
79 #ifdef HAVE_GUILE
80 &guile_extension_script_ops,
81 &guile_extension_ops
82 #else
83 NULL,
84 NULL
85 #endif
86 };
87 \f
88 #ifdef HAVE_GUILE
89
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;
96
97 int gdb_scheme_initialized;
98
99 /* Symbol for setting documentation strings. */
100 SCM gdbscm_documentation_symbol;
101
102 /* Keywords used by various functions. */
103 static SCM from_tty_keyword;
104 static SCM to_string_keyword;
105
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";
109
110 /* The name of the bootstrap file. */
111 static const char boot_scm_filename[] = "boot.scm";
112
113 /* The interface between gdb proper and loading of python scripts. */
114
115 static const struct extension_language_script_ops guile_extension_script_ops =
116 {
117 gdbscm_source_script,
118 gdbscm_source_objfile_script,
119 gdbscm_auto_load_enabled
120 };
121
122 /* The interface between gdb proper and guile scripting. */
123
124 static const struct extension_language_ops guile_extension_ops =
125 {
126 gdbscm_finish_initialization,
127 gdbscm_initialized,
128
129 gdbscm_eval_from_control_command,
130
131 NULL, /* gdbscm_start_type_printers, */
132 NULL, /* gdbscm_apply_type_printers, */
133 NULL, /* gdbscm_free_type_printers, */
134
135 gdbscm_apply_val_pretty_printer,
136
137 NULL, /* gdbscm_apply_frame_filter, */
138
139 gdbscm_preserve_values,
140
141 gdbscm_breakpoint_has_cond,
142 gdbscm_breakpoint_cond_says_stop,
143
144 NULL, /* gdbscm_check_quit_flag, */
145 NULL, /* gdbscm_clear_quit_flag, */
146 NULL, /* gdbscm_set_quit_flag, */
147 };
148
149 /* Implementation of the gdb "guile-repl" command. */
150
151 static void
152 guile_repl_command (char *arg, int from_tty)
153 {
154 struct cleanup *cleanup;
155
156 cleanup = make_cleanup_restore_integer (&interpreter_async);
157 interpreter_async = 0;
158
159 arg = skip_spaces (arg);
160
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. */
166
167 if (arg && *arg)
168 error (_("guile-repl currently does not take any arguments."));
169 else
170 {
171 dont_repeat ();
172 gdbscm_enter_repl ();
173 }
174
175 do_cleanups (cleanup);
176 }
177
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.
181
182 TODO: Add the result to Guile's history? */
183
184 static void
185 guile_command (char *arg, int from_tty)
186 {
187 struct cleanup *cleanup;
188
189 cleanup = make_cleanup_restore_integer (&interpreter_async);
190 interpreter_async = 0;
191
192 arg = skip_spaces (arg);
193
194 if (arg && *arg)
195 {
196 char *msg = gdbscm_safe_eval_string (arg, 1);
197
198 if (msg != NULL)
199 {
200 make_cleanup (xfree, msg);
201 error ("%s", msg);
202 }
203 }
204 else
205 {
206 struct command_line *l = get_command_line (guile_control, "");
207
208 make_cleanup_free_command_lines (&l);
209 execute_control_command_untraced (l);
210 }
211
212 do_cleanups (cleanup);
213 }
214
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
218 freeing it. */
219
220 static char *
221 compute_scheme_string (struct command_line *l)
222 {
223 struct command_line *iter;
224 char *script = NULL;
225 int size = 0;
226 int here;
227
228 for (iter = l; iter; iter = iter->next)
229 size += strlen (iter->line) + 1;
230
231 script = xmalloc (size + 1);
232 here = 0;
233 for (iter = l; iter; iter = iter->next)
234 {
235 int len = strlen (iter->line);
236
237 strcpy (&script[here], iter->line);
238 here += len;
239 script[here++] = '\n';
240 }
241 script[here] = '\0';
242 return script;
243 }
244
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". */
248
249 static void
250 gdbscm_eval_from_control_command
251 (const struct extension_language_defn *extlang, struct command_line *cmd)
252 {
253 char *script, *msg;
254 struct cleanup *cleanup;
255
256 if (cmd->body_count != 1)
257 error (_("Invalid \"guile\" block structure."));
258
259 cleanup = make_cleanup (null_cleanup, NULL);
260
261 script = compute_scheme_string (cmd->body_list[0]);
262 msg = gdbscm_safe_eval_string (script, 0);
263 xfree (script);
264 if (msg != NULL)
265 {
266 make_cleanup (xfree, msg);
267 error ("%s", msg);
268 }
269
270 do_cleanups (cleanup);
271 }
272
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
277 is printed. */
278
279 static void
280 gdbscm_source_script (const struct extension_language_defn *extlang,
281 FILE *file, const char *filename)
282 {
283 char *msg = gdbscm_safe_source_script (filename);
284
285 if (msg != NULL)
286 {
287 fprintf_filtered (gdb_stderr, "%s\n", msg);
288 xfree (msg);
289 }
290 }
291 \f
292 /* (execute string [#:from-tty boolean] [#:to-string boolean\
293 A Scheme function which evaluates a string using the gdb CLI. */
294
295 static SCM
296 gdbscm_execute_gdb_command (SCM command_scm, SCM rest)
297 {
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 };
302 char *command;
303 char *result = NULL;
304 struct cleanup *cleanups;
305
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);
310
311 /* Note: The contents of "command" may get modified while it is
312 executed. */
313 cleanups = make_cleanup (xfree, command);
314
315 TRY_CATCH (except, RETURN_MASK_ALL)
316 {
317 struct cleanup *inner_cleanups;
318
319 inner_cleanups = make_cleanup_restore_integer (&interpreter_async);
320 interpreter_async = 0;
321
322 prevent_dont_repeat ();
323 if (to_string)
324 result = execute_command_to_string (command, from_tty);
325 else
326 {
327 execute_command (command, from_tty);
328 result = NULL;
329 }
330
331 /* Do any commands attached to breakpoint we stopped at. */
332 bpstat_do_actions ();
333
334 do_cleanups (inner_cleanups);
335 }
336 do_cleanups (cleanups);
337 GDBSCM_HANDLE_GDB_EXCEPTION (except);
338
339 if (result)
340 {
341 SCM r = gdbscm_scm_from_c_string (result);
342 xfree (result);
343 return r;
344 }
345 return SCM_UNSPECIFIED;
346 }
347
348 /* (data-directory) -> string */
349
350 static SCM
351 gdbscm_data_directory (void)
352 {
353 return gdbscm_scm_from_c_string (gdb_datadir);
354 }
355
356 /* (gdb-version) -> string */
357
358 static SCM
359 gdbscm_gdb_version (void)
360 {
361 return gdbscm_scm_from_c_string (version);
362 }
363
364 /* (host-config) -> string */
365
366 static SCM
367 gdbscm_host_config (void)
368 {
369 return gdbscm_scm_from_c_string (host_name);
370 }
371
372 /* (target-config) -> string */
373
374 static SCM
375 gdbscm_target_config (void)
376 {
377 return gdbscm_scm_from_c_string (target_name);
378 }
379
380 #else /* ! HAVE_GUILE */
381
382 /* Dummy implementation of the gdb "guile-repl" and "guile"
383 commands. */
384
385 static void
386 guile_repl_command (char *arg, int from_tty)
387 {
388 arg = skip_spaces (arg);
389 if (arg && *arg)
390 error (_("guile-repl currently does not take any arguments."));
391 error (_("Guile scripting is not supported in this copy of GDB."));
392 }
393
394 static void
395 guile_command (char *arg, int from_tty)
396 {
397 arg = skip_spaces (arg);
398 if (arg && *arg)
399 error (_("Guile scripting is not supported in this copy of GDB."));
400 else
401 {
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);
406
407 execute_control_command_untraced (l);
408 do_cleanups (cleanups);
409 }
410 }
411
412 #endif /* ! HAVE_GUILE */
413 \f
414 /* Lists for 'set,show,info guile' commands. */
415
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;
419
420 /* Function for use by 'set guile' prefix command. */
421
422 static void
423 set_guile_command (char *args, int from_tty)
424 {
425 help_list (set_guile_list, "set guile ", all_commands, gdb_stdout);
426 }
427
428 /* Function for use by 'show guile' prefix command. */
429
430 static void
431 show_guile_command (char *args, int from_tty)
432 {
433 cmd_show_list (show_guile_list, from_tty, "");
434 }
435
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. */
439
440 static void
441 info_guile_command (char *args, int from_tty)
442 {
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);
446 }
447 \f
448 /* Initialization. */
449
450 #ifdef HAVE_GUILE
451
452 static const scheme_function misc_guile_functions[] =
453 {
454 { "execute", 1, 0, 1, gdbscm_execute_gdb_command,
455 "\
456 Execute the given GDB command.\n\
457 \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." },
466
467 { "data-directory", 0, 0, 0, gdbscm_data_directory,
468 "\
469 Return the name of GDB's data directory." },
470
471 { "gdb-version", 0, 0, 0, gdbscm_gdb_version,
472 "\
473 Return GDB's version string." },
474
475 { "host-config", 0, 0, 0, gdbscm_host_config,
476 "\
477 Return the name of the host configuration." },
478
479 { "target-config", 0, 0, 0, gdbscm_target_config,
480 "\
481 Return the name of the target configuration." },
482
483 END_FUNCTIONS
484 };
485
486 /* Load gdb/boot.scm, the Scheme side of GDB/Guile support.
487 Note: This function assumes it's called within the gdb module. */
488
489 static void
490 initialize_scheme_side (void)
491 {
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);
495 char *msg;
496
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);
502
503 if (msg != NULL)
504 {
505 fprintf_filtered (gdb_stderr, "%s", msg);
506 xfree (msg);
507 warning (_("\n"
508 "Could not complete Guile gdb module initialization from:\n"
509 "%s.\n"
510 "Limited Guile support is available.\n"
511 "Suggest passing --data-directory=/path/to/gdb/data-directory.\n"),
512 boot_scm_path);
513 }
514
515 xfree (gdb_guile_dir);
516 xfree (boot_scm_path);
517 }
518
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. */
523
524 static void
525 initialize_gdb_module (void *data)
526 {
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");
530
531 /* The smob and exception support must be initialized early. */
532 gdbscm_initialize_smobs ();
533 gdbscm_initialize_exceptions ();
534
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_disasm ();
541 gdbscm_initialize_frames ();
542 gdbscm_initialize_iterators ();
543 gdbscm_initialize_lazy_strings ();
544 gdbscm_initialize_math ();
545 gdbscm_initialize_objfiles ();
546 gdbscm_initialize_ports ();
547 gdbscm_initialize_pretty_printers ();
548 gdbscm_initialize_pspaces ();
549 gdbscm_initialize_strings ();
550 gdbscm_initialize_symbols ();
551 gdbscm_initialize_symtabs ();
552 gdbscm_initialize_types ();
553 gdbscm_initialize_values ();
554
555 gdbscm_define_functions (misc_guile_functions, 1);
556
557 from_tty_keyword = scm_from_latin1_keyword ("from-tty");
558 to_string_keyword = scm_from_latin1_keyword ("to-string");
559
560 initialize_scheme_side ();
561
562 gdb_scheme_initialized = 1;
563 }
564
565 /* Utility to call scm_c_define_module+initialize_gdb_module from
566 within scm_with_guile. */
567
568 static void *
569 call_initialize_gdb_module (void *data)
570 {
571 /* Most of the initialization is done by initialize_gdb_module.
572 It is called via scm_c_define_module so that the initialization is
573 performed within the desired module. */
574 scm_c_define_module (gdbscm_module_name, initialize_gdb_module, NULL);
575
576 return NULL;
577 }
578
579 /* A callback to finish Guile initialization after gdb has finished all its
580 initialization.
581 This is the extension_language_ops.finish_initialization "method". */
582
583 static void
584 gdbscm_finish_initialization (const struct extension_language_defn *extlang)
585 {
586 /* Restore the environment to the user interaction one. */
587 scm_set_current_module (scm_interaction_environment ());
588 }
589
590 /* The extension_language_ops.initialized "method". */
591
592 static int
593 gdbscm_initialized (const struct extension_language_defn *extlang)
594 {
595 return gdb_scheme_initialized;
596 }
597
598 /* Enable or disable Guile backtraces. */
599
600 static void
601 gdbscm_set_backtrace (int enable)
602 {
603 static const char disable_bt[] = "(debug-disable 'backtrace)";
604 static const char enable_bt[] = "(debug-enable 'backtrace)";
605
606 if (enable)
607 gdbscm_safe_eval_string (enable_bt, 0);
608 else
609 gdbscm_safe_eval_string (disable_bt, 0);
610 }
611
612 #endif /* HAVE_GUILE */
613
614 /* Install the various gdb commands used by Guile. */
615
616 static void
617 install_gdb_commands (void)
618 {
619 add_com ("guile-repl", class_obscure,
620 guile_repl_command,
621 #ifdef HAVE_GUILE
622 _("\
623 Start an interactive Guile prompt.\n\
624 \n\
625 To return to GDB, type the EOF character (e.g., Ctrl-D on an empty\n\
626 prompt) or ,quit.")
627 #else /* HAVE_GUILE */
628 _("\
629 Start a Guile interactive prompt.\n\
630 \n\
631 Guile scripting is not supported in this copy of GDB.\n\
632 This command is only a placeholder.")
633 #endif /* HAVE_GUILE */
634 );
635 add_com_alias ("gr", "guile-repl", class_obscure, 1);
636
637 /* Since "help guile" is easy to type, and intuitive, we add general help
638 in using GDB+Guile to this command. */
639 add_com ("guile", class_obscure, guile_command,
640 #ifdef HAVE_GUILE
641 _("\
642 Evaluate one or more Guile expressions.\n\
643 \n\
644 The expression(s) can be given as an argument, for instance:\n\
645 \n\
646 guile (display 23)\n\
647 \n\
648 The result of evaluating the last expression is printed.\n\
649 \n\
650 If no argument is given, the following lines are read and passed\n\
651 to Guile for evaluation. Type a line containing \"end\" to indicate\n\
652 the end of the set of expressions.\n\
653 \n\
654 The Guile GDB module must first be imported before it can be used.\n\
655 Do this with:\n\
656 (gdb) guile (use-modules (gdb))\n\
657 or if you want to import the (gdb) module with a prefix, use:\n\
658 (gdb) guile (use-modules ((gdb) #:renamer (symbol-prefix-proc 'gdb:)))\n\
659 \n\
660 The Guile interactive session, started with the \"guile-repl\"\n\
661 command, provides extensive help and apropos capabilities.\n\
662 Type \",help\" once in a Guile interactive session.")
663 #else /* HAVE_GUILE */
664 _("\
665 Evaluate a Guile expression.\n\
666 \n\
667 Guile scripting is not supported in this copy of GDB.\n\
668 This command is only a placeholder.")
669 #endif /* HAVE_GUILE */
670 );
671 add_com_alias ("gu", "guile", class_obscure, 1);
672
673 add_prefix_cmd ("guile", class_obscure, set_guile_command,
674 _("Prefix command for Guile preference settings."),
675 &set_guile_list, "set guile ", 0,
676 &setlist);
677 add_alias_cmd ("gu", "guile", class_obscure, 1, &setlist);
678
679 add_prefix_cmd ("guile", class_obscure, show_guile_command,
680 _("Prefix command for Guile preference settings."),
681 &show_guile_list, "show guile ", 0,
682 &showlist);
683 add_alias_cmd ("gu", "guile", class_obscure, 1, &showlist);
684
685 add_prefix_cmd ("guile", class_obscure, info_guile_command,
686 _("Prefix command for Guile info displays."),
687 &info_guile_list, "info guile ", 0,
688 &infolist);
689 add_info_alias ("gu", "guile", 1);
690
691 /* The name "print-stack" is carried over from Python.
692 A better name is "print-exception". */
693 add_setshow_enum_cmd ("print-stack", no_class, guile_print_excp_enums,
694 &gdbscm_print_excp, _("\
695 Set mode for Guile exception printing on error."), _("\
696 Show the mode of Guile exception printing on error."), _("\
697 none == no stack or message will be printed.\n\
698 full == a message and a stack will be printed.\n\
699 message == an error message without a stack will be printed."),
700 NULL, NULL,
701 &set_guile_list, &show_guile_list);
702 }
703
704 /* Provide a prototype to silence -Wmissing-prototypes. */
705 extern initialize_file_ftype _initialize_guile;
706
707 void
708 _initialize_guile (void)
709 {
710 char *msg;
711
712 install_gdb_commands ();
713
714 #if HAVE_GUILE
715 /* The Python support puts the C side in module "_gdb", leaving the Python
716 side to define module "gdb" which imports "_gdb". There is evidently no
717 similar convention in Guile so we skip this. */
718
719 /* scm_with_guile is the most portable way to initialize Guile.
720 Plus we need to initialize the Guile support while in Guile mode
721 (e.g., called from within a call to scm_with_guile). */
722 scm_with_guile (call_initialize_gdb_module, NULL);
723
724 /* Set Guile's backtrace to match the "set guile print-stack" default.
725 [N.B. The two settings are still separate.]
726 But only do this after we've initialized Guile, it's nice to see a
727 backtrace if there's an error during initialization.
728 OTOH, if the error is that gdb/init.scm wasn't found because gdb is being
729 run from the build tree, the backtrace is more noise than signal.
730 Sigh. */
731 gdbscm_set_backtrace (0);
732 #endif
733 }