婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > Golang標準庫syscall詳解(什么是系統調用)

Golang標準庫syscall詳解(什么是系統調用)

熱門標簽:揚州電銷外呼系統軟件 百度地圖標注位置網站 武漢百應人工智能電銷機器人 電腦外呼系統輻射大嗎 上海企業外呼系統排名 智能語音電銷的機器人 開通400電話申請流程 如何利用高德地圖標注家 400手機電話免費辦理

一、什么是系統調用

In computing, a system call is the programmatic way in which a computer program requests a service from the kernel of the operating system it is executed on. This may include hardware-related services (for example, accessing a hard disk drive), creation and execution of new processes, and communication with integral kernel services such as process scheduling. System calls provide an essential interface between a process and the operating system.

系統調用是程序向操作系統內核請求服務的過程,通常包含硬件相關的服務(例如訪問硬盤),創建新進程等。系統調用提供了一個進程和操作系統之間的接口。

二、Golang標準庫-syscall

syscall包包含一個指向底層操作系統原語的接口。

注意:該軟件包已被鎖定。標準以外的代碼應該被遷移到golang.org/x/sys存儲庫中使用相應的軟件包。這也是應用新系統或版本所需更新的地方。 Signal , Errno 和 SysProcAttr 在 golang.org/x/sys 中尚不可用,并且仍然必須從 syscall 程序包中引用。有關更多信息,請參見 https://golang.org/s/go1.4-syscall。

https://pkg.go.dev/golang.org/x/sys
該存儲庫包含用于與操作系統進行低級交互的補充Go軟件包。

1. syscall無處不在

舉個最常用的例子, fmt.Println(“hello world”), 這里就用到了系統調用 write, 我們翻一下源碼。

func Println(a ...interface{}) (n int, err error) {
	return Fprintln(os.Stdout, a...)
}
Stdout = NewFile(uintptr(syscall.Stdout), "/dev/stdout")
 
func (f *File) write(b []byte) (n int, err error) {
    if len(b) == 0 {
        return 0, nil
    }
    // 實際的write方法,就是調用syscall.Write()
    return fixCount(syscall.Write(f.fd, b))
}

2. syscall demo舉例:

 go版本的strace Strace

strace 是用于查看進程系統調用的工具, 一般使用方法如下:

strace -c 用于統計各個系統調用的次數

[root@localhost ~]# strace -c echo hello
hello
% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
  0.00    0.000000           0         1           read
  0.00    0.000000           0         1           write
  0.00    0.000000           0         3           open
  0.00    0.000000           0         5           close
  0.00    0.000000           0         4           fstat
  0.00    0.000000           0         9           mmap
  0.00    0.000000           0         4           mprotect
  0.00    0.000000           0         2           munmap
  0.00    0.000000           0         4           brk
  0.00    0.000000           0         1         1 access
  0.00    0.000000           0         1           execve
  0.00    0.000000           0         1           arch_prctl
------ ----------- ----------- --------- --------- ----------------
100.00    0.000000                    36         1 total
[root@localhost ~]#

stace 的實現原理是系統調用 ptrace, 我們來看下 ptrace 是什么。

man page 描述如下:

The ptrace() system call provides a means by which one process (the “tracer”) may observe and control the execution of another process (the “tracee”), and examine and change the tracee's memory and registers. It is primarily used to implement breakpoint debuggingand system call tracing.

簡單來說有三大能力:

追蹤系統調用
讀寫內存和寄存器
向被追蹤程序傳遞信號

ptrace接口:

int ptrace(int request, pid_t pid, caddr_t addr, int data);
 
request包含:
PTRACE_ATTACH
PTRACE_SYSCALL
PTRACE_PEEKTEXT, PTRACE_PEEKDATA
等

tracer 使用 PTRACE_ATTACH 命令,指定需要追蹤的PID。緊接著調用 PTRACE_SYSCALL。
tracee 會一直運行,直到遇到系統調用,內核會停止執行。 此時,tracer 會收到 SIGTRAP 信號,tracer 就可以打印內存和寄存器中的信息了。

接著,tracer 繼續調用 PTRACE_SYSCALL, tracee 繼續執行,直到 tracee退出當前的系統調用。
需要注意的是,這里在進入syscall和退出syscall時,tracer都會察覺。

go版本的strace

了解以上內容后,presenter 現場實現了一個go版本的strace, 需要在 linux amd64 環境編譯。
https://github.com/silentred/gosys

// strace.go

package main
 
import (
    "fmt"
    "os"
    "os/exec"
    "syscall"
)
 
