]>
Commit | Line | Data |
---|---|---|
c17a9e46 HZ |
1 | /* Python interface to inferior stop events. |
2 | ||
d01e8234 | 3 | Copyright (C) 2009-2025 Free Software Foundation, Inc. |
c17a9e46 HZ |
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-stopevent.h" | |
e187e7c9 | 21 | #include "py-uiout.h" |
99761c5a | 22 | #include "thread-fsm.h" |
c17a9e46 | 23 | |
35c61a1d | 24 | gdbpy_ref<> |
e187e7c9 | 25 | create_stop_event_object (PyTypeObject *py_type, const gdbpy_ref<> &dict) |
c17a9e46 | 26 | { |
db1337cc | 27 | gdbpy_ref<> thread = py_get_event_thread (inferior_ptid); |
e187e7c9 TT |
28 | if (thread == nullptr) |
29 | return nullptr; | |
30 | ||
31 | gdbpy_ref<> result = create_thread_event_object (py_type, thread.get ()); | |
32 | if (result == nullptr) | |
33 | return nullptr; | |
34 | ||
35 | if (evpy_add_attribute (result.get (), "details", dict.get ()) < 0) | |
36 | return nullptr; | |
37 | ||
38 | return result; | |
39 | } | |
40 | ||
41 | /* Print BPSTAT to a new Python dictionary. Returns the dictionary, | |
42 | or null if a Python exception occurred. */ | |
43 | ||
44 | static gdbpy_ref<> | |
45 | py_print_bpstat (bpstat *bs, enum gdb_signal stop_signal) | |
46 | { | |
47 | py_ui_out uiout; | |
99761c5a | 48 | struct value *return_value = nullptr; |
e187e7c9 TT |
49 | |
50 | try | |
51 | { | |
52 | scoped_restore save_uiout = make_scoped_restore (¤t_uiout, &uiout); | |
53 | ||
54 | thread_info *tp = inferior_thread (); | |
55 | if (tp->thread_fsm () != nullptr && tp->thread_fsm ()->finished_p ()) | |
56 | { | |
57 | async_reply_reason reason = tp->thread_fsm ()->async_reply_reason (); | |
58 | uiout.field_string ("reason", async_reason_lookup (reason)); | |
99761c5a TT |
59 | |
60 | return_value_info *rvinfo = tp->thread_fsm ()->return_value (); | |
61 | if (rvinfo != nullptr && rvinfo->value != nullptr) | |
62 | return_value = rvinfo->value; | |
e187e7c9 TT |
63 | } |
64 | ||
65 | if (stop_signal != GDB_SIGNAL_0 && stop_signal != GDB_SIGNAL_TRAP) | |
66 | print_signal_received_reason (&uiout, stop_signal); | |
67 | else | |
68 | { | |
69 | struct target_waitstatus last; | |
70 | get_last_target_status (nullptr, nullptr, &last); | |
71 | ||
72 | bpstat_print (bs, last.kind ()); | |
73 | } | |
74 | } | |
75 | catch (const gdb_exception &except) | |
76 | { | |
1ccb6f10 | 77 | return gdbpy_handle_gdb_exception (nullptr, except); |
e187e7c9 TT |
78 | } |
79 | ||
99761c5a TT |
80 | gdbpy_ref<> dict = uiout.result (); |
81 | if (dict == nullptr) | |
82 | return nullptr; | |
83 | ||
84 | /* This has to be done separately to avoid error issues, and because | |
85 | there's no API to add generic Python objects to a py_ui_out. */ | |
86 | if (return_value != nullptr) | |
87 | { | |
88 | gdbpy_ref<> val (value_to_value_object (return_value)); | |
89 | if (val == nullptr) | |
90 | return nullptr; | |
91 | if (PyDict_SetItemString (dict.get (), "finish-value", val.get ()) < 0) | |
92 | return nullptr; | |
93 | } | |
94 | ||
95 | return dict; | |
c17a9e46 HZ |
96 | } |
97 | ||
98 | /* Callback observers when a stop event occurs. This function will create a | |
99 | new Python stop event object. If only a specific thread is stopped the | |
100 | thread object of the event will be set to that thread. Otherwise, if all | |
101 | threads are stopped thread object will be set to None. | |
102 | return 0 if the event was created and emitted successfully otherwise | |
103 | returns -1. */ | |
104 | ||
105 | int | |
313f3b21 | 106 | emit_stop_event (struct bpstat *bs, enum gdb_signal stop_signal) |
c17a9e46 | 107 | { |
7780f186 TT |
108 | gdbpy_ref<> stop_event_obj; |
109 | gdbpy_ref<> list; | |
6839b47f | 110 | PyObject *first_bp = NULL; |
313f3b21 | 111 | struct bpstat *current_bs; |
c17a9e46 HZ |
112 | |
113 | if (evregpy_no_listeners_p (gdb_py_events.stop)) | |
114 | return 0; | |
115 | ||
e187e7c9 TT |
116 | gdbpy_ref<> dict = py_print_bpstat (bs, stop_signal); |
117 | if (dict == nullptr) | |
118 | return -1; | |
119 | ||
6839b47f KP |
120 | /* Add any breakpoint set at this location to the list. */ |
121 | for (current_bs = bs; current_bs != NULL; current_bs = current_bs->next) | |
c17a9e46 | 122 | { |
6839b47f | 123 | if (current_bs->breakpoint_at |
dda83cd7 SM |
124 | && current_bs->breakpoint_at->py_bp_object) |
125 | { | |
126 | PyObject *current_py_bp = | |
127 | (PyObject *) current_bs->breakpoint_at->py_bp_object; | |
128 | ||
129 | if (list == NULL) | |
130 | { | |
131 | list.reset (PyList_New (0)); | |
132 | if (list == NULL) | |
abf5651e | 133 | return -1; |
dda83cd7 | 134 | } |
6839b47f | 135 | |
dda83cd7 | 136 | if (PyList_Append (list.get (), current_py_bp)) |
abf5651e | 137 | return -1; |
6839b47f | 138 | |
dda83cd7 SM |
139 | if (first_bp == NULL) |
140 | first_bp = current_py_bp; | |
141 | } | |
6839b47f KP |
142 | } |
143 | ||
144 | if (list != NULL) | |
145 | { | |
e187e7c9 TT |
146 | stop_event_obj = create_breakpoint_event_object (dict, |
147 | list.get (), | |
35c61a1d | 148 | first_bp); |
abf5651e TT |
149 | if (stop_event_obj == NULL) |
150 | return -1; | |
c17a9e46 HZ |
151 | } |
152 | ||
153 | /* Check if the signal is "Signal 0" or "Trace/breakpoint trap". */ | |
a493e3e2 PA |
154 | if (stop_signal != GDB_SIGNAL_0 |
155 | && stop_signal != GDB_SIGNAL_TRAP) | |
c17a9e46 | 156 | { |
e187e7c9 | 157 | stop_event_obj = create_signal_event_object (dict, stop_signal); |
abf5651e TT |
158 | if (stop_event_obj == NULL) |
159 | return -1; | |
c17a9e46 HZ |
160 | } |
161 | ||
162 | /* If all fails emit an unknown stop event. All event types should | |
163 | be known and this should eventually be unused. */ | |
abf5651e | 164 | if (stop_event_obj == NULL) |
c17a9e46 | 165 | { |
e187e7c9 TT |
166 | stop_event_obj = create_stop_event_object (&stop_event_object_type, |
167 | dict); | |
abf5651e TT |
168 | if (stop_event_obj == NULL) |
169 | return -1; | |
c17a9e46 HZ |
170 | } |
171 | ||
abf5651e | 172 | return evpy_emit_event (stop_event_obj.get (), gdb_py_events.stop); |
c17a9e46 | 173 | } |