Blame SOURCES/gdb-rhbz1225569-oom-killer-aarch64-frame-same-id-1of8.patch

7b26da
commit 1ec56e88aa9b052ab10b806d82fbdbc8d153d977
7b26da
Author: Pedro Alves <palves@redhat.com>
7b26da
Date:   Fri Nov 22 13:17:46 2013 +0000
7b26da
7b26da
    Eliminate dwarf2_frame_cache recursion, don't unwind from the dwarf2 sniffer (move dwarf2_tailcall_sniffer_first elsewhere).
7b26da
    
7b26da
    Two rationales, same patch.
7b26da
    
7b26da
    TL;DR 1:
7b26da
    
7b26da
     dwarf2_frame_cache recursion is evil.  dwarf2_frame_cache calls
7b26da
     dwarf2_tailcall_sniffer_first which then recurses into
7b26da
     dwarf2_frame_cache.
7b26da
    
7b26da
    TL;DR 2:
7b26da
    
7b26da
     An unwinder trying to unwind is evil.  dwarf2_frame_sniffer calls
7b26da
     dwarf2_frame_cache which calls dwarf2_tailcall_sniffer_first which
7b26da
     then tries to unwind the PC of the previous frame.
7b26da
    
7b26da
    Avoid all that by deferring dwarf2_tailcall_sniffer_first until it's
7b26da
    really necessary.
7b26da
    
7b26da
    Rationale 1
7b26da
    ===========
7b26da
    
7b26da
    A frame sniffer should not try to unwind, because that bypasses all
7b26da
    the validation checks done by get_prev_frame.  The UNWIND_SAME_ID
7b26da
    scenario is one such case where GDB is currently broken because (in
7b26da
    part) of this (the next patch adds a test that would fail without
7b26da
    this).
7b26da
    
7b26da
    GDB goes into an infinite loop in value_fetch_lazy, here:
7b26da
    
7b26da
          while (VALUE_LVAL (new_val) == lval_register && value_lazy (new_val))
7b26da
    	{
7b26da
    	  frame = frame_find_by_id (VALUE_FRAME_ID (new_val));
7b26da
    ...
7b26da
    	  new_val = get_frame_register_value (frame, regnum);
7b26da
    	}
7b26da
    
7b26da
    (top-gdb) bt
7b26da
    #0  value_fetch_lazy (val=0x11516d0) at ../../src/gdb/value.c:3510
7b26da
    #1  0x0000000000584bd8 in value_optimized_out (value=0x11516d0) at ../../src/gdb/value.c:1096
7b26da
    #2  0x00000000006fe7a1 in frame_register_unwind (frame=0x1492600, regnum=16, optimizedp=0x7fffffffcdec, unavailablep=0x7fffffffcde8, lvalp=0x7fffffffcdd8, addrp=
7b26da
        0x7fffffffcde0, realnump=0x7fffffffcddc, bufferp=0x7fffffffce10 "@\316\377\377\377\177") at ../../src/gdb/frame.c:940
7b26da
    #3  0x00000000006fea3a in frame_unwind_register (frame=0x1492600, regnum=16, buf=0x7fffffffce10 "@\316\377\377\377\177") at ../../src/gdb/frame.c:990
7b26da
    #4  0x0000000000473b9b in i386_unwind_pc (gdbarch=0xf54660, next_frame=0x1492600) at ../../src/gdb/i386-tdep.c:1771
7b26da
    #5  0x0000000000601dfa in gdbarch_unwind_pc (gdbarch=0xf54660, next_frame=0x1492600) at ../../src/gdb/gdbarch.c:2870
7b26da
    #6  0x0000000000693db5 in dwarf2_tailcall_sniffer_first (this_frame=0x1492600, tailcall_cachep=0x14926f0, entry_cfa_sp_offsetp=0x7fffffffcf00)
7b26da
        at ../../src/gdb/dwarf2-frame-tailcall.c:389
7b26da
    #7  0x0000000000690928 in dwarf2_frame_cache (this_frame=0x1492600, this_cache=0x1492618) at ../../src/gdb/dwarf2-frame.c:1245
7b26da
    #8  0x0000000000690f46 in dwarf2_frame_sniffer (self=0x8e4980, this_frame=0x1492600, this_cache=0x1492618) at ../../src/gdb/dwarf2-frame.c:1423
7b26da
    #9  0x000000000070203b in frame_unwind_find_by_frame (this_frame=0x1492600, this_cache=0x1492618) at ../../src/gdb/frame-unwind.c:112
7b26da
    #10 0x00000000006fd681 in get_frame_id (fi=0x1492600) at ../../src/gdb/frame.c:408
