Home | History | Annotate | Line # | Download | only in gdbsupport
      1 /* Temporarily install an alternate signal stack
      2 
      3    Copyright (C) 2019-2024 Free Software Foundation, Inc.
      4 
      5    This file is part of GDB.
      6 
      7    This program is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19 
     20 #ifndef GDBSUPPORT_ALT_STACK_H
     21 #define GDBSUPPORT_ALT_STACK_H
     22 
     23 #include <signal.h>
     24 
     25 namespace gdb
     26 {
     27 
     28 /* Try to set up an alternate signal stack for SIGSEGV handlers.
     29    This allows us to handle SIGSEGV signals generated when the
     30    normal process stack is exhausted.  If this stack is not set
     31    up (sigaltstack is unavailable or fails) and a SIGSEGV is
     32    generated when the normal stack is exhausted then the program
     33    will behave as though no SIGSEGV handler was installed.  */
     34 class alternate_signal_stack
     35 {
     36 public:
     37   alternate_signal_stack ()
     38   {
     39 #ifdef HAVE_SIGALTSTACK
     40     m_stack.reset ((char *) xmalloc (SIGSTKSZ));
     41 
     42     stack_t stack;
     43     stack.ss_sp = m_stack.get ();
     44     stack.ss_size = SIGSTKSZ;
     45     stack.ss_flags = 0;
     46 
     47     sigaltstack (&stack, &m_old_stack);
     48 #endif
     49   }
     50 
     51   ~alternate_signal_stack ()
     52   {
     53 #ifdef HAVE_SIGALTSTACK
     54     sigaltstack (&m_old_stack, nullptr);
     55 #endif
     56   }
     57 
     58   DISABLE_COPY_AND_ASSIGN (alternate_signal_stack);
     59 
     60 private:
     61 
     62 #ifdef HAVE_SIGALTSTACK
     63   gdb::unique_xmalloc_ptr<char> m_stack;
     64   stack_t m_old_stack;
     65 #endif
     66 };
     67 
     68 }
     69 
     70 #endif /* GDBSUPPORT_ALT_STACK_H */
     71