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