linux g++ -O1或者-O2优化之后段错误

请不要生气 2014-08-25 11:23:14
遇到一个非常奇怪的问题。

不加优化选项运行正常

加了优化就出现段错误:

(gdb) where
#0 0x0000000000c5ef32 in ?? ()
#1 0x0000000000c64528 in ?? ()
#2 0x0000000000c64674 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::_Rep::_M_clone(std::allocator<char> const&, unsigned long) ()
#3 0x0000000000c6281f in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::reserve(unsigned long) ()
#4 0x0000000000c62ac5 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::append(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) ()
#5 0x0000000000cd5f09 in std::basic_string<char, std::char_traits<char>, std::allocator<char> > std::operator+<char, std::char_traits<char>, std::allocator<char> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) ()
#6 0x0000000000cd1e9f in ?? ()
#7 0x0000000000cd27ac in ?? ()
#8 0x0000000000cd29b1 in ?? ()
#9 0x0000000000cd2e10 in ?? ()
#10 0x0000000000cd2f11 in ?? ()
#11 0x0000000000cd2fb7 in ?? ()
#12 0x0000000000441c52 in getpwuid_r ()
#13 0x0000000000435927 in getpwuid_r ()
#14 0x0000000000435e4b in getpwuid_r ()
#15 0x000000000043630b in getpwuid_r ()
#16 0x000000000042d2a2 in getpwuid_r ()
#17 0x000000000042c3f6 in getpwuid_r ()
#18 0x0000003a2da1d994 in __libc_start_main () from /lib64/libc.so.6
#19 0x000000000042bfe9 in getpwuid_r ()
#20 0x00007fffffffe3a8 in ?? ()
#21 0x0000000000000000 in ?? ()

出错相关函数主要是string 的+, += 等操作。
...全文
958 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
请不要生气 2015-07-01
  • 打赏
  • 举报
回复
请问解决了吗
mymtom 2015-06-18
  • 打赏
  • 举报
回复
调试信息很诡异啊,getpwuid_r是系统函数,怎么还会调用std::string?
ztenv 版主 2015-06-18
  • 打赏
  • 举报
回复
应该是代码的问题,你不加O的时候多运行几次,让他循环的运行几天、几周,
duanguyuan 2015-06-17
  • 打赏
  • 举报
回复
这个问题搞了我两天!!!不加 -O 编译程序正常,加了之后就段错误。出错位置为 string 的 “+” 操作。
请不要生气 2014-08-25
  • 打赏
  • 举报
回复
引用 7 楼 u010889248 的回复:
[quote=引用 5 楼 zhao4zhong1 的回复:] 进程意外退出会在当前目录下产生‘core’文件或形如‘core.数字’的文件比如‘core.1234’ 使用命令 gdb 运行程序名 core或core.数字 进入gdb然后使用bt命令 可以查看进程意外退出前函数调用的堆栈,内容为从上到下列出对应从里层到外层的函数调用历史。 如果进程意外退出不产生core文件,参考“ulimit -c core文件最大块大小”命令
多谢找老师, 有时改一点点程序,又会多运行一段, 比如, string tmpstr = string1 + “d” + string2; 改为 string tmpstr = string1 + string2; 程序就又往后运行了几步,然后依然段错误退出。。。。而且就卡在string的+ 或者+=运算符上。[/quote] 不好意思,打错了,赵老师。。。
请不要生气 2014-08-25
  • 打赏
  • 举报
回复
引用 5 楼 zhao4zhong1 的回复:
进程意外退出会在当前目录下产生‘core’文件或形如‘core.数字’的文件比如‘core.1234’ 使用命令 gdb 运行程序名 core或core.数字 进入gdb然后使用bt命令 可以查看进程意外退出前函数调用的堆栈,内容为从上到下列出对应从里层到外层的函数调用历史。 如果进程意外退出不产生core文件,参考“ulimit -c core文件最大块大小”命令
多谢找老师, 有时改一点点程序,又会多运行一段, 比如, string tmpstr = string1 + “d” + string2; 改为 string tmpstr = string1 + string2; 程序就又往后运行了几步,然后依然段错误退出。。。。而且就卡在string的+ 或者+=运算符上。
赵4老师 2014-08-25
  • 打赏
  • 举报
回复
有时不将“调用函数名字+各参数值,进入函数后各参数值,中间变量值,退出函数前准备返回的值,返回函数到调用处后函数名字+各参数值+返回值”这些信息写日志到文件中是无论如何也发现不了问题在哪里的,包括捕获各种异常、写日志到屏幕、单步或设断点或生成core文件、……这些方法都不行! 写日志到文件参考下面:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
    #include <windows.h>
    #include <io.h>
