remote-notif.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* Remote notification in GDB protocol
  2. Copyright (C) 1988-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. /* Remote async notification is sent from remote target over RSP.
  15. Each type of notification is represented by an object of
  16. 'struct notif', which has a field 'pending_reply'. It is not
  17. NULL when GDB receives a notification from GDBserver, but hasn't
  18. acknowledge yet. Before GDB acknowledges the notification,
  19. GDBserver shouldn't send notification again (see the header comments
  20. in gdbserver/notif.c).
  21. Notifications are processed in an almost-unified approach for both
  22. all-stop mode and non-stop mode, except the timing to process them.
  23. In non-stop mode, notifications are processed in
  24. remote_async_get_pending_events_handler, while in all-stop mode,
  25. they are processed in remote_resume. */
  26. #include "defs.h"
  27. #include "remote.h"
  28. #include "remote-notif.h"
  29. #include "observable.h"
  30. #include "gdbsupport/event-loop.h"
  31. #include "target.h"
  32. #include "inferior.h"
  33. #include "infrun.h"
  34. #include "gdbcmd.h"
  35. #include "async-event.h"
  36. bool notif_debug = false;
  37. /* Supported clients of notifications. */
  38. static struct notif_client *notifs[] =
  39. {
  40. &notif_client_stop,
  41. };
  42. gdb_static_assert (ARRAY_SIZE (notifs) == REMOTE_NOTIF_LAST);
  43. /* Parse the BUF for the expected notification NC, and send packet to
  44. acknowledge. */
  45. void
  46. remote_notif_ack (remote_target *remote,
  47. struct notif_client *nc, const char *buf)
  48. {
  49. notif_event_up event = nc->alloc_event ();
  50. if (notif_debug)
  51. gdb_printf (gdb_stdlog, "notif: ack '%s'\n",
  52. nc->ack_command);
  53. nc->parse (remote, nc, buf, event.get ());
  54. nc->ack (remote, nc, buf, event.release ());
  55. }
  56. /* Parse the BUF for the expected notification NC. */
  57. struct notif_event *
  58. remote_notif_parse (remote_target *remote,
  59. struct notif_client *nc, const char *buf)
  60. {
  61. notif_event_up event = nc->alloc_event ();
  62. if (notif_debug)
  63. gdb_printf (gdb_stdlog, "notif: parse '%s'\n", nc->name);
  64. nc->parse (remote, nc, buf, event.get ());
  65. return event.release ();
  66. }
  67. /* Process notifications in STATE's notification queue one by one.
  68. EXCEPT is not expected in the queue. */
  69. void
  70. remote_notif_process (struct remote_notif_state *state,
  71. struct notif_client *except)
  72. {
  73. while (!state->notif_queue.empty ())
  74. {
  75. struct notif_client *nc = state->notif_queue.front ();
  76. state->notif_queue.pop_front ();
  77. gdb_assert (nc != except);
  78. if (nc->can_get_pending_events (state->remote, nc))
  79. remote_notif_get_pending_events (state->remote, nc);
  80. }
  81. }
  82. static void
  83. remote_async_get_pending_events_handler (gdb_client_data data)
  84. {
  85. remote_notif_state *notif_state = (remote_notif_state *) data;
  86. clear_async_event_handler (notif_state->get_pending_events_token);
  87. gdb_assert (remote_target_is_non_stop_p (notif_state->remote));
  88. remote_notif_process (notif_state, NULL);
  89. }
  90. /* Remote notification handler. Parse BUF, queue notification and
  91. update STATE. */
  92. void
  93. handle_notification (struct remote_notif_state *state, const char *buf)
  94. {
  95. struct notif_client *nc;
  96. size_t i;
  97. for (i = 0; i < ARRAY_SIZE (notifs); i++)
  98. {
  99. const char *name = notifs[i]->name;
  100. if (startswith (buf, name)
  101. && buf[strlen (name)] == ':')
  102. break;
  103. }
  104. /* We ignore notifications we don't recognize, for compatibility
  105. with newer stubs. */
  106. if (i == ARRAY_SIZE (notifs))
  107. return;
  108. nc = notifs[i];
  109. if (state->pending_event[nc->id] != NULL)
  110. {
  111. /* We've already parsed the in-flight reply, but the stub for some
  112. reason thought we didn't, possibly due to timeout on its side.
  113. Just ignore it. */
  114. if (notif_debug)
  115. gdb_printf (gdb_stdlog,
  116. "notif: ignoring resent notification\n");
  117. }
  118. else
  119. {
  120. struct notif_event *event
  121. = remote_notif_parse (state->remote, nc, buf + strlen (nc->name) + 1);
  122. /* Be careful to only set it after parsing, since an error
  123. may be thrown then. */
  124. state->pending_event[nc->id] = event;
  125. /* Notify the event loop there's a stop reply to acknowledge
  126. and that there may be more events to fetch. */
  127. state->notif_queue.push_back (nc);
  128. if (target_is_non_stop_p ())
  129. {
  130. /* In non-stop, We mark REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN
  131. in order to go on what we were doing and postpone
  132. querying notification events to some point safe to do so.
  133. See details in the function comment of
  134. remote.c:remote_notif_get_pending_events.
  135. In all-stop, GDB may be blocked to wait for the reply, we
  136. shouldn't return to event loop until the expected reply
  137. arrives. For example:
  138. 1.1) --> vCont;c
  139. GDB expects getting stop reply 'T05 thread:2'.
  140. 1.2) <-- %Notif
  141. <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
  142. After step #1.2, we return to the event loop, which
  143. notices there is a new event on the
  144. REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN and calls the
  145. handler, which will send 'vNotif' packet.
  146. 1.3) --> vNotif
  147. It is not safe to start a new sequence, because target
  148. is still running and GDB is expecting the stop reply
  149. from stub.
  150. To solve this, whenever we parse a notification
  151. successfully, we don't mark the
  152. REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN and let GDB blocked
  153. there as before to get the sequence done.
  154. 2.1) --> vCont;c
  155. GDB expects getting stop reply 'T05 thread:2'
  156. 2.2) <-- %Notif
  157. <Don't mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
  158. 2.3) <-- T05 thread:2
  159. These pending notifications can be processed later. */
  160. mark_async_event_handler (state->get_pending_events_token);
  161. }
  162. if (notif_debug)
  163. gdb_printf (gdb_stdlog,
  164. "notif: Notification '%s' captured\n",
  165. nc->name);
  166. }
  167. }
  168. /* Return an allocated remote_notif_state. */
  169. struct remote_notif_state *
  170. remote_notif_state_allocate (remote_target *remote)
  171. {
  172. struct remote_notif_state *notif_state = new struct remote_notif_state;
  173. notif_state->remote = remote;
  174. /* Register async_event_handler for notification. */
  175. notif_state->get_pending_events_token
  176. = create_async_event_handler (remote_async_get_pending_events_handler,
  177. notif_state, "remote-notif");
  178. return notif_state;
  179. }
  180. /* Free STATE and its fields. */
  181. remote_notif_state::~remote_notif_state ()
  182. {
  183. int i;
  184. /* Unregister async_event_handler for notification. */
  185. if (get_pending_events_token != NULL)
  186. delete_async_event_handler (&get_pending_events_token);
  187. for (i = 0; i < REMOTE_NOTIF_LAST; i++)
  188. delete pending_event[i];
  189. }
  190. void _initialize_notif ();
  191. void
  192. _initialize_notif ()
  193. {
  194. add_setshow_boolean_cmd ("notification", no_class, &notif_debug,
  195. _("\
  196. Set debugging of async remote notification."), _("\
  197. Show debugging of async remote notification."), _("\
  198. When non-zero, debugging output about async remote notifications"
  199. " is enabled."),
  200. NULL,
  201. NULL,
  202. &setdebuglist, &showdebuglist);
  203. }