gem5  v22.1.0.0
remote_gdb.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 ARM Limited
3  *
4  * The license below extends only to copyright in the software and shall
5  * not be construed as granting a license to any other intellectual
6  * property including but not limited to intellectual property relating
7  * to a hardware implementation of the functionality of the software
8  * licensed hereunder. You may use the software subject to the license
9  * terms below provided that you ensure that this notice is replicated
10  * unmodified and in its entirety in all distributions of the software,
11  * modified or unmodified, in source code or in binary form.
12  *
13  * Copyright 2015, 2021 LabWare
14  * Copyright 2014 Google, Inc.
15  * Copyright (c) 2002-2005 The Regents of The University of Michigan
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions are
20  * met: redistributions of source code must retain the above copyright
21  * notice, this list of conditions and the following disclaimer;
22  * redistributions in binary form must reproduce the above copyright
23  * notice, this list of conditions and the following disclaimer in the
24  * documentation and/or other materials provided with the distribution;
25  * neither the name of the copyright holders nor the names of its
26  * contributors may be used to endorse or promote products derived from
27  * this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40  */
41 
42 /*
43  * Copyright (c) 1990, 1993 The Regents of the University of California
44  * All rights reserved
45  *
46  * This software was developed by the Computer Systems Engineering group
47  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
48  * contributed to Berkeley.
49  *
50  * All advertising materials mentioning features or use of this software
51  * must display the following acknowledgement:
52  * This product includes software developed by the University of
53  * California, Lawrence Berkeley Laboratories.
54  *
55  * Redistribution and use in source and binary forms, with or without
56  * modification, are permitted provided that the following conditions
57  * are met:
58  * 1. Redistributions of source code must retain the above copyright
59  * notice, this list of conditions and the following disclaimer.
60  * 2. Redistributions in binary form must reproduce the above copyright
61  * notice, this list of conditions and the following disclaimer in the
62  * documentation and/or other materials provided with the distribution.
63  * 3. All advertising materials mentioning features or use of this software
64  * must display the following acknowledgement:
65  * This product includes software developed by the University of
66  * California, Berkeley and its contributors.
67  * 4. Neither the name of the University nor the names of its contributors
68  * may be used to endorse or promote products derived from this software
69  * without specific prior written permission.
70  *
71  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
72  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
73  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
74  * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
75  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
76  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
77  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
78  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
79  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
80  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
81  * SUCH DAMAGE.
82  *
83  * @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
84  */
85 
86 /*-
87  * Copyright (c) 2001 The NetBSD Foundation, Inc.
88  * All rights reserved.
89  *
90  * This code is derived from software contributed to The NetBSD Foundation
91  * by Jason R. Thorpe.
92  *
93  * Redistribution and use in source and binary forms, with or without
94  * modification, are permitted provided that the following conditions
95  * are met:
96  * 1. Redistributions of source code must retain the above copyright
97  * notice, this list of conditions and the following disclaimer.
98  * 2. Redistributions in binary form must reproduce the above copyright
99  * notice, this list of conditions and the following disclaimer in the
100  * documentation and/or other materials provided with the distribution.
101  * 3. All advertising materials mentioning features or use of this software
102  * must display the following acknowledgement:
103  * This product includes software developed by the NetBSD
104  * Foundation, Inc. and its contributors.
105  * 4. Neither the name of The NetBSD Foundation nor the names of its
106  * contributors may be used to endorse or promote products derived
107  * from this software without specific prior written permission.
108  *
109  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
110  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
111  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
112  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
113  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
114  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
115  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
116  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
117  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
118  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
119  * POSSIBILITY OF SUCH DAMAGE.
120  */
121 
122 /*
123  * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
124  *
125  * Taken from NetBSD
126  *
127  * "Stub" to allow remote cpu to debug over a serial line using gdb.
128  */
129 
130 #include "base/remote_gdb.hh"
131 
132 #include <sys/select.h>
133 #include <sys/signal.h>
134 #include <sys/time.h>
135 #include <unistd.h>
136 
137 #include <cassert>
138 #include <csignal>
139 #include <cstdint>
140 #include <cstdio>
141 #include <iterator>
142 #include <sstream>
143 #include <string>
144 #include <utility>
145 
146 #include "base/cprintf.hh"
147 #include "base/intmath.hh"
148 #include "base/logging.hh"
149 #include "base/socket.hh"
150 #include "base/trace.hh"
151 #include "cpu/base.hh"
152 #include "cpu/static_inst.hh"
153 #include "cpu/thread_context.hh"
154 #include "debug/GDBAll.hh"
155 #include "mem/port.hh"
156 #include "mem/port_proxy.hh"
159 #include "sim/full_system.hh"
160 #include "sim/process.hh"
161 #include "sim/system.hh"
162 
163 namespace gem5
164 {
165 
166 static const char GDBStart = '$';
167 static const char GDBEnd = '#';
168 static const char GDBGoodP = '+';
169 static const char GDBBadP = '-';
170 
171 class HardBreakpoint : public PCEvent
172 {
173  private:
175 
176  public:
177  int refcount;
178 
179  public:
181  : PCEvent(s, "HardBreakpoint Event", pc),
182  gdb(_gdb), refcount(0)
183  {
184  DPRINTF(GDBMisc, "creating hardware breakpoint at %#x\n", evpc);
185  }
186 
187  const std::string name() const override { return gdb->name() + ".hwbkpt"; }
188 
189  void
190  process(ThreadContext *tc) override
191  {
192  DPRINTF(GDBMisc, "handling hardware breakpoint at %#x\n", pc());
193 
194  if (tc == gdb->tc)
195  gdb->trap(tc->contextId(), SIGTRAP,"");
196  }
197 };
198 
199 namespace {
200 
201 // Exception to throw when the connection to the client is broken.
202 struct BadClient
203 {
204  const char *warning;
205  BadClient(const char *_warning=NULL) : warning(_warning)
206  {}
207 };
208 
209 // Exception to throw when an error needs to be reported to the client.
210 struct CmdError
211 {
212  std::string error;
213  CmdError(std::string _error) : error(_error)
214  {}
215 };
216 
217 // Exception to throw when something isn't supported.
218 class Unsupported {};
219 
220 // Convert a hex digit into an integer.
221 // This returns -1 if the argument passed is no valid hex digit.
222 int
223 digit2i(char c)
224 {
225  if (c >= '0' && c <= '9')
226  return (c - '0');
227  else if (c >= 'a' && c <= 'f')
228  return (c - 'a' + 10);
229  else if (c >= 'A' && c <= 'F')
230  return (c - 'A' + 10);
231  else
232  return -1;
233 }
234 
235 //convert a hex to a char
236 char
237 hex2c(char c0,char c1)
238 {
239  char temp[3] = {c0,c1,'\0'};
240  return std::stoi(temp,0,16);
241 }
242 
243 //this function will be used in a future patch
244 //convert a encoded string to a string
245 [[maybe_unused]] std::string
246 hexS2string(std::string hex_in)
247 {
248  std::string out="";
249  for (unsigned int i = 0; i + 1 < hex_in.length();i += 2){
250  out.push_back(hex2c(hex_in[i],hex_in[i+1]));
251  }
252  return out;
253 }
254 
255 //convert a string to a hex encoded string
256 std::string
257 string2hexS(std::string in)
258 {
259  std::string out = "";
260  for (auto ch : in){
261  char temp[3] = " ";
262  std::snprintf(temp,3,"%02hhx",ch);
263  out.append(temp);
264  }
265  return out;
266 }
267 
268 // Convert the low 4 bits of an integer into an hex digit.
269 char
270 i2digit(int n)
271 {
272  return "0123456789abcdef"[n & 0x0f];
273 }
274 
275 // Convert a byte array into an hex string.
276 void
277 mem2hex(char *vdst, const char *vsrc, int len)
278 {
279  char *dst = vdst;
280  const char *src = vsrc;
281 
282  while (len--) {
283  *dst++ = i2digit(*src >> 4);
284  *dst++ = i2digit(*src++);
285  }
286  *dst = '\0';
287 }
288 
289 // Convert an hex string into a byte array.
290 // This returns a pointer to the character following the last valid
291 // hex digit. If the string ends in the middle of a byte, NULL is
292 // returned.
293 const char *
294 hex2mem(char *vdst, const char *src, int max_len)
295 {
296  char *dst = vdst;
297  int msb, lsb;
298 
299  while (*src && max_len--) {
300  msb = digit2i(*src++);
301  if (msb < 0)
302  return (src - 1);
303  lsb = digit2i(*src++);
304  if (lsb < 0)
305  return (NULL);
306  *dst++ = (msb << 4) | lsb;
307  }
308  return src;
309 }
310 
311 // Convert an hex string into an integer.
312 // This returns a pointer to the character following the last valid
313 // hex digit.
314 Addr
315 hex2i(const char **srcp)
316 {
317  const char *src = *srcp;
318  Addr r = 0;
319  int nibble;
320 
321  while ((nibble = digit2i(*src)) >= 0) {
322  r *= 16;
323  r += nibble;
324  src++;
325  }
326  *srcp = src;
327  return r;
328 }
329 
330 bool
331 parseThreadId(const char **srcp, bool &all, bool &any, ContextID &tid)
332 {
333  all = any = false;
334  tid = 0;
335  const char *src = *srcp;
336  if (*src == '-') {
337  // This could be the start of -1, which means all threads.
338  src++;
339  if (*src++ != '1')
340  return false;
341  *srcp += 2;
342  all = true;
343  return true;
344  }
345  tid = hex2i(srcp);
346  // If *srcp still points to src, no characters were consumed and no thread
347  // id was found. Without this check, we can't tell the difference between
348  // zero and a parsing error.
349  if (*srcp == src)
350  return false;
351 
352  if (tid == 0)
353  any = true;
354 
355  tid--;
356 
357  return true;
358 }
359 
360 int
361 encodeThreadId(ContextID id)
362 {
363  // Thread ID 0 is reserved and means "pick any thread".
364  return id + 1;
365 }
366 
367 enum GdbBreakpointType
368 {
369  GdbSoftBp = '0',
370  GdbHardBp = '1',
371  GdbWriteWp = '2',
372  GdbReadWp = '3',
373  GdbAccWp = '4',
374 };
375 
376 const char *
377 breakType(char c)
378 {
379  switch(c) {
380  case GdbSoftBp: return "software breakpoint";
381  case GdbHardBp: return "hardware breakpoint";
382  case GdbWriteWp: return "write watchpoint";
383  case GdbReadWp: return "read watchpoint";
384  case GdbAccWp: return "access watchpoint";
385  default: return "unknown breakpoint/watchpoint";
386  }
387 }
388 
389 std::map<Addr, HardBreakpoint *> hardBreakMap;
390 
391 }
392 
393 BaseRemoteGDB::BaseRemoteGDB(System *_system, int _port) :
394  incomingConnectionEvent(nullptr), incomingDataEvent(nullptr),
395  _port(_port), fd(-1), sys(_system),
396  connectEvent(this), disconnectEvent(this), trapEvent(this),
397  singleStepEvent(*this)
398 {}
399 
401 {
403  delete incomingDataEvent;
404 }
405 
406 std::string
408 {
409  return sys->name() + ".remote_gdb";
410 }
411 
412 void
414 {
416  warn_once("Sockets disabled, not accepting gdb connections");
417  return;
418  }
419 
420  while (!listener.listen(_port, true)) {
421  DPRINTF(GDBMisc, "Can't bind port %d\n", _port);
422  _port++;
423  }
424 
426  new IncomingConnectionEvent(this, listener.getfd(), POLLIN);
428 
429  ccprintf(std::cerr, "%d: %s: listening for remote gdb on port %d\n",
430  curTick(), name(), _port);
431 }
432 
433 void
435 {
437  "Can't accept GDB connections without any threads!");
438 
440 
441  int sfd = listener.accept(true);
442 
443  if (sfd != -1) {
444  if (isAttached())
445  close(sfd);
446  else
447  attach(sfd);
448  }
449 }
450 
451 int
453 {
455  "Remote GDB port is unknown until listen() has been called.\n");
456  return _port;
457 }
458 
459 void
461 {
462  fd = f;
463 
464  attached = true;
465  DPRINTFN("remote gdb attached\n");
466 
467  processCommands();
468 
469  if (isAttached()) {
470  // At this point an initial communication with GDB is handled
471  // and we're ready to continue. Here we arrange IncomingDataEvent
472  // to get notified when GDB breaks in.
473  //
474  // However, GDB can decide to disconnect during that initial
475  // communication. In that case, we cannot arrange data event because
476  // the socket is already closed (not that it makes any sense, anyways).
477  //
478  // Hence the check above.
479  incomingDataEvent = new IncomingDataEvent(this, fd, POLLIN);
481  }
482 }
483 
484 void
486 {
487  attached = false;
488  clearSingleStep();
489  close(fd);
490  fd = -1;
491 
492  if (incomingDataEvent) {
493  // incomingDataEvent gets scheduled in attach() after
494  // initial communication with GDB is handled and GDB tells
495  // gem5 to continue.
496  //
497  // GDB can disconnect before that in which case `incomingDataEvent`
498  // is NULL.
499  //
500  // Hence the check above.
501 
503  incomingDataEvent = nullptr;
504  }
506  DPRINTFN("remote gdb detached\n");
507 }
508 
509 void
511 {
512  [[maybe_unused]] auto it_success = threads.insert({_tc->contextId(), _tc});
513  assert(it_success.second);
514  // If no ThreadContext is current selected, select this one.
515  if (!tc)
516  assert(selectThreadContext(_tc->contextId()));
517 
518  // Now that we have a thread, we can start listening.
519  if (!listener.islistening())
520  listen();
521 }
522 
523 void
525 {
526  auto it = threads.find(_tc->contextId());
527  panic_if(it == threads.end(), "No context with ID %d found.",
528  _tc->contextId());
529  it->second = _tc;
530 }
531 
532 bool
534 {
535  auto it = threads.find(id);
536  if (it == threads.end())
537  return false;
538 
539  tc = it->second;
540  // Update the register cache for the new thread context, if there is one.
541  if (regCachePtr)
543  return true;
544 }
545 
546 // This function does all command processing for interfacing to a
547 // remote gdb. Note that the error codes are ignored by gdb at
548 // present, but might eventually become meaningful. (XXX) It might
549 // makes sense to use POSIX errno values, because that is what the
550 // gdb/remote.c functions want to return.
551 void
552 BaseRemoteGDB::trap(ContextID id, int signum,const std::string& stopReason)
553 {
554  if (!attached)
555  return;
556 
557  if (tc->contextId() != id) {
558 
559  //prevent thread switch when single stepping
560  if (singleStepEvent.scheduled()){
561  return;
562  }
563  DPRINTF(GDBMisc, "Finishing thread switch");
564  if (!selectThreadContext(id))
565  return;
566  }
567 
568  DPRINTF(GDBMisc, "trap: PC=%s\n", tc->pcState());
569 
570  clearSingleStep();
571 
572  if (threadSwitching) {
573  threadSwitching = false;
574  // Tell GDB the thread switch has completed.
575  send("OK");
576  } else {
577  // Tell remote host that an exception has occurred.
578  sendTPacket(signum,id,stopReason);
579  }
580 
581  processCommands(signum);
582 }
583 
584 bool
585 BaseRemoteGDB::sendMessage(std::string message)
586 {
587  if (!attached)
588  return false;
589  DPRINTF(GDBMisc, "passing message %s\n", message);
590  sendOPacket(message);
591  return true;
592 }
593 
594 void
596 {
597  if (connectEvent.scheduled()) {
598  warn("GDB connect event has already been scheduled!");
599  return;
600  }
601 
602  if (revent & POLLIN) {
604  }
605 }
606 
607 void
609 {
610  if (trapEvent.scheduled()) {
611  warn("GDB trap event has already been scheduled!");
612  return;
613  }
614 
615  if (revent & POLLIN) {
616  scheduleTrapEvent(tc->contextId(),SIGILL,0,"");
617  } else if (revent & POLLNVAL) {
620  }
621 }
622 
623 uint8_t
625 {
626  uint8_t b;
627  while (!try_getbyte(&b,-1));//no timeout
628  return b;
629 }
630 
631 bool
632 BaseRemoteGDB::try_getbyte(uint8_t* c,int timeout_ms)
633 {
634  if (!c)
635  panic("try_getbyte called with a null pointer as c");
636  int res,retval;
637  //Allow read to fail if it was interrupted by a signal (EINTR).
638  errno = 0;
639  //preparing fd_sets
640  fd_set rfds;
641  FD_ZERO(&rfds);
642  FD_SET(fd, &rfds);
643 
644  //setting up a timeout if timeout_ms is positive
645  struct timeval tv;struct timeval* tv_ptr;
646  if (timeout_ms >= 0){
647  tv.tv_sec = timeout_ms/1000;
648  tv.tv_usec = timeout_ms%1000;
649  tv_ptr = &tv;
650  }else{
651  tv_ptr = NULL;
652  }
653  //Using select to check if the FD is ready to be read.
654  while(true){
655  do {
656  errno = 0;
657  retval = ::select(fd + 1, &rfds, NULL, NULL, tv_ptr);
658  if (retval < 0 && errno != EINTR){//error
659  DPRINTF(GDBMisc,"getbyte failed errno=%i retval=%i\n",
660  errno,retval);
661  throw BadClient("Couldn't read data from debugger.");
662  }
663  //a EINTR error means that the select call was interrupted
664  //by another signal
665  }while (errno == EINTR);
666  if (retval == 0)
667  return false;//timed out
668  //reading (retval>0)
669  res = ::read(fd, c, sizeof(*c));
670  if (res == sizeof(*c))
671  return true;//read successfully
672  //read failed (?) retrying select
673  }
674 }
675 void
677 {
678  if (::write(fd, &b, sizeof(b)) == sizeof(b))
679  return;
680 
681  throw BadClient("Couldn't write data to the debugger.");
682 }
683 
684 // Receive a packet from gdb
685 void
687 {
688  uint8_t c;
689  int csum;
690  bp.resize(0);
691 
692  do {
693  csum = 0;
694  // Find the beginning of a packet
695  while ((c = getbyte()) != GDBStart);
696 
697  // Read until you find the end of the data in the packet, and keep
698  // track of the check sum.
699  while (true) {
700  c = getbyte();
701  if (c == GDBEnd)
702  break;
703  c &= 0x7f;
704  csum += c;
705  bp.push_back(c);
706  }
707 
708  // Mask the check sum.
709  csum &= 0xff;
710 
711  // Bring in the checksum. If the check sum matches, csum will be 0.
712  csum -= digit2i(getbyte()) * 16;
713  csum -= digit2i(getbyte());
714 
715  // If the check sum was correct
716  if (csum == 0) {
717  // Report that the packet was received correctly
718  putbyte(GDBGoodP);
719  // Sequence present?
720  if (bp.size() > 2 && bp[2] == ':') {
721  putbyte(bp[0]);
722  putbyte(bp[1]);
723  auto begin = std::begin(bp);
724  bp.erase(begin, std::next(begin, 3));
725  }
726  break;
727  }
728  // Otherwise, report that there was a mistake.
729  putbyte(GDBBadP);
730  } while (1);
731  // Sentinel.
732  bp.push_back('\0');
733  DPRINTF(GDBRecv, "recv: %s\n", bp.data());
734 }
735 
736 // Send a packet to gdb
737 void
738 BaseRemoteGDB::send(const char *bp)
739 {
740  const char *p;
741  uint8_t csum, c;
742 
743  DPRINTF(GDBSend, "send: %s\n", bp);
744  //removing GDBBadP that could be waiting in the buffer
745  while (try_getbyte(&c,0));
746  do {
747  p = bp;
748  // Start sending a packet
749  putbyte(GDBStart);
750  // Send the contents, and also keep a check sum.
751  for (csum = 0; (c = *p); p++) {
752  putbyte(c);
753  csum += c;
754  }
755  // Send the ending character.
756  putbyte(GDBEnd);
757  // Send the checksum.
758  putbyte(i2digit(csum >> 4));
759  putbyte(i2digit(csum));
760  // Try transmitting over and over again until the other end doesn't
761  // send an error back.
762  c = getbyte();
763  if ((c & 0x7f) == GDBBadP)
764  DPRINTF(GDBSend, "PacketError\n");
765  } while ((c & 0x7f) == GDBBadP);
766 }
767 
768 void
770 {
771  // Stick frame regs into our reg cache.
772  regCachePtr = gdbRegs();
774 
775  GdbCommand::Context cmd_ctx;
776  cmd_ctx.type = signum;
778 
779  for (;;) {
780  try {
781  recv(data);
782  if (data.size() == 1)
783  throw BadClient();
784  cmd_ctx.cmdByte = data[0];
785  cmd_ctx.data = data.data() + 1;
786  // One for sentinel, one for cmdByte.
787  cmd_ctx.len = data.size() - 2;
788 
789  auto cmd_it = commandMap.find(cmd_ctx.cmdByte);
790  if (cmd_it == commandMap.end()) {
791  DPRINTF(GDBMisc, "Unknown command: %c(%#x)\n",
792  cmd_ctx.cmdByte, cmd_ctx.cmdByte);
793  throw Unsupported();
794  }
795  cmd_ctx.cmd = &(cmd_it->second);
796 
797  if (!(this->*(cmd_ctx.cmd->func))(cmd_ctx))
798  break;
799 
800  } catch (BadClient &e) {
801  if (e.warning)
802  warn(e.warning);
803  detach();
804  break;
805  } catch (Unsupported &e) {
806  send("");
807  } catch (CmdError &e) {
808  send(e.error);
809  } catch (std::exception &e) {
810  panic("Unrecognized GDB exception: %s", e.what());
811  } catch (...) {
812  panic("Unrecognized GDB exception.");
813  }
814  }
815 }
816 
817 bool
819 {
820  TranslatingPortProxy fs_proxy(tc);
821  SETranslatingPortProxy se_proxy(tc);
822  PortProxy &virt_proxy = FullSystem ? fs_proxy : se_proxy;
823 
824  virt_proxy.readBlob(vaddr, data, size);
825  return true;
826 }
827 
828 bool
829 BaseRemoteGDB::writeBlob(Addr vaddr, size_t size, const char *data)
830 {
831  TranslatingPortProxy fs_proxy(tc);
832  SETranslatingPortProxy se_proxy(tc);
833  PortProxy &virt_proxy = FullSystem ? fs_proxy : se_proxy;
834 
835  virt_proxy.writeBlob(vaddr, data, size);
836  return true;
837 }
838 
839 // Read bytes from kernel address space for debugger.
840 bool
841 BaseRemoteGDB::read(Addr vaddr, size_t size, char *data)
842 {
843  DPRINTF(GDBRead, "read: addr=%#x, size=%d", vaddr, size);
844 
845  bool res = readBlob(vaddr, size, data);
846 
847  if (!res)
848  return false;
849 
850 #if TRACING_ON
851  if (debug::GDBRead) {
852  if (debug::GDBExtra) {
853  char buf[1024];
854  mem2hex(buf, data, size);
855  DPRINTFNR(": %s\n", buf);
856  } else
857  DPRINTFNR("\n");
858  }
859 #endif
860 
861  return true;
862 }
863 
864 // Write bytes to kernel address space for debugger.
865 bool
866 BaseRemoteGDB::write(Addr vaddr, size_t size, const char *data)
867 {
868  if (debug::GDBWrite) {
869  DPRINTFN("write: addr=%#x, size=%d", vaddr, size);
870  if (debug::GDBExtra) {
871  char buf[1024];
872  mem2hex(buf, data, size);
873  DPRINTFNR(": %s\n", buf);
874  } else
875  DPRINTFNR("\n");
876  }
877  return writeBlob(vaddr, size, data);
878 }
879 
880 void
882 {
883  if (!singleStepEvent.scheduled())
885  trap(tc->contextId(), SIGTRAP);
886 }
887 
888 void
890 {
892 }
893 
894 void
896 {
897  if (!singleStepEvent.scheduled())
899 }
900 
901 void
903 {
904  if (!checkBpKind(kind))
905  throw BadClient("Invalid breakpoint kind.\n");
906 
907  return insertHardBreak(addr, kind);
908 }
909 
910 void
912 {
913  if (!checkBpKind(kind))
914  throw BadClient("Invalid breakpoint kind.\n");
915 
916  return removeHardBreak(addr, kind);
917 }
918 
919 void
921 {
922  if (!checkBpKind(kind))
923  throw BadClient("Invalid breakpoint kind.\n");
924 
925  DPRINTF(GDBMisc, "Inserting hardware breakpoint at %#x\n", addr);
926 
927  HardBreakpoint *&bkpt = hardBreakMap[addr];
928  if (bkpt == 0)
929  bkpt = new HardBreakpoint(this, sys, addr);
930 
931  bkpt->refcount++;
932 }
933 
934 void
936 {
937  if (!checkBpKind(kind))
938  throw BadClient("Invalid breakpoint kind.\n");
939 
940  DPRINTF(GDBMisc, "Removing hardware breakpoint at %#x\n", addr);
941 
942  auto i = hardBreakMap.find(addr);
943  if (i == hardBreakMap.end())
944  throw CmdError("E0C");
945 
946  HardBreakpoint *hbp = (*i).second;
947  if (--hbp->refcount == 0) {
948  delete hbp;
949  hardBreakMap.erase(i);
950  }
951 }
952 
953 void
955  const std::string& stopReason)
956 {
957  if (!stopReason.empty()){
958  send("T%02xcore:%x;thread:%x;%s;",errnum,id + 1,id + 1,stopReason);
959  }else{
960  send("T%02xcore:%x;thread:%x;",errnum,id + 1,id + 1);
961  }
962 }
963 void
965  send("S%02x",errnum);
966 }
967 void
968 BaseRemoteGDB::sendOPacket(const std::string message){
969  send("O" + string2hexS(message));
970 }
971 
972 void
974  std::string stopReason){
975  ThreadContext* _tc = threads[id];
976  panic_if(_tc == nullptr, "Unknown context id :%i",id);
977  trapEvent.id(id);
979  trapEvent.stopReason(stopReason);
980  if (!trapEvent.scheduled())
982 }
983 
984 void
986 {
987  if (delta == 0 && _tc->status() != ThreadContext::Active) {
988  // If delta is zero, we're just trying to wait for an instruction
989  // boundary. If the CPU is not active, assume we're already at a
990  // boundary without waiting for the CPU to eventually wake up.
991  ev->process();
992  } else {
993  // Here "ticks" aren't simulator ticks which measure time, they're
994  // instructions committed by the CPU.
995  _tc->scheduleInstCountEvent(ev, _tc->getCurrentInstCount() + delta);
996  }
997 }
998 
999 void
1001 {
1002  if (ev->scheduled())
1004 }
1005 
1006 std::map<char, BaseRemoteGDB::GdbCommand> BaseRemoteGDB::commandMap = {
1007  // last signal
1008  { '?', { "KGDB_SIGNAL", &BaseRemoteGDB::cmdSignal } },
1009  // set baud (deprecated)
1010  { 'b', { "KGDB_SET_BAUD", &BaseRemoteGDB::cmdUnsupported } },
1011  // set breakpoint (deprecated)
1012  { 'B', { "KGDB_SET_BREAK", &BaseRemoteGDB::cmdUnsupported } },
1013  // resume
1014  { 'c', { "KGDB_CONT", &BaseRemoteGDB::cmdCont } },
1015  // continue with signal
1016  { 'C', { "KGDB_ASYNC_CONT", &BaseRemoteGDB::cmdAsyncCont } },
1017  // toggle debug flags (deprecated)
1018  { 'd', { "KGDB_DEBUG", &BaseRemoteGDB::cmdUnsupported } },
1019  // detach remote gdb
1020  { 'D', { "KGDB_DETACH", &BaseRemoteGDB::cmdDetach } },
1021  // read general registers
1022  { 'g', { "KGDB_REG_R", &BaseRemoteGDB::cmdRegR } },
1023  // write general registers
1024  { 'G', { "KGDB_REG_W", &BaseRemoteGDB::cmdRegW } },
1025  // set thread
1026  { 'H', { "KGDB_SET_THREAD", &BaseRemoteGDB::cmdSetThread } },
1027  // step a single cycle
1028  { 'i', { "KGDB_CYCLE_STEP", &BaseRemoteGDB::cmdUnsupported } },
1029  // signal then cycle step
1030  { 'I', { "KGDB_SIG_CYCLE_STEP", &BaseRemoteGDB::cmdUnsupported } },
1031  // kill program
1032  { 'k', { "KGDB_KILL", &BaseRemoteGDB::cmdDetach } },
1033  // read memory
1034  { 'm', { "KGDB_MEM_R", &BaseRemoteGDB::cmdMemR } },
1035  // write memory
1036  { 'M', { "KGDB_MEM_W", &BaseRemoteGDB::cmdMemW } },
1037  // read register
1038  { 'p', { "KGDB_READ_REG", &BaseRemoteGDB::cmdUnsupported } },
1039  // write register
1040  { 'P', { "KGDB_SET_REG", &BaseRemoteGDB::cmdUnsupported } },
1041  // query variable
1042  { 'q', { "KGDB_QUERY_VAR", &BaseRemoteGDB::cmdQueryVar } },
1043  // set variable
1044  { 'Q', { "KGDB_SET_VAR", &BaseRemoteGDB::cmdUnsupported } },
1045  // reset system (deprecated)
1046  { 'r', { "KGDB_RESET", &BaseRemoteGDB::cmdUnsupported } },
1047  // step
1048  { 's', { "KGDB_STEP", &BaseRemoteGDB::cmdStep } },
1049  // signal and step
1050  { 'S', { "KGDB_ASYNC_STEP", &BaseRemoteGDB::cmdAsyncStep } },
1051  // find out if the thread is alive
1052  { 'T', { "KGDB_THREAD_ALIVE", &BaseRemoteGDB::cmdIsThreadAlive } },
1053  //multi letter command
1054  { 'v', { "KGDB_MULTI_LETTER", &BaseRemoteGDB::cmdMultiLetter } },
1055  // target exited
1056  { 'W', { "KGDB_TARGET_EXIT", &BaseRemoteGDB::cmdUnsupported } },
1057  // write memory
1058  { 'X', { "KGDB_BINARY_DLOAD", &BaseRemoteGDB::cmdUnsupported } },
1059  // remove breakpoint or watchpoint
1060  { 'z', { "KGDB_CLR_HW_BKPT", &BaseRemoteGDB::cmdClrHwBkpt } },
1061  // insert breakpoint or watchpoint
1062  { 'Z', { "KGDB_SET_HW_BKPT", &BaseRemoteGDB::cmdSetHwBkpt } },
1063  // non-standard RSP extension: dump page table
1064  { '.', { "GET_PAGE_TABLE", &BaseRemoteGDB::cmdDumpPageTable } },
1065 };
1066 
1067 bool
1069 {
1070  return true;
1071 }
1072 
1073 bool
1075 {
1076  DPRINTF(GDBMisc, "Unsupported command: %s\n", ctx.cmd->name);
1077  DDUMP(GDBMisc, ctx.data, ctx.len);
1078  throw Unsupported();
1079 }
1080 
1081 
1082 bool
1084 {
1085  sendTPacket(ctx.type,tc->contextId(),"");
1086  return true;
1087 }
1088 
1089 bool
1091 {
1092  const char *p = ctx.data;
1093  if (ctx.len) {
1094  Addr new_pc = hex2i(&p);
1095  tc->pcState(new_pc);
1096  }
1097  clearSingleStep();
1098  return false;
1099 }
1100 
1101 bool
1103 {
1104  const char *p = ctx.data;
1105  hex2i(&p);
1106  if (*p++ == ';') {
1107  Addr new_pc = hex2i(&p);
1108  tc->pcState(new_pc);
1109  }
1110  clearSingleStep();
1111  return false;
1112 }
1113 
1114 bool
1116 {
1117  detach();
1118  return false;
1119 }
1120 
1121 bool
1123 {
1124  char buf[2 * regCachePtr->size() + 1];
1125  buf[2 * regCachePtr->size()] = '\0';
1126  mem2hex(buf, regCachePtr->data(), regCachePtr->size());
1127  send(buf);
1128  return true;
1129 }
1130 
1131 bool
1133 {
1134  const char *p = ctx.data;
1135  p = hex2mem(regCachePtr->data(), p, regCachePtr->size());
1136  if (p == NULL || *p != '\0')
1137  throw CmdError("E01");
1138 
1140  send("OK");
1141 
1142  return true;
1143 }
1144 
1145 bool
1147 {
1148  const char *p = ctx.data;
1149  char subcommand = *p++;
1150  int tid = 0;
1151  bool all, any;
1152  if (!parseThreadId(&p, all, any, tid))
1153  throw CmdError("E01");
1154 
1155  if (subcommand == 'c') {
1156  // We can only single step or continue all threads at once, since we
1157  // stop time itself and not individual threads.
1158  if (!all)
1159  throw CmdError("E02");
1160  } else if (subcommand == 'g') {
1161  // We don't currently support reading registers, memory, etc, from all
1162  // threads at once. GDB may never ask for this, but if it does we
1163  // should complain.
1164  if (all)
1165  throw CmdError("E03");
1166 
1167  // If GDB doesn't care which thread we're using, keep using the
1168  // current one, otherwise switch.
1169  if (!any && tid != tc->contextId()) {
1170  if (!selectThreadContext(tid))
1171  throw CmdError("E04");
1172  // Line up on an instruction boundary in the new thread.
1173  threadSwitching = true;
1174  scheduleTrapEvent(tid,0,0,"");
1175  return false;
1176  }
1177  } else {
1178  throw CmdError("E05");
1179  }
1180 
1181  send("OK");
1182  return true;
1183 }
1184 
1185 bool
1187 {
1188  const char *p = ctx.data;
1189  int tid = 0;
1190  bool all, any;
1191  if (!parseThreadId(&p, all, any, tid))
1192  throw CmdError("E01");
1193  if (all)
1194  throw CmdError("E03");
1195  if (threads.find(tid) == threads.end())
1196  throw CmdError("E04");
1197  send("OK");
1198  return true;
1199 }
1200 
1201 bool
1203 {
1204  const char *p = ctx.data;
1205  Addr addr = hex2i(&p);
1206  if (*p++ != ',')
1207  throw CmdError("E02");
1208  size_t len = hex2i(&p);
1209  if (*p != '\0')
1210  throw CmdError("E03");
1211  if (!acc(addr, len))
1212  throw CmdError("E05");
1213 
1214  char buf[len];
1215  if (!read(addr, len, buf))
1216  throw CmdError("E05");
1217 
1218  char temp[2 * len + 1];
1219  temp[2 * len] = '\0';
1220  mem2hex(temp, buf, len);
1221  send(temp);
1222  return true;
1223 }
1224 
1225 bool
1227 {
1228  const char *p = ctx.data;
1229  Addr addr = hex2i(&p);
1230  if (*p++ != ',')
1231  throw CmdError("E06");
1232  size_t len = hex2i(&p);
1233  if (*p++ != ':')
1234  throw CmdError("E07");
1235  if (len * 2 > ctx.len - (p - ctx.data))
1236  throw CmdError("E08");
1237  char buf[len];
1238  p = (char *)hex2mem(buf, p, len);
1239  if (p == NULL)
1240  throw CmdError("E09");
1241  if (!acc(addr, len))
1242  throw CmdError("E0A");
1243  if (!write(addr, len, buf))
1244  throw CmdError("E0B");
1245  send("OK");
1246  return true;
1247 }
1248 
1249 bool
1251 {
1253  new_ctx.type = ctx.type;
1254  strtok(ctx.data,";?");
1255  char* sep = strtok(NULL,";:?");
1256 
1257  int txt_len = (sep != NULL) ? (sep - ctx.data) : strlen(ctx.data);
1258  DPRINTF(GDBMisc, "Multi-letter: %s , len=%i\n", ctx.data,txt_len);
1259  new_ctx.cmdTxt = std::string(ctx.data,txt_len);
1260  new_ctx.data = sep;
1261  new_ctx.len = ctx.len - txt_len;
1262  try {
1263  auto cmd_it = multiLetterMap.find(new_ctx.cmdTxt);
1264  if (cmd_it == multiLetterMap.end()) {
1265  DPRINTF(GDBMisc, "Unknown command: %s\n", new_ctx.cmdTxt);
1266  throw Unsupported();
1267  }
1268  new_ctx.cmd = &(cmd_it->second);
1269 
1270  return (this->*(new_ctx.cmd->func))(new_ctx);
1271  //catching errors: we don't need to catch anything else
1272  //as it will be handled by processCommands
1273  } catch (CmdError &e) {
1274  send(e.error);
1275  }
1276  return false;
1277 }
1278 
1279 std::map<std::string, BaseRemoteGDB::GdbMultiLetterCommand>
1281  { "MustReplyEmpty", { "KGDB_REPLY_EMPTY", &BaseRemoteGDB::cmdReplyEmpty}},
1282  { "Kill", { "KGDB_VKILL", &BaseRemoteGDB::cmdVKill}},
1283 };
1284 
1285 
1286 bool
1288 {
1289  send("");
1290  return true;
1291 }
1292 
1293 bool
1295 {
1296  warn("GDB command for kill received detaching instead");
1297  detach();
1298  return false;
1299 }
1300 
1301 bool
1303 {
1304  DPRINTF(GDBMisc, "Unsupported Multi name command : %s\n",
1305  ctx.cmd->name);
1306  DDUMP(GDBMisc, ctx.data, ctx.len);
1307  throw Unsupported();
1308 }
1309 
1310 namespace {
1311 
1313 splitAt(std::string str, const char * const delim)
1314 {
1315  size_t pos = str.find_first_of(delim);
1316  if (pos == std::string::npos)
1317  return std::pair<std::string, std::string>(str, "");
1318  else
1320  str.substr(0, pos), str.substr(pos + 1));
1321 }
1322 
1323 } // anonymous namespace
1324 
1325 std::map<std::string, BaseRemoteGDB::QuerySetCommand>
1327  { "C", { &BaseRemoteGDB::queryC } },
1328  { "Attached", { &BaseRemoteGDB::queryAttached} },
1329  { "Supported", { &BaseRemoteGDB::querySupported, ";" } },
1330  { "Xfer", { &BaseRemoteGDB::queryXfer } },
1331  { "Symbol", { &BaseRemoteGDB::querySymbol ,":" } },
1332  { "fThreadInfo", { &BaseRemoteGDB::queryFThreadInfo } },
1333  { "sThreadInfo", { &BaseRemoteGDB::querySThreadInfo } },
1334 };
1335 
1336 void
1338 {
1339  send("QC%x", encodeThreadId(tc->contextId()));
1340 }
1341 
1342 void
1344 {
1345  std::ostringstream oss;
1346  // This reply field mandatory. We can receive arbitrarily
1347  // long packets, so we could choose it to be arbitrarily large.
1348  // This is just an arbitrary filler value that seems to work.
1349  oss << "PacketSize=1024";
1350  for (const auto& feature : availableFeatures())
1351  oss << ';' << feature;
1352  send(oss.str());
1353 }
1354 
1355 void
1357 {
1358  auto split = splitAt(ctx.args.at(0), ":");
1359  auto object = split.first;
1360 
1361  split = splitAt(split.second, ":");
1362  auto operation = split.first;
1363 
1364  // Only the "features" object and "read"ing are supported currently.
1365  if (object != "features" || operation != "read")
1366  throw Unsupported();
1367 
1368  // Extract the annex name.
1369  split = splitAt(split.second, ":");
1370  auto annex = split.first;
1371 
1372  // Read the contents of the annex.
1373  std::string content;
1374  if (!getXferFeaturesRead(annex, content))
1375  throw CmdError("E00");
1376 
1377  // Extract the offset and length.
1378  split = splitAt(split.second, ",");
1379  auto offset_str = split.first;
1380  auto length_str = split.second;
1381 
1382  const char *offset_ptr = offset_str.c_str();
1383  const char *length_ptr = length_str.c_str();
1384  auto offset = hex2i(&offset_ptr);
1385  auto length = hex2i(&length_ptr);
1386  if (offset_ptr != offset_str.c_str() + offset_str.length() ||
1387  length_ptr != length_str.c_str() + length_str.length()) {
1388  throw CmdError("E00");
1389  }
1390 
1391  std::string encoded;
1392  encodeXferResponse(content, encoded, offset, length);
1393  send(encoded);
1394 }
1395 void
1397 {
1398  //The target does not need to look up any (more) symbols.
1399  send("OK");
1400 }
1401 
1402 void
1404 {
1405  std::string pid="";
1406  if (!ctx.args.empty() && !ctx.args[0].empty()){
1407  pid=ctx.args[0];
1408  }
1409  DPRINTF(GDBMisc, "QAttached : pid=%s\n",pid);
1410  //The remote server is attached to an existing process.
1411  send("1");
1412 }
1413 
1414 
1415 void
1417 {
1418  threadInfoIdx = 0;
1419  querySThreadInfo(ctx);
1420 }
1421 
1422 void
1424 {
1425  if (threadInfoIdx >= threads.size()) {
1426  threadInfoIdx = 0;
1427  send("l");
1428  } else {
1429  auto it = threads.begin();
1430  std::advance(it, threadInfoIdx++);
1431  send("m%x", encodeThreadId(it->second->contextId()));
1432  }
1433 }
1434 
1435 bool
1437 {
1438  // The query command goes until the first ':', or the end of the string.
1439  std::string s(ctx.data, ctx.len);
1440  auto query_split = splitAt({ ctx.data, (size_t)ctx.len }, ":");
1441  const auto &query_str = query_split.first;
1442 
1443  // Look up the query command, and report if it isn't found.
1444  auto query_it = queryMap.find(query_str);
1445  if (query_it == queryMap.end()) {
1446  DPRINTF(GDBMisc, "Unknown query %s\n", s);
1447  throw Unsupported();
1448  }
1449 
1450  // If it was found, construct a context.
1451  QuerySetCommand::Context qctx(query_str);
1452 
1453  const auto &query = query_it->second;
1454  auto remaining = std::move(query_split.second);
1455  if (!query.argSep) {
1456  qctx.args.emplace_back(std::move(remaining));
1457  } else {
1458  while (remaining != "") {
1459  auto arg_split = splitAt(remaining, query.argSep);
1460  qctx.args.emplace_back(std::move(arg_split.first));
1461  remaining = std::move(arg_split.second);
1462  }
1463  }
1464 
1465  (this->*(query.func))(qctx);
1466 
1467  return true;
1468 }
1469 
1472 {
1473  return {};
1474 };
1475 
1476 bool
1478  const std::string &annex, std::string &output)
1479 {
1480  return false;
1481 }
1482 
1483 void
1485  const std::string &unencoded, std::string &encoded) const
1486 {
1487  for (const char& c : unencoded) {
1488  if (c == '$' || c == '#' || c == '}' || c == '*') {
1489  encoded += '}';
1490  encoded += c ^ 0x20;
1491  } else {
1492  encoded += c;
1493  }
1494  }
1495 }
1496 
1497 void
1498 BaseRemoteGDB::encodeXferResponse(const std::string &unencoded,
1499  std::string &encoded, size_t offset, size_t unencoded_length) const
1500 {
1501  if (offset + unencoded_length < unencoded.length())
1502  encoded += 'm';
1503  else
1504  encoded += 'l';
1505  encodeBinaryData(unencoded.substr(offset, unencoded_length), encoded);
1506 }
1507 
1508 bool
1510 {
1511  send(tc->getProcessPtr()->pTable->externalize().c_str());
1512  return true;
1513 }
1514 
1515 bool
1517 {
1518  const char *p = ctx.data;
1519  hex2i(&p); // Ignore the subcommand byte.
1520  if (*p++ == ';') {
1521  Addr new_pc = hex2i(&p);
1522  tc->pcState(new_pc);
1523  }
1524  setSingleStep();
1525  return false;
1526 }
1527 
1528 bool
1530 {
1531  if (ctx.len) {
1532  const char *p = ctx.data;
1533  Addr new_pc = hex2i(&p);
1534  tc->pcState(new_pc);
1535  }
1536  setSingleStep();
1537  return false;
1538 }
1539 
1540 bool
1542 {
1543  const char *p = ctx.data;
1544  char sub_cmd = *p++;
1545  if (*p++ != ',')
1546  throw CmdError("E0D");
1547  Addr addr = hex2i(&p);
1548  if (*p++ != ',')
1549  throw CmdError("E0D");
1550  size_t kind = hex2i(&p);
1551 
1552  DPRINTF(GDBMisc, "clear %s, addr=%#x, kind=%d\n",
1553  breakType(sub_cmd), addr, kind);
1554 
1555  switch (sub_cmd) {
1556  case GdbSoftBp:
1557  removeSoftBreak(addr, kind);
1558  break;
1559  case GdbHardBp:
1560  removeHardBreak(addr, kind);
1561  break;
1562  case GdbWriteWp:
1563  case GdbReadWp:
1564  case GdbAccWp:
1565  default: // unknown
1566  throw Unsupported();
1567  }
1568  send("OK");
1569 
1570  return true;
1571 }
1572 
1573 bool
1575 {
1576  const char *p = ctx.data;
1577  char sub_cmd = *p++;
1578  if (*p++ != ',')
1579  throw CmdError("E0D");
1580  Addr addr = hex2i(&p);
1581  if (*p++ != ',')
1582  throw CmdError("E0D");
1583  size_t kind = hex2i(&p);
1584 
1585  DPRINTF(GDBMisc, "set %s, addr=%#x, kind=%d\n",
1586  breakType(sub_cmd), addr, kind);
1587 
1588  switch (sub_cmd) {
1589  case GdbSoftBp:
1590  insertSoftBreak(addr, kind);
1591  break;
1592  case GdbHardBp:
1593  insertHardBreak(addr, kind);
1594  break;
1595  case GdbWriteWp:
1596  case GdbReadWp:
1597  case GdbAccWp:
1598  default: // unknown
1599  throw Unsupported();
1600  }
1601  send("OK");
1602 
1603  return true;
1604 }
1605 
1606 } // namespace gem5
const char * warning
Definition: remote_gdb.cc:204
std::string error
Definition: remote_gdb.cc:212
#define DDUMP(x, data, count)
DPRINTF is a debugging trace facility that allows one to selectively enable tracing statements.
Definition: trace.hh:180
#define DPRINTFN(...)
Definition: trace.hh:214
#define DPRINTFNR(...)
Definition: trace.hh:221
#define DPRINTF(x,...)
Definition: trace.hh:186
const char data[]
void stopReason(std::string s)
Definition: remote_gdb.hh:293
void removeHardBreak(Addr addr, size_t kind)
Definition: remote_gdb.cc:935
std::map< ContextID, ThreadContext * > threads
Definition: remote_gdb.hh:272
void querySymbol(QuerySetCommand::Context &ctx)
Definition: remote_gdb.cc:1396
bool cmdQueryVar(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:1436
ThreadContext * tc
Definition: remote_gdb.hh:273
bool cmdCont(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:1090
virtual bool checkBpKind(size_t kind)
Definition: remote_gdb.cc:1068
EventWrapper< BaseRemoteGDB, &BaseRemoteGDB::connect > connectEvent
Definition: remote_gdb.hh:277
bool cmdDetach(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:1115
EventWrapper< BaseRemoteGDB, &BaseRemoteGDB::detach > disconnectEvent
Definition: remote_gdb.hh:278
bool cmdReplyEmpty(GdbMultiLetterCommand::Context &ctx)
Definition: remote_gdb.cc:1287
EventWrapper< BaseRemoteGDB, &BaseRemoteGDB::singleStep > singleStepEvent
Definition: remote_gdb.hh:311
static std::map< std::string, GdbMultiLetterCommand > multiLetterMap
Definition: remote_gdb.hh:381
void send(const char *data)
Definition: remote_gdb.cc:738
void incomingConnection(int revent)
Definition: remote_gdb.cc:595
IncomingConnectionEvent * incomingConnectionEvent
Definition: remote_gdb.hh:231
void queryXfer(QuerySetCommand::Context &ctx)
Definition: remote_gdb.cc:1356
void encodeBinaryData(const std::string &unencoded, std::string &encoded) const
Definition: remote_gdb.cc:1484
void insertSoftBreak(Addr addr, size_t kind)
Definition: remote_gdb.cc:902
static std::map< std::string, QuerySetCommand > queryMap
Definition: remote_gdb.hh:429
virtual bool writeBlob(Addr vaddr, size_t size, const char *data)
Definition: remote_gdb.cc:829
bool cmdMemW(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:1226
void processCommands(int signum=0)
Definition: remote_gdb.cc:769
IncomingDataEvent * incomingDataEvent
Definition: remote_gdb.hh:232
void putbyte(uint8_t b)
Definition: remote_gdb.cc:676
bool cmdSetThread(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:1146
bool cmdClrHwBkpt(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:1541
bool cmdDumpPageTable(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:1509
static std::map< char, GdbCommand > commandMap
Definition: remote_gdb.hh:357
bool trap(ContextID id, int type)
Definition: remote_gdb.hh:55
void incomingData(int revent)
Definition: remote_gdb.cc:608
bool try_getbyte(uint8_t *c, int timeout=-1)
Definition: remote_gdb.cc:632
void sendSPacket(int errnum)
Definition: remote_gdb.cc:964
bool cmdAsyncStep(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:1516
virtual std::vector< std::string > availableFeatures() const
Definition: remote_gdb.cc:1471
void queryFThreadInfo(QuerySetCommand::Context &ctx)
Definition: remote_gdb.cc:1416
SocketEvent<&BaseRemoteGDB::incomingData > IncomingDataEvent
Definition: remote_gdb.hh:226
bool cmdSetHwBkpt(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:1574
virtual ~BaseRemoteGDB()
Definition: remote_gdb.hh:57
bool cmdStep(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:1529
void recv(std::vector< char > &bp)
Definition: remote_gdb.cc:686
virtual bool getXferFeaturesRead(const std::string &annex, std::string &output)
Get an XML target description.
Definition: remote_gdb.cc:1477
bool cmdRegW(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:1132
void queryAttached(QuerySetCommand::Context &ctx)
Definition: remote_gdb.cc:1403
void encodeXferResponse(const std::string &unencoded, std::string &encoded, size_t offset, size_t unencoded_length) const
Definition: remote_gdb.cc:1498
bool cmdSignal(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:1083
bool cmdMemR(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:1202
void descheduleInstCommitEvent(Event *ev)
Deschedule an instruction count based event.
Definition: remote_gdb.cc:1000
bool read(Addr vaddr, size_t size, char *data)
Definition: remote_gdb.cc:841
void querySThreadInfo(QuerySetCommand::Context &ctx)
Definition: remote_gdb.cc:1423
SocketEvent<&BaseRemoteGDB::incomingConnection > IncomingConnectionEvent
Definition: remote_gdb.hh:224
bool cmdRegR(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:1122
bool cmdIsThreadAlive(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:1186
void scheduleInstCommitEvent(Event *ev, int delta, ThreadContext *_tc)
Schedule an event which will be triggered "delta" instructions later.
Definition: remote_gdb.cc:985
virtual bool readBlob(Addr vaddr, size_t size, char *data)
Definition: remote_gdb.cc:818
void queryC(QuerySetCommand::Context &ctx)
Definition: remote_gdb.cc:1337
virtual bool acc(Addr addr, size_t len)=0
gem5::BaseRemoteGDB::TrapEvent trapEvent
bool write(Addr vaddr, size_t size, const char *data)
Definition: remote_gdb.cc:866
bool cmdMultiUnsupported(GdbMultiLetterCommand::Context &ctx)
Definition: remote_gdb.cc:1302
void querySupported(QuerySetCommand::Context &ctx)
Definition: remote_gdb.cc:1343
bool cmdUnsupported(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:1074
void sendTPacket(int errnum, ContextID id, const std::string &stopReason)
Definition: remote_gdb.cc:954
friend class HardBreakpoint
Definition: remote_gdb.hh:145
bool cmdMultiLetter(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:1250
bool cmdVKill(GdbMultiLetterCommand::Context &ctx)
Definition: remote_gdb.cc:1294
BaseGdbRegCache * regCachePtr
Definition: remote_gdb.hh:275
bool cmdAsyncCont(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:1102
void sendOPacket(const std::string message)
Definition: remote_gdb.cc:968
void removeSoftBreak(Addr addr, size_t kind)
Definition: remote_gdb.cc:911
void replaceThreadContext(ThreadContext *tc)
Definition: remote_gdb.hh:54
void insertHardBreak(Addr addr, size_t kind)
Definition: remote_gdb.cc:920
ListenSocket listener
Definition: remote_gdb.hh:234
virtual BaseGdbRegCache * gdbRegs()=0
const std::string externalize() const
Dump all items in the pTable, to a concatenation of strings of the form Addr:Entry;.
Definition: page_table.cc:224
virtual void process()=0
const std::string name() const override
Definition: remote_gdb.cc:187
HardBreakpoint(BaseRemoteGDB *_gdb, PCEventScope *s, Addr pc)
Definition: remote_gdb.cc:180
void process(ThreadContext *tc) override
Definition: remote_gdb.cc:190
BaseRemoteGDB * gdb
Definition: remote_gdb.cc:174
virtual bool listen(int port, bool reuse=true)
Definition: socket.cc:122
virtual int accept(bool nodelay=false)
Definition: socket.cc:176
static bool allDisabled()
Definition: socket.cc:71
bool islistening() const
Definition: socket.hh:78
int getfd() const
Definition: socket.hh:77
virtual std::string name() const
Definition: named.hh:47
Addr pc() const
Definition: pc_event.hh:61
This object is a proxy for a port or other object which implements the functional response protocol,...
Definition: port_proxy.hh:87
void writeBlob(Addr addr, const void *p, int size) const
Same as tryWriteBlob, but insists on success.
Definition: port_proxy.hh:192
void readBlob(Addr addr, void *p, int size) const
Higher level interfaces based on the above.
Definition: port_proxy.hh:182
EmulationPageTable * pTable
Definition: process.hh:185
ThreadContext is the external interface to all thread state for anything outside of the CPU.
virtual void descheduleInstCountEvent(Event *event)=0
virtual const PCStateBase & pcState() const =0
virtual Tick getCurrentInstCount()=0
virtual void scheduleInstCountEvent(Event *event, Tick count)=0
virtual Status status() const =0
virtual ContextID contextId() const =0
virtual Process * getProcessPtr()=0
This proxy attempts to translate virtual addresses using the TLBs.
STL pair class.
Definition: stl.hh:58
bool scheduled() const
Determine if the current event is scheduled.
Definition: eventq.hh:465
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:178
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition: logging.hh:204
void remove(PollEvent *event)
Definition: pollevent.cc:139
PollQueue pollQueue
Definition: pollevent.cc:55
void schedule(PollEvent *event)
Definition: pollevent.cc:159
void attach(int fd)
Definition: remote_gdb.cc:460
bool sendMessage(std::string message)
Definition: remote_gdb.cc:585
virtual char * data() const =0
Return the pointer to the raw bytes buffer containing the register values.
virtual void getRegs(ThreadContext *)=0
Fill the raw buffer from the registers in the ThreadContext.
std::string name()
Definition: remote_gdb.cc:407
virtual void setRegs(ThreadContext *) const =0
Set the ThreadContext's registers from the values in the raw buffer.
virtual size_t size() const =0
Return the size of the raw buffer, in bytes (i.e., half of the number of digits in the g/G packet).
void addThreadContext(ThreadContext *_tc)
Definition: remote_gdb.cc:510
BaseRemoteGDB(System *system, int _port)
Interface to other parts of the simulator.
Definition: remote_gdb.cc:393
void scheduleTrapEvent(ContextID id, int type, int delta, std::string stopReason)
Definition: remote_gdb.cc:973
int port() const
Definition: remote_gdb.cc:452
bool selectThreadContext(ContextID id)
Definition: remote_gdb.cc:533
uint16_t len
Definition: helpers.cc:62
#define warn(...)
Definition: logging.hh:246
#define warn_once(...)
Definition: logging.hh:250
Port Object Declaration.
Bitfield< 14, 12 > fd
Definition: types.hh:150
Bitfield< 31 > n
Definition: misc_types.hh:462
Bitfield< 7 > b
Definition: misc_types.hh:388
Bitfield< 7 > i
Definition: misc_types.hh:67
Bitfield< 23, 0 > offset
Definition: types.hh:144
Bitfield< 9 > e
Definition: misc_types.hh:65
Bitfield< 33 > id
Definition: misc_types.hh:257
Bitfield< 5 > r
Definition: pagetable.hh:60
Bitfield< 1 > s
Definition: pagetable.hh:64
Bitfield< 2 > c
Definition: pagetable.hh:63
Bitfield< 56 > f
Definition: pagetable.hh:53
Bitfield< 54 > p
Definition: pagetable.hh:70
Bitfield< 3 > addr
Definition: types.hh:84
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Tick curTick()
The universal simulation clock.
Definition: cur_tick.hh:46
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:147
static const char GDBEnd
Definition: remote_gdb.cc:167
bool FullSystem
The FullSystem variable can be used to determine the current mode of simulation.
Definition: root.cc:220
static void output(const char *filename)
Definition: debug.cc:60
static const char GDBBadP
Definition: remote_gdb.cc:169
int ContextID
Globally unique thread context ID.
Definition: types.hh:239
static const char GDBGoodP
Definition: remote_gdb.cc:168
static const char GDBStart
Definition: remote_gdb.cc:166
void ccprintf(cp::Print &print)
Definition: cprintf.hh:130
PortProxy Object Declaration.

Generated on Wed Dec 21 2022 10:22:25 for gem5 by doxygen 1.9.1