gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/runsc/cmd/trace/list.go (about)

     1  // Copyright 2022 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package trace
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"github.com/google/subcommands"
    22  	"gvisor.dev/gvisor/runsc/cmd/util"
    23  	"gvisor.dev/gvisor/runsc/config"
    24  	"gvisor.dev/gvisor/runsc/container"
    25  	"gvisor.dev/gvisor/runsc/flag"
    26  )
    27  
    28  // list implements subcommands.Command for the "list" command.
    29  type list struct{}
    30  
    31  // Name implements subcommands.Command.
    32  func (*list) Name() string {
    33  	return "list"
    34  }
    35  
    36  // Synopsis implements subcommands.Command.
    37  func (*list) Synopsis() string {
    38  	return "list all trace sessions"
    39  }
    40  
    41  // Usage implements subcommands.Command.
    42  func (*list) Usage() string {
    43  	return `list - list all trace sessions
    44  `
    45  }
    46  
    47  // SetFlags implements subcommands.Command.
    48  func (*list) SetFlags(*flag.FlagSet) {}
    49  
    50  // Execute implements subcommands.Command.
    51  func (l *list) Execute(_ context.Context, f *flag.FlagSet, args ...any) subcommands.ExitStatus {
    52  	if f.NArg() != 1 {
    53  		f.Usage()
    54  		return subcommands.ExitUsageError
    55  	}
    56  
    57  	id := f.Arg(0)
    58  	conf := args[0].(*config.Config)
    59  
    60  	opts := container.LoadOpts{
    61  		SkipCheck:     true,
    62  		RootContainer: true,
    63  	}
    64  	c, err := container.Load(conf.RootDir, container.FullID{ContainerID: id}, opts)
    65  	if err != nil {
    66  		util.Fatalf("loading sandbox: %v", err)
    67  	}
    68  
    69  	sessions, err := c.Sandbox.ListTraceSessions()
    70  	if err != nil {
    71  		util.Fatalf("listing sessions: %v", err)
    72  	}
    73  	fmt.Printf("SESSIONS (%d)\n", len(sessions))
    74  	for _, session := range sessions {
    75  		fmt.Printf("%q\n", session.Name)
    76  		for _, sink := range session.Sinks {
    77  			fmt.Printf("\tSink: %q, dropped: %d\n", sink.Name, sink.Status.DroppedCount)
    78  		}
    79  	}
    80  	return subcommands.ExitSuccess
    81  }