13 "github.com/Sirupsen/logrus"
17 type GdbNative struct {
27 cbOnDisconnect func(error)
28 cbRead func(timestamp, stdout, stderr string)
29 cbInferiorRead func(timestamp, stdout, stderr string)
30 cbOnExit func(code int, err error)
35 // NewGdbNative creates a new instance of GdbNative
36 func NewGdbNative(log *logrus.Logger, args []string, env []string) *GdbNative {
45 // SetConfig set additional config fields
46 func (g *GdbNative) SetConfig(name string, value interface{}) error {
47 return fmt.Errorf("Unknown %s field", name)
50 // Init initializes gdb XDS
51 func (g *GdbNative) Init() (int, error) {
53 // Create the exec command
54 g.exeCmd = exec.Command(g.ccmd, g.aargs...)
60 func (g *GdbNative) Close() error {
61 g.cbOnDisconnect = nil
64 g.cbInferiorRead = nil
71 // Start sends a request to start remotely gdb within xds-server
72 func (g *GdbNative) Start(inferiorTTY bool) (int, error) {
75 // Start pty and consequently gdb process
76 if g.fdPty, err = pty.Start(g.exeCmd); err != nil {
77 return int(syscall.ESPIPE), err
82 // Monitor gdb process EOF
84 // Execute command and wait EOF
85 err := g.exeCmd.Wait()
92 sc := bufio.NewScanner(g.fdPty)
96 g.cbRead(time.Now().String(), sc.Text(), "")
107 // Cmd returns the command name
108 func (g *GdbNative) Cmd() string {
112 // Args returns the list of arguments
113 func (g *GdbNative) Args() []string {
117 // Env returns the list of environment variables
118 func (g *GdbNative) Env() []string {
122 // OnError doesn't make sens
123 func (g *GdbNative) OnError(f func(error)) {
127 // OnDisconnect is called when stdin is disconnected
128 func (g *GdbNative) OnDisconnect(f func(error)) {
132 // OnExit calls when exit event is received
133 func (g *GdbNative) OnExit(f func(code int, err error)) {
137 // Read calls when a message/string event is received on stdout or stderr
138 func (g *GdbNative) Read(f func(timestamp, stdout, stderr string)) {
142 // InferiorRead calls when a message/string event is received on stdout or stderr of the debugged program (IOW inferior)
143 func (g *GdbNative) InferiorRead(f func(timestamp, stdout, stderr string)) {
147 // Write writes message/string into gdb stdin
148 func (g *GdbNative) Write(args ...interface{}) error {
149 s := fmt.Sprint(args...)
150 _, err := g.fdPty.Write([]byte(s))
154 // SendSignal is used to send a signal to remote process/gdb
155 func (g *GdbNative) SendSignal(sig os.Signal) error {
157 return fmt.Errorf("exeCmd not initialized")
159 return g.exeCmd.Process.Signal(sig)
162 //***** Private functions *****
164 func split(data []byte, atEOF bool) (advance int, token []byte, err error) {
165 if atEOF && len(data) == 0 {
168 return len(data), data, nil