提交 064462e5 编写于 作者: D Dan Mace 提交者: Derek Parker

Support a headless server mode

Add a -headless flag which runs only the debugger server until
a SIGINT is received.
上级 ca2508af
......@@ -3,11 +3,13 @@ package main
import (
"flag"
"fmt"
sys "golang.org/x/sys/unix"
"io/ioutil"
"log"
"net"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strconv"
......@@ -36,10 +38,12 @@ func main() {
var printv, printhelp bool
var addr string
var logEnabled bool
var headless bool
flag.BoolVar(&printv, "version", false, "Print version number and exit.")
flag.StringVar(&addr, "addr", "localhost:0", "Debugging server listen address.")
flag.BoolVar(&logEnabled, "log", false, "Enable debugging server logging.")
flag.BoolVar(&headless, "headless", false, "Run in headless mode.")
flag.Parse()
if flag.NFlag() == 0 && len(flag.Args()) == 0 {
......@@ -120,15 +124,22 @@ func main() {
})
go server.Run()
// Create and start a terminal
client := rest.NewClient(listener.Addr().String())
term := terminal.New(client)
err, status := term.Run()
status := 0
if !headless {
// Create and start a terminal
client := rest.NewClient(listener.Addr().String())
term := terminal.New(client)
err, status = term.Run()
} else {
ch := make(chan os.Signal)
signal.Notify(ch, sys.SIGINT)
<-ch
err = server.Stop(true)
}
if err != nil {
fmt.Println(err)
}
// Clean up and exit
fmt.Println("[Hope I was of service hunting your bug!]")
os.Exit(status)
}
......
......@@ -98,8 +98,13 @@ func (s *RESTServer) Run() error {
return http.Serve(s.listener, container)
}
// Stop detaches from the debugger and waits for it to stop.
func (s *RESTServer) Stop(kill bool) error {
return s.debugger.Detach(kill)
err := s.debugger.Detach(kill)
if err != nil {
return err
}
return <-s.debuggerStopped
}
// writeError writes a simple error response.
......@@ -117,17 +122,12 @@ func (s *RESTServer) detach(request *restful.Request, response *restful.Response
return
}
err = s.debugger.Detach(kill)
err = s.Stop(kill)
if err != nil {
writeError(response, http.StatusInternalServerError, err.Error())
return
}
err = <-s.debuggerStopped
if err != nil {
writeError(response, http.StatusInternalServerError, err.Error())
return
}
response.WriteHeader(http.StatusOK)
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册