gdb/
[deliverable/binutils-gdb.git] / gdb / python / py-auto-load.c
1 /* GDB routines for supporting auto-loaded scripts.
2
3 Copyright (C) 2010-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 "gdb_string.h"
22 #include "top.h"
23 #include "exceptions.h"
24 #include "gdbcmd.h"
25 #include "objfiles.h"
26 #include "python.h"
27 #include "cli/cli-cmds.h"
28 #include "auto-load.h"
29
30 #ifdef HAVE_PYTHON
31
32 #include "python-internal.h"
33
34 /* NOTE: It's trivial to also support auto-loading normal gdb scripts.
35 There has yet to be a need so it's not implemented. */
36
37 /* The suffix of per-objfile scripts to auto-load.
38 E.g. When the program loads libfoo.so, look for libfoo-gdb.py. */
39 #define GDBPY_AUTO_FILE_NAME "-gdb.py"
40
41 /* The section to look for scripts (in file formats that support sections).
42 Each entry in this section is a byte of value 1, and then the nul-terminated
43 name of the script. The script name may include a directory.
44 The leading byte is to allow upward compatible extensions. */
45 #define GDBPY_AUTO_SECTION_NAME ".debug_gdb_scripts"
46
47 /* User-settable option to enable/disable auto-loading:
48 set auto-load-scripts on|off
49 This is true if we should auto-load associated scripts when an objfile
50 is opened, false otherwise.
51 At the moment, this only affects python scripts, but there's no reason
52 one couldn't also have other kinds of auto-loaded scripts, and there's
53 no reason to have them each controlled by a separate flag.
54 So we elide "python" from the name here and in the option.
55 The fact that it lives here is just an implementation detail. */
56 static int auto_load_scripts = 1;
57
58 /* Load scripts specified in OBJFILE.
59 START,END delimit a buffer containing a list of nul-terminated
60 file names.
61 SOURCE_NAME is used in error messages.
62
63 Scripts are found per normal "source -s" command processing.
64 First the script is looked for in $cwd. If not found there the
65 source search path is used.
66
67 The section contains a list of path names of files containing
68 python code to load. Each path is null-terminated. */
69
70 static void
71 source_section_scripts (struct objfile *objfile, const char *source_name,
72 const char *start, const char *end)
73 {
74 const char *p;
75 struct auto_load_pspace_info *pspace_info;
76
77 pspace_info = get_auto_load_pspace_data_for_loading (current_program_space);
78
79 for (p = start; p < end; ++p)
80 {
81 const char *file;
82 FILE *stream;
83 char *full_path;
84 int opened, in_hash_table;
85 struct cleanup *back_to;
86
87 if (*p != 1)
88 {
89 warning (_("Invalid entry in %s section"), GDBPY_AUTO_SECTION_NAME);
90 /* We could try various heuristics to find the next valid entry,
91 but it's safer to just punt. */
92 break;
93 }
94 file = ++p;
95
96 while (p < end && *p != '\0')
97 ++p;
98 if (p == end)
99 {
100 char *buf = alloca (p - file + 1);
101
102 memcpy (buf, file, p - file);
103 buf[p - file] = '\0';
104 warning (_("Non-null-terminated path in %s: %s"),
105 source_name, buf);
106 /* Don't load it. */
107 break;
108 }
109 if (p == file)
110 {
111 warning (_("Empty path in %s"), source_name);
112 continue;
113 }
114
115 opened = find_and_open_script (file, 1 /*search_path*/,
116 &stream, &full_path);
117
118 back_to = make_cleanup (null_cleanup, NULL);
119 if (opened)
120 {
121 make_cleanup_fclose (stream);
122 make_cleanup (xfree, full_path);
123 }
124
125 /* If one script isn't found it's not uncommon for more to not be
126 found either. We don't want to print an error message for each
127 script, too much noise. Instead, we print the warning once and tell
128 the user how to find the list of scripts that weren't loaded.
129
130 IWBN if complaints.c were more general-purpose. */
131
132 in_hash_table = maybe_add_script (pspace_info, file,
133 opened ? full_path : NULL);
134
135 if (! opened)
136 {
137 /* We don't throw an error, the program is still debuggable. */
138 if (script_not_found_warning_print (pspace_info))
139 warning (_("Missing auto-load scripts referenced in section %s\n\
140 of file %s\n\
141 Use `info auto-load-scripts [REGEXP]' to list them."),
142 GDBPY_AUTO_SECTION_NAME, objfile->name);
143 }
144 else
145 {
146 /* If this file is not currently loaded, load it. */
147 if (! in_hash_table)
148 source_python_script_for_objfile (objfile, stream, full_path);
149 }
150
151 do_cleanups (back_to);
152 }
153 }
154
155 /* Load scripts specified in section SECTION_NAME of OBJFILE. */
156
157 static void
158 auto_load_section_scripts (struct objfile *objfile, const char *section_name)
159 {
160 bfd *abfd = objfile->obfd;
161 asection *scripts_sect;
162 bfd_size_type size;
163 char *p;
164 struct cleanup *cleanups;
165
166 scripts_sect = bfd_get_section_by_name (abfd, section_name);
167 if (scripts_sect == NULL)
168 return;
169
170 size = bfd_get_section_size (scripts_sect);
171 p = xmalloc (size);
172
173 cleanups = make_cleanup (xfree, p);
174
175 if (bfd_get_section_contents (abfd, scripts_sect, p, (file_ptr) 0, size))
176 source_section_scripts (objfile, section_name, p, p + size);
177 else
178 warning (_("Couldn't read %s section of %s"),
179 section_name, bfd_get_filename (abfd));
180
181 do_cleanups (cleanups);
182 }
183
184 /* Load any auto-loaded scripts for OBJFILE. */
185
186 void
187 load_auto_scripts_for_objfile (struct objfile *objfile)
188 {
189 if (auto_load_scripts && gdbpy_global_auto_load)
190 {
191 auto_load_objfile_script (objfile, GDBPY_AUTO_FILE_NAME);
192 auto_load_section_scripts (objfile, GDBPY_AUTO_SECTION_NAME);
193 }
194 }
195 \f
196 void
197 gdbpy_initialize_auto_load (void)
198 {
199 add_setshow_boolean_cmd ("auto-load-scripts", class_support,
200 &auto_load_scripts, _("\
201 Set the debugger's behaviour regarding auto-loaded scripts."), _("\
202 Show the debugger's behaviour regarding auto-loaded scripts."), _("\
203 If enabled, auto-loaded scripts are loaded when the debugger reads\n\
204 an executable or shared library."),
205 NULL, NULL,
206 &setlist,
207 &showlist);
208 }
209
210 #else /* ! HAVE_PYTHON */
211
212 void
213 load_auto_scripts_for_objfile (struct objfile *objfile)
214 {
215 }
216
217 #endif /* ! HAVE_PYTHON */
This page took 0.050774 seconds and 4 git commands to generate.