7b26da
    #11 0x00000000007006c2 in get_prev_frame_1 (this_frame=0xdc1860) at ../../src/gdb/frame.c:1826
7b26da
    #12 0x0000000000700b7a in get_prev_frame (this_frame=0xdc1860) at ../../src/gdb/frame.c:2056
7b26da
    #13 0x0000000000514588 in frame_info_to_frame_object (frame=0xdc1860) at ../../src/gdb/python/py-frame.c:322
7b26da
    #14 0x000000000051784c in bootstrap_python_frame_filters (frame=0xdc1860, frame_low=0, frame_high=-1) at ../../src/gdb/python/py-framefilter.c:1396
7b26da
    #15 0x0000000000517a6f in apply_frame_filter (frame=0xdc1860, flags=7, args_type=CLI_SCALAR_VALUES, out=0xed7a90, frame_low=0, frame_high=-1)
7b26da
        at ../../src/gdb/python/py-framefilter.c:1492
7b26da
    #16 0x00000000005e77b0 in backtrace_command_1 (count_exp=0x0, show_locals=0, no_filters=0, from_tty=1) at ../../src/gdb/stack.c:1777
7b26da
    #17 0x00000000005e7c0f in backtrace_command (arg=0x0, from_tty=1) at ../../src/gdb/stack.c:1891
7b26da
    #18 0x00000000004e37a7 in do_cfunc (c=0xda4fa0, args=0x0, from_tty=1) at ../../src/gdb/cli/cli-decode.c:107
7b26da
    #19 0x00000000004e683c in cmd_func (cmd=0xda4fa0, args=0x0, from_tty=1) at ../../src/gdb/cli/cli-decode.c:1882
7b26da
    #20 0x00000000006f35ed in execute_command (p=0xcc66c2 "", from_tty=1) at ../../src/gdb/top.c:468
7b26da
    #21 0x00000000005f8853 in command_handler (command=0xcc66c0 "bt") at ../../src/gdb/event-top.c:435
7b26da
    #22 0x00000000005f8e12 in command_line_handler (rl=0xfe05f0 "@") at ../../src/gdb/event-top.c:632
7b26da
    #23 0x000000000074d2c6 in rl_callback_read_char () at ../../src/readline/callback.c:220
7b26da
    #24 0x00000000005f8375 in rl_callback_read_char_wrapper (client_data=0x0) at ../../src/gdb/event-top.c:164
7b26da
    #25 0x00000000005f876a in stdin_event_handler (error=0, client_data=0x0) at ../../src/gdb/event-top.c:375
7b26da
    #26 0x00000000005f72fa in handle_file_event (data=...) at ../../src/gdb/event-loop.c:768
7b26da
    #27 0x00000000005f67a3 in process_event () at ../../src/gdb/event-loop.c:342
7b26da
    #28 0x00000000005f686a in gdb_do_one_event () at ../../src/gdb/event-loop.c:406
7b26da
    #29 0x00000000005f68bb in start_event_loop () at ../../src/gdb/event-loop.c:431
7b26da
    #30 0x00000000005f83a7 in cli_command_loop (data=0x0) at ../../src/gdb/event-top.c:179
7b26da
    #31 0x00000000005eeed3 in current_interp_command_loop () at ../../src/gdb/interps.c:327
7b26da
    #32 0x00000000005ef8ff in captured_command_loop (data=0x0) at ../../src/gdb/main.c:267
7b26da
    #33 0x00000000005ed2f6 in catch_errors (func=0x5ef8e4 <captured_command_loop>, func_args=0x0, errstring=0x8b6554 "", mask=RETURN_MASK_ALL)
7b26da
        at ../../src/gdb/exceptions.c:524
7b26da
    #34 0x00000000005f0d21 in captured_main (data=0x7fffffffd9e0) at ../../src/gdb/main.c:1067
7b26da
    #35 0x00000000005ed2f6 in catch_errors (func=0x5efb9b <captured_main>, func_args=0x7fffffffd9e0, errstring=0x8b6554 "", mask=RETURN_MASK_ALL)
7b26da
        at ../../src/gdb/exceptions.c:524
7b26da
    #36 0x00000000005f0d57 in gdb_main (args=0x7fffffffd9e0) at ../../src/gdb/main.c:1076
7b26da
    #37 0x000000000045bb6a in main (argc=4, argv=0x7fffffffdae8) at ../../src/gdb/gdb.c:34
7b26da
    (top-gdb)
7b26da
    
