gdb/remote-notif.c - gdb

Global variables defined

Functions defined

Source code

  1. /* Remote notification in GDB protocol

  2.    Copyright (C) 1988-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. /* 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 "observer.h"
  30. #include "event-loop.h"
  31. #include "target.h"
  32. #include "inferior.h"
  33. #include "infrun.h"
  34. #include "gdbcmd.h"

  35. int notif_debug = 0;

  36. /* Supported clients of notifications.  */

  37. static struct notif_client *notifs[] =
  38. {
  39.   &notif_client_stop,
  40. };

  41. gdb_static_assert (ARRAY_SIZE (notifs) == REMOTE_NOTIF_LAST);

  42. static void do_notif_event_xfree (void *arg);

  43. /* Parse the BUF for the expected notification NC, and send packet to
  44.    acknowledge.  */

  45. void
  46. remote_notif_ack (struct notif_client *nc, char *buf)
  47. {
  48.   struct notif_event *event = nc->alloc_event ();
  49.   struct cleanup *old_chain
  50.     = make_cleanup (do_notif_event_xfree, event);

  51.   if (notif_debug)
  52.     fprintf_unfiltered (gdb_stdlog, "notif: ack '%s'\n",
  53.                         nc->ack_command);

  54.   nc->parse (nc, buf, event);
  55.   nc->ack (nc, buf, event);

  56.   discard_cleanups (old_chain);
  57. }

  58. /* Parse the BUF for the expected notification NC.  */

  59. struct notif_event *
  60. remote_notif_parse (struct notif_client *nc, char *buf)
  61. {
  62.   struct notif_event *event = nc->alloc_event ();
  63.   struct cleanup *old_chain
  64.     = make_cleanup (do_notif_event_xfree, event);

  65.   if (notif_debug)
  66.     fprintf_unfiltered (gdb_stdlog, "notif: parse '%s'\n", nc->name);

  67.   nc->parse (nc, buf, event);

  68.   discard_cleanups (old_chain);
  69.   return event;
  70. }

  71. DEFINE_QUEUE_P (notif_client_p);

  72. /* Process notifications in STATE's notification queue one by one.
  73.    EXCEPT is not expected in the queue.  */

  74. void
  75. remote_notif_process (struct remote_notif_state *state,
  76.                       struct notif_client *except)
  77. {
  78.   while (!QUEUE_is_empty (notif_client_p, state->notif_queue))
  79.     {
  80.       struct notif_client *nc = QUEUE_deque (notif_client_p,
  81.                                              state->notif_queue);

  82.       gdb_assert (nc != except);

  83.       if (nc->can_get_pending_events (nc))
  84.         remote_notif_get_pending_events (nc);
  85.     }
  86. }

  87. static void
  88. remote_async_get_pending_events_handler (gdb_client_data data)
  89. {
  90.   gdb_assert (non_stop);
  91.   remote_notif_process (data, NULL);
  92. }

  93. /* Remote notification handler.  Parse BUF, queue notification and
  94.    update STATE.  */

  95. void
  96. handle_notification (struct remote_notif_state *state, char *buf)
  97. {
  98.   struct notif_client *nc;
  99.   size_t i;

  100.   for (i = 0; i < ARRAY_SIZE (notifs); i++)
  101.     {
  102.       const char *name = notifs[i]->name;

  103.       if (strncmp (buf, name, strlen (name)) == 0
  104.           && buf[strlen (name)] == ':')
  105.         break;
  106.     }

  107.   /* We ignore notifications we don't recognize, for compatibility
  108.      with newer stubs.  */
  109.   if (i == ARRAY_SIZE (notifs))
  110.     return;

  111.   nc =  notifs[i];

  112.   if (state->pending_event[nc->id] != NULL)
  113.     {
  114.       /* We've already parsed the in-flight reply, but the stub for some
  115.          reason thought we didn't, possibly due to timeout on its side.
  116.          Just ignore it.  */
  117.       if (notif_debug)
  118.         fprintf_unfiltered (gdb_stdlog,
  119.                             "notif: ignoring resent notification\n");
  120.     }
  121.   else
  122.     {
  123.       struct notif_event *event
  124.         = remote_notif_parse (nc, buf + strlen (nc->name) + 1);

  125.       /* Be careful to only set it after parsing, since an error
  126.          may be thrown then.  */
  127.       state->pending_event[nc->id] = event;

  128.       /* Notify the event loop there's a stop reply to acknowledge
  129.          and that there may be more events to fetch.  */
  130.       QUEUE_enque (notif_client_p, state->notif_queue, nc);
  131.       if (non_stop)
  132.         {
  133.           /* In non-stop, We mark REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN
  134.              in order to go on what we were doing and postpone
  135.              querying notification events to some point safe to do so.
  136.              See details in the function comment of
  137.              remote.c:remote_notif_get_pending_events.

  138.              In all-stop, GDB may be blocked to wait for the reply, we
  139.              shouldn't return to event loop until the expected reply
  140.              arrives.  For example:

  141.              1.1) --> vCont;c
  142.                GDB expects getting stop reply 'T05 thread:2'.
  143.              1.2) <-- %Notif
  144.                <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>

  145.              After step #1.2, we return to the event loop, which
  146.              notices there is a new event on the
  147.              REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN and calls the
  148.              handler, which will send 'vNotif' packet.
  149.              1.3) --> vNotif
  150.              It is not safe to start a new sequence, because target
  151.              is still running and GDB is expecting the stop reply
  152.              from stub.

  153.              To solve this, whenever we parse a notification
  154.              successfully, we don't mark the
  155.              REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN and let GDB blocked
  156.              there as before to get the sequence done.

  157.              2.1) --> vCont;c
  158.                GDB expects getting stop reply 'T05 thread:2'
  159.              2.2) <-- %Notif
  160.                <Don't mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
  161.              2.3) <-- T05 thread:2

  162.              These pending notifications can be processed later.  */
  163.           mark_async_event_handler (state->get_pending_events_token);
  164.         }

  165.       if (notif_debug)
  166.         fprintf_unfiltered (gdb_stdlog,
  167.                             "notif: Notification '%s' captured\n",
  168.                             nc->name);
  169.     }
  170. }

  171. /* Invoke destructor of EVENT and xfree it.  */

  172. void
  173. notif_event_xfree (struct notif_event *event)
  174. {
  175.   if (event != NULL && event->dtr != NULL)
  176.     event->dtr (event);

  177.   xfree (event);
  178. }

  179. /* Cleanup wrapper.  */

  180. static void
  181. do_notif_event_xfree (void *arg)
  182. {
  183.   notif_event_xfree (arg);
  184. }

  185. /* Return an allocated remote_notif_state.  */

  186. struct remote_notif_state *
  187. remote_notif_state_allocate (void)
  188. {
  189.   struct remote_notif_state *notif_state = xzalloc (sizeof (*notif_state));

  190.   notif_state->notif_queue = QUEUE_alloc (notif_client_p, NULL);

  191.   /* Register async_event_handler for notification.  */

  192.   notif_state->get_pending_events_token
  193.     = create_async_event_handler (remote_async_get_pending_events_handler,
  194.                                   notif_state);

  195.   return notif_state;
  196. }

  197. /* Free STATE and its fields.  */

  198. void
  199. remote_notif_state_xfree (struct remote_notif_state *state)
  200. {
  201.   int i;

  202.   QUEUE_free (notif_client_p, state->notif_queue);

  203.   /* Unregister async_event_handler for notification.  */
  204.   if (state->get_pending_events_token != NULL)
  205.     delete_async_event_handler (&state->get_pending_events_token);

  206.   for (i = 0; i < REMOTE_NOTIF_LAST; i++)
  207.     notif_event_xfree (state->pending_event[i]);

  208.   xfree (state);
  209. }

  210. /* -Wmissing-prototypes */
  211. extern initialize_file_ftype _initialize_notif;

  212. void
  213. _initialize_notif (void)
  214. {
  215.   add_setshow_boolean_cmd ("notification", no_class, &notif_debug,
  216.                            _("\
  217. Set debugging of async remote notification."), _("\
  218. Show debugging of async remote notification."), _("\
  219. When non-zero, debugging output about async remote notifications"
  220. " is enabled."),
  221.                            NULL,
  222.                            NULL,
  223.                            &setdebuglist, &showdebuglist);
  224. }