py-stopevent.c 2.9 KB

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