00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <iostream>
00027 using namespace std;
00028
00029 #include <assert.h>
00030 #include <stdio.h>
00031 #include <string.h>
00032 #include <errno.h>
00033 #ifndef _MSC_VER
00034 #include <unistd.h>
00035 #endif
00036 #include <fcntl.h>
00037 #include <time.h>
00038 #include <signal.h>
00039
00040 #include "avrmalloc.h"
00041 #include "avrerror.h"
00042 #include "types.h"
00043 #include "global.h"
00044 #include "breakpoint.h"
00045 #include "systemclock.h"
00046
00047
00048 #include "avrdevice.h"
00049 #include "avrdevice_impl.h"
00050 #include "gdb.h"
00051
00052 #ifdef _MSC_VER
00053 # define snprintf _snprintf
00054 #endif
00055
00056 #ifndef DOXYGEN
00057 enum {
00058 MAX_READ_RETRY = 50,
00059
00060 MEM_SPACE_MASK = 0x00ff0000,
00061 FLASH_OFFSET = 0x00000000,
00062 SRAM_OFFSET = 0x00800000,
00063 EEPROM_OFFSET = 0x00810000,
00064 SIGNATURE_OFFSET = 0x00840000,
00065
00066 GDB_BLOCKING_OFF = 0,
00067 GDB_BLOCKING_ON = 1,
00068
00069 GDB_RET_NOTHING_RECEIVED = -5,
00070 GDB_RET_SINGLE_STEP = -4,
00071 GDB_RET_CONTINUE = -3,
00072 GDB_RET_CTRL_C = -2,
00073 GDB_RET_KILL_REQUEST = -1,
00074 GDB_RET_OK = 0
00075
00076 };
00077 #endif
00078
00079 #if defined(HAVE_SYS_MINGW) || defined(_MSC_VER)
00080
00081 int GdbServerSocketMingW::socketCount = 0;
00082
00083 void GdbServerSocketMingW::Start() {
00084 if(socketCount == 0) {
00085 WSADATA info;
00086 if(WSAStartup(MAKEWORD(2, 2), &info))
00087 avr_error("Could not start WSA");
00088 }
00089 socketCount++;
00090 }
00091
00092 void GdbServerSocketMingW::End() {
00093 WSACleanup();
00094 }
00095
00096 GdbServerSocketMingW::GdbServerSocketMingW(int port): _socket(0), _conn(0) {
00097 sockaddr_in sa;
00098
00099 Start();
00100 _socket = socket(AF_INET, SOCK_STREAM, 0);
00101 if(_socket == INVALID_SOCKET)
00102 avr_error("Couldn't create socket: INVALID_SOCKET");
00103
00104 u_long arg = 1;
00105 ioctlsocket(_socket, FIONBIO, &arg);
00106
00107 memset(&sa, 0, sizeof(sa));
00108 sa.sin_family = PF_INET;
00109 sa.sin_port = htons(port);
00110 if(bind(_socket, (sockaddr *)&sa, sizeof(sockaddr_in)) == SOCKET_ERROR) {
00111 closesocket(_socket);
00112 avr_error("Couldn't bind socket: INVALID_SOCKET");
00113 }
00114
00115 listen(_socket, 1);
00116 }
00117
00118 GdbServerSocketMingW::~GdbServerSocketMingW() {
00119 Close();
00120 socketCount--;
00121 if(socketCount == 0)
00122 End();
00123 }
00124
00125 void GdbServerSocketMingW::Close(void) {
00126 CloseConnection();
00127 closesocket(_socket);
00128 }
00129
00130 int GdbServerSocketMingW::ReadByte(void) {
00131 char buf[1];
00132 int rv = recv(_conn, buf, 1, 0);
00133 if(rv <= 0)
00134 return -1;
00135 return buf[0];
00136 }
00137
00138 void GdbServerSocketMingW::Write(const void* buf, size_t count) {
00139 send(_conn, (const char *)buf, count, 0);
00140 }
00141
00142 void GdbServerSocketMingW::SetBlockingMode(int mode) {
00143 u_long arg = 1;
00144 if(mode)
00145 arg = 0;
00146 int res = ioctlsocket(_conn, FIONBIO, &arg);
00147 if(res)
00148 avr_warning( "fcntl failed: %d\n", WSAGetLastError() );
00149 }
00150
00151 bool GdbServerSocketMingW::Connect(void) {
00152 _conn = accept(_socket, 0, 0);
00153 if(_conn == INVALID_SOCKET) {
00154 int rc = WSAGetLastError();
00155 if(rc == WSAEWOULDBLOCK)
00156 return false;
00157 else
00158 avr_error("Couldn't connect: INVALID_SOCKET");
00159 }
00160 return true;
00161 }
00162
00163 void GdbServerSocketMingW::CloseConnection(void) {
00164 closesocket(_conn);
00165 }
00166
00167 #else
00168
00169 GdbServerSocketUnix::GdbServerSocketUnix(int port) {
00170 conn = -1;
00171
00172 if((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0)
00173 avr_error("Can't create socket: %s", strerror(errno));
00174
00175
00176
00177
00178 int i = 1;
00179 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
00180 fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
00181
00182 address->sin_family = AF_INET;
00183 address->sin_port = htons(port);
00184 memset(&address->sin_addr, 0, sizeof(address->sin_addr));
00185
00186 if(bind(sock, (struct sockaddr *)address, sizeof(address)))
00187 avr_error("Can not bind socket: %s", strerror(errno));
00188
00189 if(listen(sock, 1) < 0)
00190 avr_error("Can not listen on socket: %s", strerror(errno));
00191 }
00192
00193 GdbServerSocketUnix::~GdbServerSocketUnix() {
00194
00195 }
00196
00197 void GdbServerSocketUnix::Close(void) {
00198 CloseConnection();
00199 close(sock);
00200 }
00201
00202 int GdbServerSocketUnix::ReadByte(void) {
00203 char c;
00204 int res;
00205 int cnt = MAX_READ_RETRY;
00206
00207 while(cnt--) {
00208 res = read(conn, &c, 1);
00209 if(res < 0) {
00210 if (errno == EAGAIN)
00211
00212 return -1;
00213
00214 avr_error("read failed: %s", strerror(errno));
00215 }
00216
00217 if (res == 0) {
00218 usleep(1000);
00219 avr_warning("incomplete read\n");
00220 continue;
00221 }
00222 return c;
00223 }
00224 avr_error("Maximum read reties reached");
00225
00226 return 0;
00227 }
00228
00229 void GdbServerSocketUnix::Write(const void* buf, size_t count) {
00230 int res;
00231
00232 res = write(conn, buf, count);
00233
00234
00235 if(res < 0)
00236 avr_error("write failed: %s", strerror(errno));
00237
00238
00239
00240 if((unsigned int)res != count)
00241 avr_error("write only wrote %d of %d bytes", res, count);
00242 }
00243
00244 void GdbServerSocketUnix::SetBlockingMode(int mode) {
00245 if(mode) {
00246
00247 if(fcntl(conn, F_SETFL, fcntl(conn, F_GETFL, 0) & ~O_NONBLOCK) < 0)
00248 avr_warning("fcntl failed: %s\n", strerror(errno));
00249 } else {
00250
00251 if(fcntl(conn, F_SETFL, fcntl(conn, F_GETFL, 0) | O_NONBLOCK) < 0)
00252 avr_warning("fcntl failed: %s\n", strerror(errno));
00253 }
00254 }
00255
00256 bool GdbServerSocketUnix::Connect(void) {
00257
00258 socklen_t addrLength = sizeof(struct sockaddr_in);
00259
00260
00261
00262 conn = accept(sock, (struct sockaddr *)address, &addrLength);
00263 if(conn > 0) {
00264
00265
00266
00267
00268
00269
00270 int i = 1;
00271 setsockopt (conn, IPPROTO_TCP, TCP_NODELAY, &i, sizeof (i));
00272
00273
00274
00275 fprintf(stderr, "Connection opened by host %s, port %hd.\n",
00276 inet_ntoa(address->sin_addr), ntohs(address->sin_port));
00277
00278 return true;
00279 } else
00280 return false;
00281 }
00282
00283 void GdbServerSocketUnix::CloseConnection(void) {
00284 close(conn);
00285 conn = -1;
00286 }
00287
00288 #endif
00289
00290 GdbServer::GdbServer(AvrDevice *c, int _port, int debug, int _waitForGdbConnection):
00291 core(c),
00292 global_debug_on(debug),
00293 waitForGdbConnection(_waitForGdbConnection),
00294 exitOnKillRequest(false)
00295 {
00296 last_reply = NULL;
00297 runMode = GDB_RET_NOTHING_RECEIVED;
00298 lastCoreStepFinished = true;
00299 connState = false;
00300 m_gdb_thread_id = 1;
00301
00302 #if defined(HAVE_SYS_MINGW) || defined(_MSC_VER)
00303 server = new GdbServerSocketMingW(_port);
00304 #else
00305 server = new GdbServerSocketUnix(_port);
00306 #endif
00307
00308 fprintf(stderr, "Waiting on port %d for gdb client to connect...\n", _port);
00309
00310 }
00311
00312
00313 vector<GdbServer*> GdbServer::allGdbServers;
00314
00315 GdbServer::~GdbServer() {
00316 server->Close();
00317 avr_free(last_reply);
00318 delete server;
00319 }
00320
00321 word GdbServer::avr_core_flash_read(int addr) {
00322 assert(0 <= addr && (unsigned) addr+1 < core->Flash->GetSize());
00323 return core->Flash->ReadMemRawWord(addr);
00324 }
00325
00326 void GdbServer::avr_core_flash_write(int addr, word val) {
00327 if((addr + 1) >= (int)core->Flash->GetSize())
00328 avr_error("try to write in flash after last valid address!");
00329 core->Flash->WriteMemByte(val & 0xff, addr + 1);
00330 core->Flash->WriteMemByte((val >> 8) & 0xff, addr);
00331 core->Flash->Decode(addr);
00332 }
00333
00334 void GdbServer::avr_core_flash_write_hi8(int addr, byte val) {
00335 if(addr >= (int)core->Flash->GetSize())
00336 avr_error("try to write in flash after last valid address! (hi8)");
00337 core->Flash->WriteMemByte(val, addr);
00338 core->Flash->Decode();
00339 }
00340
00341 void GdbServer::avr_core_flash_write_lo8(int addr, byte val) {
00342 if(addr + 1 >= (int)core->Flash->GetSize())
00343 avr_error("try to write in flash after last valid address! (lo8)");
00344 core->Flash->WriteMemByte(val, addr + 1);
00345 core->Flash->Decode();
00346 }
00347
00348 void GdbServer::avr_core_remove_breakpoint(dword pc) {
00349 Breakpoints::iterator ii;
00350 if ((ii= find(core->BP.begin(), core->BP.end(), pc)) != core->BP.end()) core->BP.erase(ii);
00351 }
00352
00353 void GdbServer::avr_core_insert_breakpoint(dword pc) {
00354 core->BP.push_back(pc);
00355 }
00356
00357 int GdbServer::signal_has_occurred(int signo) {return 0;}
00358 void GdbServer::signal_watch_start(int signo){};
00359 void GdbServer::signal_watch_stop(int signo){};
00360
00361 static char HEX_DIGIT[] = "0123456789abcdef";
00362
00364 int GdbServer::hex2nib( char hex )
00365 {
00366 if ( (hex >= 'A') && (hex <= 'F') )
00367 return (10 + (hex - 'A'));
00368
00369 else if ( (hex >= 'a') && (hex <= 'f') )
00370 return (10 + (hex - 'a'));
00371
00372 else if ( (hex >= '0') && (hex <= '9') )
00373 return (hex - '0');
00374
00375
00376 avr_error( "Invalid hexidecimal digit: 0x%02x", hex );
00377
00378 return 0;
00379 }
00380
00384 const char* GdbServer::gdb_last_reply( const char *reply )
00385 {
00386
00387 if (reply == NULL)
00388 {
00389 if (last_reply == NULL)
00390 return "";
00391 else
00392 return last_reply;
00393 }
00394
00395 if(last_reply == reply)
00396 return reply;
00397 avr_free( last_reply );
00398 last_reply = avr_strdup( reply );
00399
00400 return last_reply;
00401 }
00402
00404 void GdbServer::gdb_send_ack( )
00405 {
00406 if (global_debug_on)
00407 fprintf( stderr, " Ack -> gdb\n");
00408
00409 server->Write( "+", 1 );
00410 }
00411
00413 void GdbServer::gdb_send_reply( const char *reply )
00414 {
00415 int cksum = 0;
00416 int bytes;
00417
00418
00419 gdb_last_reply( reply );
00420
00421 if (global_debug_on)
00422 fprintf( stderr, "Sent: $%s#", reply );
00423
00424 if (*reply == '\0')
00425 {
00426 server->Write( "$#00", 4 );
00427
00428 if (global_debug_on)
00429 fprintf( stderr, "%02x\n", cksum & 0xff );
00430 }
00431 else
00432 {
00433 memset( buf, '\0', sizeof(buf) );
00434
00435 buf[0] = '$';
00436 bytes = 1;
00437
00438 while (*reply)
00439 {
00440 cksum += (unsigned char)*reply;
00441 buf[bytes] = *reply;
00442 bytes++;
00443 reply++;
00444
00445
00446 if (bytes == (sizeof(buf) - 3))
00447 {
00448
00449 avr_error( "buffer overflow" );
00450 }
00451 }
00452
00453 if (global_debug_on)
00454 fprintf( stderr, "%02x\n", cksum & 0xff );
00455
00456 buf[bytes++] = '#';
00457 buf[bytes++] = HEX_DIGIT[(cksum >> 4) & 0xf];
00458 buf[bytes++] = HEX_DIGIT[cksum & 0xf];
00459
00460 server->Write( buf, bytes );
00461 }
00462 }
00463
00464 void GdbServer::gdb_send_hex_reply(const char *reply, const char *reply_to_encode)
00465 {
00466 std::string result = reply;
00467 for(int i = 0; reply_to_encode[i] != '\0'; i++) {
00468 byte val = reply_to_encode[i];
00469 result += HEX_DIGIT[(val >> 4) & 0xf];
00470 result += HEX_DIGIT[val & 0xf];
00471 }
00472
00473 gdb_send_reply(result.c_str());
00474 }
00475
00481 void GdbServer::gdb_read_registers( )
00482 {
00483 bool current = core->stack->m_ThreadList.GetCurrentThreadForGDB() == m_gdb_thread_id;
00484 const Thread* nonrunning = core->stack->m_ThreadList.GetThreadFromGDB(m_gdb_thread_id);
00485 assert(current || nonrunning->m_sp != 0x0000);
00486
00487 int i;
00488 dword val;
00489
00490
00491 size_t buf_sz = (32 + 1 + 2 + 4)*2 + 1;
00492 char *buf;
00493
00494 buf = avr_new0( char, buf_sz );
00495
00496
00497 for ( i=0; i<32; i++ )
00498 {
00499 val = current ? core->GetCoreReg(i) : nonrunning->registers[i];
00500 buf[i*2] = HEX_DIGIT[(val >> 4) & 0xf];
00501 buf[i*2+1] = HEX_DIGIT[val & 0xf];
00502 }
00503
00504
00505 val = *(core->status);
00506 buf[i*2] = HEX_DIGIT[(val >> 4) & 0xf];
00507 buf[i*2+1] = HEX_DIGIT[val & 0xf];
00508 i++;
00509
00510
00511 val = current ? core->stack->GetStackPointer() : nonrunning->m_sp;
00512 buf[i*2] = HEX_DIGIT[(val >> 4) & 0xf];
00513 buf[i*2+1] = HEX_DIGIT[val & 0xf];
00514 i++;
00515 val>>=8;
00516 buf[i*2] = HEX_DIGIT[(val >> 4) & 0xf];
00517 buf[i*2+1] = HEX_DIGIT[val & 0xf];
00518 i++;
00519
00520
00521
00522
00523
00524 val = current ? core->PC * 2 : nonrunning->m_ip;
00525 buf[i*2] = HEX_DIGIT[(val >> 4) & 0xf];
00526 buf[i*2+1] = HEX_DIGIT[val & 0xf];
00527
00528 val >>= 8;
00529 buf[i*2+2] = HEX_DIGIT[(val >> 4) & 0xf];
00530 buf[i*2+3] = HEX_DIGIT[val & 0xf];
00531
00532 val >>= 8;
00533 buf[i*2+4] = HEX_DIGIT[(val >> 4) & 0xf];
00534 buf[i*2+5] = HEX_DIGIT[val & 0xf];
00535
00536 val >>= 8;
00537 buf[i*2+6] = HEX_DIGIT[(val >> 4) & 0xf];
00538 buf[i*2+7] = HEX_DIGIT[val & 0xf];
00539
00540 gdb_send_reply( buf );
00541 avr_free( buf );
00542 }
00543
00546 void GdbServer::gdb_write_registers(const char *pkt) {
00547 int i;
00548 byte bval;
00549 dword val;
00550
00551
00552 for ( i=0; i<32; i++ )
00553 {
00554 bval = hex2nib(*pkt++) << 4;
00555 bval += hex2nib(*pkt++);
00556 core->SetCoreReg(i, bval);
00557
00558 }
00559
00560
00561 bval = hex2nib(*pkt++) << 4;
00562 bval += hex2nib(*pkt++);
00563 *(core->status)=bval;
00564
00565
00566 bval = hex2nib(*pkt++) << 4;
00567 bval += hex2nib(*pkt++);
00568 val = hex2nib(*pkt++) << 4;
00569 val += hex2nib(*pkt++);
00570 val <<= 8;
00571 val += bval;
00572 core->stack->SetStackPointer(val);
00573
00574
00575
00576
00577
00578
00579
00580 val = ((dword)hex2nib(*pkt++)) << 4;
00581 val += ((dword)hex2nib(*pkt++));
00582
00583 val += ((dword)hex2nib(*pkt++)) << 12;
00584 val += ((dword)hex2nib(*pkt++)) << 8;
00585
00586 val += ((dword)hex2nib(*pkt++)) << 20;
00587 val += ((dword)hex2nib(*pkt++)) << 16;
00588
00589 val += ((dword)hex2nib(*pkt++)) << 28;
00590 val += ((dword)hex2nib(*pkt++)) << 24;
00591 core->PC=val/2;
00592
00593 gdb_send_reply( "OK" );
00594 }
00595
00603 int GdbServer::gdb_extract_hex_num(const char **pkt, char stop) {
00604 int i = 0;
00605 int num = 0;
00606 const char *p = *pkt;
00607 int max_shifts = sizeof(int)*2-1;
00608
00609 while ( (*p != stop) && (*p != '\0') )
00610 {
00611 if (i > max_shifts)
00612 avr_error( "number too large" );
00613
00614 num = (num << 4) | hex2nib(*p);
00615 i++;
00616 p++;
00617 }
00618
00619 *pkt = p;
00620 return num;
00621 }
00622
00625 void GdbServer::gdb_read_register(const char *pkt) {
00626 int reg;
00627
00628 char reply[MAX_BUF + 1];
00629
00630 memset(reply, '\0', sizeof(reply));
00631
00632 reg = gdb_extract_hex_num(&pkt, '\0');
00633
00634 if ( (reg >= 0) && (reg < 32) )
00635 {
00636 byte val = core->GetCoreReg(reg);
00637 snprintf(reply, sizeof(reply), "%02x", val);
00638 }
00639 else if (reg == 32)
00640 {
00641 byte val = *(core->status);
00642 snprintf(reply, sizeof(reply), "%02x", val);
00643 }
00644 else if (reg == 33)
00645 {
00646 byte spl, sph;
00647 unsigned long sp = core->stack->GetStackPointer();
00648 spl = sp & 0xff;
00649 sph = (sp >> 8) & 0xff;
00650 snprintf(reply, sizeof(reply), "%02x%02x", spl, sph);
00651 }
00652 else if (reg == 34)
00653 {
00654 dword val = core->PC * 2;
00655 snprintf(reply, sizeof(reply),
00656 "%02x%02x" "%02x%02x",
00657 val & 0xff, (val >> 8) & 0xff,
00658 (val >> 16) & 0xff, (val >> 24) & 0xff );
00659 }
00660 else
00661 {
00662 avr_warning( "Bad register value: %d\n", reg );
00663 gdb_send_reply( "E00" );
00664 return;
00665 }
00666 gdb_send_reply( reply );
00667 }
00668
00672 void GdbServer::gdb_write_register(const char *pkt) {
00673 int reg;
00674 int val, hval;
00675 dword dval;
00676
00677 reg = gdb_extract_hex_num(&pkt, '=');
00678 pkt++;
00679
00680
00681 val = hex2nib(*pkt++) << 4;
00682 val += hex2nib(*pkt++);
00683
00684 if ( (reg >= 0) && (reg < 33) )
00685 {
00686
00687 if (reg == 32)
00688 {
00689 *(core->status)=val&0xff;
00690 }
00691 else
00692 core->SetCoreReg(reg, val & 0xff);
00693 }
00694 else if (reg == 33)
00695 {
00696
00697 hval = hex2nib(*pkt++) << 4;
00698 hval += hex2nib(*pkt++);
00699
00700 core->stack->SetStackPointer((val & 0xff) + ((hval & 0xff) << 8));
00701 }
00702 else if (reg == 34)
00703 {
00704
00705
00706
00707
00708
00709
00710 dval = (dword)val;
00711
00712 dval += ((dword)hex2nib(*pkt++)) << 12;
00713 dval += ((dword)hex2nib(*pkt++)) << 8;
00714
00715 dval += ((dword)hex2nib(*pkt++)) << 20;
00716 dval += ((dword)hex2nib(*pkt++)) << 16;
00717
00718 dval += ((dword)hex2nib(*pkt++)) << 28;
00719 dval += ((dword)hex2nib(*pkt++)) << 24;
00720 core->PC=dval/2;
00721 }
00722 else
00723 {
00724 avr_warning( "Bad register value: %d\n", reg );
00725 gdb_send_reply( "E00" );
00726 return;
00727 }
00728
00729 gdb_send_reply( "OK" );
00730 }
00731
00736 int GdbServer::gdb_get_addr_len(const char *pkt, char a_end, char l_end, unsigned int *addr, int *len) {
00737 const char *orig_pkt = pkt;
00738
00739 *addr = 0;
00740 *len = 0;
00741
00742
00743 while (*pkt != a_end)
00744 *addr = (*addr << 4) + hex2nib(*pkt++);
00745 pkt++;
00746
00747
00748 while (*pkt != l_end)
00749 *len = (*len << 4) + hex2nib(*pkt++);
00750 pkt++;
00751
00752
00753
00754
00755 return (pkt - orig_pkt);
00756 }
00757
00758 void GdbServer::gdb_read_memory(const char *pkt) {
00759 unsigned int addr = 0;
00760 int len = 0;
00761 byte *buf;
00762 byte bval;
00763 word wval;
00764 int i;
00765 int is_odd_addr;
00766
00767 pkt += gdb_get_addr_len( pkt, ',', '\0', &addr, &len );
00768
00769 buf = avr_new0( byte, (len*2)+1 );
00770
00771 if ( (addr & MEM_SPACE_MASK) == EEPROM_OFFSET )
00772 {
00773
00774
00775 addr = addr & ~MEM_SPACE_MASK;
00776
00777 for ( i=0; i<len; i++ )
00778 {
00779 bval = core->eeprom->ReadFromAddress( addr+i );
00780 buf[i*2] = HEX_DIGIT[bval >> 4];
00781 buf[i*2+1] = HEX_DIGIT[bval & 0xf];
00782 }
00783 }
00784 else if ( (addr & MEM_SPACE_MASK) == SRAM_OFFSET )
00785 {
00786
00787
00788 addr = addr & ~MEM_SPACE_MASK;
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798 if (0)
00799 {
00800 snprintf( (char*)buf, len*2, "E%02x", EIO );
00801 }
00802 else
00803 {
00804 for ( i=0; i<len; i++ )
00805 {
00806 bval = core->GetRWMem(addr + i);
00807 buf[i*2] = HEX_DIGIT[bval >> 4];
00808 buf[i*2+1] = HEX_DIGIT[bval & 0xf];
00809 }
00810 }
00811 }
00812 else if ( (addr & MEM_SPACE_MASK) == FLASH_OFFSET )
00813 {
00814
00815
00816 addr = addr & ~MEM_SPACE_MASK;
00817
00818 is_odd_addr = addr % 2;
00819 i = 0;
00820
00821 if (is_odd_addr)
00822 {
00823 bval = avr_core_flash_read( addr ) >> 8;
00824 buf[i++] = HEX_DIGIT[bval >> 4];
00825 buf[i++] = HEX_DIGIT[bval & 0xf];
00826 addr++;
00827 len--;
00828 }
00829
00830 while (len > 1)
00831 {
00832 wval = avr_core_flash_read( addr );
00833
00834 bval = wval & 0xff;
00835 buf[i++] = HEX_DIGIT[bval >> 4];
00836 buf[i++] = HEX_DIGIT[bval & 0xf];
00837
00838 bval = wval >> 8;
00839 buf[i++] = HEX_DIGIT[bval >> 4];
00840 buf[i++] = HEX_DIGIT[bval & 0xf];
00841
00842 len -= 2;
00843 addr += 2;
00844 }
00845
00846 if (len == 1)
00847 {
00848 bval = avr_core_flash_read( addr ) & 0xff;
00849 buf[i++] = HEX_DIGIT[bval >> 4];
00850 buf[i++] = HEX_DIGIT[bval & 0xf];
00851 }
00852 }
00853 else
00854 {
00855
00856 avr_warning( "Invalid memory address: 0x%x.\n", addr );
00857 snprintf( (char*)buf, len*2, "E%02x", EIO );
00858 }
00859
00860
00861 gdb_send_reply( (char*)buf );
00862
00863 avr_free( buf );
00864 }
00865
00866 void GdbServer::gdb_write_memory(const char *pkt) {
00867 unsigned int addr = 0;
00868 int len = 0;
00869 byte bval;
00870 unsigned int i;
00871 char reply[10];
00872
00873
00874 strncpy( reply, "OK", sizeof(reply) );
00875
00876 pkt += gdb_get_addr_len( pkt, ',', ':', &addr, &len );
00877
00878
00879 if ( (addr & MEM_SPACE_MASK) == EEPROM_OFFSET )
00880 {
00881
00882
00883 addr = addr & ~MEM_SPACE_MASK;
00884
00885 while (len>0) {
00886 bval = hex2nib(*pkt++) << 4;
00887 bval += hex2nib(*pkt++);
00888 len--;
00889 core->eeprom->WriteAtAddress(addr, bval);
00890 addr++;
00891 }
00892 }
00893 else if ( (addr & MEM_SPACE_MASK) == SRAM_OFFSET )
00894 {
00895
00896
00897 addr = addr & ~MEM_SPACE_MASK;
00898
00899
00900
00901
00902
00903 if (0)
00904 {
00905 snprintf( reply, sizeof(reply), "E%02x", EIO );
00906 }
00907 else
00908 {
00909 for ( i=addr; i < addr+len; i++ )
00910 {
00911 bval = hex2nib(*pkt++) << 4;
00912 bval += hex2nib(*pkt++);
00913 core->SetRWMem(i, bval);
00914 }
00915 }
00916 }
00917 else if ( (addr & MEM_SPACE_MASK) == FLASH_OFFSET )
00918 {
00919
00920
00921 addr = addr & ~MEM_SPACE_MASK;
00922
00923 if (addr % 2)
00924 {
00925 bval = hex2nib(*pkt++) << 4;
00926 bval += hex2nib(*pkt++);
00927 avr_core_flash_write_hi8(addr, bval);
00928 len--;
00929 addr++;
00930 }
00931
00932 while (len > 1)
00933 {
00934 word wval;
00935 wval = hex2nib(*pkt++) << 4;
00936 wval += hex2nib(*pkt++);
00937 wval += hex2nib(*pkt++) << 12;
00938 wval += hex2nib(*pkt++) << 8;
00939 avr_core_flash_write( addr, wval);
00940 len -= 2;
00941 addr += 2;
00942 }
00943
00944 if ( len == 1 )
00945 {
00946
00947 bval = hex2nib(*pkt++) << 4;
00948 bval += hex2nib(*pkt++);
00949 avr_core_flash_write_lo8( addr, bval );
00950 }
00951 }
00952 else if ( (addr & MEM_SPACE_MASK) == SIGNATURE_OFFSET && len >= 3)
00953 {
00954 int sig3 = (hex2nib(*pkt++) << 4) + hex2nib(*pkt++);
00955 int sig2 = (hex2nib(*pkt++) << 4) + hex2nib(*pkt++);
00956 int sig1 = (hex2nib(*pkt++) << 4) + hex2nib(*pkt++);
00957 if (global_debug_on)
00958 fprintf(stderr, "Device signature %02x %02x %02x\n", sig1, sig2, sig3);
00959 }
00960 else
00961 {
00962
00963 avr_warning( "Invalid memory address: 0x%x.\n", addr );
00964 snprintf( reply, sizeof(reply), "E%02x", EIO );
00965 }
00966
00967 gdb_send_reply( reply );
00968 }
00969
00989 void GdbServer::gdb_break_point(const char *pkt) {
00990 unsigned int addr = 0;
00991 int len = 0;
00992
00993 char z = *(pkt-1);
00994 char t = *pkt++;
00995 pkt++;
00996
00997 gdb_get_addr_len( pkt, ',', '\0', &addr, &len );
00998
00999 switch (t) {
01000 case '0':
01001
01002 if ( addr >= core->Flash->GetSize() )
01003 {
01004 avr_warning( "Attempt to set break at invalid addr\n" );
01005 gdb_send_reply( "E01" );
01006 return;
01007 }
01008
01009 if (z == 'z')
01010 {
01011
01012
01013 avr_core_remove_breakpoint( addr/2 );
01014 }
01015 else
01016 {
01017
01018
01019 avr_core_insert_breakpoint( addr/2 );
01020 }
01021 break;
01022
01023 case '1':
01024
01025
01026
01027 gdb_send_reply( "" );
01028 return;
01029 break;
01030
01031 case '2':
01032
01033
01034 gdb_send_reply( "" );
01035 return;
01036 break;
01037
01038 case '3':
01039
01040
01041 gdb_send_reply( "" );
01042 return;
01043 break;
01044
01045 case '4':
01046
01047
01048 gdb_send_reply( "" );
01049 return;
01050 }
01051
01052 gdb_send_reply( "OK" );
01053 }
01054
01055 void GdbServer::gdb_select_thread(const char *pkt)
01056 {
01057 if(pkt[0] == 'c') {
01058 gdb_send_reply( "" );
01059 return;
01060 }
01061 if(pkt[0] != 'g') {
01062 gdb_send_reply( "" );
01063 pkt--;
01064 if (global_debug_on)
01065 fprintf(stderr, "gdb '%s' not supported\n", pkt);
01066 return;
01067 }
01068
01069 int thread_id = 0;
01070 if(strcmp(pkt+1, "-1") == 0)
01071 thread_id = -1;
01072 else{
01073 for(const char* p = pkt+1; *p != '\0'; p++) {
01074 thread_id = thread_id << 4 | hex2nib(*p);
01075 }
01076 }
01077
01078 if (global_debug_on)
01079 fprintf(stderr, "gdb* set thread %d\n", thread_id);
01080 m_gdb_thread_id = (thread_id >= 1) ? thread_id : 1;
01081 gdb_send_reply( "OK" );
01082 }
01083
01084 void GdbServer::gdb_is_thread_alive(const char *pkt)
01085 {
01086 int thread_id = 0;
01087 if(strcmp(pkt, "-1") == 0)
01088 thread_id = -1;
01089 else{
01090 for(const char* p = pkt; *p != '\0'; p++) {
01091 thread_id = thread_id << 4 | hex2nib(*p);
01092 }
01093 }
01094 if (global_debug_on)
01095 fprintf(stderr, "gdb is thread %d alive\n", thread_id);
01096 bool alive = core->stack->m_ThreadList.IsGDBThreadAlive(thread_id);
01097 assert(alive);
01098 gdb_send_reply( alive ? "OK" : "E00" );
01099 }
01100
01101 void GdbServer::gdb_get_thread_list(const char *pkt)
01102 {
01103 if (global_debug_on)
01104 fprintf(stderr, "gdb get thread info\n");
01105 unsigned char allocated = core->stack->m_ThreadList.GetCount() * 3 + 5;
01106 char * response = new char[allocated];
01107 response[0] = 'm';
01108 unsigned char pos = 1;
01109
01110 for(unsigned int i = 0; i < core->stack->m_ThreadList.GetCount(); i++) {
01111 int used = snprintf(response + pos, allocated-pos, "%d,", i+1);
01112 pos += used;
01113 }
01114 assert(response[pos-1] == ',');
01115 response[pos-1] = '\0';
01116
01117 gdb_send_reply(response);
01118 delete [] response;
01119 }
01120
01132 int GdbServer::gdb_get_signal(const char *pkt) {
01133 int signo;
01134
01135
01136
01137
01138 signo = (hex2nib( *pkt++ ) << 4);
01139 signo += (hex2nib( *pkt++ ) & 0xf);
01140
01141 if (global_debug_on)
01142 fprintf( stderr, "GDB sent signal: %d\n", signo );
01143
01144
01145
01146
01147
01148 switch (signo)
01149 {
01150 case GDB_SIGHUP:
01151
01152
01153
01154 core->Reset( );
01155 gdb_send_reply( "S05" );
01156 break;
01157 }
01158
01159 return signo;
01160 }
01161
01165 int GdbServer::gdb_parse_packet(const char *pkt) {
01166 switch (*pkt++) {
01167 case '?':
01168 gdb_send_reply("S05");
01169 break;
01170
01171 case 'H':
01172 gdb_select_thread(pkt);
01173 break;
01174 case 'T':
01175 gdb_is_thread_alive(pkt);
01176 break;
01177 case 'g':
01178 gdb_read_registers();
01179 break;
01180
01181 case 'G':
01182 gdb_write_registers(pkt);
01183 break;
01184
01185 case 'p':
01186 gdb_read_register(pkt);
01187 break;
01188
01189 case 'P':
01190 gdb_write_register(pkt);
01191 break;
01192
01193 case 'm':
01194 gdb_read_memory(pkt);
01195 break;
01196
01197 case 'M':
01198 gdb_write_memory(pkt);
01199 break;
01200
01201 case 'D':
01202 case 'k':
01203
01204
01205 gdb_send_reply("OK");
01206 if(exitOnKillRequest)
01207 raise(SIGINT);
01208 return GDB_RET_KILL_REQUEST;
01209
01210 case 'c':
01211 if(!core->Flash->IsProgramLoaded()) {
01212 gdb_send_hex_reply("O", "No program to simulate. Use 'load' to upload it.\n");
01213 SendPosition(GDB_SIGHUP);
01214 return GDB_RET_OK;
01215 }
01216 return GDB_RET_CONTINUE;
01217 break;
01218
01219 case 'C':
01220 if(GDB_SIGHUP==gdb_get_signal(pkt)) {
01221
01222
01223 exitOnKillRequest = true;
01224 return GDB_RET_OK;
01225 }
01226 return GDB_RET_CONTINUE;
01227 break;
01228
01229 case 'S':
01230 gdb_get_signal(pkt);
01231
01232 case 's':
01233 if(!core->Flash->IsProgramLoaded()) {
01234 gdb_send_hex_reply("O", "No program to simulate. Use 'load' to upload it.\n");
01235
01236 SendPosition(GDB_SIGHUP);
01237 return GDB_RET_OK;
01238 }
01239 return GDB_RET_SINGLE_STEP;
01240
01241 case 'z':
01242 case 'Z':
01243 gdb_break_point(pkt);
01244 break;
01245
01246 case 'q':
01247 pkt--;
01248 if(memcmp(pkt, "qSupported", 10) == 0) {
01249 gdb_send_reply("PacketSize=800;qXfer:features:read+");
01250 return GDB_RET_OK;
01251 } else if(memcmp(pkt, "qXfer:features:read:target.xml:", 31) == 0) {
01252
01253
01254 gdb_send_reply("l"
01255 "<?xml version=\"1.0\"?>\n"
01256 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">\n"
01257 "<target version=\"1.0\">\n"
01258 " <architecture>avr</architecture>\n"
01259 "</target>\n");
01260 return GDB_RET_OK;
01261 } else if(strcmp(pkt, "qC") == 0) {
01262 int thread_id = core->stack->m_ThreadList.GetCurrentThreadForGDB();
01263 if (global_debug_on)
01264 fprintf(stderr, "gdb get current thread: %d\n", thread_id);
01265 char reply[100];
01266 snprintf( reply, sizeof(reply), "QC%02x", thread_id);
01267 gdb_send_reply( reply );
01268 return GDB_RET_OK;
01269 } else if(strcmp(pkt, "qfThreadInfo") == 0) {
01270 gdb_get_thread_list(pkt);
01271 return GDB_RET_OK;
01272 } else if(strcmp(pkt, "qsThreadInfo") == 0) {
01273 gdb_send_reply( "l" );
01274 return GDB_RET_OK;
01275 }
01276
01277 if(global_debug_on)
01278 fprintf(stderr, "gdb query '%s' not supported\n", pkt);
01279 gdb_send_reply("");
01280 break;
01281
01282 default:
01283 pkt--;
01284 if(global_debug_on)
01285 fprintf(stderr, "gdb command '%s' not supported\n", pkt);
01286 gdb_send_reply("");
01287 }
01288
01289 return GDB_RET_OK;
01290 }
01291
01297 int GdbServer::gdb_receive_and_process_packet(int blocking) {
01298 int res;
01299 int c;
01300 std::string pkt_buf;
01301 int cksum, pkt_cksum;
01302
01303 server->SetBlockingMode(blocking);
01304
01305 c = server->ReadByte();
01306
01307 switch(c) {
01308 case '$':
01309
01310 server->SetBlockingMode(GDB_BLOCKING_ON);
01311
01312 pkt_cksum = 0;
01313 c = server->ReadByte();
01314 while(c != '#') {
01315 pkt_buf.push_back(c);
01316 pkt_cksum += (unsigned char)c;
01317 c = server->ReadByte();
01318 }
01319
01320 cksum = hex2nib(server->ReadByte()) << 4;
01321 cksum |= hex2nib(server->ReadByte());
01322
01323
01324
01325
01326
01327
01328
01329 if((pkt_cksum & 0xff) != cksum)
01330 avr_error("Bad checksum: sent 0x%x <--> computed 0x%x",
01331 cksum, pkt_cksum);
01332
01333 if(global_debug_on)
01334 fprintf(stderr, "Recv: \"$%s#%02x\"\n", pkt_buf.c_str(), cksum);
01335
01336
01337 gdb_send_ack();
01338
01339 res = gdb_parse_packet(pkt_buf.c_str());
01340 if(res < 0)
01341 return res;
01342
01343 break;
01344
01345 case '-':
01346
01347 if(global_debug_on)
01348 fprintf(stderr, " gdb -> Nak\n");
01349 gdb_send_reply(gdb_last_reply(NULL));
01350 break;
01351
01352 case '+':
01353 if(global_debug_on)
01354 fprintf(stderr, " gdb -> Ack\n");
01355 break;
01356
01357 case 0x03:
01358
01359
01360
01361
01362 if (global_debug_on)
01363 fprintf( stderr, "gdb* Ctrl-C\n" );
01364 return GDB_RET_CTRL_C;
01365
01366 case -1:
01367
01368 return GDB_RET_NOTHING_RECEIVED;
01369 break;
01370
01371 default:
01372 avr_warning("Unknown request from gdb: %c (0x%02x)\n", c, c);
01373 }
01374
01375 return GDB_RET_OK;
01376 }
01377
01389 void GdbServer::Run( )
01390 {
01391 int res;
01392 char reply[MAX_BUF + 1];
01393
01394 while (1)
01395 {
01396 res = gdb_receive_and_process_packet( GDB_BLOCKING_ON);
01397 switch (res)
01398 {
01399 case GDB_RET_KILL_REQUEST:
01400 return;
01401
01402 case GDB_RET_CTRL_C:
01403 gdb_send_ack( );
01404 snprintf(reply, sizeof(reply), "S%02x", GDB_SIGINT);
01405 gdb_send_reply( reply );
01406 break;
01407
01408 default:
01409 break;
01410 }
01411 }
01412 }
01413
01415 void GdbServer::TryConnectGdb() {
01416 time_t newTime = time(NULL);
01417
01418 if(oldTime != newTime) {
01419 oldTime = newTime;
01420
01421 if(connState = server->Connect())
01422 allGdbServers.push_back(this);
01423 }
01424 }
01425
01426 int GdbServer::Step(bool &trueHwStep, SystemClockOffset *timeToNextStepIn_ns) {
01427 if(!connState) {
01428 TryConnectGdb();
01429 if (!waitForGdbConnection) {
01430 core->Step(trueHwStep, timeToNextStepIn_ns);
01431 } else {
01432 if (timeToNextStepIn_ns!=0) *timeToNextStepIn_ns=core->GetClockFreq();
01433 }
01434 return 0;
01435 } else {
01436 return InternalStep(trueHwStep, timeToNextStepIn_ns);
01437 }
01438 }
01439
01440 void GdbServer::IdleStep() {
01441 int gdbRet=gdb_receive_and_process_packet(GDB_BLOCKING_OFF);
01442 cout << "IdleStep Instance" << this << " RunMode:" << dec << runMode << endl;
01443
01444 if (lastCoreStepFinished) {
01445 switch(gdbRet) {
01446 case GDB_RET_NOTHING_RECEIVED:
01447 break;
01448
01449 case GDB_RET_OK:
01450 break;
01451
01452 case GDB_RET_CONTINUE:
01453 runMode=GDB_RET_CONTINUE;
01454 break;
01455
01456 case GDB_RET_CTRL_C:
01457 runMode=GDB_RET_CTRL_C;
01458 SendPosition(GDB_SIGINT);
01459 break;
01460
01461 default:
01462 cout << "wondering" << endl;
01463 }
01464 }
01465 }
01466
01467 int GdbServer::InternalStep(bool &untilCoreStepFinished, SystemClockOffset *timeToNextStepIn_ns) {
01468
01469
01470
01471 if (lastCoreStepFinished) {
01472 bool leave;
01473
01474 do {
01475
01476 int gdbRet=gdb_receive_and_process_packet((runMode==GDB_RET_CONTINUE) ? GDB_BLOCKING_OFF : GDB_BLOCKING_ON);
01477
01478 switch (gdbRet) {
01479 case GDB_RET_NOTHING_RECEIVED:
01480 break;
01481
01482 case GDB_RET_OK:
01483 runMode=GDB_RET_OK;
01484 break;
01485
01486 case GDB_RET_CONTINUE:
01487
01488 runMode=GDB_RET_CONTINUE;
01489 break;
01490
01491 case GDB_RET_SINGLE_STEP:
01492
01493 runMode=GDB_RET_SINGLE_STEP;
01494 break;
01495
01496 case GDB_RET_CTRL_C:
01497
01498 runMode=GDB_RET_CTRL_C;
01499 SendPosition(GDB_SIGINT);
01500 break;
01501
01502 case GDB_RET_KILL_REQUEST:
01503 core->Reset();
01504 server->CloseConnection();
01505 connState = false;
01506 core->DeleteAllBreakpoints();
01507 return 0;
01508 }
01509
01510 if (runMode == GDB_RET_SINGLE_STEP || runMode == GDB_RET_CONTINUE) {
01511 leave = true;
01512 } else {
01513 leave = false;
01514 }
01515
01516 if(!leave) {
01517
01518
01519 vector<GdbServer*>::iterator ii;
01520 for (ii=allGdbServers.begin(); ii!=allGdbServers.end(); ii++) {
01521 if (*ii!=this) {
01522 (*ii)->IdleStep();
01523 }
01524 }
01525 }
01526 } while (leave==false);
01527
01528 }
01529
01530 int res=core->Step(untilCoreStepFinished, timeToNextStepIn_ns);
01531 lastCoreStepFinished=untilCoreStepFinished;
01532
01533 if (res == BREAK_POINT) {
01534 runMode=GDB_RET_OK;
01535 SendPosition(GDB_SIGTRAP);
01536 }
01537
01538 if (res == INVALID_OPCODE)
01539 {
01540
01541
01542 char reply[MAX_BUF+1];
01543 snprintf(reply, sizeof(reply), "S%02x", GDB_SIGILL);
01544 gdb_send_reply( reply );
01545 runMode=GDB_RET_OK;
01546 SendPosition(GDB_SIGILL);
01547 }
01548
01549 if (runMode==GDB_RET_SINGLE_STEP) {
01550 runMode=GDB_RET_OK;
01551 SendPosition(GDB_SIGTRAP);
01552 }
01553
01554 return 0;
01555 }
01556
01557 void GdbServer::SendPosition(int signo) {
01558
01559 int bytes = 0;
01560 char reply[MAX_BUF + 1];
01561 unsigned long sp = core->stack->GetStackPointer();
01562 unsigned int spl = sp & 0xff;
01563 unsigned int sph = (sp >> 8) & 0xff;
01564 int pc = core->PC * 2;
01565 int thread_id = core->stack->m_ThreadList.GetCurrentThreadForGDB();
01566
01567 bytes = snprintf(reply, sizeof(reply), "T%02x", signo);
01568
01569
01570 snprintf(reply + bytes, sizeof(reply) - bytes,
01571 "20:%02x;" "21:%02x%02x;" "22:%02x%02x%02x%02x;" "thread:%d;",
01572 ((int)(*(core->status))),
01573 spl, sph,
01574 pc & 0xff, (pc >> 8) & 0xff, (pc >> 16) & 0xff, (pc >> 24) & 0xff,
01575 thread_id);
01576
01577 gdb_send_reply(reply);
01578
01579 m_gdb_thread_id = thread_id;
01580 }
01581
01582 int GdbServer::SleepStep() {
01583 return 0;
01584 }