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