C++: dlsym casts in gdb/linux-thread-db.c and gdb/gdbserver/thread-db.c
[deliverable/binutils-gdb.git] / gdb / auto-load.c
CommitLineData
e2207b9a
JK
1/* GDB routines for supporting auto-loaded scripts.
2
32d0add0 3 Copyright (C) 2012-2015 Free Software Foundation, Inc.
e2207b9a
JK
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#include "defs.h"
9f050062 21#include <ctype.h>
e2207b9a
JK
22#include "auto-load.h"
23#include "progspace.h"
e2207b9a
JK
24#include "gdb_regex.h"
25#include "ui-out.h"
26#include "filenames.h"
27#include "command.h"
28#include "observer.h"
29#include "objfiles.h"
bf88dd68
JK
30#include "cli/cli-script.h"
31#include "gdbcmd.h"
5b2bf947 32#include "cli/cli-cmds.h"
bf88dd68
JK
33#include "cli/cli-decode.h"
34#include "cli/cli-setshow.h"
bccbefd2
JK
35#include "gdb_vecs.h"
36#include "readline/tilde.h"
37#include "completer.h"
202cbf1c 38#include "fnmatch.h"
9a950c7c 39#include "top.h"
614c279d 40#include "filestuff.h"
6dddc817 41#include "extension.h"
ed3ef339 42#include "gdb/section-scripts.h"
bf88dd68 43
5b2bf947
DE
44/* The section to look in for auto-loaded scripts (in file formats that
45 support sections).
46 Each entry in this section is a record that begins with a leading byte
47 identifying the record type.
48 At the moment we only support one record type: A leading byte of 1,
49 followed by the path of a python script to load. */
50#define AUTO_SECTION_NAME ".debug_gdb_scripts"
51
9f050062
DE
52static void maybe_print_unsupported_script_warning
53 (struct auto_load_pspace_info *, struct objfile *objfile,
54 const struct extension_language_defn *language,
55 const char *section_name, unsigned offset);
6dddc817 56
9f050062
DE
57static void maybe_print_script_not_found_warning
58 (struct auto_load_pspace_info *, struct objfile *objfile,
59 const struct extension_language_defn *language,
60 const char *section_name, unsigned offset);
bf88dd68 61
4dc84fd1
JK
62/* Value of the 'set debug auto-load' configuration variable. */
63static int debug_auto_load = 0;
64
65/* "show" command for the debug_auto_load configuration variable. */
66
67static void
68show_debug_auto_load (struct ui_file *file, int from_tty,
69 struct cmd_list_element *c, const char *value)
70{
71 fprintf_filtered (file, _("Debugging output for files "
72 "of 'set auto-load ...' is %s.\n"),
73 value);
74}
75
bf88dd68
JK
76/* User-settable option to enable/disable auto-loading of GDB_AUTO_FILE_NAME
77 scripts:
78 set auto-load gdb-scripts on|off
79 This is true if we should auto-load associated scripts when an objfile
80 is opened, false otherwise. */
81static int auto_load_gdb_scripts = 1;
82
83/* "show" command for the auto_load_gdb_scripts configuration variable. */
84
85static void
86show_auto_load_gdb_scripts (struct ui_file *file, int from_tty,
87 struct cmd_list_element *c, const char *value)
88{
89 fprintf_filtered (file, _("Auto-loading of canned sequences of commands "
90 "scripts is %s.\n"),
91 value);
92}
e2207b9a 93
5b2bf947
DE
94/* Return non-zero if auto-loading gdb scripts is enabled. */
95
6dddc817
DE
96int
97auto_load_gdb_scripts_enabled (const struct extension_language_defn *extlang)
5b2bf947
DE
98{
99 return auto_load_gdb_scripts;
100}
101
e2207b9a
JK
102/* Internal-use flag to enable/disable auto-loading.
103 This is true if we should auto-load python code when an objfile is opened,
104 false otherwise.
105
bf88dd68 106 Both auto_load_scripts && global_auto_load must be true to enable
e2207b9a
JK
107 auto-loading.
108
109 This flag exists to facilitate deferring auto-loading during start-up
110 until after ./.gdbinit has been read; it may augment the search directories
111 used to find the scripts. */
bf88dd68
JK
112int global_auto_load = 1;
113
114/* Auto-load .gdbinit file from the current directory? */
115int auto_load_local_gdbinit = 1;
116
117/* Absolute pathname to the current directory .gdbinit, if it exists. */
118char *auto_load_local_gdbinit_pathname = NULL;
119
120/* Boolean value if AUTO_LOAD_LOCAL_GDBINIT_PATHNAME has been loaded. */
121int auto_load_local_gdbinit_loaded = 0;
122
123/* "show" command for the auto_load_local_gdbinit configuration variable. */
124
125static void
126show_auto_load_local_gdbinit (struct ui_file *file, int from_tty,
127 struct cmd_list_element *c, const char *value)
128{
129 fprintf_filtered (file, _("Auto-loading of .gdbinit script from current "
130 "directory is %s.\n"),
131 value);
132}
133
7349ff92
JK
134/* Directory list from which to load auto-loaded scripts. It is not checked
135 for absolute paths but they are strongly recommended. It is initialized by
136 _initialize_auto_load. */
137static char *auto_load_dir;
138
139/* "set" command for the auto_load_dir configuration variable. */
140
141static void
142set_auto_load_dir (char *args, int from_tty, struct cmd_list_element *c)
143{
144 /* Setting the variable to "" resets it to the compile time defaults. */
145 if (auto_load_dir[0] == '\0')
146 {
147 xfree (auto_load_dir);
148 auto_load_dir = xstrdup (AUTO_LOAD_DIR);
149 }
150}
151
152/* "show" command for the auto_load_dir configuration variable. */
153
154static void
155show_auto_load_dir (struct ui_file *file, int from_tty,
156 struct cmd_list_element *c, const char *value)
157{
158 fprintf_filtered (file, _("List of directories from which to load "
159 "auto-loaded scripts is %s.\n"),
160 value);
161}
162
bccbefd2
JK
163/* Directory list safe to hold auto-loaded files. It is not checked for
164 absolute paths but they are strongly recommended. It is initialized by
165 _initialize_auto_load. */
166static char *auto_load_safe_path;
167
168/* Vector of directory elements of AUTO_LOAD_SAFE_PATH with each one normalized
169 by tilde_expand and possibly each entries has added its gdb_realpath
170 counterpart. */
171static VEC (char_ptr) *auto_load_safe_path_vec;
172
1564a261
JK
173/* Expand $datadir and $debugdir in STRING according to the rules of
174 substitute_path_component. Return vector from dirnames_to_char_ptr_vec,
175 this vector must be freed by free_char_ptr_vec by the caller. */
176
177static VEC (char_ptr) *
178auto_load_expand_dir_vars (const char *string)
179{
180 VEC (char_ptr) *dir_vec;
181 char *s;
182
183 s = xstrdup (string);
184 substitute_path_component (&s, "$datadir", gdb_datadir);
185 substitute_path_component (&s, "$debugdir", debug_file_directory);
186
187 if (debug_auto_load && strcmp (s, string) != 0)
188 fprintf_unfiltered (gdb_stdlog,
189 _("auto-load: Expanded $-variables to \"%s\".\n"), s);
190
191 dir_vec = dirnames_to_char_ptr_vec (s);
192 xfree(s);
193
194 return dir_vec;
195}
196
bccbefd2
JK
197/* Update auto_load_safe_path_vec from current AUTO_LOAD_SAFE_PATH. */
198
199static void
200auto_load_safe_path_vec_update (void)
201{
bccbefd2
JK
202 unsigned len;
203 int ix;
204
4dc84fd1
JK
205 if (debug_auto_load)
206 fprintf_unfiltered (gdb_stdlog,
207 _("auto-load: Updating directories of \"%s\".\n"),
208 auto_load_safe_path);
209
bccbefd2
JK
210 free_char_ptr_vec (auto_load_safe_path_vec);
211
1564a261 212 auto_load_safe_path_vec = auto_load_expand_dir_vars (auto_load_safe_path);
bccbefd2
JK
213 len = VEC_length (char_ptr, auto_load_safe_path_vec);
214
215 /* Apply tilde_expand and gdb_realpath to each AUTO_LOAD_SAFE_PATH_VEC
216 element. */
217 for (ix = 0; ix < len; ix++)
218 {
219 char *dir = VEC_index (char_ptr, auto_load_safe_path_vec, ix);
1564a261
JK
220 char *expanded = tilde_expand (dir);
221 char *real_path = gdb_realpath (expanded);
bccbefd2 222
1564a261 223 /* Ensure the current entry is at least tilde_expand-ed. */
bccbefd2
JK
224 VEC_replace (char_ptr, auto_load_safe_path_vec, ix, expanded);
225
4dc84fd1
JK
226 if (debug_auto_load)
227 {
228 if (strcmp (expanded, dir) == 0)
229 fprintf_unfiltered (gdb_stdlog,
230 _("auto-load: Using directory \"%s\".\n"),
231 expanded);
232 else
233 fprintf_unfiltered (gdb_stdlog,
234 _("auto-load: Resolved directory \"%s\" "
235 "as \"%s\".\n"),
236 dir, expanded);
237 }
238 xfree (dir);
239
bccbefd2
JK
240 /* If gdb_realpath returns a different content, append it. */
241 if (strcmp (real_path, expanded) == 0)
242 xfree (real_path);
243 else
4dc84fd1
JK
244 {
245 VEC_safe_push (char_ptr, auto_load_safe_path_vec, real_path);
246
247 if (debug_auto_load)
248 fprintf_unfiltered (gdb_stdlog,
249 _("auto-load: And canonicalized as \"%s\".\n"),
250 real_path);
251 }
bccbefd2
JK
252 }
253}
254
aff139ff 255/* Variable gdb_datadir has been set. Update content depending on $datadir. */
6dea1fbd
JK
256
257static void
258auto_load_gdb_datadir_changed (void)
259{
260 auto_load_safe_path_vec_update ();
261}
262
bccbefd2
JK
263/* "set" command for the auto_load_safe_path configuration variable. */
264
265static void
266set_auto_load_safe_path (char *args, int from_tty, struct cmd_list_element *c)
267{
6dea1fbd 268 /* Setting the variable to "" resets it to the compile time defaults. */
af2c1515
JK
269 if (auto_load_safe_path[0] == '\0')
270 {
271 xfree (auto_load_safe_path);
6dea1fbd 272 auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH);
af2c1515
JK
273 }
274
bccbefd2
JK
275 auto_load_safe_path_vec_update ();
276}
277
278/* "show" command for the auto_load_safe_path configuration variable. */
279
280static void
281show_auto_load_safe_path (struct ui_file *file, int from_tty,
282 struct cmd_list_element *c, const char *value)
283{
f7bfa992
JK
284 const char *cs;
285
286 /* Check if user has entered either "/" or for example ":".
287 But while more complicate content like ":/foo" would still also
288 permit any location do not hide those. */
289
290 for (cs = value; *cs && (*cs == DIRNAME_SEPARATOR || IS_DIR_SEPARATOR (*cs));
291 cs++);
292 if (*cs == 0)
bccbefd2
JK
293 fprintf_filtered (file, _("Auto-load files are safe to load from any "
294 "directory.\n"));
295 else
296 fprintf_filtered (file, _("List of directories from which it is safe to "
297 "auto-load files is %s.\n"),
298 value);
299}
300
301/* "add-auto-load-safe-path" command for the auto_load_safe_path configuration
302 variable. */
303
304static void
305add_auto_load_safe_path (char *args, int from_tty)
306{
307 char *s;
308
309 if (args == NULL || *args == 0)
310 error (_("\
af2c1515
JK
311Directory argument required.\n\
312Use 'set auto-load safe-path /' for disabling the auto-load safe-path security.\
313"));
bccbefd2
JK
314
315 s = xstrprintf ("%s%c%s", auto_load_safe_path, DIRNAME_SEPARATOR, args);
316 xfree (auto_load_safe_path);
317 auto_load_safe_path = s;
318
319 auto_load_safe_path_vec_update ();
320}
321
f10c5b19
JK
322/* "add-auto-load-scripts-directory" command for the auto_load_dir configuration
323 variable. */
324
325static void
326add_auto_load_dir (char *args, int from_tty)
327{
328 char *s;
329
330 if (args == NULL || *args == 0)
331 error (_("Directory argument required."));
332
333 s = xstrprintf ("%s%c%s", auto_load_dir, DIRNAME_SEPARATOR, args);
334 xfree (auto_load_dir);
335 auto_load_dir = s;
336}
337
202cbf1c
JK
338/* Implementation for filename_is_in_pattern overwriting the caller's FILENAME
339 and PATTERN. */
bccbefd2 340
202cbf1c
JK
341static int
342filename_is_in_pattern_1 (char *filename, char *pattern)
bccbefd2 343{
202cbf1c
JK
344 size_t pattern_len = strlen (pattern);
345 size_t filename_len = strlen (filename);
346
347 if (debug_auto_load)
348 fprintf_unfiltered (gdb_stdlog, _("auto-load: Matching file \"%s\" "
349 "to pattern \"%s\"\n"),
350 filename, pattern);
bccbefd2 351
202cbf1c
JK
352 /* Trim trailing slashes ("/") from PATTERN. Even for "d:\" paths as
353 trailing slashes are trimmed also from FILENAME it still matches
354 correctly. */
355 while (pattern_len && IS_DIR_SEPARATOR (pattern[pattern_len - 1]))
356 pattern_len--;
357 pattern[pattern_len] = '\0';
bccbefd2 358
1ef71717
JK
359 /* Ensure auto_load_safe_path "/" matches any FILENAME. On MS-Windows
360 platform FILENAME even after gdb_realpath does not have to start with
361 IS_DIR_SEPARATOR character, such as the 'C:\x.exe' filename. */
202cbf1c
JK
362 if (pattern_len == 0)
363 {
364 if (debug_auto_load)
365 fprintf_unfiltered (gdb_stdlog,
366 _("auto-load: Matched - empty pattern\n"));
367 return 1;
368 }
369
370 for (;;)
371 {
372 /* Trim trailing slashes ("/"). PATTERN also has slashes trimmed the
373 same way so they will match. */
374 while (filename_len && IS_DIR_SEPARATOR (filename[filename_len - 1]))
375 filename_len--;
376 filename[filename_len] = '\0';
377 if (filename_len == 0)
378 {
379 if (debug_auto_load)
380 fprintf_unfiltered (gdb_stdlog,
381 _("auto-load: Not matched - pattern \"%s\".\n"),
382 pattern);
383 return 0;
384 }
385
386 if (gdb_filename_fnmatch (pattern, filename, FNM_FILE_NAME | FNM_NOESCAPE)
387 == 0)
388 {
389 if (debug_auto_load)
390 fprintf_unfiltered (gdb_stdlog, _("auto-load: Matched - file "
391 "\"%s\" to pattern \"%s\".\n"),
392 filename, pattern);
393 return 1;
394 }
395
396 /* Trim trailing FILENAME component. */
397 while (filename_len > 0 && !IS_DIR_SEPARATOR (filename[filename_len - 1]))
398 filename_len--;
399 }
400}
401
402/* Return 1 if FILENAME matches PATTERN or if FILENAME resides in
403 a subdirectory of a directory that matches PATTERN. Return 0 otherwise.
404 gdb_realpath normalization is never done here. */
405
406static ATTRIBUTE_PURE int
407filename_is_in_pattern (const char *filename, const char *pattern)
408{
409 char *filename_copy, *pattern_copy;
410
411 filename_copy = alloca (strlen (filename) + 1);
412 strcpy (filename_copy, filename);
413 pattern_copy = alloca (strlen (pattern) + 1);
414 strcpy (pattern_copy, pattern);
1ef71717 415
202cbf1c 416 return filename_is_in_pattern_1 (filename_copy, pattern_copy);
bccbefd2
JK
417}
418
419/* Return 1 if FILENAME belongs to one of directory components of
420 AUTO_LOAD_SAFE_PATH_VEC. Return 0 otherwise.
421 auto_load_safe_path_vec_update is never called.
422 *FILENAME_REALP may be updated by gdb_realpath of FILENAME - it has to be
423 freed by the caller. */
424
425static int
426filename_is_in_auto_load_safe_path_vec (const char *filename,
427 char **filename_realp)
428{
202cbf1c 429 char *pattern;
bccbefd2
JK
430 int ix;
431
202cbf1c
JK
432 for (ix = 0; VEC_iterate (char_ptr, auto_load_safe_path_vec, ix, pattern);
433 ++ix)
434 if (*filename_realp == NULL && filename_is_in_pattern (filename, pattern))
bccbefd2
JK
435 break;
436
202cbf1c 437 if (pattern == NULL)
bccbefd2
JK
438 {
439 if (*filename_realp == NULL)
4dc84fd1
JK
440 {
441 *filename_realp = gdb_realpath (filename);
442 if (debug_auto_load && strcmp (*filename_realp, filename) != 0)
443 fprintf_unfiltered (gdb_stdlog,
444 _("auto-load: Resolved "
445 "file \"%s\" as \"%s\".\n"),
446 filename, *filename_realp);
447 }
bccbefd2 448
4dc84fd1 449 if (strcmp (*filename_realp, filename) != 0)
202cbf1c
JK
450 for (ix = 0;
451 VEC_iterate (char_ptr, auto_load_safe_path_vec, ix, pattern); ++ix)
452 if (filename_is_in_pattern (*filename_realp, pattern))
4dc84fd1 453 break;
bccbefd2
JK
454 }
455
202cbf1c 456 if (pattern != NULL)
4dc84fd1
JK
457 {
458 if (debug_auto_load)
459 fprintf_unfiltered (gdb_stdlog, _("auto-load: File \"%s\" matches "
460 "directory \"%s\".\n"),
202cbf1c 461 filename, pattern);
4dc84fd1
JK
462 return 1;
463 }
bccbefd2
JK
464
465 return 0;
466}
467
468/* Return 1 if FILENAME is located in one of the directories of
469 AUTO_LOAD_SAFE_PATH. Otherwise call warning and return 0. FILENAME does
470 not have to be an absolute path.
471
472 Existence of FILENAME is not checked. Function will still give a warning
473 even if the caller would quietly skip non-existing file in unsafe
474 directory. */
475
476int
4dc84fd1 477file_is_auto_load_safe (const char *filename, const char *debug_fmt, ...)
bccbefd2
JK
478{
479 char *filename_real = NULL;
480 struct cleanup *back_to;
9a950c7c 481 static int advice_printed = 0;
bccbefd2 482
4dc84fd1
JK
483 if (debug_auto_load)
484 {
485 va_list debug_args;
486
487 va_start (debug_args, debug_fmt);
488 vfprintf_unfiltered (gdb_stdlog, debug_fmt, debug_args);
489 va_end (debug_args);
490 }
491
bccbefd2
JK
492 back_to = make_cleanup (free_current_contents, &filename_real);
493
494 if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
495 {
496 do_cleanups (back_to);
497 return 1;
498 }
499
500 auto_load_safe_path_vec_update ();
501 if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
502 {
503 do_cleanups (back_to);
504 return 1;
505 }
506
507 warning (_("File \"%s\" auto-loading has been declined by your "
508 "`auto-load safe-path' set to \"%s\"."),
509 filename_real, auto_load_safe_path);
510
9a950c7c
JK
511 if (!advice_printed)
512 {
513 const char *homedir = getenv ("HOME");
514 char *homeinit;
515
516 if (homedir == NULL)
517 homedir = "$HOME";
518 homeinit = xstrprintf ("%s/%s", homedir, gdbinit);
519 make_cleanup (xfree, homeinit);
520
521 printf_filtered (_("\
522To enable execution of this file add\n\
523\tadd-auto-load-safe-path %s\n\
524line to your configuration file \"%s\".\n\
525To completely disable this security protection add\n\
526\tset auto-load safe-path /\n\
527line to your configuration file \"%s\".\n\
528For more information about this security protection see the\n\
529\"Auto-loading safe path\" section in the GDB manual. E.g., run from the shell:\n\
530\tinfo \"(gdb)Auto-loading safe path\"\n"),
531 filename_real, homeinit, homeinit);
532 advice_printed = 1;
533 }
534
bccbefd2
JK
535 do_cleanups (back_to);
536 return 0;
537}
538
e2207b9a
JK
539/* For scripts specified in .debug_gdb_scripts, multiple objfiles may load
540 the same script. There's no point in loading the script multiple times,
541 and there can be a lot of objfiles and scripts, so we keep track of scripts
542 loaded this way. */
543
544struct auto_load_pspace_info
545{
9f050062
DE
546 /* For each program space we keep track of loaded scripts, both when
547 specified as file names and as scripts to be executed directly. */
548 struct htab *loaded_script_files;
549 struct htab *loaded_script_texts;
e2207b9a 550
6dddc817
DE
551 /* Non-zero if we've issued the warning about an auto-load script not being
552 supported. We only want to issue this warning once. */
553 int unsupported_script_warning_printed;
554
e2207b9a
JK
555 /* Non-zero if we've issued the warning about an auto-load script not being
556 found. We only want to issue this warning once. */
557 int script_not_found_warning_printed;
558};
559
9f050062 560/* Objects of this type are stored in the loaded_script hash table. */
e2207b9a
JK
561
562struct loaded_script
563{
564 /* Name as provided by the objfile. */
565 const char *name;
bf88dd68 566
e2207b9a 567 /* Full path name or NULL if script wasn't found (or was otherwise
9f050062 568 inaccessible), or NULL for loaded_script_texts. */
e2207b9a 569 const char *full_path;
bf88dd68 570
bccbefd2
JK
571 /* Non-zero if this script has been loaded. */
572 int loaded;
573
6dddc817 574 const struct extension_language_defn *language;
e2207b9a
JK
575};
576
577/* Per-program-space data key. */
578static const struct program_space_data *auto_load_pspace_data;
579
580static void
581auto_load_pspace_data_cleanup (struct program_space *pspace, void *arg)
582{
487ad57c 583 struct auto_load_pspace_info *info = arg;
e2207b9a 584
9f050062
DE
585 if (info->loaded_script_files)
586 htab_delete (info->loaded_script_files);
587 if (info->loaded_script_texts)
588 htab_delete (info->loaded_script_texts);
487ad57c 589 xfree (info);
e2207b9a
JK
590}
591
592/* Get the current autoload data. If none is found yet, add it now. This
593 function always returns a valid object. */
594
595static struct auto_load_pspace_info *
596get_auto_load_pspace_data (struct program_space *pspace)
597{
598 struct auto_load_pspace_info *info;
599
600 info = program_space_data (pspace, auto_load_pspace_data);
601 if (info == NULL)
602 {
41bf6aca 603 info = XCNEW (struct auto_load_pspace_info);
e2207b9a
JK
604 set_program_space_data (pspace, auto_load_pspace_data, info);
605 }
606
607 return info;
608}
609
610/* Hash function for the loaded script hash. */
611
612static hashval_t
613hash_loaded_script_entry (const void *data)
614{
615 const struct loaded_script *e = data;
616
bf88dd68 617 return htab_hash_string (e->name) ^ htab_hash_pointer (e->language);
e2207b9a
JK
618}
619
620/* Equality function for the loaded script hash. */
621
622static int
623eq_loaded_script_entry (const void *a, const void *b)
624{
625 const struct loaded_script *ea = a;
626 const struct loaded_script *eb = b;
627
bf88dd68 628 return strcmp (ea->name, eb->name) == 0 && ea->language == eb->language;
e2207b9a
JK
629}
630
631/* Initialize the table to track loaded scripts.
632 Each entry is hashed by the full path name. */
633
634static void
635init_loaded_scripts_info (struct auto_load_pspace_info *pspace_info)
636{
637 /* Choose 31 as the starting size of the hash table, somewhat arbitrarily.
638 Space for each entry is obtained with one malloc so we can free them
639 easily. */
640
9f050062
DE
641 pspace_info->loaded_script_files = htab_create (31,
642 hash_loaded_script_entry,
643 eq_loaded_script_entry,
644 xfree);
645 pspace_info->loaded_script_texts = htab_create (31,
646 hash_loaded_script_entry,
647 eq_loaded_script_entry,
648 xfree);
e2207b9a 649
6dddc817 650 pspace_info->unsupported_script_warning_printed = FALSE;
e2207b9a
JK
651 pspace_info->script_not_found_warning_printed = FALSE;
652}
653
654/* Wrapper on get_auto_load_pspace_data to also allocate the hash table
655 for loading scripts. */
656
657struct auto_load_pspace_info *
658get_auto_load_pspace_data_for_loading (struct program_space *pspace)
659{
660 struct auto_load_pspace_info *info;
661
662 info = get_auto_load_pspace_data (pspace);
9f050062 663 if (info->loaded_script_files == NULL)
e2207b9a
JK
664 init_loaded_scripts_info (info);
665
666 return info;
667}
668
9f050062
DE
669/* Add script file NAME in LANGUAGE to hash table of PSPACE_INFO.
670 LOADED 1 if the script has been (is going to) be loaded, 0 otherwise
671 (such as if it has not been found).
672 FULL_PATH is NULL if the script wasn't found.
673 The result is true if the script was already in the hash table. */
e2207b9a 674
6dddc817 675static int
9f050062
DE
676maybe_add_script_file (struct auto_load_pspace_info *pspace_info, int loaded,
677 const char *name, const char *full_path,
678 const struct extension_language_defn *language)
e2207b9a 679{
9f050062 680 struct htab *htab = pspace_info->loaded_script_files;
e2207b9a
JK
681 struct loaded_script **slot, entry;
682 int in_hash_table;
683
684 entry.name = name;
bf88dd68 685 entry.language = language;
e2207b9a
JK
686 slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT);
687 in_hash_table = *slot != NULL;
688
689 /* If this script is not in the hash table, add it. */
690
9f050062 691 if (!in_hash_table)
e2207b9a
JK
692 {
693 char *p;
694
695 /* Allocate all space in one chunk so it's easier to free. */
696 *slot = xmalloc (sizeof (**slot)
697 + strlen (name) + 1
698 + (full_path != NULL ? (strlen (full_path) + 1) : 0));
699 p = ((char*) *slot) + sizeof (**slot);
700 strcpy (p, name);
701 (*slot)->name = p;
702 if (full_path != NULL)
703 {
704 p += strlen (p) + 1;
705 strcpy (p, full_path);
706 (*slot)->full_path = p;
707 }
708 else
709 (*slot)->full_path = NULL;
bccbefd2 710 (*slot)->loaded = loaded;
bf88dd68 711 (*slot)->language = language;
e2207b9a
JK
712 }
713
714 return in_hash_table;
715}
716
9f050062
DE
717/* Add script contents NAME in LANGUAGE to hash table of PSPACE_INFO.
718 LOADED 1 if the script has been (is going to) be loaded, 0 otherwise
719 (such as if it has not been found).
720 The result is true if the script was already in the hash table. */
721
722static int
723maybe_add_script_text (struct auto_load_pspace_info *pspace_info,
724 int loaded, const char *name,
725 const struct extension_language_defn *language)
726{
727 struct htab *htab = pspace_info->loaded_script_texts;
728 struct loaded_script **slot, entry;
729 int in_hash_table;
730
731 entry.name = name;
732 entry.language = language;
733 slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT);
734 in_hash_table = *slot != NULL;
735
736 /* If this script is not in the hash table, add it. */
737
738 if (!in_hash_table)
739 {
740 char *p;
741
742 /* Allocate all space in one chunk so it's easier to free. */
743 *slot = xmalloc (sizeof (**slot) + strlen (name) + 1);
744 p = ((char*) *slot) + sizeof (**slot);
745 strcpy (p, name);
746 (*slot)->name = p;
747 (*slot)->full_path = NULL;
748 (*slot)->loaded = loaded;
749 (*slot)->language = language;
750 }
751
752 return in_hash_table;
753}
754
e2207b9a
JK
755/* Clear the table of loaded section scripts. */
756
757static void
758clear_section_scripts (void)
759{
760 struct program_space *pspace = current_program_space;
761 struct auto_load_pspace_info *info;
762
763 info = program_space_data (pspace, auto_load_pspace_data);
9f050062 764 if (info != NULL && info->loaded_script_files != NULL)
e2207b9a 765 {
9f050062
DE
766 htab_delete (info->loaded_script_files);
767 htab_delete (info->loaded_script_texts);
768 info->loaded_script_files = NULL;
769 info->loaded_script_texts = NULL;
6dddc817 770 info->unsupported_script_warning_printed = FALSE;
e2207b9a
JK
771 info->script_not_found_warning_printed = FALSE;
772 }
773}
774
e9687799
JK
775/* Look for the auto-load script in LANGUAGE associated with OBJFILE where
776 OBJFILE's gdb_realpath is REALNAME and load it. Return 1 if we found any
777 matching script, return 0 otherwise. */
e2207b9a 778
e9687799
JK
779static int
780auto_load_objfile_script_1 (struct objfile *objfile, const char *realname,
6dddc817 781 const struct extension_language_defn *language)
e2207b9a 782{
e2207b9a 783 char *filename, *debugfile;
e9687799 784 int len, retval;
e2207b9a
JK
785 FILE *input;
786 struct cleanup *cleanups;
6dddc817 787 const char *suffix = ext_lang_auto_load_suffix (language);
e2207b9a 788
e2207b9a 789 len = strlen (realname);
6dddc817 790 filename = xmalloc (len + strlen (suffix) + 1);
e2207b9a 791 memcpy (filename, realname, len);
6dddc817 792 strcpy (filename + len, suffix);
e2207b9a
JK
793
794 cleanups = make_cleanup (xfree, filename);
e2207b9a 795
614c279d 796 input = gdb_fopen_cloexec (filename, "r");
e2207b9a 797 debugfile = filename;
7349ff92
JK
798 if (debug_auto_load)
799 fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file \"%s\" %s.\n"),
800 debugfile, input ? _("exists") : _("does not exist"));
e2207b9a 801
7349ff92 802 if (!input)
e2207b9a 803 {
7349ff92
JK
804 VEC (char_ptr) *vec;
805 int ix;
806 char *dir;
807
e2207b9a
JK
808 /* Also try the same file in a subdirectory of gdb's data
809 directory. */
7349ff92 810
1564a261 811 vec = auto_load_expand_dir_vars (auto_load_dir);
7349ff92
JK
812 make_cleanup_free_char_ptr_vec (vec);
813
814 if (debug_auto_load)
815 fprintf_unfiltered (gdb_stdlog, _("auto-load: Searching 'set auto-load "
816 "scripts-directory' path \"%s\".\n"),
817 auto_load_dir);
818
819 for (ix = 0; VEC_iterate (char_ptr, vec, ix, dir); ++ix)
820 {
1564a261
JK
821 debugfile = xmalloc (strlen (dir) + strlen (filename) + 1);
822 strcpy (debugfile, dir);
7349ff92
JK
823
824 /* FILENAME is absolute, so we don't need a "/" here. */
825 strcat (debugfile, filename);
826
827 make_cleanup (xfree, debugfile);
614c279d 828 input = gdb_fopen_cloexec (debugfile, "r");
7349ff92
JK
829 if (debug_auto_load)
830 fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file "
831 "\"%s\" %s.\n"),
832 debugfile,
833 input ? _("exists") : _("does not exist"));
834 if (input != NULL)
835 break;
836 }
e2207b9a
JK
837 }
838
839 if (input)
840 {
5b2bf947
DE
841 int is_safe;
842 struct auto_load_pspace_info *pspace_info;
843
e2207b9a
JK
844 make_cleanup_fclose (input);
845
5b2bf947 846 is_safe
849c862e 847 = file_is_auto_load_safe (debugfile,
5b2bf947
DE
848 _("auto-load: Loading %s script \"%s\""
849 " by extension for objfile \"%s\".\n"),
6dddc817 850 ext_lang_name (language),
849c862e 851 debugfile, objfile_name (objfile));
5b2bf947
DE
852
853 /* Add this script to the hash table too so
854 "info auto-load ${lang}-scripts" can print it. */
855 pspace_info
856 = get_auto_load_pspace_data_for_loading (current_program_space);
9f050062
DE
857 maybe_add_script_file (pspace_info, is_safe, debugfile, debugfile,
858 language);
5b2bf947 859
e2207b9a
JK
860 /* To preserve existing behaviour we don't check for whether the
861 script was already in the table, and always load it.
862 It's highly unlikely that we'd ever load it twice,
863 and these scripts are required to be idempotent under multiple
864 loads anyway. */
5b2bf947 865 if (is_safe)
6dddc817
DE
866 {
867 objfile_script_sourcer_func *sourcer
868 = ext_lang_objfile_script_sourcer (language);
869
870 /* We shouldn't get here if support for the language isn't
871 compiled in. And the extension language is required to implement
872 this function. */
873 gdb_assert (sourcer != NULL);
874 sourcer (language, objfile, input, debugfile);
875 }
e9687799
JK
876
877 retval = 1;
878 }
879 else
880 retval = 0;
881
882 do_cleanups (cleanups);
883 return retval;
884}
885
886/* Look for the auto-load script in LANGUAGE associated with OBJFILE and load
887 it. */
888
6dddc817 889void
e9687799 890auto_load_objfile_script (struct objfile *objfile,
6dddc817 891 const struct extension_language_defn *language)
e9687799 892{
6dddc817
DE
893 char *realname = gdb_realpath (objfile_name (objfile));
894 struct cleanup *cleanups = make_cleanup (xfree, realname);
e9687799
JK
895
896 if (!auto_load_objfile_script_1 (objfile, realname, language))
897 {
898 /* For Windows/DOS .exe executables, strip the .exe suffix, so that
899 FOO-gdb.gdb could be used for FOO.exe, and try again. */
900
901 size_t len = strlen (realname);
902 const size_t lexe = sizeof (".exe") - 1;
903
904 if (len > lexe && strcasecmp (realname + len - lexe, ".exe") == 0)
905 {
906 len -= lexe;
907 realname[len] = '\0';
908 if (debug_auto_load)
909 fprintf_unfiltered (gdb_stdlog, _("auto-load: Stripped .exe suffix, "
910 "retrying with \"%s\".\n"),
911 realname);
912 auto_load_objfile_script_1 (objfile, realname, language);
913 }
e2207b9a
JK
914 }
915
916 do_cleanups (cleanups);
917}
918
9f050062
DE
919/* Subroutine of source_section_scripts to simplify it.
920 Load FILE as a script in extension language LANGUAGE.
921 The script is from section SECTION_NAME in OBJFILE at offset OFFSET. */
922
923static void
924source_script_file (struct auto_load_pspace_info *pspace_info,
925 struct objfile *objfile,
926 const struct extension_language_defn *language,
927 const char *section_name, unsigned int offset,
928 const char *file)
929{
930 FILE *stream;
931 char *full_path;
932 int opened, in_hash_table;
933 struct cleanup *cleanups;
934 objfile_script_sourcer_func *sourcer;
935
936 /* Skip this script if support is not compiled in. */
937 sourcer = ext_lang_objfile_script_sourcer (language);
938 if (sourcer == NULL)
939 {
940 /* We don't throw an error, the program is still debuggable. */
941 maybe_print_unsupported_script_warning (pspace_info, objfile, language,
942 section_name, offset);
943 /* We *could* still try to open it, but there's no point. */
944 maybe_add_script_file (pspace_info, 0, file, NULL, language);
945 return;
946 }
947
948 /* Skip this script if auto-loading it has been disabled. */
949 if (!ext_lang_auto_load_enabled (language))
950 {
951 /* No message is printed, just skip it. */
952 return;
953 }
954
955 opened = find_and_open_script (file, 1 /*search_path*/,
956 &stream, &full_path);
957
958 cleanups = make_cleanup (null_cleanup, NULL);
959 if (opened)
960 {
961 make_cleanup_fclose (stream);
962 make_cleanup (xfree, full_path);
963
964 if (!file_is_auto_load_safe (full_path,
965 _("auto-load: Loading %s script "
966 "\"%s\" from section \"%s\" of "
967 "objfile \"%s\".\n"),
968 ext_lang_name (language), full_path,
969 section_name, objfile_name (objfile)))
970 opened = 0;
971 }
972 else
973 {
974 full_path = NULL;
975
976 /* If one script isn't found it's not uncommon for more to not be
977 found either. We don't want to print a message for each script,
978 too much noise. Instead, we print the warning once and tell the
979 user how to find the list of scripts that weren't loaded.
980 We don't throw an error, the program is still debuggable.
981
982 IWBN if complaints.c were more general-purpose. */
983
984 maybe_print_script_not_found_warning (pspace_info, objfile, language,
985 section_name, offset);
986 }
987
988 in_hash_table = maybe_add_script_file (pspace_info, opened, file, full_path,
989 language);
990
991 /* If this file is not currently loaded, load it. */
992 if (opened && !in_hash_table)
993 sourcer (language, objfile, stream, full_path);
994
995 do_cleanups (cleanups);
996}
997
998/* Subroutine of source_section_scripts to simplify it.
999 Execute SCRIPT as a script in extension language LANG.
1000 The script is from section SECTION_NAME in OBJFILE at offset OFFSET. */
1001
1002static void
1003execute_script_contents (struct auto_load_pspace_info *pspace_info,
1004 struct objfile *objfile,
1005 const struct extension_language_defn *language,
1006 const char *section_name, unsigned int offset,
1007 const char *script)
1008{
1009 objfile_script_executor_func *executor;
1010 const char *newline, *script_text;
1011 char *name, *end;
1012 int is_safe, in_hash_table;
1013 struct cleanup *cleanups;
1014
1015 cleanups = make_cleanup (null_cleanup, NULL);
1016
1017 /* The first line of the script is the name of the script.
1018 It must not contain any kind of space character. */
1019 name = NULL;
1020 newline = strchr (script, '\n');
1021 if (newline != NULL)
1022 {
1023 char *buf, *p;
1024
1025 /* Put the name in a buffer and validate it. */
1026 buf = xstrndup (script, newline - script);
1027 make_cleanup (xfree, buf);
1028 for (p = buf; *p != '\0'; ++p)
1029 {
1030 if (isspace (*p))
1031 break;
1032 }
1033 /* We don't allow nameless scripts, they're not helpful to the user. */
1034 if (p != buf && *p == '\0')
1035 name = buf;
1036 }
1037 if (name == NULL)
1038 {
1039 /* We don't throw an error, the program is still debuggable. */
1040 warning (_("\
1041Missing/bad script name in entry at offset %u in section %s\n\
1042of file %s."),
1043 offset, section_name, objfile_name (objfile));
1044 do_cleanups (cleanups);
1045 return;
1046 }
1047 script_text = newline + 1;
1048
1049 /* Skip this script if support is not compiled in. */
1050 executor = ext_lang_objfile_script_executor (language);
1051 if (executor == NULL)
1052 {
1053 /* We don't throw an error, the program is still debuggable. */
1054 maybe_print_unsupported_script_warning (pspace_info, objfile, language,
1055 section_name, offset);
1056 maybe_add_script_text (pspace_info, 0, name, language);
1057 do_cleanups (cleanups);
1058 return;
1059 }
1060
1061 /* Skip this script if auto-loading it has been disabled. */
1062 if (!ext_lang_auto_load_enabled (language))
1063 {
1064 /* No message is printed, just skip it. */
1065 do_cleanups (cleanups);
1066 return;
1067 }
1068
1069 is_safe = file_is_auto_load_safe (objfile_name (objfile),
1070 _("auto-load: Loading %s script "
1071 "\"%s\" from section \"%s\" of "
1072 "objfile \"%s\".\n"),
1073 ext_lang_name (language), name,
1074 section_name, objfile_name (objfile));
1075
1076 in_hash_table = maybe_add_script_text (pspace_info, is_safe, name, language);
1077
1078 /* If this file is not currently loaded, load it. */
1079 if (is_safe && !in_hash_table)
1080 executor (language, objfile, name, script_text);
1081
1082 do_cleanups (cleanups);
1083}
1084
5b2bf947
DE
1085/* Load scripts specified in OBJFILE.
1086 START,END delimit a buffer containing a list of nul-terminated
1087 file names.
1088 SECTION_NAME is used in error messages.
1089
9f050062
DE
1090 Scripts specified as file names are found per normal "source -s" command
1091 processing. First the script is looked for in $cwd. If not found there
1092 the source search path is used.
5b2bf947 1093
9f050062
DE
1094 The section contains a list of path names of script files to load or
1095 actual script contents. Each entry is nul-terminated. */
5b2bf947
DE
1096
1097static void
1098source_section_scripts (struct objfile *objfile, const char *section_name,
1099 const char *start, const char *end)
1100{
1101 const char *p;
1102 struct auto_load_pspace_info *pspace_info;
1103
1104 pspace_info = get_auto_load_pspace_data_for_loading (current_program_space);
1105
1106 for (p = start; p < end; ++p)
1107 {
9f050062 1108 const char *entry;
ed3ef339 1109 const struct extension_language_defn *language;
9f050062
DE
1110 unsigned int offset = p - start;
1111 int code = *p;
5b2bf947 1112
9f050062 1113 switch (code)
5b2bf947 1114 {
ed3ef339 1115 case SECTION_SCRIPT_ID_PYTHON_FILE:
9f050062 1116 case SECTION_SCRIPT_ID_PYTHON_TEXT:
ed3ef339
DE
1117 language = get_ext_lang_defn (EXT_LANG_PYTHON);
1118 break;
1119 case SECTION_SCRIPT_ID_SCHEME_FILE:
9f050062 1120 case SECTION_SCRIPT_ID_SCHEME_TEXT:
ed3ef339
DE
1121 language = get_ext_lang_defn (EXT_LANG_GUILE);
1122 break;
1123 default:
5b2bf947
DE
1124 warning (_("Invalid entry in %s section"), section_name);
1125 /* We could try various heuristics to find the next valid entry,
1126 but it's safer to just punt. */
ed3ef339 1127 return;
5b2bf947 1128 }
9f050062 1129 entry = ++p;
5b2bf947
DE
1130
1131 while (p < end && *p != '\0')
1132 ++p;
1133 if (p == end)
1134 {
9f050062
DE
1135 warning (_("Non-nul-terminated entry in %s at offset %u"),
1136 section_name, offset);
1137 /* Don't load/execute it. */
5b2bf947
DE
1138 break;
1139 }
6dddc817 1140
9f050062 1141 switch (code)
6dddc817 1142 {
9f050062
DE
1143 case SECTION_SCRIPT_ID_PYTHON_FILE:
1144 case SECTION_SCRIPT_ID_SCHEME_FILE:
1145 if (p == entry)
6dddc817 1146 {
9f050062
DE
1147 warning (_("Empty entry in %s at offset %u"),
1148 section_name, offset);
1149 continue;
6dddc817 1150 }
9f050062
DE
1151 source_script_file (pspace_info, objfile, language,
1152 section_name, offset, entry);
1153 break;
1154 case SECTION_SCRIPT_ID_PYTHON_TEXT:
1155 case SECTION_SCRIPT_ID_SCHEME_TEXT:
1156 execute_script_contents (pspace_info, objfile, language,
1157 section_name, offset, entry);
1158 break;
5b2bf947 1159 }
5b2bf947
DE
1160 }
1161}
1162
1163/* Load scripts specified in section SECTION_NAME of OBJFILE. */
1164
1165static void
1166auto_load_section_scripts (struct objfile *objfile, const char *section_name)
1167{
1168 bfd *abfd = objfile->obfd;
1169 asection *scripts_sect;
1170 bfd_byte *data = NULL;
1171
1172 scripts_sect = bfd_get_section_by_name (abfd, section_name);
1173 if (scripts_sect == NULL)
1174 return;
1175
1176 if (!bfd_get_full_section_contents (abfd, scripts_sect, &data))
1177 warning (_("Couldn't read %s section of %s"),
1178 section_name, bfd_get_filename (abfd));
1179 else
1180 {
1181 struct cleanup *cleanups;
1182 char *p = (char *) data;
1183
1184 cleanups = make_cleanup (xfree, p);
1185 source_section_scripts (objfile, section_name, p,
1186 p + bfd_get_section_size (scripts_sect));
1187 do_cleanups (cleanups);
1188 }
1189}
1190
bf88dd68
JK
1191/* Load any auto-loaded scripts for OBJFILE. */
1192
1193void
1194load_auto_scripts_for_objfile (struct objfile *objfile)
1195{
c47cf547
DE
1196 /* Return immediately if auto-loading has been globally disabled.
1197 This is to handle sequencing of operations during gdb startup.
5fbae7d1
GB
1198 Also return immediately if OBJFILE was not created from a file
1199 on the local filesystem. */
1200 if (!global_auto_load
1201 || (objfile->flags & OBJF_NOT_FILENAME) != 0
1202 || is_target_filename (objfile->original_name))
bf88dd68
JK
1203 return;
1204
6dddc817
DE
1205 /* Load any extension language scripts for this objfile.
1206 E.g., foo-gdb.gdb, foo-gdb.py. */
1207 auto_load_ext_lang_scripts_for_objfile (objfile);
bf88dd68 1208
c47cf547 1209 /* Load any scripts mentioned in AUTO_SECTION_NAME (.debug_gdb_scripts). */
5b2bf947 1210 auto_load_section_scripts (objfile, AUTO_SECTION_NAME);
bf88dd68
JK
1211}
1212
e2207b9a
JK
1213/* This is a new_objfile observer callback to auto-load scripts.
1214
1215 Two flavors of auto-loaded scripts are supported.
1216 1) based on the path to the objfile
1217 2) from .debug_gdb_scripts section */
1218
1219static void
1220auto_load_new_objfile (struct objfile *objfile)
1221{
1222 if (!objfile)
1223 {
1224 /* OBJFILE is NULL when loading a new "main" symbol-file. */
1225 clear_section_scripts ();
1226 return;
1227 }
1228
1229 load_auto_scripts_for_objfile (objfile);
1230}
1231
1232/* Collect scripts to be printed in a vec. */
1233
1234typedef struct loaded_script *loaded_script_ptr;
1235DEF_VEC_P (loaded_script_ptr);
1236
bf88dd68
JK
1237struct collect_matching_scripts_data
1238{
1239 VEC (loaded_script_ptr) **scripts_p;
1240
6dddc817 1241 const struct extension_language_defn *language;
bf88dd68
JK
1242};
1243
e2207b9a
JK
1244/* Traversal function for htab_traverse.
1245 Collect the entry if it matches the regexp. */
1246
1247static int
1248collect_matching_scripts (void **slot, void *info)
1249{
1250 struct loaded_script *script = *slot;
bf88dd68 1251 struct collect_matching_scripts_data *data = info;
e2207b9a 1252
bf88dd68
JK
1253 if (script->language == data->language && re_exec (script->name))
1254 VEC_safe_push (loaded_script_ptr, *data->scripts_p, script);
e2207b9a
JK
1255
1256 return 1;
1257}
1258
1259/* Print SCRIPT. */
1260
1261static void
1262print_script (struct loaded_script *script)
1263{
1264 struct ui_out *uiout = current_uiout;
1265 struct cleanup *chain;
1266
1267 chain = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
1268
bccbefd2 1269 ui_out_field_string (uiout, "loaded", script->loaded ? "Yes" : "No");
e2207b9a
JK
1270 ui_out_field_string (uiout, "script", script->name);
1271 ui_out_text (uiout, "\n");
1272
1273 /* If the name isn't the full path, print it too. */
1274 if (script->full_path != NULL
1275 && strcmp (script->name, script->full_path) != 0)
1276 {
1277 ui_out_text (uiout, "\tfull name: ");
1278 ui_out_field_string (uiout, "full_path", script->full_path);
1279 ui_out_text (uiout, "\n");
1280 }
1281
1282 do_cleanups (chain);
1283}
1284
1285/* Helper for info_auto_load_scripts to sort the scripts by name. */
1286
1287static int
1288sort_scripts_by_name (const void *ap, const void *bp)
1289{
1290 const struct loaded_script *a = *(const struct loaded_script **) ap;
1291 const struct loaded_script *b = *(const struct loaded_script **) bp;
1292
1293 return FILENAME_CMP (a->name, b->name);
1294}
1295
bf88dd68
JK
1296/* Special internal GDB value of auto_load_info_scripts's PATTERN identify
1297 the "info auto-load XXX" command has been executed through the general
1298 "info auto-load" invocation. Extra newline will be printed if needed. */
1299char auto_load_info_scripts_pattern_nl[] = "";
e2207b9a 1300
9f050062
DE
1301/* Subroutine of auto_load_info_scripts to simplify it.
1302 Print SCRIPTS. */
1303
1304static void
1305print_scripts (VEC (loaded_script_ptr) *scripts)
1306{
1307 struct ui_out *uiout = current_uiout;
1308 int i;
1309 loaded_script_ptr script;
1310
1311 qsort (VEC_address (loaded_script_ptr, scripts),
1312 VEC_length (loaded_script_ptr, scripts),
1313 sizeof (loaded_script_ptr), sort_scripts_by_name);
1314 for (i = 0; VEC_iterate (loaded_script_ptr, scripts, i, script); ++i)
1315 print_script (script);
1316}
1317
bf88dd68
JK
1318/* Implementation for "info auto-load gdb-scripts"
1319 (and "info auto-load python-scripts"). List scripts in LANGUAGE matching
1320 PATTERN. FROM_TTY is the usual GDB boolean for user interactivity. */
1321
1322void
1323auto_load_info_scripts (char *pattern, int from_tty,
6dddc817 1324 const struct extension_language_defn *language)
e2207b9a
JK
1325{
1326 struct ui_out *uiout = current_uiout;
1327 struct auto_load_pspace_info *pspace_info;
1328 struct cleanup *script_chain;
9f050062 1329 VEC (loaded_script_ptr) *script_files, *script_texts;
e2207b9a
JK
1330 int nr_scripts;
1331
1332 dont_repeat ();
1333
1334 pspace_info = get_auto_load_pspace_data (current_program_space);
1335
1336 if (pattern && *pattern)
1337 {
1338 char *re_err = re_comp (pattern);
1339
1340 if (re_err)
1341 error (_("Invalid regexp: %s"), re_err);
1342 }
1343 else
1344 {
1345 re_comp ("");
1346 }
1347
1348 /* We need to know the number of rows before we build the table.
1349 Plus we want to sort the scripts by name.
1350 So first traverse the hash table collecting the matching scripts. */
1351
9f050062
DE
1352 script_files = VEC_alloc (loaded_script_ptr, 10);
1353 script_texts = VEC_alloc (loaded_script_ptr, 10);
1354 script_chain = make_cleanup (VEC_cleanup (loaded_script_ptr), &script_files);
1355 make_cleanup (VEC_cleanup (loaded_script_ptr), &script_texts);
1356
1357 if (pspace_info != NULL && pspace_info->loaded_script_files != NULL)
1358 {
1359 struct collect_matching_scripts_data data = { &script_files, language };
1360
1361 /* Pass a pointer to scripts as VEC_safe_push can realloc space. */
1362 htab_traverse_noresize (pspace_info->loaded_script_files,
1363 collect_matching_scripts, &data);
1364 }
e2207b9a 1365
9f050062 1366 if (pspace_info != NULL && pspace_info->loaded_script_texts != NULL)
e2207b9a 1367 {
9f050062 1368 struct collect_matching_scripts_data data = { &script_texts, language };
bf88dd68 1369
e2207b9a 1370 /* Pass a pointer to scripts as VEC_safe_push can realloc space. */
9f050062 1371 htab_traverse_noresize (pspace_info->loaded_script_texts,
bf88dd68 1372 collect_matching_scripts, &data);
e2207b9a
JK
1373 }
1374
9f050062
DE
1375 nr_scripts = (VEC_length (loaded_script_ptr, script_files)
1376 + VEC_length (loaded_script_ptr, script_texts));
bf88dd68
JK
1377
1378 /* Table header shifted right by preceding "gdb-scripts: " would not match
1379 its columns. */
1380 if (nr_scripts > 0 && pattern == auto_load_info_scripts_pattern_nl)
1381 ui_out_text (uiout, "\n");
1382
9f050062 1383 /* Note: This creates a cleanup to output the table end marker. */
e2207b9a
JK
1384 make_cleanup_ui_out_table_begin_end (uiout, 2, nr_scripts,
1385 "AutoLoadedScriptsTable");
1386
1387 ui_out_table_header (uiout, 7, ui_left, "loaded", "Loaded");
1388 ui_out_table_header (uiout, 70, ui_left, "script", "Script");
1389 ui_out_table_body (uiout);
1390
9f050062
DE
1391 print_scripts (script_files);
1392 print_scripts (script_texts);
e2207b9a 1393
9f050062 1394 /* Finish up the table before checking for no matching scripts. */
e2207b9a
JK
1395 do_cleanups (script_chain);
1396
1397 if (nr_scripts == 0)
1398 {
1399 if (pattern && *pattern)
1400 ui_out_message (uiout, 0, "No auto-load scripts matching %s.\n",
1401 pattern);
1402 else
1403 ui_out_message (uiout, 0, "No auto-load scripts.\n");
1404 }
1405}
1406
bf88dd68
JK
1407/* Wrapper for "info auto-load gdb-scripts". */
1408
1409static void
1410info_auto_load_gdb_scripts (char *pattern, int from_tty)
1411{
6dddc817 1412 auto_load_info_scripts (pattern, from_tty, &extension_language_gdb);
bf88dd68
JK
1413}
1414
1415/* Implement 'info auto-load local-gdbinit'. */
1416
1417static void
1418info_auto_load_local_gdbinit (char *args, int from_tty)
1419{
1420 if (auto_load_local_gdbinit_pathname == NULL)
1421 printf_filtered (_("Local .gdbinit file was not found.\n"));
1422 else if (auto_load_local_gdbinit_loaded)
1423 printf_filtered (_("Local .gdbinit file \"%s\" has been loaded.\n"),
1424 auto_load_local_gdbinit_pathname);
1425 else
1426 printf_filtered (_("Local .gdbinit file \"%s\" has not been loaded.\n"),
1427 auto_load_local_gdbinit_pathname);
1428}
1429
9f050062
DE
1430/* Print an "unsupported script" warning if it has not already been printed.
1431 The script is in language LANGUAGE at offset OFFSET in section SECTION_NAME
1432 of OBJFILE. */
6dddc817 1433
9f050062
DE
1434static void
1435maybe_print_unsupported_script_warning
1436 (struct auto_load_pspace_info *pspace_info,
1437 struct objfile *objfile, const struct extension_language_defn *language,
1438 const char *section_name, unsigned offset)
6dddc817 1439{
9f050062
DE
1440 if (!pspace_info->unsupported_script_warning_printed)
1441 {
1442 warning (_("\
1443Unsupported auto-load script at offset %u in section %s\n\
1444of file %s.\n\
1445Use `info auto-load %s-scripts [REGEXP]' to list them."),
1446 offset, section_name, objfile_name (objfile),
1447 ext_lang_name (language));
1448 pspace_info->unsupported_script_warning_printed = 1;
1449 }
6dddc817
DE
1450}
1451
e2207b9a
JK
1452/* Return non-zero if SCRIPT_NOT_FOUND_WARNING_PRINTED of PSPACE_INFO was unset
1453 before calling this function. Always set SCRIPT_NOT_FOUND_WARNING_PRINTED
1454 of PSPACE_INFO. */
1455
9f050062
DE
1456static void
1457maybe_print_script_not_found_warning
1458 (struct auto_load_pspace_info *pspace_info,
1459 struct objfile *objfile, const struct extension_language_defn *language,
1460 const char *section_name, unsigned offset)
e2207b9a 1461{
9f050062
DE
1462 if (!pspace_info->script_not_found_warning_printed)
1463 {
1464 warning (_("\
1465Missing auto-load script at offset %u in section %s\n\
1466of file %s.\n\
1467Use `info auto-load %s-scripts [REGEXP]' to list them."),
1468 offset, section_name, objfile_name (objfile),
1469 ext_lang_name (language));
1470 pspace_info->script_not_found_warning_printed = 1;
1471 }
e2207b9a
JK
1472}
1473
bf88dd68
JK
1474/* The only valid "set auto-load" argument is off|0|no|disable. */
1475
1476static void
1477set_auto_load_cmd (char *args, int from_tty)
1478{
1479 struct cmd_list_element *list;
1480 size_t length;
1481
1482 /* See parse_binary_operation in use by the sub-commands. */
1483
1484 length = args ? strlen (args) : 0;
1485
1486 while (length > 0 && (args[length - 1] == ' ' || args[length - 1] == '\t'))
1487 length--;
1488
1489 if (length == 0 || (strncmp (args, "off", length) != 0
1490 && strncmp (args, "0", length) != 0
1491 && strncmp (args, "no", length) != 0
1492 && strncmp (args, "disable", length) != 0))
1493 error (_("Valid is only global 'set auto-load no'; "
1494 "otherwise check the auto-load sub-commands."));
1495
1496 for (list = *auto_load_set_cmdlist_get (); list != NULL; list = list->next)
1497 if (list->var_type == var_boolean)
1498 {
1499 gdb_assert (list->type == set_cmd);
5b9afe8a 1500 do_set_command (args, from_tty, list);
bf88dd68
JK
1501 }
1502}
1503
1504/* Initialize "set auto-load " commands prefix and return it. */
1505
1506struct cmd_list_element **
1507auto_load_set_cmdlist_get (void)
1508{
1509 static struct cmd_list_element *retval;
1510
1511 if (retval == NULL)
1512 add_prefix_cmd ("auto-load", class_maintenance, set_auto_load_cmd, _("\
1513Auto-loading specific settings.\n\
1514Configure various auto-load-specific variables such as\n\
1515automatic loading of Python scripts."),
1516 &retval, "set auto-load ",
1517 1/*allow-unknown*/, &setlist);
1518
1519 return &retval;
1520}
1521
1522/* Command "show auto-load" displays summary of all the current
1523 "show auto-load " settings. */
1524
1525static void
1526show_auto_load_cmd (char *args, int from_tty)
1527{
1528 cmd_show_list (*auto_load_show_cmdlist_get (), from_tty, "");
1529}
1530
1531/* Initialize "show auto-load " commands prefix and return it. */
1532
1533struct cmd_list_element **
1534auto_load_show_cmdlist_get (void)
1535{
1536 static struct cmd_list_element *retval;
1537
1538 if (retval == NULL)
1539 add_prefix_cmd ("auto-load", class_maintenance, show_auto_load_cmd, _("\
1540Show auto-loading specific settings.\n\
1541Show configuration of various auto-load-specific variables such as\n\
1542automatic loading of Python scripts."),
1543 &retval, "show auto-load ",
1544 0/*allow-unknown*/, &showlist);
1545
1546 return &retval;
1547}
1548
1549/* Command "info auto-load" displays whether the various auto-load files have
1550 been loaded. This is reimplementation of cmd_show_list which inserts
1551 newlines at proper places. */
1552
1553static void
1554info_auto_load_cmd (char *args, int from_tty)
1555{
1556 struct cmd_list_element *list;
1557 struct cleanup *infolist_chain;
1558 struct ui_out *uiout = current_uiout;
1559
1560 infolist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "infolist");
1561
1562 for (list = *auto_load_info_cmdlist_get (); list != NULL; list = list->next)
1563 {
1564 struct cleanup *option_chain
1565 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
1566
1567 gdb_assert (!list->prefixlist);
1568 gdb_assert (list->type == not_set_cmd);
1569
1570 ui_out_field_string (uiout, "name", list->name);
1571 ui_out_text (uiout, ": ");
1572 cmd_func (list, auto_load_info_scripts_pattern_nl, from_tty);
1573
1574 /* Close the tuple. */
1575 do_cleanups (option_chain);
1576 }
1577
1578 /* Close the tuple. */
1579 do_cleanups (infolist_chain);
1580}
1581
1582/* Initialize "info auto-load " commands prefix and return it. */
1583
1584struct cmd_list_element **
1585auto_load_info_cmdlist_get (void)
1586{
1587 static struct cmd_list_element *retval;
1588
1589 if (retval == NULL)
1590 add_prefix_cmd ("auto-load", class_info, info_auto_load_cmd, _("\
1591Print current status of auto-loaded files.\n\
1592Print whether various files like Python scripts or .gdbinit files have been\n\
1593found and/or loaded."),
1594 &retval, "info auto-load ",
1595 0/*allow-unknown*/, &infolist);
1596
1597 return &retval;
1598}
1599
e2207b9a
JK
1600void _initialize_auto_load (void);
1601
1602void
1603_initialize_auto_load (void)
1604{
bccbefd2 1605 struct cmd_list_element *cmd;
6dddc817 1606 char *scripts_directory_help, *gdb_name_help, *python_name_help;
ed3ef339
DE
1607 char *guile_name_help;
1608 const char *suffix;
bccbefd2 1609
e2207b9a 1610 auto_load_pspace_data
8e260fc0
TT
1611 = register_program_space_data_with_cleanup (NULL,
1612 auto_load_pspace_data_cleanup);
e2207b9a
JK
1613
1614 observer_attach_new_objfile (auto_load_new_objfile);
1615
bf88dd68
JK
1616 add_setshow_boolean_cmd ("gdb-scripts", class_support,
1617 &auto_load_gdb_scripts, _("\
1618Enable or disable auto-loading of canned sequences of commands scripts."), _("\
1619Show whether auto-loading of canned sequences of commands scripts is enabled."),
1620 _("\
1621If enabled, canned sequences of commands are loaded when the debugger reads\n\
1622an executable or shared library.\n\
1623This options has security implications for untrusted inferiors."),
1624 NULL, show_auto_load_gdb_scripts,
1625 auto_load_set_cmdlist_get (),
1626 auto_load_show_cmdlist_get ());
1627
1628 add_cmd ("gdb-scripts", class_info, info_auto_load_gdb_scripts,
1629 _("Print the list of automatically loaded sequences of commands.\n\
1630Usage: info auto-load gdb-scripts [REGEXP]"),
1631 auto_load_info_cmdlist_get ());
1632
1633 add_setshow_boolean_cmd ("local-gdbinit", class_support,
1634 &auto_load_local_gdbinit, _("\
1635Enable or disable auto-loading of .gdbinit script in current directory."), _("\
1636Show whether auto-loading .gdbinit script in current directory is enabled."),
1637 _("\
1638If enabled, canned sequences of commands are loaded when debugger starts\n\
1639from .gdbinit file in current directory. Such files are deprecated,\n\
1640use a script associated with inferior executable file instead.\n\
1641This options has security implications for untrusted inferiors."),
1642 NULL, show_auto_load_local_gdbinit,
1643 auto_load_set_cmdlist_get (),
1644 auto_load_show_cmdlist_get ());
1645
1646 add_cmd ("local-gdbinit", class_info, info_auto_load_local_gdbinit,
1647 _("Print whether current directory .gdbinit file has been loaded.\n\
1648Usage: info auto-load local-gdbinit"),
1649 auto_load_info_cmdlist_get ());
bccbefd2 1650
7349ff92 1651 auto_load_dir = xstrdup (AUTO_LOAD_DIR);
6dddc817 1652
ed3ef339 1653 suffix = ext_lang_auto_load_suffix (get_ext_lang_defn (EXT_LANG_GDB));
6dddc817
DE
1654 gdb_name_help
1655 = xstrprintf (_("\
1656GDB scripts: OBJFILE%s\n"),
ed3ef339 1657 suffix);
6dddc817 1658 python_name_help = NULL;
9a950c7c 1659#ifdef HAVE_PYTHON
ed3ef339 1660 suffix = ext_lang_auto_load_suffix (get_ext_lang_defn (EXT_LANG_PYTHON));
6dddc817
DE
1661 python_name_help
1662 = xstrprintf (_("\
1663Python scripts: OBJFILE%s\n"),
ed3ef339
DE
1664 suffix);
1665#endif
1666 guile_name_help = NULL;
1667#ifdef HAVE_GUILE
1668 suffix = ext_lang_auto_load_suffix (get_ext_lang_defn (EXT_LANG_GUILE));
1669 guile_name_help
1670 = xstrprintf (_("\
1671Guile scripts: OBJFILE%s\n"),
1672 suffix);
9a950c7c 1673#endif
6dddc817
DE
1674 scripts_directory_help
1675 = xstrprintf (_("\
1676Automatically loaded scripts are located in one of the directories listed\n\
1677by this option.\n\
1678\n\
1679Script names:\n\
ed3ef339 1680%s%s%s\
6dddc817 1681\n\
9a950c7c
JK
1682This option is ignored for the kinds of scripts \
1683having 'set auto-load ... off'.\n\
1684Directories listed here need to be present also \
1685in the 'set auto-load safe-path'\n\
6dddc817
DE
1686option."),
1687 gdb_name_help,
ed3ef339
DE
1688 python_name_help ? python_name_help : "",
1689 guile_name_help ? guile_name_help : "");
6dddc817 1690
7349ff92
JK
1691 add_setshow_optional_filename_cmd ("scripts-directory", class_support,
1692 &auto_load_dir, _("\
1693Set the list of directories from which to load auto-loaded scripts."), _("\
9a950c7c
JK
1694Show the list of directories from which to load auto-loaded scripts."),
1695 scripts_directory_help,
7349ff92
JK
1696 set_auto_load_dir, show_auto_load_dir,
1697 auto_load_set_cmdlist_get (),
1698 auto_load_show_cmdlist_get ());
9a950c7c 1699 xfree (scripts_directory_help);
6dddc817
DE
1700 xfree (python_name_help);
1701 xfree (gdb_name_help);
ed3ef339 1702 xfree (guile_name_help);
7349ff92 1703
6dea1fbd 1704 auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH);
bccbefd2
JK
1705 auto_load_safe_path_vec_update ();
1706 add_setshow_optional_filename_cmd ("safe-path", class_support,
1707 &auto_load_safe_path, _("\
9a950c7c
JK
1708Set the list of files and directories that are safe for auto-loading."), _("\
1709Show the list of files and directories that are safe for auto-loading."), _("\
bccbefd2
JK
1710Various files loaded automatically for the 'set auto-load ...' options must\n\
1711be located in one of the directories listed by this option. Warning will be\n\
af2c1515 1712printed and file will not be used otherwise.\n\
9a950c7c 1713You can mix both directory and filename entries.\n\
af2c1515
JK
1714Setting this parameter to an empty list resets it to its default value.\n\
1715Setting this parameter to '/' (without the quotes) allows any file\n\
9a950c7c 1716for the 'set auto-load ...' options. Each path entry can be also shell\n\
202cbf1c 1717wildcard pattern; '*' does not match directory separator.\n\
bccbefd2
JK
1718This option is ignored for the kinds of files having 'set auto-load ... off'.\n\
1719This options has security implications for untrusted inferiors."),
1720 set_auto_load_safe_path,
1721 show_auto_load_safe_path,
1722 auto_load_set_cmdlist_get (),
1723 auto_load_show_cmdlist_get ());
6dea1fbd 1724 observer_attach_gdb_datadir_changed (auto_load_gdb_datadir_changed);
bccbefd2
JK
1725
1726 cmd = add_cmd ("add-auto-load-safe-path", class_support,
1727 add_auto_load_safe_path,
1728 _("Add entries to the list of directories from which it is safe "
1729 "to auto-load files.\n\
1730See the commands 'set auto-load safe-path' and 'show auto-load safe-path' to\n\
1731access the current full list setting."),
1732 &cmdlist);
1733 set_cmd_completer (cmd, filename_completer);
4dc84fd1 1734
f10c5b19
JK
1735 cmd = add_cmd ("add-auto-load-scripts-directory", class_support,
1736 add_auto_load_dir,
1737 _("Add entries to the list of directories from which to load "
1738 "auto-loaded scripts.\n\
1739See the commands 'set auto-load scripts-directory' and\n\
1740'show auto-load scripts-directory' to access the current full list setting."),
1741 &cmdlist);
1742 set_cmd_completer (cmd, filename_completer);
1743
4dc84fd1
JK
1744 add_setshow_boolean_cmd ("auto-load", class_maintenance,
1745 &debug_auto_load, _("\
1746Set auto-load verifications debugging."), _("\
1747Show auto-load verifications debugging."), _("\
1748When non-zero, debugging output for files of 'set auto-load ...'\n\
1749is displayed."),
1750 NULL, show_debug_auto_load,
1751 &setdebuglist, &showdebuglist);
e2207b9a 1752}
This page took 0.352511 seconds and 4 git commands to generate.