gdb/python/py-stopevent.c - gdb

Functions defined

Source code

  1. /* Python interface to inferior stop events.

  2.    Copyright (C) 2009-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "py-stopevent.h"

  16. PyObject *
  17. create_stop_event_object (PyTypeObject *py_type)
  18. {
  19.   PyObject *stop_event_obj = create_thread_event_object (py_type);

  20.   if (!stop_event_obj)
  21.     goto fail;

  22.   return stop_event_obj;

  23.   fail:
  24.    Py_XDECREF (stop_event_obj);
  25.    return NULL;
  26. }

  27. /* Callback observers when a stop event occurs.  This function will create a
  28.    new Python stop event object.  If only a specific thread is stopped the
  29.    thread object of the event will be set to that thread.  Otherwise, if all
  30.    threads are stopped thread object will be set to None.
  31.    return 0 if the event was created and emitted successfully otherwise
  32.    returns -1.  */

  33. int
  34. emit_stop_event (struct bpstats *bs, enum gdb_signal stop_signal)
  35. {
  36.   PyObject *stop_event_obj = NULL; /* Appease GCC warning.  */
  37.   PyObject *list = NULL;
  38.   PyObject *first_bp = NULL;
  39.   struct bpstats *current_bs;

  40.   if (evregpy_no_listeners_p (gdb_py_events.stop))
  41.     return 0;

  42.   /* Add any breakpoint set at this location to the list.  */
  43.   for (current_bs = bs; current_bs != NULL; current_bs = current_bs->next)
  44.     {
  45.       if (current_bs->breakpoint_at
  46.           && current_bs->breakpoint_at->py_bp_object)
  47.         {
  48.           PyObject *current_py_bp =
  49.               (PyObject *) current_bs->breakpoint_at->py_bp_object;

  50.           if (list == NULL)
  51.             {
  52.               list = PyList_New (0);
  53.               if (!list)
  54.                 goto fail;
  55.             }

  56.           if (PyList_Append (list, current_py_bp))
  57.             goto fail;

  58.           if (first_bp == NULL)
  59.             first_bp = current_py_bp;
  60.         }
  61.     }

  62.   if (list != NULL)
  63.     {
  64.       stop_event_obj = create_breakpoint_event_object (list, first_bp);
  65.       if (!stop_event_obj)
  66.         goto fail;
  67.       Py_DECREF (list);
  68.     }

  69.   /* Check if the signal is "Signal 0" or "Trace/breakpoint trap".  */
  70.   if (stop_signal != GDB_SIGNAL_0
  71.       && stop_signal != GDB_SIGNAL_TRAP)
  72.     {
  73.       stop_event_obj =
  74.           create_signal_event_object (stop_signal);
  75.       if (!stop_event_obj)
  76.         goto fail;
  77.     }

  78.   /* If all fails emit an unknown stop event.  All event types should
  79.      be known and this should eventually be unused.  */
  80.   if (!stop_event_obj)
  81.     {
  82.       stop_event_obj = create_stop_event_object (&stop_event_object_type);
  83.       if (!stop_event_obj)
  84.         goto fail;
  85.     }

  86.   return evpy_emit_event (stop_event_obj, gdb_py_events.stop);

  87. fail:
  88.   Py_XDECREF (list);
  89.   return -1;
  90. }

  91. GDBPY_NEW_EVENT_TYPE (stop,
  92.                       "gdb.StopEvent",
  93.                       "StopEvent",
  94.                       "GDB stop event object",
  95.                       thread_event_object_type,
  96.                       /*no qual*/);