From 08beba2babe038b80ef77bf73384b0fefcdde3be Mon Sep 17 00:00:00 2001 From: Mikhail Klementev Date: Sun, 19 Mar 2023 13:14:14 +0000 Subject: [PATCH] Add debug logs for exec.Command --- images.go | 3 +++ kernel.go | 26 +++++++++++++++++++++++++- kernel_linux.go | 2 ++ main.go | 14 ++++++++++++++ pew.go | 7 ++++++- qemu/qemu-kernel.go | 3 +++ 6 files changed, 53 insertions(+), 2 deletions(-) diff --git a/images.go b/images.go index 53df892..e7b51fb 100644 --- a/images.go +++ b/images.go @@ -17,6 +17,7 @@ import ( "code.dumpstack.io/tools/out-of-tree/config" "code.dumpstack.io/tools/out-of-tree/qemu" + "github.com/rs/zerolog/log" ) type ImageCmd struct { @@ -146,6 +147,8 @@ func unpackTar(archive, destination string) (err error) { cmd := exec.Command("tar", "-Sxf", archive) cmd.Dir = destination + "/" + log.Debug().Msgf("%v", cmd) + rawOutput, err := cmd.CombinedOutput() if err != nil { err = fmt.Errorf("%v: %s", err, rawOutput) diff --git a/kernel.go b/kernel.go index 86b5e91..8b109e5 100644 --- a/kernel.go +++ b/kernel.go @@ -99,6 +99,8 @@ func (cmd *KernelDockerRegenCmd) Run(kernelCmd *KernelCmd, g *Globals) (err erro args = append(args, "-t", d.ContainerName, imagePath) cmd := exec.Command("docker", args...) + log.Debug().Msgf("%v", cmd) + var rawOutput []byte rawOutput, err = cmd.CombinedOutput() if err != nil { @@ -244,6 +246,8 @@ func generateBaseDockerImage(registry string, commands []config.DockerCommand, d := "# BASE\n" cmd := exec.Command("docker", "images", "-q", sk.DockerName()) + log.Debug().Msgf("%v", cmd) + rawOutput, err := cmd.CombinedOutput() if err != nil { return @@ -349,6 +353,8 @@ func generateBaseDockerImage(registry string, commands []config.DockerCommand, args = append(args, "-t", sk.DockerName(), imagePath) cmd = exec.Command("docker", args...) + log.Debug().Msgf("%v", cmd) + rawOutput, err = cmd.CombinedOutput() if err != nil { log.Printf("Base image for %s:%s generating error, see log", @@ -419,6 +425,8 @@ func dockerImageAppend(sk config.KernelMask, pkgname string) (err error) { args = append(args, "-t", sk.DockerName(), imagePath) cmd := exec.Command("docker", args...) + log.Debug().Msgf("%v", cmd) + rawOutput, err := cmd.CombinedOutput() if err != nil { // Fallback to previous state @@ -441,12 +449,16 @@ func dockerImageAppend(sk config.KernelMask, pkgname string) (err error) { func kickImage(name string) (err error) { cmd := exec.Command("docker", "run", name, "bash", "-c", "ls") + log.Debug().Msgf("%v", cmd) + _, err = cmd.CombinedOutput() return } func copyKernels(name string) (err error) { cmd := exec.Command("docker", "ps", "-a") + log.Debug().Msgf("%v", cmd) + rawOutput, err := cmd.CombinedOutput() if err != nil { log.Print(string(rawOutput)) @@ -463,7 +475,9 @@ func copyKernels(name string) (err error) { what := r.FindAll(rawOutput, -1) for _, w := range what { containerID = strings.Fields(string(w))[0] - _, err = exec.Command("which", "podman").CombinedOutput() + cmd = exec.Command("which", "podman") + log.Debug().Msgf("%v", cmd) + _, err = cmd.CombinedOutput() if err != nil { break } @@ -480,6 +494,8 @@ func copyKernels(name string) (err error) { } cmd = exec.Command("docker", "cp", containerID+":/boot/.", target) + log.Debug().Msgf("%v", cmd) + rawOutput, err = cmd.CombinedOutput() if err != nil { log.Print(string(rawOutput)) @@ -487,6 +503,8 @@ func copyKernels(name string) (err error) { } cmd = exec.Command("docker", "cp", containerID+":/lib/modules", target) + log.Debug().Msgf("%v", cmd) + rawOutput, err = cmd.CombinedOutput() if err != nil { log.Print(string(rawOutput)) @@ -494,6 +512,8 @@ func copyKernels(name string) (err error) { } cmd = exec.Command("find", target+"modules", "-type", "l", "-delete") + log.Debug().Msgf("%v", cmd) + rawOutput, err = cmd.CombinedOutput() if err != nil { log.Print(string(rawOutput)) @@ -555,6 +575,8 @@ type dockerImageInfo struct { func listDockerImages() (diis []dockerImageInfo, err error) { cmd := exec.Command("docker", "images") + log.Debug().Msgf("%v", cmd) + rawOutput, err := cmd.CombinedOutput() if err != nil { return @@ -648,6 +670,8 @@ func genDockerKernels(dii dockerImageInfo, newkcfg *config.KernelConfig, name := dii.ContainerName cmd := exec.Command("docker", "run", name, "ls", "/lib/modules") + log.Debug().Msgf("%v", cmd) + rawOutput, err := cmd.CombinedOutput() if err != nil { log.Print(string(rawOutput), err) diff --git a/kernel_linux.go b/kernel_linux.go index 3b8b2cc..1dd6287 100644 --- a/kernel_linux.go +++ b/kernel_linux.go @@ -28,6 +28,8 @@ func genHostKernels(download bool) (kcfg config.KernelConfig, err error) { } cmd := exec.Command("ls", "/lib/modules") + log.Debug().Msgf("%v", cmd) + rawOutput, err := cmd.CombinedOutput() if err != nil { log.Print(string(rawOutput), err) diff --git a/main.go b/main.go index 3025fd1..41b6fd4 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "fmt" "math/rand" "os" + "strconv" "time" "github.com/rs/zerolog" @@ -69,6 +70,19 @@ func (v VersionFlag) BeforeApply(app *kong.Kong, vars kong.Vars) error { func main() { log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) + zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string { + short := file + for i := len(file) - 1; i > 0; i-- { + if file[i] == '/' { + short = file[i+1:] + break + } + } + file = short + return file + ":" + strconv.Itoa(line) + } + log.Logger = log.With().Caller().Logger() + rand.Seed(time.Now().UnixNano()) cli := CLI{} diff --git a/pew.go b/pew.go index 72af76c..e8a0afb 100644 --- a/pew.go +++ b/pew.go @@ -174,8 +174,10 @@ func dockerRun(timeout time.Duration, container, workdir, command string) ( } func sh(workdir, cmd string) (output string, err error) { - log.Debug().Str("workdir", workdir).Str("cmd", cmd).Msg("run command") command := exec.Command("sh", "-c", "cd "+workdir+" && "+cmd) + + log.Debug().Msgf("%v", command) + raw, err := command.CombinedOutput() output = string(raw) if err != nil { @@ -265,6 +267,9 @@ func build(tmp string, ka config.Artifact, ki config.KernelInfo, } else { cmd := exec.Command("bash", "-c", "cd "+outdir+" && "+ buildCommand) + + log.Debug().Msgf("%v", cmd) + timer := time.AfterFunc(dockerTimeout, func() { cmd.Process.Kill() }) diff --git a/qemu/qemu-kernel.go b/qemu/qemu-kernel.go index 38f2dda..cb57b85 100644 --- a/qemu/qemu-kernel.go +++ b/qemu/qemu-kernel.go @@ -18,6 +18,7 @@ import ( "syscall" "time" + "github.com/rs/zerolog/log" "golang.org/x/crypto/ssh" ) @@ -257,6 +258,7 @@ func (q *System) Start() (err error) { qemuArgs = append(qemuArgs, "-append", q.cmdline()) q.cmd = exec.Command("qemu-system-"+string(q.arch), qemuArgs...) + log.Debug().Msgf("%v", q.cmd) if q.pipe.stdin, err = q.cmd.StdinPipe(); err != nil { return @@ -392,6 +394,7 @@ func (q System) scp(user, localPath, remotePath string, recursive bool) (err err args = append(args, localPath, user+"@"+addr+":"+remotePath) cmd := exec.Command("scp", args...) + log.Debug().Msgf("%v", cmd) output, err := cmd.CombinedOutput() if err != nil || string(output) != "" {