remote_gdb.cc (10601:6efb37480d87) remote_gdb.cc (11274:d9a0136ab8cc)
1/*
1/*
2 * Copyright 2015 LabWare
2 * Copyright 2014 Google, Inc.
3 * Copyright (c) 2002-2005 The Regents of The University of Michigan
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;

--- 12 unchanged lines hidden (view full) ---

22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Authors: Nathan Binkert
3 * Copyright 2014 Google, Inc.
4 * Copyright (c) 2002-2005 The Regents of The University of Michigan
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met: redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer;

--- 12 unchanged lines hidden (view full) ---

23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * Authors: Nathan Binkert
31 * Boris Shingarov
30 */
31
32/*
33 * Copyright (c) 1990, 1993 The Regents of the University of California
34 * All rights reserved
35 *
36 * This software was developed by the Computer Systems Engineering group
37 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and

--- 227 unchanged lines hidden (view full) ---

265void
266BaseRemoteGDB::SingleStepEvent::process()
267{
268 if (!gdb->singleStepEvent.scheduled())
269 gdb->scheduleInstCommitEvent(&gdb->singleStepEvent, 1);
270 gdb->trap(SIGTRAP);
271}
272
32 */
33
34/*
35 * Copyright (c) 1990, 1993 The Regents of the University of California
36 * All rights reserved
37 *
38 * This software was developed by the Computer Systems Engineering group
39 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and

--- 227 unchanged lines hidden (view full) ---

267void
268BaseRemoteGDB::SingleStepEvent::process()
269{
270 if (!gdb->singleStepEvent.scheduled())
271 gdb->scheduleInstCommitEvent(&gdb->singleStepEvent, 1);
272 gdb->trap(SIGTRAP);
273}
274
273BaseRemoteGDB::BaseRemoteGDB(System *_system, ThreadContext *c,
274 size_t cacheSize) : inputEvent(NULL), trapEvent(this), listener(NULL),
275BaseRemoteGDB::BaseRemoteGDB(System *_system, ThreadContext *c) :
276 inputEvent(NULL), trapEvent(this), listener(NULL),
275 number(-1), fd(-1), active(false), attached(false), system(_system),
277 number(-1), fd(-1), active(false), attached(false), system(_system),
276 context(c), gdbregs(cacheSize), singleStepEvent(this)
278 context(c), singleStepEvent(this)
277{
279{
278 memset(gdbregs.regs, 0, gdbregs.bytes());
279}
280
281BaseRemoteGDB::~BaseRemoteGDB()
282{
283 if (inputEvent)
284 delete inputEvent;
285}
286

--- 408 unchanged lines hidden (view full) ---

695 const char *p;
696 char command, subcmd;
697 string var;
698 bool ret;
699
700 if (!attached)
701 return false;
702
280}
281
282BaseRemoteGDB::~BaseRemoteGDB()
283{
284 if (inputEvent)
285 delete inputEvent;
286}
287

--- 408 unchanged lines hidden (view full) ---

696 const char *p;
697 char command, subcmd;
698 string var;
699 bool ret;
700
701 if (!attached)
702 return false;
703
703 bufferSize = gdbregs.bytes() * 2 + 256;
704 unique_ptr<BaseRemoteGDB::BaseGdbRegCache> regCache(gdbRegs());
705
706 bufferSize = regCache->size() * 2 + 256;
704 buffer = (char*)malloc(bufferSize);
705
706 DPRINTF(GDBMisc, "trap: PC=%s\n", context->pcState());
707
708 clearSingleStep();
709
710 /*
711 * The first entry to this function is normally through

--- 4 unchanged lines hidden (view full) ---

716 * listening to us, so just enter the interaction loop.
717 * After the debugger is "active" (connected) it will be
718 * waiting for a "signaled" message from us.
719 */
720 if (!active) {
721 active = true;
722 } else {
723 // Tell remote host that an exception has occurred.
707 buffer = (char*)malloc(bufferSize);
708
709 DPRINTF(GDBMisc, "trap: PC=%s\n", context->pcState());
710
711 clearSingleStep();
712
713 /*
714 * The first entry to this function is normally through

--- 4 unchanged lines hidden (view full) ---

719 * listening to us, so just enter the interaction loop.
720 * After the debugger is "active" (connected) it will be
721 * waiting for a "signaled" message from us.
722 */
723 if (!active) {
724 active = true;
725 } else {
726 // Tell remote host that an exception has occurred.
724 snprintf((char *)buffer, bufferSize, "S%02x", type);
727 snprintf(buffer, bufferSize, "S%02x", type);
725 send(buffer);
726 }
727
728 // Stick frame regs into our reg cache.
728 send(buffer);
729 }
730
731 // Stick frame regs into our reg cache.
729 getregs();
732 regCache->getRegs(context);
730
731 for (;;) {
732 datalen = recv(data, sizeof(data));
733 data[sizeof(data) - 1] = 0; // Sentinel
734 command = data[0];
735 subcmd = 0;
736 p = data + 1;
737 switch (command) {
738
739 case GDBSignal:
740 // if this command came from a running gdb, answer it --
741 // the other guy has no way of knowing if we're in or out
742 // of this loop when he issues a "remote-signal".
733
734 for (;;) {
735 datalen = recv(data, sizeof(data));
736 data[sizeof(data) - 1] = 0; // Sentinel
737 command = data[0];
738 subcmd = 0;
739 p = data + 1;
740 switch (command) {
741
742 case GDBSignal:
743 // if this command came from a running gdb, answer it --
744 // the other guy has no way of knowing if we're in or out
745 // of this loop when he issues a "remote-signal".
743 snprintf((char *)buffer, bufferSize,
746 snprintf(buffer, bufferSize,
744 "S%02x", type);
745 send(buffer);
746 continue;
747
748 case GDBRegR:
747 "S%02x", type);
748 send(buffer);
749 continue;
750
751 case GDBRegR:
749 if (2 * gdbregs.bytes() > bufferSize)
752 if (2 * regCache->size() > bufferSize)
750 panic("buffer too small");
751
753 panic("buffer too small");
754
752 mem2hex(buffer, gdbregs.regs, gdbregs.bytes());
755 mem2hex(buffer, regCache->data(), regCache->size());
753 send(buffer);
754 continue;
755
756 case GDBRegW:
756 send(buffer);
757 continue;
758
759 case GDBRegW:
757 p = hex2mem(gdbregs.regs, p, gdbregs.bytes());
760 p = hex2mem(regCache->data(), p, regCache->size());
758 if (p == NULL || *p != '\0')
759 send("E01");
760 else {
761 if (p == NULL || *p != '\0')
762 send("E01");
763 else {
761 setregs();
764 regCache->setRegs(context);
762 send("OK");
763 }
764 continue;
765
765 send("OK");
766 }
767 continue;
768
766#if 0
767 case GDBSetReg:
768 val = hex2i(&p);
769 if (*p++ != '=') {
770 send("E01");
771 continue;
772 }
773 if (val < 0 && val >= KGDB_NUMREGS) {
774 send("E01");
775 continue;
776 }
777
778 gdbregs.regs[val] = hex2i(&p);
779 setregs();
780 send("OK");
781
782 continue;
783#endif
784
785 case GDBMemR:
786 val = hex2i(&p);
787 if (*p++ != ',') {
788 send("E02");
789 continue;
790 }
791 len = hex2i(&p);
792 if (*p != '\0') {

--- 4 unchanged lines hidden (view full) ---

797 send("E04");
798 continue;
799 }
800 if (!acc(val, len)) {
801 send("E05");
802 continue;
803 }
804
769 case GDBMemR:
770 val = hex2i(&p);
771 if (*p++ != ',') {
772 send("E02");
773 continue;
774 }
775 len = hex2i(&p);
776 if (*p != '\0') {

--- 4 unchanged lines hidden (view full) ---

781 send("E04");
782 continue;
783 }
784 if (!acc(val, len)) {
785 send("E05");
786 continue;
787 }
788
805 if (read(val, (size_t)len, (char *)buffer)) {
789 if (read(val, (size_t)len, buffer)) {
806 // variable length array would be nice, but C++ doesn't
807 // officially support those...
808 char *temp = new char[2*len+1];
809 mem2hex(temp, buffer, len);
810 send(temp);
811 delete [] temp;
812 } else {
813 send("E05");

--- 19 unchanged lines hidden (view full) ---

833 if (p == NULL) {
834 send("E09");
835 continue;
836 }
837 if (!acc(val, len)) {
838 send("E0A");
839 continue;
840 }
790 // variable length array would be nice, but C++ doesn't
791 // officially support those...
792 char *temp = new char[2*len+1];
793 mem2hex(temp, buffer, len);
794 send(temp);
795 delete [] temp;
796 } else {
797 send("E05");

--- 19 unchanged lines hidden (view full) ---

817 if (p == NULL) {
818 send("E09");
819 continue;
820 }
821 if (!acc(val, len)) {
822 send("E0A");
823 continue;
824 }
841 if (write(val, (size_t)len, (char *)buffer))
825 if (write(val, (size_t)len, buffer))
842 send("OK");
843 else
844 send("E0B");
845 continue;
846
847 case GDBSetThread:
848 subcmd = *p++;
849 val = hex2i(&p);

--- 170 unchanged lines hidden (view full) ---

1020char
1021BaseRemoteGDB::i2digit(int n)
1022{
1023 return ("0123456789abcdef"[n & 0x0f]);
1024}
1025
1026// Convert a byte array into an hex string.
1027void
826 send("OK");
827 else
828 send("E0B");
829 continue;
830
831 case GDBSetThread:
832 subcmd = *p++;
833 val = hex2i(&p);

--- 170 unchanged lines hidden (view full) ---

1004char
1005BaseRemoteGDB::i2digit(int n)
1006{
1007 return ("0123456789abcdef"[n & 0x0f]);
1008}
1009
1010// Convert a byte array into an hex string.
1011void
1028BaseRemoteGDB::mem2hex(void *vdst, const void *vsrc, int len)
1012BaseRemoteGDB::mem2hex(char *vdst, const char *vsrc, int len)
1029{
1013{
1030 char *dst = (char *)vdst;
1031 const char *src = (const char *)vsrc;
1014 char *dst = vdst;
1015 const char *src = vsrc;
1032
1033 while (len--) {
1034 *dst++ = i2digit(*src >> 4);
1035 *dst++ = i2digit(*src++);
1036 }
1037 *dst = '\0';
1038}
1039
1040// Convert an hex string into a byte array.
1041// This returns a pointer to the character following the last valid
1042// hex digit. If the string ends in the middle of a byte, NULL is
1043// returned.
1044const char *
1016
1017 while (len--) {
1018 *dst++ = i2digit(*src >> 4);
1019 *dst++ = i2digit(*src++);
1020 }
1021 *dst = '\0';
1022}
1023
1024// Convert an hex string into a byte array.
1025// This returns a pointer to the character following the last valid
1026// hex digit. If the string ends in the middle of a byte, NULL is
1027// returned.
1028const char *
1045BaseRemoteGDB::hex2mem(void *vdst, const char *src, int maxlen)
1029BaseRemoteGDB::hex2mem(char *vdst, const char *src, int maxlen)
1046{
1030{
1047 char *dst = (char *)vdst;
1031 char *dst = vdst;
1048 int msb, lsb;
1049
1050 while (*src && maxlen--) {
1051 msb = digit2i(*src++);
1052 if (msb < 0)
1053 return (src - 1);
1054 lsb = digit2i(*src++);
1055 if (lsb < 0)

--- 25 unchanged lines hidden ---
1032 int msb, lsb;
1033
1034 while (*src && maxlen--) {
1035 msb = digit2i(*src++);
1036 if (msb < 0)
1037 return (src - 1);
1038 lsb = digit2i(*src++);
1039 if (lsb < 0)

--- 25 unchanged lines hidden ---