xref: /netbsd-src/external/gpl3/gdb/dist/gdb/python/py-exitedevent.c (revision 7b75fb82a29e85f63942bcd36fb838f6ffe46195)
1 /* Python interface to inferior exit events.
2 
3    Copyright (C) 2009-2024 Free Software Foundation, Inc.
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-event.h"
21 
22 static gdbpy_ref<>
23 create_exited_event_object (const LONGEST *exit_code, struct inferior *inf)
24 {
25   gdbpy_ref<> exited_event = create_event_object (&exited_event_object_type);
26 
27   if (exited_event == NULL)
28     return NULL;
29 
30   if (exit_code)
31     {
32       gdbpy_ref<> exit_code_obj = gdb_py_object_from_longest (*exit_code);
33 
34       if (exit_code_obj == NULL)
35 	return NULL;
36       if (evpy_add_attribute (exited_event.get (), "exit_code",
37 			      exit_code_obj.get ()) < 0)
38 	return NULL;
39     }
40 
41   gdbpy_ref<inferior_object> inf_obj = inferior_to_inferior_object (inf);
42   if (inf_obj == NULL || evpy_add_attribute (exited_event.get (),
43 					     "inferior",
44 					     (PyObject *) inf_obj.get ()) < 0)
45     return NULL;
46 
47   return exited_event;
48 }
49 
50 /* Callback that is used when an exit event occurs.  This function
51    will create a new Python exited event object.  */
52 
53 int
54 emit_exited_event (const LONGEST *exit_code, struct inferior *inf)
55 {
56   if (evregpy_no_listeners_p (gdb_py_events.exited))
57     return 0;
58 
59   gdbpy_ref<> event = create_exited_event_object (exit_code, inf);
60 
61   if (event != NULL)
62     return evpy_emit_event (event.get (), gdb_py_events.exited);
63 
64   return -1;
65 }
66