【程序调试命令 strace】

命令:

strace -C -t -T -o log.txt ./test
  • -C :添加统计信息
  • -t,-tt, -ttt:记录系统调用发生的时间
  • -T:记录系统的耗时
  • -o:将调试信息输出到指定文件中

示例:

#include 
#include 
#include 

void straceTest() {
    char buffer[256];
    std::ifstream in("input.txt");
    if (!in.is_open()) {
        std::cout << "error open file" << std::endl;
        exit(-1);
    }
    while (!in.eof()) {
        in.getline(buffer, 100);
        std::cout << buffer << std::endl;
    }
    in.close();
}


int main() {
    straceTest();
    return 0;
}

日志结果输出:

15:14:14 execve("./test", ["./test"], 0x7ffe377fa4c8 /* 44 vars */) = 0 <0.000186>
15:14:14 brk(NULL)                      = 0x55c05a583000 <0.000025>
15:14:14 arch_prctl(0x3001 /* ARCH_??? */, 0x7ffc1073c810) = -1 EINVAL (Invalid argument) <0.000024>
15:14:14 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory) <0.000038>
15:14:14 openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3 <0.000025>
15:14:14 fstat(3, {st_mode=S_IFREG|0644, st_size=36026, ...}) = 0 <0.000024>
15:14:14 mmap(NULL, 36026, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fb3a2a44000 <0.000026>
15:14:14 close(3)                       = 0 <0.000022>
15:14:14 openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libstdc++.so.6", O_RDONLY|O_CLOEXEC) = 3 <0.000028>
15:14:14 read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0`\341\t\0\0\0\0\0"..., 832) = 832 <0.000023>
15:14:14 fstat(3, {st_mode=S_IFREG|0644, st_size=1956992, ...}) = 0 <0.000028>
15:14:14 mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fb3a2a42000 <0.000024>
15:14:14 mmap(NULL, 1972224, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fb3a2860000 <0.000027>
15:14:14 mprotect(0x7fb3a28f6000, 1290240, PROT_NONE) = 0 <0.000027>
15:14:14 mmap(0x7fb3a28f6000, 987136, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x96000) = 0x7fb3a28f6000 <0.000027>
15:14:14 mmap(0x7fb3a29e7000, 299008, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x187000) = 0x7fb3a29e7000 <0.000024>
15:14:14 mmap(0x7fb3a2a31000, 57344, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1d0000) = 0x7fb3a2a31000 <0.000037>
15:14:14 mmap(0x7fb3a2a3f000, 10240, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fb3a2a3f000 <0.000026>
15:14:14 close(3)                       = 0 <0.000042>
15:14:14 openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libgcc_s.so.1", O_RDONLY|O_CLOEXEC) = 3 <0.000027>
15:14:14 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\3405\0\0\0\0\0\0"..., 832) = 832 <0.000023>
15:14:14 fstat(3, {st_mode=S_IFREG|0644, st_size=104984, ...}) = 0 <0.000025>
15:14:14 mmap(NULL, 107592, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fb3a2845000 <0.000029>
15:14:14 mmap(0x7fb3a2848000, 73728, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x3000) = 0x7fb3a2848000 <0.000054>
15:14:14 mmap(0x7fb3a285a000, 16384, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x15000) = 0x7fb3a285a000 <0.000029>
15:14:14 mmap(0x7fb3a285e000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x18000) = 0x7fb3a285e000 <0.000067>
15:14:14 close(3)                       = 0 <0.000024>
15:14:14 openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3 <0.000027>
15:14:14 read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\300A\2\0\0\0\0\0"..., 832) = 832 <0.000023>
15:14:14 pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784 <0.000022>
15:14:14 pread64(3, "\4\0\0\0\20\0\0\0\5\0\0\0GNU\0\2\0\0\300\4\0\0\0\3\0\0\0\0\0\0\0", 32, 848) = 32 <0.000022>
15:14:14 pread64(3, "\4\0\0\0\24\0\0\0\3\0\0\0GNU\0\30x\346\264ur\f|Q\226\236i\253-'o"..., 68, 880) = 68 <0.000021>
15:14:14 fstat(3, {st_mode=S_IFREG|0755, st_size=2029592, ...}) = 0 <0.000021>
15:14:14 pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784 <0.000021>
15:14:14 pread64(3, "\4\0\0\0\20\0\0\0\5\0\0\0GNU\0\2\0\0\300\4\0\0\0\3\0\0\0\0\0\0\0", 32, 848) = 32 <0.000021>
15:14:14 pread64(3, "\4\0\0\0\24\0\0\0\3\0\0\0GNU\0\30x\346\264ur\f|Q\226\236i\253-'o"..., 68, 880) = 68 <0.000021>
15:14:14 mmap(NULL, 2037344, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fb3a2653000 <0.000024>
15:14:14 mmap(0x7fb3a2675000, 1540096, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x22000) = 0x7fb3a2675000 <0.000027>
15:14:14 mmap(0x7fb3a27ed000, 319488, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x19a000) = 0x7fb3a27ed000 <0.000029>
15:14:14 mmap(0x7fb3a283b000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1e7000) = 0x7fb3a283b000 <0.000026>
15:14:14 mmap(0x7fb3a2841000, 13920, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fb3a2841000 <0.000024>
15:14:14 close(3)                       = 0 <0.000021>
15:14:14 openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libm.so.6", O_RDONLY|O_CLOEXEC) = 3 <0.000026>
15:14:14 read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\300\323\0\0\0\0\0\0"..., 832) = 832 <0.000023>
15:14:14 fstat(3, {st_mode=S_IFREG|0644, st_size=1369384, ...}) = 0 <0.000022>
15:14:14 mmap(NULL, 1368336, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fb3a2504000 <0.000024>
15:14:14 mmap(0x7fb3a2511000, 684032, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0xd000) = 0x7fb3a2511000 <0.000026>
15:14:14 mmap(0x7fb3a25b8000, 626688, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0xb4000) = 0x7fb3a25b8000 <0.000024>
15:14:14 mmap(0x7fb3a2651000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x14c000) = 0x7fb3a2651000 <0.000024>
15:14:14 close(3)                       = 0 <0.000023>
15:14:14 mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fb3a2502000 <0.000023>
15:14:14 arch_prctl(ARCH_SET_FS, 0x7fb3a2503100) = 0 <0.000021>
15:14:14 mprotect(0x7fb3a283b000, 16384, PROT_READ) = 0 <0.000025>
15:14:14 mprotect(0x7fb3a2651000, 4096, PROT_READ) = 0 <0.000026>
15:14:14 mprotect(0x7fb3a285e000, 4096, PROT_READ) = 0 <0.000024>
15:14:14 mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fb3a2500000 <0.000023>
15:14:14 mprotect(0x7fb3a2a31000, 45056, PROT_READ) = 0 <0.000029>
15:14:14 mprotect(0x55c059572000, 4096, PROT_READ) = 0 <0.000024>
15:14:14 mprotect(0x7fb3a2a7a000, 4096, PROT_READ) = 0 <0.000028>
15:14:14 munmap(0x7fb3a2a44000, 36026)  = 0 <0.000031>
15:14:14 brk(NULL)                      = 0x55c05a583000 <0.000023>
15:14:14 brk(0x55c05a5a4000)            = 0x55c05a5a4000 <0.000023>
15:14:14 fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(0x88, 0x40), ...}) = 0 <0.000030>
15:14:14 write(1, "sizeof(buffer) = 256\n", 21) = 21 <0.000032>
15:14:14 openat(AT_FDCWD, "input.txt", O_RDONLY) = 3 <0.000030>
15:14:14 read(3, "hhh\nxxx\nttttttt\npppppppppppppppp"..., 8191) = 44 <0.000025>
15:14:14 write(1, "hhh\n", 4)           = 4 <0.000030>
15:14:14 write(1, "xxx\n", 4)           = 4 <0.000026>
15:14:14 write(1, "ttttttt\n", 8)       = 8 <0.000034>
15:14:14 read(3, "", 8191)              = 0 <0.000023>
15:14:14 write(1, "pppppppppppppppppppppppppppp\n", 29) = 29 <0.000034>
15:14:14 close(3)                       = 0 <0.000026>
15:14:14 exit_group(0)                  = ?
15:14:14 +++ exited with 0 +++
% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
  0.00    0.000000           0         6           read
  0.00    0.000000           0         5           write
  0.00    0.000000           0         6           close
  0.00    0.000000           0         6           fstat
  0.00    0.000000           0        22           mmap
  0.00    0.000000           0         7           mprotect
  0.00    0.000000           0         1           munmap
  0.00    0.000000           0         3           brk
  0.00    0.000000           0         6           pread64
  0.00    0.000000           0         1         1 access
  0.00    0.000000           0         1           execve
  0.00    0.000000           0         2         1 arch_prctl
  0.00    0.000000           0         6           openat
------ ----------- ----------- --------- --------- ----------------
100.00    0.000000                    72         2 total

你可能感兴趣的:(c++,开发语言)