gdb/dummy-frame.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Code dealing with dummy stack frames, 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 "dummy-frame.h"
  16. #include "regcache.h"
  17. #include "frame.h"
  18. #include "inferior.h"
  19. #include "frame-unwind.h"
  20. #include "command.h"
  21. #include "gdbcmd.h"
  22. #include "observer.h"
  23. #include "gdbthread.h"

  24. struct dummy_frame_id
  25. {
  26.   /* This frame's ID.  Must match the value returned by
  27.      gdbarch_dummy_id.  */
  28.   struct frame_id id;

  29.   /* The thread this dummy_frame relates to.  */
  30.   ptid_t ptid;
  31. };

  32. /* Return whether dummy_frame_id *ID1 and *ID2 are equal.  */

  33. static int
  34. dummy_frame_id_eq (struct dummy_frame_id *id1,
  35.                    struct dummy_frame_id *id2)
  36. {
  37.   return frame_id_eq (id1->id, id2->id) && ptid_equal (id1->ptid, id2->ptid);
  38. }

  39. /* Dummy frame.  This saves the processor state just prior to setting
  40.    up the inferior function call.  Older targets save the registers
  41.    on the target stack (but that really slows down function calls).  */

  42. struct dummy_frame
  43. {
  44.   struct dummy_frame *next;

  45.   /* An id represents a dummy frame.  */
  46.   struct dummy_frame_id id;

  47.   /* The caller's state prior to the call.  */
  48.   struct infcall_suspend_state *caller_state;

  49.   /* If non-NULL, a destructor that is run when this dummy frame is
  50.      popped.  */
  51.   void (*dtor) (void *data);

  52.   /* Arbitrary data that is passed to DTOR.  */
  53.   void *dtor_data;
  54. };

  55. static struct dummy_frame *dummy_frame_stack = NULL;

  56. /* Push the caller's state, along with the dummy frame info, onto the
  57.    dummy-frame stack.  */

  58. void
  59. dummy_frame_push (struct infcall_suspend_state *caller_state,
  60.                   const struct frame_id *dummy_id, ptid_t ptid)
  61. {
  62.   struct dummy_frame *dummy_frame;

  63.   dummy_frame = XCNEW (struct dummy_frame);
  64.   dummy_frame->caller_state = caller_state;
  65.   dummy_frame->id.id = (*dummy_id);
  66.   dummy_frame->id.ptid = ptid;
  67.   dummy_frame->next = dummy_frame_stack;
  68.   dummy_frame_stack = dummy_frame;
  69. }

  70. /* Remove *DUMMY_PTR from the dummy frame stack.  */

  71. static void
  72. remove_dummy_frame (struct dummy_frame **dummy_ptr)
  73. {
  74.   struct dummy_frame *dummy = *dummy_ptr;

  75.   *dummy_ptr = dummy->next;
  76.   discard_infcall_suspend_state (dummy->caller_state);
  77.   xfree (dummy);
  78. }

  79. /* Delete any breakpoint B which is a momentary breakpoint for return from
  80.    inferior call matching DUMMY_VOIDP.  */

  81. static int
  82. pop_dummy_frame_bpt (struct breakpoint *b, void *dummy_voidp)
  83. {
  84.   struct dummy_frame *dummy = dummy_voidp;

  85.   if (b->thread == pid_to_thread_id (dummy->id.ptid)
  86.       && b->disposition == disp_del && frame_id_eq (b->frame_id, dummy->id.id))
  87.     {
  88.       while (b->related_breakpoint != b)
  89.         delete_breakpoint (b->related_breakpoint);

  90.       delete_breakpoint (b);

  91.       /* Stop the traversal.  */
  92.       return 1;
  93.     }

  94.   /* Continue the traversal.  */
  95.   return 0;
  96. }

  97. /* Pop *DUMMY_PTR, restoring program state to that before the
  98.    frame was created.  */

  99. static void
  100. pop_dummy_frame (struct dummy_frame **dummy_ptr)
  101. {
  102.   struct dummy_frame *dummy = *dummy_ptr;

  103.   gdb_assert (ptid_equal (dummy->id.ptid, inferior_ptid));

  104.   if (dummy->dtor != NULL)
  105.     dummy->dtor (dummy->dtor_data);

  106.   restore_infcall_suspend_state (dummy->caller_state);

  107.   iterate_over_breakpoints (pop_dummy_frame_bpt, dummy);

  108.   /* restore_infcall_control_state frees inf_state,
  109.      all that remains is to pop *dummy_ptr.  */
  110.   *dummy_ptr = dummy->next;
  111.   xfree (dummy);

  112.   /* We've made right mess of GDB's local state, just discard
  113.      everything.  */
  114.   reinit_frame_cache ();
  115. }

  116. /* Look up DUMMY_ID.
  117.    Return NULL if not found.  */

  118. static struct dummy_frame **
  119. lookup_dummy_frame (struct dummy_frame_id *dummy_id)
  120. {
  121.   struct dummy_frame **dp;

  122.   for (dp = &dummy_frame_stack; *dp != NULL; dp = &(*dp)->next)
  123.     {
  124.       if (dummy_frame_id_eq (&(*dp)->id, dummy_id))
  125.         return dp;
  126.     }

  127.   return NULL;
  128. }

  129. /* Find the dummy frame by DUMMY_ID and PTID, and pop it, restoring
  130.    program state to that before the frame was created.
  131.    On return reinit_frame_cache has been called.
  132.    If the frame isn't found, flag an internal error.  */

  133. void
  134. dummy_frame_pop (struct frame_id dummy_id, ptid_t ptid)
  135. {
  136.   struct dummy_frame **dp;
  137.   struct dummy_frame_id id = { dummy_id, ptid };

  138.   dp = lookup_dummy_frame (&id);
  139.   gdb_assert (dp != NULL);

  140.   pop_dummy_frame (dp);
  141. }

  142. /* Find the dummy frame by DUMMY_ID and PTID and drop it.  Do nothing
  143.    if it is not found.  Do not restore its state into inferior, just
  144.    free its memory.  */

  145. void
  146. dummy_frame_discard (struct frame_id dummy_id, ptid_t ptid)
  147. {
  148.   struct dummy_frame **dp;
  149.   struct dummy_frame_id id = { dummy_id, ptid };

  150.   dp = lookup_dummy_frame (&id);
  151.   if (dp)
  152.     remove_dummy_frame (dp);
  153. }

  154. /* See dummy-frame.h.  */

  155. void
  156. register_dummy_frame_dtor (struct frame_id dummy_id, ptid_t ptid,
  157.                            dummy_frame_dtor_ftype *dtor, void *dtor_data)
  158. {
  159.   struct dummy_frame_id id = { dummy_id, ptid };
  160.   struct dummy_frame **dp, *d;

  161.   dp = lookup_dummy_frame (&id);
  162.   gdb_assert (dp != NULL);
  163.   d = *dp;
  164.   gdb_assert (d->dtor == NULL);
  165.   d->dtor = dtor;
  166.   d->dtor_data = dtor_data;
  167. }

  168. /* See dummy-frame.h.  */

  169. int
  170. find_dummy_frame_dtor (dummy_frame_dtor_ftype *dtor, void *dtor_data)
  171. {
  172.   struct dummy_frame *d;

  173.   for (d = dummy_frame_stack; d != NULL; d = d->next)
  174.     if (d->dtor == dtor && d->dtor_data == dtor_data)
  175.       return 1;
  176.   return 0;
  177. }

  178. /* There may be stale dummy frames, perhaps left over from when an uncaught
  179.    longjmp took us out of a function that was called by the debugger.  Clean
  180.    them up at least once whenever we start a new inferior.  */

  181. static void
  182. cleanup_dummy_frames (struct target_ops *target, int from_tty)
  183. {
  184.   while (dummy_frame_stack != NULL)
  185.     remove_dummy_frame (&dummy_frame_stack);
  186. }

  187. /* Return the dummy frame cache, it contains both the ID, and a
  188.    pointer to the regcache.  */
  189. struct dummy_frame_cache
  190. {
  191.   struct frame_id this_id;
  192.   struct regcache *prev_regcache;
  193. };

  194. static int
  195. dummy_frame_sniffer (const struct frame_unwind *self,
  196.                      struct frame_info *this_frame,
  197.                      void **this_prologue_cache)
  198. {
  199.   /* When unwinding a normal frame, the stack structure is determined
  200.      by analyzing the frame's function's code (be it using brute force
  201.      prologue analysis, or the dwarf2 CFI).  In the case of a dummy
  202.      frame, that simply isn't possible.  The PC is either the program
  203.      entry point, or some random address on the stack.  Trying to use
  204.      that PC to apply standard frame ID unwind techniques is just
  205.      asking for trouble.  */

  206.   /* Don't bother unless there is at least one dummy frame.  */
  207.   if (dummy_frame_stack != NULL)
  208.     {
  209.       struct dummy_frame *dummyframe;
  210.       /* Use an architecture specific method to extract this frame's
  211.          dummy ID, assuming it is a dummy frame.  */
  212.       struct frame_id this_id
  213.         = gdbarch_dummy_id (get_frame_arch (this_frame), this_frame);
  214.       struct dummy_frame_id dummy_id = { this_id, inferior_ptid };

  215.       /* Use that ID to find the corresponding cache entry.  */
  216.       for (dummyframe = dummy_frame_stack;
  217.            dummyframe != NULL;
  218.            dummyframe = dummyframe->next)
  219.         {
  220.           if (dummy_frame_id_eq (&dummyframe->id, &dummy_id))
  221.             {
  222.               struct dummy_frame_cache *cache;

  223.               cache = FRAME_OBSTACK_ZALLOC (struct dummy_frame_cache);
  224.               cache->prev_regcache = get_infcall_suspend_state_regcache
  225.                                                    (dummyframe->caller_state);
  226.               cache->this_id = this_id;
  227.               (*this_prologue_cache) = cache;
  228.               return 1;
  229.             }
  230.         }
  231.     }
  232.   return 0;
  233. }

  234. /* Given a call-dummy dummy-frame, return the registers.  Here the
  235.    register value is taken from the local copy of the register buffer.  */

  236. static struct value *
  237. dummy_frame_prev_register (struct frame_info *this_frame,
  238.                            void **this_prologue_cache,
  239.                            int regnum)
  240. {
  241.   struct dummy_frame_cache *cache = (*this_prologue_cache);
  242.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  243.   struct value *reg_val;

  244.   /* The dummy-frame sniffer always fills in the cache.  */
  245.   gdb_assert (cache != NULL);

  246.   /* Describe the register's location.  Generic dummy frames always
  247.      have the register value in an ``expression''.  */
  248.   reg_val = value_zero (register_type (gdbarch, regnum), not_lval);

  249.   /* Use the regcache_cooked_read() method so that it, on the fly,
  250.      constructs either a raw or pseudo register from the raw
  251.      register cache.  */
  252.   regcache_cooked_read (cache->prev_regcache, regnum,
  253.                         value_contents_writeable (reg_val));
  254.   return reg_val;
  255. }

  256. /* Assuming that THIS_FRAME is a dummy, return its ID.  That ID is
  257.    determined by examining the NEXT frame's unwound registers using
  258.    the method dummy_id().  As a side effect, THIS dummy frame's
  259.    dummy cache is located and saved in THIS_PROLOGUE_CACHE.  */

  260. static void
  261. dummy_frame_this_id (struct frame_info *this_frame,
  262.                      void **this_prologue_cache,
  263.                      struct frame_id *this_id)
  264. {
  265.   /* The dummy-frame sniffer always fills in the cache.  */
  266.   struct dummy_frame_cache *cache = (*this_prologue_cache);

  267.   gdb_assert (cache != NULL);
  268.   (*this_id) = cache->this_id;
  269. }

  270. const struct frame_unwind dummy_frame_unwind =
  271. {
  272.   DUMMY_FRAME,
  273.   default_frame_unwind_stop_reason,
  274.   dummy_frame_this_id,
  275.   dummy_frame_prev_register,
  276.   NULL,
  277.   dummy_frame_sniffer,
  278. };

  279. static void
  280. fprint_dummy_frames (struct ui_file *file)
  281. {
  282.   struct dummy_frame *s;

  283.   for (s = dummy_frame_stack; s != NULL; s = s->next)
  284.     {
  285.       gdb_print_host_address (s, file);
  286.       fprintf_unfiltered (file, ":");
  287.       fprintf_unfiltered (file, " id=");
  288.       fprint_frame_id (file, s->id.id);
  289.       fprintf_unfiltered (file, ", ptid=%s",
  290.                           target_pid_to_str (s->id.ptid));
  291.       fprintf_unfiltered (file, "\n");
  292.     }
  293. }

  294. static void
  295. maintenance_print_dummy_frames (char *args, int from_tty)
  296. {
  297.   if (args == NULL)
  298.     fprint_dummy_frames (gdb_stdout);
  299.   else
  300.     {
  301.       struct cleanup *cleanups;
  302.       struct ui_file *file = gdb_fopen (args, "w");

  303.       if (file == NULL)
  304.         perror_with_name (_("maintenance print dummy-frames"));
  305.       cleanups = make_cleanup_ui_file_delete (file);
  306.       fprint_dummy_frames (file);
  307.       do_cleanups (cleanups);
  308.     }
  309. }

  310. extern void _initialize_dummy_frame (void);

  311. void
  312. _initialize_dummy_frame (void)
  313. {
  314.   add_cmd ("dummy-frames", class_maintenance, maintenance_print_dummy_frames,
  315.            _("Print the contents of the internal dummy-frame stack."),
  316.            &maintenanceprintlist);

  317.   observer_attach_inferior_created (cleanup_dummy_frames);
  318. }