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