Home | History | Annotate | Line # | Download | only in python
py-infevents.c revision 1.8
      1  1.1  christos /* Python interface to inferior function events.
      2  1.1  christos 
      3  1.8  christos    Copyright (C) 2013-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 #include "defs.h"
     21  1.1  christos #include "py-event.h"
     22  1.1  christos 
     23  1.1  christos /* Construct either a gdb.InferiorCallPreEvent or a
     24  1.1  christos    gdb.InferiorCallPostEvent. */
     25  1.1  christos 
     26  1.6  christos static gdbpy_ref<>
     27  1.1  christos create_inferior_call_event_object (inferior_call_kind flag, ptid_t ptid,
     28  1.1  christos 				   CORE_ADDR addr)
     29  1.1  christos {
     30  1.5  christos   gdbpy_ref<> event;
     31  1.1  christos 
     32  1.1  christos   switch (flag)
     33  1.1  christos     {
     34  1.1  christos     case INFERIOR_CALL_PRE:
     35  1.6  christos       event = create_event_object (&inferior_call_pre_event_object_type);
     36  1.1  christos       break;
     37  1.1  christos     case INFERIOR_CALL_POST:
     38  1.6  christos       event = create_event_object (&inferior_call_post_event_object_type);
     39  1.1  christos       break;
     40  1.1  christos     default:
     41  1.5  christos       gdb_assert_not_reached ("invalid inferior_call_kind");
     42  1.1  christos     }
     43  1.1  christos 
     44  1.5  christos   gdbpy_ref<> ptid_obj (gdbpy_create_ptid_object (ptid));
     45  1.1  christos   if (ptid_obj == NULL)
     46  1.5  christos     return NULL;
     47  1.1  christos 
     48  1.5  christos   if (evpy_add_attribute (event.get (), "ptid", ptid_obj.get ()) < 0)
     49  1.5  christos     return NULL;
     50  1.1  christos 
     51  1.7  christos   gdbpy_ref<> addr_obj = gdb_py_object_from_ulongest (addr);
     52  1.1  christos   if (addr_obj == NULL)
     53  1.5  christos     return NULL;
     54  1.5  christos 
     55  1.5  christos   if (evpy_add_attribute (event.get (), "address", addr_obj.get ()) < 0)
     56  1.5  christos     return NULL;
     57  1.1  christos 
     58  1.6  christos   return event;
     59  1.1  christos }
     60  1.1  christos 
     61  1.1  christos /* Construct a gdb.RegisterChangedEvent containing the affected
     62  1.1  christos    register number. */
     63  1.1  christos 
     64  1.6  christos static gdbpy_ref<>
     65  1.8  christos create_register_changed_event_object (frame_info_ptr frame,
     66  1.1  christos 				      int regnum)
     67  1.1  christos {
     68  1.6  christos   gdbpy_ref<> event = create_event_object (&register_changed_event_object_type);
     69  1.1  christos   if (event == NULL)
     70  1.1  christos     return NULL;
     71  1.1  christos 
     72  1.5  christos   gdbpy_ref<> frame_obj (frame_info_to_frame_object (frame));
     73  1.1  christos   if (frame_obj == NULL)
     74  1.5  christos     return NULL;
     75  1.1  christos 
     76  1.5  christos   if (evpy_add_attribute (event.get (), "frame", frame_obj.get ()) < 0)
     77  1.5  christos     return NULL;
     78  1.1  christos 
     79  1.8  christos   gdbpy_ref<> regnum_obj = gdb_py_object_from_longest (regnum);
     80  1.1  christos   if (regnum_obj == NULL)
     81  1.5  christos     return NULL;
     82  1.1  christos 
     83  1.5  christos   if (evpy_add_attribute (event.get (), "regnum", regnum_obj.get ()) < 0)
     84  1.5  christos     return NULL;
     85  1.5  christos 
     86  1.6  christos   return event;
     87  1.1  christos }
     88  1.1  christos 
     89  1.1  christos /* Construct a gdb.MemoryChangedEvent describing the extent of the
     90  1.1  christos    affected memory. */
     91  1.1  christos 
     92  1.6  christos static gdbpy_ref<>
     93  1.1  christos create_memory_changed_event_object (CORE_ADDR addr, ssize_t len)
     94  1.1  christos {
     95  1.6  christos   gdbpy_ref<> event = create_event_object (&memory_changed_event_object_type);
     96  1.1  christos 
     97  1.1  christos   if (event == NULL)
     98  1.1  christos     return NULL;
     99  1.1  christos 
    100  1.7  christos   gdbpy_ref<> addr_obj = gdb_py_object_from_ulongest (addr);
    101  1.1  christos   if (addr_obj == NULL)
    102  1.5  christos     return NULL;
    103  1.1  christos 
    104  1.5  christos   if (evpy_add_attribute (event.get (), "address", addr_obj.get ()) < 0)
    105  1.5  christos     return NULL;
    106  1.1  christos 
    107  1.8  christos   gdbpy_ref<> len_obj = gdb_py_object_from_longest (len);
    108  1.1  christos   if (len_obj == NULL)
    109  1.5  christos     return NULL;
    110  1.5  christos 
    111  1.5  christos   if (evpy_add_attribute (event.get (), "length", len_obj.get ()) < 0)
    112  1.5  christos     return NULL;
    113  1.1  christos 
    114  1.6  christos   return event;
    115  1.1  christos }
    116  1.1  christos 
    117  1.1  christos /* Callback function which notifies observers when an event occurs which
    118  1.1  christos    calls a function in the inferior.
    119  1.1  christos    This function will create a new Python inferior-call event object.
    120  1.1  christos    Return -1 if emit fails.  */
    121  1.1  christos 
    122  1.1  christos int
    123  1.1  christos emit_inferior_call_event (inferior_call_kind flag, ptid_t thread,
    124  1.1  christos 			  CORE_ADDR addr)
    125  1.1  christos {
    126  1.1  christos   if (evregpy_no_listeners_p (gdb_py_events.inferior_call))
    127  1.1  christos     return 0;
    128  1.1  christos 
    129  1.6  christos   gdbpy_ref<> event = create_inferior_call_event_object (flag, thread, addr);
    130  1.1  christos   if (event != NULL)
    131  1.5  christos     return evpy_emit_event (event.get (), gdb_py_events.inferior_call);
    132  1.1  christos   return -1;
    133  1.1  christos }
    134  1.1  christos 
    135  1.1  christos /* Callback when memory is modified by the user.  This function will
    136  1.1  christos    create a new Python memory changed event object. */
    137  1.1  christos 
    138  1.1  christos int
    139  1.1  christos emit_memory_changed_event (CORE_ADDR addr, ssize_t len)
    140  1.1  christos {
    141  1.1  christos   if (evregpy_no_listeners_p (gdb_py_events.memory_changed))
    142  1.1  christos     return 0;
    143  1.1  christos 
    144  1.6  christos   gdbpy_ref<> event = create_memory_changed_event_object (addr, len);
    145  1.1  christos   if (event != NULL)
    146  1.5  christos     return evpy_emit_event (event.get (), gdb_py_events.memory_changed);
    147  1.1  christos   return -1;
    148  1.1  christos }
    149  1.1  christos 
    150  1.1  christos /* Callback when a register is modified by the user.  This function
    151  1.1  christos    will create a new Python register changed event object. */
    152  1.1  christos 
    153  1.1  christos int
    154  1.8  christos emit_register_changed_event (frame_info_ptr frame, int regnum)
    155  1.1  christos {
    156  1.1  christos   if (evregpy_no_listeners_p (gdb_py_events.register_changed))
    157  1.1  christos     return 0;
    158  1.1  christos 
    159  1.6  christos   gdbpy_ref<> event = create_register_changed_event_object (frame, regnum);
    160  1.1  christos   if (event != NULL)
    161  1.5  christos     return evpy_emit_event (event.get (), gdb_py_events.register_changed);
    162  1.1  christos   return -1;
    163  1.1  christos }
    164