scm-objfile.c revision 1.1.1.1 1 1.1 christos /* Scheme interface to objfiles.
2 1.1 christos
3 1.1 christos Copyright (C) 2008-2015 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos This file is part of GDB.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 1.1 christos
20 1.1 christos /* See README file in this directory for implementation notes, coding
21 1.1 christos conventions, et.al. */
22 1.1 christos
23 1.1 christos #include "defs.h"
24 1.1 christos #include "objfiles.h"
25 1.1 christos #include "language.h"
26 1.1 christos #include "guile-internal.h"
27 1.1 christos
28 1.1 christos /* The <gdb:objfile> smob.
29 1.1 christos The typedef for this struct is in guile-internal.h. */
30 1.1 christos
31 1.1 christos struct _objfile_smob
32 1.1 christos {
33 1.1 christos /* This always appears first. */
34 1.1 christos gdb_smob base;
35 1.1 christos
36 1.1 christos /* The corresponding objfile. */
37 1.1 christos struct objfile *objfile;
38 1.1 christos
39 1.1 christos /* The pretty-printer list of functions. */
40 1.1 christos SCM pretty_printers;
41 1.1 christos
42 1.1 christos /* The <gdb:objfile> object we are contained in, needed to protect/unprotect
43 1.1 christos the object since a reference to it comes from non-gc-managed space
44 1.1 christos (the objfile). */
45 1.1 christos SCM containing_scm;
46 1.1 christos };
47 1.1 christos
48 1.1 christos static const char objfile_smob_name[] = "gdb:objfile";
49 1.1 christos
50 1.1 christos /* The tag Guile knows the objfile smob by. */
51 1.1 christos static scm_t_bits objfile_smob_tag;
52 1.1 christos
53 1.1 christos static const struct objfile_data *ofscm_objfile_data_key;
54 1.1 christos
55 1.1 christos /* Return the list of pretty-printers registered with O_SMOB. */
56 1.1 christos
57 1.1 christos SCM
58 1.1 christos ofscm_objfile_smob_pretty_printers (objfile_smob *o_smob)
59 1.1 christos {
60 1.1 christos return o_smob->pretty_printers;
61 1.1 christos }
62 1.1 christos
63 1.1 christos /* Administrivia for objfile smobs. */
65 1.1 christos
66 1.1 christos /* The smob "print" function for <gdb:objfile>. */
67 1.1 christos
68 1.1 christos static int
69 1.1 christos ofscm_print_objfile_smob (SCM self, SCM port, scm_print_state *pstate)
70 1.1 christos {
71 1.1 christos objfile_smob *o_smob = (objfile_smob *) SCM_SMOB_DATA (self);
72 1.1 christos
73 1.1 christos gdbscm_printf (port, "#<%s ", objfile_smob_name);
74 1.1 christos gdbscm_printf (port, "%s",
75 1.1 christos o_smob->objfile != NULL
76 1.1 christos ? objfile_name (o_smob->objfile)
77 1.1 christos : "{invalid}");
78 1.1 christos scm_puts (">", port);
79 1.1 christos
80 1.1 christos scm_remember_upto_here_1 (self);
81 1.1 christos
82 1.1 christos /* Non-zero means success. */
83 1.1 christos return 1;
84 1.1 christos }
85 1.1 christos
86 1.1 christos /* Low level routine to create a <gdb:objfile> object.
87 1.1 christos It's empty in the sense that an OBJFILE still needs to be associated
88 1.1 christos with it. */
89 1.1 christos
90 1.1 christos static SCM
91 1.1 christos ofscm_make_objfile_smob (void)
92 1.1 christos {
93 1.1 christos objfile_smob *o_smob = (objfile_smob *)
94 1.1 christos scm_gc_malloc (sizeof (objfile_smob), objfile_smob_name);
95 1.1 christos SCM o_scm;
96 1.1 christos
97 1.1 christos o_smob->objfile = NULL;
98 1.1 christos o_smob->pretty_printers = SCM_EOL;
99 1.1 christos o_scm = scm_new_smob (objfile_smob_tag, (scm_t_bits) o_smob);
100 1.1 christos o_smob->containing_scm = o_scm;
101 1.1 christos gdbscm_init_gsmob (&o_smob->base);
102 1.1 christos
103 1.1 christos return o_scm;
104 1.1 christos }
105 1.1 christos
106 1.1 christos /* Clear the OBJFILE pointer in O_SMOB and unprotect the object from GC. */
107 1.1 christos
108 1.1 christos static void
109 1.1 christos ofscm_release_objfile (objfile_smob *o_smob)
110 1.1 christos {
111 1.1 christos o_smob->objfile = NULL;
112 1.1 christos scm_gc_unprotect_object (o_smob->containing_scm);
113 1.1 christos }
114 1.1 christos
115 1.1 christos /* Objfile registry cleanup handler for when an objfile is deleted. */
116 1.1 christos
117 1.1 christos static void
118 1.1 christos ofscm_handle_objfile_deleted (struct objfile *objfile, void *datum)
119 1.1 christos {
120 1.1 christos objfile_smob *o_smob = datum;
121 1.1 christos
122 1.1 christos gdb_assert (o_smob->objfile == objfile);
123 1.1 christos
124 1.1 christos ofscm_release_objfile (o_smob);
125 1.1 christos }
126 1.1 christos
127 1.1 christos /* Return non-zero if SCM is a <gdb:objfile> object. */
128 1.1 christos
129 1.1 christos static int
130 1.1 christos ofscm_is_objfile (SCM scm)
131 1.1 christos {
132 1.1 christos return SCM_SMOB_PREDICATE (objfile_smob_tag, scm);
133 1.1 christos }
134 1.1 christos
135 1.1 christos /* (objfile? object) -> boolean */
136 1.1 christos
137 1.1 christos static SCM
138 1.1 christos gdbscm_objfile_p (SCM scm)
139 1.1 christos {
140 1.1 christos return scm_from_bool (ofscm_is_objfile (scm));
141 1.1 christos }
142 1.1 christos
143 1.1 christos /* Return a pointer to the objfile_smob that encapsulates OBJFILE,
144 1.1 christos creating one if necessary.
145 1.1 christos The result is cached so that we have only one copy per objfile. */
146 1.1 christos
147 1.1 christos objfile_smob *
148 1.1 christos ofscm_objfile_smob_from_objfile (struct objfile *objfile)
149 1.1 christos {
150 1.1 christos objfile_smob *o_smob;
151 1.1 christos
152 1.1 christos o_smob = objfile_data (objfile, ofscm_objfile_data_key);
153 1.1 christos if (o_smob == NULL)
154 1.1 christos {
155 1.1 christos SCM o_scm = ofscm_make_objfile_smob ();
156 1.1 christos
157 1.1 christos o_smob = (objfile_smob *) SCM_SMOB_DATA (o_scm);
158 1.1 christos o_smob->objfile = objfile;
159 1.1 christos
160 1.1 christos set_objfile_data (objfile, ofscm_objfile_data_key, o_smob);
161 1.1 christos scm_gc_protect_object (o_smob->containing_scm);
162 1.1 christos }
163 1.1 christos
164 1.1 christos return o_smob;
165 1.1 christos }
166 1.1 christos
167 1.1 christos /* Return the <gdb:objfile> object that encapsulates OBJFILE. */
168 1.1 christos
169 1.1 christos SCM
170 1.1 christos ofscm_scm_from_objfile (struct objfile *objfile)
171 1.1 christos {
172 1.1 christos objfile_smob *o_smob = ofscm_objfile_smob_from_objfile (objfile);
173 1.1 christos
174 1.1 christos return o_smob->containing_scm;
175 1.1 christos }
176 1.1 christos
177 1.1 christos /* Returns the <gdb:objfile> object in SELF.
178 1.1 christos Throws an exception if SELF is not a <gdb:objfile> object. */
179 1.1 christos
180 1.1 christos static SCM
181 1.1 christos ofscm_get_objfile_arg_unsafe (SCM self, int arg_pos, const char *func_name)
182 1.1 christos {
183 1.1 christos SCM_ASSERT_TYPE (ofscm_is_objfile (self), self, arg_pos, func_name,
184 1.1 christos objfile_smob_name);
185 1.1 christos
186 1.1 christos return self;
187 1.1 christos }
188 1.1 christos
189 1.1 christos /* Returns a pointer to the objfile smob of SELF.
190 1.1 christos Throws an exception if SELF is not a <gdb:objfile> object. */
191 1.1 christos
192 1.1 christos static objfile_smob *
193 1.1 christos ofscm_get_objfile_smob_arg_unsafe (SCM self, int arg_pos,
194 1.1 christos const char *func_name)
195 1.1 christos {
196 1.1 christos SCM o_scm = ofscm_get_objfile_arg_unsafe (self, arg_pos, func_name);
197 1.1 christos objfile_smob *o_smob = (objfile_smob *) SCM_SMOB_DATA (o_scm);
198 1.1 christos
199 1.1 christos return o_smob;
200 1.1 christos }
201 1.1 christos
202 1.1 christos /* Return non-zero if objfile O_SMOB is valid. */
203 1.1 christos
204 1.1 christos static int
205 1.1 christos ofscm_is_valid (objfile_smob *o_smob)
206 1.1 christos {
207 1.1 christos return o_smob->objfile != NULL;
208 1.1 christos }
209 1.1 christos
210 1.1 christos /* Return the objfile smob in SELF, verifying it's valid.
211 1.1 christos Throws an exception if SELF is not a <gdb:objfile> object or is invalid. */
212 1.1 christos
213 1.1 christos static objfile_smob *
214 1.1 christos ofscm_get_valid_objfile_smob_arg_unsafe (SCM self, int arg_pos,
215 1.1 christos const char *func_name)
216 1.1 christos {
217 1.1 christos objfile_smob *o_smob
218 1.1 christos = ofscm_get_objfile_smob_arg_unsafe (self, arg_pos, func_name);
219 1.1 christos
220 1.1 christos if (!ofscm_is_valid (o_smob))
221 1.1 christos {
222 1.1 christos gdbscm_invalid_object_error (func_name, arg_pos, self,
223 1.1 christos _("<gdb:objfile>"));
224 1.1 christos }
225 1.1 christos
226 1.1 christos return o_smob;
227 1.1 christos }
228 1.1 christos
229 1.1 christos /* Objfile methods. */
231 1.1 christos
232 1.1 christos /* (objfile-valid? <gdb:objfile>) -> boolean
233 1.1 christos Returns #t if this object file still exists in GDB. */
234 1.1 christos
235 1.1 christos static SCM
236 1.1 christos gdbscm_objfile_valid_p (SCM self)
237 1.1 christos {
238 1.1 christos objfile_smob *o_smob
239 1.1 christos = ofscm_get_objfile_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
240 1.1 christos
241 1.1 christos return scm_from_bool (o_smob->objfile != NULL);
242 1.1 christos }
243 1.1 christos
244 1.1 christos /* (objfile-filename <gdb:objfile>) -> string
245 1.1 christos Returns the objfile's file name.
246 1.1 christos Throw's an exception if the underlying objfile is invalid. */
247 1.1 christos
248 1.1 christos static SCM
249 1.1 christos gdbscm_objfile_filename (SCM self)
250 1.1 christos {
251 1.1 christos objfile_smob *o_smob
252 1.1 christos = ofscm_get_valid_objfile_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
253 1.1 christos
254 1.1 christos return gdbscm_scm_from_c_string (objfile_name (o_smob->objfile));
255 1.1 christos }
256 1.1 christos
257 1.1 christos /* (objfile-pretty-printers <gdb:objfile>) -> list
258 1.1 christos Returns the list of pretty-printers for this objfile. */
259 1.1 christos
260 1.1 christos static SCM
261 1.1 christos gdbscm_objfile_pretty_printers (SCM self)
262 1.1 christos {
263 1.1 christos objfile_smob *o_smob
264 1.1 christos = ofscm_get_objfile_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
265 1.1 christos
266 1.1 christos return o_smob->pretty_printers;
267 1.1 christos }
268 1.1 christos
269 1.1 christos /* (set-objfile-pretty-printers! <gdb:objfile> list) -> unspecified
270 1.1 christos Set the pretty-printers for this objfile. */
271 1.1 christos
272 1.1 christos static SCM
273 1.1 christos gdbscm_set_objfile_pretty_printers_x (SCM self, SCM printers)
274 1.1 christos {
275 1.1 christos objfile_smob *o_smob
276 1.1 christos = ofscm_get_objfile_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
277 1.1 christos
278 1.1 christos SCM_ASSERT_TYPE (gdbscm_is_true (scm_list_p (printers)), printers,
279 1.1 christos SCM_ARG2, FUNC_NAME, _("list"));
280 1.1 christos
281 1.1 christos o_smob->pretty_printers = printers;
282 1.1 christos
283 1.1 christos return SCM_UNSPECIFIED;
284 1.1 christos }
285 1.1 christos
286 1.1 christos /* The "current" objfile. This is set when gdb detects that a new
288 1.1 christos objfile has been loaded. It is only set for the duration of a call to
289 1.1 christos gdbscm_source_objfile_script; it is NULL at other times. */
290 1.1 christos static struct objfile *ofscm_current_objfile;
291 1.1 christos
292 1.1 christos /* Set the current objfile to OBJFILE and then read FILE named FILENAME
293 1.1 christos as Guile code. This does not throw any errors. If an exception
294 1.1 christos occurs Guile will print the backtrace.
295 1.1 christos This is the extension_language_script_ops.objfile_script_sourcer
296 1.1 christos "method". */
297 1.1 christos
298 1.1 christos void
299 1.1 christos gdbscm_source_objfile_script (const struct extension_language_defn *extlang,
300 1.1 christos struct objfile *objfile, FILE *file,
301 1.1 christos const char *filename)
302 1.1 christos {
303 1.1 christos char *msg;
304 1.1 christos
305 1.1 christos ofscm_current_objfile = objfile;
306 1.1 christos
307 1.1 christos msg = gdbscm_safe_source_script (filename);
308 1.1 christos if (msg != NULL)
309 1.1 christos {
310 1.1 christos fprintf_filtered (gdb_stderr, "%s", msg);
311 1.1 christos xfree (msg);
312 1.1 christos }
313 1.1 christos
314 1.1 christos ofscm_current_objfile = NULL;
315 1.1 christos }
316 1.1 christos
317 1.1 christos /* (current-objfile) -> <gdb:obfjile>
318 1.1 christos Return the current objfile, or #f if there isn't one.
319 1.1 christos Ideally this would be named ofscm_current_objfile, but that name is
320 1.1 christos taken by the variable recording the current objfile. */
321 1.1 christos
322 1.1 christos static SCM
323 1.1 christos gdbscm_get_current_objfile (void)
324 1.1 christos {
325 1.1 christos if (ofscm_current_objfile == NULL)
326 1.1 christos return SCM_BOOL_F;
327 1.1 christos
328 1.1 christos return ofscm_scm_from_objfile (ofscm_current_objfile);
329 1.1 christos }
330 1.1 christos
331 1.1 christos /* (objfiles) -> list
332 1.1 christos Return a list of all objfiles in the current program space. */
333 1.1 christos
334 1.1 christos static SCM
335 1.1 christos gdbscm_objfiles (void)
336 1.1 christos {
337 1.1 christos struct objfile *objf;
338 1.1 christos SCM result;
339 1.1 christos
340 1.1 christos result = SCM_EOL;
341 1.1 christos
342 1.1 christos ALL_OBJFILES (objf)
343 1.1 christos {
344 1.1 christos SCM item = ofscm_scm_from_objfile (objf);
345 1.1 christos
346 1.1 christos result = scm_cons (item, result);
347 1.1 christos }
348 1.1 christos
349 1.1 christos return scm_reverse_x (result, SCM_EOL);
350 1.1 christos }
351 1.1 christos
352 1.1 christos /* Initialize the Scheme objfile support. */
354 1.1 christos
355 1.1 christos static const scheme_function objfile_functions[] =
356 1.1 christos {
357 1.1 christos { "objfile?", 1, 0, 0, gdbscm_objfile_p,
358 1.1 christos "\
359 1.1 christos Return #t if the object is a <gdb:objfile> object." },
360 1.1 christos
361 1.1 christos { "objfile-valid?", 1, 0, 0, gdbscm_objfile_valid_p,
362 1.1 christos "\
363 1.1 christos Return #t if the objfile is valid (hasn't been deleted from gdb)." },
364 1.1 christos
365 1.1 christos { "objfile-filename", 1, 0, 0, gdbscm_objfile_filename,
366 1.1 christos "\
367 1.1 christos Return the file name of the objfile." },
368 1.1 christos
369 1.1 christos { "objfile-pretty-printers", 1, 0, 0, gdbscm_objfile_pretty_printers,
370 1.1 christos "\
371 1.1 christos Return a list of pretty-printers of the objfile." },
372 1.1 christos
373 1.1 christos { "set-objfile-pretty-printers!", 2, 0, 0,
374 1.1 christos gdbscm_set_objfile_pretty_printers_x,
375 1.1 christos "\
376 1.1 christos Set the list of pretty-printers of the objfile." },
377 1.1 christos
378 1.1 christos { "current-objfile", 0, 0, 0, gdbscm_get_current_objfile,
379 1.1 christos "\
380 1.1 christos Return the current objfile if there is one or #f if there isn't one." },
381 1.1 christos
382 1.1 christos { "objfiles", 0, 0, 0, gdbscm_objfiles,
383 1.1 christos "\
384 1.1 christos Return a list of all objfiles in the current program space." },
385 1.1 christos
386 1.1 christos END_FUNCTIONS
387 1.1 christos };
388 1.1 christos
389 1.1 christos void
390 1.1 christos gdbscm_initialize_objfiles (void)
391 1.1 christos {
392 1.1 christos objfile_smob_tag
393 1.1 christos = gdbscm_make_smob_type (objfile_smob_name, sizeof (objfile_smob));
394 1.1 christos scm_set_smob_print (objfile_smob_tag, ofscm_print_objfile_smob);
395 1.1 christos
396 1.1 christos gdbscm_define_functions (objfile_functions, 1);
397
398 ofscm_objfile_data_key
399 = register_objfile_data_with_cleanup (NULL, ofscm_handle_objfile_deleted);
400 }
401