【问题标题】:Problems debugging with gdb使用 gdb 调试的问题
【发布时间】:2021-05-05 20:24:02
【问题描述】:

我有一个简单的hello world程序(不管是什么程序结果都是一样的)。我用 -g 标志编译程序。我运行它运行良好的可执行文件,然后我尝试调试它并且 8/10 次我得到一个 segfault 错误,gdb 找不到源文件。

首先这不是这个的重复:Include source code of malloc.c in gdb?,没有任何东西可以解决我的问题。

我说 10 次中有 8 次是因为有时它按预期工作得很好,然后又回到错误。

当我运行实际程序时,我没有遇到分段错误。没有错误,只有在我使用gnu调试器时才会出现错误,通常是这样说的:

Breakpoint 1, main () at hello.cpp:4
4               std::cout << "hello world" << std::endl;
(gdb) n

Program received signal SIGSEGV, Segmentation fault.
strlen () at ../sysdeps/aarch64/strlen.S:94
94      ../sysdeps/aarch64/strlen.S: No such file or directory.
(gdb) bt 
#0  strlen () at ../sysdeps/aarch64/strlen.S:94
#1  0x0000007ff7f4ee58 in std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) () from /usr/lib/aarch64-linux-gnu/libstdc++.so.6
#2  0x0000005555555a00 in main () at hello.cpp:4

代码:(一个基本的 hello world 程序)

#include <iostream>
  
int main () {
        std::cout << "hello world" << std::endl;
}

这可能是我的系统故障或 gdb 问题。我正在使用 Chromeos arm64 拱门。它支持 linux (debian Buster)。 gdb 版本:GNU gdb (Debian 8.2.1-2+b3) 8.2.1。

如果我确实加载了一个源文件(使用 dir 命令),它恰好是一些 C 代码内部甚至是程序集,我仍然遇到分段错误,这里我在加载命令处遇到分段错误:

GNU gdb (Debian 8.2.1-2+b3) 8.2.1
Copyright (C) 2018 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 "aarch64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from helloCpp...done.
(gdb) dir /usr/src/glibc/glibc-2.28/sysdeps/aarch64/
Source directories searched: /usr/src/glibc/glibc-2.28/sysdeps/aarch64:$cdir:$cwd
(gdb) b main
Breakpoint 1 at 0x9ec: file hello.cpp, line 4.
(gdb) r
Starting program: /home/name/helloCpp 

Breakpoint 1, main () at hello.cpp:4
4               std::cout << "hello world" << std::endl;
(gdb) n

Program received signal SIGSEGV, Segmentation fault.
strlen () at ../sysdeps/aarch64/strlen.S:94
94              ldp     data1, data2, [srcin]
(gdb) bt 
#0  strlen () at ../sysdeps/aarch64/strlen.S:94
#1  0x0000007ff7f4ee58 in std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) () from /usr/lib/aarch64-linux-gnu/libstdc++.so.6
#2  0x0000005555555a00 in main () at hello.cpp:4
(gdb)

在 C 和 C++ 中运行实际程序可以正常工作:

name@penguin:~$ ./helloCpp
hello world
name@penguin:~$ ./helloC
hello world

【问题讨论】:

  • 请包含以“此 GDB 已配置为”开头的行
  • 如果你在 gdb 中输入 c 而不是 n,你还会遇到段错误吗?
  • 老实说,我不知道这是否相关,但在“sysdeps/aarch64/strlen .S" 已针对 glibc-2.32 重写为“与 Arm MTE 兼容”(有关详细信息,请参阅commitdiff)。
  • 您的程序所做的一切都不应该崩溃 系统。仅对您的程序进行段错误。如果您不指定 any -O,则它等效于 -O0。如果您的系统在重新启动后正常工作,则可能是侥幸。与您的程序无关的东西使系统处于“有趣的状态”。尝试您的程序 [with n] 几次,看看问题是否再次出现 - 否则,别担心 - 开心就好。
  • 我不熟悉 chromeos 拱门。我对其他手臂弓有一些经验(例如 raspberry pi 和 nVidia jetson)。您可能想尝试另一个手臂系统进行比较。 arm [holdings] 提供 CPU/arch 的固件,但问题可能出在芯片供应商添加的额外硬件逻辑上(例如,硬件或平台特定操作系统代码中的错误)。或者,手臂弓的版本(例如 cortex-v8)有一个已知的勘误表。

标签: c++ c linux arm gdb


【解决方案1】:

也许你的问题是你没有 返回0; 在程序的最后一行。我有一个想法 返回0; C 不需要,但 C++ 需要。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2017-10-10
    • 2010-12-09
    • 2019-09-30
    • 2013-11-20
    相关资源
    最近更新 更多