227f4a48f1ed25d23feb2014ff790fbfe723704a
[external/binutils.git] / gdb / python / py-newobjfileevent.c
1 /* Python interface to new object file loading events.
2
3    Copyright (C) 2011-2017 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 "defs.h"
21 #include "py-event.h"
22
23 static PyObject *
24 create_new_objfile_event_object (struct objfile *objfile)
25 {
26   PyObject *py_objfile;
27
28   gdbpy_ref<> objfile_event
29     (create_event_object (&new_objfile_event_object_type));
30   if (objfile_event == NULL)
31     return NULL;
32
33   /* Note that objfile_to_objfile_object returns a borrowed reference,
34      so we don't need a decref here.  */
35   py_objfile = objfile_to_objfile_object (objfile);
36   if (!py_objfile || evpy_add_attribute (objfile_event.get (),
37                                          "new_objfile",
38                                          py_objfile) < 0)
39     return NULL;
40
41   return objfile_event.release ();
42 }
43
44 /* Callback function which notifies observers when a new objfile event occurs.
45    This function will create a new Python new_objfile event object.
46    Return -1 if emit fails.  */
47
48 int
49 emit_new_objfile_event (struct objfile *objfile)
50 {
51   if (evregpy_no_listeners_p (gdb_py_events.new_objfile))
52     return 0;
53
54   gdbpy_ref<> event (create_new_objfile_event_object (objfile));
55   if (event != NULL)
56     return evpy_emit_event (event.get (), gdb_py_events.new_objfile);
57   return -1;
58 }
59
60 \f
61 /* Subroutine of emit_clear_objfiles_event to simplify it.  */
62
63 static PyObject *
64 create_clear_objfiles_event_object (void)
65 {
66   PyObject *py_progspace;
67
68   gdbpy_ref<> objfile_event
69     (create_event_object (&clear_objfiles_event_object_type));
70   if (objfile_event == NULL)
71     return NULL;
72
73   /* Note that pspace_to_pspace_object returns a borrowed reference,
74      so we don't need a decref here.  */
75   py_progspace = pspace_to_pspace_object (current_program_space);
76   if (!py_progspace || evpy_add_attribute (objfile_event.get (),
77                                            "progspace",
78                                            py_progspace) < 0)
79     return NULL;
80
81   return objfile_event.release ();
82 }
83
84 /* Callback function which notifies observers when the "clear objfiles"
85    event occurs.
86    This function will create a new Python clear_objfiles event object.
87    Return -1 if emit fails.  */
88
89 int
90 emit_clear_objfiles_event (void)
91 {
92   if (evregpy_no_listeners_p (gdb_py_events.clear_objfiles))
93     return 0;
94
95   gdbpy_ref<> event (create_clear_objfiles_event_object ());
96   if (event != NULL)
97     return evpy_emit_event (event.get (), gdb_py_events.clear_objfiles);
98   return -1;
99 }