Deleted Added
sdiff udiff text old ( 11793:ef606668d247 ) new ( 12019:65939e37768a )
full compact
1/*
2 * Copyright 2015 LabWare
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

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

120 */
121
122#include "base/remote_gdb.hh"
123
124#include <sys/signal.h>
125#include <unistd.h>
126
127#include <csignal>
128#include <cstdio>
129#include <string>
130
131#include "arch/vtophys.hh"
132#include "base/intmath.hh"
133#include "base/socket.hh"
134#include "base/trace.hh"
135#include "config/the_isa.hh"

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

363 default: return "KGDB_UNKNOWN";
364 }
365}
366
367/////////////////////////
368//
369//
370
371uint8_t
372BaseRemoteGDB::getbyte()
373{
374 uint8_t b;
375 if (::read(fd, &b, 1) != 1)
376 warn("could not read byte from debugger");
377 return b;
378}
379
380void
381BaseRemoteGDB::putbyte(uint8_t b)
382{
383 if (::write(fd, &b, 1) != 1)
384 warn("could not write byte to debugger");
385}
386
387// Send a packet to gdb
388void
389BaseRemoteGDB::send(const char *bp)
390{
391 const char *p;
392 uint8_t csum, c;
393
394 DPRINTF(GDBSend, "send: %s\n", bp);
395
396 do {
397 p = bp;
398 //Start sending a packet
399 putbyte(GDBStart);
400 //Send the contents, and also keep a check sum.
401 for (csum = 0; (c = *p); p++) {
402 putbyte(c);
403 csum += c;
404 }
405 //Send the ending character.
406 putbyte(GDBEnd);
407 //Sent the checksum.
408 putbyte(i2digit(csum >> 4));
409 putbyte(i2digit(csum));
410 //Try transmitting over and over again until the other end doesn't send an
411 //error back.
412 } while ((c = getbyte() & 0x7f) == GDBBadP);
413}
414
415// Receive a packet from gdb
416int
417BaseRemoteGDB::recv(char *bp, int maxlen)
418{
419 char *p;
420 int c, csum;
421 int len;
422
423 do {
424 p = bp;
425 csum = len = 0;
426 //Find the beginning of a packet
427 while ((c = getbyte()) != GDBStart)
428 ;
429
430 //Read until you find the end of the data in the packet, and keep
431 //track of the check sum.
432 while ((c = getbyte()) != GDBEnd && len < maxlen) {
433 c &= 0x7f;
434 csum += c;
435 *p++ = c;
436 len++;
437 }
438
439 //Mask the check sum, and terminate the command string.
440 csum &= 0xff;
441 *p = '\0';
442
443 //If the command was too long, report an error.
444 if (len >= maxlen) {
445 putbyte(GDBBadP);
446 continue;
447 }
448
449 //Bring in the checksum. If the check sum matches, csum will be 0.
450 csum -= digit2i(getbyte()) * 16;
451 csum -= digit2i(getbyte());
452
453 //If the check sum was correct
454 if (csum == 0) {
455 //Report that the packet was received correctly
456 putbyte(GDBGoodP);
457 // Sequence present?
458 if (bp[2] == ':') {
459 putbyte(bp[0]);
460 putbyte(bp[1]);
461 len -= 3;
462 memcpy(bp, bp+3, len);
463 }
464 break;
465 }
466 //Otherwise, report that there was a mistake.
467 putbyte(GDBBadP);
468 } while (1);
469
470 DPRINTF(GDBRecv, "recv: %s: %s\n", gdb_command(*bp), bp);
471
472 return (len);
473}
474
475// Read bytes from kernel address space for debugger.

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

