github.com/undoio/delve@v1.9.0/service/dap/types.go (about)

     1  package dap
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"fmt"
     7  )
     8  
     9  // Launch debug sessions support the following modes:
    10  //
    11  //	-- [DEFAULT] "debug" - builds and launches debugger for specified program (similar to 'dlv debug')
    12  //
    13  //	   Required args: program
    14  //	   Optional args with default: output, cwd, noDebug
    15  //	   Optional args: buildFlags, args
    16  //
    17  //	-- "test" - builds and launches debugger for specified test (similar to 'dlv test')
    18  //
    19  //	   same args as above
    20  //
    21  //	-- "exec" - launches debugger for precompiled binary (similar to 'dlv exec')
    22  //
    23  //	   Required args: program
    24  //	   Optional args with default: cwd, noDebug
    25  //	   Optional args: args
    26  //
    27  //	-- "replay" - replays a trace generated by mozilla rr. Mozilla rr must be installed.
    28  //
    29  //	   Required args: traceDirPath
    30  //	   Optional args: args
    31  //
    32  //	-- "core" - examines a core dump (only supports linux and windows core dumps).
    33  //
    34  //	   Required args: program, coreFilePath
    35  //	   Optional args: args
    36  //
    37  // TODO(hyangah): change this to 'validateLaunchMode' that checks
    38  // all the required/optional fields mentioned above.
    39  func isValidLaunchMode(mode string) bool {
    40  	switch mode {
    41  	case "exec", "debug", "test", "replay", "core":
    42  		return true
    43  	}
    44  	return false
    45  }
    46  
    47  // Default values for Launch/Attach configs.
    48  // Used to initialize configuration variables before decoding
    49  // arguments in launch/attach requests.
    50  var (
    51  	defaultLaunchAttachCommonConfig = LaunchAttachCommonConfig{
    52  		Backend:         "default",
    53  		StackTraceDepth: 50,
    54  	}
    55  	defaultLaunchConfig = LaunchConfig{
    56  		Mode:                     "debug",
    57  		LaunchAttachCommonConfig: defaultLaunchAttachCommonConfig,
    58  	}
    59  	defaultAttachConfig = AttachConfig{
    60  		Mode:                     "local",
    61  		LaunchAttachCommonConfig: defaultLaunchAttachCommonConfig,
    62  	}
    63  )
    64  
    65  // LaunchConfig is the collection of launch request attributes recognized by DAP implementation.
    66  type LaunchConfig struct {
    67  	// Acceptable values are:
    68  	//   "debug": compiles your program with optimizations disabled, starts and attaches to it.
    69  	//   "test": compiles your unit test program with optimizations disabled, starts and attaches to it.
    70  	//   "exec": executes a precompiled binary and begins a debug session.
    71  	//   "replay": replays an rr trace.
    72  	//   "core": examines a core dump.
    73  	//
    74  	// Default is "debug".
    75  	Mode string `json:"mode,omitempty"`
    76  
    77  	// Path to the program folder (or any go file within that folder)
    78  	// when in `debug` or `test` mode, and to the pre-built binary file
    79  	// to debug in `exec` mode.
    80  	// If it is not an absolute path, it will be interpreted as a path
    81  	// relative to Delve's working directory.
    82  	// Required when mode is `debug`, `test`, `exec`, and `core`.
    83  	Program string `json:"program,omitempty"`
    84  
    85  	// Command line arguments passed to the debugged program.
    86  	// Relative paths used in Args will be interpreted as paths relative
    87  	// to `cwd`.
    88  	Args []string `json:"args,omitempty"`
    89  
    90  	// Working directory of the program being debugged.
    91  	// If a relative path is provided, it will be interpreted as
    92  	// a relative path to Delve's working directory. This is
    93  	// similar to `dlv --wd` flag.
    94  	//
    95  	// If not specified or empty, Delve's working directory is
    96  	// used by default. But for `test` mode, Delve tries to find
    97  	// the test's package source directory and run tests from there.
    98  	// This matches the behavior of `dlv test` and `go test`.
    99  	Cwd string `json:"cwd,omitempty"`
   100  
   101  	// Build flags, to be passed to the Go compiler.
   102  	// Relative paths used in BuildFlags will be interpreted as paths
   103  	// relative to Delve's current working directory.
   104  	//
   105  	// It is like `dlv --build-flags`. For example,
   106  	//    "buildFlags": "-tags=integration -mod=vendor -cover -v"
   107  	BuildFlags string `json:"buildFlags,omitempty"`
   108  
   109  	// Output path for the binary of the debugee.
   110  	// Relative path is interpreted as the path relative to
   111  	// the Delve's current working directory.
   112  	// This is deleted after the debug session ends.
   113  	Output string `json:"output,omitempty"`
   114  
   115  	// NoDebug is used to run the program without debugging.
   116  	NoDebug bool `json:"noDebug,omitempty"`
   117  
   118  	// TraceDirPath is the trace directory path for replay mode.
   119  	// Relative path is interpreted as a path relative to Delve's
   120  	// current working directory.
   121  	// This is required for "replay" mode but unused in other modes.
   122  	TraceDirPath string `json:"traceDirPath,omitempty"`
   123  
   124  	// CoreFilePath is the core file path for core mode.
   125  	//
   126  	// This is required for "core" mode but unused in other modes.
   127  	CoreFilePath string `json:"coreFilePath,omitempty"`
   128  
   129  	// DlvCwd is the new working directory for Delve server.
   130  	// If specified, the server will change its working
   131  	// directory to the specified directory using os.Chdir.
   132  	// Any other launch attributes with relative paths interpreted
   133  	// using Delve's working directory will use this new directory.
   134  	// When Delve needs to build the program (in debug/test modes),
   135  	// it will run the go command from this directory as well.
   136  	//
   137  	// If a relative path is provided as DlvCwd, it will be
   138  	// interpreted as a path relative to Delve's current working
   139  	// directory.
   140  	DlvCwd string `json:"dlvCwd,omitempty"`
   141  
   142  	// Env specifies optional environment variables for Delve server
   143  	// in addition to the environment variables Delve initially
   144  	// started with.
   145  	// Variables with 'nil' values can be used to unset the named
   146  	// environment variables.
   147  	// Values are interpreted verbatim. Variable substitution or
   148  	// reference to other environment variables is not supported.
   149  	Env map[string]*string `json:"env,omitempty"`
   150  
   151  	LaunchAttachCommonConfig
   152  }
   153  
   154  // LaunchAttachCommonConfig is the attributes common in both launch/attach requests.
   155  type LaunchAttachCommonConfig struct {
   156  	// Automatically stop program after launch or attach.
   157  	StopOnEntry bool `json:"stopOnEntry,omitempty"`
   158  
   159  	// Backend used for debugging. See `dlv backend` for allowed values.
   160  	// Default is "default".
   161  	Backend string `json:"backend,omitempty"`
   162  
   163  	// Maximum depth of stack trace to return.
   164  	// Default is 50.
   165  	StackTraceDepth int `json:"stackTraceDepth,omitempty"`
   166  
   167  	// Boolean value to indicate whether global package variables
   168  	// should be shown in the variables pane or not.
   169  	ShowGlobalVariables bool `json:"showGlobalVariables,omitempty"`
   170  
   171  	// Boolean value to indicate whether registers should be shown
   172  	// in the variables pane or not.
   173  	ShowRegisters bool `json:"showRegisters,omitempty"`
   174  
   175  	// Boolean value to indicate whether system goroutines
   176  	// should be hidden from the call stack view.
   177  	HideSystemGoroutines bool `json:"hideSystemGoroutines,omitempty"`
   178  
   179  	// String value to indicate which system goroutines should be
   180  	// shown in the call stack view. See filtering documentation:
   181  	// https://github.com/go-delve/delve/blob/master/Documentation/cli/README.md#goroutines
   182  	GoroutineFilters string `json:"goroutineFilters,omitempty"`
   183  
   184  	// An array of mappings from a local path (client) to the remote path (debugger).
   185  	// This setting is useful when working in a file system with symbolic links,
   186  	// running remote debugging, or debugging an executable compiled externally.
   187  	// The debug adapter will replace the local path with the remote path in all of the calls.
   188  	SubstitutePath []SubstitutePath `json:"substitutePath,omitempty"`
   189  }
   190  
   191  // SubstitutePath defines a mapping from a local path to the remote path.
   192  // Both 'from' and 'to' must be specified and non-empty.
   193  type SubstitutePath struct {
   194  	// The local path to be replaced when passing paths to the debugger.
   195  	From string `json:"from,omitempty"`
   196  	// The remote path to be replaced when passing paths back to the client.
   197  	To string `json:"to,omitempty"`
   198  }
   199  
   200  func (m *SubstitutePath) UnmarshalJSON(data []byte) error {
   201  	// use custom unmarshal to check if both from/to are set.
   202  	type tmpType SubstitutePath
   203  	var tmp tmpType
   204  
   205  	if err := json.Unmarshal(data, &tmp); err != nil {
   206  		if _, ok := err.(*json.UnmarshalTypeError); ok {
   207  			return fmt.Errorf(`cannot use %s as 'substitutePath' of type {"from":string, "to":string}`, data)
   208  		}
   209  		return err
   210  	}
   211  	if tmp.From == "" || tmp.To == "" {
   212  		return errors.New("'substitutePath' requires both 'from' and 'to' entries")
   213  	}
   214  	*m = SubstitutePath(tmp)
   215  	return nil
   216  }
   217  
   218  // AttachConfig is the collection of attach request attributes recognized by DAP implementation.
   219  type AttachConfig struct {
   220  	// Acceptable values are:
   221  	//   "local": attaches to the local process with the given ProcessID.
   222  	//   "remote": expects the debugger to already be running to "attach" to an in-progress debug session.
   223  	//
   224  	// Default is "local".
   225  	Mode string `json:"mode"`
   226  
   227  	// The numeric ID of the process to be debugged. Required and must not be 0.
   228  	ProcessID int `json:"processId,omitempty"`
   229  
   230  	LaunchAttachCommonConfig
   231  }
   232  
   233  // unmarshalLaunchAttachArgs wraps unmarshalling of launch/attach request's
   234  // arguments attribute. Upon unmarshal failure, it returns an error massaged
   235  // to be suitable for end-users.
   236  func unmarshalLaunchAttachArgs(input json.RawMessage, config interface{}) error {
   237  	if err := json.Unmarshal(input, config); err != nil {
   238  		if uerr, ok := err.(*json.UnmarshalTypeError); ok {
   239  			// Format json.UnmarshalTypeError error string in our own way. E.g.,
   240  			//   "json: cannot unmarshal number into Go struct field LaunchArgs.substitutePath of type dap.SubstitutePath"
   241  			//   => "cannot unmarshal number into 'substitutePath' of type {from:string, to:string}"
   242  			//   "json: cannot unmarshal number into Go struct field LaunchArgs.program of type string" (go1.16)
   243  			//   => "cannot unmarshal number into 'program' of type string"
   244  			typ := uerr.Type.String()
   245  			if uerr.Field == "substitutePath" {
   246  				typ = `{"from":string, "to":string}`
   247  			}
   248  			return fmt.Errorf("cannot unmarshal %v into %q of type %v", uerr.Value, uerr.Field, typ)
   249  		}
   250  		return err
   251  	}
   252  	return nil
   253  }
   254  
   255  func prettyPrint(config interface{}) string {
   256  	pretty, err := json.MarshalIndent(config, "", "\t")
   257  	if err != nil {
   258  		return fmt.Sprintf("%#v", config)
   259  	}
   260  	return string(pretty)
   261  }