7b26da
    GDB is trying to unwind the PC register of the previous frame (frame
7b26da
    #5 above), starting from the frame being sniffed (the THIS frame).
7b26da
    But the THIS frame's unwinder says the PC of the previous frame is
7b26da
    actually the same as the previous's frame's next frame (which is the
7b26da
    same frame we started with, the THIS frame), therefore it returns an
7b26da
    lval_register lazy value with frame set to THIS frame.  And so the
7b26da
    value_fetch_lazy loop never ends.
7b26da
    
7b26da
    
7b26da
    Rationale 2
7b26da
    ===========
7b26da
    
7b26da
    As an experiment, I tried making dwarf2-frame.c:read_addr_from_reg use
7b26da
    address_from_register.  That caused a bunch of regressions, but it
7b26da
    actually took me a long while to figure out what was going on.  Turns
7b26da
    out dwarf2-frame.c:read_addr_from_reg is called while computing the
7b26da
    frame's CFA, from within dwarf2_frame_cache.  address_from_register
7b26da
    wants to create a register with frame_id set to the frame being
7b26da
    constructed.  To create the frame id, we again call dwarf2_frame_cache,
7b26da
    which given:
7b26da
    
7b26da
    static struct dwarf2_frame_cache *
7b26da
    dwarf2_frame_cache (struct frame_info *this_frame, void **this_cache)
7b26da
    {
7b26da
    ...
7b26da
      if (*this_cache)
7b26da
        return *this_cache;
7b26da
    
7b26da
    returns an incomplete object to the caller:
7b26da
    static void
7b26da
    dwarf2_frame_this_id (struct frame_info *this_frame, void **this_cache,
7b26da
    		      struct frame_id *this_id)
7b26da
    {
7b26da
      struct dwarf2_frame_cache *cache =
7b26da
        dwarf2_frame_cache (this_frame, this_cache);
7b26da
    ...
7b26da
     (*this_id) = frame_id_build (cache->cfa, get_frame_func (this_frame));
7b26da
    }
7b26da
    
7b26da
    As cache->cfa is still 0 (we were trying to compute it!), and
7b26da
    get_frame_id recalls this id from here on, we end up with a broken
7b26da
    frame id in recorded for this frame.  Later, when inspecting locals,
7b26da
    the dwarf machinery needs to know the selected frame's base, which
7b26da
    calls get_frame_base:
7b26da
    
7b26da
    CORE_ADDR
7b26da
    get_frame_base (struct frame_info *fi)
7b26da
    {
7b26da
      return get_frame_id (fi).stack_addr;
7b26da
    }
7b26da
    
7b26da
    which as seen above then returns 0 ...
7b26da
    
7b26da
    So I gave up using address_from_register.
7b26da
    
7b26da
    But, the pain of investigating this made me want to have GDB itself
7b26da
    assert that recursion never happens here.  So I wrote a patch to do
7b26da
    that.  But, it triggers on current mainline, because
7b26da
    dwarf2_tailcall_sniffer_first, called from dwarf2_frame_cache, unwinds
7b26da
    the this_frame.
7b26da
    
7b26da
    A sniffer shouldn't be trying to unwind, exactly because of this sort
7b26da
    of tricky issue.  The patch defers calling
7b26da
    dwarf2_tailcall_sniffer_first until it's really necessary, in
7b26da
    dwarf2_frame_prev_register (thus actually outside the sniffer path).
7b26da
    As this makes the call to dwarf2_frame_sniffer in dwarf2_frame_cache
7b26da
    unnecessary again, the patch removes that too.
7b26da
    
7b26da
    Tested on x86_64 Fedora 17.
7b26da
    
7b26da
    gdb/
7b26da
    2013-11-22  Pedro Alves  <palves@redhat.com>
7b26da
    
7b26da
    	PR 16155
7b26da
    	* dwarf2-frame.c (struct dwarf2_frame_cache)
7b26da
    	
7b26da
    	entry_cfa_sp_offset_p>: New fields.
7b26da
    	(dwarf2_frame_cache): Adjust to use the new cache fields instead
7b26da
    	of locals.  Don't call dwarf2_tailcall_sniffer_first here.
7b26da
    	(dwarf2_frame_prev_register): Call it here, but only once.
7b26da
7b26da
Index: gdb-7.6.1/gdb/dwarf2-frame.c
7b26da
===================================================================
7b26da
--- gdb-7.6.1.orig/gdb/dwarf2-frame.c
7b26da
+++ gdb-7.6.1/gdb/dwarf2-frame.c
7b26da
@@ -984,12 +984,22 @@ struct dwarf2_frame_cache
7b26da
   /* The .text offset.  */
7b26da
   CORE_ADDR text_offset;
7b26da
 
7b26da
+  /* True if we already checked whether this frame is the bottom frame
7b26da
+     of a virtual tail call frame chain.  */
7b26da
+  int checked_tailcall_bottom;
7b26da
+
7b26da
   /* If not NULL then this frame is the bottom frame of a TAILCALL_FRAME
7b26da
      sequence.  If NULL then it is a normal case with no TAILCALL_FRAME
7b26da
      involved.  Non-bottom frames of a virtual tail call frames chain use
7b26da
      dwarf2_tailcall_frame_unwind unwinder so this field does not apply for
7b26da
      them.  */
7b26da
   void *tailcall_cache;
7b26da
+
7b26da
+  /* The number of bytes to subtract from TAILCALL_FRAME frames frame
7b26da
+     base to get the SP, to simulate the return address pushed on the
7b26da
+     stack.  */
7b26da
+  LONGEST entry_cfa_sp_offset;
7b26da
+  int entry_cfa_sp_offset_p;
7b26da
 };
7b26da
 
7b26da
 /* A cleanup that sets a pointer to NULL.  */
7b26da
@@ -1014,8 +1024,6 @@ dwarf2_frame_cache (struct frame_info *t
7b26da
   struct dwarf2_fde *fde;
7b26da
   volatile struct gdb_exception ex;
7b26da
   CORE_ADDR entry_pc;
7b26da
-  LONGEST entry_cfa_sp_offset;
7b26da
-  int entry_cfa_sp_offset_p = 0;
7b26da
   const gdb_byte *instr;
7b26da
 
7b26da
   if (*this_cache)
7b26da
@@ -1080,8 +1088,8 @@ dwarf2_frame_cache (struct frame_info *t
7b26da
 	  && (gdbarch_dwarf2_reg_to_regnum (gdbarch, fs->regs.cfa_reg)
7b26da
 	      == gdbarch_sp_regnum (gdbarch)))
7b26da
 	{
7b26da
-	  entry_cfa_sp_offset = fs->regs.cfa_offset;
7b26da
-	  entry_cfa_sp_offset_p = 1;
7b26da
+	  cache->entry_cfa_sp_offset = fs->regs.cfa_offset;
7b26da
+	  cache->entry_cfa_sp_offset_p = 1;
7b26da
 	}
7b26da
     }
7b26da
   else
7b26da
@@ -1230,13 +1238,6 @@ incomplete CFI data; unspecified registe
7b26da
     cache->undefined_retaddr = 1;
7b26da
 
7b26da
   do_cleanups (old_chain);
7b26da
-
7b26da
-  /* Try to find a virtual tail call frames chain with bottom (callee) frame
7b26da
-     starting at THIS_FRAME.  */
7b26da
-  dwarf2_tailcall_sniffer_first (this_frame, &cache->tailcall_cache,
7b26da
-				 (entry_cfa_sp_offset_p
7b26da
-				  ? &entry_cfa_sp_offset : NULL));
7b26da
-
7b26da
   discard_cleanups (reset_cache_cleanup);
7b26da
   return cache;
7b26da
 }
