Commit | Line | Data |
---|---|---|
ed3ef339 DE |
1 | /* General GDB/Guile code. |
2 | ||
42a4f53d | 3 | Copyright (C) 2014-2019 Free Software Foundation, Inc. |
ed3ef339 DE |
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" | |
ed3ef339 DE |
24 | #include "breakpoint.h" |
25 | #include "cli/cli-cmds.h" | |
26 | #include "cli/cli-script.h" | |
27 | #include "cli/cli-utils.h" | |
28 | #include "command.h" | |
29 | #include "gdbcmd.h" | |
cb814510 | 30 | #include "top.h" |
ed3ef339 DE |
31 | #include "extension-priv.h" |
32 | #include "utils.h" | |
268a13a5 | 33 | #include "gdbsupport/version.h" |
ed3ef339 DE |
34 | #ifdef HAVE_GUILE |
35 | #include "guile.h" | |
36 | #include "guile-internal.h" | |
37 | #endif | |
92d8d229 | 38 | #include <signal.h> |
ed3ef339 | 39 | |
d2929fdc DE |
40 | /* The Guile version we're using. |
41 | We *could* use the macros in libguile/version.h but that would preclude | |
42 | handling the user switching in a different version with, e.g., | |
43 | LD_LIBRARY_PATH (using a different version than what gdb was compiled with | |
44 | is not something to be done lightly, but can be useful). */ | |
45 | int gdbscm_guile_major_version; | |
46 | int gdbscm_guile_minor_version; | |
47 | int gdbscm_guile_micro_version; | |
48 | ||
15766370 | 49 | #ifdef HAVE_GUILE |
d2929fdc DE |
50 | /* The guile subdirectory within gdb's data-directory. */ |
51 | static const char *guile_datadir; | |
15766370 | 52 | #endif |
d2929fdc | 53 | |
ed3ef339 DE |
54 | /* Declared constants and enum for guile exception printing. */ |
55 | const char gdbscm_print_excp_none[] = "none"; | |
56 | const char gdbscm_print_excp_full[] = "full"; | |
57 | const char gdbscm_print_excp_message[] = "message"; | |
58 | ||
59 | /* "set guile print-stack" choices. */ | |
60 | static const char *const guile_print_excp_enums[] = | |
61 | { | |
62 | gdbscm_print_excp_none, | |
63 | gdbscm_print_excp_full, | |
64 | gdbscm_print_excp_message, | |
65 | NULL | |
66 | }; | |
67 | ||
68 | /* The exception printing variable. 'full' if we want to print the | |
69 | error message and stack, 'none' if we want to print nothing, and | |
70 | 'message' if we only want to print the error message. 'message' is | |
71 | the default. */ | |
72 | const char *gdbscm_print_excp = gdbscm_print_excp_message; | |
73 | ||
ed3ef339 DE |
74 | \f |
75 | #ifdef HAVE_GUILE | |
76 | ||
77 | static void gdbscm_finish_initialization | |
78 | (const struct extension_language_defn *); | |
79 | static int gdbscm_initialized (const struct extension_language_defn *); | |
80 | static void gdbscm_eval_from_control_command | |
81 | (const struct extension_language_defn *, struct command_line *); | |
82 | static script_sourcer_func gdbscm_source_script; | |
83 | ||
84 | int gdb_scheme_initialized; | |
85 | ||
86 | /* Symbol for setting documentation strings. */ | |
87 | SCM gdbscm_documentation_symbol; | |
88 | ||
89 | /* Keywords used by various functions. */ | |
90 | static SCM from_tty_keyword; | |
91 | static SCM to_string_keyword; | |
92 | ||
93 | /* The name of the various modules (without the surrounding parens). */ | |
94 | const char gdbscm_module_name[] = "gdb"; | |
186fcde0 | 95 | const char gdbscm_init_module_name[] = "gdb"; |
ed3ef339 DE |
96 | |
97 | /* The name of the bootstrap file. */ | |
98 | static const char boot_scm_filename[] = "boot.scm"; | |
99 | ||
100 | /* The interface between gdb proper and loading of python scripts. */ | |
101 | ||
6a25e8a2 | 102 | static const struct extension_language_script_ops guile_extension_script_ops = |
ed3ef339 DE |
103 | { |
104 | gdbscm_source_script, | |
105 | gdbscm_source_objfile_script, | |
9f050062 | 106 | gdbscm_execute_objfile_script, |
ed3ef339 DE |
107 | gdbscm_auto_load_enabled |
108 | }; | |
109 | ||
110 | /* The interface between gdb proper and guile scripting. */ | |
111 | ||
6a25e8a2 | 112 | static const struct extension_language_ops guile_extension_ops = |
ed3ef339 DE |
113 | { |
114 | gdbscm_finish_initialization, | |
115 | gdbscm_initialized, | |
116 | ||
117 | gdbscm_eval_from_control_command, | |
118 | ||
119 | NULL, /* gdbscm_start_type_printers, */ | |
120 | NULL, /* gdbscm_apply_type_printers, */ | |
121 | NULL, /* gdbscm_free_type_printers, */ | |
122 | ||
123 | gdbscm_apply_val_pretty_printer, | |
124 | ||
125 | NULL, /* gdbscm_apply_frame_filter, */ | |
126 | ||
127 | gdbscm_preserve_values, | |
128 | ||
129 | gdbscm_breakpoint_has_cond, | |
130 | gdbscm_breakpoint_cond_says_stop, | |
131 | ||
132 | NULL, /* gdbscm_check_quit_flag, */ | |
ed3ef339 DE |
133 | NULL, /* gdbscm_set_quit_flag, */ |
134 | }; | |
6a25e8a2 CB |
135 | #endif |
136 | ||
137 | /* The main struct describing GDB's interface to the Guile | |
138 | extension language. */ | |
139 | extern const struct extension_language_defn extension_language_guile = | |
140 | { | |
141 | EXT_LANG_GUILE, | |
142 | "guile", | |
143 | "Guile", | |
144 | ||
145 | ".scm", | |
146 | "-gdb.scm", | |
ed3ef339 | 147 | |
6a25e8a2 CB |
148 | guile_control, |
149 | ||
150 | #ifdef HAVE_GUILE | |
151 | &guile_extension_script_ops, | |
152 | &guile_extension_ops | |
153 | #else | |
154 | NULL, | |
155 | NULL | |
156 | #endif | |
157 | }; | |
158 | ||
159 | #ifdef HAVE_GUILE | |
ed3ef339 DE |
160 | /* Implementation of the gdb "guile-repl" command. */ |
161 | ||
162 | static void | |
0b39b52e | 163 | guile_repl_command (const char *arg, int from_tty) |
ed3ef339 | 164 | { |
156d9eab | 165 | scoped_restore restore_async = make_scoped_restore (¤t_ui->async, 0); |
ed3ef339 DE |
166 | |
167 | arg = skip_spaces (arg); | |
168 | ||
169 | /* This explicitly rejects any arguments for now. | |
170 | "It is easier to relax a restriction than impose one after the fact." | |
171 | We would *like* to be able to pass arguments to the interactive shell | |
172 | but that's not what python-interactive does. Until there is time to | |
173 | sort it out, we forbid arguments. */ | |
174 | ||
175 | if (arg && *arg) | |
176 | error (_("guile-repl currently does not take any arguments.")); | |
177 | else | |
178 | { | |
179 | dont_repeat (); | |
180 | gdbscm_enter_repl (); | |
181 | } | |
ed3ef339 DE |
182 | } |
183 | ||
184 | /* Implementation of the gdb "guile" command. | |
185 | Note: Contrary to the Python version this displays the result. | |
186 | Have to see which is better. | |
187 | ||
188 | TODO: Add the result to Guile's history? */ | |
189 | ||
190 | static void | |
0b39b52e | 191 | guile_command (const char *arg, int from_tty) |
ed3ef339 | 192 | { |
156d9eab | 193 | scoped_restore restore_async = make_scoped_restore (¤t_ui->async, 0); |
ed3ef339 DE |
194 | |
195 | arg = skip_spaces (arg); | |
196 | ||
197 | if (arg && *arg) | |
198 | { | |
a1a31cb8 | 199 | gdb::unique_xmalloc_ptr<char> msg = gdbscm_safe_eval_string (arg, 1); |
ed3ef339 DE |
200 | |
201 | if (msg != NULL) | |
a1a31cb8 | 202 | error ("%s", msg.get ()); |
ed3ef339 DE |
203 | } |
204 | else | |
205 | { | |
12973681 | 206 | counted_command_line l = get_command_line (guile_control, ""); |
ed3ef339 | 207 | |
93921405 | 208 | execute_control_command_untraced (l.get ()); |
ed3ef339 | 209 | } |
ed3ef339 DE |
210 | } |
211 | ||
212 | /* Given a command_line, return a command string suitable for passing | |
213 | to Guile. Lines in the string are separated by newlines. The return | |
214 | value is allocated using xmalloc and the caller is responsible for | |
215 | freeing it. */ | |
216 | ||
217 | static char * | |
218 | compute_scheme_string (struct command_line *l) | |
219 | { | |
220 | struct command_line *iter; | |
221 | char *script = NULL; | |
222 | int size = 0; | |
223 | int here; | |
224 | ||
225 | for (iter = l; iter; iter = iter->next) | |
226 | size += strlen (iter->line) + 1; | |
227 | ||
224c3ddb | 228 | script = (char *) xmalloc (size + 1); |
ed3ef339 DE |
229 | here = 0; |
230 | for (iter = l; iter; iter = iter->next) | |
231 | { | |
232 | int len = strlen (iter->line); | |
233 | ||
234 | strcpy (&script[here], iter->line); | |
235 | here += len; | |
236 | script[here++] = '\n'; | |
237 | } | |
238 | script[here] = '\0'; | |
239 | return script; | |
240 | } | |
241 | ||
242 | /* Take a command line structure representing a "guile" command, and | |
243 | evaluate its body using the Guile interpreter. | |
244 | This is the extension_language_ops.eval_from_control_command "method". */ | |
245 | ||
246 | static void | |
247 | gdbscm_eval_from_control_command | |
248 | (const struct extension_language_defn *extlang, struct command_line *cmd) | |
249 | { | |
a1a31cb8 | 250 | char *script; |
ed3ef339 | 251 | |
12973681 | 252 | if (cmd->body_list_1 != nullptr) |
ed3ef339 DE |
253 | error (_("Invalid \"guile\" block structure.")); |
254 | ||
12973681 | 255 | script = compute_scheme_string (cmd->body_list_0.get ()); |
a1a31cb8 | 256 | gdb::unique_xmalloc_ptr<char> msg = gdbscm_safe_eval_string (script, 0); |
ed3ef339 DE |
257 | xfree (script); |
258 | if (msg != NULL) | |
a1a31cb8 | 259 | error ("%s", msg.get ()); |
ed3ef339 DE |
260 | } |
261 | ||
262 | /* Read a file as Scheme code. | |
263 | This is the extension_language_script_ops.script_sourcer "method". | |
264 | FILE is the file to run. FILENAME is name of the file FILE. | |
265 | This does not throw any errors. If an exception occurs an error message | |
266 | is printed. */ | |
267 | ||
268 | static void | |
269 | gdbscm_source_script (const struct extension_language_defn *extlang, | |
270 | FILE *file, const char *filename) | |
271 | { | |
272 | char *msg = gdbscm_safe_source_script (filename); | |
273 | ||
274 | if (msg != NULL) | |
275 | { | |
276 | fprintf_filtered (gdb_stderr, "%s\n", msg); | |
277 | xfree (msg); | |
278 | } | |
279 | } | |
280 | \f | |
0c3abbc7 | 281 | /* (execute string [#:from-tty boolean] [#:to-string boolean]) |
ed3ef339 DE |
282 | A Scheme function which evaluates a string using the gdb CLI. */ |
283 | ||
284 | static SCM | |
285 | gdbscm_execute_gdb_command (SCM command_scm, SCM rest) | |
286 | { | |
287 | int from_tty_arg_pos = -1, to_string_arg_pos = -1; | |
288 | int from_tty = 0, to_string = 0; | |
ed3ef339 DE |
289 | const SCM keywords[] = { from_tty_keyword, to_string_keyword, SCM_BOOL_F }; |
290 | char *command; | |
ed3ef339 DE |
291 | |
292 | gdbscm_parse_function_args (FUNC_NAME, SCM_ARG1, keywords, "s#tt", | |
293 | command_scm, &command, rest, | |
294 | &from_tty_arg_pos, &from_tty, | |
295 | &to_string_arg_pos, &to_string); | |
296 | ||
3a5f2a48 | 297 | return gdbscm_wrap ([=] |
ed3ef339 | 298 | { |
3a5f2a48 PA |
299 | gdb::unique_xmalloc_ptr<char> command_holder (command); |
300 | std::string to_string_res; | |
301 | ||
156d9eab TT |
302 | scoped_restore restore_async = make_scoped_restore (¤t_ui->async, |
303 | 0); | |
ed3ef339 | 304 | |
1ac32117 | 305 | scoped_restore preventer = prevent_dont_repeat (); |
ed3ef339 | 306 | if (to_string) |
fa731fa0 | 307 | to_string_res = execute_command_to_string (command, from_tty, false); |
ed3ef339 | 308 | else |
db1ec11f | 309 | execute_command (command, from_tty); |
ed3ef339 DE |
310 | |
311 | /* Do any commands attached to breakpoint we stopped at. */ | |
312 | bpstat_do_actions (); | |
492d29ea | 313 | |
3a5f2a48 PA |
314 | if (to_string) |
315 | return gdbscm_scm_from_c_string (to_string_res.c_str ()); | |
316 | return SCM_UNSPECIFIED; | |
317 | }); | |
ed3ef339 DE |
318 | } |
319 | ||
320 | /* (data-directory) -> string */ | |
321 | ||
322 | static SCM | |
323 | gdbscm_data_directory (void) | |
324 | { | |
f2aec7f6 | 325 | return gdbscm_scm_from_c_string (gdb_datadir.c_str ()); |
ed3ef339 DE |
326 | } |
327 | ||
d2929fdc DE |
328 | /* (guile-data-directory) -> string */ |
329 | ||
330 | static SCM | |
331 | gdbscm_guile_data_directory (void) | |
332 | { | |
333 | return gdbscm_scm_from_c_string (guile_datadir); | |
334 | } | |
335 | ||
ed3ef339 DE |
336 | /* (gdb-version) -> string */ |
337 | ||
338 | static SCM | |
339 | gdbscm_gdb_version (void) | |
340 | { | |
341 | return gdbscm_scm_from_c_string (version); | |
342 | } | |
343 | ||
344 | /* (host-config) -> string */ | |
345 | ||
346 | static SCM | |
347 | gdbscm_host_config (void) | |
348 | { | |
349 | return gdbscm_scm_from_c_string (host_name); | |
350 | } | |
351 | ||
352 | /* (target-config) -> string */ | |
353 | ||
354 | static SCM | |
355 | gdbscm_target_config (void) | |
356 | { | |
357 | return gdbscm_scm_from_c_string (target_name); | |
358 | } | |
359 | ||
360 | #else /* ! HAVE_GUILE */ | |
361 | ||
362 | /* Dummy implementation of the gdb "guile-repl" and "guile" | |
363 | commands. */ | |
364 | ||
365 | static void | |
0b39b52e | 366 | guile_repl_command (const char *arg, int from_tty) |
ed3ef339 DE |
367 | { |
368 | arg = skip_spaces (arg); | |
369 | if (arg && *arg) | |
370 | error (_("guile-repl currently does not take any arguments.")); | |
371 | error (_("Guile scripting is not supported in this copy of GDB.")); | |
372 | } | |
373 | ||
374 | static void | |
0b39b52e | 375 | guile_command (const char *arg, int from_tty) |
ed3ef339 DE |
376 | { |
377 | arg = skip_spaces (arg); | |
378 | if (arg && *arg) | |
379 | error (_("Guile scripting is not supported in this copy of GDB.")); | |
380 | else | |
381 | { | |
382 | /* Even if Guile isn't enabled, we still have to slurp the | |
383 | command list to the corresponding "end". */ | |
12973681 | 384 | counted_command_line l = get_command_line (guile_control, ""); |
ed3ef339 | 385 | |
93921405 | 386 | execute_control_command_untraced (l.get ()); |
ed3ef339 DE |
387 | } |
388 | } | |
389 | ||
390 | #endif /* ! HAVE_GUILE */ | |
391 | \f | |
392 | /* Lists for 'set,show,info guile' commands. */ | |
393 | ||
394 | static struct cmd_list_element *set_guile_list; | |
395 | static struct cmd_list_element *show_guile_list; | |
396 | static struct cmd_list_element *info_guile_list; | |
397 | ||
398 | /* Function for use by 'set guile' prefix command. */ | |
399 | ||
400 | static void | |
981a3fb3 | 401 | set_guile_command (const char *args, int from_tty) |
ed3ef339 DE |
402 | { |
403 | help_list (set_guile_list, "set guile ", all_commands, gdb_stdout); | |
404 | } | |
405 | ||
406 | /* Function for use by 'show guile' prefix command. */ | |
407 | ||
408 | static void | |
981a3fb3 | 409 | show_guile_command (const char *args, int from_tty) |
ed3ef339 DE |
410 | { |
411 | cmd_show_list (show_guile_list, from_tty, ""); | |
412 | } | |
413 | ||
414 | /* The "info scheme" command is defined as a prefix, with | |
415 | allow_unknown 0. Therefore, its own definition is called only for | |
416 | "info scheme" with no args. */ | |
417 | ||
418 | static void | |
981a3fb3 | 419 | info_guile_command (const char *args, int from_tty) |
ed3ef339 DE |
420 | { |
421 | printf_unfiltered (_("\"info guile\" must be followed" | |
422 | " by the name of an info command.\n")); | |
635c7e8a | 423 | help_list (info_guile_list, "info guile ", all_commands, gdb_stdout); |
ed3ef339 DE |
424 | } |
425 | \f | |
426 | /* Initialization. */ | |
427 | ||
428 | #ifdef HAVE_GUILE | |
429 | ||
430 | static const scheme_function misc_guile_functions[] = | |
431 | { | |
72e02483 | 432 | { "execute", 1, 0, 1, as_a_scm_t_subr (gdbscm_execute_gdb_command), |
ed3ef339 DE |
433 | "\ |
434 | Execute the given GDB command.\n\ | |
435 | \n\ | |
436 | Arguments: string [#:to-string boolean] [#:from-tty boolean]\n\ | |
437 | If #:from-tty is true then the command executes as if entered\n\ | |
438 | from the keyboard. The default is false (#f).\n\ | |
439 | If #:to-string is true then the result is returned as a string.\n\ | |
440 | Otherwise output is sent to the current output port,\n\ | |
441 | which is the default.\n\ | |
442 | Returns: The result of the command if #:to-string is true.\n\ | |
443 | Otherwise returns unspecified." }, | |
444 | ||
72e02483 | 445 | { "data-directory", 0, 0, 0, as_a_scm_t_subr (gdbscm_data_directory), |
ed3ef339 DE |
446 | "\ |
447 | Return the name of GDB's data directory." }, | |
448 | ||
72e02483 PA |
449 | { "guile-data-directory", 0, 0, 0, |
450 | as_a_scm_t_subr (gdbscm_guile_data_directory), | |
d2929fdc DE |
451 | "\ |
452 | Return the name of the Guile directory within GDB's data directory." }, | |
453 | ||
72e02483 | 454 | { "gdb-version", 0, 0, 0, as_a_scm_t_subr (gdbscm_gdb_version), |
ed3ef339 DE |
455 | "\ |
456 | Return GDB's version string." }, | |
457 | ||
72e02483 | 458 | { "host-config", 0, 0, 0, as_a_scm_t_subr (gdbscm_host_config), |
ed3ef339 DE |
459 | "\ |
460 | Return the name of the host configuration." }, | |
461 | ||
72e02483 | 462 | { "target-config", 0, 0, 0, as_a_scm_t_subr (gdbscm_target_config), |
ed3ef339 DE |
463 | "\ |
464 | Return the name of the target configuration." }, | |
465 | ||
466 | END_FUNCTIONS | |
467 | }; | |
468 | ||
e76c5d17 DE |
469 | /* Load BOOT_SCM_FILE, the first Scheme file that gets loaded. */ |
470 | ||
471 | static SCM | |
472 | boot_guile_support (void *boot_scm_file) | |
473 | { | |
474 | /* Load boot.scm without compiling it (there's no need to compile it). | |
475 | The other files should have been compiled already, and boot.scm is | |
476 | expected to adjust '%load-compiled-path' accordingly. If they haven't | |
477 | been compiled, Guile will auto-compile them. The important thing to keep | |
478 | in mind is that there's a >= 100x speed difference between compiled and | |
479 | non-compiled files. */ | |
480 | return scm_c_primitive_load ((const char *) boot_scm_file); | |
481 | } | |
482 | ||
483 | /* Return non-zero if ARGS has the "standard" format for throw args. | |
484 | The standard format is: | |
485 | (function format-string (format-string-args-list) ...). | |
486 | FUNCTION is #f if no function was recorded. */ | |
487 | ||
488 | static int | |
489 | standard_throw_args_p (SCM args) | |
490 | { | |
491 | if (gdbscm_is_true (scm_list_p (args)) | |
492 | && scm_ilength (args) >= 3) | |
493 | { | |
494 | /* The function in which the error occurred. */ | |
495 | SCM arg0 = scm_list_ref (args, scm_from_int (0)); | |
496 | /* The format string. */ | |
497 | SCM arg1 = scm_list_ref (args, scm_from_int (1)); | |
498 | /* The arguments of the format string. */ | |
499 | SCM arg2 = scm_list_ref (args, scm_from_int (2)); | |
500 | ||
501 | if ((scm_is_string (arg0) || gdbscm_is_false (arg0)) | |
502 | && scm_is_string (arg1) | |
503 | && gdbscm_is_true (scm_list_p (arg2))) | |
504 | return 1; | |
505 | } | |
506 | ||
507 | return 0; | |
508 | } | |
509 | ||
510 | /* Print the error recorded in a "standard" throw args. */ | |
511 | ||
512 | static void | |
513 | print_standard_throw_error (SCM args) | |
514 | { | |
515 | /* The function in which the error occurred. */ | |
516 | SCM arg0 = scm_list_ref (args, scm_from_int (0)); | |
517 | /* The format string. */ | |
518 | SCM arg1 = scm_list_ref (args, scm_from_int (1)); | |
519 | /* The arguments of the format string. */ | |
520 | SCM arg2 = scm_list_ref (args, scm_from_int (2)); | |
521 | ||
522 | /* ARG0 is #f if no function was recorded. */ | |
523 | if (gdbscm_is_true (arg0)) | |
524 | { | |
525 | scm_simple_format (scm_current_error_port (), | |
526 | scm_from_latin1_string (_("Error in function ~s:~%")), | |
527 | scm_list_1 (arg0)); | |
528 | } | |
529 | scm_simple_format (scm_current_error_port (), arg1, arg2); | |
530 | } | |
531 | ||
532 | /* Print the error message recorded in KEY, ARGS, the arguments to throw. | |
533 | Normally we let Scheme print the error message. | |
534 | This function is used when Scheme initialization fails. | |
535 | We can still use the Scheme C API though. */ | |
536 | ||
537 | static void | |
538 | print_throw_error (SCM key, SCM args) | |
539 | { | |
540 | /* IWBN to call gdbscm_print_exception_with_stack here, but Guile didn't | |
541 | boot successfully so play it safe and avoid it. The "format string" and | |
542 | its args are embedded in ARGS, but the content of ARGS depends on KEY. | |
543 | Make sure ARGS has the expected canonical content before trying to use | |
544 | it. */ | |
545 | if (standard_throw_args_p (args)) | |
546 | print_standard_throw_error (args); | |
547 | else | |
548 | { | |
549 | scm_simple_format (scm_current_error_port (), | |
550 | scm_from_latin1_string (_("Throw to key `~a' with args `~s'.~%")), | |
551 | scm_list_2 (key, args)); | |
552 | } | |
553 | } | |
554 | ||
555 | /* Handle an exception thrown while loading BOOT_SCM_FILE. */ | |
556 | ||
557 | static SCM | |
558 | handle_boot_error (void *boot_scm_file, SCM key, SCM args) | |
559 | { | |
560 | fprintf_unfiltered (gdb_stderr, ("Exception caught while booting Guile.\n")); | |
561 | ||
562 | print_throw_error (key, args); | |
563 | ||
564 | fprintf_unfiltered (gdb_stderr, "\n"); | |
565 | warning (_("Could not complete Guile gdb module initialization from:\n" | |
566 | "%s.\n" | |
567 | "Limited Guile support is available.\n" | |
422186a9 | 568 | "Suggest passing --data-directory=/path/to/gdb/data-directory."), |
e76c5d17 DE |
569 | (const char *) boot_scm_file); |
570 | ||
571 | return SCM_UNSPECIFIED; | |
572 | } | |
573 | ||
ed3ef339 DE |
574 | /* Load gdb/boot.scm, the Scheme side of GDB/Guile support. |
575 | Note: This function assumes it's called within the gdb module. */ | |
576 | ||
577 | static void | |
578 | initialize_scheme_side (void) | |
579 | { | |
d2929fdc | 580 | char *boot_scm_path; |
ed3ef339 | 581 | |
f2aec7f6 CB |
582 | guile_datadir = concat (gdb_datadir.c_str (), SLASH_STRING, "guile", |
583 | (char *) NULL); | |
d2929fdc | 584 | boot_scm_path = concat (guile_datadir, SLASH_STRING, "gdb", |
b36cec19 | 585 | SLASH_STRING, boot_scm_filename, (char *) NULL); |
d2929fdc | 586 | |
e76c5d17 DE |
587 | scm_c_catch (SCM_BOOL_T, boot_guile_support, boot_scm_path, |
588 | handle_boot_error, boot_scm_path, NULL, NULL); | |
ed3ef339 | 589 | |
ed3ef339 DE |
590 | xfree (boot_scm_path); |
591 | } | |
592 | ||
593 | /* Install the gdb scheme module. | |
594 | The result is a boolean indicating success. | |
595 | If initializing the gdb module fails an error message is printed. | |
596 | Note: This function runs in the context of the gdb module. */ | |
597 | ||
598 | static void | |
599 | initialize_gdb_module (void *data) | |
600 | { | |
d2929fdc DE |
601 | /* Computing these is a pain, so only do it once. |
602 | Also, do it here and save the result so that obtaining the values | |
603 | is thread-safe. */ | |
604 | gdbscm_guile_major_version = gdbscm_scm_string_to_int (scm_major_version ()); | |
605 | gdbscm_guile_minor_version = gdbscm_scm_string_to_int (scm_minor_version ()); | |
606 | gdbscm_guile_micro_version = gdbscm_scm_string_to_int (scm_micro_version ()); | |
607 | ||
ed3ef339 DE |
608 | /* The documentation symbol needs to be defined before any calls to |
609 | gdbscm_define_{variables,functions}. */ | |
610 | gdbscm_documentation_symbol = scm_from_latin1_symbol ("documentation"); | |
611 | ||
612 | /* The smob and exception support must be initialized early. */ | |
613 | gdbscm_initialize_smobs (); | |
614 | gdbscm_initialize_exceptions (); | |
615 | ||
616 | /* The rest are initialized in alphabetical order. */ | |
617 | gdbscm_initialize_arches (); | |
618 | gdbscm_initialize_auto_load (); | |
619 | gdbscm_initialize_blocks (); | |
620 | gdbscm_initialize_breakpoints (); | |
e698b8c4 | 621 | gdbscm_initialize_commands (); |
ed3ef339 DE |
622 | gdbscm_initialize_disasm (); |
623 | gdbscm_initialize_frames (); | |
624 | gdbscm_initialize_iterators (); | |
625 | gdbscm_initialize_lazy_strings (); | |
626 | gdbscm_initialize_math (); | |
627 | gdbscm_initialize_objfiles (); | |
06eb1586 | 628 | gdbscm_initialize_parameters (); |
ed3ef339 DE |
629 | gdbscm_initialize_ports (); |
630 | gdbscm_initialize_pretty_printers (); | |
ded03782 | 631 | gdbscm_initialize_pspaces (); |
ed3ef339 DE |
632 | gdbscm_initialize_strings (); |
633 | gdbscm_initialize_symbols (); | |
634 | gdbscm_initialize_symtabs (); | |
635 | gdbscm_initialize_types (); | |
636 | gdbscm_initialize_values (); | |
637 | ||
638 | gdbscm_define_functions (misc_guile_functions, 1); | |
639 | ||
640 | from_tty_keyword = scm_from_latin1_keyword ("from-tty"); | |
641 | to_string_keyword = scm_from_latin1_keyword ("to-string"); | |
642 | ||
643 | initialize_scheme_side (); | |
644 | ||
645 | gdb_scheme_initialized = 1; | |
646 | } | |
647 | ||
c1966e26 DE |
648 | /* Utility to call scm_c_define_module+initialize_gdb_module from |
649 | within scm_with_guile. */ | |
650 | ||
651 | static void * | |
652 | call_initialize_gdb_module (void *data) | |
653 | { | |
654 | /* Most of the initialization is done by initialize_gdb_module. | |
655 | It is called via scm_c_define_module so that the initialization is | |
656 | performed within the desired module. */ | |
657 | scm_c_define_module (gdbscm_module_name, initialize_gdb_module, NULL); | |
658 | ||
92fab5a6 AW |
659 | #if HAVE_GUILE_MANUAL_FINALIZATION |
660 | scm_run_finalizers (); | |
661 | #endif | |
662 | ||
c1966e26 DE |
663 | return NULL; |
664 | } | |
665 | ||
ed3ef339 DE |
666 | /* A callback to finish Guile initialization after gdb has finished all its |
667 | initialization. | |
668 | This is the extension_language_ops.finish_initialization "method". */ | |
669 | ||
670 | static void | |
671 | gdbscm_finish_initialization (const struct extension_language_defn *extlang) | |
672 | { | |
673 | /* Restore the environment to the user interaction one. */ | |
674 | scm_set_current_module (scm_interaction_environment ()); | |
675 | } | |
676 | ||
677 | /* The extension_language_ops.initialized "method". */ | |
678 | ||
679 | static int | |
680 | gdbscm_initialized (const struct extension_language_defn *extlang) | |
681 | { | |
682 | return gdb_scheme_initialized; | |
683 | } | |
684 | ||
685 | /* Enable or disable Guile backtraces. */ | |
686 | ||
687 | static void | |
688 | gdbscm_set_backtrace (int enable) | |
689 | { | |
690 | static const char disable_bt[] = "(debug-disable 'backtrace)"; | |
691 | static const char enable_bt[] = "(debug-enable 'backtrace)"; | |
692 | ||
693 | if (enable) | |
694 | gdbscm_safe_eval_string (enable_bt, 0); | |
695 | else | |
696 | gdbscm_safe_eval_string (disable_bt, 0); | |
697 | } | |
698 | ||
699 | #endif /* HAVE_GUILE */ | |
700 | ||
8588b356 SM |
701 | /* See guile.h. */ |
702 | cmd_list_element *guile_cmd_element = nullptr; | |
703 | ||
ed3ef339 DE |
704 | /* Install the various gdb commands used by Guile. */ |
705 | ||
706 | static void | |
707 | install_gdb_commands (void) | |
708 | { | |
709 | add_com ("guile-repl", class_obscure, | |
710 | guile_repl_command, | |
711 | #ifdef HAVE_GUILE | |
712 | _("\ | |
713 | Start an interactive Guile prompt.\n\ | |
714 | \n\ | |
715 | To return to GDB, type the EOF character (e.g., Ctrl-D on an empty\n\ | |
716 | prompt) or ,quit.") | |
717 | #else /* HAVE_GUILE */ | |
718 | _("\ | |
719 | Start a Guile interactive prompt.\n\ | |
720 | \n\ | |
721 | Guile scripting is not supported in this copy of GDB.\n\ | |
722 | This command is only a placeholder.") | |
723 | #endif /* HAVE_GUILE */ | |
724 | ); | |
725 | add_com_alias ("gr", "guile-repl", class_obscure, 1); | |
726 | ||
727 | /* Since "help guile" is easy to type, and intuitive, we add general help | |
728 | in using GDB+Guile to this command. */ | |
8588b356 | 729 | guile_cmd_element = add_com ("guile", class_obscure, guile_command, |
ed3ef339 DE |
730 | #ifdef HAVE_GUILE |
731 | _("\ | |
732 | Evaluate one or more Guile expressions.\n\ | |
733 | \n\ | |
734 | The expression(s) can be given as an argument, for instance:\n\ | |
735 | \n\ | |
736 | guile (display 23)\n\ | |
737 | \n\ | |
738 | The result of evaluating the last expression is printed.\n\ | |
739 | \n\ | |
740 | If no argument is given, the following lines are read and passed\n\ | |
741 | to Guile for evaluation. Type a line containing \"end\" to indicate\n\ | |
742 | the end of the set of expressions.\n\ | |
743 | \n\ | |
744 | The Guile GDB module must first be imported before it can be used.\n\ | |
745 | Do this with:\n\ | |
746 | (gdb) guile (use-modules (gdb))\n\ | |
747 | or if you want to import the (gdb) module with a prefix, use:\n\ | |
748 | (gdb) guile (use-modules ((gdb) #:renamer (symbol-prefix-proc 'gdb:)))\n\ | |
749 | \n\ | |
750 | The Guile interactive session, started with the \"guile-repl\"\n\ | |
751 | command, provides extensive help and apropos capabilities.\n\ | |
752 | Type \",help\" once in a Guile interactive session.") | |
753 | #else /* HAVE_GUILE */ | |
754 | _("\ | |
755 | Evaluate a Guile expression.\n\ | |
756 | \n\ | |
757 | Guile scripting is not supported in this copy of GDB.\n\ | |
758 | This command is only a placeholder.") | |
759 | #endif /* HAVE_GUILE */ | |
760 | ); | |
761 | add_com_alias ("gu", "guile", class_obscure, 1); | |
762 | ||
763 | add_prefix_cmd ("guile", class_obscure, set_guile_command, | |
764 | _("Prefix command for Guile preference settings."), | |
765 | &set_guile_list, "set guile ", 0, | |
766 | &setlist); | |
767 | add_alias_cmd ("gu", "guile", class_obscure, 1, &setlist); | |
768 | ||
769 | add_prefix_cmd ("guile", class_obscure, show_guile_command, | |
770 | _("Prefix command for Guile preference settings."), | |
771 | &show_guile_list, "show guile ", 0, | |
772 | &showlist); | |
773 | add_alias_cmd ("gu", "guile", class_obscure, 1, &showlist); | |
774 | ||
775 | add_prefix_cmd ("guile", class_obscure, info_guile_command, | |
776 | _("Prefix command for Guile info displays."), | |
777 | &info_guile_list, "info guile ", 0, | |
778 | &infolist); | |
779 | add_info_alias ("gu", "guile", 1); | |
780 | ||
781 | /* The name "print-stack" is carried over from Python. | |
782 | A better name is "print-exception". */ | |
783 | add_setshow_enum_cmd ("print-stack", no_class, guile_print_excp_enums, | |
784 | &gdbscm_print_excp, _("\ | |
785 | Set mode for Guile exception printing on error."), _("\ | |
786 | Show the mode of Guile exception printing on error."), _("\ | |
787 | none == no stack or message will be printed.\n\ | |
788 | full == a message and a stack will be printed.\n\ | |
789 | message == an error message without a stack will be printed."), | |
790 | NULL, NULL, | |
791 | &set_guile_list, &show_guile_list); | |
792 | } | |
793 | ||
ed3ef339 DE |
794 | void |
795 | _initialize_guile (void) | |
796 | { | |
ed3ef339 DE |
797 | install_gdb_commands (); |
798 | ||
799 | #if HAVE_GUILE | |
92d8d229 DE |
800 | { |
801 | #ifdef HAVE_SIGPROCMASK | |
802 | sigset_t sigchld_mask, prev_mask; | |
74edf516 | 803 | #endif |
92d8d229 DE |
804 | |
805 | /* The Python support puts the C side in module "_gdb", leaving the Python | |
806 | side to define module "gdb" which imports "_gdb". There is evidently no | |
807 | similar convention in Guile so we skip this. */ | |
808 | ||
92fab5a6 AW |
809 | #if HAVE_GUILE_MANUAL_FINALIZATION |
810 | /* Our SMOB free functions are not thread-safe, as GDB itself is not | |
811 | intended to be thread-safe. Disable automatic finalization so that | |
812 | finalizers aren't run in other threads. */ | |
813 | scm_set_automatic_finalization_enabled (0); | |
814 | #endif | |
815 | ||
92d8d229 DE |
816 | #ifdef HAVE_SIGPROCMASK |
817 | /* Before we initialize Guile, block SIGCHLD. | |
818 | This is done so that all threads created during Guile initialization | |
819 | have SIGCHLD blocked. PR 17247. | |
820 | Really libgc and Guile should do this, but we need to work with | |
821 | libgc 7.4.x. */ | |
822 | sigemptyset (&sigchld_mask); | |
823 | sigaddset (&sigchld_mask, SIGCHLD); | |
824 | sigprocmask (SIG_BLOCK, &sigchld_mask, &prev_mask); | |
825 | #endif | |
826 | ||
827 | /* scm_with_guile is the most portable way to initialize Guile. | |
828 | Plus we need to initialize the Guile support while in Guile mode | |
829 | (e.g., called from within a call to scm_with_guile). */ | |
830 | scm_with_guile (call_initialize_gdb_module, NULL); | |
831 | ||
832 | #ifdef HAVE_SIGPROCMASK | |
833 | sigprocmask (SIG_SETMASK, &prev_mask, NULL); | |
74edf516 DE |
834 | #endif |
835 | ||
92d8d229 DE |
836 | /* Set Guile's backtrace to match the "set guile print-stack" default. |
837 | [N.B. The two settings are still separate.] | |
838 | But only do this after we've initialized Guile, it's nice to see a | |
839 | backtrace if there's an error during initialization. | |
840 | OTOH, if the error is that gdb/init.scm wasn't found because gdb is | |
841 | being run from the build tree, the backtrace is more noise than signal. | |
842 | Sigh. */ | |
843 | gdbscm_set_backtrace (0); | |
844 | } | |
ed3ef339 DE |
845 | #endif |
846 | } |