gdb/continuations.c - gdb

Data types defined

Functions defined

Source code

  1. /* Continuations for GDB, the GNU debugger.

  2.    Copyright (C) 1986-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. #include "defs.h"
  15. #include "gdbthread.h"
  16. #include "inferior.h"
  17. #include "continuations.h"

  18. struct continuation
  19. {
  20.   struct continuation *next;
  21.   continuation_ftype *function;
  22.   continuation_free_arg_ftype *free_arg;
  23.   void *arg;
  24. };

  25. /* Add a new continuation to the continuation chain.  Args are
  26.    FUNCTION to run the continuation up with, and ARG to pass to
  27.    it.  */

  28. static void
  29. make_continuation (struct continuation **pmy_chain,
  30.                    continuation_ftype *function,
  31.                    void *argvoid (*free_arg) (void *))
  32. {
  33.   struct continuation *new = XNEW (struct continuation);

  34.   new->next = *pmy_chain;
  35.   new->function = function;
  36.   new->free_arg = free_arg;
  37.   new->arg = arg;
  38.   *pmy_chain = new;
  39. }

  40. static void
  41. do_my_continuations_1 (struct continuation **pmy_chain, int err)
  42. {
  43.   struct continuation *ptr;

  44.   while ((ptr = *pmy_chain) != NULL)
  45.     {
  46.       *pmy_chain = ptr->next;        /* Do this first in case of recursion.  */
  47.       (*ptr->function) (ptr->arg, err);
  48.       if (ptr->free_arg)
  49.         (*ptr->free_arg) (ptr->arg);
  50.       xfree (ptr);
  51.     }
  52. }

  53. static void
  54. do_my_continuations (struct continuation **list, int err)
  55. {
  56.   struct continuation *continuations;

  57.   if (*list == NULL)
  58.     return;

  59.   /* Copy the list header into another pointer, and set the global
  60.      list header to null, so that the global list can change as a side
  61.      effect of invoking the continuations and the processing of the
  62.      preexisting continuations will not be affected.  */

  63.   continuations = *list;
  64.   *list = NULL;

  65.   /* Work now on the list we have set aside.  */
  66.   do_my_continuations_1 (&continuations, err);
  67. }

  68. static void
  69. discard_my_continuations_1 (struct continuation **pmy_chain)
  70. {
  71.   struct continuation *ptr;

  72.   while ((ptr = *pmy_chain) != NULL)
  73.     {
  74.       *pmy_chain = ptr->next;
  75.       if (ptr->free_arg)
  76.         (*ptr->free_arg) (ptr->arg);
  77.       xfree (ptr);
  78.     }
  79. }

  80. static void
  81. discard_my_continuations (struct continuation **list)
  82. {
  83.   discard_my_continuations_1 (list);
  84.   *list = NULL;
  85. }

  86. /* Add a continuation to the continuation list of INFERIOR.  The new
  87.    continuation will be added at the front.  */

  88. void
  89. add_inferior_continuation (continuation_ftype *hook, void *args,
  90.                            continuation_free_arg_ftype *free_arg)
  91. {
  92.   struct inferior *inf = current_inferior ();

  93.   make_continuation (&inf->continuations, hook, args, free_arg);
  94. }

  95. /* Do all continuations of the current inferior.  */

  96. void
  97. do_all_inferior_continuations (int err)
  98. {
  99.   struct inferior *inf = current_inferior ();
  100.   do_my_continuations (&inf->continuations, err);
  101. }

  102. /* Get rid of all the inferior-wide continuations of INF.  */

  103. void
  104. discard_all_inferior_continuations (struct inferior *inf)
  105. {
  106.   discard_my_continuations (&inf->continuations);
  107. }

  108. /* Add a continuation to the continuation list of THREAD.  The new
  109.    continuation will be added at the front.  */

  110. void
  111. add_continuation (struct thread_info *thread,
  112.                   continuation_ftype *hook, void *args,
  113.                   continuation_free_arg_ftype *free_arg)
  114. {
  115.   make_continuation (&thread->continuations, hook, args, free_arg);
  116. }

  117. static void
  118. restore_thread_cleanup (void *arg)
  119. {
  120.   ptid_t *ptid_p = arg;

  121.   switch_to_thread (*ptid_p);
  122. }

  123. /* Walk down the continuation list of PTID, and execute all the
  124.    continuations.  There is a problem though.  In some cases new
  125.    continuations may be added while we are in the middle of this loop.
  126.    If this happens they will be added in the front, and done before we
  127.    have a chance of exhausting those that were already there.  We need
  128.    to then save the beginning of the list in a pointer and do the
  129.    continuations from there on, instead of using the global beginning
  130.    of list as our iteration pointer.  */

  131. static void
  132. do_all_continuations_ptid (ptid_t ptid,
  133.                            struct continuation **continuations_p,
  134.                            int err)
  135. {
  136.   struct cleanup *old_chain;
  137.   ptid_t current_thread;

  138.   if (*continuations_p == NULL)
  139.     return;

  140.   current_thread = inferior_ptid;

  141.   /* Restore selected thread on exit.  Don't try to restore the frame
  142.      as well, because:

  143.      - When running continuations, the selected frame is always #0.

  144.      - The continuations may trigger symbol file loads, which may
  145.      change the frame layout (frame ids change), which would trigger
  146.      a warning if we used make_cleanup_restore_current_thread.  */

  147.   old_chain = make_cleanup (restore_thread_cleanup, &current_thread);

  148.   /* Let the continuation see this thread as selected.  */
  149.   switch_to_thread (ptid);

  150.   do_my_continuations (continuations_p, err);

  151.   do_cleanups (old_chain);
  152. }

  153. /* Callback for iterate over threads.  */

  154. static int
  155. do_all_continuations_thread_callback (struct thread_info *thread, void *data)
  156. {
  157.   int err = * (int *) data;
  158.   do_all_continuations_ptid (thread->ptid, &thread->continuations, err);
  159.   return 0;
  160. }

  161. /* Do all continuations of thread THREAD.  */

  162. void
  163. do_all_continuations_thread (struct thread_info *thread, int err)
  164. {
  165.   do_all_continuations_thread_callback (thread, &err);
  166. }

  167. /* Do all continuations of all threads.  */

  168. void
  169. do_all_continuations (int err)
  170. {
  171.   iterate_over_threads (do_all_continuations_thread_callback, &err);
  172. }

  173. /* Callback for iterate over threads.  */

  174. static int
  175. discard_all_continuations_thread_callback (struct thread_info *thread,
  176.                                            void *data)
  177. {
  178.   discard_my_continuations (&thread->continuations);
  179.   return 0;
  180. }

  181. /* Get rid of all the continuations of THREAD.  */

  182. void
  183. discard_all_continuations_thread (struct thread_info *thread)
  184. {
  185.   discard_all_continuations_thread_callback (thread, NULL);
  186. }

  187. /* Get rid of all the continuations of all threads.  */

  188. void
  189. discard_all_continuations (void)
  190. {
  191.   iterate_over_threads (discard_all_continuations_thread_callback, NULL);
  192. }


  193. /* Add a continuation to the intermediate continuation list of THREAD.
  194.    The new continuation will be added at the front.  */

  195. void
  196. add_intermediate_continuation (struct thread_info *thread,
  197.                                continuation_ftype *hook,
  198.                                void *args,
  199.                                continuation_free_arg_ftype *free_arg)
  200. {
  201.   make_continuation (&thread->intermediate_continuations, hook,
  202.                      args, free_arg);
  203. }

  204. /* Walk down the cmd_continuation list, and execute all the
  205.    continuations.  There is a problem though.  In some cases new
  206.    continuations may be added while we are in the middle of this
  207.    loop.  If this happens they will be added in the front, and done
  208.    before we have a chance of exhausting those that were already
  209.    there.  We need to then save the beginning of the list in a pointer
  210.    and do the continuations from there on, instead of using the
  211.    global beginning of list as our iteration pointer.  */

  212. static int
  213. do_all_intermediate_continuations_thread_callback (struct thread_info *thread,
  214.                                                    void *data)
  215. {
  216.   int err = * (int *) data;

  217.   do_all_continuations_ptid (thread->ptid,
  218.                              &thread->intermediate_continuations, err);
  219.   return 0;
  220. }

  221. /* Do all intermediate continuations of thread THREAD.  */

  222. void
  223. do_all_intermediate_continuations_thread (struct thread_info *thread, int err)
  224. {
  225.   do_all_intermediate_continuations_thread_callback (thread, &err);
  226. }

  227. /* Do all intermediate continuations of all threads.  */

  228. void
  229. do_all_intermediate_continuations (int err)
  230. {
  231.   iterate_over_threads (do_all_intermediate_continuations_thread_callback,
  232.                         &err);
  233. }

  234. /* Callback for iterate over threads.  */

  235. static int
  236. discard_all_intermediate_continuations_thread_callback (struct thread_info *thread,
  237.                                                         void *data)
  238. {
  239.   discard_my_continuations (&thread->intermediate_continuations);
  240.   return 0;
  241. }

  242. /* Get rid of all the intermediate continuations of THREAD.  */

  243. void
  244. discard_all_intermediate_continuations_thread (struct thread_info *thread)
  245. {
  246.   discard_all_intermediate_continuations_thread_callback (thread, NULL);
  247. }

  248. /* Get rid of all the intermediate continuations of all threads.  */

  249. void
  250. discard_all_intermediate_continuations (void)
  251. {
  252.   iterate_over_threads (discard_all_intermediate_continuations_thread_callback,
  253.                         NULL);
  254. }