1 /* Python interface to inferior stop events.
3 Copyright (C) 2009-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "py-stopevent.h"
23 #include "thread-fsm.h"
26 create_stop_event_object (PyTypeObject
*py_type
, const gdbpy_ref
<> &dict
)
28 gdbpy_ref
<> thread
= py_get_event_thread (inferior_ptid
);
29 if (thread
== nullptr)
32 gdbpy_ref
<> result
= create_thread_event_object (py_type
, thread
.get ());
33 if (result
== nullptr)
36 if (evpy_add_attribute (result
.get (), "details", dict
.get ()) < 0)
42 /* Print BPSTAT to a new Python dictionary. Returns the dictionary,
43 or null if a Python exception occurred. */
46 py_print_bpstat (bpstat
*bs
, enum gdb_signal stop_signal
)
49 struct value
*return_value
= nullptr;
53 scoped_restore save_uiout
= make_scoped_restore (¤t_uiout
, &uiout
);
55 thread_info
*tp
= inferior_thread ();
56 if (tp
->thread_fsm () != nullptr && tp
->thread_fsm ()->finished_p ())
58 async_reply_reason reason
= tp
->thread_fsm ()->async_reply_reason ();
59 uiout
.field_string ("reason", async_reason_lookup (reason
));
61 return_value_info
*rvinfo
= tp
->thread_fsm ()->return_value ();
62 if (rvinfo
!= nullptr && rvinfo
->value
!= nullptr)
63 return_value
= rvinfo
->value
;
66 if (stop_signal
!= GDB_SIGNAL_0
&& stop_signal
!= GDB_SIGNAL_TRAP
)
67 print_signal_received_reason (&uiout
, stop_signal
);
70 struct target_waitstatus last
;
71 get_last_target_status (nullptr, nullptr, &last
);
73 bpstat_print (bs
, last
.kind ());
76 catch (const gdb_exception
&except
)
78 gdbpy_convert_exception (except
);
82 gdbpy_ref
<> dict
= uiout
.result ();
86 /* This has to be done separately to avoid error issues, and because
87 there's no API to add generic Python objects to a py_ui_out. */
88 if (return_value
!= nullptr)
90 gdbpy_ref
<> val (value_to_value_object (return_value
));
93 if (PyDict_SetItemString (dict
.get (), "finish-value", val
.get ()) < 0)
100 /* Callback observers when a stop event occurs. This function will create a
101 new Python stop event object. If only a specific thread is stopped the
102 thread object of the event will be set to that thread. Otherwise, if all
103 threads are stopped thread object will be set to None.
104 return 0 if the event was created and emitted successfully otherwise
108 emit_stop_event (struct bpstat
*bs
, enum gdb_signal stop_signal
)
110 gdbpy_ref
<> stop_event_obj
;
112 PyObject
*first_bp
= NULL
;
113 struct bpstat
*current_bs
;
115 if (evregpy_no_listeners_p (gdb_py_events
.stop
))
118 gdbpy_ref
<> dict
= py_print_bpstat (bs
, stop_signal
);
122 /* Add any breakpoint set at this location to the list. */
123 for (current_bs
= bs
; current_bs
!= NULL
; current_bs
= current_bs
->next
)
125 if (current_bs
->breakpoint_at
126 && current_bs
->breakpoint_at
->py_bp_object
)
128 PyObject
*current_py_bp
=
129 (PyObject
*) current_bs
->breakpoint_at
->py_bp_object
;
133 list
.reset (PyList_New (0));
138 if (PyList_Append (list
.get (), current_py_bp
))
141 if (first_bp
== NULL
)
142 first_bp
= current_py_bp
;
148 stop_event_obj
= create_breakpoint_event_object (dict
,
151 if (stop_event_obj
== NULL
)
155 /* Check if the signal is "Signal 0" or "Trace/breakpoint trap". */
156 if (stop_signal
!= GDB_SIGNAL_0
157 && stop_signal
!= GDB_SIGNAL_TRAP
)
159 stop_event_obj
= create_signal_event_object (dict
, stop_signal
);
160 if (stop_event_obj
== NULL
)
164 /* If all fails emit an unknown stop event. All event types should
165 be known and this should eventually be unused. */
166 if (stop_event_obj
== NULL
)
168 stop_event_obj
= create_stop_event_object (&stop_event_object_type
,
170 if (stop_event_obj
== NULL
)
174 return evpy_emit_event (stop_event_obj
.get (), gdb_py_events
.stop
);