gdb/observer.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* GDB Notifications to Observers.

  2.    Copyright (C) 2003-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. /* An observer is an entity who is interested in being notified when GDB
  15.    reaches certain states, or certain events occur in GDB.  The entity being
  16.    observed is called the Subject.  To receive notifications, the observer
  17.    attaches a callback to the subject.  One subject can have several
  18.    observers.

  19.    This file implements an internal generic low-level event notification
  20.    mechanism based on the Observer paradigm described in the book "Design
  21.    Patterns".  This generic event notification mechansim is then re-used
  22.    to implement the exported high-level notification management routines
  23.    for all possible notifications.

  24.    The current implementation of the generic observer provides support
  25.    for contextual data.  This contextual data is given to the subject
  26.    when attaching the callback.  In return, the subject will provide
  27.    this contextual data back to the observer as a parameter of the
  28.    callback.

  29.    FIXME: The current support for the contextual data is only partial,
  30.    as it lacks a mechanism that would deallocate this data when the
  31.    callback is detached.  This is not a problem so far, as this contextual
  32.    data is only used internally to hold a function pointer.  Later on,
  33.    if a certain observer needs to provide support for user-level
  34.    contextual data, then the generic notification mechanism will need
  35.    need to be enhanced to allow the observer to provide a routine to
  36.    deallocate the data when attaching the callback.

  37.    This file is currently maintained by hand, but the long term plan
  38.    if the number of different notifications starts growing is to create
  39.    a new script (observer.sh) that would generate this file, and the
  40.    associated documentation.  */

  41. #include "defs.h"
  42. #include "observer.h"
  43. #include "command.h"
  44. #include "gdbcmd.h"

  45. static unsigned int observer_debug;
  46. static void
  47. show_observer_debug (struct ui_file *file, int from_tty,
  48.                      struct cmd_list_element *c, const char *value)
  49. {
  50.   fprintf_filtered (file, _("Observer debugging is %s.\n"), value);
  51. }

  52. /* The internal generic observer.  */

  53. typedef void (generic_observer_notification_ftype) (const void *data,
  54.                                                     const void *args);

  55. struct observer
  56. {
  57.   generic_observer_notification_ftype *notify;
  58.   /* No memory management needed for the following field for now.  */
  59.   void *data;
  60. };

  61. /* A list of observers, maintained by the subject.  A subject is
  62.    actually represented by its list of observers.  */

  63. struct observer_list
  64. {
  65.   struct observer_list *next;
  66.   struct observer *observer;
  67. };

  68. /* Allocate a struct observer_list, intended to be used as a node
  69.    in the list of observers maintained by a subject.  */

  70. static struct observer_list *
  71. xalloc_observer_list_node (void)
  72. {
  73.   struct observer_list *node = XNEW (struct observer_list);

  74.   node->observer = XNEW (struct observer);
  75.   return node;
  76. }

  77. /* The opposite of xalloc_observer_list_node, frees the memory for
  78.    the given node.  */

  79. static void
  80. xfree_observer_list_node (struct observer_list *node)
  81. {
  82.   xfree (node->observer);
  83.   xfree (node);
  84. }

  85. /* Attach the callback NOTIFY to a SUBJECT.  The DATA is also stored,
  86.    in order for the subject to provide it back to the observer during
  87.    a notification.  */

  88. static struct observer *
  89. generic_observer_attach (struct observer_list **subject,
  90.                          generic_observer_notification_ftype * notify,
  91.                          void *data)
  92. {
  93.   struct observer_list *observer_list = xalloc_observer_list_node ();

  94.   observer_list->next = *subject;
  95.   observer_list->observer->notify = notify;
  96.   observer_list->observer->data = data;
  97.   *subject = observer_list;

  98.   return observer_list->observer;
  99. }

  100. /* Remove the given OBSERVER from the SUBJECT.  Once detached, OBSERVER
  101.    should no longer be used, as it is no longer valid.  */

  102. static void
  103. generic_observer_detach (struct observer_list **subject,
  104.                          const struct observer *observer)
  105. {
  106.   struct observer_list *previous_node = NULL;
  107.   struct observer_list *current_node = *subject;

  108.   while (current_node != NULL)
  109.     {
  110.       if (current_node->observer == observer)
  111.         {
  112.           if (previous_node != NULL)
  113.             previous_node->next = current_node->next;
  114.           else
  115.             *subject = current_node->next;
  116.           xfree_observer_list_node (current_node);
  117.           return;
  118.         }
  119.       previous_node = current_node;
  120.       current_node = current_node->next;
  121.     }

  122.   /* We should never reach this point.  However, this should not be
  123.      a very serious error, so simply report a warning to the user.  */
  124.   warning (_("Failed to detach observer"));
  125. }

  126. /* Send a notification to all the observers of SUBJECT.  ARGS is passed to
  127.    all observers as an argument to the notification callback.  */

  128. static void
  129. generic_observer_notify (struct observer_list *subject, const void *args)
  130. {
  131.   struct observer_list *current_node = subject;

  132.   while (current_node != NULL)
  133.     {
  134.       (*current_node->observer->notify) (current_node->observer->data, args);
  135.       current_node = current_node->next;
  136.     }
  137. }


  138. /* The following code is only used to unit-test the observers from our
  139.    testsuite.  DO NOT USE IT within observer.c (or anywhere else for
  140.    that matter)!  */

  141. /* If we define these variables and functions as `static', the
  142.    compiler will optimize them out.  */

  143. int observer_test_first_observer = 0;
  144. int observer_test_second_observer = 0;
  145. int observer_test_third_observer = 0;

  146. /* Provide prototypes to silence -Wmissing-prototypes.  */
  147. extern void observer_test_first_notification_function (int arg);
  148. extern void observer_test_second_notification_function (int arg);
  149. extern void observer_test_third_notification_function (int arg);

  150. void
  151. observer_test_first_notification_function (int arg)
  152. {
  153.   observer_test_first_observer++;
  154. }

  155. void
  156. observer_test_second_notification_function (int arg)
  157. {
  158.   observer_test_second_observer++;
  159. }

  160. void
  161. observer_test_third_notification_function (int arg)
  162. {
  163.   observer_test_third_observer++;
  164. }

  165. extern initialize_file_ftype _initialize_observer; /* -Wmissing-prototypes */

  166. void
  167. _initialize_observer (void)
  168. {
  169.   add_setshow_zuinteger_cmd ("observer", class_maintenance,
  170.                              &observer_debug, _("\
  171. Set observer debugging."), _("\
  172. Show observer debugging."), _("\
  173. When non-zero, observer debugging is enabled."),
  174.                              NULL,
  175.                              show_observer_debug,
  176.                              &setdebuglist, &showdebuglist);
  177. }

  178. #include "observer.inc"