如何查看进程的系统调用

什么是系统调用我就不赘述了,不清楚的可以去参考我的另外一篇博客用户态 内核态

本文主要是带大家看看怎样去查看 进程对于内核的系统调用

linux 进程即文件

  • 在 linux 中,一切皆为文件,socket 是文件,进程也是文件
  • 那么进程的文件在哪呢?
    • 每个进程都有一个进程号(eg :pid = 3645)
    • 那么进程的相关文件就在 /proc/3645 目录下
    • 重点关注几个文件夹:
      • ask 目录下就有所有 线程 的信息
      • fd 文件描述符(一般用数字代表),每个进程都有io,至少三个(标准输入 0,标准输出 1,错误输出 2)
        • 比如一个tcp服务程序,必定至少有一个 listen 的 socket,就对多出一个文件
        • 又比如这个 tcp server 使用了 epoll,使用 epoll_create系统调用 会创建一个fd,也会在这个目录之下
  • 如果出现某个服务(进程)比较卡的时候,就可以进入 task 目录查看是否是进程太多,进行问题排查

例子

  • 一个简单的 echo tcpserver,监听在 0.0.0.0:9999

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    package main

    import (
    "bufio"
    "fmt"
    "io"
    "log"
    "net"
    )

    func main() {
    addr := "0.0.0.0:9999"

    tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
    if err != nil {
    log.Fatalf("net.ResovleTCPAddr fail:%s", addr)
    }

    listener, err := net.ListenTCP("tcp", tcpAddr)
    if err != nil {
    log.Fatalf("listen %s fail: %s", addr, err)
    } else {
    log.Println("tcp listening", addr)
    }

    for {
    conn, err := listener.Accept()
    if err != nil {
    log.Println("listener.Accept error:", err)
    continue
    }

    go handleConnection(conn)
    }

    }

    func handleConnection(conn net.Conn) {
    defer conn.Close()
    reader := bufio.NewReader(conn)
    for {
    // read client request data
    bytes, err := reader.ReadBytes(byte('\n'))
    if err != nil {
    if err != io.EOF {
    fmt.Println("failed to read data, err:", err)
    } else {
    fmt.Println("connection closed")
    }
    return
    }
    fmt.Printf("request: %s", bytes)

    // prepend prefix and send as response
    line := fmt.Sprintf("%s", bytes)
    fmt.Printf("response: %s", line)
    conn.Write([]byte(line))
    }
    }
  • 编译成linux版本

    1
    CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ./tcp-echo-server-linux ./main.go

echo tcp server

  • 上面的终端中启动 echo tcpserver
  • 下面的终端中依次:
    • 获取 echo server 进程号
    • 进入进程对于的目录
    • 进入 fd 目录,可以看到有三个基础的fd(0,1,2)以及一个 socket fd & epoll fd
    • 进入 task 目录

