Introduce and use gdb_file_up
[deliverable/binutils-gdb.git] / gdb / auto-load.c
CommitLineData
e2207b9a
JK
1/* GDB routines for supporting auto-loaded scripts.
2
61baf725 3 Copyright (C) 2012-2017 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
224c3ddb 411 filename_copy = (char *) alloca (strlen (filename) + 1);
202cbf1c 412 strcpy (filename_copy, filename);
224c3ddb 413 pattern_copy = (char *) alloca (strlen (pattern) + 1);
202cbf1c 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{
9a3c8263 583 struct auto_load_pspace_info *info = (struct auto_load_pspace_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
9a3c8263
SM
600 info = ((struct auto_load_pspace_info *)
601 program_space_data (pspace, auto_load_pspace_data));
e2207b9a
JK
602 if (info == NULL)
603 {
41bf6aca 604 info = XCNEW (struct auto_load_pspace_info);
e2207b9a
JK
605 set_program_space_data (pspace, auto_load_pspace_data, info);
606 }
607
608 return info;
609}
610
611/* Hash function for the loaded script hash. */
612
613static hashval_t
614hash_loaded_script_entry (const void *data)
615{
9a3c8263 616 const struct loaded_script *e = (const struct loaded_script *) data;
e2207b9a 617
bf88dd68 618 return htab_hash_string (e->name) ^ htab_hash_pointer (e->language);
e2207b9a
JK
619}
620
621/* Equality function for the loaded script hash. */
622
623static int
624eq_loaded_script_entry (const void *a, const void *b)
625{
9a3c8263
SM
626 const struct loaded_script *ea = (const struct loaded_script *) a;
627 const struct loaded_script *eb = (const struct loaded_script *) b;
e2207b9a 628
bf88dd68 629 return strcmp (ea->name, eb->name) == 0 && ea->language == eb->language;
e2207b9a
JK
630}
631
632/* Initialize the table to track loaded scripts.
633 Each entry is hashed by the full path name. */
634
635static void
636init_loaded_scripts_info (struct auto_load_pspace_info *pspace_info)
637{
638 /* Choose 31 as the starting size of the hash table, somewhat arbitrarily.
639 Space for each entry is obtained with one malloc so we can free them
640 easily. */
641
9f050062
DE
642 pspace_info->loaded_script_files = htab_create (31,
643 hash_loaded_script_entry,
644 eq_loaded_script_entry,
645 xfree);
646 pspace_info->loaded_script_texts = htab_create (31,
647 hash_loaded_script_entry,
648 eq_loaded_script_entry,
649 xfree);
e2207b9a 650
6dddc817 651 pspace_info->unsupported_script_warning_printed = FALSE;
e2207b9a
JK
652 pspace_info->script_not_found_warning_printed = FALSE;
653}
654
655/* Wrapper on get_auto_load_pspace_data to also allocate the hash table
656 for loading scripts. */
657
658struct auto_load_pspace_info *
659get_auto_load_pspace_data_for_loading (struct program_space *pspace)
660{
661 struct auto_load_pspace_info *info;
662
663 info = get_auto_load_pspace_data (pspace);
9f050062 664 if (info->loaded_script_files == NULL)
e2207b9a
JK
665 init_loaded_scripts_info (info);
666
667 return info;
668}
669
9f050062
DE
670/* Add script file NAME in LANGUAGE to hash table of PSPACE_INFO.
671 LOADED 1 if the script has been (is going to) be loaded, 0 otherwise
672 (such as if it has not been found).
673 FULL_PATH is NULL if the script wasn't found.
674 The result is true if the script was already in the hash table. */
e2207b9a 675
6dddc817 676static int
9f050062
DE
677maybe_add_script_file (struct auto_load_pspace_info *pspace_info, int loaded,
678 const char *name, const char *full_path,
679 const struct extension_language_defn *language)
e2207b9a 680{
9f050062 681 struct htab *htab = pspace_info->loaded_script_files;
e2207b9a
JK
682 struct loaded_script **slot, entry;
683 int in_hash_table;
684
685 entry.name = name;
bf88dd68 686 entry.language = language;
e2207b9a
JK
687 slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT);
688 in_hash_table = *slot != NULL;
689
690 /* If this script is not in the hash table, add it. */
691
9f050062 692 if (!in_hash_table)
e2207b9a
JK
693 {
694 char *p;
695
696 /* Allocate all space in one chunk so it's easier to free. */
224c3ddb
SM
697 *slot = ((struct loaded_script *)
698 xmalloc (sizeof (**slot)
699 + strlen (name) + 1
700 + (full_path != NULL ? (strlen (full_path) + 1) : 0)));
e2207b9a
JK
701 p = ((char*) *slot) + sizeof (**slot);
702 strcpy (p, name);
703 (*slot)->name = p;
704 if (full_path != NULL)
705 {
706 p += strlen (p) + 1;
707 strcpy (p, full_path);
708 (*slot)->full_path = p;
709 }
710 else
711 (*slot)->full_path = NULL;
bccbefd2 712 (*slot)->loaded = loaded;
bf88dd68 713 (*slot)->language = language;
e2207b9a
JK
714 }
715
716 return in_hash_table;
717}
718
9f050062
DE
719/* Add script contents NAME in LANGUAGE to hash table of PSPACE_INFO.
720 LOADED 1 if the script has been (is going to) be loaded, 0 otherwise
721 (such as if it has not been found).
722 The result is true if the script was already in the hash table. */
723
724static int
725maybe_add_script_text (struct auto_load_pspace_info *pspace_info,
726 int loaded, const char *name,
727 const struct extension_language_defn *language)
728{
729 struct htab *htab = pspace_info->loaded_script_texts;
730 struct loaded_script **slot, entry;
731 int in_hash_table;
732
733 entry.name = name;
734 entry.language = language;
735 slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT);
736 in_hash_table = *slot != NULL;
737
738 /* If this script is not in the hash table, add it. */
739
740 if (!in_hash_table)
741 {
742 char *p;
743
744 /* Allocate all space in one chunk so it's easier to free. */
224c3ddb
SM
745 *slot = ((struct loaded_script *)
746 xmalloc (sizeof (**slot) + strlen (name) + 1));
9f050062
DE
747 p = ((char*) *slot) + sizeof (**slot);
748 strcpy (p, name);
749 (*slot)->name = p;
750 (*slot)->full_path = NULL;
751 (*slot)->loaded = loaded;
752 (*slot)->language = language;
753 }
754
755 return in_hash_table;
756}
757
e2207b9a
JK
758/* Clear the table of loaded section scripts. */
759
760static void
761clear_section_scripts (void)
762{
763 struct program_space *pspace = current_program_space;
764 struct auto_load_pspace_info *info;
765
9a3c8263
SM
766 info = ((struct auto_load_pspace_info *)
767 program_space_data (pspace, auto_load_pspace_data));
9f050062 768 if (info != NULL && info->loaded_script_files != NULL)
e2207b9a 769 {
9f050062
DE
770 htab_delete (info->loaded_script_files);
771 htab_delete (info->loaded_script_texts);
772 info->loaded_script_files = NULL;
773 info->loaded_script_texts = NULL;
6dddc817 774 info->unsupported_script_warning_printed = FALSE;
e2207b9a
JK
775 info->script_not_found_warning_printed = FALSE;
776 }
777}
778
e9687799
JK
779/* Look for the auto-load script in LANGUAGE associated with OBJFILE where
780 OBJFILE's gdb_realpath is REALNAME and load it. Return 1 if we found any
781 matching script, return 0 otherwise. */
e2207b9a 782
e9687799
JK
783static int
784auto_load_objfile_script_1 (struct objfile *objfile, const char *realname,
6dddc817 785 const struct extension_language_defn *language)
e2207b9a 786{
e2207b9a 787 char *filename, *debugfile;
e9687799 788 int len, retval;
e2207b9a 789 struct cleanup *cleanups;
6dddc817 790 const char *suffix = ext_lang_auto_load_suffix (language);
e2207b9a 791
e2207b9a 792 len = strlen (realname);
224c3ddb 793 filename = (char *) xmalloc (len + strlen (suffix) + 1);
e2207b9a 794 memcpy (filename, realname, len);
6dddc817 795 strcpy (filename + len, suffix);
e2207b9a
JK
796
797 cleanups = make_cleanup (xfree, filename);
e2207b9a 798
d419f42d 799 gdb_file_up input = gdb_fopen_cloexec (filename, "r");
e2207b9a 800 debugfile = filename;
7349ff92
JK
801 if (debug_auto_load)
802 fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file \"%s\" %s.\n"),
803 debugfile, input ? _("exists") : _("does not exist"));
e2207b9a 804
7349ff92 805 if (!input)
e2207b9a 806 {
7349ff92
JK
807 VEC (char_ptr) *vec;
808 int ix;
809 char *dir;
810
e2207b9a
JK
811 /* Also try the same file in a subdirectory of gdb's data
812 directory. */
7349ff92 813
1564a261 814 vec = auto_load_expand_dir_vars (auto_load_dir);
7349ff92
JK
815 make_cleanup_free_char_ptr_vec (vec);
816
817 if (debug_auto_load)
818 fprintf_unfiltered (gdb_stdlog, _("auto-load: Searching 'set auto-load "
819 "scripts-directory' path \"%s\".\n"),
820 auto_load_dir);
821
822 for (ix = 0; VEC_iterate (char_ptr, vec, ix, dir); ++ix)
823 {
224c3ddb 824 debugfile = (char *) xmalloc (strlen (dir) + strlen (filename) + 1);
1564a261 825 strcpy (debugfile, dir);
7349ff92
JK
826
827 /* FILENAME is absolute, so we don't need a "/" here. */
828 strcat (debugfile, filename);
829
830 make_cleanup (xfree, debugfile);
614c279d 831 input = gdb_fopen_cloexec (debugfile, "r");
7349ff92
JK
832 if (debug_auto_load)
833 fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file "
834 "\"%s\" %s.\n"),
835 debugfile,
836 input ? _("exists") : _("does not exist"));
837 if (input != NULL)
838 break;
839 }
e2207b9a
JK
840 }
841
842 if (input)
843 {
5b2bf947
DE
844 int is_safe;
845 struct auto_load_pspace_info *pspace_info;
846
5b2bf947 847 is_safe
849c862e 848 = file_is_auto_load_safe (debugfile,
5b2bf947
DE
849 _("auto-load: Loading %s script \"%s\""
850 " by extension for objfile \"%s\".\n"),
6dddc817 851 ext_lang_name (language),
849c862e 852 debugfile, objfile_name (objfile));
5b2bf947
DE
853
854 /* Add this script to the hash table too so
855 "info auto-load ${lang}-scripts" can print it. */
856 pspace_info
857 = get_auto_load_pspace_data_for_loading (current_program_space);
9f050062
DE
858 maybe_add_script_file (pspace_info, is_safe, debugfile, debugfile,
859 language);
5b2bf947 860
e2207b9a
JK
861 /* To preserve existing behaviour we don't check for whether the
862 script was already in the table, and always load it.
863 It's highly unlikely that we'd ever load it twice,
864 and these scripts are required to be idempotent under multiple
865 loads anyway. */
5b2bf947 866 if (is_safe)
6dddc817
DE
867 {
868 objfile_script_sourcer_func *sourcer
869 = ext_lang_objfile_script_sourcer (language);
870
871 /* We shouldn't get here if support for the language isn't
872 compiled in. And the extension language is required to implement
873 this function. */
874 gdb_assert (sourcer != NULL);
d419f42d 875 sourcer (language, objfile, input.get (), debugfile);
6dddc817 876 }
e9687799
JK
877
878 retval = 1;
879 }
880 else
881 retval = 0;
882
883 do_cleanups (cleanups);
884 return retval;
885}
886
887/* Look for the auto-load script in LANGUAGE associated with OBJFILE and load
888 it. */
889
6dddc817 890void
e9687799 891auto_load_objfile_script (struct objfile *objfile,
6dddc817 892 const struct extension_language_defn *language)
e9687799 893{
6dddc817
DE
894 char *realname = gdb_realpath (objfile_name (objfile));
895 struct cleanup *cleanups = make_cleanup (xfree, realname);
e9687799
JK
896
897 if (!auto_load_objfile_script_1 (objfile, realname, language))
898 {
899 /* For Windows/DOS .exe executables, strip the .exe suffix, so that
900 FOO-gdb.gdb could be used for FOO.exe, and try again. */
901
902 size_t len = strlen (realname);
903 const size_t lexe = sizeof (".exe") - 1;
904
905 if (len > lexe && strcasecmp (realname + len - lexe, ".exe") == 0)
906 {
907 len -= lexe;
908 realname[len] = '\0';
909 if (debug_auto_load)
910 fprintf_unfiltered (gdb_stdlog, _("auto-load: Stripped .exe suffix, "
911 "retrying with \"%s\".\n"),
912 realname);
913 auto_load_objfile_script_1 (objfile, realname, language);
914 }
e2207b9a
JK
915 }
916
917 do_cleanups (cleanups);
918}
919
9f050062
DE
920/* Subroutine of source_section_scripts to simplify it.
921 Load FILE as a script in extension language LANGUAGE.
922 The script is from section SECTION_NAME in OBJFILE at offset OFFSET. */
923
924static void
925source_script_file (struct auto_load_pspace_info *pspace_info,
926 struct objfile *objfile,
927 const struct extension_language_defn *language,
928 const char *section_name, unsigned int offset,
929 const char *file)
930{
931 FILE *stream;
932 char *full_path;
933 int opened, in_hash_table;
934 struct cleanup *cleanups;
935 objfile_script_sourcer_func *sourcer;
936
937 /* Skip this script if support is not compiled in. */
938 sourcer = ext_lang_objfile_script_sourcer (language);
939 if (sourcer == NULL)
940 {
941 /* We don't throw an error, the program is still debuggable. */
942 maybe_print_unsupported_script_warning (pspace_info, objfile, language,
943 section_name, offset);
944 /* We *could* still try to open it, but there's no point. */
945 maybe_add_script_file (pspace_info, 0, file, NULL, language);
946 return;
947 }
948
949 /* Skip this script if auto-loading it has been disabled. */
950 if (!ext_lang_auto_load_enabled (language))
951 {
952 /* No message is printed, just skip it. */
953 return;
954 }
955
956 opened = find_and_open_script (file, 1 /*search_path*/,
957 &stream, &full_path);
958
959 cleanups = make_cleanup (null_cleanup, NULL);
960 if (opened)
961 {
962 make_cleanup_fclose (stream);
963 make_cleanup (xfree, full_path);
964
965 if (!file_is_auto_load_safe (full_path,
966 _("auto-load: Loading %s script "
967 "\"%s\" from section \"%s\" of "
968 "objfile \"%s\".\n"),
969 ext_lang_name (language), full_path,
970 section_name, objfile_name (objfile)))
971 opened = 0;
972 }
973 else
974 {
975 full_path = NULL;
976
977 /* If one script isn't found it's not uncommon for more to not be
978 found either. We don't want to print a message for each script,
979 too much noise. Instead, we print the warning once and tell the
980 user how to find the list of scripts that weren't loaded.
981 We don't throw an error, the program is still debuggable.
982
983 IWBN if complaints.c were more general-purpose. */
984
985 maybe_print_script_not_found_warning (pspace_info, objfile, language,
986 section_name, offset);
987 }
988
989 in_hash_table = maybe_add_script_file (pspace_info, opened, file, full_path,
990 language);
991
992 /* If this file is not currently loaded, load it. */
993 if (opened && !in_hash_table)
994 sourcer (language, objfile, stream, full_path);
995
996 do_cleanups (cleanups);
997}
998
999/* Subroutine of source_section_scripts to simplify it.
1000 Execute SCRIPT as a script in extension language LANG.
1001 The script is from section SECTION_NAME in OBJFILE at offset OFFSET. */
1002
1003static void
1004execute_script_contents (struct auto_load_pspace_info *pspace_info,
1005 struct objfile *objfile,
1006 const struct extension_language_defn *language,
1007 const char *section_name, unsigned int offset,
1008 const char *script)
1009{
1010 objfile_script_executor_func *executor;
1011 const char *newline, *script_text;
870f88f7 1012 char *name;
9f050062
DE
1013 int is_safe, in_hash_table;
1014 struct cleanup *cleanups;
1015
1016 cleanups = make_cleanup (null_cleanup, NULL);
1017
1018 /* The first line of the script is the name of the script.
1019 It must not contain any kind of space character. */
1020 name = NULL;
1021 newline = strchr (script, '\n');
1022 if (newline != NULL)
1023 {
1024 char *buf, *p;
1025
1026 /* Put the name in a buffer and validate it. */
1027 buf = xstrndup (script, newline - script);
1028 make_cleanup (xfree, buf);
1029 for (p = buf; *p != '\0'; ++p)
1030 {
1031 if (isspace (*p))
1032 break;
1033 }
1034 /* We don't allow nameless scripts, they're not helpful to the user. */
1035 if (p != buf && *p == '\0')
1036 name = buf;
1037 }
1038 if (name == NULL)
1039 {
1040 /* We don't throw an error, the program is still debuggable. */
1041 warning (_("\
1042Missing/bad script name in entry at offset %u in section %s\n\
1043of file %s."),
1044 offset, section_name, objfile_name (objfile));
1045 do_cleanups (cleanups);
1046 return;
1047 }
1048 script_text = newline + 1;
1049
1050 /* Skip this script if support is not compiled in. */
1051 executor = ext_lang_objfile_script_executor (language);
1052 if (executor == NULL)
1053 {
1054 /* We don't throw an error, the program is still debuggable. */
1055 maybe_print_unsupported_script_warning (pspace_info, objfile, language,
1056 section_name, offset);
1057 maybe_add_script_text (pspace_info, 0, name, language);
1058 do_cleanups (cleanups);
1059 return;
1060 }
1061
1062 /* Skip this script if auto-loading it has been disabled. */
1063 if (!ext_lang_auto_load_enabled (language))
1064 {
1065 /* No message is printed, just skip it. */
1066 do_cleanups (cleanups);
1067 return;
1068 }
1069
1070 is_safe = file_is_auto_load_safe (objfile_name (objfile),
1071 _("auto-load: Loading %s script "
1072 "\"%s\" from section \"%s\" of "
1073 "objfile \"%s\".\n"),
1074 ext_lang_name (language), name,
1075 section_name, objfile_name (objfile));
1076
1077 in_hash_table = maybe_add_script_text (pspace_info, is_safe, name, language);
1078
1079 /* If this file is not currently loaded, load it. */
1080 if (is_safe && !in_hash_table)
1081 executor (language, objfile, name, script_text);
1082
1083 do_cleanups (cleanups);
1084}
1085
5b2bf947
DE
1086/* Load scripts specified in OBJFILE.
1087 START,END delimit a buffer containing a list of nul-terminated
1088 file names.
1089 SECTION_NAME is used in error messages.
1090
9f050062
DE
1091 Scripts specified as file names are found per normal "source -s" command
1092 processing. First the script is looked for in $cwd. If not found there
1093 the source search path is used.
5b2bf947 1094
9f050062
DE
1095 The section contains a list of path names of script files to load or
1096 actual script contents. Each entry is nul-terminated. */
5b2bf947
DE
1097
1098static void
1099source_section_scripts (struct objfile *objfile, const char *section_name,
1100 const char *start, const char *end)
1101{
1102 const char *p;
1103 struct auto_load_pspace_info *pspace_info;
1104
1105 pspace_info = get_auto_load_pspace_data_for_loading (current_program_space);
1106
1107 for (p = start; p < end; ++p)
1108 {
9f050062 1109 const char *entry;
ed3ef339 1110 const struct extension_language_defn *language;
9f050062
DE
1111 unsigned int offset = p - start;
1112 int code = *p;
5b2bf947 1113
9f050062 1114 switch (code)
5b2bf947 1115 {
ed3ef339 1116 case SECTION_SCRIPT_ID_PYTHON_FILE:
9f050062 1117 case SECTION_SCRIPT_ID_PYTHON_TEXT:
ed3ef339
DE
1118 language = get_ext_lang_defn (EXT_LANG_PYTHON);
1119 break;
1120 case SECTION_SCRIPT_ID_SCHEME_FILE:
9f050062 1121 case SECTION_SCRIPT_ID_SCHEME_TEXT:
ed3ef339
DE
1122 language = get_ext_lang_defn (EXT_LANG_GUILE);
1123 break;
1124 default:
5b2bf947
DE
1125 warning (_("Invalid entry in %s section"), section_name);
1126 /* We could try various heuristics to find the next valid entry,
1127 but it's safer to just punt. */
ed3ef339 1128 return;
5b2bf947 1129 }
9f050062 1130 entry = ++p;
5b2bf947
DE
1131
1132 while (p < end && *p != '\0')
1133 ++p;
1134 if (p == end)
1135 {
9f050062
DE
1136 warning (_("Non-nul-terminated entry in %s at offset %u"),
1137 section_name, offset);
1138 /* Don't load/execute it. */
5b2bf947
DE
1139 break;
1140 }
6dddc817 1141
9f050062 1142 switch (code)
6dddc817 1143 {
9f050062
DE
1144 case SECTION_SCRIPT_ID_PYTHON_FILE:
1145 case SECTION_SCRIPT_ID_SCHEME_FILE:
1146 if (p == entry)
6dddc817 1147 {
9f050062
DE
1148 warning (_("Empty entry in %s at offset %u"),
1149 section_name, offset);
1150 continue;
6dddc817 1151 }
9f050062
DE
1152 source_script_file (pspace_info, objfile, language,
1153 section_name, offset, entry);
1154 break;
1155 case SECTION_SCRIPT_ID_PYTHON_TEXT:
1156 case SECTION_SCRIPT_ID_SCHEME_TEXT:
1157 execute_script_contents (pspace_info, objfile, language,
1158 section_name, offset, entry);
1159 break;
5b2bf947 1160 }
5b2bf947
DE
1161 }
1162}
1163
1164/* Load scripts specified in section SECTION_NAME of OBJFILE. */
1165
1166static void
1167auto_load_section_scripts (struct objfile *objfile, const char *section_name)
1168{
1169 bfd *abfd = objfile->obfd;
1170 asection *scripts_sect;
1171 bfd_byte *data = NULL;
1172
1173 scripts_sect = bfd_get_section_by_name (abfd, section_name);
ec13808e
JK
1174 if (scripts_sect == NULL
1175 || (bfd_get_section_flags (abfd, scripts_sect) & SEC_HAS_CONTENTS) == 0)
5b2bf947
DE
1176 return;
1177
1178 if (!bfd_get_full_section_contents (abfd, scripts_sect, &data))
1179 warning (_("Couldn't read %s section of %s"),
1180 section_name, bfd_get_filename (abfd));
1181 else
1182 {
1183 struct cleanup *cleanups;
1184 char *p = (char *) data;
1185
1186 cleanups = make_cleanup (xfree, p);
1187 source_section_scripts (objfile, section_name, p,
1188 p + bfd_get_section_size (scripts_sect));
1189 do_cleanups (cleanups);
1190 }
1191}
1192
bf88dd68
JK
1193/* Load any auto-loaded scripts for OBJFILE. */
1194
1195void
1196load_auto_scripts_for_objfile (struct objfile *objfile)
1197{
c47cf547
DE
1198 /* Return immediately if auto-loading has been globally disabled.
1199 This is to handle sequencing of operations during gdb startup.
5fbae7d1
GB
1200 Also return immediately if OBJFILE was not created from a file
1201 on the local filesystem. */
1202 if (!global_auto_load
1203 || (objfile->flags & OBJF_NOT_FILENAME) != 0
1204 || is_target_filename (objfile->original_name))
bf88dd68
JK
1205 return;
1206
6dddc817
DE
1207 /* Load any extension language scripts for this objfile.
1208 E.g., foo-gdb.gdb, foo-gdb.py. */
1209 auto_load_ext_lang_scripts_for_objfile (objfile);
bf88dd68 1210
c47cf547 1211 /* Load any scripts mentioned in AUTO_SECTION_NAME (.debug_gdb_scripts). */
5b2bf947 1212 auto_load_section_scripts (objfile, AUTO_SECTION_NAME);
bf88dd68
JK
1213}
1214
e2207b9a
JK
1215/* This is a new_objfile observer callback to auto-load scripts.
1216
1217 Two flavors of auto-loaded scripts are supported.
1218 1) based on the path to the objfile
1219 2) from .debug_gdb_scripts section */
1220
1221static void
1222auto_load_new_objfile (struct objfile *objfile)
1223{
1224 if (!objfile)
1225 {
1226 /* OBJFILE is NULL when loading a new "main" symbol-file. */
1227 clear_section_scripts ();
1228 return;
1229 }
1230
1231 load_auto_scripts_for_objfile (objfile);
1232}
1233
1234/* Collect scripts to be printed in a vec. */
1235
1236typedef struct loaded_script *loaded_script_ptr;
1237DEF_VEC_P (loaded_script_ptr);
1238
bf88dd68
JK
1239struct collect_matching_scripts_data
1240{
1241 VEC (loaded_script_ptr) **scripts_p;
1242
6dddc817 1243 const struct extension_language_defn *language;
bf88dd68
JK
1244};
1245
e2207b9a
JK
1246/* Traversal function for htab_traverse.
1247 Collect the entry if it matches the regexp. */
1248
1249static int
1250collect_matching_scripts (void **slot, void *info)
1251{
9a3c8263
SM
1252 struct loaded_script *script = (struct loaded_script *) *slot;
1253 struct collect_matching_scripts_data *data
1254 = (struct collect_matching_scripts_data *) info;
e2207b9a 1255
bf88dd68
JK
1256 if (script->language == data->language && re_exec (script->name))
1257 VEC_safe_push (loaded_script_ptr, *data->scripts_p, script);
e2207b9a
JK
1258
1259 return 1;
1260}
1261
1262/* Print SCRIPT. */
1263
1264static void
1265print_script (struct loaded_script *script)
1266{
1267 struct ui_out *uiout = current_uiout;
e2207b9a 1268
2e783024 1269 ui_out_emit_tuple tuple_emitter (uiout, NULL);
e2207b9a 1270
112e8700
SM
1271 uiout->field_string ("loaded", script->loaded ? "Yes" : "No");
1272 uiout->field_string ("script", script->name);
1273 uiout->text ("\n");
e2207b9a
JK
1274
1275 /* If the name isn't the full path, print it too. */
1276 if (script->full_path != NULL
1277 && strcmp (script->name, script->full_path) != 0)
1278 {
112e8700
SM
1279 uiout->text ("\tfull name: ");
1280 uiout->field_string ("full_path", script->full_path);
1281 uiout->text ("\n");
e2207b9a 1282 }
e2207b9a
JK
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{
9f050062
DE
1307 int i;
1308 loaded_script_ptr script;
1309
1310 qsort (VEC_address (loaded_script_ptr, scripts),
1311 VEC_length (loaded_script_ptr, scripts),
1312 sizeof (loaded_script_ptr), sort_scripts_by_name);
1313 for (i = 0; VEC_iterate (loaded_script_ptr, scripts, i, script); ++i)
1314 print_script (script);
1315}
1316
bf88dd68
JK
1317/* Implementation for "info auto-load gdb-scripts"
1318 (and "info auto-load python-scripts"). List scripts in LANGUAGE matching
1319 PATTERN. FROM_TTY is the usual GDB boolean for user interactivity. */
1320
1321void
1322auto_load_info_scripts (char *pattern, int from_tty,
6dddc817 1323 const struct extension_language_defn *language)
e2207b9a
JK
1324{
1325 struct ui_out *uiout = current_uiout;
1326 struct auto_load_pspace_info *pspace_info;
1327 struct cleanup *script_chain;
9f050062 1328 VEC (loaded_script_ptr) *script_files, *script_texts;
e2207b9a
JK
1329 int nr_scripts;
1330
1331 dont_repeat ();
1332
1333 pspace_info = get_auto_load_pspace_data (current_program_space);
1334
1335 if (pattern && *pattern)
1336 {
1337 char *re_err = re_comp (pattern);
1338
1339 if (re_err)
1340 error (_("Invalid regexp: %s"), re_err);
1341 }
1342 else
1343 {
1344 re_comp ("");
1345 }
1346
1347 /* We need to know the number of rows before we build the table.
1348 Plus we want to sort the scripts by name.
1349 So first traverse the hash table collecting the matching scripts. */
1350
9f050062
DE
1351 script_files = VEC_alloc (loaded_script_ptr, 10);
1352 script_texts = VEC_alloc (loaded_script_ptr, 10);
1353 script_chain = make_cleanup (VEC_cleanup (loaded_script_ptr), &script_files);
1354 make_cleanup (VEC_cleanup (loaded_script_ptr), &script_texts);
1355
1356 if (pspace_info != NULL && pspace_info->loaded_script_files != NULL)
1357 {
1358 struct collect_matching_scripts_data data = { &script_files, language };
1359
1360 /* Pass a pointer to scripts as VEC_safe_push can realloc space. */
1361 htab_traverse_noresize (pspace_info->loaded_script_files,
1362 collect_matching_scripts, &data);
1363 }
e2207b9a 1364
9f050062 1365 if (pspace_info != NULL && pspace_info->loaded_script_texts != NULL)
e2207b9a 1366 {
9f050062 1367 struct collect_matching_scripts_data data = { &script_texts, language };
bf88dd68 1368
e2207b9a 1369 /* Pass a pointer to scripts as VEC_safe_push can realloc space. */
9f050062 1370 htab_traverse_noresize (pspace_info->loaded_script_texts,
bf88dd68 1371 collect_matching_scripts, &data);
e2207b9a
JK
1372 }
1373
9f050062
DE
1374 nr_scripts = (VEC_length (loaded_script_ptr, script_files)
1375 + VEC_length (loaded_script_ptr, script_texts));
bf88dd68
JK
1376
1377 /* Table header shifted right by preceding "gdb-scripts: " would not match
1378 its columns. */
1379 if (nr_scripts > 0 && pattern == auto_load_info_scripts_pattern_nl)
112e8700 1380 uiout->text ("\n");
bf88dd68 1381
4a2b031d
TT
1382 {
1383 ui_out_emit_table table_emitter (uiout, 2, nr_scripts,
1384 "AutoLoadedScriptsTable");
e2207b9a 1385
4a2b031d
TT
1386 uiout->table_header (7, ui_left, "loaded", "Loaded");
1387 uiout->table_header (70, ui_left, "script", "Script");
1388 uiout->table_body ();
e2207b9a 1389
4a2b031d
TT
1390 print_scripts (script_files);
1391 print_scripts (script_texts);
1392 }
e2207b9a
JK
1393
1394 do_cleanups (script_chain);
1395
1396 if (nr_scripts == 0)
1397 {
1398 if (pattern && *pattern)
112e8700 1399 uiout->message ("No auto-load scripts matching %s.\n", pattern);
e2207b9a 1400 else
112e8700 1401 uiout->message ("No auto-load scripts.\n");
e2207b9a
JK
1402 }
1403}
1404
bf88dd68
JK
1405/* Wrapper for "info auto-load gdb-scripts". */
1406
1407static void
1408info_auto_load_gdb_scripts (char *pattern, int from_tty)
1409{
6dddc817 1410 auto_load_info_scripts (pattern, from_tty, &extension_language_gdb);
bf88dd68
JK
1411}
1412
1413/* Implement 'info auto-load local-gdbinit'. */
1414
1415static void
1416info_auto_load_local_gdbinit (char *args, int from_tty)
1417{
1418 if (auto_load_local_gdbinit_pathname == NULL)
1419 printf_filtered (_("Local .gdbinit file was not found.\n"));
1420 else if (auto_load_local_gdbinit_loaded)
1421 printf_filtered (_("Local .gdbinit file \"%s\" has been loaded.\n"),
1422 auto_load_local_gdbinit_pathname);
1423 else
1424 printf_filtered (_("Local .gdbinit file \"%s\" has not been loaded.\n"),
1425 auto_load_local_gdbinit_pathname);
1426}
1427
9f050062
DE
1428/* Print an "unsupported script" warning if it has not already been printed.
1429 The script is in language LANGUAGE at offset OFFSET in section SECTION_NAME
1430 of OBJFILE. */
6dddc817 1431
9f050062
DE
1432static void
1433maybe_print_unsupported_script_warning
1434 (struct auto_load_pspace_info *pspace_info,
1435 struct objfile *objfile, const struct extension_language_defn *language,
1436 const char *section_name, unsigned offset)
6dddc817 1437{
9f050062
DE
1438 if (!pspace_info->unsupported_script_warning_printed)
1439 {
1440 warning (_("\
1441Unsupported auto-load script at offset %u in section %s\n\
1442of file %s.\n\
1443Use `info auto-load %s-scripts [REGEXP]' to list them."),
1444 offset, section_name, objfile_name (objfile),
1445 ext_lang_name (language));
1446 pspace_info->unsupported_script_warning_printed = 1;
1447 }
6dddc817
DE
1448}
1449
e2207b9a
JK
1450/* Return non-zero if SCRIPT_NOT_FOUND_WARNING_PRINTED of PSPACE_INFO was unset
1451 before calling this function. Always set SCRIPT_NOT_FOUND_WARNING_PRINTED
1452 of PSPACE_INFO. */
1453
9f050062
DE
1454static void
1455maybe_print_script_not_found_warning
1456 (struct auto_load_pspace_info *pspace_info,
1457 struct objfile *objfile, const struct extension_language_defn *language,
1458 const char *section_name, unsigned offset)
e2207b9a 1459{
9f050062
DE
1460 if (!pspace_info->script_not_found_warning_printed)
1461 {
1462 warning (_("\
1463Missing auto-load script at offset %u in section %s\n\
1464of file %s.\n\
1465Use `info auto-load %s-scripts [REGEXP]' to list them."),
1466 offset, section_name, objfile_name (objfile),
1467 ext_lang_name (language));
1468 pspace_info->script_not_found_warning_printed = 1;
1469 }
e2207b9a
JK
1470}
1471
bf88dd68
JK
1472/* The only valid "set auto-load" argument is off|0|no|disable. */
1473
1474static void
1475set_auto_load_cmd (char *args, int from_tty)
1476{
1477 struct cmd_list_element *list;
1478 size_t length;
1479
1480 /* See parse_binary_operation in use by the sub-commands. */
1481
1482 length = args ? strlen (args) : 0;
1483
1484 while (length > 0 && (args[length - 1] == ' ' || args[length - 1] == '\t'))
1485 length--;
1486
1487 if (length == 0 || (strncmp (args, "off", length) != 0
1488 && strncmp (args, "0", length) != 0
1489 && strncmp (args, "no", length) != 0
1490 && strncmp (args, "disable", length) != 0))
1491 error (_("Valid is only global 'set auto-load no'; "
1492 "otherwise check the auto-load sub-commands."));
1493
1494 for (list = *auto_load_set_cmdlist_get (); list != NULL; list = list->next)
1495 if (list->var_type == var_boolean)
1496 {
1497 gdb_assert (list->type == set_cmd);
5b9afe8a 1498 do_set_command (args, from_tty, list);
bf88dd68
JK
1499 }
1500}
1501
1502/* Initialize "set auto-load " commands prefix and return it. */
1503
1504struct cmd_list_element **
1505auto_load_set_cmdlist_get (void)
1506{
1507 static struct cmd_list_element *retval;
1508
1509 if (retval == NULL)
1510 add_prefix_cmd ("auto-load", class_maintenance, set_auto_load_cmd, _("\
1511Auto-loading specific settings.\n\
1512Configure various auto-load-specific variables such as\n\
1513automatic loading of Python scripts."),
1514 &retval, "set auto-load ",
1515 1/*allow-unknown*/, &setlist);
1516
1517 return &retval;
1518}
1519
1520/* Command "show auto-load" displays summary of all the current
1521 "show auto-load " settings. */
1522
1523static void
1524show_auto_load_cmd (char *args, int from_tty)
1525{
1526 cmd_show_list (*auto_load_show_cmdlist_get (), from_tty, "");
1527}
1528
1529/* Initialize "show auto-load " commands prefix and return it. */
1530
1531struct cmd_list_element **
1532auto_load_show_cmdlist_get (void)
1533{
1534 static struct cmd_list_element *retval;
1535
1536 if (retval == NULL)
1537 add_prefix_cmd ("auto-load", class_maintenance, show_auto_load_cmd, _("\
1538Show auto-loading specific settings.\n\
1539Show configuration of various auto-load-specific variables such as\n\
1540automatic loading of Python scripts."),
1541 &retval, "show auto-load ",
1542 0/*allow-unknown*/, &showlist);
1543
1544 return &retval;
1545}
1546
1547/* Command "info auto-load" displays whether the various auto-load files have
1548 been loaded. This is reimplementation of cmd_show_list which inserts
1549 newlines at proper places. */
1550
1551static void
1552info_auto_load_cmd (char *args, int from_tty)
1553{
1554 struct cmd_list_element *list;
1555 struct cleanup *infolist_chain;
1556 struct ui_out *uiout = current_uiout;
1557
2e783024 1558 ui_out_emit_tuple tuple_emitter (uiout, "infolist");
bf88dd68
JK
1559
1560 for (list = *auto_load_info_cmdlist_get (); list != NULL; list = list->next)
1561 {
2e783024 1562 ui_out_emit_tuple option_emitter (uiout, "option");
bf88dd68
JK
1563
1564 gdb_assert (!list->prefixlist);
1565 gdb_assert (list->type == not_set_cmd);
1566
112e8700
SM
1567 uiout->field_string ("name", list->name);
1568 uiout->text (": ");
bf88dd68 1569 cmd_func (list, auto_load_info_scripts_pattern_nl, from_tty);
bf88dd68 1570 }
bf88dd68
JK
1571}
1572
1573/* Initialize "info auto-load " commands prefix and return it. */
1574
1575struct cmd_list_element **
1576auto_load_info_cmdlist_get (void)
1577{
1578 static struct cmd_list_element *retval;
1579
1580 if (retval == NULL)
1581 add_prefix_cmd ("auto-load", class_info, info_auto_load_cmd, _("\
1582Print current status of auto-loaded files.\n\
1583Print whether various files like Python scripts or .gdbinit files have been\n\
1584found and/or loaded."),
1585 &retval, "info auto-load ",
1586 0/*allow-unknown*/, &infolist);
1587
1588 return &retval;
1589}
1590
e2207b9a
JK
1591void _initialize_auto_load (void);
1592
1593void
1594_initialize_auto_load (void)
1595{
bccbefd2 1596 struct cmd_list_element *cmd;
6dddc817 1597 char *scripts_directory_help, *gdb_name_help, *python_name_help;
ed3ef339
DE
1598 char *guile_name_help;
1599 const char *suffix;
bccbefd2 1600
e2207b9a 1601 auto_load_pspace_data
8e260fc0
TT
1602 = register_program_space_data_with_cleanup (NULL,
1603 auto_load_pspace_data_cleanup);
e2207b9a
JK
1604
1605 observer_attach_new_objfile (auto_load_new_objfile);
1606
bf88dd68
JK
1607 add_setshow_boolean_cmd ("gdb-scripts", class_support,
1608 &auto_load_gdb_scripts, _("\
1609Enable or disable auto-loading of canned sequences of commands scripts."), _("\
1610Show whether auto-loading of canned sequences of commands scripts is enabled."),
1611 _("\
1612If enabled, canned sequences of commands are loaded when the debugger reads\n\
1613an executable or shared library.\n\
1614This options has security implications for untrusted inferiors."),
1615 NULL, show_auto_load_gdb_scripts,
1616 auto_load_set_cmdlist_get (),
1617 auto_load_show_cmdlist_get ());
1618
1619 add_cmd ("gdb-scripts", class_info, info_auto_load_gdb_scripts,
1620 _("Print the list of automatically loaded sequences of commands.\n\
1621Usage: info auto-load gdb-scripts [REGEXP]"),
1622 auto_load_info_cmdlist_get ());
1623
1624 add_setshow_boolean_cmd ("local-gdbinit", class_support,
1625 &auto_load_local_gdbinit, _("\
1626Enable or disable auto-loading of .gdbinit script in current directory."), _("\
1627Show whether auto-loading .gdbinit script in current directory is enabled."),
1628 _("\
1629If enabled, canned sequences of commands are loaded when debugger starts\n\
1630from .gdbinit file in current directory. Such files are deprecated,\n\
1631use a script associated with inferior executable file instead.\n\
1632This options has security implications for untrusted inferiors."),
1633 NULL, show_auto_load_local_gdbinit,
1634 auto_load_set_cmdlist_get (),
1635 auto_load_show_cmdlist_get ());
1636
1637 add_cmd ("local-gdbinit", class_info, info_auto_load_local_gdbinit,
1638 _("Print whether current directory .gdbinit file has been loaded.\n\
1639Usage: info auto-load local-gdbinit"),
1640 auto_load_info_cmdlist_get ());
bccbefd2 1641
7349ff92 1642 auto_load_dir = xstrdup (AUTO_LOAD_DIR);
6dddc817 1643
ed3ef339 1644 suffix = ext_lang_auto_load_suffix (get_ext_lang_defn (EXT_LANG_GDB));
6dddc817
DE
1645 gdb_name_help
1646 = xstrprintf (_("\
1647GDB scripts: OBJFILE%s\n"),
ed3ef339 1648 suffix);
6dddc817 1649 python_name_help = NULL;
9a950c7c 1650#ifdef HAVE_PYTHON
ed3ef339 1651 suffix = ext_lang_auto_load_suffix (get_ext_lang_defn (EXT_LANG_PYTHON));
6dddc817
DE
1652 python_name_help
1653 = xstrprintf (_("\
1654Python scripts: OBJFILE%s\n"),
ed3ef339
DE
1655 suffix);
1656#endif
1657 guile_name_help = NULL;
1658#ifdef HAVE_GUILE
1659 suffix = ext_lang_auto_load_suffix (get_ext_lang_defn (EXT_LANG_GUILE));
1660 guile_name_help
1661 = xstrprintf (_("\
1662Guile scripts: OBJFILE%s\n"),
1663 suffix);
9a950c7c 1664#endif
6dddc817
DE
1665 scripts_directory_help
1666 = xstrprintf (_("\
1667Automatically loaded scripts are located in one of the directories listed\n\
1668by this option.\n\
1669\n\
1670Script names:\n\
ed3ef339 1671%s%s%s\
6dddc817 1672\n\
9a950c7c
JK
1673This option is ignored for the kinds of scripts \
1674having 'set auto-load ... off'.\n\
1675Directories listed here need to be present also \
1676in the 'set auto-load safe-path'\n\
6dddc817
DE
1677option."),
1678 gdb_name_help,
ed3ef339
DE
1679 python_name_help ? python_name_help : "",
1680 guile_name_help ? guile_name_help : "");
6dddc817 1681
7349ff92
JK
1682 add_setshow_optional_filename_cmd ("scripts-directory", class_support,
1683 &auto_load_dir, _("\
1684Set the list of directories from which to load auto-loaded scripts."), _("\
9a950c7c
JK
1685Show the list of directories from which to load auto-loaded scripts."),
1686 scripts_directory_help,
7349ff92
JK
1687 set_auto_load_dir, show_auto_load_dir,
1688 auto_load_set_cmdlist_get (),
1689 auto_load_show_cmdlist_get ());
9a950c7c 1690 xfree (scripts_directory_help);
6dddc817
DE
1691 xfree (python_name_help);
1692 xfree (gdb_name_help);
ed3ef339 1693 xfree (guile_name_help);
7349ff92 1694
6dea1fbd 1695 auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH);
bccbefd2
JK
1696 auto_load_safe_path_vec_update ();
1697 add_setshow_optional_filename_cmd ("safe-path", class_support,
1698 &auto_load_safe_path, _("\
9a950c7c
JK
1699Set the list of files and directories that are safe for auto-loading."), _("\
1700Show the list of files and directories that are safe for auto-loading."), _("\
bccbefd2
JK
1701Various files loaded automatically for the 'set auto-load ...' options must\n\
1702be located in one of the directories listed by this option. Warning will be\n\
af2c1515 1703printed and file will not be used otherwise.\n\
9a950c7c 1704You can mix both directory and filename entries.\n\
af2c1515
JK
1705Setting this parameter to an empty list resets it to its default value.\n\
1706Setting this parameter to '/' (without the quotes) allows any file\n\
9a950c7c 1707for the 'set auto-load ...' options. Each path entry can be also shell\n\
202cbf1c 1708wildcard pattern; '*' does not match directory separator.\n\
bccbefd2
JK
1709This option is ignored for the kinds of files having 'set auto-load ... off'.\n\
1710This options has security implications for untrusted inferiors."),
1711 set_auto_load_safe_path,
1712 show_auto_load_safe_path,
1713 auto_load_set_cmdlist_get (),
1714 auto_load_show_cmdlist_get ());
6dea1fbd 1715 observer_attach_gdb_datadir_changed (auto_load_gdb_datadir_changed);
bccbefd2
JK
1716
1717 cmd = add_cmd ("add-auto-load-safe-path", class_support,
1718 add_auto_load_safe_path,
1719 _("Add entries to the list of directories from which it is safe "
1720 "to auto-load files.\n\
1721See the commands 'set auto-load safe-path' and 'show auto-load safe-path' to\n\
1722access the current full list setting."),
1723 &cmdlist);
1724 set_cmd_completer (cmd, filename_completer);
4dc84fd1 1725
f10c5b19
JK
1726 cmd = add_cmd ("add-auto-load-scripts-directory", class_support,
1727 add_auto_load_dir,
1728 _("Add entries to the list of directories from which to load "
1729 "auto-loaded scripts.\n\
1730See the commands 'set auto-load scripts-directory' and\n\
1731'show auto-load scripts-directory' to access the current full list setting."),
1732 &cmdlist);
1733 set_cmd_completer (cmd, filename_completer);
1734
4dc84fd1
JK
1735 add_setshow_boolean_cmd ("auto-load", class_maintenance,
1736 &debug_auto_load, _("\
1737Set auto-load verifications debugging."), _("\
1738Show auto-load verifications debugging."), _("\
1739When non-zero, debugging output for files of 'set auto-load ...'\n\
1740is displayed."),
1741 NULL, show_debug_auto_load,
1742 &setdebuglist, &showdebuglist);
e2207b9a 1743}
This page took 0.422386 seconds and 4 git commands to generate.