LoginSignup
14
7

More than 5 years have passed since last update.

dtrussでgolangのシステムコールをトレースしたい

Last updated at Posted at 2018-02-10

goはあまり関係ない

macでシステムコールをモニタリングするために、strace/trussの代わりにdtrace/dtrussがインストールされている。
しかしmacOS Sieera(10.12)以降はSIPにより動かない(syscallのトレースが制限される?)ため、SIPのを停止する必要がある。

動作環境

  • os: macOS High Sierra (10.13.3)
  • go version: go1.9.2 darwin/amd64

実行するサンプルコード

  • main.go
package main

import (
    "C"
    "os"
)

func main() {
    file, err := os.Create("sample.txt")
    if err != nil {
        panic(err)
    }
    defer file.Close()
    file.Write([]byte("system call example\n"))
}

SIPが有効でsyscallの解析が制限されていることを確認する

SIPをの状態を確認する

  • SIPの全項目が有効な状態
$ sudo  csrutil status
System Integrity Protection status: enabled

実行(error pattern)

  • system integrity protection(SIP)が有効なため一部の機能は利用できない
$ go build main.go
$ sudo dtruss ./main
dtrace: system integrity protection is on, some features will not be available

SYSCALL(args)            = return
open("/dev/dtracehelper\0", 0x2, 0xFFFFFFFFEFBFE9E0)             = 3 0
ioctl(0x3, 0x80086804, 0x7FFEEFBFE940)           = 0 0
close(0x3)               = 0 0

︙(省略)

dtrace: error on enabled probe ID 2190 (ID 566: syscall::sysctl:return): invalid kernel access in action #10 at DIF offset 40
csops(0x79E, 0x7, 0x7FFEEFBFD440)                = -1 Err#22
dtrace: error on enabled probe ID 2190 (ID 566: syscall::sysctl:return): invalid kernel access in action #10 at DIF offset 40
dtrace: error on enabled probe ID 2190 (ID 566: syscall::sysctl:return): invalid kernel access in action #10 at DIF offset 40(省略)

SIPの DTraceに関する制限を無効にして syscall の解析が制限されていることを確認する

リカバリーモード(起動時にCommand + R)で起動し、 Terminalからdtraceの実行制限だけ解除

csrutil enable --without dtrace

reboot後に、[DTrace Restrictions]が無効になっていることを確認

  • DTrace Restrictions が disabledになっている
$ csrutil status
System Integrity Protection status: enabled (Custom Configuration).

Configuration:
    Apple Internal: disabled
    Kext Signing: enabled
    Filesystem Protections: enabled
    Debugging Restrictions: enabled
    DTrace Restrictions: disabled
    NVRAM Protections: enabled
    BaseSystem Verification: enabled

再度実行

$ sudo dtruss ./main
SYSCALL(args)            = return
open("/dev/dtracehelper\0", 0x2, 0xFFFFFFFFEFBFE9E0)             = 3 0
ioctl(0x3, 0x80086804, 0x7FFEEFBFE940)           = 0 0
close(0x3)               = 0 0

︙(省略)

sysctl([CTL_KERN, 14, 1, 1806, 0, 0] (4), 0x7FFEEFBFDC98, 0x7FFEEFBFDC90, 0x0, 0x0)              = 0 0
csops(0x70E, 0x7, 0x7FFEEFBFD440)                = -1 Err#22
sysctl([CTL_HW, 3, 0, 0, 0, 0] (2), 0x7FFEEFBFF854, 0x7FFEEFBFF858, 0x0, 0x0)            = 0 0
sysctl([CTL_HW, 7, 0, 0, 0, 0] (2), 0x7FFEEFBFF854, 0x7FFEEFBFF858, 0x0, 0x0)            = 0 0

︙(省略)

参考リンク

14
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
7