#else
    #include <unistd.h>
    #include <sys/time.h>
    #include <pthread.h>
    #define  CRITICAL_SECTION   pthread_mutex_t
    #define  _vsnprintf         vsnprintf
#endif
//Log{
#define MAXLOGSIZE 20000000
#define MAXLINSIZE 16000
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
static char logstr[MAXLINSIZE+1];
char datestr[16];
char timestr[16];
char mss[4];
CRITICAL_SECTION cs_log;
FILE *flog;
#ifdef WIN32
void Lock(CRITICAL_SECTION *l) {
    EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
    LeaveCriticalSection(l);
}
#else
void Lock(CRITICAL_SECTION *l) {
    pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
    pthread_mutex_unlock(l);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
    struct tm *now;
    struct timeb tb;

    if (NULL==pszFmt||0==pszFmt[0]) return;
    _vsnprintf(logstr,MAXLINSIZE,pszFmt,argp);
    ftime(&tb);
    now=localtime(&tb.time);
    sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
    sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );
    sprintf(mss,"%03d",tb.millitm);
    printf("%s %s.%s %s",datestr,timestr,mss,logstr);
    flog=fopen(logfilename1,"a");
    if (NULL!=flog) {
        fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);
        if (ftell(flog)>MAXLOGSIZE) {
            fclose(flog);
            if (rename(logfilename1,logfilename2)) {
                remove(logfilename2);
                rename(logfilename1,logfilename2);
            }
        } else {
            fclose(flog);
        }
    }
}
void Log(const char *pszFmt,...) {
    va_list argp;

    Lock(&cs_log);
    va_start(argp,pszFmt);
    LogV(pszFmt,argp);
    va_end(argp);
    Unlock(&cs_log);
}
//Log}
int main(int argc,char * argv[]) {
    int i;
#ifdef WIN32
    InitializeCriticalSection(&cs_log);
#else
    pthread_mutex_init(&cs_log,NULL);
#endif
    for (i=0;i<10000;i++) {
        Log("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__);
    }
#ifdef WIN32
    DeleteCriticalSection(&cs_log);
#else
    pthread_mutex_destroy(&cs_log);
#endif
    return 0;
}
//1-78行添加到你带main的.c或.cpp的那个文件的最前面
//81-85行添加到你的main函数开头
//89-93行添加到你的main函数结束前
//在要写LOG的地方仿照第87行的写法写LOG到文件MyLog1.log中
赵4老师 2014-08-25
  • 打赏
  • 举报
回复
进程意外退出会在当前目录下产生‘core’文件或形如‘core.数字’的文件比如‘core.1234’ 使用命令 gdb 运行程序名 core或core.数字 进入gdb然后使用bt命令 可以查看进程意外退出前函数调用的堆栈,内容为从上到下列出对应从里层到外层的函数调用历史。 如果进程意外退出不产生core文件,参考“ulimit -c core文件最大块大小”命令
passion_wu128 2014-08-25
  • 打赏
  • 举报
回复
windows下能够在崩溃时生成dump文件,快速定位到错误代码,Linux下面不知道有没有类似机制。
  • 打赏
  • 举报
回复
g++编译时候,加上-ggdb,-ldl,然后把ulimit打开,程序crash时候,会生成一个core文件,挂上core调试,打印寄存器信息。然后加上nm工具和objdump,把执行文件的符号dump出来分析。
请不要生气 2014-08-25
  • 打赏
  • 举报
回复
引用 1 楼 passion_wu128 的回复:
我也遇到过,一般是代码写得有问题,从你的dump看不出出错信息。
因为优化了,所以看不到什么信息, 只有通过printf找到出错地方,比如: string tmpstr = "v" + string1; 这样的语句。
请不要生气 2014-08-25
  • 打赏
  • 举报
回复
因为优化了,所以看不到什么信息, 只有通过printf找到出错地方,比如: string tmpstr = "v" + string1; 这样的语句。
passion_wu128 2014-08-25
  • 打赏
  • 举报
回复
我也遇到过,一般是代码写得有问题,从你的dump看不出出错信息。
yuanhong2910 2014-08-25
  • 打赏
  • 举报
回复
应该是string为null,good luck
AncientLysine 2014-08-25
  • 打赏
  • 举报
回复
前几天写的时候发现如果一个变量光有声明和后续赋值而没有参与计算就会被优化掉,然后跑到赋值时就会段错误.上古时期的GCC4.3上出的问题.
赵4老师 2014-08-25
  • 打赏
  • 举报
回复
凡是不加优化运行正常,加优化运行异常的代码,99%都不是真正的运行正常!

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