func main() {
    var err error
    var regs syscall.PtraceRegs
    var ss syscallCounter
    ss = ss.init()
 
    fmt.Println("Run: ", os.Args[1:])
 
    cmd := exec.Command(os.Args[1], os.Args[2:]...)
    cmd.Stderr = os.Stderr
    cmd.Stdout = os.Stdout
    cmd.Stdin = os.Stdin
    cmd.SysProcAttr = syscall.SysProcAttr{
        Ptrace: true,
    }
 
    cmd.Start()
    err = cmd.Wait()
    if err != nil {
        fmt.Printf("Wait err %v \n", err)
    }
 
    pid := cmd.Process.Pid
    exit := true
 
    for {
        // 記得 PTRACE_SYSCALL 會在進入和退出syscall時使 tracee 暫停,所以這里用一個變量控制,RAX的內容只打印一遍
        if exit {
            err = syscall.PtraceGetRegs(pid, regs)
            if err != nil {
                break
            }
            //fmt.Printf("%#v \n",regs)
            name := ss.getName(regs.Orig_rax)
            fmt.Printf("name: %s, id: %d \n", name, regs.Orig_rax)
            ss.inc(regs.Orig_rax)
        }
        // 上面Ptrace有提到的一個request命令
        err = syscall.PtraceSyscall(pid, 0)
        if err != nil {
            panic(err)
        }
        // 猜測是等待進程進入下一個stop,這里如果不等待,那么會打印大量重復的調用函數名
        _, err = syscall.Wait4(pid, nil, 0, nil)
        if err != nil {
            panic(err)
        }
 
        exit = !exit
    }
 
    ss.print()
}

// 用于統計信息的counter, syscallcounter.go

package main
 
import (
    "fmt"
    "os"
    "text/tabwriter"
 
    "github.com/seccomp/libseccomp-golang"
)
 
type syscallCounter []int
 
const maxSyscalls = 303
 
func (s syscallCounter) init() syscallCounter {
    s = make(syscallCounter, maxSyscalls)
    return s
}
 
func (s syscallCounter) inc(syscallID uint64) error {
    if syscallID > maxSyscalls {
        return fmt.Errorf("invalid syscall ID (%x)", syscallID)
    }
 
    s[syscallID]++
    return nil
}
 
func (s syscallCounter) print() {
    w := tabwriter.NewWriter(os.Stdout, 0, 0, 8, ' ', tabwriter.AlignRight|tabwriter.Debug)
    for k, v := range s {
        if v > 0 {
            name, _ := seccomp.ScmpSyscall(k).GetName()
            fmt.Fprintf(w, "%d\t%s\n", v, name)
        }
    }
    w.Flush()
}
 
func (s syscallCounter) getName(syscallID uint64) string {
    name, _ := seccomp.ScmpSyscall(syscallID).GetName()
    return name
}

最后結果:

Run:  [echo hello]
Wait err stop signal: trace/breakpoint trap
name: execve, id: 59
name: brk, id: 12
name: access, id: 21
name: mmap, id: 9
name: access, id: 21
name: open, id: 2
name: fstat, id: 5
name: mmap, id: 9
name: close, id: 3
name: access, id: 21
name: open, id: 2
name: read, id: 0
name: fstat, id: 5
name: mmap, id: 9
name: mprotect, id: 10
name: mmap, id: 9
name: mmap, id: 9
name: close, id: 3
name: mmap, id: 9
name: arch_prctl, id: 158
name: mprotect, id: 10
name: mprotect, id: 10
name: mprotect, id: 10
name: munmap, id: 11
name: brk, id: 12
name: brk, id: 12
name: open, id: 2
name: fstat, id: 5
name: mmap, id: 9
name: close, id: 3
name: fstat, id: 5
hello
name: write, id: 1
name: close, id: 3
name: close, id: 3
        1|read
        1|write
        3|open
        5|close
        4|fstat
        7|mmap
        4|mprotect
        1|munmap
        3|brk
        3|access
        1|execve
        1|arch_prctl

三、參考

Golang標準庫——syscall
參考URL: https://www.jianshu.com/p/44109d5e045b
Golang 與系統調用
參考URL: https://blog.csdn.net/weixin_33744141/article/details/89033990

以上就是Golang標準庫syscall詳解(什么是系統調用)的詳細內容,更多關于Golang標準庫syscall的資料請關注腳本之家其它相關文章!

您可能感興趣的文章:
  • 解決Golang 中使用WaitGroup的那點坑
  • 在golang中使用Sync.WaitGroup解決等待的問題
  • Golang中的sync包的WaitGroup操作
  • Golang中的sync.WaitGroup用法實例
  • Golang的os標準庫中常用函數的整理介紹
  • Golang 標準庫 tips之waitgroup詳解

標簽:宜賓 江西 張掖 黑龍江 延邊 嘉峪關 武漢 新余

巨人網絡通訊聲明:本文標題《Golang標準庫syscall詳解(什么是系統調用)》,本文關鍵詞  Golang,標準,庫,syscall,詳解,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Golang標準庫syscall詳解(什么是系統調用)》相關的同類信息!
  • 本頁收集關于Golang標準庫syscall詳解(什么是系統調用)的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 方山县| 康平县| 延边| 花莲县| 锡林郭勒盟| 绵阳市| 沂水县| 玉屏| 化德县| 永昌县| 河池市| 宽甸| 惠来县| 阿鲁科尔沁旗| 和平区| 普安县| 永胜县| 综艺| 徐水县| 荆门市| 五原县| 常熟市| 象山县| 共和县| 亚东县| 柞水县| 桂阳县| 翁源县| 孟津县| 星子县| 维西| 师宗县| 鄱阳县| 临夏市| 涟源市| 泾源县| 磴口县| 兴化市| 汉中市| 台东市| 河东区|