github.com/MerlinKodo/gvisor@v0.0.0-20231110090155-957f62ecf90e/runsc/cmd/trace/procfs.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 "encoding/json" 20 "fmt" 21 22 "github.com/MerlinKodo/gvisor/pkg/log" 23 "github.com/MerlinKodo/gvisor/runsc/cmd/util" 24 "github.com/MerlinKodo/gvisor/runsc/config" 25 "github.com/MerlinKodo/gvisor/runsc/container" 26 "github.com/MerlinKodo/gvisor/runsc/flag" 27 "github.com/google/subcommands" 28 ) 29 30 // procfs implements subcommands.Command for the "procfs" command. 31 type procfs struct { 32 } 33 34 // Name implements subcommands.Command. 35 func (*procfs) Name() string { 36 return "procfs" 37 } 38 39 // Synopsis implements subcommands.Command. 40 func (*procfs) Synopsis() string { 41 return "dump procfs state for sandbox" 42 } 43 44 // Usage implements subcommands.Command. 45 func (*procfs) Usage() string { 46 return `procfs <sandbox id> - get procfs dump for a trace session 47 ` 48 } 49 50 // SetFlags implements subcommands.Command. 51 func (*procfs) SetFlags(*flag.FlagSet) {} 52 53 // Execute implements subcommands.Command. 54 func (*procfs) Execute(_ context.Context, f *flag.FlagSet, args ...any) subcommands.ExitStatus { 55 if f.NArg() != 1 { 56 f.Usage() 57 return subcommands.ExitUsageError 58 } 59 60 id := f.Arg(0) 61 conf := args[0].(*config.Config) 62 63 opts := container.LoadOpts{ 64 SkipCheck: true, 65 RootContainer: true, 66 } 67 c, err := container.Load(conf.RootDir, container.FullID{ContainerID: id}, opts) 68 if err != nil { 69 util.Fatalf("loading sandbox: %v", err) 70 } 71 72 dump, err := c.Sandbox.ProcfsDump() 73 if err != nil { 74 util.Fatalf("procfs dump: %v", err) 75 } 76 77 fmt.Println("PROCFS DUMP") 78 for _, procDump := range dump { 79 out, err := json.Marshal(procDump) 80 if err != nil { 81 log.Warningf("json.Marshal failed to marshal %+v: %v", procDump, err) 82 continue 83 } 84 85 fmt.Println("") 86 fmt.Println(string(out)) 87 } 88 return subcommands.ExitSuccess 89 }