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