Skip to content

Commit

Permalink
Merge pull request #1219 from criblio/feat/1108-ipc-mechanism
Browse files Browse the repository at this point in the history
IPC mechanism #1108
  • Loading branch information
jrcheli authored Jan 11, 2023
2 parents ed58090 + a75b5e5 commit f35f6bb
Show file tree
Hide file tree
Showing 41 changed files with 3,898 additions and 148 deletions.
10 changes: 10 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(go) scope command",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}/cli/main.go",
// "args": ["ps"]
"args": ["inspect", "665601"]
// "args": ["attach", "8896"]
},
{
"name": "(gdb) curl x86_64 w/preload",
"type": "cppdbg",
Expand Down
51 changes: 51 additions & 0 deletions cli/cmd/inspect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cmd

import (
"errors"
"fmt"
"strconv"

"github.com/criblio/scope/inspect"
"github.com/criblio/scope/internal"
"github.com/criblio/scope/ipc"
"github.com/criblio/scope/util"
"github.com/spf13/cobra"
)

var pidCtx *ipc.IpcPidCtx = &ipc.IpcPidCtx{}

// inspectCmd represents the inspect command
var inspectCmd = &cobra.Command{
Use: "inspect",
Short: "Return information on scoped process",
Long: `Return information on scoped process identified by PID.`,
Example: `scope inspect 1000`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
internal.InitConfig()
// Nice message for non-adminstrators
err := util.UserVerifyRootPerm()
if errors.Is(err, util.ErrGetCurrentUser) {
util.ErrAndExit("Unable to get current user: %v", err)
}
if errors.Is(err, util.ErrMissingAdmPriv) {
fmt.Println("INFO: Run as root (or via sudo) to get info from all processes")
}

pid, err := strconv.Atoi(args[0])
if err != nil {
util.ErrAndExit("Convert PID fails: %v", err)
}
pidCtx.Pid = pid
cfg, err := inspect.InspectScopeCfg(*pidCtx)
if err != nil {
util.ErrAndExit("Inspect PID fails: %v", err)
}
fmt.Println(cfg)
},
}

func init() {
ipcCmdFlags(inspectCmd, pidCtx)
RootCmd.AddCommand(inspectCmd)
}
57 changes: 57 additions & 0 deletions cli/cmd/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package cmd

import (
"errors"
"fmt"
"strconv"

"github.com/criblio/scope/internal"
"github.com/criblio/scope/update"
"github.com/criblio/scope/util"
"github.com/spf13/cobra"
)

var cfgPath string

// updateCmd represents the info command
var updateCmd = &cobra.Command{
Use: "update",
Short: "Updates configuration of scoped process",
Long: `Updates configuration of scoped process identified by PID.`,
Example: `scope update 1000 --config test_cfg.yml`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if !util.CheckFileExists(cfgPath) {
util.ErrAndExit("Configuration file: %s does not exist", cfgPath)
}
internal.InitConfig()
// Nice message for non-adminstrators
err := util.UserVerifyRootPerm()
if errors.Is(err, util.ErrGetCurrentUser) {
util.ErrAndExit("Unable to get current user: %v", err)
}
if errors.Is(err, util.ErrMissingAdmPriv) {
fmt.Println("INFO: Run as root (or via sudo) to get info from all processes")
}

pid, err := strconv.Atoi(args[0])
if err != nil {
util.ErrAndExit("Convert PID fails: %v", err)
}

pidCtx.Pid = pid

err = update.UpdateScopeCfg(*pidCtx, cfgPath)
if err != nil {
util.ErrAndExit("Update Scope configuration fails: %v", err)
}
fmt.Println("Update Scope configuration success.")
},
}

func init() {
ipcCmdFlags(updateCmd, pidCtx)
updateCmd.Flags().StringVarP(&cfgPath, "config", "c", "", "Path to configuration file")
updateCmd.MarkFlagRequired("config")
RootCmd.AddCommand(updateCmd)
}
5 changes: 5 additions & 0 deletions cli/cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"

"github.com/criblio/scope/history"
"github.com/criblio/scope/ipc"
"github.com/criblio/scope/run"
"github.com/criblio/scope/util"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -64,3 +65,7 @@ func runCmdFlags(cmd *cobra.Command, rc *run.Config) {
cmd.Flags().StringVarP(&rc.UserConfig, "userconfig", "u", "", "Run ldscope with a user specified config file; overrides all other settings.")
metricAndEventDestFlags(cmd, rc)
}

func ipcCmdFlags(cmd *cobra.Command, ipc *ipc.IpcPidCtx) {
cmd.Flags().StringVarP(&ipc.PrefixPath, "prefix", "p", "", "Prefix to proc filesystem")
}
33 changes: 33 additions & 0 deletions cli/inspect/inspect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package inspect

import (
"encoding/json"
"errors"

"github.com/criblio/scope/ipc"
)

var errInspectCfg = errors.New("error inspect cfg")

// InspectScopeCfg returns the configuration of scoped process
func InspectScopeCfg(pidCtx ipc.IpcPidCtx) (string, error) {

cmd := ipc.CmdGetScopeCfg{}
resp, err := cmd.Request(pidCtx)
if err != nil {
return "", err
}

err = cmd.UnmarshalResp(resp.ResponseScopeMsgData)
if err != nil {
return "", err
}
if resp.MetaMsgStatus != ipc.ResponseOK || *cmd.Response.Status != ipc.ResponseOK {
return "", errInspectCfg
}
marshalToPrint, err := json.MarshalIndent(cmd.Response.Cfg.Current, "", " ")
if err != nil {
return "", err
}
return string(marshalToPrint), nil
}
Loading

0 comments on commit f35f6bb

Please sign in to comment.