Использование gdb и -D_GLIBCXX_DEBUG - PullRequest
1 голос
/ 18 апреля 2020

Я не могу понять, как использовать отладчик GDB. Я пытаюсь отладить очень простой код

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

Я получаю следующее, когда пытаюсь запустить его на 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)

Как видите, он не останавливается в SIGABRT. Я считаю, что что-то вроде этого должно показать что-то вроде

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.

и позволить мне напечатать обратный след. Я пытался использовать

(gdb) handle SIGABRT stop print

и

(gdb) catch signal SIGABRT

Установка «броска броска» в середине программы тоже не помогает

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) 

Компиляция с g cc выдает следующую ошибку

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

но все равно не останавливается. Что мне нужно сделать, чтобы иметь возможность распечатать трассировку?

1 Ответ

2 голосов
/ 18 апреля 2020

SIGABRT отсутствует в Windows, если выброшено исключение.

Выполните следующую команду GDB до run

(gdb) catch throw
(gdb) run

В качестве альтернативы вы можете добавить catch throw в .gdbinit.

Если команда gdb catch throw не работает, попробуйте вместо этой команды установить ожидающую точку останова.

(gdb) break __cxa_throw

После того, как gdb остановился из-за возникшего исключения, используйте команду bt для просмотра в стеке вызовов.

...