C++:不在崩溃时显示 glibc 的回溯和内存映射
C++: Do not show glibc's Backtrace and Memory map on crash
我正在使用 Python 进行自动 C++ 代码测试。所以我有一个编译和执行 C++ 代码的 Python 脚本。当 C++ 代码崩溃时,从我的 Python 脚本输出中可以看到 libc 输出,即使我将正在执行的 C++ 程序的 cout
和 cerr
重定向到 /dev/null
.
这是一个隔离问题的示例:
#! /usr/bin/env python
# -*- coding: utf8 *-*
import sys
import os.path
import subprocess
import shutil
import tempfile
import string
import argparse
bug_folder = tempfile.mkdtemp("bug")
# Create main.cpp
with open( os.path.join(bug_folder,'main.cpp'), "w") as mainfile:
mainfile.write( "int main()\n" )
mainfile.write( "{\n" )
mainfile.write( " int* toto = new int(4);\n" )
mainfile.write( " delete toto;\n" )
mainfile.write( " delete toto;\n" )
mainfile.write( "}" )
# Create Makefile
project = "bug"
with open( os.path.join(bug_folder,'Makefile'), "w") as makefile:
makefile.write( "PROG = " + project + "\n" )
makefile.write( "OBJS = main.o\n" )
makefile.write( "CC = g++\n" )
makefile.write( "CFLAGS = -Wall -g -pedantic\n" )
makefile.write( "\n" )
makefile.write( "$(PROG): $(OBJS)\n" )
makefile.write( "\t$(CC) $(CFLAGS) -o $@ $(OBJS)\n" )
makefile.close()
# Compile
delete_folder = True
old_path = os.getcwd()
os.chdir(bug_folder)
if subprocess.call(["make"]) == 0 and os.path.isfile( project ):
# Project could be compiled!!
print "Running program..."
with open( os.devnull, "w") as outfile:
with open( os.devnull, "w") as errfile:
subprocess.call( [os.path.join(bug_folder,project)], stdout=outfile, stderr=errfile )
print "Done"
else:
print "Failed to compile (" + bug_folder + ")"
delete_folder = False
os.chdir(old_path)
if delete_folder:
shutil.rmtree( bug_folder )
程序输出为:
g++ -c -o main.o main.cpp
g++ -Wall -g -pedantic -o bug main.o
Running program...
*** glibc detected *** /tmp/tmpxX20mLbug/bug: double free or corruption (fasttop): 0x00000000006cc010 ***
======= Backtrace: =========
/lib64/libc.so.6[0x328c275e66]
/tmp/tmpxX20mLbug/bug[0x40063b]
/lib64/libc.so.6(__libc_start_main+0xfd)[0x328c21ed5d]
/tmp/tmpxX20mLbug/bug[0x400549]
======= Memory map: ========
00400000-00401000 r-xp 00000000 00:12 93358464 /tmp/tmpxX20mLbug/bug
00600000-00601000 rw-p 00000000 00:12 93358464 /tmp/tmpxX20mLbug/bug
006cc000-006ed000 rw-p 00000000 00:00 0 [heap]
328be00000-328be20000 r-xp 00000000 fd:00 136549 /lib64/ld-2.12.so
328c01f000-328c020000 r--p 0001f000 fd:00 136549 /lib64/ld-2.12.so
328c020000-328c021000 rw-p 00020000 fd:00 136549 /lib64/ld-2.12.so
328c021000-328c022000 rw-p 00000000 00:00 0
328c200000-328c38a000 r-xp 00000000 fd:00 136550 /lib64/libc-2.12.so
328c38a000-328c58a000 ---p 0018a000 fd:00 136550 /lib64/libc-2.12.so
328c58a000-328c58e000 r--p 0018a000 fd:00 136550 /lib64/libc-2.12.so
328c58e000-328c58f000 rw-p 0018e000 fd:00 136550 /lib64/libc-2.12.so
328c58f000-328c594000 rw-p 00000000 00:00 0
328c600000-328c683000 r-xp 00000000 fd:00 136551 /lib64/libm-2.12.so
328c683000-328c882000 ---p 00083000 fd:00 136551 /lib64/libm-2.12.so
328c882000-328c883000 r--p 00082000 fd:00 136551 /lib64/libm-2.12.so
328c883000-328c884000 rw-p 00083000 fd:00 136551 /lib64/libm-2.12.so
390fa00000-390fa16000 r-xp 00000000 fd:00 131093 /lib64/libgcc_s-4.4.7-20120601.so.1
390fa16000-390fc15000 ---p 00016000 fd:00 131093 /lib64/libgcc_s-4.4.7-20120601.so.1
390fc15000-390fc16000 rw-p 00015000 fd:00 131093 /lib64/libgcc_s-4.4.7-20120601.so.1
390fe00000-390fee8000 r-xp 00000000 fd:00 658079 /usr/lib64/libstdc++.so.6.0.13
390fee8000-39100e8000 ---p 000e8000 fd:00 658079 /usr/lib64/libstdc++.so.6.0.13
39100e8000-39100ef000 r--p 000e8000 fd:00 658079 /usr/lib64/libstdc++.so.6.0.13
39100ef000-39100f1000 rw-p 000ef000 fd:00 658079 /usr/lib64/libstdc++.so.6.0.13
39100f1000-3910106000 rw-p 00000000 00:00 0
7f302c4bb000-7f302c4c0000 rw-p 00000000 00:00 0
7f302c4e5000-7f302c4e7000 rw-p 00000000 00:00 0
7fffc6840000-7fffc6856000 rw-p 00000000 00:00 0 [stack]
7fffc6a00000-7fffc6a01000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Done
我更愿意:
g++ -c -o main.o main.cpp
g++ -Wall -g -pedantic -o bug main.o
Running program...
Done
是否可以让执行不显示整个glibc错误报告?
注意:在运行 python 脚本之前,我在我的 shell 中尝试了 set LIBC_FATAL_STDERR_=1
,但它根本没有解决问题。
我正在使用 Python 2.7.4 和 g++ 4.4.7
以下对我有用(通过将 glibc 写入错误 stderr
而不是 /dev/tty
):
import copy
import os
# ...
with open(os.devnull, "w") as outfile:
with open(os.devnull, "w") as errfile:
env = copy.copy(os.environ)
env["LIBC_FATAL_STDERR_"] = "1"
p = subprocess.Popen([os.path.join(bug_folder, project)],
stdout=outfile, stderr=errfile, env=env)
p.wait()
我正在使用 Python 进行自动 C++ 代码测试。所以我有一个编译和执行 C++ 代码的 Python 脚本。当 C++ 代码崩溃时,从我的 Python 脚本输出中可以看到 libc 输出,即使我将正在执行的 C++ 程序的 cout
和 cerr
重定向到 /dev/null
.
这是一个隔离问题的示例:
#! /usr/bin/env python
# -*- coding: utf8 *-*
import sys
import os.path
import subprocess
import shutil
import tempfile
import string
import argparse
bug_folder = tempfile.mkdtemp("bug")
# Create main.cpp
with open( os.path.join(bug_folder,'main.cpp'), "w") as mainfile:
mainfile.write( "int main()\n" )
mainfile.write( "{\n" )
mainfile.write( " int* toto = new int(4);\n" )
mainfile.write( " delete toto;\n" )
mainfile.write( " delete toto;\n" )
mainfile.write( "}" )
# Create Makefile
project = "bug"
with open( os.path.join(bug_folder,'Makefile'), "w") as makefile:
makefile.write( "PROG = " + project + "\n" )
makefile.write( "OBJS = main.o\n" )
makefile.write( "CC = g++\n" )
makefile.write( "CFLAGS = -Wall -g -pedantic\n" )
makefile.write( "\n" )
makefile.write( "$(PROG): $(OBJS)\n" )
makefile.write( "\t$(CC) $(CFLAGS) -o $@ $(OBJS)\n" )
makefile.close()
# Compile
delete_folder = True
old_path = os.getcwd()
os.chdir(bug_folder)
if subprocess.call(["make"]) == 0 and os.path.isfile( project ):
# Project could be compiled!!
print "Running program..."
with open( os.devnull, "w") as outfile:
with open( os.devnull, "w") as errfile:
subprocess.call( [os.path.join(bug_folder,project)], stdout=outfile, stderr=errfile )
print "Done"
else:
print "Failed to compile (" + bug_folder + ")"
delete_folder = False
os.chdir(old_path)
if delete_folder:
shutil.rmtree( bug_folder )
程序输出为:
g++ -c -o main.o main.cpp
g++ -Wall -g -pedantic -o bug main.o
Running program...
*** glibc detected *** /tmp/tmpxX20mLbug/bug: double free or corruption (fasttop): 0x00000000006cc010 ***
======= Backtrace: =========
/lib64/libc.so.6[0x328c275e66]
/tmp/tmpxX20mLbug/bug[0x40063b]
/lib64/libc.so.6(__libc_start_main+0xfd)[0x328c21ed5d]
/tmp/tmpxX20mLbug/bug[0x400549]
======= Memory map: ========
00400000-00401000 r-xp 00000000 00:12 93358464 /tmp/tmpxX20mLbug/bug
00600000-00601000 rw-p 00000000 00:12 93358464 /tmp/tmpxX20mLbug/bug
006cc000-006ed000 rw-p 00000000 00:00 0 [heap]
328be00000-328be20000 r-xp 00000000 fd:00 136549 /lib64/ld-2.12.so
328c01f000-328c020000 r--p 0001f000 fd:00 136549 /lib64/ld-2.12.so
328c020000-328c021000 rw-p 00020000 fd:00 136549 /lib64/ld-2.12.so
328c021000-328c022000 rw-p 00000000 00:00 0
328c200000-328c38a000 r-xp 00000000 fd:00 136550 /lib64/libc-2.12.so
328c38a000-328c58a000 ---p 0018a000 fd:00 136550 /lib64/libc-2.12.so
328c58a000-328c58e000 r--p 0018a000 fd:00 136550 /lib64/libc-2.12.so
328c58e000-328c58f000 rw-p 0018e000 fd:00 136550 /lib64/libc-2.12.so
328c58f000-328c594000 rw-p 00000000 00:00 0
328c600000-328c683000 r-xp 00000000 fd:00 136551 /lib64/libm-2.12.so
328c683000-328c882000 ---p 00083000 fd:00 136551 /lib64/libm-2.12.so
328c882000-328c883000 r--p 00082000 fd:00 136551 /lib64/libm-2.12.so
328c883000-328c884000 rw-p 00083000 fd:00 136551 /lib64/libm-2.12.so
390fa00000-390fa16000 r-xp 00000000 fd:00 131093 /lib64/libgcc_s-4.4.7-20120601.so.1
390fa16000-390fc15000 ---p 00016000 fd:00 131093 /lib64/libgcc_s-4.4.7-20120601.so.1
390fc15000-390fc16000 rw-p 00015000 fd:00 131093 /lib64/libgcc_s-4.4.7-20120601.so.1
390fe00000-390fee8000 r-xp 00000000 fd:00 658079 /usr/lib64/libstdc++.so.6.0.13
390fee8000-39100e8000 ---p 000e8000 fd:00 658079 /usr/lib64/libstdc++.so.6.0.13
39100e8000-39100ef000 r--p 000e8000 fd:00 658079 /usr/lib64/libstdc++.so.6.0.13
39100ef000-39100f1000 rw-p 000ef000 fd:00 658079 /usr/lib64/libstdc++.so.6.0.13
39100f1000-3910106000 rw-p 00000000 00:00 0
7f302c4bb000-7f302c4c0000 rw-p 00000000 00:00 0
7f302c4e5000-7f302c4e7000 rw-p 00000000 00:00 0
7fffc6840000-7fffc6856000 rw-p 00000000 00:00 0 [stack]
7fffc6a00000-7fffc6a01000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Done
我更愿意:
g++ -c -o main.o main.cpp
g++ -Wall -g -pedantic -o bug main.o
Running program...
Done
是否可以让执行不显示整个glibc错误报告?
注意:在运行 python 脚本之前,我在我的 shell 中尝试了 set LIBC_FATAL_STDERR_=1
,但它根本没有解决问题。
我正在使用 Python 2.7.4 和 g++ 4.4.7
以下对我有用(通过将 glibc 写入错误 stderr
而不是 /dev/tty
):
import copy
import os
# ...
with open(os.devnull, "w") as outfile:
with open(os.devnull, "w") as errfile:
env = copy.copy(os.environ)
env["LIBC_FATAL_STDERR_"] = "1"
p = subprocess.Popen([os.path.join(bug_folder, project)],
stdout=outfile, stderr=errfile, env=env)
p.wait()