chg(ptrace): Move c files out from go directory and fix build.
authorsgf <sgf.dma@gmail.com>
Mon, 4 Dec 2023 20:51:48 +0000 (23:51 +0300)
committersgf <sgf.dma@gmail.com>
Tue, 5 Dec 2023 20:57:52 +0000 (23:57 +0300)
containers_from_scratch/guide_to_syscalls/.gitignore [new file with mode: 0644]
containers_from_scratch/guide_to_syscalls/hello/.gitignore [new file with mode: 0644]
containers_from_scratch/guide_to_syscalls/hello/go.mod [new file with mode: 0644]
containers_from_scratch/guide_to_syscalls/hello/hello.go [new file with mode: 0644]
containers_from_scratch/guide_to_syscalls/myStrace/.gitignore [new file with mode: 0644]
containers_from_scratch/guide_to_syscalls/myStrace/go.mod [new file with mode: 0644]
containers_from_scratch/guide_to_syscalls/myStrace/go.sum [new file with mode: 0644]
containers_from_scratch/guide_to_syscalls/myStrace/main.go [new file with mode: 0644]
containers_from_scratch/guide_to_syscalls/ptrace-chain.c [moved from containers_from_scratch/guide_to_syscalls/myStrace/ptrace-chain.c with 100% similarity]

diff --git a/containers_from_scratch/guide_to_syscalls/.gitignore b/containers_from_scratch/guide_to_syscalls/.gitignore
new file mode 100644 (file)
index 0000000..cba7efc
--- /dev/null
@@ -0,0 +1 @@
+a.out
diff --git a/containers_from_scratch/guide_to_syscalls/hello/.gitignore b/containers_from_scratch/guide_to_syscalls/hello/.gitignore
new file mode 100644 (file)
index 0000000..ce01362
--- /dev/null
@@ -0,0 +1 @@
+hello
diff --git a/containers_from_scratch/guide_to_syscalls/hello/go.mod b/containers_from_scratch/guide_to_syscalls/hello/go.mod
new file mode 100644 (file)
index 0000000..f0a0072
--- /dev/null
@@ -0,0 +1,3 @@
+module hello
+
+go 1.21.4
diff --git a/containers_from_scratch/guide_to_syscalls/hello/hello.go b/containers_from_scratch/guide_to_syscalls/hello/hello.go
new file mode 100644 (file)
index 0000000..15a1421
--- /dev/null
@@ -0,0 +1,10 @@
+
+package main
+
+import (
+    "fmt"
+)
+
+func main() {
+    fmt.Println("Hello world")
+}
diff --git a/containers_from_scratch/guide_to_syscalls/myStrace/.gitignore b/containers_from_scratch/guide_to_syscalls/myStrace/.gitignore
new file mode 100644 (file)
index 0000000..83bad49
--- /dev/null
@@ -0,0 +1 @@
+myStrace
diff --git a/containers_from_scratch/guide_to_syscalls/myStrace/go.mod b/containers_from_scratch/guide_to_syscalls/myStrace/go.mod
new file mode 100644 (file)
index 0000000..9a51c5b
--- /dev/null
@@ -0,0 +1,5 @@
+module myStrace
+
+go 1.21.4
+
+require github.com/seccomp/libseccomp-golang v0.10.0
diff --git a/containers_from_scratch/guide_to_syscalls/myStrace/go.sum b/containers_from_scratch/guide_to_syscalls/myStrace/go.sum
new file mode 100644 (file)
index 0000000..f5e6023
--- /dev/null
@@ -0,0 +1,2 @@
+github.com/seccomp/libseccomp-golang v0.10.0 h1:aA4bp+/Zzi0BnWZ2F1wgNBs5gTpm+na2rWM6M9YjLpY=
+github.com/seccomp/libseccomp-golang v0.10.0/go.mod h1:JA8cRccbGaA1s33RQf7Y1+q9gHmZX1yB/z9WDN1C6fg=
diff --git a/containers_from_scratch/guide_to_syscalls/myStrace/main.go b/containers_from_scratch/guide_to_syscalls/myStrace/main.go
new file mode 100644 (file)
index 0000000..f935ce8
--- /dev/null
@@ -0,0 +1,77 @@
+
+package main
+
+import (
+    "fmt"
+    "os"
+    "os/exec"
+    "syscall"
+    "time"
+
+    sec "github.com/seccomp/libseccomp-golang"
+)
+
+func SsGetName(ss uint64) string {
+    v := sec.ScmpSyscall(ss)
+    name, err := v.GetName()
+    if err != nil {
+        panic(err)
+    }
+    return name
+}
+
+func main() {
+    fmt.Printf("Run %v\n", os.Args[1:])
+
+    cmd := exec.Command(os.Args[1], os.Args[2:]...)
+    cmd.SysProcAttr = &syscall.SysProcAttr {
+        Ptrace: true,
+    }
+    cmd.Stderr = os.Stderr
+    cmd.Stdin = os.Stdin
+    cmd.Stdout = os.Stdout
+
+    cmd.Start()
+    if err := cmd.Wait(); err != nil {
+        fmt.Printf("Wait returned: %v\n", err)
+    }
+    exit := true
+
+    time.Sleep(time.Second * 3)
+
+    pid := cmd.Process.Pid
+
+    var regs syscall.PtraceRegs
+    var ws syscall.WaitStatus
+    for {
+        if exit {
+            if err := syscall.PtraceGetRegs(pid, &regs); err != nil {
+                panic(err)
+            }
+            //time.Sleep(700 * time.Millisecond)
+            time.Sleep(time.Second)
+            fmt.Printf("%#v\n", SsGetName(regs.Orig_rax))
+        }
+
+        if err := syscall.PtraceSyscall(pid, 0); err != nil {
+            panic(err)
+        }
+
+        if _, err := syscall.Wait4(pid, &ws, 0, nil); err != nil {
+            panic(err)
+        }
+
+        if ws.Exited() {
+            fmt.Printf("Exited: %v\n", ws.ExitStatus())
+            break
+        } else if ws.Signaled() {
+            fmt.Printf("Killed by signal: %v\n", ws.Signal().String())
+        } else if ws.Stopped() {
+            fmt.Printf("Stopped by signal: %v\n", ws.StopSignal().String())
+        } else if ws.Continued() {
+            fmt.Printf("Continued\n")
+        }
+
+        exit = !exit
+    }
+}