11 "github.com/Sirupsen/logrus"
15 type GdbNative struct {
25 cbOnDisconnect func(error)
26 cbRead func(timestamp, stdout, stderr string)
27 cbInferiorRead func(timestamp, stdout, stderr string)
28 cbOnExit func(code int, err error)
33 // NewGdbNative creates a new instance of GdbNative
34 func NewGdbNative(log *logrus.Logger, args []string, env []string) *GdbNative {
43 // SetConfig set additional config fields
44 func (g *GdbNative) SetConfig(name string, value interface{}) error {
45 return fmt.Errorf("Unknown %s field", name)
48 // Init initializes gdb XDS
49 func (g *GdbNative) Init() (int, error) {
51 // Create the exec command
52 g.exeCmd = exec.Command(g.ccmd, g.aargs...)
58 func (g *GdbNative) Close() error {
59 g.cbOnDisconnect = nil
62 g.cbInferiorRead = nil
69 // Start sends a request to start remotely gdb within xds-server
70 func (g *GdbNative) Start(inferiorTTY bool) (int, error) {
73 // Start pty and consequently gdb process
74 if g.fdPty, err = pty.Start(g.exeCmd); err != nil {
75 return int(syscall.ESPIPE), err
80 // Monitor gdb process EOF
82 // Execute command and wait EOF
83 err := g.exeCmd.Wait()
90 sc := bufio.NewScanner(g.fdPty)
94 g.cbRead(time.Now().String(), sc.Text(), "")
105 // Cmd returns the command name
106 func (g *GdbNative) Cmd() string {
110 // Args returns the list of arguments
111 func (g *GdbNative) Args() []string {
115 // Env returns the list of environment variables
116 func (g *GdbNative) Env() []string {
120 // OnError doesn't make sens
121 func (g *GdbNative) OnError(f func(error)) {
125 // OnDisconnect is called when stdin is disconnected
126 func (g *GdbNative) OnDisconnect(f func(error)) {
130 // OnExit calls when exit event is received
131 func (g *GdbNative) OnExit(f func(code int, err error)) {
135 // Read calls when a message/string event is received on stdout or stderr
136 func (g *GdbNative) Read(f func(timestamp, stdout, stderr string)) {
140 // InferiorRead calls when a message/string event is received on stdout or stderr of the debugged program (IOW inferior)
141 func (g *GdbNative) InferiorRead(f func(timestamp, stdout, stderr string)) {
145 // Write writes message/string into gdb stdin
146 func (g *GdbNative) Write(args ...interface{}) error {
147 s := fmt.Sprint(args...)
148 _, err := g.fdPty.Write([]byte(s))
152 // SendSignal is used to send a signal to remote process/gdb
153 func (g *GdbNative) SendSignal(sig os.Signal) error {
154 return g.exeCmd.Process.Signal(sig)
157 //***** Private functions *****
159 func split(data []byte, atEOF bool) (advance int, token []byte, err error) {
160 if atEOF && len(data) == 0 {
163 return len(data), data, nil