zoukankan      html  css  js  c++  java
  • 解决Linux c语言运行时候“段错误 (核心已转储)”问题-采用gdb 解决

    编译没有警告,没有错误,运行就打印 段错误 (核心已转储)

    网上找了一下,都是各种问题,都推荐用gdb 调试解决,咱也来趁机学习gdb一下。
     

    gcc+gdb)输入命令行 运行

    sudo apt-get install build-essential

    build-essential包含gcc和gdb等工具,是C语言的开发包。

    安装完了可以执行

    一般来说GDB主要调试的是C/C++的程序。要调试C/C
    ++的程序,首先在编译时,我们必须要把调试信息加到可执行文件中。使用编译器(cc/
    gcc/g++)的 -g 参数可以做到这一点。

    进程意外退出会在当前目录下产生‘code’文件或形如‘core.数字’的文件比如‘core.1234’
    使用命令
    gdb 运行程序名 core或core.数字
    进入gdb然后使用bt命令
    可以查看进程意外退出前函数调用的堆栈,内容为从上到下列出对应从里层到外层的函数调用历史。
    如果进程意外退出不产生core文件

    运行 ulimit -c unlimited   后再运行编译出的可执行程序,就可以产生core 文件

    gdb <program> core
    用gdb同时调试一个运行程序和core文件,core是程序非法执行後core dump
    後产生的文件。

    然后再使用bt 命令就可以看出程序问题在哪里了,gdb真是好东西。

    gdb a.out core
    GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
    Copyright (C) 2014 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 "x86_64-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 a.out...done.
    [New LWP 1537]
    Core was generated by `./a.out'.
    Program terminated with signal SIGSEGV, Segmentation fault.
    #0  _IO_fgets (buf=0x7ffedcceef10 "", n=256, fp=0x0) at iofgets.c:50
    50      iofgets.c: 没有那个文件或目录.
    (gdb) bt
    #0  _IO_fgets (buf=0x7ffedcceef10 "", n=256, fp=0x0) at iofgets.c:50
    #1  0x000000000040198a in load_properties (path=0x401ad2 "/system/etc/tdgnss.conf") at rwfile.c:519
    #2  0x00000000004019d9 in main () at rwfile.c:548

    原来是不存在这个路径/system/etc/tdgnss.conf ,这个路径是以前Android上的,现在在Ubuntu 就不存在了,改一下估计就ok 了。

    在Linux 上gdb 调试程序很方便,如果在arm 上怎么办呢?尤其是我这种没有usb 也没有网卡的Linux arm 机器上?

  • 相关阅读:
    Spring学习(一):理解IoC容器
    Spring学习(零):我们为什么要学习Spring
    git push提交报错,提示文件过大,且去掉大文件也报同样的错误
    There was a problem with the instance info replicator
    Eureka配置instanceId显示IP
    Cannot execute request on any known server或DiscoveryClient_UNKNOWN/DESKTOP-MQ8D0C9:8761
    Hexo优化 | 创建sitemap站点地图并向Google提交
    matlab与python读取tiff文件
    Visual studio中编译和使用libpng和zlib
    关于softmax、argmax、softargmax
  • 原文地址:https://www.cnblogs.com/dpf-learn/p/6224789.html
Copyright © 2011-2022 走看看