如何查看进程的系统调用

  • strace -ff -o ./ooxx ./tcp-echo-server-linux

  • 上述命令会运行 tcp-echo-server-linux,并且对它进行系统调用的抓取,并放到当前目录下
    echo tcp server

  • 执行上述命令之后,多出来很多 xxoo* 的文件,这些文件就记录来每个线程的系统调用

  • 我将 ooxx.30927 中的内容贴出来

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    execve("./tcp-echo-server-linux", ["./tcp-echo-server-linux"], 0x7ffcc5d16c08 /* 24 vars */) = 0
    arch_prctl(ARCH_SET_FS, 0x60c1f0) = 0
    sched_getaffinity(0, 8192, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]) = 8
    openat(AT_FDCWD, "/sys/kernel/mm/transparent_hugepage/hpage_pmd_size", O_RDONLY) = 3
    read(3, "2097152\n", 20) = 8
    close(3) = 0
    mmap(NULL, 262144, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7ff92955b000
    mmap(0xc000000000, 67108864, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xc000000000
    mmap(0xc000000000, 67108864, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0xc000000000
    mmap(NULL, 33554432, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7ff92755b000
    mmap(NULL, 2164736, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7ff92734a000
    mmap(NULL, 65536, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7ff92733a000
    mmap(NULL, 65536, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7ff92732a000
    rt_sigprocmask(SIG_SETMASK, NULL, [], 8) = 0
    sigaltstack(NULL, {ss_sp=NULL, ss_flags=SS_DISABLE, ss_size=0}) = 0
    sigaltstack({ss_sp=0xc000002000, ss_flags=0, ss_size=32768}, NULL) = 0
    rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
    gettid() = 30927
    rt_sigaction(SIGHUP, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGHUP, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGINT, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGINT, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGQUIT, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGQUIT, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGILL, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGILL, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGTRAP, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGTRAP, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGABRT, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGABRT, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGBUS, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGBUS, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGFPE, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGFPE, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGUSR1, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGUSR1, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGSEGV, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGSEGV, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGUSR2, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGUSR2, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGPIPE, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGPIPE, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGALRM, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGALRM, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGTERM, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGTERM, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGSTKFLT, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGSTKFLT, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGCHLD, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGCHLD, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGURG, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGURG, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGXCPU, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGXCPU, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGXFSZ, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGXFSZ, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGVTALRM, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGVTALRM, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGPROF, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGPROF, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGWINCH, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGWINCH, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGIO, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGIO, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGPWR, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGPWR, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGSYS, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGSYS, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGRTMIN, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_1, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_2, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_2, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGRT_3, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_3, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGRT_4, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_4, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGRT_5, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_5, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGRT_6, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_6, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGRT_7, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_7, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGRT_8, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_8, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGRT_9, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_9, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGRT_10, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_10, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGRT_11, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_11, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGRT_12, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_12, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGRT_13, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_13, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGRT_14, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_14, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGRT_15, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_15, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGRT_16, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_16, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGRT_17, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_17, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGRT_18, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_18, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGRT_19, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_19, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGRT_20, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_20, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGRT_21, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_21, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGRT_22, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_22, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGRT_23, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_23, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGRT_24, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_24, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGRT_25, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_25, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGRT_26, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_26, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGRT_27, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_27, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGRT_28, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_28, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGRT_29, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_29, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGRT_30, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_30, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGRT_31, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_31, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigaction(SIGRT_32, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
    rt_sigaction(SIGRT_32, {sa_handler=0x459560, sa_mask=~[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESTART|SA_SIGINFO, sa_restorer=0x459690}, NULL, 8) = 0
    rt_sigprocmask(SIG_SETMASK, ~[], [], 8) = 0
    clone(child_stack=0xc000062000, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM) = 30928
    rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
    rt_sigprocmask(SIG_SETMASK, ~[], [], 8) = 0
    clone(child_stack=0xc000064000, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM) = 30929
    rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
    rt_sigprocmask(SIG_SETMASK, ~[], [], 8) = 0
    clone(child_stack=0xc00005e000, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM) = 30930
    rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
    futex(0x60c2a8, FUTEX_WAIT_PRIVATE, 0, NULL) = 0
    rt_sigprocmask(SIG_SETMASK, ~[], [], 8) = 0
    clone(child_stack=0xc000060000, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM) = 30932
    rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
    futex(0x60c2a8, FUTEX_WAIT_PRIVATE, 0, NULL) = 0
    readlinkat(AT_FDCWD, "/proc/self/exe", "/root/temp/temp/tcp-echo-server-"..., 128) = 37
    fcntl(0, F_GETFL) = 0x2 (flags O_RDWR)
    futex(0xc0000524c8, FUTEX_WAKE_PRIVATE, 1) = 1
    mmap(NULL, 262144, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7ff9272ea000
    fcntl(1, F_GETFL) = 0x2 (flags O_RDWR)
    fcntl(2, F_GETFL) = 0x2 (flags O_RDWR)
    socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, IPPROTO_TCP) = 3
    close(3) = 0
    socket(AF_INET6, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, IPPROTO_TCP) = 3
    setsockopt(3, SOL_IPV6, IPV6_V6ONLY, [1], 4) = 0
    bind(3, {sa_family=AF_INET6, sin6_port=htons(0), inet_pton(AF_INET6, "::1", &sin6_addr), sin6_flowinfo=htonl(0), sin6_scope_id=0}, 28) = -1 EADDRNOTAVAIL (Cannot assign requested address)
    socket(AF_INET6, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, IPPROTO_TCP) = 4
    setsockopt(4, SOL_IPV6, IPV6_V6ONLY, [0], 4) = 0
    bind(4, {sa_family=AF_INET6, sin6_port=htons(0), inet_pton(AF_INET6, "::ffff:127.0.0.1", &sin6_addr), sin6_flowinfo=htonl(0), sin6_scope_id=0}, 28) = 0
    close(4) = 0
    close(3) = 0
    socket(AF_INET6, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, IPPROTO_IP) = 3
    setsockopt(3, SOL_IPV6, IPV6_V6ONLY, [0], 4) = 0
    setsockopt(3, SOL_SOCKET, SO_BROADCAST, [1], 4) = 0
    openat(AT_FDCWD, "/proc/sys/net/core/somaxconn", O_RDONLY|O_CLOEXEC) = 4
    epoll_create1(EPOLL_CLOEXEC) = 5
    epoll_ctl(5, EPOLL_CTL_ADD, 4, {EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLET, {u32=657387272, u64=140708080971528}}) = 0
    fcntl(4, F_GETFL) = 0x8000 (flags O_RDONLY|O_LARGEFILE)
    fcntl(4, F_SETFL, O_RDONLY|O_NONBLOCK|O_LARGEFILE) = 0
    read(4, "128\n", 65536) = 4
    read(4, "", 65532) = 0
    epoll_ctl(5, EPOLL_CTL_DEL, 4, 0xc0000a49a4) = 0
    close(4) = 0
    setsockopt(3, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
    bind(3, {sa_family=AF_INET6, sin6_port=htons(9999), inet_pton(AF_INET6, "::", &sin6_addr), sin6_flowinfo=htonl(0), sin6_scope_id=0}, 28) = 0
    listen(3, 128) = 0
    epoll_ctl(5, EPOLL_CTL_ADD, 3, {EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLET, {u32=657387272, u64=140708080971528}}) = 0
    getsockname(3, {sa_family=AF_INET6, sin6_port=htons(9999), inet_pton(AF_INET6, "::", &sin6_addr), sin6_flowinfo=htonl(0), sin6_scope_id=0}, [112->28]) = 0
    openat(AT_FDCWD, "/etc//localtime", O_RDONLY) = 4
    read(4, "TZif2\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\3\0\0\0\3\0\0\0\0"..., 4096) = 554
    read(4, "", 4096) = 0
    close(4) = 0
    write(2, "2020/04/15 10:30:01 tcp listenin"..., 47) = 47
    accept4(3, 0xc000085bc0, [112], SOCK_CLOEXEC|SOCK_NONBLOCK) = -1 EAGAIN (Resource temporarily unavailable)
    epoll_pwait(5, [], 128, 0, NULL, 0) = 0
    epoll_pwait(5, 0x7fffa12415e0, 128, -1, NULL, 0) = -1 EINTR (Interrupted system call)
    --- SIGWINCH {si_signo=SIGWINCH, si_code=SI_KERNEL} ---
    rt_sigreturn({mask=[]}) = -1 EINTR (Interrupted system call)
    epoll_pwait(5, 0x7fffa12415e0, 128, -1, NULL, 0) = -1 EINTR (Interrupted system call)
    --- SIGWINCH {si_signo=SIGWINCH, si_code=SI_KERNEL} ---
    rt_sigreturn({mask=[]}) = -1 EINTR (Interrupted system call)
    epoll_pwait(5, 0x7fffa12415e0, 128, -1, NULL, 0) = -1 EINTR (Interrupted system call)
    --- SIGWINCH {si_signo=SIGWINCH, si_code=SI_KERNEL} ---
    rt_sigreturn({mask=[]}) = -1 EINTR (Interrupted system call)
    epoll_pwait(5, 0x7fffa12415e0, 128, -1, NULL, 0) = -1 EINTR (Interrupted system call)
    --- SIGWINCH {si_signo=SIGWINCH, si_code=SI_KERNEL} ---
    rt_sigreturn({mask=[]}) = -1 EINTR (Interrupted system call)
    epoll_pwait(5, 0x7fffa12415e0, 128, -1, NULL, 0) = -1 EINTR (Interrupted system call)
    --- SIGWINCH {si_signo=SIGWINCH, si_code=SI_KERNEL} ---
    rt_sigreturn({mask=[]}) = -1 EINTR (Interrupted system call)
    epoll_pwait(5, 0x7fffa12415e0, 128, -1, NULL, 0) = -1 EINTR (Interrupted system call)
    --- SIGWINCH {si_signo=SIGWINCH, si_code=SI_KERNEL} ---
    rt_sigreturn({mask=[]}) = -1 EINTR (Interrupted system call)
    epoll_pwait(5, root
  • 可以看到 134,137,140,144 行通过 clone 系统调用 创建新的线程

  • 153,155 行分别调用 socket系统调用 创建了 socket,用于后续的 listen

    • 一个是ipv4(fd=3)
    • 一个是ipv6 (fd=4)
  • 157,160 行分别调用了 bind系统调用 绑定监听的地址

  • 167 行调用了 epoll_create系统调用 创建了 epoll 实例(fd = 5)

  • 168,173,178 行分别调用 epoll_ctl系统调用

  • 176 行调用了 bind系统调用 绑定9999端口

  • 最后一行也可以看到调用了 epoll_wait 系统调用 等待连接