1 /* Guile interface to program spaces.
3 Copyright (C) 2010-2017 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/>. */
22 #include "progspace.h"
25 #include "arch-utils.h"
26 #include "guile-internal.h"
28 /* NOTE: Python exports the name "Progspace", so we export "progspace".
29 Internally we shorten that to "pspace". */
31 /* The <gdb:progspace> smob.
32 The typedef for this struct is in guile-internal.h. */
36 /* This always appears first. */
39 /* The corresponding pspace. */
40 struct program_space *pspace;
42 /* The pretty-printer list of functions. */
45 /* The <gdb:progspace> object we are contained in, needed to
46 protect/unprotect the object since a reference to it comes from
47 non-gc-managed space (the progspace). */
51 static const char pspace_smob_name[] = "gdb:progspace";
53 /* The tag Guile knows the pspace smob by. */
54 static scm_t_bits pspace_smob_tag;
56 static const struct program_space_data *psscm_pspace_data_key;
58 /* Return the list of pretty-printers registered with P_SMOB. */
61 psscm_pspace_smob_pretty_printers (const pspace_smob *p_smob)
63 return p_smob->pretty_printers;
66 /* Administrivia for progspace smobs. */
68 /* The smob "print" function for <gdb:progspace>. */
71 psscm_print_pspace_smob (SCM self, SCM port, scm_print_state *pstate)
73 pspace_smob *p_smob = (pspace_smob *) SCM_SMOB_DATA (self);
75 gdbscm_printf (port, "#<%s ", pspace_smob_name);
76 if (p_smob->pspace != NULL)
78 struct objfile *objfile = p_smob->pspace->symfile_object_file;
80 gdbscm_printf (port, "%s",
82 ? objfile_name (objfile)
86 scm_puts ("{invalid}", port);
89 scm_remember_upto_here_1 (self);
91 /* Non-zero means success. */
95 /* Low level routine to create a <gdb:progspace> object.
96 It's empty in the sense that a progspace still needs to be associated
100 psscm_make_pspace_smob (void)
102 pspace_smob *p_smob = (pspace_smob *)
103 scm_gc_malloc (sizeof (pspace_smob), pspace_smob_name);
106 p_smob->pspace = NULL;
107 p_smob->pretty_printers = SCM_EOL;
108 p_scm = scm_new_smob (pspace_smob_tag, (scm_t_bits) p_smob);
109 p_smob->containing_scm = p_scm;
110 gdbscm_init_gsmob (&p_smob->base);
115 /* Clear the progspace pointer in P_SMOB and unprotect the object from GC. */
118 psscm_release_pspace (pspace_smob *p_smob)
120 p_smob->pspace = NULL;
121 scm_gc_unprotect_object (p_smob->containing_scm);
124 /* Progspace registry cleanup handler for when a progspace is deleted. */
127 psscm_handle_pspace_deleted (struct program_space *pspace, void *datum)
129 pspace_smob *p_smob = (pspace_smob *) datum;
131 gdb_assert (p_smob->pspace == pspace);
133 psscm_release_pspace (p_smob);
136 /* Return non-zero if SCM is a <gdb:progspace> object. */
139 psscm_is_pspace (SCM scm)
141 return SCM_SMOB_PREDICATE (pspace_smob_tag, scm);
144 /* (progspace? object) -> boolean */
147 gdbscm_progspace_p (SCM scm)
149 return scm_from_bool (psscm_is_pspace (scm));
152 /* Return a pointer to the progspace_smob that encapsulates PSPACE,
153 creating one if necessary.
154 The result is cached so that we have only one copy per objfile. */
157 psscm_pspace_smob_from_pspace (struct program_space *pspace)
161 p_smob = (pspace_smob *) program_space_data (pspace, psscm_pspace_data_key);
164 SCM p_scm = psscm_make_pspace_smob ();
166 p_smob = (pspace_smob *) SCM_SMOB_DATA (p_scm);
167 p_smob->pspace = pspace;
169 set_program_space_data (pspace, psscm_pspace_data_key, p_smob);
170 scm_gc_protect_object (p_smob->containing_scm);
176 /* Return the <gdb:progspace> object that encapsulates PSPACE. */
179 psscm_scm_from_pspace (struct program_space *pspace)
181 pspace_smob *p_smob = psscm_pspace_smob_from_pspace (pspace);
183 return p_smob->containing_scm;
186 /* Returns the <gdb:progspace> object in SELF.
187 Throws an exception if SELF is not a <gdb:progspace> object. */
190 psscm_get_pspace_arg_unsafe (SCM self, int arg_pos, const char *func_name)
192 SCM_ASSERT_TYPE (psscm_is_pspace (self), self, arg_pos, func_name,
198 /* Returns a pointer to the pspace smob of SELF.
199 Throws an exception if SELF is not a <gdb:progspace> object. */
202 psscm_get_pspace_smob_arg_unsafe (SCM self, int arg_pos,
203 const char *func_name)
205 SCM p_scm = psscm_get_pspace_arg_unsafe (self, arg_pos, func_name);
206 pspace_smob *p_smob = (pspace_smob *) SCM_SMOB_DATA (p_scm);
211 /* Return non-zero if pspace P_SMOB is valid. */
214 psscm_is_valid (pspace_smob *p_smob)
216 return p_smob->pspace != NULL;
219 /* Return the pspace smob in SELF, verifying it's valid.
220 Throws an exception if SELF is not a <gdb:progspace> object or is
224 psscm_get_valid_pspace_smob_arg_unsafe (SCM self, int arg_pos,
225 const char *func_name)
228 = psscm_get_pspace_smob_arg_unsafe (self, arg_pos, func_name);
230 if (!psscm_is_valid (p_smob))
232 gdbscm_invalid_object_error (func_name, arg_pos, self,
233 _("<gdb:progspace>"));
239 /* Program space methods. */
241 /* (progspace-valid? <gdb:progspace>) -> boolean
242 Returns #t if this program space still exists in GDB. */
245 gdbscm_progspace_valid_p (SCM self)
248 = psscm_get_pspace_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
250 return scm_from_bool (p_smob->pspace != NULL);
253 /* (progspace-filename <gdb:progspace>) -> string
254 Returns the name of the main symfile associated with the progspace,
255 or #f if there isn't one.
256 Throw's an exception if the underlying pspace is invalid. */
259 gdbscm_progspace_filename (SCM self)
262 = psscm_get_valid_pspace_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
263 struct objfile *objfile = p_smob->pspace->symfile_object_file;
266 return gdbscm_scm_from_c_string (objfile_name (objfile));
270 /* (progspace-objfiles <gdb:progspace>) -> list
271 Return the list of objfiles in the progspace.
272 Objfiles that are separate debug objfiles are *not* included in the result,
273 only the "original/real" one appears in the result.
274 The order of appearance of objfiles in the result is arbitrary.
275 Throw's an exception if the underlying pspace is invalid.
277 Some apps can have 1000s of shared libraries. Seriously.
278 A future extension here could be to provide, e.g., a regexp to select
279 just the ones the caller is interested in (rather than building the list
280 and then selecting the desired ones). Another alternative is passing a
281 predicate, then the filter criteria can be more general. */
284 gdbscm_progspace_objfiles (SCM self)
287 = psscm_get_valid_pspace_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
288 struct objfile *objfile;
293 ALL_PSPACE_OBJFILES (p_smob->pspace, objfile)
295 if (objfile->separate_debug_objfile_backlink == NULL)
297 SCM item = ofscm_scm_from_objfile (objfile);
299 result = scm_cons (item, result);
303 /* We don't really have to return the list in the same order as recorded
304 internally, but for consistency we do. We still advertise that one
305 cannot assume anything about the order. */
306 return scm_reverse_x (result, SCM_EOL);
309 /* (progspace-pretty-printers <gdb:progspace>) -> list
310 Returns the list of pretty-printers for this program space. */
313 gdbscm_progspace_pretty_printers (SCM self)
316 = psscm_get_pspace_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
318 return p_smob->pretty_printers;
321 /* (set-progspace-pretty-printers! <gdb:progspace> list) -> unspecified
322 Set the pretty-printers for this program space. */
325 gdbscm_set_progspace_pretty_printers_x (SCM self, SCM printers)
328 = psscm_get_pspace_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
330 SCM_ASSERT_TYPE (gdbscm_is_true (scm_list_p (printers)), printers,
331 SCM_ARG2, FUNC_NAME, _("list"));
333 p_smob->pretty_printers = printers;
335 return SCM_UNSPECIFIED;
338 /* (current-progspace) -> <gdb:progspace>
339 Return the current program space. There always is one. */
342 gdbscm_current_progspace (void)
346 result = psscm_scm_from_pspace (current_program_space);
351 /* (progspaces) -> list
352 Return a list of all progspaces. */
355 gdbscm_progspaces (void)
357 struct program_space *ps;
364 SCM item = psscm_scm_from_pspace (ps);
366 result = scm_cons (item, result);
369 return scm_reverse_x (result, SCM_EOL);
372 /* Initialize the Scheme program space support. */
374 static const scheme_function pspace_functions[] =
376 { "progspace?", 1, 0, 0, as_a_scm_t_subr (gdbscm_progspace_p),
378 Return #t if the object is a <gdb:objfile> object." },
380 { "progspace-valid?", 1, 0, 0, as_a_scm_t_subr (gdbscm_progspace_valid_p),
382 Return #t if the progspace is valid (hasn't been deleted from gdb)." },
384 { "progspace-filename", 1, 0, 0, as_a_scm_t_subr (gdbscm_progspace_filename),
386 Return the name of the main symbol file of the progspace." },
388 { "progspace-objfiles", 1, 0, 0, as_a_scm_t_subr (gdbscm_progspace_objfiles),
390 Return the list of objfiles associated with the progspace.\n\
391 Objfiles that are separate debug objfiles are not included in the result.\n\
392 The order of appearance of objfiles in the result is arbitrary." },
394 { "progspace-pretty-printers", 1, 0, 0,
395 as_a_scm_t_subr (gdbscm_progspace_pretty_printers),
397 Return a list of pretty-printers of the progspace." },
399 { "set-progspace-pretty-printers!", 2, 0, 0,
400 as_a_scm_t_subr (gdbscm_set_progspace_pretty_printers_x),
402 Set the list of pretty-printers of the progspace." },
404 { "current-progspace", 0, 0, 0, as_a_scm_t_subr (gdbscm_current_progspace),
406 Return the current program space if there is one or #f if there isn't one." },
408 { "progspaces", 0, 0, 0, as_a_scm_t_subr (gdbscm_progspaces),
410 Return a list of all program spaces." },
416 gdbscm_initialize_pspaces (void)
419 = gdbscm_make_smob_type (pspace_smob_name, sizeof (pspace_smob));
420 scm_set_smob_print (pspace_smob_tag, psscm_print_pspace_smob);
422 gdbscm_define_functions (pspace_functions, 1);
424 psscm_pspace_data_key
425 = register_program_space_data_with_cleanup (NULL,
426 psscm_handle_pspace_deleted);