strace调试程序实战

一 代码

#include   
#include   
#include 
using namespace std;  
int main(){  
   char buffer[256];  
   ifstream in("input.txt");  
   if (! in.is_open()){
       cout << "Error opening file"<

二 编译运行

[root@localhost charpter05]# g++ 0503.cpp -o test
[root@localhost charpter05]# ./test
Error opening file

出现运行时错误。

三 调试

[root@localhost charpter05]# strace ./test
execve("./test", ["./test"], [/* 25 vars */]) = 0
......
mprotect(0x7f07fadd7000, 4096, PROT_READ) = 0
munmap(0x7f07fadcd000, 35467)           = 0
brk(NULL)                               = 0x17fb000
brk(0x181c000)                          = 0x181c000
brk(NULL)                               = 0x181c000
open("input.txt", O_RDONLY)             = -1 ENOENT (No such file or directory)
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f07fadd5000
write(1, "Error opening file\n", 19Error opening file
)    = 19
exit_group(1)                           = ?
+++ exited with 1 +++

四 分析

从调试结果来看,是因为open("input.txt", O_RDONLY)调用失败,失败原因是因为没有input.txt文件。

你可能感兴趣的:(C++)