gdb/gdbserver/notif.c - gdb

Global variables defined

Functions defined

Source code

  1. /* Notification to GDB.
  2.    Copyright (C) 1989-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. /* Async notifications to GDB.  When the state of remote target is
  15.    changed or something interesting to GDB happened, async
  16.    notifications are used to tell GDB.

  17.    Each type of notification is represented by an object
  18.    'struct notif_server', in which there is a queue for events to GDB
  19.    represented by 'struct notif_event'.  GDBserver writes (by means of
  20.    'write' field) each event in the queue into the buffer and send the
  21.    contents in buffer to GDB.  The contents in buffer is specified in
  22.    RSP.  See more in the comments to field 'queue' of
  23.    'struct notif_server'.

  24.    Here is the workflow of sending events and managing queue:
  25.    1.  At any time, when something interesting FOO happens, a object
  26.    of 'struct notif_event' or its sub-class EVENT is created for FOO.

  27.    2.  Enque EVENT to the 'queue' field of 'struct notif_server' for
  28.    FOO and send corresponding notification packet to GDB if EVENT is
  29.    the first one.
  30.    #1 and #2 are done by function 'notif_push'.

  31.    3.  EVENT is not deque'ed until the ack of FOO from GDB arrives.
  32.    Before ack of FOO arrives, FOO happens again, a new object of
  33.    EVENT is created and enque EVENT silently.
  34.    Once GDB has a chance to ack to FOO, it sends an ack to GDBserver,
  35.    and GDBserver repeatedly sends events to GDB and gets ack of FOO,
  36.    until queue is empty.  Then, GDBserver sends 'OK' to GDB that all
  37.    queued notification events are done.

  38.    # 3 is done by function 'handle_notif_ack'.  */

  39. #include "server.h"
  40. #include "notif.h"

  41. static struct notif_server *notifs[] =
  42. {
  43.   &notif_stop,
  44. };

  45. /* Write another event or an OK, if there are no more left, to
  46.    OWN_BUF.  */

  47. void
  48. notif_write_event (struct notif_server *notif, char *own_buf)
  49. {
  50.   if (!QUEUE_is_empty (notif_event_p, notif->queue))
  51.     {
  52.       struct notif_event *event
  53.         = QUEUE_peek (notif_event_p, notif->queue);

  54.       notif->write (event, own_buf);
  55.     }
  56.   else
  57.     write_ok (own_buf);
  58. }

  59. /* Handle the ack in buffer OWN_BUF,and packet length is PACKET_LEN.
  60.    Return 1 if the ack is handled, and return 0 if the contents
  61.    in OWN_BUF is not a ack.  */

  62. int
  63. handle_notif_ack (char *own_buf, int packet_len)
  64. {
  65.   size_t i;
  66.   struct notif_server *np;

  67.   for (i = 0; i < ARRAY_SIZE (notifs); i++)
  68.     {
  69.       const char *ack_name = notifs[i]->ack_name;

  70.       if (strncmp (own_buf, ack_name, strlen (ack_name)) == 0
  71.           && packet_len == strlen (ack_name))
  72.         break;
  73.     }

  74.   if (i == ARRAY_SIZE (notifs))
  75.     return 0;

  76.   np = notifs[i];

  77.   /* If we're waiting for GDB to acknowledge a pending event,
  78.      consider that done.  */
  79.   if (!QUEUE_is_empty (notif_event_p, np->queue))
  80.     {
  81.       struct notif_event *head
  82.         = QUEUE_deque (notif_event_p, np->queue);

  83.       if (remote_debug)
  84.         fprintf (stderr, "%s: acking %d\n", np->ack_name,
  85.                  QUEUE_length (notif_event_p, np->queue));

  86.       xfree (head);
  87.     }

  88.   notif_write_event (np, own_buf);

  89.   return 1;
  90. }

  91. /* Put EVENT to the queue of NOTIF.  */

  92. void
  93. notif_event_enque (struct notif_server *notif,
  94.                    struct notif_event *event)
  95. {
  96.   QUEUE_enque (notif_event_p, notif->queue, event);

  97.   if (remote_debug)
  98.     fprintf (stderr, "pending events: %s %d\n", notif->notif_name,
  99.              QUEUE_length (notif_event_p, notif->queue));

  100. }

  101. /* Push one event NEW_EVENT of notification NP into NP->queue.  */

  102. void
  103. notif_push (struct notif_server *np, struct notif_event *new_event)
  104. {
  105.   int is_first_event = QUEUE_is_empty (notif_event_p, np->queue);

  106.   /* Something interesting.  Tell GDB about it.  */
  107.   notif_event_enque (np, new_event);

  108.   /* If this is the first stop reply in the queue, then inform GDB
  109.      about it, by sending a corresponding notification.  */
  110.   if (is_first_event)
  111.     {
  112.       char buf[PBUFSIZ];
  113.       char *p = buf;

  114.       xsnprintf (p, PBUFSIZ, "%s:", np->notif_name);
  115.       p += strlen (p);

  116.       np->write (new_event, p);
  117.       putpkt_notif (buf);
  118.     }
  119. }

  120. static void
  121. notif_event_xfree (struct notif_event *event)
  122. {
  123.   xfree (event);
  124. }

  125. void
  126. initialize_notif (void)
  127. {
  128.   int i = 0;

  129.   for (i = 0; i < ARRAY_SIZE (notifs); i++)
  130.     notifs[i]->queue
  131.       = QUEUE_alloc (notif_event_p, notif_event_xfree);
  132. }