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