2017-03-19 22:41:36 +00:00
|
|
|
// Copyright 2017 bee authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"): you may
|
|
|
|
// not use this file except in compliance with the License. You may obtain
|
|
|
|
// a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing permissions and limitations
|
|
|
|
// under the License.
|
|
|
|
|
|
|
|
// Package dlv ...
|
|
|
|
package dlv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2017-03-21 14:31:29 +00:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-03-19 22:41:36 +00:00
|
|
|
|
|
|
|
"github.com/beego/bee/cmd/commands"
|
|
|
|
"github.com/beego/bee/cmd/commands/version"
|
|
|
|
beeLogger "github.com/beego/bee/logger"
|
2017-03-21 14:31:29 +00:00
|
|
|
"github.com/beego/bee/utils"
|
2017-03-19 22:41:36 +00:00
|
|
|
"github.com/derekparker/delve/pkg/terminal"
|
|
|
|
"github.com/derekparker/delve/service"
|
|
|
|
"github.com/derekparker/delve/service/rpc2"
|
2017-03-21 14:31:29 +00:00
|
|
|
"github.com/derekparker/delve/service/rpccommon"
|
2017-03-19 22:41:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var cmdDlv = &commands.Command{
|
|
|
|
CustomFlags: true,
|
|
|
|
UsageLine: "dlv [-package=\"\"] [-port=8181]",
|
|
|
|
Short: "Start a debugging session using Delve",
|
|
|
|
Long: `dlv command start a debugging session using debugging tool Delve.
|
|
|
|
|
|
|
|
To debug your application using Delve, use: {{"$ bee dlv" | bold}}
|
|
|
|
|
|
|
|
For more information on Delve: https://github.com/derekparker/delve
|
|
|
|
`,
|
|
|
|
PreRun: func(cmd *commands.Command, args []string) { version.ShowShortVersionBanner() },
|
|
|
|
Run: runDlv,
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
packageName string
|
2017-03-21 14:31:29 +00:00
|
|
|
port int
|
2017-03-19 22:41:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
fs := flag.NewFlagSet("dlv", flag.ContinueOnError)
|
2017-03-21 14:31:29 +00:00
|
|
|
fs.IntVar(&port, "port", 8181, "Port to listen to for clients")
|
2017-03-19 22:41:36 +00:00
|
|
|
fs.StringVar(&packageName, "package", "", "The package to debug (Must have a main package)")
|
|
|
|
cmdDlv.Flag = *fs
|
|
|
|
commands.AvailableCommands = append(commands.AvailableCommands, cmdDlv)
|
|
|
|
}
|
|
|
|
|
|
|
|
func runDlv(cmd *commands.Command, args []string) int {
|
|
|
|
if err := cmd.Flag.Parse(args); err != nil {
|
|
|
|
beeLogger.Log.Fatalf("Error parsing flags: %v", err.Error())
|
|
|
|
}
|
|
|
|
|
2017-03-21 14:31:29 +00:00
|
|
|
debugname := "debug"
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d", port)
|
|
|
|
return runDelve(addr, debugname)
|
|
|
|
}
|
2017-03-19 22:41:36 +00:00
|
|
|
|
2017-03-21 14:31:29 +00:00
|
|
|
// runDelve runs the Delve debugger server
|
|
|
|
func runDelve(addr, debugname string) int {
|
|
|
|
beeLogger.Log.Info("Starting Delve Debugger...")
|
2017-03-19 22:41:36 +00:00
|
|
|
|
2017-03-22 10:42:08 +00:00
|
|
|
err := gobuild(debugname, packageName)
|
2017-03-21 14:31:29 +00:00
|
|
|
if err != nil {
|
|
|
|
beeLogger.Log.Fatalf("%v", err)
|
2017-03-19 22:41:36 +00:00
|
|
|
}
|
|
|
|
|
2017-03-21 14:31:29 +00:00
|
|
|
fp, err := filepath.Abs("./" + debugname)
|
|
|
|
if err != nil {
|
|
|
|
beeLogger.Log.Fatalf("%v", err)
|
2017-03-19 22:41:36 +00:00
|
|
|
}
|
2017-03-21 14:31:29 +00:00
|
|
|
defer os.Remove(fp)
|
|
|
|
|
|
|
|
abs, err := filepath.Abs(debugname)
|
|
|
|
if err != nil {
|
|
|
|
beeLogger.Log.Fatalf("%v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create and start the debugger server
|
|
|
|
listener, err := net.Listen("tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
beeLogger.Log.Fatalf("Could not start listener: %s", err)
|
|
|
|
}
|
|
|
|
defer listener.Close()
|
|
|
|
|
|
|
|
server := rpccommon.NewServer(&service.Config{
|
|
|
|
Listener: listener,
|
|
|
|
AcceptMulti: true,
|
|
|
|
AttachPid: 0,
|
|
|
|
APIVersion: 2,
|
|
|
|
WorkingDir: "./",
|
|
|
|
ProcessArgs: []string{abs},
|
|
|
|
}, false)
|
|
|
|
if err := server.Run(); err != nil {
|
|
|
|
beeLogger.Log.Fatalf("Could not start debugger server: %v", err)
|
2017-03-19 22:41:36 +00:00
|
|
|
}
|
|
|
|
|
2017-03-21 14:31:29 +00:00
|
|
|
// Start the Delve client REPL
|
|
|
|
client := rpc2.NewClient(addr)
|
|
|
|
term := terminal.New(client, nil)
|
2017-03-19 22:41:36 +00:00
|
|
|
|
|
|
|
status, err := term.Run()
|
|
|
|
if err != nil {
|
2017-03-21 14:31:29 +00:00
|
|
|
beeLogger.Log.Fatalf("Could not start Delve REPL: %v", err)
|
2017-03-19 22:41:36 +00:00
|
|
|
}
|
|
|
|
defer term.Close()
|
|
|
|
|
2017-03-21 14:31:29 +00:00
|
|
|
// Stop and kill the debugger server once
|
|
|
|
// user quits the REPL
|
|
|
|
if err := server.Stop(true); err != nil {
|
|
|
|
beeLogger.Log.Fatalf("Could not stop Delve server: %v", err)
|
|
|
|
}
|
|
|
|
return status
|
2017-03-19 22:41:36 +00:00
|
|
|
}
|
2017-03-22 10:42:08 +00:00
|
|
|
|
|
|
|
// gobuild runs the "go build" command on the specified package
|
|
|
|
func gobuild(debugname, pkg string) error {
|
|
|
|
args := []string{"-gcflags", "-N -l", "-o", debugname}
|
|
|
|
args = append(args, utils.SplitQuotedFields("-ldflags='-linkmode internal'")...)
|
|
|
|
args = append(args, pkg)
|
|
|
|
return utils.GoCommand("build", args...)
|
|
|
|
}
|