• 段错误解决方法


    1、添加打印信息确定问题具体位置。

    2、使用gdb调试,确定问题点。

    3、使用core dump确定问题位置。

      在main函数开头处添加以下代码,程序运行后,会在程序所在文件夹下生成core.xxx文件,然后使用此命令行:gdb ./myProgram core.xxx

      这种办法和gdb调试是一样的。

     1 #include <unistd.h>
     2 #include <sys/time.h>
     3 #include <sys/resource.h>
     4 
     5     struct rlimit rlmt;
     6     if (getrlimit(RLIMIT_CORE, &rlmt) == -1) {
     7         return -1; 
     8     }   
     9     printf("Before set rlimit CORE dump current is:%d, max is:%d
    ", (int)rlmt.rlim_cur, (int)rlmt.rlim_max);
    10 
    11     rlmt.rlim_cur = (rlim_t)CORE_SIZE;
    12     rlmt.rlim_max  = (rlim_t)CORE_SIZE;
    13 
    14     if (setrlimit(RLIMIT_CORE, &rlmt) == -1) {
    15         return -1; 
    16     }   
    17 
    18     if (getrlimit(RLIMIT_CORE, &rlmt) == -1) {
    19         return -1; 
    20     }   
    21     printf("After set rlimit CORE dump current is:%d, max is:%d
    ", (int)rlmt.rlim_cur, (int)rlmt.rlim_max);

    4、nm查看可执行程序中的符号,使用objdump反汇编可执行程序,通过nm得到地址,在汇编中找到相应的函数名称,确定问题点。

    5、使用dmesg命令查看信息,但是获得的信息用处不大(待深究)。

    6、使用catchsegv捕捉段错误信息,用处也不大(待深究)。

    7、使用mtrace机制,查看内存申请释放情况,从中判断出哪里的内存使用出现的问题。这篇介绍的更加详细。

    8、使用PC-lint(windows)、splint(Linux)工具检查代码。

      安装splint:

    1 wget http://www.splint.org/downloads/splint-3.1.2.src.tgz
    2 yum install flex
    3 yum install flex-devel
    4 tar xzf splint-3.1.2.src.tgz
    5 cd splint-3.1.2
    6 ./configure --prefix=/root/splint/soft
    7 make
    8 make install

     9、如果以上方法都不能够找到问题所在,可以排查一下代码,查看一下变量的初始化,尤其是结构体变量的初始化,像这种memset(struct_xxx, '', sizeof(struct_name))语句重点排查。

  • 相关阅读:
    网络配置br0 brtcl
    vlan pvid vid access口 trunk口
    虚拟化设置
    debian配置网络
    ps -a,job,netstat,daemons
    windows用命令行查看硬件信息
    查看linux硬件信息
    ruby send respond_to
    打开Win7休眠模式和离开模式的方法
    vim手册
  • 原文地址:https://www.cnblogs.com/coolYuan/p/14267720.html
Copyright © 2020-2023  润新知