2019-04-29 21:25:05 +00:00
|
|
|
// Copyright 2018 The gVisor Authors.
|
2018-06-20 20:30:39 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2018-11-28 22:00:54 +00:00
|
|
|
"context"
|
2019-03-11 18:46:18 +00:00
|
|
|
"os"
|
2018-08-28 03:35:00 +00:00
|
|
|
"syscall"
|
2019-03-11 18:46:18 +00:00
|
|
|
"time"
|
2018-08-28 03:35:00 +00:00
|
|
|
|
2018-06-20 20:30:39 +00:00
|
|
|
"flag"
|
|
|
|
"github.com/google/subcommands"
|
|
|
|
"gvisor.googlesource.com/gvisor/pkg/log"
|
|
|
|
"gvisor.googlesource.com/gvisor/runsc/boot"
|
|
|
|
"gvisor.googlesource.com/gvisor/runsc/container"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Debug implements subcommands.Command for the "debug" command.
|
|
|
|
type Debug struct {
|
2019-03-11 18:46:18 +00:00
|
|
|
pid int
|
|
|
|
stacks bool
|
|
|
|
signal int
|
|
|
|
profileHeap string
|
|
|
|
profileCPU string
|
|
|
|
profileDelay int
|
2019-05-29 18:30:59 +00:00
|
|
|
trace string
|
2018-06-20 20:30:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name implements subcommands.Command.
|
|
|
|
func (*Debug) Name() string {
|
|
|
|
return "debug"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Synopsis implements subcommands.Command.
|
|
|
|
func (*Debug) Synopsis() string {
|
|
|
|
return "shows a variety of debug information"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Usage implements subcommands.Command.
|
|
|
|
func (*Debug) Usage() string {
|
|
|
|
return `debug [flags] <container id>`
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFlags implements subcommands.Command.
|
|
|
|
func (d *Debug) SetFlags(f *flag.FlagSet) {
|
|
|
|
f.IntVar(&d.pid, "pid", 0, "sandbox process ID. Container ID is not necessary if this is set")
|
|
|
|
f.BoolVar(&d.stacks, "stacks", false, "if true, dumps all sandbox stacks to the log")
|
2019-03-11 18:46:18 +00:00
|
|
|
f.StringVar(&d.profileHeap, "profile-heap", "", "writes heap profile to the given file.")
|
|
|
|
f.StringVar(&d.profileCPU, "profile-cpu", "", "writes CPU profile to the given file.")
|
|
|
|
f.IntVar(&d.profileDelay, "profile-delay", 5, "amount of time to wait before stoping CPU profile")
|
2019-05-29 18:30:59 +00:00
|
|
|
f.StringVar(&d.trace, "trace", "", "writes an execution trace to the given file.")
|
2018-08-28 03:35:00 +00:00
|
|
|
f.IntVar(&d.signal, "signal", -1, "sends signal to the sandbox")
|
2018-06-20 20:30:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute implements subcommands.Command.Execute.
|
|
|
|
func (d *Debug) Execute(_ context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
|
|
|
|
var c *container.Container
|
|
|
|
conf := args[0].(*boot.Config)
|
|
|
|
|
|
|
|
if d.pid == 0 {
|
|
|
|
// No pid, container ID must have been provided.
|
|
|
|
if f.NArg() != 1 {
|
|
|
|
f.Usage()
|
|
|
|
return subcommands.ExitUsageError
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
c, err = container.Load(conf.RootDir, f.Arg(0))
|
|
|
|
if err != nil {
|
2019-01-19 01:35:09 +00:00
|
|
|
Fatalf("loading container %q: %v", f.Arg(0), err)
|
2018-06-20 20:30:39 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if f.NArg() != 0 {
|
|
|
|
f.Usage()
|
|
|
|
return subcommands.ExitUsageError
|
|
|
|
}
|
|
|
|
// Go over all sandboxes and find the one that matches PID.
|
|
|
|
ids, err := container.List(conf.RootDir)
|
|
|
|
if err != nil {
|
2019-01-19 01:35:09 +00:00
|
|
|
Fatalf("listing containers: %v", err)
|
2018-06-20 20:30:39 +00:00
|
|
|
}
|
|
|
|
for _, id := range ids {
|
|
|
|
candidate, err := container.Load(conf.RootDir, id)
|
|
|
|
if err != nil {
|
2019-01-19 01:35:09 +00:00
|
|
|
Fatalf("loading container %q: %v", id, err)
|
2018-06-20 20:30:39 +00:00
|
|
|
}
|
2018-10-10 04:06:18 +00:00
|
|
|
if candidate.SandboxPid() == d.pid {
|
2018-06-20 20:30:39 +00:00
|
|
|
c = candidate
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c == nil {
|
|
|
|
Fatalf("container with PID %d not found", d.pid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-13 23:36:53 +00:00
|
|
|
if c.Sandbox == nil || !c.Sandbox.IsRunning() {
|
|
|
|
Fatalf("container sandbox is not running")
|
2018-06-20 20:30:39 +00:00
|
|
|
}
|
2018-09-13 23:36:53 +00:00
|
|
|
log.Infof("Found sandbox %q, PID: %d", c.Sandbox.ID, c.Sandbox.Pid)
|
2018-06-20 20:30:39 +00:00
|
|
|
|
2018-08-28 03:35:00 +00:00
|
|
|
if d.signal > 0 {
|
|
|
|
log.Infof("Sending signal %d to process: %d", d.signal, c.Sandbox.Pid)
|
|
|
|
if err := syscall.Kill(c.Sandbox.Pid, syscall.Signal(d.signal)); err != nil {
|
|
|
|
Fatalf("failed to send signal %d to processs %d", d.signal, c.Sandbox.Pid)
|
|
|
|
}
|
|
|
|
}
|
2018-06-20 20:30:39 +00:00
|
|
|
if d.stacks {
|
|
|
|
log.Infof("Retrieving sandbox stacks")
|
|
|
|
stacks, err := c.Sandbox.Stacks()
|
|
|
|
if err != nil {
|
2019-01-19 01:35:09 +00:00
|
|
|
Fatalf("retrieving stacks: %v", err)
|
2018-06-20 20:30:39 +00:00
|
|
|
}
|
|
|
|
log.Infof(" *** Stack dump ***\n%s", stacks)
|
|
|
|
}
|
2019-05-29 18:30:59 +00:00
|
|
|
if d.profileHeap != "" {
|
|
|
|
f, err := os.Create(d.profileHeap)
|
2019-03-11 18:46:18 +00:00
|
|
|
if err != nil {
|
|
|
|
Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2019-05-29 18:30:59 +00:00
|
|
|
if err := c.Sandbox.HeapProfile(f); err != nil {
|
2019-03-11 18:46:18 +00:00
|
|
|
Fatalf(err.Error())
|
|
|
|
}
|
2019-05-29 18:30:59 +00:00
|
|
|
log.Infof("Heap profile written to %q", d.profileHeap)
|
|
|
|
}
|
2019-03-11 18:46:18 +00:00
|
|
|
|
2019-05-29 18:30:59 +00:00
|
|
|
delay := false
|
|
|
|
if d.profileCPU != "" {
|
|
|
|
delay = true
|
|
|
|
f, err := os.Create(d.profileCPU)
|
|
|
|
if err != nil {
|
2019-03-11 18:46:18 +00:00
|
|
|
Fatalf(err.Error())
|
|
|
|
}
|
2019-05-29 18:30:59 +00:00
|
|
|
defer func() {
|
|
|
|
f.Close()
|
|
|
|
if err := c.Sandbox.StopCPUProfile(); err != nil {
|
|
|
|
Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
log.Infof("CPU profile written to %q", d.profileCPU)
|
|
|
|
}()
|
|
|
|
if err := c.Sandbox.StartCPUProfile(f); err != nil {
|
|
|
|
Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
log.Infof("CPU profile started for %d sec, writing to %q", d.profileDelay, d.profileCPU)
|
2019-03-11 18:46:18 +00:00
|
|
|
}
|
2019-05-29 18:30:59 +00:00
|
|
|
if d.trace != "" {
|
|
|
|
delay = true
|
|
|
|
f, err := os.Create(d.trace)
|
2019-03-11 18:46:18 +00:00
|
|
|
if err != nil {
|
|
|
|
Fatalf(err.Error())
|
|
|
|
}
|
2019-05-29 18:30:59 +00:00
|
|
|
defer func() {
|
|
|
|
f.Close()
|
|
|
|
if err := c.Sandbox.StopTrace(); err != nil {
|
|
|
|
Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
log.Infof("Trace written to %q", d.trace)
|
|
|
|
}()
|
|
|
|
if err := c.Sandbox.StartTrace(f); err != nil {
|
2019-03-11 18:46:18 +00:00
|
|
|
Fatalf(err.Error())
|
|
|
|
}
|
2019-05-29 18:30:59 +00:00
|
|
|
log.Infof("Tracing started for %d sec, writing to %q", d.profileDelay, d.trace)
|
|
|
|
|
2019-03-11 18:46:18 +00:00
|
|
|
}
|
2019-05-29 18:30:59 +00:00
|
|
|
|
|
|
|
if delay {
|
|
|
|
time.Sleep(time.Duration(d.profileDelay) * time.Second)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-06-20 20:30:39 +00:00
|
|
|
return subcommands.ExitSuccess
|
|
|
|
}
|