1 /* Scheme interface to objfiles.
3 Copyright (C) 2008-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 /* See README file in this directory for implementation notes, coding
21 conventions, et.al. */
26 #include "guile-internal.h"
28 /* The <gdb:objfile> smob. */
32 /* This always appears first. */
35 /* The corresponding objfile. */
36 struct objfile
*objfile
;
38 /* The pretty-printer list of functions. */
41 /* The <gdb:objfile> object we are contained in, needed to protect/unprotect
42 the object since a reference to it comes from non-gc-managed space
47 static const char objfile_smob_name
[] = "gdb:objfile";
49 /* The tag Guile knows the objfile smob by. */
50 static scm_t_bits objfile_smob_tag
;
52 static const struct objfile_data
*ofscm_objfile_data_key
;
54 /* Return the list of pretty-printers registered with O_SMOB. */
57 ofscm_objfile_smob_pretty_printers (objfile_smob
*o_smob
)
59 return o_smob
->pretty_printers
;
62 /* Administrivia for objfile smobs. */
64 /* The smob "print" function for <gdb:objfile>. */
67 ofscm_print_objfile_smob (SCM self
, SCM port
, scm_print_state
*pstate
)
69 objfile_smob
*o_smob
= (objfile_smob
*) SCM_SMOB_DATA (self
);
71 gdbscm_printf (port
, "#<%s ", objfile_smob_name
);
72 gdbscm_printf (port
, "%s",
73 o_smob
->objfile
!= NULL
74 ? objfile_name (o_smob
->objfile
)
78 scm_remember_upto_here_1 (self
);
80 /* Non-zero means success. */
84 /* Low level routine to create a <gdb:objfile> object.
85 It's empty in the sense that an OBJFILE still needs to be associated
89 ofscm_make_objfile_smob (void)
91 objfile_smob
*o_smob
= (objfile_smob
*)
92 scm_gc_malloc (sizeof (objfile_smob
), objfile_smob_name
);
95 o_smob
->objfile
= NULL
;
96 o_smob
->pretty_printers
= SCM_EOL
;
97 o_scm
= scm_new_smob (objfile_smob_tag
, (scm_t_bits
) o_smob
);
98 o_smob
->containing_scm
= o_scm
;
99 gdbscm_init_gsmob (&o_smob
->base
);
104 /* Clear the OBJFILE pointer in O_SMOB and unprotect the object from GC. */
107 ofscm_release_objfile (objfile_smob
*o_smob
)
109 o_smob
->objfile
= NULL
;
110 scm_gc_unprotect_object (o_smob
->containing_scm
);
113 /* Objfile registry cleanup handler for when an objfile is deleted. */
116 ofscm_handle_objfile_deleted (struct objfile
*objfile
, void *datum
)
118 objfile_smob
*o_smob
= (objfile_smob
*) datum
;
120 gdb_assert (o_smob
->objfile
== objfile
);
122 ofscm_release_objfile (o_smob
);
125 /* Return non-zero if SCM is a <gdb:objfile> object. */
128 ofscm_is_objfile (SCM scm
)
130 return SCM_SMOB_PREDICATE (objfile_smob_tag
, scm
);
133 /* (objfile? object) -> boolean */
136 gdbscm_objfile_p (SCM scm
)
138 return scm_from_bool (ofscm_is_objfile (scm
));
141 /* Return a pointer to the objfile_smob that encapsulates OBJFILE,
142 creating one if necessary.
143 The result is cached so that we have only one copy per objfile. */
146 ofscm_objfile_smob_from_objfile (struct objfile
*objfile
)
148 objfile_smob
*o_smob
;
150 o_smob
= (objfile_smob
*) objfile_data (objfile
, ofscm_objfile_data_key
);
153 SCM o_scm
= ofscm_make_objfile_smob ();
155 o_smob
= (objfile_smob
*) SCM_SMOB_DATA (o_scm
);
156 o_smob
->objfile
= objfile
;
158 set_objfile_data (objfile
, ofscm_objfile_data_key
, o_smob
);
159 scm_gc_protect_object (o_smob
->containing_scm
);
165 /* Return the <gdb:objfile> object that encapsulates OBJFILE. */
168 ofscm_scm_from_objfile (struct objfile
*objfile
)
170 objfile_smob
*o_smob
= ofscm_objfile_smob_from_objfile (objfile
);
172 return o_smob
->containing_scm
;
175 /* Returns the <gdb:objfile> object in SELF.
176 Throws an exception if SELF is not a <gdb:objfile> object. */
179 ofscm_get_objfile_arg_unsafe (SCM self
, int arg_pos
, const char *func_name
)
181 SCM_ASSERT_TYPE (ofscm_is_objfile (self
), self
, arg_pos
, func_name
,
187 /* Returns a pointer to the objfile smob of SELF.
188 Throws an exception if SELF is not a <gdb:objfile> object. */
190 static objfile_smob
*
191 ofscm_get_objfile_smob_arg_unsafe (SCM self
, int arg_pos
,
192 const char *func_name
)
194 SCM o_scm
= ofscm_get_objfile_arg_unsafe (self
, arg_pos
, func_name
);
195 objfile_smob
*o_smob
= (objfile_smob
*) SCM_SMOB_DATA (o_scm
);
200 /* Return non-zero if objfile O_SMOB is valid. */
203 ofscm_is_valid (objfile_smob
*o_smob
)
205 return o_smob
->objfile
!= NULL
;
208 /* Return the objfile smob in SELF, verifying it's valid.
209 Throws an exception if SELF is not a <gdb:objfile> object or is invalid. */
211 static objfile_smob
*
212 ofscm_get_valid_objfile_smob_arg_unsafe (SCM self
, int arg_pos
,
213 const char *func_name
)
216 = ofscm_get_objfile_smob_arg_unsafe (self
, arg_pos
, func_name
);
218 if (!ofscm_is_valid (o_smob
))
220 gdbscm_invalid_object_error (func_name
, arg_pos
, self
,
227 /* Objfile methods. */
229 /* (objfile-valid? <gdb:objfile>) -> boolean
230 Returns #t if this object file still exists in GDB. */
233 gdbscm_objfile_valid_p (SCM self
)
236 = ofscm_get_objfile_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
238 return scm_from_bool (o_smob
->objfile
!= NULL
);
241 /* (objfile-filename <gdb:objfile>) -> string
242 Returns the objfile's file name.
243 Throw's an exception if the underlying objfile is invalid. */
246 gdbscm_objfile_filename (SCM self
)
249 = ofscm_get_valid_objfile_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
251 return gdbscm_scm_from_c_string (objfile_name (o_smob
->objfile
));
254 /* (objfile-progspace <gdb:objfile>) -> <gdb:progspace>
255 Returns the objfile's progspace.
256 Throw's an exception if the underlying objfile is invalid. */
259 gdbscm_objfile_progspace (SCM self
)
262 = ofscm_get_valid_objfile_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
264 return psscm_scm_from_pspace (o_smob
->objfile
->pspace
);
267 /* (objfile-pretty-printers <gdb:objfile>) -> list
268 Returns the list of pretty-printers for this objfile. */
271 gdbscm_objfile_pretty_printers (SCM self
)
274 = ofscm_get_objfile_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
276 return o_smob
->pretty_printers
;
279 /* (set-objfile-pretty-printers! <gdb:objfile> list) -> unspecified
280 Set the pretty-printers for this objfile. */
283 gdbscm_set_objfile_pretty_printers_x (SCM self
, SCM printers
)
286 = ofscm_get_objfile_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
288 SCM_ASSERT_TYPE (gdbscm_is_true (scm_list_p (printers
)), printers
,
289 SCM_ARG2
, FUNC_NAME
, _("list"));
291 o_smob
->pretty_printers
= printers
;
293 return SCM_UNSPECIFIED
;
296 /* The "current" objfile. This is set when gdb detects that a new
297 objfile has been loaded. It is only set for the duration of a call to
298 gdbscm_source_objfile_script and gdbscm_execute_objfile_script; it is NULL
300 static struct objfile
*ofscm_current_objfile
;
302 /* Set the current objfile to OBJFILE and then read FILE named FILENAME
303 as Guile code. This does not throw any errors. If an exception
304 occurs Guile will print the backtrace.
305 This is the extension_language_script_ops.objfile_script_sourcer
309 gdbscm_source_objfile_script (const struct extension_language_defn
*extlang
,
310 struct objfile
*objfile
, FILE *file
,
311 const char *filename
)
313 ofscm_current_objfile
= objfile
;
315 gdb::unique_xmalloc_ptr
<char> msg
= gdbscm_safe_source_script (filename
);
317 gdb_printf (gdb_stderr
, "%s", msg
.get ());
319 ofscm_current_objfile
= NULL
;
322 /* Set the current objfile to OBJFILE and then read FILE named FILENAME
323 as Guile code. This does not throw any errors. If an exception
324 occurs Guile will print the backtrace.
325 This is the extension_language_script_ops.objfile_script_sourcer
329 gdbscm_execute_objfile_script (const struct extension_language_defn
*extlang
,
330 struct objfile
*objfile
, const char *name
,
333 ofscm_current_objfile
= objfile
;
335 gdb::unique_xmalloc_ptr
<char> msg
336 = gdbscm_safe_eval_string (script
, 0 /* display_result */);
338 gdb_printf (gdb_stderr
, "%s", msg
.get ());
340 ofscm_current_objfile
= NULL
;
343 /* (current-objfile) -> <gdb:objfile>
344 Return the current objfile, or #f if there isn't one.
345 Ideally this would be named ofscm_current_objfile, but that name is
346 taken by the variable recording the current objfile. */
349 gdbscm_get_current_objfile (void)
351 if (ofscm_current_objfile
== NULL
)
354 return ofscm_scm_from_objfile (ofscm_current_objfile
);
357 /* (objfiles) -> list
358 Return a list of all objfiles in the current program space. */
361 gdbscm_objfiles (void)
367 for (objfile
*objf
: current_program_space
->objfiles ())
369 SCM item
= ofscm_scm_from_objfile (objf
);
371 result
= scm_cons (item
, result
);
374 return scm_reverse_x (result
, SCM_EOL
);
377 /* Initialize the Scheme objfile support. */
379 static const scheme_function objfile_functions
[] =
381 { "objfile?", 1, 0, 0, as_a_scm_t_subr (gdbscm_objfile_p
),
383 Return #t if the object is a <gdb:objfile> object." },
385 { "objfile-valid?", 1, 0, 0, as_a_scm_t_subr (gdbscm_objfile_valid_p
),
387 Return #t if the objfile is valid (hasn't been deleted from gdb)." },
389 { "objfile-filename", 1, 0, 0, as_a_scm_t_subr (gdbscm_objfile_filename
),
391 Return the file name of the objfile." },
393 { "objfile-progspace", 1, 0, 0, as_a_scm_t_subr (gdbscm_objfile_progspace
),
395 Return the progspace that the objfile lives in." },
397 { "objfile-pretty-printers", 1, 0, 0,
398 as_a_scm_t_subr (gdbscm_objfile_pretty_printers
),
400 Return a list of pretty-printers of the objfile." },
402 { "set-objfile-pretty-printers!", 2, 0, 0,
403 as_a_scm_t_subr (gdbscm_set_objfile_pretty_printers_x
),
405 Set the list of pretty-printers of the objfile." },
407 { "current-objfile", 0, 0, 0, as_a_scm_t_subr (gdbscm_get_current_objfile
),
409 Return the current objfile if there is one or #f if there isn't one." },
411 { "objfiles", 0, 0, 0, as_a_scm_t_subr (gdbscm_objfiles
),
413 Return a list of all objfiles in the current program space." },
419 gdbscm_initialize_objfiles (void)
422 = gdbscm_make_smob_type (objfile_smob_name
, sizeof (objfile_smob
));
423 scm_set_smob_print (objfile_smob_tag
, ofscm_print_objfile_smob
);
425 gdbscm_define_functions (objfile_functions
, 1);
428 void _initialize_scm_objfile ();
430 _initialize_scm_objfile ()
432 ofscm_objfile_data_key
433 = register_objfile_data_with_cleanup (NULL
, ofscm_handle_objfile_deleted
);