title: Using gdb and -D_GLIBCXX_DEBUG
body: <p>I'm not able to understand how to use the gdb debugger. I'm trying to debug a very simple code</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include&lt;vector&gt;
  int main() {
      int x;
      std::vector&lt;int&gt; arr(3);
      std::cout&lt;&lt;arr[5]; 
      std::cin&gt;&gt;x;
  }
  </code></pre>
  
  <p>I get the following when I'm trying to run it on gdb</p>
  
  <pre><code>PS C:\Users\Public\My codes&gt; g++ -g todebug.cpp -o todebug -D_GLIBCXX_DEBUG
  PS C:\Users\Public\My codes&gt; gdb .\todebug
  GNU gdb (GDB) 7.6.1
  Copyright (C) 2013 Free Software Foundation, Inc.
  License GPLv3+: GNU GPL version 3 or later &lt;http://gnu.org/licenses/gpl.html&gt;
  This is free software: you are free to change and redistribute it.
  There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
  and "show warranty" for details.
  This GDB was configured as "mingw32".
  For bug reporting instructions, please see:
  &lt;http://www.gnu.org/software/gdb/bugs/&gt;...
  Reading symbols from C:\Users\Public\My codes\todebug.exe...done.
  (gdb) run
  Starting program: C:\Users\Public\My codes/.\todebug.exe 
  [New Thread 22800.0x3318]
  [New Thread 22800.0x4340]
  [New Thread 22800.0x343c]
  [New Thread 22800.0x5a2c]
  c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\debug\vector:415:
  Error: attempt to subscript container with out-of-bounds index 5, but
  container only holds 3 elements.
  
  Objects involved in the operation:
      sequence "this" @ 0x0062FEEC {
        type = std::__debug::vector&lt;int, std::allocator&lt;int&gt; &gt;;
      }
  [Inferior 1 (process 22800) exited with code 03]
  (gdb)
  </code></pre>
  
  <p>As you can see, It's not stopping at the SIGABRT.
  I believe something like this should show something like</p>
  
  <pre><code>Program received signal SIGABRT, Aborted.
  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
  51      ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
  </code></pre>
  
  <p>and allow me to print the backtrace.
  I tried to use</p>
  
  <pre><code>(gdb) handle SIGABRT stop print
  </code></pre>
  
  <p>and</p>
  
  <pre><code>(gdb) catch signal SIGABRT
  </code></pre>
  
  <p>Setting "catch throw" in the middle of the program doesn't help either</p>
  
  <pre><code>PS C:\Users\Public\My codes&gt; g++ -g todebug.cpp -o todebug -D_GLIBCXX_DEBUG
  PS C:\Users\Public\My codes&gt; gdb .\todebug
  GNU gdb (GDB) 7.6.1
  Copyright (C) 2013 Free Software Foundation, Inc.
  License GPLv3+: GNU GPL version 3 or later &lt;http://gnu.org/licenses/gpl.html&gt;
  This is free software: you are free to change and redistribute it.
  There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
  and "show warranty" for details.
  This GDB was configured as "mingw32".
  For bug reporting instructions, please see:
  &lt;http://www.gnu.org/software/gdb/bugs/&gt;...
  Reading symbols from C:\Users\Public\My codes\todebug.exe...done.
  (gdb) b main
  Breakpoint 1 at 0x401477: file todebug.cpp, line 5.
  (gdb) run
  Starting program: C:\Users\Public\My codes/.\todebug.exe 
  [New Thread 4004.0x577c]
  [New Thread 4004.0x5674]
  [New Thread 4004.0x5808]
  [New Thread 4004.0x2748]
  
  Breakpoint 1, main () at todebug.cpp:5
  5           std::vector&lt;int&gt; arr(3);
  (gdb) catch throw
  Function "__cxa_throw" not defined.
  Catchpoint 2 (throw)
  (gdb) c
  Continuing.
  c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\debug\vector:415:
  Error: attempt to subscript container with out-of-bounds index 5, but
  container only holds 3 elements.
  
  Objects involved in the operation:
      sequence "this" @ 0x0062FEEC {
        type = std::__debug::vector&lt;int, std::allocator&lt;int&gt; &gt;;
      }
  [Inferior 1 (process 4004) exited with code 03]
  (gdb) 
  </code></pre>
  
  <p>Compiling with gcc gives the following error</p>
  
  <pre><code>PS C:\Users\Public\My codes&gt; gcc -ggdb todebug.cpp -o todebug -D_GLIBCXX_DEBUG
  C:\Users\RASHMI~1\AppData\Local\Temp\ccPza7pk.o: In function `main':
  C:\Users\Public\My codes/todebug.cpp:6: undefined reference to `std::cout'
  C:\Users\Public\My codes/todebug.cpp:6: undefined reference to `std::ostream::operator&lt;&lt;(int)'
  C:\Users\Public\My codes/todebug.cpp:7: undefined reference to `std::cin'
  C:\Users\Public\My codes/todebug.cpp:7: undefined reference to `std::istream::operator&gt;&gt;(int&amp;)'
  C:\Users\RASHMI~1\AppData\Local\Temp\ccPza7pk.o: In function `_tcf_0':
  c:/users/rashmi jain/downloads/mingw/lib/gcc/mingw32/6.3.0/include/c++/iostream:74: undefined reference to `std::ios_base::Init::~Init()'
  C:\Users\RASHMI~1\AppData\Local\Temp\ccPza7pk.o: In function `_static_initialization_and_destruction_0':
  c:/users/rashmi jain/downloads/mingw/lib/gcc/mingw32/6.3.0/include/c++/iostream:74: undefined reference to `std::ios_base::Init::Init()'
  C:\Users\RASHMI~1\AppData\Local\Temp\ccPza7pk.o: In function `ZN11__gnu_debug16_Error_formatter5_M_atEPKcj':
  c:/users/rashmi jain/downloads/mingw/lib/gcc/mingw32/6.3.0/include/c++/debug/formatter.h:485: undefined reference to `__cxa_guard_acquire'
  c:/users/rashmi jain/downloads/mingw/lib/gcc/mingw32/6.3.0/include/c++/debug/formatter.h:485: undefined reference to `__cxa_guard_release'
  C:\Users\RASHMI~1\AppData\Local\Temp\ccPza7pk.o: In function `ZN11__gnu_debug19_Safe_sequence_baseD2Ev':
  c:/users/rashmi jain/downloads/mingw/lib/gcc/mingw32/6.3.0/include/c++/debug/safe_base.h:212: undefined reference to `__gnu_debug::_Safe_sequence_base::_M_detach_all()'
  C:\Users\RASHMI~1\AppData\Local\Temp\ccPza7pk.o: In function `ZNSt7__debug6vectorIiSaIiEEixEj':
  c:/users/rashmi jain/downloads/mingw/lib/gcc/mingw32/6.3.0/include/c++/debug/vector:415: undefined reference to `__gnu_debug::_Error_formatter::_M_message(__gnu_debug::_Debug_msg_id) const'
  c:/users/rashmi jain/downloads/mingw/lib/gcc/mingw32/6.3.0/include/c++/debug/vector:415: undefined reference to `__gnu_debug::_Error_formatter::_M_error() const'
  C:\Users\RASHMI~1\AppData\Local\Temp\ccPza7pk.o: In function `ZN9__gnu_cxx13new_allocatorIiE10deallocateEPij':
  c:/users/rashmi jain/downloads/mingw/lib/gcc/mingw32/6.3.0/include/c++/ext/new_allocator.h:110: undefined reference to `operator delete(void*)'
  C:\Users\RASHMI~1\AppData\Local\Temp\ccPza7pk.o: In function `ZN9__gnu_cxx13new_allocatorIiE8allocateEjPKv':
  c:/users/rashmi jain/downloads/mingw/lib/gcc/mingw32/6.3.0/include/c++/ext/new_allocator.h:102: undefined reference to `std::__throw_bad_alloc()'
  c:/users/rashmi jain/downloads/mingw/lib/gcc/mingw32/6.3.0/include/c++/ext/new_allocator.h:104: undefined reference to `operator new(unsigned int)'
  C:\Users\RASHMI~1\AppData\Local\Temp\ccPza7pk.o:todebug.cpp:(.rdata$_ZTINSt7__debug6vectorIiSaIiEEE[__ZTINSt7__debug6vectorIiSaIiEEE]+0x0): undefined reference to `vtable for __cxxabiv1::__vmi_class_type_info'
  C:\Users\RASHMI~1\AppData\Local\Temp\ccPza7pk.o:todebug.cpp:(.rdata$_ZTINSt9__cxx19986vectorIiSaIiEEE[__ZTINSt9__cxx19986vectorIiSaIiEEE]+0x0): undefined reference to `vtable for __cxxabiv1::__vmi_class_type_info'
  C:\Users\RASHMI~1\AppData\Local\Temp\ccPza7pk.o:todebug.cpp:(.rdata$_ZTIN11__gnu_debug12_Safe_vectorINSt7__debug6vectorIiSaIiEEENSt9__cxx19986vectorIiS3_EEEE[__ZTIN11__gnu_debug12_Safe_vectorINSt7__debug6vectorIiSaIiEEENSt9__cxx19986vectorIiS3_EEEE]+0x0): undefined reference to `vtable for __cxxabiv1::__class_type_info'
  C:\Users\RASHMI~1\AppData\Local\Temp\ccPza7pk.o:todebug.cpp:(.rdata$_ZTIN11__gnu_debug15_Safe_containerINSt7__debug6vectorIiSaIiEEES3_NS_14_Safe_sequenceELb1EEE[__ZTIN11__gnu_debug15_Safe_containerINSt7__debug6vectorIiSaIiEEES3_NS_14_Safe_sequenceELb1EEE]+0x0): 
  undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
  C:\Users\RASHMI~1\AppData\Local\Temp\ccPza7pk.o:todebug.cpp:(.rdata$_ZTINSt9__cxx199812_Vector_baseIiSaIiEEE[__ZTINSt9__cxx199812_Vector_baseIiSaIiEEE]+0x0): undefined reference to `vtable for __cxxabiv1::__class_type_info'
  C:\Users\RASHMI~1\AppData\Local\Temp\ccPza7pk.o:todebug.cpp:(.rdata$_ZTIN11__gnu_debug14_Safe_sequenceINSt7__debug6vectorIiSaIiEEEEE[__ZTIN11__gnu_debug14_Safe_sequenceINSt7__debug6vectorIiSaIiEEEEE]+0x0): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
  C:\Users\RASHMI~1\AppData\Local\Temp\ccPza7pk.o:todebug.cpp:(.rdata$_ZTIN11__gnu_debug19_Safe_sequence_baseE[__ZTIN11__gnu_debug19_Safe_sequence_baseE]+0x0): undefined reference to `vtable for __cxxabiv1::__class_type_info'
  C:\Users\RASHMI~1\AppData\Local\Temp\ccPza7pk.o:todebug.cpp:(.eh_frame$_ZN11__gnu_debug19_Safe_sequence_baseD2Ev+0x13): undefined reference to `__gxx_personality_v0'
  C:\Users\RASHMI~1\AppData\Local\Temp\ccPza7pk.o:todebug.cpp:(.eh_frame+0x13): undefined reference to `__gxx_personality_v0'        
  C:\Users\RASHMI~1\AppData\Local\Temp\ccPza7pk.o:todebug.cpp:(.eh_frame$_ZNSt7__debug6vectorIiSaIiEEC1EjRKS1_+0x13): undefined reference to `__gxx_personality_v0'
  C:\Users\RASHMI~1\AppData\Local\Temp\ccPza7pk.o:todebug.cpp:(.eh_frame$_ZNSt9__cxx19986vectorIiSaIiEED2Ev+0x13): undefined reference to `__gxx_personality_v0'
  C:\Users\RASHMI~1\AppData\Local\Temp\ccPza7pk.o:todebug.cpp:(.eh_frame$_ZNSt7__debug6vectorIiSaIiEEixEj+0x13): undefined reference 
  to `__gxx_personality_v0'
  C:\Users\RASHMI~1\AppData\Local\Temp\ccPza7pk.o:todebug.cpp:(.eh_frame$_ZNSt9__cxx19986vectorIiSaIiEEC2EjRKS1_+0x13): more undefined references to `__gxx_personality_v0' follow
  collect2.exe: error: ld returned 1 exit status
  </code></pre>
  
  <p>But it still does not stop. What do I need to do to be able to print the backtrace?</p>