7b26da
@@ -1282,6 +1283,16 @@ dwarf2_frame_prev_register (struct frame
7b26da
   CORE_ADDR addr;
7b26da
   int realnum;
7b26da
 
7b26da
+  /* Check whether THIS_FRAME is the bottom frame of a virtual tail
7b26da
+     call frame chain.  */
7b26da
+  if (!cache->checked_tailcall_bottom)
7b26da
+    {
7b26da
+      cache->checked_tailcall_bottom = 1;
7b26da
+      dwarf2_tailcall_sniffer_first (this_frame, &cache->tailcall_cache,
7b26da
+				     (cache->entry_cfa_sp_offset_p
7b26da
+				      ? &cache->entry_cfa_sp_offset : NULL));
7b26da
+    }
7b26da
+
7b26da
   /* Non-bottom frames of a virtual tail call frames chain use
7b26da
      dwarf2_tailcall_frame_unwind unwinder so this code does not apply for
7b26da
      them.  If dwarf2_tailcall_prev_register_first does not have specific value
7b26da
@@ -1408,10 +1419,6 @@ dwarf2_frame_sniffer (const struct frame
7b26da
   if (self->type != NORMAL_FRAME)
7b26da
     return 0;
7b26da
 
7b26da
-  /* Preinitializa the cache so that TAILCALL_FRAME can find the record by
7b26da
-     dwarf2_tailcall_sniffer_first.  */
7b26da
-  dwarf2_frame_cache (this_frame, this_cache);
7b26da
-
7b26da
   return 1;
7b26da
 }
7b26da