+2017-01-10 Tom Tromey <tom@tromey.com>
+
+ * python/py-threadevent.c (create_thread_event_object): Use
+ gdbpy_ref.
+ * python/py-stopevent.c (create_stop_event_object): Simplify.
+ (emit_stop_event): Use gdbpy_ref.
+ * python/py-signalevent.c (create_signal_event_object): Use
+ gdbpy_ref.
+ * python/py-newobjfileevent.c (create_new_objfile_event_object)
+ (emit_new_objfile_event, create_clear_objfiles_event_object)
+ (emit_clear_objfiles_event): Use gdbpy_ref.
+ * python/py-infevents.c (create_inferior_call_event_object)
+ (create_register_changed_event_object)
+ (create_memory_changed_event_object, emit_inferior_call_event)
+ (emit_memory_changed_event, emit_register_changed_event): Use
+ gdbpy_ref.
+ * python/py-exitedevent.c (create_exited_event_object)
+ (emit_exited_event): Use gdbpy_ref.
+ * python/py-event.h (evpy_emit_event): Remove
+ CPYCHECKER_STEALS_REFERENCE_TO_ARG annotation.
+ * python/py-event.c (evpy_emit_event): Use gdbpy_ref.
+ * python/py-continueevent.c (emit_continue_event): Use
+ gdbpy_ref.
+ * python/py-breakpoint.c (gdbpy_breakpoint_created)
+ (gdbpy_breakpoint_deleted, gdbpy_breakpoint_modified): Use
+ gdbpy_ref.
+ * python/py-bpevent.c (create_breakpoint_event_object): Use
+ gdbpy_ref.
+
2017-01-10 Tom Tromey <tom@tromey.com>
* python/py-ref.h: New file.
#include "defs.h"
#include "py-stopevent.h"
+#include "py-ref.h"
extern PyTypeObject breakpoint_event_object_type
CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("event_object");
PyObject *
create_breakpoint_event_object (PyObject *breakpoint_list, PyObject *first_bp)
{
- PyObject *breakpoint_event_obj =
- create_stop_event_object (&breakpoint_event_object_type);
+ gdbpy_ref breakpoint_event_obj
+ (create_stop_event_object (&breakpoint_event_object_type));
- if (!breakpoint_event_obj)
- goto fail;
+ if (breakpoint_event_obj == NULL)
+ return NULL;
- if (evpy_add_attribute (breakpoint_event_obj,
+ if (evpy_add_attribute (breakpoint_event_obj.get (),
"breakpoint",
first_bp) < 0)
- goto fail;
- if (evpy_add_attribute (breakpoint_event_obj,
+ return NULL;
+ if (evpy_add_attribute (breakpoint_event_obj.get (),
"breakpoints",
breakpoint_list) < 0)
- goto fail;
+ return NULL;
- return breakpoint_event_obj;
-
- fail:
- Py_XDECREF (breakpoint_event_obj);
- return NULL;
+ return breakpoint_event_obj.release ();
}
GDBPY_NEW_EVENT_TYPE (breakpoint,
if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_created))
{
- Py_INCREF (newbp);
if (evpy_emit_event ((PyObject *) newbp,
gdb_py_events.breakpoint_created) < 0)
gdbpy_print_stack ();
{
if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_deleted))
{
- PyObject *bp_obj_alias = (PyObject *) bp_obj;
-
- Py_INCREF (bp_obj_alias);
- if (evpy_emit_event (bp_obj_alias,
+ if (evpy_emit_event ((PyObject *) bp_obj,
gdb_py_events.breakpoint_deleted) < 0)
gdbpy_print_stack ();
}
{
if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_modified))
{
- Py_INCREF (bp_obj);
if (evpy_emit_event (bp_obj,
gdb_py_events.breakpoint_modified) < 0)
gdbpy_print_stack ();
#include "defs.h"
#include "py-event.h"
+#include "py-ref.h"
extern PyTypeObject continue_event_object_type
CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("event_object");
int
emit_continue_event (ptid_t ptid)
{
- PyObject *event;
-
if (evregpy_no_listeners_p (gdb_py_events.cont))
return 0;
- event = create_continue_event_object ();
- if (event)
- return evpy_emit_event (event, gdb_py_events.cont);
+ gdbpy_ref event (create_continue_event_object ());
+ if (event != NULL)
+ return evpy_emit_event (event.get (), gdb_py_events.cont);
return -1;
}
evpy_emit_event (PyObject *event,
eventregistry_object *registry)
{
- PyObject *callback_list_copy = NULL;
Py_ssize_t i;
/* Create a copy of call back list and use that for
notifying listeners to avoid skipping callbacks
in the case of a callback being disconnected during
a notification. */
- callback_list_copy = PySequence_List (registry->callbacks);
- if (!callback_list_copy)
- goto fail;
+ gdbpy_ref callback_list_copy (PySequence_List (registry->callbacks));
+ if (callback_list_copy == NULL)
+ return -1;
- for (i = 0; i < PyList_Size (callback_list_copy); i++)
+ for (i = 0; i < PyList_Size (callback_list_copy.get ()); i++)
{
- PyObject *func = PyList_GetItem (callback_list_copy, i);
- PyObject *func_result;
+ PyObject *func = PyList_GetItem (callback_list_copy.get (), i);
if (func == NULL)
- goto fail;
+ return -1;
- func_result = PyObject_CallFunctionObjArgs (func, event, NULL);
+ gdbpy_ref func_result (PyObject_CallFunctionObjArgs (func, event, NULL));
if (func_result == NULL)
{
call all of the callbacks even if one is broken. */
gdbpy_print_stack ();
}
- else
- {
- Py_DECREF (func_result);
- }
}
- Py_XDECREF (callback_list_copy);
- Py_XDECREF (event);
return 0;
-
- fail:
- gdbpy_print_stack ();
- Py_XDECREF (callback_list_copy);
- Py_XDECREF (event);
- return -1;
}
static PyGetSetDef event_object_getset[] =
#include "command.h"
#include "python-internal.h"
#include "inferior.h"
+#include "py-ref.h"
/* This macro creates the following functions:
int regnum);
extern int emit_memory_changed_event (CORE_ADDR addr, ssize_t len);
extern int evpy_emit_event (PyObject *event,
- eventregistry_object *registry)
- CPYCHECKER_STEALS_REFERENCE_TO_ARG (1);
+ eventregistry_object *registry);
extern PyObject *create_event_object (PyTypeObject *py_type);
extern PyObject *create_thread_event_object (PyTypeObject *py_type);
static PyObject *
create_exited_event_object (const LONGEST *exit_code, struct inferior *inf)
{
- PyObject *exited_event;
- PyObject *inf_obj = NULL;
+ gdbpy_ref exited_event (create_event_object (&exited_event_object_type));
- exited_event = create_event_object (&exited_event_object_type);
-
- if (!exited_event)
- goto fail;
+ if (exited_event == NULL)
+ return NULL;
if (exit_code)
{
- PyObject *exit_code_obj = PyLong_FromLongLong (*exit_code);
- int failed;
+ gdbpy_ref exit_code_obj (PyLong_FromLongLong (*exit_code));
if (exit_code_obj == NULL)
- goto fail;
-
- failed = evpy_add_attribute (exited_event, "exit_code",
- exit_code_obj) < 0;
- Py_DECREF (exit_code_obj);
- if (failed)
- goto fail;
+ return NULL;
+ if (evpy_add_attribute (exited_event.get (), "exit_code",
+ exit_code_obj.get ()) < 0)
+ return NULL;
}
- inf_obj = inferior_to_inferior_object (inf);
- if (!inf_obj || evpy_add_attribute (exited_event,
- "inferior",
- inf_obj) < 0)
- goto fail;
- Py_DECREF (inf_obj);
+ gdbpy_ref inf_obj (inferior_to_inferior_object (inf));
+ if (inf_obj == NULL || evpy_add_attribute (exited_event.get (),
+ "inferior",
+ inf_obj.get ()) < 0)
+ return NULL;
- return exited_event;
-
- fail:
- Py_XDECREF (inf_obj);
- Py_XDECREF (exited_event);
- return NULL;
+ return exited_event.release ();
}
/* Callback that is used when an exit event occurs. This function
int
emit_exited_event (const LONGEST *exit_code, struct inferior *inf)
{
- PyObject *event;
-
if (evregpy_no_listeners_p (gdb_py_events.exited))
return 0;
- event = create_exited_event_object (exit_code, inf);
+ gdbpy_ref event (create_exited_event_object (exit_code, inf));
- if (event)
- return evpy_emit_event (event, gdb_py_events.exited);
+ if (event != NULL)
+ return evpy_emit_event (event.get (), gdb_py_events.exited);
return -1;
}
#include "defs.h"
#include "py-event.h"
+#include "py-ref.h"
extern PyTypeObject inferior_call_pre_event_object_type
CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("event_object");
create_inferior_call_event_object (inferior_call_kind flag, ptid_t ptid,
CORE_ADDR addr)
{
- PyObject *event;
- PyObject *ptid_obj = NULL;
- PyObject *addr_obj = NULL;
+ gdbpy_ref event;
int failed;
- struct cleanup *cleanups;
- struct cleanup *member_cleanups;
switch (flag)
{
case INFERIOR_CALL_PRE:
- event = create_event_object (&inferior_call_pre_event_object_type);
+ event.reset (create_event_object (&inferior_call_pre_event_object_type));
break;
case INFERIOR_CALL_POST:
- event = create_event_object (&inferior_call_post_event_object_type);
+ event.reset (create_event_object (&inferior_call_post_event_object_type));
break;
default:
- return NULL;
+ gdb_assert_not_reached ("invalid inferior_call_kind");
}
- cleanups = make_cleanup_py_decref (event);
-
- ptid_obj = gdbpy_create_ptid_object (ptid);
+ gdbpy_ref ptid_obj (gdbpy_create_ptid_object (ptid));
if (ptid_obj == NULL)
- goto fail;
- member_cleanups = make_cleanup_py_decref (ptid_obj);
+ return NULL;
- failed = evpy_add_attribute (event, "ptid", ptid_obj) < 0;
- if (failed)
- goto fail;
+ if (evpy_add_attribute (event.get (), "ptid", ptid_obj.get ()) < 0)
+ return NULL;
- addr_obj = PyLong_FromLongLong (addr);
+ gdbpy_ref addr_obj (PyLong_FromLongLong (addr));
if (addr_obj == NULL)
- goto fail;
- make_cleanup_py_decref (addr_obj);
-
- failed = evpy_add_attribute (event, "address", addr_obj) < 0;
- if (failed)
- goto fail;
+ return NULL;
- do_cleanups (member_cleanups);
- discard_cleanups (cleanups);
- return event;
+ if (evpy_add_attribute (event.get (), "address", addr_obj.get ()) < 0)
+ return NULL;
- fail:
- do_cleanups (cleanups);
- return NULL;
+ return event.release ();
}
/* Construct a gdb.RegisterChangedEvent containing the affected
create_register_changed_event_object (struct frame_info *frame,
int regnum)
{
- PyObject *event;
- PyObject *frame_obj = NULL;
- PyObject *regnum_obj = NULL;
- int failed;
- struct cleanup *cleanups;
- struct cleanup *member_cleanups;
-
- event = create_event_object (®ister_changed_event_object_type);
+ gdbpy_ref event (create_event_object (®ister_changed_event_object_type));
if (event == NULL)
return NULL;
- cleanups = make_cleanup_py_decref (event);
-
- frame_obj = frame_info_to_frame_object (frame);
+ gdbpy_ref frame_obj (frame_info_to_frame_object (frame));
if (frame_obj == NULL)
- goto fail;
- member_cleanups = make_cleanup_py_decref (frame_obj);
+ return NULL;
- failed = evpy_add_attribute (event, "frame", frame_obj) < 0;
- if (failed)
- goto fail;
+ if (evpy_add_attribute (event.get (), "frame", frame_obj.get ()) < 0)
+ return NULL;
- regnum_obj = PyLong_FromLongLong (regnum);
+ gdbpy_ref regnum_obj (PyLong_FromLongLong (regnum));
if (regnum_obj == NULL)
- goto fail;
- make_cleanup_py_decref (regnum_obj);
-
- failed = evpy_add_attribute (event, "regnum", regnum_obj) < 0;
- if (failed)
- goto fail;
+ return NULL;
- do_cleanups (member_cleanups);
- discard_cleanups (cleanups);
- return event;
+ if (evpy_add_attribute (event.get (), "regnum", regnum_obj.get ()) < 0)
+ return NULL;
- fail:
- do_cleanups (cleanups);
- return NULL;
+ return event.release ();
}
/* Construct a gdb.MemoryChangedEvent describing the extent of the
static PyObject *
create_memory_changed_event_object (CORE_ADDR addr, ssize_t len)
{
- PyObject *event;
- PyObject *addr_obj = NULL;
- PyObject *len_obj = NULL;
- int failed;
- struct cleanup *cleanups;
- struct cleanup *member_cleanups;
-
- event = create_event_object (&memory_changed_event_object_type);
+ gdbpy_ref event (create_event_object (&memory_changed_event_object_type));
if (event == NULL)
return NULL;
- cleanups = make_cleanup_py_decref (event);
- addr_obj = PyLong_FromLongLong (addr);
+ gdbpy_ref addr_obj (PyLong_FromLongLong (addr));
if (addr_obj == NULL)
- goto fail;
- member_cleanups = make_cleanup_py_decref (addr_obj);
+ return NULL;
- failed = evpy_add_attribute (event, "address", addr_obj) < 0;
- if (failed)
- goto fail;
+ if (evpy_add_attribute (event.get (), "address", addr_obj.get ()) < 0)
+ return NULL;
- len_obj = PyLong_FromLong (len);
+ gdbpy_ref len_obj (PyLong_FromLong (len));
if (len_obj == NULL)
- goto fail;
- make_cleanup_py_decref (len_obj);
-
- failed = evpy_add_attribute (event, "length", len_obj) < 0;
- if (failed)
- goto fail;
+ return NULL;
- do_cleanups (member_cleanups);
- discard_cleanups (cleanups);
- return event;
+ if (evpy_add_attribute (event.get (), "length", len_obj.get ()) < 0)
+ return NULL;
- fail:
- do_cleanups (cleanups);
- return NULL;
+ return event.release ();
}
/* Callback function which notifies observers when an event occurs which
emit_inferior_call_event (inferior_call_kind flag, ptid_t thread,
CORE_ADDR addr)
{
- PyObject *event;
-
if (evregpy_no_listeners_p (gdb_py_events.inferior_call))
return 0;
- event = create_inferior_call_event_object (flag, thread, addr);
+ gdbpy_ref event (create_inferior_call_event_object (flag, thread, addr));
if (event != NULL)
- return evpy_emit_event (event, gdb_py_events.inferior_call);
+ return evpy_emit_event (event.get (), gdb_py_events.inferior_call);
return -1;
}
int
emit_memory_changed_event (CORE_ADDR addr, ssize_t len)
{
- PyObject *event;
-
if (evregpy_no_listeners_p (gdb_py_events.memory_changed))
return 0;
- event = create_memory_changed_event_object (addr, len);
+ gdbpy_ref event (create_memory_changed_event_object (addr, len));
if (event != NULL)
- return evpy_emit_event (event, gdb_py_events.memory_changed);
+ return evpy_emit_event (event.get (), gdb_py_events.memory_changed);
return -1;
}
int
emit_register_changed_event (struct frame_info* frame, int regnum)
{
- PyObject *event;
-
if (evregpy_no_listeners_p (gdb_py_events.register_changed))
return 0;
- event = create_register_changed_event_object (frame, regnum);
+ gdbpy_ref event (create_register_changed_event_object (frame, regnum));
if (event != NULL)
- return evpy_emit_event (event, gdb_py_events.register_changed);
+ return evpy_emit_event (event.get (), gdb_py_events.register_changed);
return -1;
}
static PyObject *
create_new_objfile_event_object (struct objfile *objfile)
{
- PyObject *objfile_event;
PyObject *py_objfile;
- objfile_event = create_event_object (&new_objfile_event_object_type);
- if (!objfile_event)
- goto fail;
+ gdbpy_ref objfile_event
+ (create_event_object (&new_objfile_event_object_type));
+ if (objfile_event == NULL)
+ return NULL;
/* Note that objfile_to_objfile_object returns a borrowed reference,
so we don't need a decref here. */
py_objfile = objfile_to_objfile_object (objfile);
- if (!py_objfile || evpy_add_attribute (objfile_event,
+ if (!py_objfile || evpy_add_attribute (objfile_event.get (),
"new_objfile",
py_objfile) < 0)
- goto fail;
+ return NULL;
- return objfile_event;
-
- fail:
- Py_XDECREF (objfile_event);
- return NULL;
+ return objfile_event.release ();
}
/* Callback function which notifies observers when a new objfile event occurs.
int
emit_new_objfile_event (struct objfile *objfile)
{
- PyObject *event;
-
if (evregpy_no_listeners_p (gdb_py_events.new_objfile))
return 0;
- event = create_new_objfile_event_object (objfile);
- if (event)
- return evpy_emit_event (event, gdb_py_events.new_objfile);
+ gdbpy_ref event (create_new_objfile_event_object (objfile));
+ if (event != NULL)
+ return evpy_emit_event (event.get (), gdb_py_events.new_objfile);
return -1;
}
static PyObject *
create_clear_objfiles_event_object (void)
{
- PyObject *objfile_event;
PyObject *py_progspace;
- objfile_event = create_event_object (&clear_objfiles_event_object_type);
- if (!objfile_event)
- goto fail;
+ gdbpy_ref objfile_event
+ (create_event_object (&clear_objfiles_event_object_type));
+ if (objfile_event == NULL)
+ return NULL;
/* Note that pspace_to_pspace_object returns a borrowed reference,
so we don't need a decref here. */
py_progspace = pspace_to_pspace_object (current_program_space);
- if (!py_progspace || evpy_add_attribute (objfile_event,
+ if (!py_progspace || evpy_add_attribute (objfile_event.get (),
"progspace",
py_progspace) < 0)
- goto fail;
+ return NULL;
- return objfile_event;
-
- fail:
- Py_XDECREF (objfile_event);
- return NULL;
+ return objfile_event.release ();
}
/* Callback function which notifies observers when the "clear objfiles"
int
emit_clear_objfiles_event (void)
{
- PyObject *event;
-
if (evregpy_no_listeners_p (gdb_py_events.clear_objfiles))
return 0;
- event = create_clear_objfiles_event_object ();
- if (event)
- return evpy_emit_event (event, gdb_py_events.clear_objfiles);
+ gdbpy_ref event (create_clear_objfiles_event_object ());
+ if (event != NULL)
+ return evpy_emit_event (event.get (), gdb_py_events.clear_objfiles);
return -1;
}
create_signal_event_object (enum gdb_signal stop_signal)
{
const char *signal_name;
- PyObject *signal_name_obj = NULL;
- PyObject *signal_event_obj =
- create_stop_event_object (&signal_event_object_type);
+ gdbpy_ref signal_event_obj
+ (create_stop_event_object (&signal_event_object_type));
- if (!signal_event_obj)
- goto fail;
+ if (signal_event_obj == NULL)
+ return NULL;
signal_name = gdb_signal_to_name (stop_signal);
- signal_name_obj = PyString_FromString (signal_name);
+ gdbpy_ref signal_name_obj (PyString_FromString (signal_name));
if (signal_name_obj == NULL)
- goto fail;
- if (evpy_add_attribute (signal_event_obj,
+ return NULL;
+ if (evpy_add_attribute (signal_event_obj.get (),
"stop_signal",
- signal_name_obj) < 0)
- goto fail;
- Py_DECREF (signal_name_obj);
+ signal_name_obj.get ()) < 0)
+ return NULL;
- return signal_event_obj;
-
- fail:
- Py_XDECREF (signal_name_obj);
- Py_XDECREF (signal_event_obj);
- return NULL;
+ return signal_event_obj.release ();
}
GDBPY_NEW_EVENT_TYPE (signal,
PyObject *
create_stop_event_object (PyTypeObject *py_type)
{
- PyObject *stop_event_obj = create_thread_event_object (py_type);
-
- if (!stop_event_obj)
- goto fail;
-
- return stop_event_obj;
-
- fail:
- Py_XDECREF (stop_event_obj);
- return NULL;
+ return create_thread_event_object (py_type);
}
/* Callback observers when a stop event occurs. This function will create a
int
emit_stop_event (struct bpstats *bs, enum gdb_signal stop_signal)
{
- PyObject *stop_event_obj = NULL; /* Appease GCC warning. */
- PyObject *list = NULL;
+ gdbpy_ref stop_event_obj;
+ gdbpy_ref list;
PyObject *first_bp = NULL;
struct bpstats *current_bs;
if (list == NULL)
{
- list = PyList_New (0);
- if (!list)
- goto fail;
+ list.reset (PyList_New (0));
+ if (list == NULL)
+ return -1;
}
- if (PyList_Append (list, current_py_bp))
- goto fail;
+ if (PyList_Append (list.get (), current_py_bp))
+ return -1;
if (first_bp == NULL)
first_bp = current_py_bp;
if (list != NULL)
{
- stop_event_obj = create_breakpoint_event_object (list, first_bp);
- if (!stop_event_obj)
- goto fail;
- Py_DECREF (list);
+ stop_event_obj.reset (create_breakpoint_event_object (list.get (),
+ first_bp));
+ if (stop_event_obj == NULL)
+ return -1;
}
/* Check if the signal is "Signal 0" or "Trace/breakpoint trap". */
if (stop_signal != GDB_SIGNAL_0
&& stop_signal != GDB_SIGNAL_TRAP)
{
- stop_event_obj =
- create_signal_event_object (stop_signal);
- if (!stop_event_obj)
- goto fail;
+ stop_event_obj.reset (create_signal_event_object (stop_signal));
+ if (stop_event_obj == NULL)
+ return -1;
}
/* If all fails emit an unknown stop event. All event types should
be known and this should eventually be unused. */
- if (!stop_event_obj)
+ if (stop_event_obj == NULL)
{
- stop_event_obj = create_stop_event_object (&stop_event_object_type);
- if (!stop_event_obj)
- goto fail;
+ stop_event_obj.reset (create_stop_event_object (&stop_event_object_type));
+ if (stop_event_obj == NULL)
+ return -1;
}
- return evpy_emit_event (stop_event_obj, gdb_py_events.stop);
-
- fail:
- Py_XDECREF (list);
- return -1;
+ return evpy_emit_event (stop_event_obj.get (), gdb_py_events.stop);
}
GDBPY_NEW_EVENT_TYPE (stop,
create_thread_event_object (PyTypeObject *py_type)
{
PyObject *thread = NULL;
- PyObject *thread_event_obj = NULL;
- thread_event_obj = create_event_object (py_type);
- if (!thread_event_obj)
- goto fail;
+ gdbpy_ref thread_event_obj (create_event_object (py_type));
+ if (thread_event_obj == NULL)
+ return NULL;
thread = get_event_thread ();
if (!thread)
- goto fail;
+ return NULL;
- if (evpy_add_attribute (thread_event_obj,
+ if (evpy_add_attribute (thread_event_obj.get (),
"inferior_thread",
thread) < 0)
- goto fail;
+ return NULL;
- return thread_event_obj;
-
- fail:
- Py_XDECREF (thread_event_obj);
- return NULL;
+ return thread_event_obj.release ();
}
GDBPY_NEW_EVENT_TYPE (thread,