Regenerate AArch64 opcodes files
[binutils-gdb.git] / gdb / python / py-stopevent.c
blobfcaebe26f13c0cdd8f706035dc56f8f68878f334
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/>. */
20 #include "defs.h"
21 #include "py-stopevent.h"
22 #include "py-uiout.h"
23 #include "thread-fsm.h"
25 gdbpy_ref<>
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)
30 return nullptr;
32 gdbpy_ref<> result = create_thread_event_object (py_type, thread.get ());
33 if (result == nullptr)
34 return nullptr;
36 if (evpy_add_attribute (result.get (), "details", dict.get ()) < 0)
37 return nullptr;
39 return result;
42 /* Print BPSTAT to a new Python dictionary. Returns the dictionary,
43 or null if a Python exception occurred. */
45 static gdbpy_ref<>
46 py_print_bpstat (bpstat *bs, enum gdb_signal stop_signal)
48 py_ui_out uiout;
49 struct value *return_value = nullptr;
51 try
53 scoped_restore save_uiout = make_scoped_restore (&current_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);
68 else
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);
79 return nullptr;
82 gdbpy_ref<> dict = uiout.result ();
83 if (dict == nullptr)
84 return nullptr;
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));
91 if (val == nullptr)
92 return nullptr;
93 if (PyDict_SetItemString (dict.get (), "finish-value", val.get ()) < 0)
94 return nullptr;
97 return dict;
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
105 returns -1. */
108 emit_stop_event (struct bpstat *bs, enum gdb_signal stop_signal)
110 gdbpy_ref<> stop_event_obj;
111 gdbpy_ref<> list;
112 PyObject *first_bp = NULL;
113 struct bpstat *current_bs;
115 if (evregpy_no_listeners_p (gdb_py_events.stop))
116 return 0;
118 gdbpy_ref<> dict = py_print_bpstat (bs, stop_signal);
119 if (dict == nullptr)
120 return -1;
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;
131 if (list == NULL)
133 list.reset (PyList_New (0));
134 if (list == NULL)
135 return -1;
138 if (PyList_Append (list.get (), current_py_bp))
139 return -1;
141 if (first_bp == NULL)
142 first_bp = current_py_bp;
146 if (list != NULL)
148 stop_event_obj = create_breakpoint_event_object (dict,
149 list.get (),
150 first_bp);
151 if (stop_event_obj == NULL)
152 return -1;
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)
161 return -1;
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,
169 dict);
170 if (stop_event_obj == NULL)
171 return -1;
174 return evpy_emit_event (stop_event_obj.get (), gdb_py_events.stop);