aboutsummaryrefslogtreecommitdiffstats
path: root/gdb-native.go
blob: e83f9ef79ca9dbfe3316fcc63f563f13b0717ec8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// +build !windows

package main

import (
	"bufio"
	"fmt"
	"os"
	"os/exec"
	"syscall"
	"time"

	"github.com/Sirupsen/logrus"
	"github.com/kr/pty"
)

type GdbNative struct {
	log   *logrus.Logger
	ccmd  string
	aargs []string
	eenv  []string

	exeCmd *exec.Cmd
	fdPty  *os.File

	// callbacks
	cbOnDisconnect func(error)
	cbRead         func(timestamp, stdout, stderr string)
	cbInferiorRead func(timestamp, stdout, stderr string)
	cbOnExit       func(code int, err error)

	running bool
}

// NewGdbNative creates a new instance of GdbNative
func NewGdbNative(log *logrus.Logger, args []string, env []string) *GdbNative {
	return &GdbNative{
		log:   log,
		ccmd:  "/usr/bin/gdb",
		aargs: args,
		eenv:  env,
	}
}

// SetConfig set additional config fields
func (g *GdbNative) SetConfig(name string, value interface{}) error {
	return fmt.Errorf("Unknown %s field", name)
}

// Init initializes gdb XDS
func (g *GdbNative) Init() (int, error) {

	// Create the exec command
	g.exeCmd = exec.Command(g.ccmd, g.aargs...)

	return 0, nil
}

// Close
func (g *GdbNative) Close() error {
	g.cbOnDisconnect = nil
	g.cbOnExit = nil
	g.cbRead = nil
	g.cbInferiorRead = nil

	g.running = false

	return nil
}

// Start sends a request to start remotely gdb within xds-server
func (g *GdbNative) Start(inferiorTTY bool) (int, error) {
	var err error

	// Start pty and consequently gdb process
	if g.fdPty, err = pty.Start(g.exeCmd); err != nil {
		return int(syscall.ESPIPE), err
	}

	g.running = true

	// Monitor gdb process EOF
	go func() {
		// Execute command and wait EOF
		err := g.exeCmd.Wait()
		g.cbOnDisconnect(err)
		g.running = false
	}()

	// Handle STDOUT
	go func() {
		sc := bufio.NewScanner(g.fdPty)
		sc.Split(split)
		for sc.Scan() {
			if g.cbRead != nil {
				g.cbRead(time.Now().String(), sc.Text(), "")
			}
			if !g.running {
				return
			}
		}
	}()

	return 0, nil
}

// Cmd returns the command name
func (g *GdbNative) Cmd() string {
	return g.ccmd
}

// Args returns the list of arguments
func (g *GdbNative) Args() []string {
	return g.aargs
}

// Env returns the list of environment variables
func (g *GdbNative) Env() []string {
	return g.eenv
}

// OnError doesn't make sens
func (g *GdbNative) OnError(f func(error)) {
	// nothing to do
}

// OnDisconnect is called when stdin is disconnected
func (g *GdbNative) OnDisconnect(f func(error)) {
	g.cbOnDisconnect = f
}

// OnExit calls when exit event is received
func (g *GdbNative) OnExit(f func(code int, err error)) {
	g.cbOnExit = f
}

// Read calls when a message/string event is received on stdout or stderr
func (g *GdbNative) Read(f func(timestamp, stdout, stderr string)) {
	g.cbRead = f
}

// InferiorRead calls when a message/string event is received on stdout or stderr of the debugged program (IOW inferior)
func (g *GdbNative) InferiorRead(f func(timestamp, stdout, stderr string)) {
	g.cbInferiorRead = f
}

// Write writes message/string into gdb stdin
func (g *GdbNative) Write(args ...interface{}) error {
	s := fmt.Sprint(args...)
	_, err := g.fdPty.Write([]byte(s))
	return err
}

// SendSignal is used to send a signal to remote process/gdb
func (g *GdbNative) SendSignal(sig os.Signal) error {
	if g.exeCmd == nil {
		return fmt.Errorf("exeCmd not initialized")
	}
	return g.exeCmd.Process.Signal(sig)
}

//***** Private functions *****

func split(data []byte, atEOF bool) (advance int, token []byte, err error) {
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}
	return len(data), data, nil
}