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 /* Objfile registry cleanup handler for when an objfile is deleted. */
55 void operator() (objfile_smob
*o_smob
)
57 o_smob
->objfile
= NULL
;
58 scm_gc_unprotect_object (o_smob
->containing_scm
);
62 static const registry
<objfile
>::key
<objfile_smob
, ofscm_deleter
>
63 ofscm_objfile_data_key
;
65 /* Return the list of pretty-printers registered with O_SMOB. */
68 ofscm_objfile_smob_pretty_printers (objfile_smob
*o_smob
)
70 return o_smob
->pretty_printers
;
73 /* Administrivia for objfile smobs. */
75 /* The smob "print" function for <gdb:objfile>. */
78 ofscm_print_objfile_smob (SCM self
, SCM port
, scm_print_state
*pstate
)
80 objfile_smob
*o_smob
= (objfile_smob
*) SCM_SMOB_DATA (self
);
82 gdbscm_printf (port
, "#<%s ", objfile_smob_name
);
83 gdbscm_printf (port
, "%s",
84 o_smob
->objfile
!= NULL
85 ? objfile_name (o_smob
->objfile
)
89 scm_remember_upto_here_1 (self
);
91 /* Non-zero means success. */
95 /* Low level routine to create a <gdb:objfile> object.
96 It's empty in the sense that an OBJFILE still needs to be associated
100 ofscm_make_objfile_smob (void)
102 objfile_smob
*o_smob
= (objfile_smob
*)
103 scm_gc_malloc (sizeof (objfile_smob
), objfile_smob_name
);
106 o_smob
->objfile
= NULL
;
107 o_smob
->pretty_printers
= SCM_EOL
;
108 o_scm
= scm_new_smob (objfile_smob_tag
, (scm_t_bits
) o_smob
);
109 o_smob
->containing_scm
= o_scm
;
110 gdbscm_init_gsmob (&o_smob
->base
);
115 /* Return non-zero if SCM is a <gdb:objfile> object. */
118 ofscm_is_objfile (SCM scm
)
120 return SCM_SMOB_PREDICATE (objfile_smob_tag
, scm
);
123 /* (objfile? object) -> boolean */
126 gdbscm_objfile_p (SCM scm
)
128 return scm_from_bool (ofscm_is_objfile (scm
));
131 /* Return a pointer to the objfile_smob that encapsulates OBJFILE,
132 creating one if necessary.
133 The result is cached so that we have only one copy per objfile. */
136 ofscm_objfile_smob_from_objfile (struct objfile
*objfile
)
138 objfile_smob
*o_smob
;
140 o_smob
= ofscm_objfile_data_key
.get (objfile
);
143 SCM o_scm
= ofscm_make_objfile_smob ();
145 o_smob
= (objfile_smob
*) SCM_SMOB_DATA (o_scm
);
146 o_smob
->objfile
= objfile
;
148 ofscm_objfile_data_key
.set (objfile
, o_smob
);
149 scm_gc_protect_object (o_smob
->containing_scm
);
155 /* Return the <gdb:objfile> object that encapsulates OBJFILE. */
158 ofscm_scm_from_objfile (struct objfile
*objfile
)
160 objfile_smob
*o_smob
= ofscm_objfile_smob_from_objfile (objfile
);
162 return o_smob
->containing_scm
;
165 /* Returns the <gdb:objfile> object in SELF.
166 Throws an exception if SELF is not a <gdb:objfile> object. */
169 ofscm_get_objfile_arg_unsafe (SCM self
, int arg_pos
, const char *func_name
)
171 SCM_ASSERT_TYPE (ofscm_is_objfile (self
), self
, arg_pos
, func_name
,
177 /* Returns a pointer to the objfile smob of SELF.
178 Throws an exception if SELF is not a <gdb:objfile> object. */
180 static objfile_smob
*
181 ofscm_get_objfile_smob_arg_unsafe (SCM self
, int arg_pos
,
182 const char *func_name
)
184 SCM o_scm
= ofscm_get_objfile_arg_unsafe (self
, arg_pos
, func_name
);
185 objfile_smob
*o_smob
= (objfile_smob
*) SCM_SMOB_DATA (o_scm
);
190 /* Return non-zero if objfile O_SMOB is valid. */
193 ofscm_is_valid (objfile_smob
*o_smob
)
195 return o_smob
->objfile
!= NULL
;
198 /* Return the objfile smob in SELF, verifying it's valid.
199 Throws an exception if SELF is not a <gdb:objfile> object or is invalid. */
201 static objfile_smob
*
202 ofscm_get_valid_objfile_smob_arg_unsafe (SCM self
, int arg_pos
,
203 const char *func_name
)
206 = ofscm_get_objfile_smob_arg_unsafe (self
, arg_pos
, func_name
);
208 if (!ofscm_is_valid (o_smob
))
210 gdbscm_invalid_object_error (func_name
, arg_pos
, self
,
217 /* Objfile methods. */
219 /* (objfile-valid? <gdb:objfile>) -> boolean
220 Returns #t if this object file still exists in GDB. */
223 gdbscm_objfile_valid_p (SCM self
)
226 = ofscm_get_objfile_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
228 return scm_from_bool (o_smob
->objfile
!= NULL
);
231 /* (objfile-filename <gdb:objfile>) -> string
232 Returns the objfile's file name.
233 Throw's an exception if the underlying objfile is invalid. */
236 gdbscm_objfile_filename (SCM self
)
239 = ofscm_get_valid_objfile_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
241 return gdbscm_scm_from_c_string (objfile_name (o_smob
->objfile
));
244 /* (objfile-progspace <gdb:objfile>) -> <gdb:progspace>
245 Returns the objfile's progspace.
246 Throw's an exception if the underlying objfile is invalid. */
249 gdbscm_objfile_progspace (SCM self
)
252 = ofscm_get_valid_objfile_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
254 return psscm_scm_from_pspace (o_smob
->objfile
->pspace
);
257 /* (objfile-pretty-printers <gdb:objfile>) -> list
258 Returns the list of pretty-printers for this objfile. */
261 gdbscm_objfile_pretty_printers (SCM self
)
264 = ofscm_get_objfile_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
266 return o_smob
->pretty_printers
;
269 /* (set-objfile-pretty-printers! <gdb:objfile> list) -> unspecified
270 Set the pretty-printers for this objfile. */
273 gdbscm_set_objfile_pretty_printers_x (SCM self
, SCM printers
)
276 = ofscm_get_objfile_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
278 SCM_ASSERT_TYPE (gdbscm_is_true (scm_list_p (printers
)), printers
,
279 SCM_ARG2
, FUNC_NAME
, _("list"));
281 o_smob
->pretty_printers
= printers
;
283 return SCM_UNSPECIFIED
;
286 /* The "current" objfile. This is set when gdb detects that a new
287 objfile has been loaded. It is only set for the duration of a call to
288 gdbscm_source_objfile_script and gdbscm_execute_objfile_script; it is NULL
290 static struct objfile
*ofscm_current_objfile
;
292 /* Set the current objfile to OBJFILE and then read FILE named FILENAME
293 as Guile code. This does not throw any errors. If an exception
294 occurs Guile will print the backtrace.
295 This is the extension_language_script_ops.objfile_script_sourcer
299 gdbscm_source_objfile_script (const struct extension_language_defn
*extlang
,
300 struct objfile
*objfile
, FILE *file
,
301 const char *filename
)
303 ofscm_current_objfile
= objfile
;
305 gdb::unique_xmalloc_ptr
<char> msg
= gdbscm_safe_source_script (filename
);
307 gdb_printf (gdb_stderr
, "%s", msg
.get ());
309 ofscm_current_objfile
= NULL
;
312 /* Set the current objfile to OBJFILE and then read FILE named FILENAME
313 as Guile code. This does not throw any errors. If an exception
314 occurs Guile will print the backtrace.
315 This is the extension_language_script_ops.objfile_script_sourcer
319 gdbscm_execute_objfile_script (const struct extension_language_defn
*extlang
,
320 struct objfile
*objfile
, const char *name
,
323 ofscm_current_objfile
= objfile
;
325 gdb::unique_xmalloc_ptr
<char> msg
326 = gdbscm_safe_eval_string (script
, 0 /* display_result */);
328 gdb_printf (gdb_stderr
, "%s", msg
.get ());
330 ofscm_current_objfile
= NULL
;
333 /* (current-objfile) -> <gdb:objfile>
334 Return the current objfile, or #f if there isn't one.
335 Ideally this would be named ofscm_current_objfile, but that name is
336 taken by the variable recording the current objfile. */
339 gdbscm_get_current_objfile (void)
341 if (ofscm_current_objfile
== NULL
)
344 return ofscm_scm_from_objfile (ofscm_current_objfile
);
347 /* (objfiles) -> list
348 Return a list of all objfiles in the current program space. */
351 gdbscm_objfiles (void)
357 for (objfile
*objf
: current_program_space
->objfiles ())
359 SCM item
= ofscm_scm_from_objfile (objf
);
361 result
= scm_cons (item
, result
);
364 return scm_reverse_x (result
, SCM_EOL
);
367 /* Initialize the Scheme objfile support. */
369 static const scheme_function objfile_functions
[] =
371 { "objfile?", 1, 0, 0, as_a_scm_t_subr (gdbscm_objfile_p
),
373 Return #t if the object is a <gdb:objfile> object." },
375 { "objfile-valid?", 1, 0, 0, as_a_scm_t_subr (gdbscm_objfile_valid_p
),
377 Return #t if the objfile is valid (hasn't been deleted from gdb)." },
379 { "objfile-filename", 1, 0, 0, as_a_scm_t_subr (gdbscm_objfile_filename
),
381 Return the file name of the objfile." },
383 { "objfile-progspace", 1, 0, 0, as_a_scm_t_subr (gdbscm_objfile_progspace
),
385 Return the progspace that the objfile lives in." },
387 { "objfile-pretty-printers", 1, 0, 0,
388 as_a_scm_t_subr (gdbscm_objfile_pretty_printers
),
390 Return a list of pretty-printers of the objfile." },
392 { "set-objfile-pretty-printers!", 2, 0, 0,
393 as_a_scm_t_subr (gdbscm_set_objfile_pretty_printers_x
),
395 Set the list of pretty-printers of the objfile." },
397 { "current-objfile", 0, 0, 0, as_a_scm_t_subr (gdbscm_get_current_objfile
),
399 Return the current objfile if there is one or #f if there isn't one." },
401 { "objfiles", 0, 0, 0, as_a_scm_t_subr (gdbscm_objfiles
),
403 Return a list of all objfiles in the current program space." },
409 gdbscm_initialize_objfiles (void)
412 = gdbscm_make_smob_type (objfile_smob_name
, sizeof (objfile_smob
));
413 scm_set_smob_print (objfile_smob_tag
, ofscm_print_objfile_smob
);
415 gdbscm_define_functions (objfile_functions
, 1);