Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: gdb/auto-load.c

Issue 11969036: Merge GDB 7.5.1 (Closed) Base URL: http://git.chromium.org/native_client/nacl-gdb.git@master
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « gdb/auto-load.h ('k') | gdb/auxv.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /* GDB routines for supporting auto-loaded scripts.
2
3 Copyright (C) 2012 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "auto-load.h"
22 #include "progspace.h"
23 #include "python/python.h"
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"
30 #include "exceptions.h"
31 #include "cli/cli-script.h"
32 #include "gdbcmd.h"
33 #include "cli/cli-decode.h"
34 #include "cli/cli-setshow.h"
35 #include "gdb_vecs.h"
36 #include "readline/tilde.h"
37 #include "completer.h"
38 #include "observer.h"
39 #include "fnmatch.h"
40 #include "top.h"
41
42 /* The suffix of per-objfile scripts to auto-load as non-Python command files.
43 E.g. When the program loads libfoo.so, look for libfoo-gdb.gdb. */
44 #define GDB_AUTO_FILE_NAME "-gdb.gdb"
45
46 static void source_gdb_script_for_objfile (struct objfile *objfile, FILE *file,
47 const char *filename);
48
49 /* Value of the 'set debug auto-load' configuration variable. */
50 static int debug_auto_load = 0;
51
52 /* "show" command for the debug_auto_load configuration variable. */
53
54 static void
55 show_debug_auto_load (struct ui_file *file, int from_tty,
56 struct cmd_list_element *c, const char *value)
57 {
58 fprintf_filtered (file, _("Debugging output for files "
59 "of 'set auto-load ...' is %s.\n"),
60 value);
61 }
62
63 /* User-settable option to enable/disable auto-loading of GDB_AUTO_FILE_NAME
64 scripts:
65 set auto-load gdb-scripts on|off
66 This is true if we should auto-load associated scripts when an objfile
67 is opened, false otherwise. */
68 static int auto_load_gdb_scripts = 1;
69
70 /* "show" command for the auto_load_gdb_scripts configuration variable. */
71
72 static void
73 show_auto_load_gdb_scripts (struct ui_file *file, int from_tty,
74 struct cmd_list_element *c, const char *value)
75 {
76 fprintf_filtered (file, _("Auto-loading of canned sequences of commands "
77 "scripts is %s.\n"),
78 value);
79 }
80
81 /* Internal-use flag to enable/disable auto-loading.
82 This is true if we should auto-load python code when an objfile is opened,
83 false otherwise.
84
85 Both auto_load_scripts && global_auto_load must be true to enable
86 auto-loading.
87
88 This flag exists to facilitate deferring auto-loading during start-up
89 until after ./.gdbinit has been read; it may augment the search directories
90 used to find the scripts. */
91 int global_auto_load = 1;
92
93 /* Auto-load .gdbinit file from the current directory? */
94 int auto_load_local_gdbinit = 1;
95
96 /* Absolute pathname to the current directory .gdbinit, if it exists. */
97 char *auto_load_local_gdbinit_pathname = NULL;
98
99 /* Boolean value if AUTO_LOAD_LOCAL_GDBINIT_PATHNAME has been loaded. */
100 int auto_load_local_gdbinit_loaded = 0;
101
102 /* "show" command for the auto_load_local_gdbinit configuration variable. */
103
104 static void
105 show_auto_load_local_gdbinit (struct ui_file *file, int from_tty,
106 struct cmd_list_element *c, const char *value)
107 {
108 fprintf_filtered (file, _("Auto-loading of .gdbinit script from current "
109 "directory is %s.\n"),
110 value);
111 }
112
113 /* Directory list from which to load auto-loaded scripts. It is not checked
114 for absolute paths but they are strongly recommended. It is initialized by
115 _initialize_auto_load. */
116 static char *auto_load_dir;
117
118 /* "set" command for the auto_load_dir configuration variable. */
119
120 static void
121 set_auto_load_dir (char *args, int from_tty, struct cmd_list_element *c)
122 {
123 /* Setting the variable to "" resets it to the compile time defaults. */
124 if (auto_load_dir[0] == '\0')
125 {
126 xfree (auto_load_dir);
127 auto_load_dir = xstrdup (AUTO_LOAD_DIR);
128 }
129 }
130
131 /* "show" command for the auto_load_dir configuration variable. */
132
133 static void
134 show_auto_load_dir (struct ui_file *file, int from_tty,
135 struct cmd_list_element *c, const char *value)
136 {
137 fprintf_filtered (file, _("List of directories from which to load "
138 "auto-loaded scripts is %s.\n"),
139 value);
140 }
141
142 /* Directory list safe to hold auto-loaded files. It is not checked for
143 absolute paths but they are strongly recommended. It is initialized by
144 _initialize_auto_load. */
145 static char *auto_load_safe_path;
146
147 /* Vector of directory elements of AUTO_LOAD_SAFE_PATH with each one normalized
148 by tilde_expand and possibly each entries has added its gdb_realpath
149 counterpart. */
150 static VEC (char_ptr) *auto_load_safe_path_vec;
151
152 /* Expand $datadir and $debugdir in STRING according to the rules of
153 substitute_path_component. Return vector from dirnames_to_char_ptr_vec,
154 this vector must be freed by free_char_ptr_vec by the caller. */
155
156 static VEC (char_ptr) *
157 auto_load_expand_dir_vars (const char *string)
158 {
159 VEC (char_ptr) *dir_vec;
160 char *s;
161
162 s = xstrdup (string);
163 substitute_path_component (&s, "$datadir", gdb_datadir);
164 substitute_path_component (&s, "$debugdir", debug_file_directory);
165
166 if (debug_auto_load && strcmp (s, string) != 0)
167 fprintf_unfiltered (gdb_stdlog,
168 _("auto-load: Expanded $-variables to \"%s\".\n"), s);
169
170 dir_vec = dirnames_to_char_ptr_vec (s);
171 xfree(s);
172
173 return dir_vec;
174 }
175
176 /* Update auto_load_safe_path_vec from current AUTO_LOAD_SAFE_PATH. */
177
178 static void
179 auto_load_safe_path_vec_update (void)
180 {
181 VEC (char_ptr) *dir_vec = NULL;
182 unsigned len;
183 int ix;
184
185 if (debug_auto_load)
186 fprintf_unfiltered (gdb_stdlog,
187 _("auto-load: Updating directories of \"%s\".\n"),
188 auto_load_safe_path);
189
190 free_char_ptr_vec (auto_load_safe_path_vec);
191
192 auto_load_safe_path_vec = auto_load_expand_dir_vars (auto_load_safe_path);
193 len = VEC_length (char_ptr, auto_load_safe_path_vec);
194
195 /* Apply tilde_expand and gdb_realpath to each AUTO_LOAD_SAFE_PATH_VEC
196 element. */
197 for (ix = 0; ix < len; ix++)
198 {
199 char *dir = VEC_index (char_ptr, auto_load_safe_path_vec, ix);
200 char *expanded = tilde_expand (dir);
201 char *real_path = gdb_realpath (expanded);
202
203 /* Ensure the current entry is at least tilde_expand-ed. */
204 VEC_replace (char_ptr, auto_load_safe_path_vec, ix, expanded);
205
206 if (debug_auto_load)
207 {
208 if (strcmp (expanded, dir) == 0)
209 fprintf_unfiltered (gdb_stdlog,
210 _("auto-load: Using directory \"%s\".\n"),
211 expanded);
212 else
213 fprintf_unfiltered (gdb_stdlog,
214 _("auto-load: Resolved directory \"%s\" "
215 "as \"%s\".\n"),
216 dir, expanded);
217 }
218 xfree (dir);
219
220 /* If gdb_realpath returns a different content, append it. */
221 if (strcmp (real_path, expanded) == 0)
222 xfree (real_path);
223 else
224 {
225 VEC_safe_push (char_ptr, auto_load_safe_path_vec, real_path);
226
227 if (debug_auto_load)
228 fprintf_unfiltered (gdb_stdlog,
229 _("auto-load: And canonicalized as \"%s\".\n"),
230 real_path);
231 }
232 }
233 }
234
235 /* Variable gdb_datadir has been set. Update content depending on $datadir. */
236
237 static void
238 auto_load_gdb_datadir_changed (void)
239 {
240 auto_load_safe_path_vec_update ();
241 }
242
243 /* "set" command for the auto_load_safe_path configuration variable. */
244
245 static void
246 set_auto_load_safe_path (char *args, int from_tty, struct cmd_list_element *c)
247 {
248 /* Setting the variable to "" resets it to the compile time defaults. */
249 if (auto_load_safe_path[0] == '\0')
250 {
251 xfree (auto_load_safe_path);
252 auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH);
253 }
254
255 auto_load_safe_path_vec_update ();
256 }
257
258 /* "show" command for the auto_load_safe_path configuration variable. */
259
260 static void
261 show_auto_load_safe_path (struct ui_file *file, int from_tty,
262 struct cmd_list_element *c, const char *value)
263 {
264 const char *cs;
265
266 /* Check if user has entered either "/" or for example ":".
267 But while more complicate content like ":/foo" would still also
268 permit any location do not hide those. */
269
270 for (cs = value; *cs && (*cs == DIRNAME_SEPARATOR || IS_DIR_SEPARATOR (*cs));
271 cs++);
272 if (*cs == 0)
273 fprintf_filtered (file, _("Auto-load files are safe to load from any "
274 "directory.\n"));
275 else
276 fprintf_filtered (file, _("List of directories from which it is safe to "
277 "auto-load files is %s.\n"),
278 value);
279 }
280
281 /* "add-auto-load-safe-path" command for the auto_load_safe_path configuration
282 variable. */
283
284 static void
285 add_auto_load_safe_path (char *args, int from_tty)
286 {
287 char *s;
288
289 if (args == NULL || *args == 0)
290 error (_("\
291 Directory argument required.\n\
292 Use 'set auto-load safe-path /' for disabling the auto-load safe-path security.\
293 "));
294
295 s = xstrprintf ("%s%c%s", auto_load_safe_path, DIRNAME_SEPARATOR, args);
296 xfree (auto_load_safe_path);
297 auto_load_safe_path = s;
298
299 auto_load_safe_path_vec_update ();
300 }
301
302 /* Implementation for filename_is_in_pattern overwriting the caller's FILENAME
303 and PATTERN. */
304
305 static int
306 filename_is_in_pattern_1 (char *filename, char *pattern)
307 {
308 size_t pattern_len = strlen (pattern);
309 size_t filename_len = strlen (filename);
310
311 if (debug_auto_load)
312 fprintf_unfiltered (gdb_stdlog, _("auto-load: Matching file \"%s\" "
313 "to pattern \"%s\"\n"),
314 filename, pattern);
315
316 /* Trim trailing slashes ("/") from PATTERN. Even for "d:\" paths as
317 trailing slashes are trimmed also from FILENAME it still matches
318 correctly. */
319 while (pattern_len && IS_DIR_SEPARATOR (pattern[pattern_len - 1]))
320 pattern_len--;
321 pattern[pattern_len] = '\0';
322
323 /* Ensure auto_load_safe_path "/" matches any FILENAME. On MS-Windows
324 platform FILENAME even after gdb_realpath does not have to start with
325 IS_DIR_SEPARATOR character, such as the 'C:\x.exe' filename. */
326 if (pattern_len == 0)
327 {
328 if (debug_auto_load)
329 fprintf_unfiltered (gdb_stdlog,
330 _("auto-load: Matched - empty pattern\n"));
331 return 1;
332 }
333
334 for (;;)
335 {
336 /* Trim trailing slashes ("/"). PATTERN also has slashes trimmed the
337 same way so they will match. */
338 while (filename_len && IS_DIR_SEPARATOR (filename[filename_len - 1]))
339 filename_len--;
340 filename[filename_len] = '\0';
341 if (filename_len == 0)
342 {
343 if (debug_auto_load)
344 fprintf_unfiltered (gdb_stdlog,
345 _("auto-load: Not matched - pattern \"%s\".\n"),
346 pattern);
347 return 0;
348 }
349
350 if (gdb_filename_fnmatch (pattern, filename, FNM_FILE_NAME | FNM_NOESCAPE)
351 == 0)
352 {
353 if (debug_auto_load)
354 fprintf_unfiltered (gdb_stdlog, _("auto-load: Matched - file "
355 "\"%s\" to pattern \"%s\".\n"),
356 filename, pattern);
357 return 1;
358 }
359
360 /* Trim trailing FILENAME component. */
361 while (filename_len > 0 && !IS_DIR_SEPARATOR (filename[filename_len - 1]))
362 filename_len--;
363 }
364 }
365
366 /* Return 1 if FILENAME matches PATTERN or if FILENAME resides in
367 a subdirectory of a directory that matches PATTERN. Return 0 otherwise.
368 gdb_realpath normalization is never done here. */
369
370 static ATTRIBUTE_PURE int
371 filename_is_in_pattern (const char *filename, const char *pattern)
372 {
373 char *filename_copy, *pattern_copy;
374
375 filename_copy = alloca (strlen (filename) + 1);
376 strcpy (filename_copy, filename);
377 pattern_copy = alloca (strlen (pattern) + 1);
378 strcpy (pattern_copy, pattern);
379
380 return filename_is_in_pattern_1 (filename_copy, pattern_copy);
381 }
382
383 /* Return 1 if FILENAME belongs to one of directory components of
384 AUTO_LOAD_SAFE_PATH_VEC. Return 0 otherwise.
385 auto_load_safe_path_vec_update is never called.
386 *FILENAME_REALP may be updated by gdb_realpath of FILENAME - it has to be
387 freed by the caller. */
388
389 static int
390 filename_is_in_auto_load_safe_path_vec (const char *filename,
391 char **filename_realp)
392 {
393 char *pattern;
394 int ix;
395
396 for (ix = 0; VEC_iterate (char_ptr, auto_load_safe_path_vec, ix, pattern);
397 ++ix)
398 if (*filename_realp == NULL && filename_is_in_pattern (filename, pattern))
399 break;
400
401 if (pattern == NULL)
402 {
403 if (*filename_realp == NULL)
404 {
405 *filename_realp = gdb_realpath (filename);
406 if (debug_auto_load && strcmp (*filename_realp, filename) != 0)
407 fprintf_unfiltered (gdb_stdlog,
408 _("auto-load: Resolved "
409 "file \"%s\" as \"%s\".\n"),
410 filename, *filename_realp);
411 }
412
413 if (strcmp (*filename_realp, filename) != 0)
414 for (ix = 0;
415 VEC_iterate (char_ptr, auto_load_safe_path_vec, ix, pattern); ++ix)
416 if (filename_is_in_pattern (*filename_realp, pattern))
417 break;
418 }
419
420 if (pattern != NULL)
421 {
422 if (debug_auto_load)
423 fprintf_unfiltered (gdb_stdlog, _("auto-load: File \"%s\" matches "
424 "directory \"%s\".\n"),
425 filename, pattern);
426 return 1;
427 }
428
429 return 0;
430 }
431
432 /* Return 1 if FILENAME is located in one of the directories of
433 AUTO_LOAD_SAFE_PATH. Otherwise call warning and return 0. FILENAME does
434 not have to be an absolute path.
435
436 Existence of FILENAME is not checked. Function will still give a warning
437 even if the caller would quietly skip non-existing file in unsafe
438 directory. */
439
440 int
441 file_is_auto_load_safe (const char *filename, const char *debug_fmt, ...)
442 {
443 char *filename_real = NULL;
444 struct cleanup *back_to;
445 static int advice_printed = 0;
446
447 if (debug_auto_load)
448 {
449 va_list debug_args;
450
451 va_start (debug_args, debug_fmt);
452 vfprintf_unfiltered (gdb_stdlog, debug_fmt, debug_args);
453 va_end (debug_args);
454 }
455
456 back_to = make_cleanup (free_current_contents, &filename_real);
457
458 if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
459 {
460 do_cleanups (back_to);
461 return 1;
462 }
463
464 auto_load_safe_path_vec_update ();
465 if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
466 {
467 do_cleanups (back_to);
468 return 1;
469 }
470
471 warning (_("File \"%s\" auto-loading has been declined by your "
472 "`auto-load safe-path' set to \"%s\"."),
473 filename_real, auto_load_safe_path);
474
475 if (!advice_printed)
476 {
477 const char *homedir = getenv ("HOME");
478 char *homeinit;
479
480 if (homedir == NULL)
481 homedir = "$HOME";
482 homeinit = xstrprintf ("%s/%s", homedir, gdbinit);
483 make_cleanup (xfree, homeinit);
484
485 printf_filtered (_("\
486 To enable execution of this file add\n\
487 \tadd-auto-load-safe-path %s\n\
488 line to your configuration file \"%s\".\n\
489 To completely disable this security protection add\n\
490 \tset auto-load safe-path /\n\
491 line to your configuration file \"%s\".\n\
492 For more information about this security protection see the\n\
493 \"Auto-loading safe path\" section in the GDB manual. E.g., run from the shell: \n\
494 \tinfo \"(gdb)Auto-loading safe path\"\n"),
495 filename_real, homeinit, homeinit);
496 advice_printed = 1;
497 }
498
499 do_cleanups (back_to);
500 return 0;
501 }
502
503 /* Definition of script language for GDB canned sequences of commands. */
504
505 static const struct script_language script_language_gdb
506 = { GDB_AUTO_FILE_NAME, source_gdb_script_for_objfile };
507
508 static void
509 source_gdb_script_for_objfile (struct objfile *objfile, FILE *file,
510 const char *filename)
511 {
512 int is_safe;
513 struct auto_load_pspace_info *pspace_info;
514 volatile struct gdb_exception e;
515
516 is_safe = file_is_auto_load_safe (filename, _("auto-load: Loading canned "
517 "sequences of commands script "
518 "\"%s\" for objfile \"%s\".\n"),
519 filename, objfile->name);
520
521 /* Add this script to the hash table too so "info auto-load gdb-scripts"
522 can print it. */
523 pspace_info = get_auto_load_pspace_data_for_loading (current_program_space);
524 maybe_add_script (pspace_info, is_safe, filename, filename,
525 &script_language_gdb);
526
527 if (!is_safe)
528 return;
529
530 TRY_CATCH (e, RETURN_MASK_ALL)
531 {
532 script_from_file (file, filename);
533 }
534 exception_print (gdb_stderr, e);
535 }
536
537 /* For scripts specified in .debug_gdb_scripts, multiple objfiles may load
538 the same script. There's no point in loading the script multiple times,
539 and there can be a lot of objfiles and scripts, so we keep track of scripts
540 loaded this way. */
541
542 struct auto_load_pspace_info
543 {
544 /* For each program space we keep track of loaded scripts. */
545 struct htab *loaded_scripts;
546
547 /* Non-zero if we've issued the warning about an auto-load script not being
548 found. We only want to issue this warning once. */
549 int script_not_found_warning_printed;
550 };
551
552 /* Objects of this type are stored in the loaded script hash table. */
553
554 struct loaded_script
555 {
556 /* Name as provided by the objfile. */
557 const char *name;
558
559 /* Full path name or NULL if script wasn't found (or was otherwise
560 inaccessible). */
561 const char *full_path;
562
563 /* Non-zero if this script has been loaded. */
564 int loaded;
565
566 const struct script_language *language;
567 };
568
569 /* Per-program-space data key. */
570 static const struct program_space_data *auto_load_pspace_data;
571
572 static void
573 auto_load_pspace_data_cleanup (struct program_space *pspace, void *arg)
574 {
575 struct auto_load_pspace_info *info;
576
577 info = program_space_data (pspace, auto_load_pspace_data);
578 if (info != NULL)
579 {
580 if (info->loaded_scripts)
581 htab_delete (info->loaded_scripts);
582 xfree (info);
583 }
584 }
585
586 /* Get the current autoload data. If none is found yet, add it now. This
587 function always returns a valid object. */
588
589 static struct auto_load_pspace_info *
590 get_auto_load_pspace_data (struct program_space *pspace)
591 {
592 struct auto_load_pspace_info *info;
593
594 info = program_space_data (pspace, auto_load_pspace_data);
595 if (info == NULL)
596 {
597 info = XZALLOC (struct auto_load_pspace_info);
598 set_program_space_data (pspace, auto_load_pspace_data, info);
599 }
600
601 return info;
602 }
603
604 /* Hash function for the loaded script hash. */
605
606 static hashval_t
607 hash_loaded_script_entry (const void *data)
608 {
609 const struct loaded_script *e = data;
610
611 return htab_hash_string (e->name) ^ htab_hash_pointer (e->language);
612 }
613
614 /* Equality function for the loaded script hash. */
615
616 static int
617 eq_loaded_script_entry (const void *a, const void *b)
618 {
619 const struct loaded_script *ea = a;
620 const struct loaded_script *eb = b;
621
622 return strcmp (ea->name, eb->name) == 0 && ea->language == eb->language;
623 }
624
625 /* Initialize the table to track loaded scripts.
626 Each entry is hashed by the full path name. */
627
628 static void
629 init_loaded_scripts_info (struct auto_load_pspace_info *pspace_info)
630 {
631 /* Choose 31 as the starting size of the hash table, somewhat arbitrarily.
632 Space for each entry is obtained with one malloc so we can free them
633 easily. */
634
635 pspace_info->loaded_scripts = htab_create (31,
636 hash_loaded_script_entry,
637 eq_loaded_script_entry,
638 xfree);
639
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
646 struct auto_load_pspace_info *
647 get_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);
652 if (info->loaded_scripts == NULL)
653 init_loaded_scripts_info (info);
654
655 return info;
656 }
657
658 /* Add script NAME in LANGUAGE to hash table of PSPACE_INFO. LOADED 1 if the
659 script has been (is going to) be loaded, 0 otherwise (such as if it has not
660 been found). FULL_PATH is NULL if the script wasn't found. The result is
661 true if the script was already in the hash table. */
662
663 int
664 maybe_add_script (struct auto_load_pspace_info *pspace_info, int loaded,
665 const char *name, const char *full_path,
666 const struct script_language *language)
667 {
668 struct htab *htab = pspace_info->loaded_scripts;
669 struct loaded_script **slot, entry;
670 int in_hash_table;
671
672 entry.name = name;
673 entry.language = language;
674 slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT);
675 in_hash_table = *slot != NULL;
676
677 /* If this script is not in the hash table, add it. */
678
679 if (! in_hash_table)
680 {
681 char *p;
682
683 /* Allocate all space in one chunk so it's easier to free. */
684 *slot = xmalloc (sizeof (**slot)
685 + strlen (name) + 1
686 + (full_path != NULL ? (strlen (full_path) + 1) : 0));
687 p = ((char*) *slot) + sizeof (**slot);
688 strcpy (p, name);
689 (*slot)->name = p;
690 if (full_path != NULL)
691 {
692 p += strlen (p) + 1;
693 strcpy (p, full_path);
694 (*slot)->full_path = p;
695 }
696 else
697 (*slot)->full_path = NULL;
698 (*slot)->loaded = loaded;
699 (*slot)->language = language;
700 }
701
702 return in_hash_table;
703 }
704
705 /* Clear the table of loaded section scripts. */
706
707 static void
708 clear_section_scripts (void)
709 {
710 struct program_space *pspace = current_program_space;
711 struct auto_load_pspace_info *info;
712
713 info = program_space_data (pspace, auto_load_pspace_data);
714 if (info != NULL && info->loaded_scripts != NULL)
715 {
716 htab_delete (info->loaded_scripts);
717 info->loaded_scripts = NULL;
718 info->script_not_found_warning_printed = FALSE;
719 }
720 }
721
722 /* Look for the auto-load script in LANGUAGE associated with OBJFILE where
723 OBJFILE's gdb_realpath is REALNAME and load it. Return 1 if we found any
724 matching script, return 0 otherwise. */
725
726 static int
727 auto_load_objfile_script_1 (struct objfile *objfile, const char *realname,
728 const struct script_language *language)
729 {
730 char *filename, *debugfile;
731 int len, retval;
732 FILE *input;
733 struct cleanup *cleanups;
734
735 len = strlen (realname);
736 filename = xmalloc (len + strlen (language->suffix) + 1);
737 memcpy (filename, realname, len);
738 strcpy (filename + len, language->suffix);
739
740 cleanups = make_cleanup (xfree, filename);
741
742 input = fopen (filename, "r");
743 debugfile = filename;
744 if (debug_auto_load)
745 fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file \"%s\" %s.\n"),
746 debugfile, input ? _("exists") : _("does not exist"));
747
748 if (!input)
749 {
750 VEC (char_ptr) *vec;
751 int ix;
752 char *dir;
753
754 /* Also try the same file in a subdirectory of gdb's data
755 directory. */
756
757 vec = auto_load_expand_dir_vars (auto_load_dir);
758 make_cleanup_free_char_ptr_vec (vec);
759
760 if (debug_auto_load)
761 fprintf_unfiltered (gdb_stdlog, _("auto-load: Searching 'set auto-load "
762 "scripts-directory' path \"%s\".\n"),
763 auto_load_dir);
764
765 for (ix = 0; VEC_iterate (char_ptr, vec, ix, dir); ++ix)
766 {
767 debugfile = xmalloc (strlen (dir) + strlen (filename) + 1);
768 strcpy (debugfile, dir);
769
770 /* FILENAME is absolute, so we don't need a "/" here. */
771 strcat (debugfile, filename);
772
773 make_cleanup (xfree, debugfile);
774 input = fopen (debugfile, "r");
775 if (debug_auto_load)
776 fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file "
777 "\"%s\" %s.\n"),
778 debugfile,
779 input ? _("exists") : _("does not exist"));
780 if (input != NULL)
781 break;
782 }
783 }
784
785 if (input)
786 {
787 make_cleanup_fclose (input);
788
789 /* To preserve existing behaviour we don't check for whether the
790 script was already in the table, and always load it.
791 It's highly unlikely that we'd ever load it twice,
792 and these scripts are required to be idempotent under multiple
793 loads anyway. */
794 language->source_script_for_objfile (objfile, input, debugfile);
795
796 retval = 1;
797 }
798 else
799 retval = 0;
800
801 do_cleanups (cleanups);
802 return retval;
803 }
804
805 /* Look for the auto-load script in LANGUAGE associated with OBJFILE and load
806 it. */
807
808 void
809 auto_load_objfile_script (struct objfile *objfile,
810 const struct script_language *language)
811 {
812 char *realname = gdb_realpath (objfile->name);
813 struct cleanup *cleanups = make_cleanup (xfree, realname);
814
815 if (!auto_load_objfile_script_1 (objfile, realname, language))
816 {
817 /* For Windows/DOS .exe executables, strip the .exe suffix, so that
818 FOO-gdb.gdb could be used for FOO.exe, and try again. */
819
820 size_t len = strlen (realname);
821 const size_t lexe = sizeof (".exe") - 1;
822
823 if (len > lexe && strcasecmp (realname + len - lexe, ".exe") == 0)
824 {
825 len -= lexe;
826 realname[len] = '\0';
827 if (debug_auto_load)
828 fprintf_unfiltered (gdb_stdlog, _("auto-load: Stripped .exe suffix, "
829 "retrying with \"%s\".\n"),
830 realname);
831 auto_load_objfile_script_1 (objfile, realname, language);
832 }
833 }
834
835 do_cleanups (cleanups);
836 }
837
838 /* Load any auto-loaded scripts for OBJFILE. */
839
840 void
841 load_auto_scripts_for_objfile (struct objfile *objfile)
842 {
843 if (!global_auto_load)
844 return;
845
846 if (auto_load_gdb_scripts)
847 auto_load_objfile_script (objfile, &script_language_gdb);
848
849 gdbpy_load_auto_scripts_for_objfile (objfile);
850 }
851
852 /* This is a new_objfile observer callback to auto-load scripts.
853
854 Two flavors of auto-loaded scripts are supported.
855 1) based on the path to the objfile
856 2) from .debug_gdb_scripts section */
857
858 static void
859 auto_load_new_objfile (struct objfile *objfile)
860 {
861 if (!objfile)
862 {
863 /* OBJFILE is NULL when loading a new "main" symbol-file. */
864 clear_section_scripts ();
865 return;
866 }
867
868 load_auto_scripts_for_objfile (objfile);
869 }
870
871 /* Collect scripts to be printed in a vec. */
872
873 typedef struct loaded_script *loaded_script_ptr;
874 DEF_VEC_P (loaded_script_ptr);
875
876 struct collect_matching_scripts_data
877 {
878 VEC (loaded_script_ptr) **scripts_p;
879
880 const struct script_language *language;
881 };
882
883 /* Traversal function for htab_traverse.
884 Collect the entry if it matches the regexp. */
885
886 static int
887 collect_matching_scripts (void **slot, void *info)
888 {
889 struct loaded_script *script = *slot;
890 struct collect_matching_scripts_data *data = info;
891
892 if (script->language == data->language && re_exec (script->name))
893 VEC_safe_push (loaded_script_ptr, *data->scripts_p, script);
894
895 return 1;
896 }
897
898 /* Print SCRIPT. */
899
900 static void
901 print_script (struct loaded_script *script)
902 {
903 struct ui_out *uiout = current_uiout;
904 struct cleanup *chain;
905
906 chain = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
907
908 ui_out_field_string (uiout, "loaded", script->loaded ? "Yes" : "No");
909 ui_out_field_string (uiout, "script", script->name);
910 ui_out_text (uiout, "\n");
911
912 /* If the name isn't the full path, print it too. */
913 if (script->full_path != NULL
914 && strcmp (script->name, script->full_path) != 0)
915 {
916 ui_out_text (uiout, "\tfull name: ");
917 ui_out_field_string (uiout, "full_path", script->full_path);
918 ui_out_text (uiout, "\n");
919 }
920
921 do_cleanups (chain);
922 }
923
924 /* Helper for info_auto_load_scripts to sort the scripts by name. */
925
926 static int
927 sort_scripts_by_name (const void *ap, const void *bp)
928 {
929 const struct loaded_script *a = *(const struct loaded_script **) ap;
930 const struct loaded_script *b = *(const struct loaded_script **) bp;
931
932 return FILENAME_CMP (a->name, b->name);
933 }
934
935 /* Special internal GDB value of auto_load_info_scripts's PATTERN identify
936 the "info auto-load XXX" command has been executed through the general
937 "info auto-load" invocation. Extra newline will be printed if needed. */
938 char auto_load_info_scripts_pattern_nl[] = "";
939
940 /* Implementation for "info auto-load gdb-scripts"
941 (and "info auto-load python-scripts"). List scripts in LANGUAGE matching
942 PATTERN. FROM_TTY is the usual GDB boolean for user interactivity. */
943
944 void
945 auto_load_info_scripts (char *pattern, int from_tty,
946 const struct script_language *language)
947 {
948 struct ui_out *uiout = current_uiout;
949 struct auto_load_pspace_info *pspace_info;
950 struct cleanup *script_chain;
951 VEC (loaded_script_ptr) *scripts;
952 int nr_scripts;
953
954 dont_repeat ();
955
956 pspace_info = get_auto_load_pspace_data (current_program_space);
957
958 if (pattern && *pattern)
959 {
960 char *re_err = re_comp (pattern);
961
962 if (re_err)
963 error (_("Invalid regexp: %s"), re_err);
964 }
965 else
966 {
967 re_comp ("");
968 }
969
970 /* We need to know the number of rows before we build the table.
971 Plus we want to sort the scripts by name.
972 So first traverse the hash table collecting the matching scripts. */
973
974 scripts = VEC_alloc (loaded_script_ptr, 10);
975 script_chain = make_cleanup (VEC_cleanup (loaded_script_ptr), &scripts);
976
977 if (pspace_info != NULL && pspace_info->loaded_scripts != NULL)
978 {
979 struct collect_matching_scripts_data data = { &scripts, language };
980
981 immediate_quit++;
982 /* Pass a pointer to scripts as VEC_safe_push can realloc space. */
983 htab_traverse_noresize (pspace_info->loaded_scripts,
984 collect_matching_scripts, &data);
985 immediate_quit--;
986 }
987
988 nr_scripts = VEC_length (loaded_script_ptr, scripts);
989
990 /* Table header shifted right by preceding "gdb-scripts: " would not match
991 its columns. */
992 if (nr_scripts > 0 && pattern == auto_load_info_scripts_pattern_nl)
993 ui_out_text (uiout, "\n");
994
995 make_cleanup_ui_out_table_begin_end (uiout, 2, nr_scripts,
996 "AutoLoadedScriptsTable");
997
998 ui_out_table_header (uiout, 7, ui_left, "loaded", "Loaded");
999 ui_out_table_header (uiout, 70, ui_left, "script", "Script");
1000 ui_out_table_body (uiout);
1001
1002 if (nr_scripts > 0)
1003 {
1004 int i;
1005 loaded_script_ptr script;
1006
1007 qsort (VEC_address (loaded_script_ptr, scripts),
1008 VEC_length (loaded_script_ptr, scripts),
1009 sizeof (loaded_script_ptr), sort_scripts_by_name);
1010 for (i = 0; VEC_iterate (loaded_script_ptr, scripts, i, script); ++i)
1011 print_script (script);
1012 }
1013
1014 do_cleanups (script_chain);
1015
1016 if (nr_scripts == 0)
1017 {
1018 if (pattern && *pattern)
1019 ui_out_message (uiout, 0, "No auto-load scripts matching %s.\n",
1020 pattern);
1021 else
1022 ui_out_message (uiout, 0, "No auto-load scripts.\n");
1023 }
1024 }
1025
1026 /* Wrapper for "info auto-load gdb-scripts". */
1027
1028 static void
1029 info_auto_load_gdb_scripts (char *pattern, int from_tty)
1030 {
1031 auto_load_info_scripts (pattern, from_tty, &script_language_gdb);
1032 }
1033
1034 /* Implement 'info auto-load local-gdbinit'. */
1035
1036 static void
1037 info_auto_load_local_gdbinit (char *args, int from_tty)
1038 {
1039 if (auto_load_local_gdbinit_pathname == NULL)
1040 printf_filtered (_("Local .gdbinit file was not found.\n"));
1041 else if (auto_load_local_gdbinit_loaded)
1042 printf_filtered (_("Local .gdbinit file \"%s\" has been loaded.\n"),
1043 auto_load_local_gdbinit_pathname);
1044 else
1045 printf_filtered (_("Local .gdbinit file \"%s\" has not been loaded.\n"),
1046 auto_load_local_gdbinit_pathname);
1047 }
1048
1049 /* Return non-zero if SCRIPT_NOT_FOUND_WARNING_PRINTED of PSPACE_INFO was unset
1050 before calling this function. Always set SCRIPT_NOT_FOUND_WARNING_PRINTED
1051 of PSPACE_INFO. */
1052
1053 int
1054 script_not_found_warning_print (struct auto_load_pspace_info *pspace_info)
1055 {
1056 int retval = !pspace_info->script_not_found_warning_printed;
1057
1058 pspace_info->script_not_found_warning_printed = 1;
1059
1060 return retval;
1061 }
1062
1063 /* The only valid "set auto-load" argument is off|0|no|disable. */
1064
1065 static void
1066 set_auto_load_cmd (char *args, int from_tty)
1067 {
1068 struct cmd_list_element *list;
1069 size_t length;
1070
1071 /* See parse_binary_operation in use by the sub-commands. */
1072
1073 length = args ? strlen (args) : 0;
1074
1075 while (length > 0 && (args[length - 1] == ' ' || args[length - 1] == '\t'))
1076 length--;
1077
1078 if (length == 0 || (strncmp (args, "off", length) != 0
1079 && strncmp (args, "0", length) != 0
1080 && strncmp (args, "no", length) != 0
1081 && strncmp (args, "disable", length) != 0))
1082 error (_("Valid is only global 'set auto-load no'; "
1083 "otherwise check the auto-load sub-commands."));
1084
1085 for (list = *auto_load_set_cmdlist_get (); list != NULL; list = list->next)
1086 if (list->var_type == var_boolean)
1087 {
1088 gdb_assert (list->type == set_cmd);
1089 do_setshow_command (args, from_tty, list);
1090 }
1091 }
1092
1093 /* Initialize "set auto-load " commands prefix and return it. */
1094
1095 struct cmd_list_element **
1096 auto_load_set_cmdlist_get (void)
1097 {
1098 static struct cmd_list_element *retval;
1099
1100 if (retval == NULL)
1101 add_prefix_cmd ("auto-load", class_maintenance, set_auto_load_cmd, _("\
1102 Auto-loading specific settings.\n\
1103 Configure various auto-load-specific variables such as\n\
1104 automatic loading of Python scripts."),
1105 &retval, "set auto-load ",
1106 1/*allow-unknown*/, &setlist);
1107
1108 return &retval;
1109 }
1110
1111 /* Command "show auto-load" displays summary of all the current
1112 "show auto-load " settings. */
1113
1114 static void
1115 show_auto_load_cmd (char *args, int from_tty)
1116 {
1117 cmd_show_list (*auto_load_show_cmdlist_get (), from_tty, "");
1118 }
1119
1120 /* Initialize "show auto-load " commands prefix and return it. */
1121
1122 struct cmd_list_element **
1123 auto_load_show_cmdlist_get (void)
1124 {
1125 static struct cmd_list_element *retval;
1126
1127 if (retval == NULL)
1128 add_prefix_cmd ("auto-load", class_maintenance, show_auto_load_cmd, _("\
1129 Show auto-loading specific settings.\n\
1130 Show configuration of various auto-load-specific variables such as\n\
1131 automatic loading of Python scripts."),
1132 &retval, "show auto-load ",
1133 0/*allow-unknown*/, &showlist);
1134
1135 return &retval;
1136 }
1137
1138 /* Command "info auto-load" displays whether the various auto-load files have
1139 been loaded. This is reimplementation of cmd_show_list which inserts
1140 newlines at proper places. */
1141
1142 static void
1143 info_auto_load_cmd (char *args, int from_tty)
1144 {
1145 struct cmd_list_element *list;
1146 struct cleanup *infolist_chain;
1147 struct ui_out *uiout = current_uiout;
1148
1149 infolist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "infolist");
1150
1151 for (list = *auto_load_info_cmdlist_get (); list != NULL; list = list->next)
1152 {
1153 struct cleanup *option_chain
1154 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
1155
1156 gdb_assert (!list->prefixlist);
1157 gdb_assert (list->type == not_set_cmd);
1158
1159 ui_out_field_string (uiout, "name", list->name);
1160 ui_out_text (uiout, ": ");
1161 cmd_func (list, auto_load_info_scripts_pattern_nl, from_tty);
1162
1163 /* Close the tuple. */
1164 do_cleanups (option_chain);
1165 }
1166
1167 /* Close the tuple. */
1168 do_cleanups (infolist_chain);
1169 }
1170
1171 /* Initialize "info auto-load " commands prefix and return it. */
1172
1173 struct cmd_list_element **
1174 auto_load_info_cmdlist_get (void)
1175 {
1176 static struct cmd_list_element *retval;
1177
1178 if (retval == NULL)
1179 add_prefix_cmd ("auto-load", class_info, info_auto_load_cmd, _("\
1180 Print current status of auto-loaded files.\n\
1181 Print whether various files like Python scripts or .gdbinit files have been\n\
1182 found and/or loaded."),
1183 &retval, "info auto-load ",
1184 0/*allow-unknown*/, &infolist);
1185
1186 return &retval;
1187 }
1188
1189 void _initialize_auto_load (void);
1190
1191 void
1192 _initialize_auto_load (void)
1193 {
1194 struct cmd_list_element *cmd;
1195 char *scripts_directory_help;
1196
1197 auto_load_pspace_data
1198 = register_program_space_data_with_cleanup (auto_load_pspace_data_cleanup);
1199
1200 observer_attach_new_objfile (auto_load_new_objfile);
1201
1202 add_setshow_boolean_cmd ("gdb-scripts", class_support,
1203 &auto_load_gdb_scripts, _("\
1204 Enable or disable auto-loading of canned sequences of commands scripts."), _("\
1205 Show whether auto-loading of canned sequences of commands scripts is enabled."),
1206 _("\
1207 If enabled, canned sequences of commands are loaded when the debugger reads\n\
1208 an executable or shared library.\n\
1209 This options has security implications for untrusted inferiors."),
1210 NULL, show_auto_load_gdb_scripts,
1211 auto_load_set_cmdlist_get (),
1212 auto_load_show_cmdlist_get ());
1213
1214 add_cmd ("gdb-scripts", class_info, info_auto_load_gdb_scripts,
1215 _("Print the list of automatically loaded sequences of commands.\n\
1216 Usage: info auto-load gdb-scripts [REGEXP]"),
1217 auto_load_info_cmdlist_get ());
1218
1219 add_setshow_boolean_cmd ("local-gdbinit", class_support,
1220 &auto_load_local_gdbinit, _("\
1221 Enable or disable auto-loading of .gdbinit script in current directory."), _("\
1222 Show whether auto-loading .gdbinit script in current directory is enabled."),
1223 _("\
1224 If enabled, canned sequences of commands are loaded when debugger starts\n\
1225 from .gdbinit file in current directory. Such files are deprecated,\n\
1226 use a script associated with inferior executable file instead.\n\
1227 This options has security implications for untrusted inferiors."),
1228 NULL, show_auto_load_local_gdbinit,
1229 auto_load_set_cmdlist_get (),
1230 auto_load_show_cmdlist_get ());
1231
1232 add_cmd ("local-gdbinit", class_info, info_auto_load_local_gdbinit,
1233 _("Print whether current directory .gdbinit file has been loaded.\n\
1234 Usage: info auto-load local-gdbinit"),
1235 auto_load_info_cmdlist_get ());
1236
1237 auto_load_dir = xstrdup (AUTO_LOAD_DIR);
1238 scripts_directory_help = xstrprintf (
1239 #ifdef HAVE_PYTHON
1240 _("\
1241 Automatically loaded Python scripts (named OBJFILE%s) and GDB scripts\n\
1242 (named OBJFILE%s) are located in one of the directories listed by this\n\
1243 option.\n\
1244 %s"),
1245 GDBPY_AUTO_FILE_NAME,
1246 #else
1247 _("\
1248 Automatically loaded GDB scripts (named OBJFILE%s) are located in one\n\
1249 of the directories listed by this option.\n\
1250 %s"),
1251 #endif
1252 GDB_AUTO_FILE_NAME,
1253 _("\
1254 This option is ignored for the kinds of scripts \
1255 having 'set auto-load ... off'.\n\
1256 Directories listed here need to be present also \
1257 in the 'set auto-load safe-path'\n\
1258 option."));
1259 add_setshow_optional_filename_cmd ("scripts-directory", class_support,
1260 &auto_load_dir, _("\
1261 Set the list of directories from which to load auto-loaded scripts."), _("\
1262 Show the list of directories from which to load auto-loaded scripts."),
1263 scripts_directory_help,
1264 set_auto_load_dir, show_auto_load_dir,
1265 auto_load_set_cmdlist_get (),
1266 auto_load_show_cmdlist_get ());
1267 xfree (scripts_directory_help);
1268
1269 auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH);
1270 auto_load_safe_path_vec_update ();
1271 add_setshow_optional_filename_cmd ("safe-path", class_support,
1272 &auto_load_safe_path, _("\
1273 Set the list of files and directories that are safe for auto-loading."), _("\
1274 Show the list of files and directories that are safe for auto-loading."), _("\
1275 Various files loaded automatically for the 'set auto-load ...' options must\n\
1276 be located in one of the directories listed by this option. Warning will be\n\
1277 printed and file will not be used otherwise.\n\
1278 You can mix both directory and filename entries.\n\
1279 Setting this parameter to an empty list resets it to its default value.\n\
1280 Setting this parameter to '/' (without the quotes) allows any file\n\
1281 for the 'set auto-load ...' options. Each path entry can be also shell\n\
1282 wildcard pattern; '*' does not match directory separator.\n\
1283 This option is ignored for the kinds of files having 'set auto-load ... off'.\n\
1284 This options has security implications for untrusted inferiors."),
1285 set_auto_load_safe_path,
1286 show_auto_load_safe_path,
1287 auto_load_set_cmdlist_get (),
1288 auto_load_show_cmdlist_get ());
1289 observer_attach_gdb_datadir_changed (auto_load_gdb_datadir_changed);
1290
1291 cmd = add_cmd ("add-auto-load-safe-path", class_support,
1292 add_auto_load_safe_path,
1293 _("Add entries to the list of directories from which it is safe "
1294 "to auto-load files.\n\
1295 See the commands 'set auto-load safe-path' and 'show auto-load safe-path' to\n\
1296 access the current full list setting."),
1297 &cmdlist);
1298 set_cmd_completer (cmd, filename_completer);
1299
1300 add_setshow_boolean_cmd ("auto-load", class_maintenance,
1301 &debug_auto_load, _("\
1302 Set auto-load verifications debugging."), _("\
1303 Show auto-load verifications debugging."), _("\
1304 When non-zero, debugging output for files of 'set auto-load ...'\n\
1305 is displayed."),
1306 NULL, show_debug_auto_load,
1307 &setdebuglist, &showdebuglist);
1308 }
OLDNEW
« no previous file with comments | « gdb/auto-load.h ('k') | gdb/auxv.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698