github.com/cnboonhan/delve@v0.0.0-20230908061759-363f2388c2fb/pkg/proc/gdbserial/gdbserver_conn.go (about)

     1  package gdbserial
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  	"debug/macho"
     7  	"encoding/json"
     8  	"encoding/xml"
     9  	"errors"
    10  	"fmt"
    11  	"io"
    12  	"net"
    13  	"os"
    14  	"strconv"
    15  	"strings"
    16  	"time"
    17  
    18  	"github.com/go-delve/delve/pkg/logflags"
    19  	"github.com/go-delve/delve/pkg/proc"
    20  )
    21  
    22  type gdbConn struct {
    23  	conn net.Conn
    24  	rdr  *bufio.Reader
    25  
    26  	inbuf  []byte
    27  	outbuf bytes.Buffer
    28  
    29  	running bool
    30  
    31  	direction proc.Direction // direction of execution
    32  
    33  	packetSize int               // maximum packet size supported by stub
    34  	regsInfo   []gdbRegisterInfo // list of registers
    35  
    36  	workaroundReg *gdbRegisterInfo // used to work-around a register setting bug in debugserver, see use in gdbserver.go
    37  
    38  	pid int // cache process id
    39  
    40  	ack                   bool // when ack is true acknowledgment packets are enabled
    41  	multiprocess          bool // multiprocess extensions are active
    42  	maxTransmitAttempts   int  // maximum number of transmit or receive attempts when bad checksums are read
    43  	threadSuffixSupported bool // thread suffix supported by stub
    44  	isDebugserver         bool // true if the stub is debugserver
    45  	xcmdok                bool // x command can be used to transfer memory
    46  	goarch                string
    47  	goos                  string
    48  
    49  	useXcmd bool // forces writeMemory to use the 'X' command
    50  
    51  	log logflags.Logger
    52  }
    53  
    54  var ErrTooManyAttempts = errors.New("too many transmit attempts")
    55  
    56  // GdbProtocolError is an error response (Exx) of Gdb Remote Serial Protocol
    57  // or an "unsupported command" response (empty packet).
    58  type GdbProtocolError struct {
    59  	context string
    60  	cmd     string
    61  	code    string
    62  }
    63  
    64  func (err *GdbProtocolError) Error() string {
    65  	cmd := err.cmd
    66  	if len(cmd) > 20 {
    67  		cmd = cmd[:20] + "..."
    68  	}
    69  	if err.code == "" {
    70  		return fmt.Sprintf("unsupported packet %s during %s", cmd, err.context)
    71  	}
    72  	return fmt.Sprintf("protocol error %s during %s for packet %s", err.code, err.context, cmd)
    73  }
    74  
    75  func isProtocolErrorUnsupported(err error) bool {
    76  	gdberr, ok := err.(*GdbProtocolError)
    77  	if !ok {
    78  		return false
    79  	}
    80  	return gdberr.code == ""
    81  }
    82  
    83  // GdbMalformedThreadIDError is returned when the stub responds with a
    84  // thread ID that does not conform with the Gdb Remote Serial Protocol
    85  // specification.
    86  type GdbMalformedThreadIDError struct {
    87  	tid string
    88  }
    89  
    90  func (err *GdbMalformedThreadIDError) Error() string {
    91  	return fmt.Sprintf("malformed thread ID %q", err.tid)
    92  }
    93  
    94  const (
    95  	qSupportedSimple       = "$qSupported:swbreak+;hwbreak+;no-resumed+;xmlRegisters=i386"
    96  	qSupportedMultiprocess = "$qSupported:multiprocess+;swbreak+;hwbreak+;no-resumed+;xmlRegisters=i386"
    97  )
    98  
    99  func (conn *gdbConn) handshake(regnames *gdbRegnames) error {
   100  	conn.ack = true
   101  	conn.packetSize = 256
   102  	conn.rdr = bufio.NewReader(conn.conn)
   103  
   104  	// This first ack packet is needed to start up the connection
   105  	conn.sendack('+')
   106  
   107  	conn.disableAck()
   108  
   109  	// Try to enable thread suffixes for the command 'g' and 'p'
   110  	if _, err := conn.exec([]byte("$QThreadSuffixSupported"), "init"); err != nil {
   111  		if isProtocolErrorUnsupported(err) {
   112  			conn.threadSuffixSupported = false
   113  		} else {
   114  			return err
   115  		}
   116  	} else {
   117  		conn.threadSuffixSupported = true
   118  	}
   119  
   120  	if !conn.threadSuffixSupported {
   121  		features, err := conn.qSupported(true)
   122  		if err != nil {
   123  			return err
   124  		}
   125  		conn.multiprocess = features["multiprocess"]
   126  
   127  		// for some reason gdbserver won't let us read target.xml unless first we
   128  		// select a thread.
   129  		if conn.multiprocess {
   130  			conn.exec([]byte("$Hgp0.0"), "init")
   131  		} else {
   132  			conn.exec([]byte("$Hgp0"), "init")
   133  		}
   134  	} else {
   135  		// execute qSupported with the multiprocess feature disabled (the
   136  		// interaction of thread suffixes and multiprocess is not documented), we
   137  		// only need this call to configure conn.packetSize.
   138  		if _, err := conn.qSupported(false); err != nil {
   139  			return err
   140  		}
   141  	}
   142  
   143  	// Attempt to figure out the name of the processor register.
   144  	// We either need qXfer:features:read (gdbserver/rr) or qRegisterInfo (lldb)
   145  	regFound := map[string]bool{
   146  		regnames.PC: false,
   147  		regnames.SP: false,
   148  		regnames.BP: false,
   149  		regnames.CX: false,
   150  	}
   151  	if err := conn.readRegisterInfo(regFound); err != nil {
   152  		if isProtocolErrorUnsupported(err) {
   153  			if err := conn.readTargetXml(regFound); err != nil {
   154  				return err
   155  			}
   156  		} else {
   157  			return err
   158  		}
   159  	}
   160  	for n := range regFound {
   161  		if n == "" {
   162  			continue
   163  		}
   164  		if !regFound[n] {
   165  			return fmt.Errorf("could not find %s register", n)
   166  		}
   167  	}
   168  
   169  	// We either need:
   170  	//  * QListThreadsInStopReply + qThreadStopInfo (i.e. lldb-server/debugserver),
   171  	//  * or a stub that runs the inferior in single threaded mode (i.e. rr).
   172  	// Otherwise we'll have problems handling breakpoints in multithreaded programs.
   173  	if _, err := conn.exec([]byte("$QListThreadsInStopReply"), "init"); err != nil {
   174  		gdberr, ok := err.(*GdbProtocolError)
   175  		if !ok {
   176  			return err
   177  		}
   178  		if gdberr.code != "" {
   179  			return err
   180  		}
   181  	}
   182  
   183  	if resp, err := conn.exec([]byte("$x0,0"), "init"); err == nil && string(resp) == "OK" {
   184  		conn.xcmdok = true
   185  	}
   186  
   187  	return nil
   188  }
   189  
   190  // qSupported interprets qSupported responses.
   191  func (conn *gdbConn) qSupported(multiprocess bool) (features map[string]bool, err error) {
   192  	q := qSupportedSimple
   193  	if multiprocess {
   194  		q = qSupportedMultiprocess
   195  	}
   196  	respBuf, err := conn.exec([]byte(q), "init/qSupported")
   197  	if err != nil {
   198  		return nil, err
   199  	}
   200  	resp := strings.Split(string(respBuf), ";")
   201  	features = make(map[string]bool)
   202  	for _, stubfeature := range resp {
   203  		if len(stubfeature) == 0 {
   204  			continue
   205  		} else if equal := strings.Index(stubfeature, "="); equal >= 0 {
   206  			if stubfeature[:equal] == "PacketSize" {
   207  				if n, err := strconv.ParseInt(stubfeature[equal+1:], 16, 64); err == nil {
   208  					conn.packetSize = int(n)
   209  				}
   210  			}
   211  		} else if stubfeature[len(stubfeature)-1] == '+' {
   212  			features[stubfeature[:len(stubfeature)-1]] = true
   213  		}
   214  	}
   215  	return features, nil
   216  }
   217  
   218  // disableAck disables protocol acks.
   219  func (conn *gdbConn) disableAck() error {
   220  	_, err := conn.exec([]byte("$QStartNoAckMode"), "init/disableAck")
   221  	if err == nil {
   222  		conn.ack = false
   223  	}
   224  	return err
   225  }
   226  
   227  // gdbTarget is a struct type used to parse target.xml
   228  type gdbTarget struct {
   229  	Includes  []gdbTargetInclude `xml:"xi include"`
   230  	Registers []gdbRegisterInfo  `xml:"reg"`
   231  }
   232  
   233  type gdbTargetInclude struct {
   234  	Href string `xml:"href,attr"`
   235  }
   236  
   237  type gdbRegisterInfo struct {
   238  	Name    string `xml:"name,attr"`
   239  	Bitsize int    `xml:"bitsize,attr"`
   240  	Offset  int
   241  	Regnum  int    `xml:"regnum,attr"`
   242  	Group   string `xml:"group,attr"`
   243  
   244  	ignoreOnWrite bool
   245  }
   246  
   247  func setRegFound(regFound map[string]bool, name string) {
   248  	for n := range regFound {
   249  		if name == n {
   250  			regFound[n] = true
   251  		}
   252  	}
   253  }
   254  
   255  // readTargetXml reads target.xml file from stub using qXfer:features:read,
   256  // then parses it requesting any additional files.
   257  // The schema of target.xml is described by:
   258  //
   259  //	https://github.com/bminor/binutils-gdb/blob/61baf725eca99af2569262d10aca03dcde2698f6/gdb/features/gdb-target.dtd
   260  func (conn *gdbConn) readTargetXml(regFound map[string]bool) (err error) {
   261  	conn.regsInfo, err = conn.readAnnex("target.xml")
   262  	if err != nil {
   263  		return err
   264  	}
   265  	var offset int
   266  	regnum := 0
   267  	for i := range conn.regsInfo {
   268  		if conn.regsInfo[i].Regnum == 0 {
   269  			conn.regsInfo[i].Regnum = regnum
   270  		} else {
   271  			regnum = conn.regsInfo[i].Regnum
   272  		}
   273  		conn.regsInfo[i].Offset = offset
   274  		offset += conn.regsInfo[i].Bitsize / 8
   275  
   276  		setRegFound(regFound, conn.regsInfo[i].Name)
   277  		regnum++
   278  	}
   279  
   280  	return nil
   281  }
   282  
   283  // readRegisterInfo uses qRegisterInfo to read register information (used
   284  // when qXfer:feature:read is not supported).
   285  func (conn *gdbConn) readRegisterInfo(regFound map[string]bool) (err error) {
   286  	regnum := 0
   287  	for {
   288  		conn.outbuf.Reset()
   289  		fmt.Fprintf(&conn.outbuf, "$qRegisterInfo%x", regnum)
   290  		respbytes, err := conn.exec(conn.outbuf.Bytes(), "register info")
   291  		if err != nil {
   292  			if regnum == 0 {
   293  				return err
   294  			}
   295  			break
   296  		}
   297  
   298  		var regname string
   299  		var offset int
   300  		var bitsize int
   301  		var contained bool
   302  		var ignoreOnWrite bool
   303  
   304  		resp := string(respbytes)
   305  		for {
   306  			semicolon := strings.Index(resp, ";")
   307  			keyval := resp
   308  			if semicolon >= 0 {
   309  				keyval = resp[:semicolon]
   310  			}
   311  
   312  			colon := strings.Index(keyval, ":")
   313  			if colon >= 0 {
   314  				name := keyval[:colon]
   315  				value := keyval[colon+1:]
   316  
   317  				switch name {
   318  				case "name":
   319  					regname = value
   320  				case "offset":
   321  					offset, _ = strconv.Atoi(value)
   322  				case "bitsize":
   323  					bitsize, _ = strconv.Atoi(value)
   324  				case "container-regs":
   325  					contained = true
   326  				case "set":
   327  					if value == "Exception State Registers" || value == "AMX Registers" {
   328  						// debugserver doesn't like it if we try to write these
   329  						ignoreOnWrite = true
   330  					}
   331  				}
   332  			}
   333  
   334  			if semicolon < 0 {
   335  				break
   336  			}
   337  			resp = resp[semicolon+1:]
   338  		}
   339  
   340  		if contained {
   341  			if regname == "xmm0" {
   342  				conn.workaroundReg = &gdbRegisterInfo{Regnum: regnum, Name: regname, Bitsize: bitsize, Offset: offset, ignoreOnWrite: ignoreOnWrite}
   343  			}
   344  			regnum++
   345  			continue
   346  		}
   347  
   348  		setRegFound(regFound, regname)
   349  
   350  		conn.regsInfo = append(conn.regsInfo, gdbRegisterInfo{Regnum: regnum, Name: regname, Bitsize: bitsize, Offset: offset, ignoreOnWrite: ignoreOnWrite})
   351  
   352  		regnum++
   353  	}
   354  
   355  	return nil
   356  }
   357  
   358  func (conn *gdbConn) readAnnex(annex string) ([]gdbRegisterInfo, error) {
   359  	tgtbuf, err := conn.qXfer("features", annex, false)
   360  	if err != nil {
   361  		return nil, err
   362  	}
   363  	var tgt gdbTarget
   364  	if err := xml.Unmarshal(tgtbuf, &tgt); err != nil {
   365  		return nil, err
   366  	}
   367  
   368  	for _, incl := range tgt.Includes {
   369  		regs, err := conn.readAnnex(incl.Href)
   370  		if err != nil {
   371  			return nil, err
   372  		}
   373  		tgt.Registers = append(tgt.Registers, regs...)
   374  	}
   375  	return tgt.Registers, nil
   376  }
   377  
   378  func (conn *gdbConn) readExecFile() (string, error) {
   379  	outbuf, err := conn.qXfer("exec-file", "", true)
   380  	if err != nil {
   381  		return "", err
   382  	}
   383  	return string(outbuf), nil
   384  }
   385  
   386  func (conn *gdbConn) readAuxv() ([]byte, error) {
   387  	return conn.qXfer("auxv", "", true)
   388  }
   389  
   390  // qXfer executes a 'qXfer' read with the specified kind (i.e. feature,
   391  // exec-file, etc...) and annex.
   392  func (conn *gdbConn) qXfer(kind, annex string, binary bool) ([]byte, error) {
   393  	out := []byte{}
   394  	for {
   395  		cmd := []byte(fmt.Sprintf("$qXfer:%s:read:%s:%x,fff", kind, annex, len(out)))
   396  		err := conn.send(cmd)
   397  		if err != nil {
   398  			return nil, err
   399  		}
   400  		buf, err := conn.recv(cmd, "target features transfer", binary)
   401  		if err != nil {
   402  			return nil, err
   403  		}
   404  
   405  		out = append(out, buf[1:]...)
   406  		if buf[0] == 'l' {
   407  			break
   408  		}
   409  	}
   410  	return out, nil
   411  }
   412  
   413  // qXferWrite executes a 'qXfer' write with the specified kind and annex.
   414  func (conn *gdbConn) qXferWrite(kind, annex string) error {
   415  	conn.outbuf.Reset()
   416  	fmt.Fprintf(&conn.outbuf, "$qXfer:%s:write:%s:0:", kind, annex)
   417  	//TODO(aarzilli): if we ever actually need to write something with qXfer,
   418  	//this will need to be implemented properly. At the moment it is only used
   419  	//for a fake write to the siginfo kind, to end a diversion in 'rr'.
   420  	_, err := conn.exec(conn.outbuf.Bytes(), "qXfer")
   421  	return err
   422  }
   423  
   424  type breakpointType uint8
   425  
   426  const (
   427  	swBreakpoint     breakpointType = 0
   428  	hwBreakpoint     breakpointType = 1
   429  	writeWatchpoint  breakpointType = 2
   430  	readWatchpoint   breakpointType = 3
   431  	accessWatchpoint breakpointType = 4
   432  )
   433  
   434  // setBreakpoint executes a 'Z' (insert breakpoint) command of type '0' and kind '1' or '4'
   435  func (conn *gdbConn) setBreakpoint(addr uint64, typ breakpointType, kind int) error {
   436  	conn.outbuf.Reset()
   437  	fmt.Fprintf(&conn.outbuf, "$Z%d,%x,%d", typ, addr, kind)
   438  	_, err := conn.exec(conn.outbuf.Bytes(), "set breakpoint")
   439  	return err
   440  }
   441  
   442  // clearBreakpoint executes a 'z' (remove breakpoint) command of type '0' and kind '1' or '4'
   443  func (conn *gdbConn) clearBreakpoint(addr uint64, typ breakpointType, kind int) error {
   444  	conn.outbuf.Reset()
   445  	fmt.Fprintf(&conn.outbuf, "$z%d,%x,%d", typ, addr, kind)
   446  	_, err := conn.exec(conn.outbuf.Bytes(), "clear breakpoint")
   447  	return err
   448  }
   449  
   450  // kill executes a 'k' (kill) command.
   451  func (conn *gdbConn) kill() error {
   452  	resp, err := conn.exec([]byte{'$', 'k'}, "kill")
   453  	if err == io.EOF {
   454  		// The stub is allowed to shut the connection on us immediately after a
   455  		// kill. This is not an error.
   456  		conn.conn.Close()
   457  		conn.conn = nil
   458  		return proc.ErrProcessExited{Pid: conn.pid}
   459  	}
   460  	if err != nil {
   461  		return err
   462  	}
   463  	_, _, err = conn.parseStopPacket(resp, "", nil)
   464  	return err
   465  }
   466  
   467  // detach executes a 'D' (detach) command.
   468  func (conn *gdbConn) detach() error {
   469  	if conn.conn == nil {
   470  		// Already detached
   471  		return nil
   472  	}
   473  	_, err := conn.exec([]byte{'$', 'D'}, "detach")
   474  	conn.conn.Close()
   475  	conn.conn = nil
   476  	return err
   477  }
   478  
   479  // readRegisters executes a 'g' (read registers) command.
   480  func (conn *gdbConn) readRegisters(threadID string, data []byte) error {
   481  	if !conn.threadSuffixSupported {
   482  		if err := conn.selectThread('g', threadID, "registers read"); err != nil {
   483  			return err
   484  		}
   485  	}
   486  	conn.outbuf.Reset()
   487  	conn.outbuf.WriteString("$g")
   488  	conn.appendThreadSelector(threadID)
   489  	resp, err := conn.exec(conn.outbuf.Bytes(), "registers read")
   490  	if err != nil {
   491  		return err
   492  	}
   493  
   494  	for i := 0; i < len(resp); i += 2 {
   495  		n, _ := strconv.ParseUint(string(resp[i:i+2]), 16, 8)
   496  		data[i/2] = uint8(n)
   497  	}
   498  
   499  	return nil
   500  }
   501  
   502  // writeRegisters executes a 'G' (write registers) command.
   503  func (conn *gdbConn) writeRegisters(threadID string, data []byte) error {
   504  	if !conn.threadSuffixSupported {
   505  		if err := conn.selectThread('g', threadID, "registers write"); err != nil {
   506  			return err
   507  		}
   508  	}
   509  	conn.outbuf.Reset()
   510  	conn.outbuf.WriteString("$G")
   511  
   512  	for _, b := range data {
   513  		fmt.Fprintf(&conn.outbuf, "%02x", b)
   514  	}
   515  	conn.appendThreadSelector(threadID)
   516  	_, err := conn.exec(conn.outbuf.Bytes(), "registers write")
   517  	return err
   518  }
   519  
   520  // readRegister executes 'p' (read register) command.
   521  func (conn *gdbConn) readRegister(threadID string, regnum int, data []byte) error {
   522  	if !conn.threadSuffixSupported {
   523  		if err := conn.selectThread('g', threadID, "registers write"); err != nil {
   524  			return err
   525  		}
   526  	}
   527  	conn.outbuf.Reset()
   528  	fmt.Fprintf(&conn.outbuf, "$p%x", regnum)
   529  	conn.appendThreadSelector(threadID)
   530  	resp, err := conn.exec(conn.outbuf.Bytes(), "register read")
   531  	if err != nil {
   532  		return err
   533  	}
   534  
   535  	for i := 0; i < len(resp); i += 2 {
   536  		n, _ := strconv.ParseUint(string(resp[i:i+2]), 16, 8)
   537  		data[i/2] = uint8(n)
   538  	}
   539  
   540  	return nil
   541  }
   542  
   543  // writeRegister executes 'P' (write register) command.
   544  func (conn *gdbConn) writeRegister(threadID string, regnum int, data []byte) error {
   545  	if !conn.threadSuffixSupported {
   546  		if err := conn.selectThread('g', threadID, "registers write"); err != nil {
   547  			return err
   548  		}
   549  	}
   550  	conn.outbuf.Reset()
   551  	fmt.Fprintf(&conn.outbuf, "$P%x=", regnum)
   552  	for _, b := range data {
   553  		fmt.Fprintf(&conn.outbuf, "%02x", b)
   554  	}
   555  	conn.appendThreadSelector(threadID)
   556  	_, err := conn.exec(conn.outbuf.Bytes(), "register write")
   557  	return err
   558  }
   559  
   560  // resume execution of the target process.
   561  // If the current direction is proc.Backward this is done with the 'bc' command.
   562  // If the current direction is proc.Forward this is done with the vCont command.
   563  // The threads argument will be used to determine which signal to use to
   564  // resume each thread. If a thread has sig == 0 the 'c' action will be used,
   565  // otherwise the 'C' action will be used and the value of sig will be passed
   566  // to it.
   567  func (conn *gdbConn) resume(cctx *proc.ContinueOnceContext, threads map[int]*gdbThread, tu *threadUpdater) (stopPacket, error) {
   568  	if conn.direction == proc.Forward {
   569  		conn.outbuf.Reset()
   570  		fmt.Fprintf(&conn.outbuf, "$vCont")
   571  		for _, th := range threads {
   572  			if th.sig != 0 {
   573  				fmt.Fprintf(&conn.outbuf, ";C%02x:%s", th.sig, th.strID)
   574  			}
   575  		}
   576  		fmt.Fprintf(&conn.outbuf, ";c")
   577  	} else {
   578  		if err := conn.selectThread('c', "p-1.-1", "resume"); err != nil {
   579  			return stopPacket{}, err
   580  		}
   581  		conn.outbuf.Reset()
   582  		fmt.Fprint(&conn.outbuf, "$bc")
   583  	}
   584  	cctx.StopMu.Lock()
   585  	if err := conn.send(conn.outbuf.Bytes()); err != nil {
   586  		cctx.StopMu.Unlock()
   587  		return stopPacket{}, err
   588  	}
   589  	conn.running = true
   590  	cctx.StopMu.Unlock()
   591  	defer func() {
   592  		cctx.StopMu.Lock()
   593  		conn.running = false
   594  		cctx.StopMu.Unlock()
   595  	}()
   596  	if cctx.ResumeChan != nil {
   597  		close(cctx.ResumeChan)
   598  		cctx.ResumeChan = nil
   599  	}
   600  	return conn.waitForvContStop("resume", "-1", tu)
   601  }
   602  
   603  // step executes a 'vCont' command on the specified thread with 's' action.
   604  func (conn *gdbConn) step(th *gdbThread, tu *threadUpdater, ignoreFaultSignal bool) error {
   605  	threadID := th.strID
   606  	if conn.direction != proc.Forward {
   607  		if err := conn.selectThread('c', threadID, "step"); err != nil {
   608  			return err
   609  		}
   610  		conn.outbuf.Reset()
   611  		fmt.Fprint(&conn.outbuf, "$bs")
   612  		if err := conn.send(conn.outbuf.Bytes()); err != nil {
   613  			return err
   614  		}
   615  		_, err := conn.waitForvContStop("singlestep", threadID, tu)
   616  		return err
   617  	}
   618  
   619  	var _SIGBUS uint8
   620  	switch conn.goos {
   621  	case "linux":
   622  		_SIGBUS = 0x7
   623  	case "darwin":
   624  		_SIGBUS = 0xa
   625  	default:
   626  		panic(fmt.Errorf("unknown GOOS %s", conn.goos))
   627  	}
   628  
   629  	var sig uint8 = 0
   630  	for {
   631  		conn.outbuf.Reset()
   632  		if sig == 0 {
   633  			fmt.Fprintf(&conn.outbuf, "$vCont;s:%s", threadID)
   634  		} else {
   635  			fmt.Fprintf(&conn.outbuf, "$vCont;S%02x:%s", sig, threadID)
   636  		}
   637  		if err := conn.send(conn.outbuf.Bytes()); err != nil {
   638  			return err
   639  		}
   640  		if tu != nil {
   641  			tu.Reset()
   642  		}
   643  		sp, err := conn.waitForvContStop("singlestep", threadID, tu)
   644  		sig = sp.sig
   645  		if err != nil {
   646  			return err
   647  		}
   648  		switch sig {
   649  		case faultSignal:
   650  			if ignoreFaultSignal { // we attempting to read the TLS, a fault here should be ignored
   651  				return nil
   652  			}
   653  		case _SIGILL, _SIGBUS, _SIGFPE:
   654  			// propagate these signals to inferior immediately
   655  		case interruptSignal, breakpointSignal, stopSignal:
   656  			return nil
   657  		case childSignal: // stop on debugserver but SIGCHLD on lldb-server/linux
   658  			if conn.isDebugserver {
   659  				return nil
   660  			}
   661  		case debugServerTargetExcBadAccess, debugServerTargetExcBadInstruction, debugServerTargetExcArithmetic, debugServerTargetExcEmulation, debugServerTargetExcSoftware, debugServerTargetExcBreakpoint:
   662  			if ignoreFaultSignal {
   663  				return nil
   664  			}
   665  			return machTargetExcToError(sig)
   666  		default:
   667  			// delay propagation of any other signal to until after the stepping is done
   668  			th.sig = sig
   669  			sig = 0
   670  		}
   671  	}
   672  }
   673  
   674  var errThreadBlocked = errors.New("thread blocked")
   675  
   676  func (conn *gdbConn) waitForvContStop(context, threadID string, tu *threadUpdater) (stopPacket, error) {
   677  	count := 0
   678  	failed := false
   679  	for {
   680  		conn.conn.SetReadDeadline(time.Now().Add(heartbeatInterval))
   681  		resp, err := conn.recv(nil, context, false)
   682  		conn.conn.SetReadDeadline(time.Time{})
   683  		if neterr, isneterr := err.(net.Error); isneterr && neterr.Timeout() {
   684  			// Debugserver sometimes forgets to inform us that inferior stopped,
   685  			// sending this status request after a timeout helps us get unstuck.
   686  			// Debugserver will not respond to this request unless inferior is
   687  			// already stopped.
   688  			if conn.isDebugserver {
   689  				conn.send([]byte("$?"))
   690  			}
   691  			if count > 1 && context == "singlestep" {
   692  				failed = true
   693  				conn.sendCtrlC()
   694  			}
   695  			count++
   696  		} else if failed {
   697  			return stopPacket{}, errThreadBlocked
   698  		} else if err != nil {
   699  			return stopPacket{}, err
   700  		} else {
   701  			repeat, sp, err := conn.parseStopPacket(resp, threadID, tu)
   702  			if !repeat {
   703  				return sp, err
   704  			}
   705  		}
   706  	}
   707  }
   708  
   709  type stopPacket struct {
   710  	threadID  string
   711  	sig       uint8
   712  	reason    string
   713  	watchAddr uint64
   714  }
   715  
   716  // Mach exception codes used to decode metype/medata keys in stop packets (necessary to support watchpoints with debugserver).
   717  // See:
   718  //
   719  //	https://opensource.apple.com/source/xnu/xnu-4570.1.46/osfmk/mach/exception_types.h.auto.html
   720  //	https://opensource.apple.com/source/xnu/xnu-4570.1.46/osfmk/mach/i386/exception.h.auto.html
   721  //	https://opensource.apple.com/source/xnu/xnu-4570.1.46/osfmk/mach/arm/exception.h.auto.html
   722  const (
   723  	_EXC_BREAKPOINT   = 6     // mach exception type for hardware breakpoints
   724  	_EXC_I386_SGL     = 1     // mach exception code for single step on x86, for some reason this is also used for watchpoints
   725  	_EXC_ARM_DA_DEBUG = 0x102 // mach exception code for debug fault on arm/arm64
   726  )
   727  
   728  // executes 'vCont' (continue/step) command
   729  func (conn *gdbConn) parseStopPacket(resp []byte, threadID string, tu *threadUpdater) (repeat bool, sp stopPacket, err error) {
   730  	switch resp[0] {
   731  	case 'T':
   732  		if len(resp) < 3 {
   733  			return false, stopPacket{}, fmt.Errorf("malformed response for vCont %s", string(resp))
   734  		}
   735  
   736  		sig, err := strconv.ParseUint(string(resp[1:3]), 16, 8)
   737  		if err != nil {
   738  			return false, stopPacket{}, fmt.Errorf("malformed stop packet: %s", string(resp))
   739  		}
   740  		sp.sig = uint8(sig)
   741  
   742  		if logflags.GdbWire() && gdbWireFullStopPacket {
   743  			conn.log.Debugf("full stop packet: %s", string(resp))
   744  		}
   745  
   746  		var metype int
   747  		var medata = make([]uint64, 0, 10)
   748  
   749  		buf := resp[3:]
   750  		for buf != nil {
   751  			colon := bytes.Index(buf, []byte{':'})
   752  			if colon < 0 {
   753  				break
   754  			}
   755  			key := buf[:colon]
   756  			buf = buf[colon+1:]
   757  
   758  			semicolon := bytes.Index(buf, []byte{';'})
   759  			var value []byte
   760  			if semicolon < 0 {
   761  				value = buf
   762  				buf = nil
   763  			} else {
   764  				value = buf[:semicolon]
   765  				buf = buf[semicolon+1:]
   766  			}
   767  
   768  			switch string(key) {
   769  			case "thread":
   770  				sp.threadID = string(value)
   771  			case "threads":
   772  				if tu != nil {
   773  					tu.Add(strings.Split(string(value), ","))
   774  					tu.Finish()
   775  				}
   776  			case "reason":
   777  				sp.reason = string(value)
   778  			case "watch", "awatch", "rwatch":
   779  				sp.watchAddr, err = strconv.ParseUint(string(value), 16, 64)
   780  				if err != nil {
   781  					return false, stopPacket{}, fmt.Errorf("malformed stop packet: %s (wrong watch address)", string(resp))
   782  				}
   783  			case "metype":
   784  				// mach exception type (debugserver extension)
   785  				metype, _ = strconv.Atoi(string(value))
   786  			case "medata":
   787  				// mach exception data (debugserver extension)
   788  				d, _ := strconv.ParseUint(string(value), 16, 64)
   789  				medata = append(medata, d)
   790  			}
   791  		}
   792  
   793  		// Debugserver does not report watchpoint stops in the standard way preferring
   794  		// instead the semi-undocumented metype/medata keys.
   795  		// These values also have different meanings depending on the CPU architecture.
   796  		switch conn.goarch {
   797  		case "amd64":
   798  			if metype == _EXC_BREAKPOINT && len(medata) >= 2 && medata[0] == _EXC_I386_SGL {
   799  				sp.watchAddr = medata[1] // this should be zero if this is really a single step stop and non-zero for watchpoints
   800  			}
   801  		case "arm64":
   802  			if metype == _EXC_BREAKPOINT && len(medata) >= 2 && medata[0] == _EXC_ARM_DA_DEBUG {
   803  				sp.watchAddr = medata[1]
   804  			}
   805  		}
   806  
   807  		return false, sp, nil
   808  
   809  	case 'W', 'X':
   810  		// process exited, next two character are exit code
   811  
   812  		semicolon := bytes.Index(resp, []byte{';'})
   813  
   814  		if semicolon < 0 {
   815  			semicolon = len(resp)
   816  		}
   817  		status, _ := strconv.ParseUint(string(resp[1:semicolon]), 16, 8)
   818  		return false, stopPacket{}, proc.ErrProcessExited{Pid: conn.pid, Status: int(status)}
   819  
   820  	case 'N':
   821  		// we were singlestepping the thread and the thread exited
   822  		sp.threadID = threadID
   823  		return false, sp, nil
   824  
   825  	case 'O':
   826  		data := make([]byte, 0, len(resp[1:])/2)
   827  		for i := 1; i < len(resp); i += 2 {
   828  			n, _ := strconv.ParseUint(string(resp[i:i+2]), 16, 8)
   829  			data = append(data, uint8(n))
   830  		}
   831  		os.Stdout.Write(data)
   832  		return true, sp, nil
   833  
   834  	default:
   835  		return false, sp, fmt.Errorf("unexpected response for vCont %c", resp[0])
   836  	}
   837  }
   838  
   839  const ctrlC = 0x03 // the ASCII character for ^C
   840  
   841  // executes a ctrl-C on the line
   842  func (conn *gdbConn) sendCtrlC() error {
   843  	conn.log.Debug("<- interrupt")
   844  	_, err := conn.conn.Write([]byte{ctrlC})
   845  	return err
   846  }
   847  
   848  // queryProcessInfo executes a qProcessInfoPID (if pid != 0) or a qProcessInfo (if pid == 0)
   849  func (conn *gdbConn) queryProcessInfo(pid int) (map[string]string, error) {
   850  	conn.outbuf.Reset()
   851  	if pid != 0 {
   852  		fmt.Fprintf(&conn.outbuf, "$qProcessInfoPID:%d", pid)
   853  	} else {
   854  		fmt.Fprint(&conn.outbuf, "$qProcessInfo")
   855  	}
   856  	resp, err := conn.exec(conn.outbuf.Bytes(), "process info for pid")
   857  	if err != nil {
   858  		return nil, err
   859  	}
   860  
   861  	pi := make(map[string]string)
   862  
   863  	for len(resp) > 0 {
   864  		semicolon := bytes.Index(resp, []byte{';'})
   865  		keyval := resp
   866  		if semicolon >= 0 {
   867  			keyval = resp[:semicolon]
   868  			resp = resp[semicolon+1:]
   869  		}
   870  
   871  		colon := bytes.Index(keyval, []byte{':'})
   872  		if colon < 0 {
   873  			continue
   874  		}
   875  
   876  		key := string(keyval[:colon])
   877  		value := string(keyval[colon+1:])
   878  
   879  		switch key {
   880  		case "name":
   881  			name := make([]byte, len(value)/2)
   882  			for i := 0; i < len(value); i += 2 {
   883  				n, _ := strconv.ParseUint(string(value[i:i+2]), 16, 8)
   884  				name[i/2] = byte(n)
   885  			}
   886  			pi[key] = string(name)
   887  
   888  		default:
   889  			pi[key] = value
   890  		}
   891  	}
   892  	return pi, nil
   893  }
   894  
   895  // executes qfThreadInfo/qsThreadInfo commands
   896  func (conn *gdbConn) queryThreads(first bool) (threads []string, err error) {
   897  	// https://sourceware.org/gdb/onlinedocs/gdb/General-Query-Packets.html
   898  	conn.outbuf.Reset()
   899  	if first {
   900  		conn.outbuf.WriteString("$qfThreadInfo")
   901  	} else {
   902  		conn.outbuf.WriteString("$qsThreadInfo")
   903  	}
   904  
   905  	resp, err := conn.exec(conn.outbuf.Bytes(), "thread info")
   906  	if err != nil {
   907  		return nil, err
   908  	}
   909  
   910  	switch resp[0] {
   911  	case 'l':
   912  		return nil, nil
   913  	case 'm':
   914  		// parse list...
   915  	default:
   916  		return nil, errors.New("malformed qfThreadInfo response")
   917  	}
   918  
   919  	var pid int
   920  	resp = resp[1:]
   921  	for {
   922  		tidbuf := resp
   923  		comma := bytes.Index(tidbuf, []byte{','})
   924  		if comma >= 0 {
   925  			tidbuf = tidbuf[:comma]
   926  		}
   927  		if conn.multiprocess && pid == 0 {
   928  			dot := bytes.Index(tidbuf, []byte{'.'})
   929  			if dot >= 0 {
   930  				pid, _ = strconv.Atoi(string(tidbuf[1:dot]))
   931  			}
   932  		}
   933  		threads = append(threads, string(tidbuf))
   934  		if comma < 0 {
   935  			break
   936  		}
   937  		resp = resp[comma+1:]
   938  	}
   939  
   940  	if conn.multiprocess && pid > 0 {
   941  		conn.pid = pid
   942  	}
   943  	return threads, nil
   944  }
   945  
   946  func (conn *gdbConn) selectThread(kind byte, threadID string, context string) error {
   947  	if conn.threadSuffixSupported {
   948  		panic("selectThread when thread suffix is supported")
   949  	}
   950  	conn.outbuf.Reset()
   951  	fmt.Fprintf(&conn.outbuf, "$H%c%s", kind, threadID)
   952  	_, err := conn.exec(conn.outbuf.Bytes(), context)
   953  	return err
   954  }
   955  
   956  func (conn *gdbConn) appendThreadSelector(threadID string) {
   957  	if !conn.threadSuffixSupported {
   958  		return
   959  	}
   960  	fmt.Fprintf(&conn.outbuf, ";thread:%s;", threadID)
   961  }
   962  
   963  func (conn *gdbConn) readMemory(data []byte, addr uint64) error {
   964  	if conn.xcmdok && len(data) > conn.packetSize {
   965  		return conn.readMemoryBinary(data, addr)
   966  	}
   967  	return conn.readMemoryHex(data, addr)
   968  }
   969  
   970  // executes 'm' (read memory) command
   971  func (conn *gdbConn) readMemoryHex(data []byte, addr uint64) error {
   972  	size := len(data)
   973  	data = data[:0]
   974  
   975  	for size > 0 {
   976  		conn.outbuf.Reset()
   977  
   978  		// gdbserver will crash if we ask too many bytes... not return an error, actually crash
   979  		sz := size
   980  		if dataSize := (conn.packetSize - 4) / 2; sz > dataSize {
   981  			sz = dataSize
   982  		}
   983  		size = size - sz
   984  
   985  		fmt.Fprintf(&conn.outbuf, "$m%x,%x", addr+uint64(len(data)), sz)
   986  		resp, err := conn.exec(conn.outbuf.Bytes(), "memory read")
   987  		if err != nil {
   988  			return err
   989  		}
   990  
   991  		for i := 0; i < len(resp); i += 2 {
   992  			n, _ := strconv.ParseUint(string(resp[i:i+2]), 16, 8)
   993  			data = append(data, uint8(n))
   994  		}
   995  	}
   996  	return nil
   997  }
   998  
   999  // executes 'x' (binary read memory) command
  1000  func (conn *gdbConn) readMemoryBinary(data []byte, addr uint64) error {
  1001  	size := len(data)
  1002  	data = data[:0]
  1003  
  1004  	for len(data) < size {
  1005  		conn.outbuf.Reset()
  1006  
  1007  		sz := size - len(data)
  1008  
  1009  		fmt.Fprintf(&conn.outbuf, "$x%x,%x", addr+uint64(len(data)), sz)
  1010  		if err := conn.send(conn.outbuf.Bytes()); err != nil {
  1011  			return err
  1012  		}
  1013  		resp, err := conn.recv(conn.outbuf.Bytes(), "binary memory read", true)
  1014  		if err != nil {
  1015  			return err
  1016  		}
  1017  		data = append(data, resp...)
  1018  	}
  1019  	return nil
  1020  }
  1021  
  1022  func writeAsciiBytes(w io.Writer, data []byte) {
  1023  	for _, b := range data {
  1024  		fmt.Fprintf(w, "%02x", b)
  1025  	}
  1026  }
  1027  
  1028  // writeMemory writes memory using either 'M' or 'X'
  1029  func (conn *gdbConn) writeMemory(addr uint64, data []byte) (written int, err error) {
  1030  	if conn.useXcmd {
  1031  		return conn.writeMemoryBinary(addr, data)
  1032  	}
  1033  	return conn.writeMemoryHex(addr, data)
  1034  }
  1035  
  1036  // executes 'M' (write memory) command
  1037  func (conn *gdbConn) writeMemoryHex(addr uint64, data []byte) (written int, err error) {
  1038  	if len(data) == 0 {
  1039  		// LLDB can't parse requests for 0-length writes and hangs if we emit them
  1040  		return 0, nil
  1041  	}
  1042  	conn.outbuf.Reset()
  1043  	//TODO(aarzilli): do not send packets larger than conn.PacketSize
  1044  	fmt.Fprintf(&conn.outbuf, "$M%x,%x:", addr, len(data))
  1045  
  1046  	writeAsciiBytes(&conn.outbuf, data)
  1047  
  1048  	_, err = conn.exec(conn.outbuf.Bytes(), "memory write")
  1049  	if err != nil {
  1050  		return 0, err
  1051  	}
  1052  	return len(data), nil
  1053  }
  1054  
  1055  func (conn *gdbConn) writeMemoryBinary(addr uint64, data []byte) (written int, err error) {
  1056  	conn.outbuf.Reset()
  1057  	fmt.Fprintf(&conn.outbuf, "$X%x,%x:", addr, len(data))
  1058  
  1059  	for _, b := range data {
  1060  		switch b {
  1061  		case '#', '$', '}':
  1062  			conn.outbuf.WriteByte('}')
  1063  			conn.outbuf.WriteByte(b ^ escapeXor)
  1064  		default:
  1065  			conn.outbuf.WriteByte(b)
  1066  		}
  1067  	}
  1068  
  1069  	_, err = conn.exec(conn.outbuf.Bytes(), "memory write")
  1070  	if err != nil {
  1071  		return 0, err
  1072  	}
  1073  	return len(data), nil
  1074  }
  1075  
  1076  func (conn *gdbConn) allocMemory(sz uint64) (uint64, error) {
  1077  	conn.outbuf.Reset()
  1078  	fmt.Fprintf(&conn.outbuf, "$_M%x,rwx", sz)
  1079  	resp, err := conn.exec(conn.outbuf.Bytes(), "memory allocation")
  1080  	if err != nil {
  1081  		return 0, err
  1082  	}
  1083  	return strconv.ParseUint(string(resp), 16, 64)
  1084  }
  1085  
  1086  // threadStopInfo executes a 'qThreadStopInfo' and returns the reason the
  1087  // thread stopped.
  1088  func (conn *gdbConn) threadStopInfo(threadID string) (sp stopPacket, err error) {
  1089  	conn.outbuf.Reset()
  1090  	fmt.Fprintf(&conn.outbuf, "$qThreadStopInfo%s", threadID)
  1091  	resp, err := conn.exec(conn.outbuf.Bytes(), "thread stop info")
  1092  	if err != nil {
  1093  		return stopPacket{}, err
  1094  	}
  1095  	_, sp, err = conn.parseStopPacket(resp, "", nil)
  1096  	if err != nil {
  1097  		return stopPacket{}, err
  1098  	}
  1099  	if sp.threadID != threadID {
  1100  		// When we send a ^C (manual stop request) and the process is close to
  1101  		// stopping anyway, sometimes, debugserver will send back two stop
  1102  		// packets. We need to ignore this spurious stop packet. Because the first
  1103  		// thing we do after the stop is updateThreadList, which calls this
  1104  		// function, this is relatively painless. We simply need to check that the
  1105  		// stop packet we receive is for the thread we requested, if it isn't we
  1106  		// can assume it is the spurious extra stop packet and simply ignore it.
  1107  		// An example of a problematic interaction is in the commit message for
  1108  		// this change.
  1109  		// See https://github.com/go-delve/delve/issues/3013.
  1110  
  1111  		conn.conn.SetReadDeadline(time.Now().Add(10 * time.Millisecond))
  1112  		resp, err = conn.recv(conn.outbuf.Bytes(), "thread stop info", false)
  1113  		conn.conn.SetReadDeadline(time.Time{})
  1114  		if err != nil {
  1115  			if neterr, isneterr := err.(net.Error); isneterr && neterr.Timeout() {
  1116  				return stopPacket{}, fmt.Errorf("qThreadStopInfo mismatch, requested %s got %s", sp.threadID, threadID)
  1117  			}
  1118  			return stopPacket{}, err
  1119  		}
  1120  		_, sp, err = conn.parseStopPacket(resp, "", nil)
  1121  		if err != nil {
  1122  			return stopPacket{}, err
  1123  		}
  1124  		if sp.threadID != threadID {
  1125  			return stopPacket{}, fmt.Errorf("qThreadStopInfo mismatch, requested %s got %s", sp.threadID, threadID)
  1126  		}
  1127  	}
  1128  
  1129  	return sp, nil
  1130  }
  1131  
  1132  // restart executes a 'vRun' command.
  1133  func (conn *gdbConn) restart(pos string) error {
  1134  	conn.outbuf.Reset()
  1135  	fmt.Fprint(&conn.outbuf, "$vRun;")
  1136  	if pos != "" {
  1137  		fmt.Fprint(&conn.outbuf, ";")
  1138  		writeAsciiBytes(&conn.outbuf, []byte(pos))
  1139  	}
  1140  	_, err := conn.exec(conn.outbuf.Bytes(), "restart")
  1141  	return err
  1142  }
  1143  
  1144  // qRRCmd executes a qRRCmd command
  1145  func (conn *gdbConn) qRRCmd(args ...string) (string, error) {
  1146  	if len(args) == 0 {
  1147  		panic("must specify at least one argument for qRRCmd")
  1148  	}
  1149  	conn.outbuf.Reset()
  1150  	fmt.Fprint(&conn.outbuf, "$qRRCmd")
  1151  	for _, arg := range args {
  1152  		fmt.Fprint(&conn.outbuf, ":")
  1153  		writeAsciiBytes(&conn.outbuf, []byte(arg))
  1154  	}
  1155  	resp, err := conn.exec(conn.outbuf.Bytes(), "qRRCmd")
  1156  	if err != nil {
  1157  		return "", err
  1158  	}
  1159  	data := make([]byte, 0, len(resp)/2)
  1160  	for i := 0; i < len(resp); i += 2 {
  1161  		n, _ := strconv.ParseUint(string(resp[i:i+2]), 16, 8)
  1162  		data = append(data, uint8(n))
  1163  	}
  1164  	return string(data), nil
  1165  }
  1166  
  1167  type imageList struct {
  1168  	Images []imageDescription `json:"images"`
  1169  }
  1170  
  1171  type imageDescription struct {
  1172  	LoadAddress uint64     `json:"load_address"`
  1173  	Pathname    string     `json:"pathname"`
  1174  	MachHeader  machHeader `json:"mach_header"`
  1175  }
  1176  
  1177  type machHeader struct {
  1178  	FileType macho.Type `json:"filetype"`
  1179  }
  1180  
  1181  // getLoadedDynamicLibraries executes jGetLoadedDynamicLibrariesInfos which
  1182  // returns the list of loaded dynamic libraries
  1183  func (conn *gdbConn) getLoadedDynamicLibraries() ([]imageDescription, error) {
  1184  	cmd := []byte("$jGetLoadedDynamicLibrariesInfos:{\"fetch_all_solibs\":true}")
  1185  	if err := conn.send(cmd); err != nil {
  1186  		return nil, err
  1187  	}
  1188  	resp, err := conn.recv(cmd, "get dynamic libraries", true)
  1189  	if err != nil {
  1190  		return nil, err
  1191  	}
  1192  	var images imageList
  1193  	err = json.Unmarshal(resp, &images)
  1194  	return images.Images, err
  1195  }
  1196  
  1197  type memoryRegionInfo struct {
  1198  	start       uint64
  1199  	size        uint64
  1200  	permissions string
  1201  	name        string
  1202  }
  1203  
  1204  func decodeHexString(in []byte) (string, bool) {
  1205  	out := make([]byte, 0, len(in)/2)
  1206  	for i := 0; i < len(in); i += 2 {
  1207  		v, err := strconv.ParseUint(string(in[i:i+2]), 16, 8)
  1208  		if err != nil {
  1209  			return "", false
  1210  		}
  1211  		out = append(out, byte(v))
  1212  	}
  1213  	return string(out), true
  1214  }
  1215  
  1216  func (conn *gdbConn) memoryRegionInfo(addr uint64) (*memoryRegionInfo, error) {
  1217  	conn.outbuf.Reset()
  1218  	fmt.Fprintf(&conn.outbuf, "$qMemoryRegionInfo:%x", addr)
  1219  	resp, err := conn.exec(conn.outbuf.Bytes(), "qMemoryRegionInfo")
  1220  	if err != nil {
  1221  		return nil, err
  1222  	}
  1223  
  1224  	mri := &memoryRegionInfo{}
  1225  
  1226  	buf := resp
  1227  	for len(buf) > 0 {
  1228  		colon := bytes.Index(buf, []byte{':'})
  1229  		if colon < 0 {
  1230  			break
  1231  		}
  1232  		key := buf[:colon]
  1233  		buf = buf[colon+1:]
  1234  
  1235  		semicolon := bytes.Index(buf, []byte{';'})
  1236  		var value []byte
  1237  		if semicolon < 0 {
  1238  			value = buf
  1239  			buf = nil
  1240  		} else {
  1241  			value = buf[:semicolon]
  1242  			buf = buf[semicolon+1:]
  1243  		}
  1244  
  1245  		switch string(key) {
  1246  		case "start":
  1247  			start, err := strconv.ParseUint(string(value), 16, 64)
  1248  			if err != nil {
  1249  				return nil, fmt.Errorf("malformed qMemoryRegionInfo response packet (start): %v in %s", err, string(resp))
  1250  			}
  1251  			mri.start = start
  1252  		case "size":
  1253  			size, err := strconv.ParseUint(string(value), 16, 64)
  1254  			if err != nil {
  1255  				return nil, fmt.Errorf("malformed qMemoryRegionInfo response packet (size): %v in %s", err, string(resp))
  1256  			}
  1257  			mri.size = size
  1258  		case "permissions":
  1259  			mri.permissions = string(value)
  1260  		case "name":
  1261  			namestr, ok := decodeHexString(value)
  1262  			if !ok {
  1263  				return nil, fmt.Errorf("malformed qMemoryRegionInfo response packet (name): %s", string(resp))
  1264  			}
  1265  			mri.name = namestr
  1266  		case "error":
  1267  			errstr, ok := decodeHexString(value)
  1268  			if !ok {
  1269  				return nil, fmt.Errorf("malformed qMemoryRegionInfo response packet (error): %s", string(resp))
  1270  			}
  1271  			return nil, fmt.Errorf("qMemoryRegionInfo error: %s", errstr)
  1272  		}
  1273  	}
  1274  
  1275  	return mri, nil
  1276  }
  1277  
  1278  // exec executes a message to the stub and reads a response.
  1279  // The details of the wire protocol are described here:
  1280  //
  1281  //	https://sourceware.org/gdb/onlinedocs/gdb/Overview.html#Overview
  1282  func (conn *gdbConn) exec(cmd []byte, context string) ([]byte, error) {
  1283  	if err := conn.send(cmd); err != nil {
  1284  		return nil, err
  1285  	}
  1286  	return conn.recv(cmd, context, false)
  1287  }
  1288  
  1289  const hexdigit = "0123456789abcdef"
  1290  
  1291  func (conn *gdbConn) send(cmd []byte) error {
  1292  	if len(cmd) == 0 || cmd[0] != '$' {
  1293  		panic("gdb protocol error: command doesn't start with '$'")
  1294  	}
  1295  
  1296  	// append checksum to packet
  1297  	cmd = append(cmd, '#')
  1298  	sum := checksum(cmd)
  1299  	cmd = append(cmd, hexdigit[sum>>4], hexdigit[sum&0xf])
  1300  
  1301  	attempt := 0
  1302  	for {
  1303  		if logflags.GdbWire() {
  1304  			if len(cmd) > gdbWireMaxLen {
  1305  				conn.log.Debugf("<- %s...", string(cmd[:gdbWireMaxLen]))
  1306  			} else {
  1307  				conn.log.Debugf("<- %s", string(cmd))
  1308  			}
  1309  		}
  1310  		_, err := conn.conn.Write(cmd)
  1311  		if err != nil {
  1312  			return err
  1313  		}
  1314  
  1315  		if !conn.ack {
  1316  			break
  1317  		}
  1318  
  1319  		if conn.readack() {
  1320  			break
  1321  		}
  1322  		if attempt > conn.maxTransmitAttempts {
  1323  			return ErrTooManyAttempts
  1324  		}
  1325  		attempt++
  1326  	}
  1327  	return nil
  1328  }
  1329  
  1330  func (conn *gdbConn) recv(cmd []byte, context string, binary bool) (resp []byte, err error) {
  1331  	attempt := 0
  1332  	for {
  1333  		var err error
  1334  		resp, err = conn.rdr.ReadBytes('#')
  1335  		if err != nil {
  1336  			return nil, err
  1337  		}
  1338  
  1339  		// read checksum
  1340  		_, err = io.ReadFull(conn.rdr, conn.inbuf[:2])
  1341  		if err != nil {
  1342  			return nil, err
  1343  		}
  1344  		if logflags.GdbWire() {
  1345  			out := resp
  1346  			partial := false
  1347  			if !binary {
  1348  				if idx := bytes.Index(out, []byte{'\n'}); idx >= 0 {
  1349  					out = resp[:idx]
  1350  					partial = true
  1351  				}
  1352  			}
  1353  			if len(out) > gdbWireMaxLen {
  1354  				out = out[:gdbWireMaxLen]
  1355  				partial = true
  1356  			}
  1357  			if !partial {
  1358  				if binary {
  1359  					conn.log.Debugf("-> %q%s", string(resp), string(conn.inbuf[:2]))
  1360  				} else {
  1361  					conn.log.Debugf("-> %s%s", string(resp), string(conn.inbuf[:2]))
  1362  				}
  1363  			} else {
  1364  				if binary {
  1365  					conn.log.Debugf("-> %q...", string(out))
  1366  				} else {
  1367  					conn.log.Debugf("-> %s...", string(out))
  1368  				}
  1369  			}
  1370  		}
  1371  
  1372  		if !conn.ack {
  1373  			break
  1374  		}
  1375  
  1376  		if resp[0] == '%' {
  1377  			// If the first character is a % (instead of $) the stub sent us a
  1378  			// notification packet, this is weird since we specifically claimed that
  1379  			// we don't support notifications of any kind, but it should be safe to
  1380  			// ignore regardless.
  1381  			continue
  1382  		}
  1383  
  1384  		if checksumok(resp, conn.inbuf[:2]) {
  1385  			conn.sendack('+')
  1386  			break
  1387  		}
  1388  		if attempt > conn.maxTransmitAttempts {
  1389  			conn.sendack('+')
  1390  			return nil, ErrTooManyAttempts
  1391  		}
  1392  		attempt++
  1393  		conn.sendack('-')
  1394  	}
  1395  
  1396  	if binary {
  1397  		conn.inbuf, resp = binarywiredecode(resp, conn.inbuf)
  1398  	} else {
  1399  		conn.inbuf, resp = wiredecode(resp, conn.inbuf)
  1400  	}
  1401  
  1402  	if len(resp) == 0 || (resp[0] == 'E' && !binary) || (resp[0] == 'E' && len(resp) == 3) {
  1403  		cmdstr := ""
  1404  		if cmd != nil {
  1405  			cmdstr = string(cmd)
  1406  		}
  1407  		return nil, &GdbProtocolError{context, cmdstr, string(resp)}
  1408  	}
  1409  
  1410  	return resp, nil
  1411  }
  1412  
  1413  // Readack reads one byte from stub, returns true if the byte is '+'
  1414  func (conn *gdbConn) readack() bool {
  1415  	b, err := conn.rdr.ReadByte()
  1416  	if err != nil {
  1417  		return false
  1418  	}
  1419  	conn.log.Debugf("-> %s", string(b))
  1420  	return b == '+'
  1421  }
  1422  
  1423  // Sendack executes an ack character, c must be either '+' or '-'
  1424  func (conn *gdbConn) sendack(c byte) {
  1425  	if c != '+' && c != '-' {
  1426  		panic(fmt.Errorf("sendack(%c)", c))
  1427  	}
  1428  	conn.conn.Write([]byte{c})
  1429  	conn.log.Debugf("<- %s", string(c))
  1430  }
  1431  
  1432  // escapeXor is the value mandated by the specification to escape characters
  1433  const escapeXor byte = 0x20
  1434  
  1435  // wiredecode decodes the contents of in into buf.
  1436  // If buf is nil it will be allocated ex-novo, if the size of buf is not
  1437  // enough to hold the decoded contents it will be grown.
  1438  // Returns the newly allocated buffer as newbuf and the message contents as
  1439  // msg.
  1440  func wiredecode(in, buf []byte) (newbuf, msg []byte) {
  1441  	if buf != nil {
  1442  		buf = buf[:0]
  1443  	} else {
  1444  		buf = make([]byte, 0, 256)
  1445  	}
  1446  
  1447  	start := 1
  1448  
  1449  	for i := 0; i < len(in); i++ {
  1450  		switch ch := in[i]; ch {
  1451  		case '{': // escape
  1452  			if i+1 >= len(in) {
  1453  				buf = append(buf, ch)
  1454  			} else {
  1455  				buf = append(buf, in[i+1]^escapeXor)
  1456  				i++
  1457  			}
  1458  		case ':':
  1459  			buf = append(buf, ch)
  1460  			if i == 3 {
  1461  				// we just read the sequence identifier
  1462  				start = i + 1
  1463  			}
  1464  		case '#': // end of packet
  1465  			return buf, buf[start:]
  1466  		case '*': // runlength encoding marker
  1467  			if i+1 >= len(in) || i == 0 {
  1468  				buf = append(buf, ch)
  1469  			} else {
  1470  				n := in[i+1] - 29
  1471  				r := buf[len(buf)-1]
  1472  				for j := uint8(0); j < n; j++ {
  1473  					buf = append(buf, r)
  1474  				}
  1475  				i++
  1476  			}
  1477  		default:
  1478  			buf = append(buf, ch)
  1479  		}
  1480  	}
  1481  	return buf, buf[start:]
  1482  }
  1483  
  1484  // binarywiredecode is like wiredecode but decodes the wire encoding for
  1485  // binary packets, such as the 'x' and 'X' packets as well as all the json
  1486  // packets used by lldb/debugserver.
  1487  func binarywiredecode(in, buf []byte) (newbuf, msg []byte) {
  1488  	if buf != nil {
  1489  		buf = buf[:0]
  1490  	} else {
  1491  		buf = make([]byte, 0, 256)
  1492  	}
  1493  
  1494  	start := 1
  1495  
  1496  	for i := 0; i < len(in); i++ {
  1497  		switch ch := in[i]; ch {
  1498  		case '}': // escape
  1499  			if i+1 >= len(in) {
  1500  				buf = append(buf, ch)
  1501  			} else {
  1502  				buf = append(buf, in[i+1]^escapeXor)
  1503  				i++
  1504  			}
  1505  		case '#': // end of packet
  1506  			return buf, buf[start:]
  1507  		default:
  1508  			buf = append(buf, ch)
  1509  		}
  1510  	}
  1511  	return buf, buf[start:]
  1512  }
  1513  
  1514  // checksumok checks that checksum is a valid checksum for packet.
  1515  func checksumok(packet, checksumBuf []byte) bool {
  1516  	if packet[0] != '$' {
  1517  		return false
  1518  	}
  1519  
  1520  	sum := checksum(packet)
  1521  	tgt, err := strconv.ParseUint(string(checksumBuf), 16, 8)
  1522  	if err != nil {
  1523  		return false
  1524  	}
  1525  
  1526  	tgt8 := uint8(tgt)
  1527  
  1528  	return sum == tgt8
  1529  }
  1530  
  1531  func checksum(packet []byte) (sum uint8) {
  1532  	for i := 1; i < len(packet); i++ {
  1533  		if packet[i] == '#' {
  1534  			return sum
  1535  		}
  1536  		sum += packet[i]
  1537  	}
  1538  	return sum
  1539  }