用strace来跟踪信号传递

一 代码

#include 
using namespace std;
int main(){
    int a;
    cin>>a;
    cout<

二 编译并调试

[root@localhost charpter05]# strace ./test
execve("./test", ["./test"], [/* 25 vars */]) = 0
brk(NULL)                               = 0x1fe5000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f0983e49000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=35467, ...}) = 0
mmap(NULL, 35467, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f0983e40000
close(3)                                = 0
......
......
mprotect(0x7f0983e4a000, 4096, PROT_READ) = 0
munmap(0x7f0983e40000, 35467)           = 0
fstat(0, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f0983e48000
read(0,

此时程序停顿并等待输入。

三 跟踪信号传递

打开另外一个窗口,输入命令"killall test",第1个窗口输出结果如下:

......
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f0983e48000
read(0, 0x7f0983e48000, 1024)           = ? ERESTARTSYS (To be restarted if SA_RESTART is set)
--- SIGTERM {si_signo=SIGTERM, si_code=SI_USER, si_pid=1335, si_uid=0} ---
+++ killed by SIGTERM +++

四 说明

strace中结果显示test进程+++ killed by SIGTERM +++。

命令killall test,就是杀死所有名为test的进程。

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