Home | History | Annotate | Line # | Download | only in python
py-newobjfileevent.c revision 1.1.1.9
      1 /* Python interface to new object file loading events.
      2 
      3    Copyright (C) 2011-2024 Free Software Foundation, Inc.
      4 
      5    This file is part of GDB.
      6 
      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.
     11 
     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.
     16 
     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/>.  */
     19 
     20 #include "py-event.h"
     21 
     22 static gdbpy_ref<>
     23 create_new_objfile_event_object (struct objfile *objfile)
     24 {
     25   gdbpy_ref<> objfile_event
     26     = create_event_object (&new_objfile_event_object_type);
     27   if (objfile_event == NULL)
     28     return NULL;
     29 
     30   gdbpy_ref<> py_objfile = objfile_to_objfile_object (objfile);
     31   if (py_objfile == NULL || evpy_add_attribute (objfile_event.get (),
     32 						"new_objfile",
     33 						py_objfile.get ()) < 0)
     34     return NULL;
     35 
     36   return objfile_event;
     37 }
     38 
     39 /* Callback function which notifies observers when a new objfile event occurs.
     40    This function will create a new Python new_objfile event object.
     41    Return -1 if emit fails.  */
     42 
     43 int
     44 emit_new_objfile_event (struct objfile *objfile)
     45 {
     46   if (evregpy_no_listeners_p (gdb_py_events.new_objfile))
     47     return 0;
     48 
     49   gdbpy_ref<> event = create_new_objfile_event_object (objfile);
     50   if (event != NULL)
     51     return evpy_emit_event (event.get (), gdb_py_events.new_objfile);
     52   return -1;
     53 }
     54 
     55 /* Create an event object representing a to-be-freed objfile.  Return
     56    nullptr, with the Python exception set, on error.  */
     57 
     58 static gdbpy_ref<>
     59 create_free_objfile_event_object (struct objfile *objfile)
     60 {
     61   gdbpy_ref<> objfile_event
     62     = create_event_object (&free_objfile_event_object_type);
     63   if (objfile_event == nullptr)
     64     return nullptr;
     65 
     66   gdbpy_ref<> py_objfile = objfile_to_objfile_object (objfile);
     67   if (py_objfile == nullptr
     68       || evpy_add_attribute (objfile_event.get (), "objfile",
     69 			     py_objfile.get ()) < 0)
     70     return nullptr;
     71 
     72   return objfile_event;
     73 }
     74 
     75 /* Callback function which notifies observers when a free objfile
     76    event occurs.  This function will create a new Python event object.
     77    Return -1 if emit fails.  */
     78 
     79 int
     80 emit_free_objfile_event (struct objfile *objfile)
     81 {
     82   if (evregpy_no_listeners_p (gdb_py_events.free_objfile))
     83     return 0;
     84 
     85   gdbpy_ref<> event = create_free_objfile_event_object (objfile);
     86   if (event == nullptr)
     87     return -1;
     88   return evpy_emit_event (event.get (), gdb_py_events.free_objfile);
     89 }
     90 
     91 
     92 /* Subroutine of emit_clear_objfiles_event to simplify it.  */
     94 
     95 static gdbpy_ref<>
     96 create_clear_objfiles_event_object (program_space *pspace)
     97 {
     98   gdbpy_ref<> objfile_event
     99     = create_event_object (&clear_objfiles_event_object_type);
    100   if (objfile_event == NULL)
    101     return NULL;
    102 
    103   gdbpy_ref<> py_progspace = pspace_to_pspace_object (pspace);
    104   if (py_progspace == NULL || evpy_add_attribute (objfile_event.get (),
    105 						  "progspace",
    106 						  py_progspace.get ()) < 0)
    107     return NULL;
    108 
    109   return objfile_event;
    110 }
    111 
    112 /* Callback function which notifies observers when the "clear objfiles"
    113    event occurs.
    114    This function will create a new Python clear_objfiles event object.
    115    Return -1 if emit fails.  */
    116 
    117 int
    118 emit_clear_objfiles_event (program_space *pspace)
    119 {
    120   if (evregpy_no_listeners_p (gdb_py_events.clear_objfiles))
    121     return 0;
    122 
    123   gdbpy_ref<> event = create_clear_objfiles_event_object (pspace);
    124   if (event != NULL)
    125     return evpy_emit_event (event.get (), gdb_py_events.clear_objfiles);
    126   return -1;
    127 }
    128