Tracing system calls

This section will present a pretty advanced technique that uses the syscall package and allows you to monitor the system calls executed in a Go program.

The name of the Go utility is traceSyscall.go, and it is going to be presented in five code segments. The first part of traceSyscall.go follows:

package main 
 
import ( 
    "bufio" 
    "fmt" 
    "os" 
    "os/exec" 
    "strings" 
    "syscall" 
) 
 
var maxSyscalls = 0 
 
const SYSCALLFILE = "SYSCALLS" 

You will learn more about the purpose of the SYSCALLFILE variable in a short while.

The second code segment from traceSyscall.go is the following:

func main() { var SYSTEMCALLS []string f, err := os.Open(SYSCALLFILE) defer f.Close() if err != nil { fmt.Println(err) return } scanner := bufio.NewScanner(f) ...

Get Mastering Go - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.