Segmentation fault (core dumped),无法明了崩溃原因

软件和网站开发以及相关技术探讨
回复
qq_771365380
帖子: 11
注册时间: 2017-04-08 22:13
系统: Ubuntu16.04

Segmentation fault (core dumped),无法明了崩溃原因

#1

帖子 qq_771365380 » 2018-02-24 7:23

Segmentation fault (core dumped),无法明了崩溃原因

错误描述:部分代码莫名重复调用导致 Segmentation fault (core dumped)

文件一:

代码: 全选

  {
    std::string java_path = this->jdk_path + "/bin/java" + " -version";

    std::string str = utils::exec(java_path); //本调用会出现异常

    //下面注释中的代码可以正常执行
    // char *cmd = (char*)java_path.c_str();
    // std::string str = utils::exec(cmd);

    cout << str << endl;
  }
文件二:

代码: 全选

#ifndef UTIL_H
#define UTIL_H
#include <sys/stat.h>

namespace silly_monkey
{
  namespace utils
  {
    //判断文件是否存在
    inline bool file_exists (const std::string& name) {
      struct stat buffer;
      return ( stat(name.c_str(), &buffer) == 0 );
    }

    //执行命令并返回执行结果
    inline std::string exec(std::string str_cmd)
    {
      cout << "00" << endl;
      // char *cmd = (char*)str_cmd.data();
      char *cmd = (char*)str_cmd.c_str();
      cout << "11" << cmd << endl;

      std::string result = utils::exec(cmd);//这里崩溃了,我们希望通过解决Segmentation fault (core dumped) 使用gdb定位崩溃原因
      //还没成功。
      //上述方法自始至终都没有被调用
      //00 11 之间的代码被重复调用
      cout << "22" << endl;
      return result;
      // return str_cmd;
    }

    //执行命令并返回执行结果
    inline std::string exec(const char* cmd)
    {
      cout << "--------------------" << endl;
      cout << cmd << endl;
      cout << "--------------------" << endl;
      char buffer[128];
      std::string result = "";
      FILE *pipe = popen(cmd, "r");
      if (!pipe)
      {
        throw std::runtime_error("popen() failed!");
      }
      try
      {
          while ( !feof(pipe) )
          {
              if ( fgets(buffer, sizeof(buffer), pipe) != NULL )
                  result += buffer;
          }
      }
      catch (...)
      {
          pclose(pipe);
          throw;
      }
      pclose(pipe);
      return result;
    }

  }
}

#endif


崩溃打印信息:

代码: 全选

00
11/beyourself/DevTool/DevKit/Java/jdk1.7.0_80/bin/java -version
//上面两行大量重复出现
Segmentation fault (core dumped)
堆栈信息:

代码: 全选

warning: core file may not match specified executable file.
[New LWP 4506]
Core was generated by `./test_self_check.run'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00007f37069ccbbc in _int_malloc (av=av@entry=0x7f3706d0fb20 <main_arena>, bytes=bytes@entry=62) at malloc.c:3353
3353	malloc.c: No such file or directory.
poloshiao
论坛版主
帖子: 18279
注册时间: 2009-08-04 16:33

Re: Segmentation fault (core dumped),无法明了崩溃原因

#2

帖子 poloshiao » 2018-02-24 11:30

Segmentation fault (core dumped)
1. 先詳細參閱下述文章及連結網頁
1-1. https://zh.wikipedia.org/wiki/%E6%A0%B8 ... C%E5%82%A8
核心轉儲
1-2. https://en.wikipedia.org/wiki/Core_dump
Core dump
1-3. https://linux-audit.com/understand-and- ... -on-linux/
Understand and configure core dumps on Linux
无法明了崩溃原因
Ubuntu16.04
2. 善用 Journal 可以幫助你 列印出有幫助的崩溃記錄
https://wiki.archlinux.org/index.php/Systemd#Journal
Journal
头像
qgymib
帖子: 539
注册时间: 2010-04-02 16:44
系统: openSUSE 13.2 x64

Re: Segmentation fault (core dumped),无法明了崩溃原因

#3

帖子 qgymib » 2018-02-25 17:22

gdb挂起来啊
正在建设中的个人博客
回复