57179478bc1a85551c507eafc5e2f14f538f210e
[external/binutils.git] / gdb / python / py-stopevent.c
1 /* Python interface to inferior stop events.
2
3    Copyright (C) 2009-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-stopevent.h"
22
23 gdbpy_ref<>
24 create_stop_event_object (PyTypeObject *py_type)
25 {
26   return create_thread_event_object (py_type);
27 }
28
29 /* Callback observers when a stop event occurs.  This function will create a
30    new Python stop event object.  If only a specific thread is stopped the
31    thread object of the event will be set to that thread.  Otherwise, if all
32    threads are stopped thread object will be set to None.
33    return 0 if the event was created and emitted successfully otherwise
34    returns -1.  */
35
36 int
37 emit_stop_event (struct bpstats *bs, enum gdb_signal stop_signal)
38 {
39   gdbpy_ref<> stop_event_obj;
40   gdbpy_ref<> list;
41   PyObject *first_bp = NULL;
42   struct bpstats *current_bs;
43
44   if (evregpy_no_listeners_p (gdb_py_events.stop))
45     return 0;
46
47   /* Add any breakpoint set at this location to the list.  */
48   for (current_bs = bs; current_bs != NULL; current_bs = current_bs->next)
49     {
50       if (current_bs->breakpoint_at
51           && current_bs->breakpoint_at->py_bp_object)
52         {
53           PyObject *current_py_bp =
54               (PyObject *) current_bs->breakpoint_at->py_bp_object;
55
56           if (list == NULL)
57             {
58               list.reset (PyList_New (0));
59               if (list == NULL)
60                 return -1;
61             }
62
63           if (PyList_Append (list.get (), current_py_bp))
64             return -1;
65
66           if (first_bp == NULL)
67             first_bp = current_py_bp;
68         }
69     }
70
71   if (list != NULL)
72     {
73       stop_event_obj = create_breakpoint_event_object (list.get (),
74                                                        first_bp);
75       if (stop_event_obj == NULL)
76         return -1;
77     }
78
79   /* Check if the signal is "Signal 0" or "Trace/breakpoint trap".  */
80   if (stop_signal != GDB_SIGNAL_0
81       && stop_signal != GDB_SIGNAL_TRAP)
82     {
83       stop_event_obj = create_signal_event_object (stop_signal);
84       if (stop_event_obj == NULL)
85         return -1;
86     }
87
88   /* If all fails emit an unknown stop event.  All event types should
89      be known and this should eventually be unused.  */
90   if (stop_event_obj == NULL)
91     {
92       stop_event_obj = create_stop_event_object (&stop_event_object_type);
93       if (stop_event_obj == NULL)
94         return -1;
95     }
96
97   return evpy_emit_event (stop_event_obj.get (), gdb_py_events.stop);
98 }