github.com/MerlinKodo/gvisor@v0.0.0-20231110090155-957f62ecf90e/runsc/cmd/trace/metadata.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  	"sort"
    21  	"strings"
    22  
    23  	"github.com/MerlinKodo/gvisor/pkg/sentry/seccheck"
    24  	"github.com/MerlinKodo/gvisor/runsc/flag"
    25  	"github.com/google/subcommands"
    26  )
    27  
    28  // metadata implements subcommands.Command for the "metadata" command.
    29  type metadata struct{}
    30  
    31  // Name implements subcommands.Command.
    32  func (*metadata) Name() string {
    33  	return "metadata"
    34  }
    35  
    36  // Synopsis implements subcommands.Command.
    37  func (*metadata) Synopsis() string {
    38  	return "list all trace points configuration information"
    39  }
    40  
    41  // Usage implements subcommands.Command.
    42  func (*metadata) Usage() string {
    43  	return `metadata - list all trace points configuration information
    44  `
    45  }
    46  
    47  // SetFlags implements subcommands.Command.
    48  func (*metadata) SetFlags(*flag.FlagSet) {}
    49  
    50  // Execute implements subcommands.Command.
    51  func (l *metadata) Execute(context.Context, *flag.FlagSet, ...any) subcommands.ExitStatus {
    52  	// Sort to keep related points together.
    53  	points := make([]seccheck.PointDesc, 0, len(seccheck.Points))
    54  	for _, pt := range seccheck.Points {
    55  		points = append(points, pt)
    56  	}
    57  	sort.Slice(points, func(i int, j int) bool {
    58  		return points[i].Name < points[j].Name
    59  	})
    60  
    61  	fmt.Printf("POINTS (%d)\n", len(seccheck.Points))
    62  	for _, pt := range points {
    63  		optFields := fieldNames(pt.OptionalFields)
    64  		ctxFields := fieldNames(pt.ContextFields)
    65  		fmt.Printf("Name: %s, optional fields: [%s], context fields: [%s]\n", pt.Name, strings.Join(optFields, "|"), strings.Join(ctxFields, "|"))
    66  	}
    67  	fmt.Printf("\nSINKS (%d)\n", len(seccheck.Sinks))
    68  	for _, sink := range seccheck.Sinks {
    69  		fmt.Printf("Name: %s\n", sink.Name)
    70  	}
    71  
    72  	return subcommands.ExitSuccess
    73  }
    74  
    75  func fieldNames(fields []seccheck.FieldDesc) []string {
    76  	names := make([]string, 0, len(fields))
    77  	for _, f := range fields {
    78  		names = append(names, f.Name)
    79  	}
    80  	return names
    81  }