binutils-gdb/gdb/frame-info.h
Kévin Le Gouguec 995a34b177 Guard against frame.c destructors running before frame-info.c's
On x86_64-windows, since 04e2ac7b2a, we observe this internal error:

  [...]/gdbsupport/intrusive_list.h:458: internal-error: erase_element:
  Assertion `elem_node->prev != INTRUSIVE_LIST_UNLINKED_VALUE' failed.

Breaking in the destructors for intrusive_list and frame_info_ptr shows that
in this configuration, the destructors for frame.c's statically-stored
objects are run before frame-info.c's:

  Thread 1 hit Breakpoint 7, intrusive_list<frame_info_ptr,
  intrusive_base_node<frame_info_ptr> >::~intrusive_list (this=0x7ff69c418c90
  <frame_info_ptr::frame_list>, __in_chrg=<optimized out>)
  [...]/../gdbsupport/intrusive_list.h:250
  250	    clear ();
  (gdb) bt
  #0  intrusive_list<frame_info_ptr, intrusive_base_node<frame_info_ptr> >
      ::~intrusive_list (this=0x7ff69c418c90 <frame_info_ptr::frame_list>,
      __in_chrg=<optimized out>) [...]/../gdbsupport/intrusive_list.h:250
  #1  0x00007ff69b78edba in __tcf_1 () [...]/frame-info.c:27
  #2  0x00007ff9c457aa9f in msvcrt!_initterm_e ()
      from C:\Windows\System32\msvcrt.dll
  #3  0x00007ff69b8246a6 in captured_main_1 (context=0x5ffe00)
      [...]/main.c:1111
  #4  0x00007ff69b825149 in captured_main (data=0x5ffe00) [...]/main.c:1320
  #5  0x00007ff69b8251b1 in gdb_main (args=0x5ffe00) [...]/main.c:1345
  #6  0x00007ff69b5d1730 in main (argc=2, argv=0x751730) [...]/gdb.c:32
  (gdb) c
  Continuing.

  Thread 1 hit Breakpoint 8, frame_info_ptr::~frame_info_ptr
  (this=0x7ff69c418e20 <selected_frame>, __in_chrg=<optimized out>)
  [...]/frame-info.h:74
  74	    if (is_linked ())
  (gdb) bt
  #0  frame_info_ptr::~frame_info_ptr (this=0x7ff69c418e20 <selected_frame>,
      __in_chrg=<optimized out>) [...]/frame-info.h:74
  #1  0x00007ff69b79a643 in __tcf_1 () [...]/frame.c:1675
  #2  0x00007ff9c457aa9f in msvcrt!_initterm_e () from
      C:\Windows\System32\msvcrt.dll
  #3  0x00007ff69b8246a6 in captured_main_1 (context=0x5ffe00)
      [...]/main.c:1111
  #4  0x00007ff69b825149 in captured_main (data=0x5ffe00) [...]/main.c:1320
  #5  0x00007ff69b8251b1 in gdb_main (args=0x5ffe00) [...]/main.c:1345
  #6  0x00007ff69b5d1730 in main (argc=2, argv=0x751730) [...]/gdb.c:32

Approved-By: Simon Marchi <simon.marchi@efficios.com>
2022-11-17 00:21:06 +01:00

212 lines
5.4 KiB
C++

/* Frame info pointer
Copyright (C) 2022 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef GDB_FRAME_INFO_H
#define GDB_FRAME_INFO_H
#include "gdbsupport/intrusive_list.h"
#include "frame-id.h"
struct frame_info;
/* A wrapper for "frame_info *". frame_info objects are invalidated
whenever reinit_frame_cache is called. This class arranges to
invalidate the pointer when appropriate. This is done to help
detect a GDB bug that was relatively common.
A small amount of code must still operate on raw pointers, so a
"get" method is provided. However, you should normally not use
this in new code. */
class frame_info_ptr : public intrusive_list_node<frame_info_ptr>
{
public:
/* Create a frame_info_ptr from a raw pointer. */
explicit frame_info_ptr (struct frame_info *ptr)
: m_ptr (ptr)
{
frame_list.push_back (*this);
}
/* Create a null frame_info_ptr. */
frame_info_ptr ()
{
frame_list.push_back (*this);
}
frame_info_ptr (std::nullptr_t)
{
frame_list.push_back (*this);
}
frame_info_ptr (const frame_info_ptr &other)
: m_ptr (other.m_ptr),
m_cached_id (other.m_cached_id),
m_cached_level (other.m_cached_level)
{
frame_list.push_back (*this);
}
frame_info_ptr (frame_info_ptr &&other)
: m_ptr (other.m_ptr),
m_cached_id (other.m_cached_id),
m_cached_level (other.m_cached_level)
{
other.m_ptr = nullptr;
other.m_cached_id = null_frame_id;
other.m_cached_level = invalid_level;
frame_list.push_back (*this);
}
~frame_info_ptr ()
{
/* If this node has static storage, it may be deleted after
frame_list. Attempting to erase ourselves would then trigger
internal errors, so make sure we are still linked first. */
if (is_linked ())
frame_list.erase (frame_list.iterator_to (*this));
}
frame_info_ptr &operator= (const frame_info_ptr &other)
{
m_ptr = other.m_ptr;
m_cached_id = other.m_cached_id;
m_cached_level = other.m_cached_level;
return *this;
}
frame_info_ptr &operator= (std::nullptr_t)
{
m_ptr = nullptr;
m_cached_id = null_frame_id;
m_cached_level = invalid_level;
return *this;
}
frame_info_ptr &operator= (frame_info_ptr &&other)
{
m_ptr = other.m_ptr;
m_cached_id = other.m_cached_id;
m_cached_level = other.m_cached_level;
other.m_ptr = nullptr;
other.m_cached_id = null_frame_id;
other.m_cached_level = invalid_level;
return *this;
}
frame_info *operator-> () const
{
return m_ptr;
}
/* Fetch the underlying pointer. Note that new code should
generally not use this -- avoid it if at all possible. */
frame_info *get () const
{
return m_ptr;
}
/* This exists for compatibility with pre-existing code that checked
a "frame_info *" using "!". */
bool operator! () const
{
return m_ptr == nullptr;
}
/* This exists for compatibility with pre-existing code that checked
a "frame_info *" like "if (ptr)". */
explicit operator bool () const
{
return m_ptr != nullptr;
}
/* Invalidate this pointer. */
void invalidate ()
{
m_ptr = nullptr;
}
/* Cache the frame_id that the pointer will use to reinflate. */
void prepare_reinflate ();
/* Use the cached frame_id to reinflate the pointer. */
void reinflate ();
private:
/* We sometimes need to construct frame_info_ptr objects around the
sentinel_frame, which has level -1. Therefore, make the invalid frame
level value -2. */
static constexpr int invalid_level = -2;
/* The underlying pointer. */
frame_info *m_ptr = nullptr;
/* The frame_id of the underlying pointer. */
frame_id m_cached_id = null_frame_id;
/* The frame level of the underlying pointer. */
int m_cached_level = invalid_level;
/* All frame_info_ptr objects are kept on an intrusive list.
This keeps their construction and destruction costs
reasonably small. */
static intrusive_list<frame_info_ptr> frame_list;
/* A friend so it can invalidate the pointers. */
friend void reinit_frame_cache ();
};
static inline bool
operator== (const frame_info *self, const frame_info_ptr &other)
{
return self == other.get ();
}
static inline bool
operator== (const frame_info_ptr &self, const frame_info_ptr &other)
{
return self.get () == other.get ();
}
static inline bool
operator== (const frame_info_ptr &self, const frame_info *other)
{
return self.get () == other;
}
static inline bool
operator!= (const frame_info *self, const frame_info_ptr &other)
{
return self != other.get ();
}
static inline bool
operator!= (const frame_info_ptr &self, const frame_info_ptr &other)
{
return self.get () != other.get ();
}
static inline bool
operator!= (const frame_info_ptr &self, const frame_info *other)
{
return self.get () != other;
}
#endif /* GDB_FRAME_INFO_H */