cmd/dlv: Unexport flag variables

This commit is contained in:
Derek Parker 2020-03-31 15:10:37 -07:00 committed by Alessandro Arzilli
parent 3c8d4d52b8
commit 3cf685e5ea

@ -27,42 +27,42 @@ import (
) )
var ( var (
// Log is whether to log debug statements. // log is whether to log debug statements.
Log bool log bool
// LogOutput is a comma separated list of components that should produce debug output. // logOutput is a comma separated list of components that should produce debug output.
LogOutput string logOutput string
// LogDest is the file path or file descriptor where logs should go. // logDest is the file path or file descriptor where logs should go.
LogDest string logDest string
// Headless is whether to run without terminal. // headless is whether to run without terminal.
Headless bool headless bool
// ContinueOnStart is whether to continue the process on startup // continueOnStart is whether to continue the process on startup
ContinueOnStart bool continueOnStart bool
// APIVersion is the requested API version while running headless // apiVersion is the requested API version while running headless
APIVersion int apiVersion int
// AcceptMulti allows multiple clients to connect to the same server // acceptMulti allows multiple clients to connect to the same server
AcceptMulti bool acceptMulti bool
// Addr is the debugging server listen address. // addr is the debugging server listen address.
Addr string addr string
// InitFile is the path to initialization file. // initFile is the path to initialization file.
InitFile string initFile string
// BuildFlags is the flags passed during compiler invocation. // buildFlags is the flags passed during compiler invocation.
BuildFlags string buildFlags string
// WorkingDir is the working directory for running the program. // workingDir is the working directory for running the program.
WorkingDir string workingDir string
// CheckLocalConnUser is true if the debugger should check that local // checkLocalConnUser is true if the debugger should check that local
// connections come from the same user that started the headless server // connections come from the same user that started the headless server
CheckLocalConnUser bool checkLocalConnUser bool
// Backend selection // backend selection
Backend string backend string
// CheckGoVersion is true if the debugger should check the version of Go // checkGoVersion is true if the debugger should check the version of Go
// used to compile the executable and refuse to work on incompatible // used to compile the executable and refuse to work on incompatible
// versions. // versions.
CheckGoVersion bool checkGoVersion bool
// RootCommand is the root of the command tree. // rootCommand is the root of the command tree.
RootCommand *cobra.Command rootCommand *cobra.Command
traceAttachPid int traceAttachPid int
traceExecFile string traceExecFile string
@ -97,27 +97,27 @@ func New(docCall bool) *cobra.Command {
} }
// Main dlv root command. // Main dlv root command.
RootCommand = &cobra.Command{ rootCommand = &cobra.Command{
Use: "dlv", Use: "dlv",
Short: "Delve is a debugger for the Go programming language.", Short: "Delve is a debugger for the Go programming language.",
Long: dlvCommandLongDesc, Long: dlvCommandLongDesc,
} }
RootCommand.PersistentFlags().StringVarP(&Addr, "listen", "l", "127.0.0.1:0", "Debugging server listen address.") rootCommand.PersistentFlags().StringVarP(&addr, "listen", "l", "127.0.0.1:0", "Debugging server listen address.")
RootCommand.PersistentFlags().BoolVarP(&Log, "log", "", false, "Enable debugging server logging.") rootCommand.PersistentFlags().BoolVarP(&log, "log", "", false, "Enable debugging server logging.")
RootCommand.PersistentFlags().StringVarP(&LogOutput, "log-output", "", "", `Comma separated list of components that should produce debug output (see 'dlv help log')`) rootCommand.PersistentFlags().StringVarP(&logOutput, "log-output", "", "", `Comma separated list of components that should produce debug output (see 'dlv help log')`)
RootCommand.PersistentFlags().StringVarP(&LogDest, "log-dest", "", "", "Writes logs to the specified file or file descriptor (see 'dlv help log').") rootCommand.PersistentFlags().StringVarP(&logDest, "log-dest", "", "", "Writes logs to the specified file or file descriptor (see 'dlv help log').")
RootCommand.PersistentFlags().BoolVarP(&Headless, "headless", "", false, "Run debug server only, in headless mode.") rootCommand.PersistentFlags().BoolVarP(&headless, "headless", "", false, "Run debug server only, in headless mode.")
RootCommand.PersistentFlags().BoolVarP(&AcceptMulti, "accept-multiclient", "", false, "Allows a headless server to accept multiple client connections.") rootCommand.PersistentFlags().BoolVarP(&acceptMulti, "accept-multiclient", "", false, "Allows a headless server to accept multiple client connections.")
RootCommand.PersistentFlags().IntVar(&APIVersion, "api-version", 1, "Selects API version when headless.") rootCommand.PersistentFlags().IntVar(&apiVersion, "api-version", 1, "Selects API version when headless.")
RootCommand.PersistentFlags().StringVar(&InitFile, "init", "", "Init file, executed by the terminal client.") rootCommand.PersistentFlags().StringVar(&initFile, "init", "", "Init file, executed by the terminal client.")
RootCommand.PersistentFlags().StringVar(&BuildFlags, "build-flags", buildFlagsDefault, "Build flags, to be passed to the compiler.") rootCommand.PersistentFlags().StringVar(&buildFlags, "build-flags", buildFlagsDefault, "Build flags, to be passed to the compiler.")
RootCommand.PersistentFlags().StringVar(&WorkingDir, "wd", ".", "Working directory for running the program.") rootCommand.PersistentFlags().StringVar(&workingDir, "wd", ".", "Working directory for running the program.")
RootCommand.PersistentFlags().BoolVarP(&CheckGoVersion, "check-go-version", "", true, "Checks that the version of Go in use is compatible with Delve.") rootCommand.PersistentFlags().BoolVarP(&checkGoVersion, "check-go-version", "", true, "Checks that the version of Go in use is compatible with Delve.")
RootCommand.PersistentFlags().BoolVarP(&CheckLocalConnUser, "only-same-user", "", true, "Only connections from the same user that started this instance of Delve are allowed to connect.") rootCommand.PersistentFlags().BoolVarP(&checkLocalConnUser, "only-same-user", "", true, "Only connections from the same user that started this instance of Delve are allowed to connect.")
RootCommand.PersistentFlags().StringVar(&Backend, "backend", "default", `Backend selection (see 'dlv help backend').`) rootCommand.PersistentFlags().StringVar(&backend, "backend", "default", `Backend selection (see 'dlv help backend').`)
// 'attach' subcommand. // 'attach' subcommand.
attachCommand := &cobra.Command{ attachCommand := &cobra.Command{
@ -137,7 +137,7 @@ option to let the process continue or kill it.
}, },
Run: attachCmd, Run: attachCmd,
} }
RootCommand.AddCommand(attachCommand) rootCommand.AddCommand(attachCommand)
// 'connect' subcommand. // 'connect' subcommand.
connectCommand := &cobra.Command{ connectCommand := &cobra.Command{
@ -152,7 +152,7 @@ option to let the process continue or kill it.
}, },
Run: connectCmd, Run: connectCmd,
} }
RootCommand.AddCommand(connectCommand) rootCommand.AddCommand(connectCommand)
// 'dap' subcommand. // 'dap' subcommand.
dapCommand := &cobra.Command{ dapCommand := &cobra.Command{
@ -168,7 +168,7 @@ It does not yet support asynchronous request-response communication.
The server does not accept multiple client connections.`, The server does not accept multiple client connections.`,
Run: dapCmd, Run: dapCmd,
} }
RootCommand.AddCommand(dapCommand) rootCommand.AddCommand(dapCommand)
// 'debug' subcommand. // 'debug' subcommand.
debugCommand := &cobra.Command{ debugCommand := &cobra.Command{
@ -183,8 +183,8 @@ session.`,
Run: debugCmd, Run: debugCmd,
} }
debugCommand.Flags().String("output", "./__debug_bin", "Output path for the binary.") debugCommand.Flags().String("output", "./__debug_bin", "Output path for the binary.")
debugCommand.Flags().BoolVar(&ContinueOnStart, "continue", false, "Continue the debugged process on start.") debugCommand.Flags().BoolVar(&continueOnStart, "continue", false, "Continue the debugged process on start.")
RootCommand.AddCommand(debugCommand) rootCommand.AddCommand(debugCommand)
// 'exec' subcommand. // 'exec' subcommand.
execCommand := &cobra.Command{ execCommand := &cobra.Command{
@ -207,8 +207,8 @@ or later, -gcflags="-N -l" on earlier versions of Go.`,
os.Exit(execute(0, args, conf, "", executingExistingFile)) os.Exit(execute(0, args, conf, "", executingExistingFile))
}, },
} }
execCommand.Flags().BoolVar(&ContinueOnStart, "continue", false, "Continue the debugged process on start.") execCommand.Flags().BoolVar(&continueOnStart, "continue", false, "Continue the debugged process on start.")
RootCommand.AddCommand(execCommand) rootCommand.AddCommand(execCommand)
// Deprecated 'run' subcommand. // Deprecated 'run' subcommand.
runCommand := &cobra.Command{ runCommand := &cobra.Command{
@ -219,7 +219,7 @@ or later, -gcflags="-N -l" on earlier versions of Go.`,
os.Exit(0) os.Exit(0)
}, },
} }
RootCommand.AddCommand(runCommand) rootCommand.AddCommand(runCommand)
// 'test' subcommand. // 'test' subcommand.
testCommand := &cobra.Command{ testCommand := &cobra.Command{
@ -234,7 +234,7 @@ that package instead.`,
Run: testCmd, Run: testCmd,
} }
testCommand.Flags().String("output", "debug.test", "Output path for the binary.") testCommand.Flags().String("output", "debug.test", "Output path for the binary.")
RootCommand.AddCommand(testCommand) rootCommand.AddCommand(testCommand)
// 'trace' subcommand. // 'trace' subcommand.
traceCommand := &cobra.Command{ traceCommand := &cobra.Command{
@ -253,7 +253,7 @@ to know what functions your process is executing.`,
traceCommand.Flags().BoolVarP(&traceTestBinary, "test", "t", false, "Trace a test binary.") traceCommand.Flags().BoolVarP(&traceTestBinary, "test", "t", false, "Trace a test binary.")
traceCommand.Flags().IntVarP(&traceStackDepth, "stack", "s", 0, "Show stack trace with given depth.") traceCommand.Flags().IntVarP(&traceStackDepth, "stack", "s", 0, "Show stack trace with given depth.")
traceCommand.Flags().String("output", "debug", "Output path for the binary.") traceCommand.Flags().String("output", "debug", "Output path for the binary.")
RootCommand.AddCommand(traceCommand) rootCommand.AddCommand(traceCommand)
coreCommand := &cobra.Command{ coreCommand := &cobra.Command{
Use: "core <executable> <core>", Use: "core <executable> <core>",
@ -273,7 +273,7 @@ Currently supports linux/amd64 core files and windows/amd64 minidumps.`,
}, },
Run: coreCmd, Run: coreCmd,
} }
RootCommand.AddCommand(coreCommand) rootCommand.AddCommand(coreCommand)
// 'version' subcommand. // 'version' subcommand.
versionCommand := &cobra.Command{ versionCommand := &cobra.Command{
@ -283,7 +283,7 @@ Currently supports linux/amd64 core files and windows/amd64 minidumps.`,
fmt.Printf("Delve Debugger\n%s\n", version.DelveVersion) fmt.Printf("Delve Debugger\n%s\n", version.DelveVersion)
}, },
} }
RootCommand.AddCommand(versionCommand) rootCommand.AddCommand(versionCommand)
if path, _ := exec.LookPath("rr"); path != "" || docCall { if path, _ := exec.LookPath("rr"); path != "" || docCall {
replayCommand := &cobra.Command{ replayCommand := &cobra.Command{
@ -301,14 +301,14 @@ https://github.com/mozilla/rr
return nil return nil
}, },
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
Backend = "rr" backend = "rr"
os.Exit(execute(0, []string{}, conf, args[0], executingOther)) os.Exit(execute(0, []string{}, conf, args[0], executingOther))
}, },
} }
RootCommand.AddCommand(replayCommand) rootCommand.AddCommand(replayCommand)
} }
RootCommand.AddCommand(&cobra.Command{ rootCommand.AddCommand(&cobra.Command{
Use: "backend", Use: "backend",
Short: "Help about the --backend flag.", Short: "Help about the --backend flag.",
Long: `The --backend flag specifies which backend should be used, possible values Long: `The --backend flag specifies which backend should be used, possible values
@ -321,7 +321,7 @@ are:
`}) `})
RootCommand.AddCommand(&cobra.Command{ rootCommand.AddCommand(&cobra.Command{
Use: "log", Use: "log",
Short: "Help about logging flags.", Short: "Help about logging flags.",
Long: `Logging can be enabled by specifying the --log flag and using the Long: `Logging can be enabled by specifying the --log flag and using the
@ -350,35 +350,35 @@ and dap modes.
`, `,
}) })
RootCommand.DisableAutoGenTag = true rootCommand.DisableAutoGenTag = true
return RootCommand return rootCommand
} }
func dapCmd(cmd *cobra.Command, args []string) { func dapCmd(cmd *cobra.Command, args []string) {
status := func() int { status := func() int {
if err := logflags.Setup(Log, LogOutput, LogDest); err != nil { if err := logflags.Setup(log, logOutput, logDest); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err) fmt.Fprintf(os.Stderr, "%v\n", err)
return 1 return 1
} }
defer logflags.Close() defer logflags.Close()
if Headless { if headless {
fmt.Fprintf(os.Stderr, "Warning: headless mode not supported with dap\n") fmt.Fprintf(os.Stderr, "Warning: headless mode not supported with dap\n")
} }
if AcceptMulti { if acceptMulti {
fmt.Fprintf(os.Stderr, "Warning: accept multiclient mode not supported with dap\n") fmt.Fprintf(os.Stderr, "Warning: accept multiclient mode not supported with dap\n")
} }
if InitFile != "" { if initFile != "" {
fmt.Fprint(os.Stderr, "Warning: init file ignored with dap\n") fmt.Fprint(os.Stderr, "Warning: init file ignored with dap\n")
} }
if ContinueOnStart { if continueOnStart {
fmt.Fprintf(os.Stderr, "Warning: continue ignored with dap; specify via launch/attach request instead\n") fmt.Fprintf(os.Stderr, "Warning: continue ignored with dap; specify via launch/attach request instead\n")
} }
if BuildFlags != "" { if buildFlags != "" {
fmt.Fprintf(os.Stderr, "Warning: build flags ignored with dap; specify via launch/attach request instead\n") fmt.Fprintf(os.Stderr, "Warning: build flags ignored with dap; specify via launch/attach request instead\n")
} }
if WorkingDir != "" { if workingDir != "" {
fmt.Fprintf(os.Stderr, "Warning: working directory ignored with dap; launch requests must specify full program path\n") fmt.Fprintf(os.Stderr, "Warning: working directory ignored with dap; launch requests must specify full program path\n")
} }
dlvArgs, targetArgs := splitArgs(cmd, args) dlvArgs, targetArgs := splitArgs(cmd, args)
@ -389,7 +389,7 @@ func dapCmd(cmd *cobra.Command, args []string) {
fmt.Fprintf(os.Stderr, "Warning: program flags ignored with dap; specify via launch/attach request instead\n") fmt.Fprintf(os.Stderr, "Warning: program flags ignored with dap; specify via launch/attach request instead\n")
} }
listener, err := net.Listen("tcp", Addr) listener, err := net.Listen("tcp", addr)
if err != nil { if err != nil {
fmt.Printf("couldn't start listener: %s\n", err) fmt.Printf("couldn't start listener: %s\n", err)
return 1 return 1
@ -397,10 +397,10 @@ func dapCmd(cmd *cobra.Command, args []string) {
disconnectChan := make(chan struct{}) disconnectChan := make(chan struct{})
server := dap.NewServer(&service.Config{ server := dap.NewServer(&service.Config{
Listener: listener, Listener: listener,
Backend: Backend, Backend: backend,
Foreground: true, // always headless Foreground: true, // always headless
DebugInfoDirectories: conf.DebugInfoDirectories, DebugInfoDirectories: conf.DebugInfoDirectories,
CheckGoVersion: CheckGoVersion, CheckGoVersion: checkGoVersion,
DisconnectChan: disconnectChan, DisconnectChan: disconnectChan,
}) })
defer server.Stop() defer server.Stop()
@ -421,7 +421,7 @@ func debugCmd(cmd *cobra.Command, args []string) {
} }
dlvArgs, targetArgs := splitArgs(cmd, args) dlvArgs, targetArgs := splitArgs(cmd, args)
err = gobuild.GoBuild(debugname, dlvArgs, BuildFlags) err = gobuild.GoBuild(debugname, dlvArgs, buildFlags)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err) fmt.Fprintf(os.Stderr, "%v\n", err)
return 1 return 1
@ -435,17 +435,17 @@ func debugCmd(cmd *cobra.Command, args []string) {
func traceCmd(cmd *cobra.Command, args []string) { func traceCmd(cmd *cobra.Command, args []string) {
status := func() int { status := func() int {
err := logflags.Setup(Log, LogOutput, LogDest) err := logflags.Setup(log, logOutput, logDest)
defer logflags.Close() defer logflags.Close()
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err) fmt.Fprintf(os.Stderr, "%v\n", err)
return 1 return 1
} }
if Headless { if headless {
fmt.Fprintf(os.Stderr, "Warning: headless mode not supported with trace\n") fmt.Fprintf(os.Stderr, "Warning: headless mode not supported with trace\n")
} }
if AcceptMulti { if acceptMulti {
fmt.Fprintf(os.Stderr, "Warning: accept multiclient mode not supported with trace") fmt.Fprintf(os.Stderr, "Warning: accept multiclient mode not supported with trace")
} }
@ -477,12 +477,12 @@ func traceCmd(cmd *cobra.Command, args []string) {
return 1 return 1
} }
if traceTestBinary { if traceTestBinary {
if err := gobuild.GoTestBuild(debugname, dlvArgs, BuildFlags); err != nil { if err := gobuild.GoTestBuild(debugname, dlvArgs, buildFlags); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err) fmt.Fprintf(os.Stderr, "%v\n", err)
return 1 return 1
} }
} else { } else {
if err := gobuild.GoBuild(debugname, dlvArgs, BuildFlags); err != nil { if err := gobuild.GoBuild(debugname, dlvArgs, buildFlags); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err) fmt.Fprintf(os.Stderr, "%v\n", err)
return 1 return 1
} }
@ -503,9 +503,9 @@ func traceCmd(cmd *cobra.Command, args []string) {
ProcessArgs: processArgs, ProcessArgs: processArgs,
AttachPid: traceAttachPid, AttachPid: traceAttachPid,
APIVersion: 2, APIVersion: 2,
WorkingDir: WorkingDir, WorkingDir: workingDir,
Backend: Backend, Backend: backend,
CheckGoVersion: CheckGoVersion, CheckGoVersion: checkGoVersion,
}) })
if err := server.Run(); err != nil { if err := server.Run(); err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
@ -569,7 +569,7 @@ func testCmd(cmd *cobra.Command, args []string) {
} }
dlvArgs, targetArgs := splitArgs(cmd, args) dlvArgs, targetArgs := splitArgs(cmd, args)
err = gobuild.GoTestBuild(debugname, dlvArgs, BuildFlags) err = gobuild.GoTestBuild(debugname, dlvArgs, buildFlags)
if err != nil { if err != nil {
return 1 return 1
} }
@ -664,7 +664,7 @@ func connect(addr string, clientConn net.Conn, conf *config.Config, kind execute
} }
} }
term := terminal.New(client, conf) term := terminal.New(client, conf)
term.InitFile = InitFile term.InitFile = initFile
status, err := term.Run() status, err := term.Run()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -682,31 +682,31 @@ const (
) )
func execute(attachPid int, processArgs []string, conf *config.Config, coreFile string, kind executeKind) int { func execute(attachPid int, processArgs []string, conf *config.Config, coreFile string, kind executeKind) int {
if err := logflags.Setup(Log, LogOutput, LogDest); err != nil { if err := logflags.Setup(log, logOutput, logDest); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err) fmt.Fprintf(os.Stderr, "%v\n", err)
return 1 return 1
} }
defer logflags.Close() defer logflags.Close()
if Headless && (InitFile != "") { if headless && (initFile != "") {
fmt.Fprint(os.Stderr, "Warning: init file ignored with --headless\n") fmt.Fprint(os.Stderr, "Warning: init file ignored with --headless\n")
} }
if ContinueOnStart { if continueOnStart {
if !Headless { if !headless {
fmt.Fprint(os.Stderr, "Error: --continue only works with --headless; use an init file\n") fmt.Fprint(os.Stderr, "Error: --continue only works with --headless; use an init file\n")
return 1 return 1
} }
if !AcceptMulti { if !acceptMulti {
fmt.Fprint(os.Stderr, "Error: --continue requires --accept-multiclient\n") fmt.Fprint(os.Stderr, "Error: --continue requires --accept-multiclient\n")
return 1 return 1
} }
} }
if !Headless && AcceptMulti { if !headless && acceptMulti {
fmt.Fprint(os.Stderr, "Warning accept-multi: ignored\n") fmt.Fprint(os.Stderr, "Warning accept-multi: ignored\n")
// AcceptMulti won't work in normal (non-headless) mode because we always // acceptMulti won't work in normal (non-headless) mode because we always
// call server.Stop after the terminal client exits. // call server.Stop after the terminal client exits.
AcceptMulti = false acceptMulti = false
} }
var listener net.Listener var listener net.Listener
@ -714,8 +714,8 @@ func execute(attachPid int, processArgs []string, conf *config.Config, coreFile
var err error var err error
// Make a TCP listener // Make a TCP listener
if Headless { if headless {
listener, err = net.Listen("tcp", Addr) listener, err = net.Listen("tcp", addr)
} else { } else {
listener, clientConn = service.ListenerPipe() listener, clientConn = service.ListenerPipe()
} }
@ -730,25 +730,25 @@ func execute(attachPid int, processArgs []string, conf *config.Config, coreFile
disconnectChan := make(chan struct{}) disconnectChan := make(chan struct{})
// Create and start a debugger server // Create and start a debugger server
switch APIVersion { switch apiVersion {
case 1, 2: case 1, 2:
server = rpccommon.NewServer(&service.Config{ server = rpccommon.NewServer(&service.Config{
Listener: listener, Listener: listener,
ProcessArgs: processArgs, ProcessArgs: processArgs,
AttachPid: attachPid, AttachPid: attachPid,
AcceptMulti: AcceptMulti, AcceptMulti: acceptMulti,
APIVersion: APIVersion, APIVersion: apiVersion,
WorkingDir: WorkingDir, WorkingDir: workingDir,
Backend: Backend, Backend: backend,
CoreFile: coreFile, CoreFile: coreFile,
Foreground: Headless, Foreground: headless,
DebugInfoDirectories: conf.DebugInfoDirectories, DebugInfoDirectories: conf.DebugInfoDirectories,
CheckGoVersion: CheckGoVersion, CheckGoVersion: checkGoVersion,
CheckLocalConnUser: CheckLocalConnUser, CheckLocalConnUser: checkLocalConnUser,
DisconnectChan: disconnectChan, DisconnectChan: disconnectChan,
}) })
default: default:
fmt.Printf("Unknown API version: %d\n", APIVersion) fmt.Printf("Unknown API version: %d\n", apiVersion)
return 1 return 1
} }
@ -770,8 +770,8 @@ func execute(attachPid int, processArgs []string, conf *config.Config, coreFile
} }
var status int var status int
if Headless { if headless {
if ContinueOnStart { if continueOnStart {
client := rpc2.NewClient(listener.Addr().String()) client := rpc2.NewClient(listener.Addr().String())
client.Disconnect(true) // true = continue after disconnect client.Disconnect(true) // true = continue after disconnect
} }