693// makes sense to use POSIX errno values, because that is what the
694// gdb/remote.c functions want to return.
695bool
696BaseRemoteGDB::trap(int type)
697{
698 uint64_t val;
699 size_t datalen, len;
700 char data[GDBPacketBufLen + 1];
701 char *buffer;
702 size_t bufferSize;
703 const char *p;
704 char command, subcmd;
705 string var;
706 bool ret;
707
708 if (!attached)
709 return false;
710
711 unique_ptr<BaseRemoteGDB::BaseGdbRegCache> regCache(gdbRegs());
712
713 bufferSize = regCache->size() * 2 + 256;
714 buffer = (char*)malloc(bufferSize);
715
716 DPRINTF(GDBMisc, "trap: PC=%s\n", context->pcState());
717
718 clearSingleStep();
719
720 /*
721 * The first entry to this function is normally through
722 * a breakpoint trap in kgdb_connect(), in which case we

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

727 * After the debugger is "active" (connected) it will be
728 * waiting for a "signaled" message from us.
729 */
730 if (!active) {
731 active = true;
732 } else {
733 // Tell remote host that an exception has occurred.
734 snprintf(buffer, bufferSize, "S%02x", type);
735 send(buffer);
736 }
737
738 // Stick frame regs into our reg cache.
739 regCache->getRegs(context);
740
741 for (;;) {
742 datalen = recv(data, sizeof(data));
743 data[sizeof(data) - 1] = 0; // Sentinel
744 command = data[0];
745 subcmd = 0;
746 p = data + 1;
747 switch (command) {
748
749 case GDBSignal:
750 // if this command came from a running gdb, answer it --
751 // the other guy has no way of knowing if we're in or out
752 // of this loop when he issues a "remote-signal".
753 snprintf(buffer, bufferSize,
754 "S%02x", type);
755 send(buffer);
756 continue;
757
758 case GDBRegR:
759 if (2 * regCache->size() > bufferSize)
760 panic("buffer too small");
761
762 mem2hex(buffer, regCache->data(), regCache->size());
763 send(buffer);
764 continue;
765
766 case GDBRegW:
767 p = hex2mem(regCache->data(), p, regCache->size());
768 if (p == NULL || *p != '\0')
769 send("E01");
770 else {
771 regCache->setRegs(context);
772 send("OK");
773 }
774 continue;
775
776 case GDBMemR:
777 val = hex2i(&p);
778 if (*p++ != ',') {
779 send("E02");
780 continue;
781 }
782 len = hex2i(&p);
783 if (*p != '\0') {
784 send("E03");
785 continue;
786 }
787 if (len > bufferSize) {
788 send("E04");
789 continue;
790 }
791 if (!acc(val, len)) {
792 send("E05");
793 continue;
794 }
795
796 if (read(val, (size_t)len, buffer)) {
797 // variable length array would be nice, but C++ doesn't
798 // officially support those...
799 char *temp = new char[2*len+1];
800 mem2hex(temp, buffer, len);
801 send(temp);
802 delete [] temp;
803 } else {
804 send("E05");
805 }
806 continue;
807
808 case GDBMemW:
809 val = hex2i(&p);
810 if (*p++ != ',') {
811 send("E06");
812 continue;
813 }
814 len = hex2i(&p);
815 if (*p++ != ':') {
816 send("E07");
817 continue;
818 }
819 if (len > datalen - (p - data)) {
820 send("E08");
821 continue;
822 }
823 p = hex2mem(buffer, p, bufferSize);
824 if (p == NULL) {
825 send("E09");
826 continue;
827 }
828 if (!acc(val, len)) {
829 send("E0A");
830 continue;
831 }
832 if (write(val, (size_t)len, buffer))
833 send("OK");
834 else
835 send("E0B");
836 continue;
837
838 case GDBSetThread:
839 subcmd = *p++;
840 val = hex2i(&p);
841 if (val == 0)
842 send("OK");
843 else
844 send("E01");
845 continue;
846
847 case GDBDetach:
848 case GDBKill:
849 active = false;
850 clearSingleStep();
851 detach();
852 goto out;
853
854 case GDBAsyncCont:
855 subcmd = hex2i(&p);
856 if (*p++ == ';') {
857 val = hex2i(&p);
858 context->pcState(val);
859 }
860 clearSingleStep();
861 goto out;
862
863 case GDBCont:
864 if (p - data < (ptrdiff_t)datalen) {
865 val = hex2i(&p);
866 context->pcState(val);
867 }
868 clearSingleStep();
869 goto out;
870
871 case GDBAsyncStep:
872 subcmd = hex2i(&p);
873 if (*p++ == ';') {
874 val = hex2i(&p);
875 context->pcState(val);
876 }
877 setSingleStep();
878 goto out;
879
880 case GDBStep:
881 if (p - data < (ptrdiff_t)datalen) {
882 val = hex2i(&p);
883 context->pcState(val);
884 }
885 setSingleStep();
886 goto out;
887
888 case GDBClrHwBkpt:
889 subcmd = *p++;
890 if (*p++ != ',') send("E0D");
891 val = hex2i(&p);
892 if (*p++ != ',') send("E0D");
893 len = hex2i(&p);
894
895 DPRINTF(GDBMisc, "clear %s, addr=%#x, len=%d\n",
896 break_type(subcmd), val, len);
897
898 ret = false;
899
900 switch (subcmd) {

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

905 case '1': // hardware breakpoint
906 ret = removeHardBreak(val, len);
907 break;
908
909 case '2': // write watchpoint
910 case '3': // read watchpoint
911 case '4': // access watchpoint
912 default: // unknown
913 send("");
914 break;
915 }
916
917 send(ret ? "OK" : "E0C");
918 continue;
919
920 case GDBSetHwBkpt:
921 subcmd = *p++;
922 if (*p++ != ',') send("E0D");
923 val = hex2i(&p);
924 if (*p++ != ',') send("E0D");
925 len = hex2i(&p);
926
927 DPRINTF(GDBMisc, "set %s, addr=%#x, len=%d\n",
928 break_type(subcmd), val, len);
929
930 ret = false;
931
932 switch (subcmd) {

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

937 case '1': // hardware breakpoint
938 ret = insertHardBreak(val, len);
939 break;
940
941 case '2': // write watchpoint
942 case '3': // read watchpoint
943 case '4': // access watchpoint
944 default: // unknown
945 send("");
946 break;
947 }
948
949 send(ret ? "OK" : "E0C");
950 continue;
951
952 case GDBQueryVar:
953 var = string(p, datalen - 1);
954 if (var == "C")
955 send("QC0");
956 else
957 send("");
958 continue;
959
960 case GDBSetBaud:
961 case GDBSetBreak:
962 case GDBDebug:
963 case GDBCycleStep:
964 case GDBSigCycleStep:
965 case GDBReadReg:
966 case GDBSetVar:
967 case GDBReset:
968 case GDBThreadAlive:
969 case GDBTargetExit:
970 case GDBBinaryDload:
971 // Unsupported command
972 DPRINTF(GDBMisc, "Unsupported command: %s\n",
973 gdb_command(command));
974 DDUMP(GDBMisc, (uint8_t *)data, datalen);
975 send("");
976 continue;
977
978 default:
979 // Unknown command.
980 DPRINTF(GDBMisc, "Unknown command: %c(%#x)\n",
981 command, command);
982 send("");
983 continue;
984
985
986 }
987 }
988
989 out:
990 free(buffer);
991 return true;
992}
993
994// Convert a hex digit into an integer.
995// This returns -1 if the argument passed is no valid hex digit.
996int
997BaseRemoteGDB::digit2i(char c)
998{

--- 73 unchanged lines hidden ---