Aditya Jain
Aditya Jain

Reputation: 59

Using gdb and -D_GLIBCXX_DEBUG

I'm not able to understand how to use the gdb debugger. I'm trying to debug a very simple code

#include <iostream>
#include<vector>
int main() {
    int x;
    std::vector<int> arr(3);
    std::cout<<arr[5]; 
    std::cin>>x;
}

I get the following when I'm trying to run it on gdb

PS C:\Users\Public\My codes> g++ -g todebug.cpp -o todebug -D_GLIBCXX_DEBUG
PS C:\Users\Public\My codes> gdb .\todebug
GNU gdb (GDB) 7.6.1
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
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:
<http://www.gnu.org/software/gdb/bugs/>...
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<int, std::allocator<int> >;
    }
[Inferior 1 (process 22800) exited with code 03]
(gdb)

As you can see, It's not stopping at the SIGABRT. I believe something like this should show something like

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.

and allow me to print the backtrace. I tried to use

(gdb) handle SIGABRT stop print

and

(gdb) catch signal SIGABRT

Setting "catch throw" in the middle of the program doesn't help either

PS C:\Users\Public\My codes> g++ -g todebug.cpp -o todebug -D_GLIBCXX_DEBUG
PS C:\Users\Public\My codes> gdb .\todebug
GNU gdb (GDB) 7.6.1
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
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:
<http://www.gnu.org/software/gdb/bugs/>...
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<int> 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<int, std::allocator<int> >;
    }
[Inferior 1 (process 4004) exited with code 03]
(gdb) 

Compiling with gcc gives the following error

PS C:\Users\Public\My codes> 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<<(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>>(int&)'
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

But it still does not stop. What do I need to do to be able to print the backtrace?

Upvotes: 1

Views: 1322

Answers (1)

3CxEZiVlQ
3CxEZiVlQ

Reputation: 38257

There is no SIGABRT on Windows if exception thrown.

Run the following gdb command prior run

(gdb) catch throw
(gdb) run

Alternatively you can add catch throw to your .gdbinit.

If gdb command catch throw does not work, try to set pending breakpoint instead that command.

(gdb) break __cxa_throw

After gdb stopped on thrown exception use the command bt to look at a call stack.

Upvotes: 3

Related Questions