-
Notifications
You must be signed in to change notification settings - Fork 7
/
gdbserver.cpp
1390 lines (1117 loc) · 39.2 KB
/
gdbserver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
****************************************************************************
* Uzem - The Uzebox Emulator
*
* This file was derived from the Simulavr project by Filipe Rinaldi (2009)
*
* simulavr - A simulator for the Atmel AVR family of microcontrollers.
* Copyright (C) 2001, 2002, 2003 Theodore A. Roth, Klaus Rudolph
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
****************************************************************************
*/
#include <iostream>
#include <algorithm>
using namespace std;
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include "gdbserver.h"
#include "avr8.h"
#define avr_new(type, count, flag) ((type *) do_avr_new(((unsigned) sizeof (type) * (count)), flag))
static void *do_avr_new(size_t size, bool blank_it)
{
if (size)
{
void *ptr;
ptr = malloc( size );
if (ptr)
{
if (blank_it == true)
memset(ptr, 0, size);
return ptr;
}
else
{
fprintf(stderr, "malloc failed");
exit(0);
}
}
return NULL;
}
static char *avr_strdup(const char *s)
{
if (s)
{
char *ptr;
ptr = strdup(s);
if (ptr)
return ptr;
fprintf(stderr, "strdup failed" );
exit(0);
}
return NULL;
}
static void avr_free(void *ptr)
{
if (ptr)
free(ptr);
}
GdbServer::GdbServer(avr8 *c, int _port, int debug, int _waitForGdbConnection): core(c), port(_port), global_debug_on(debug), waitForGdbConnection(_waitForGdbConnection) {
last_reply=NULL; //init static var for last_reply()
//is_running=0; //init static var for continue()
block_on=1; //init static var for pre_parse_packet()
conn=-1; //no connection opened
runMode= GDB_RET_NOTHING_RECEIVED;
int i;
#if defined(__WIN32__)
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData)) {
printf("WSAStartup() failed");
exit(1);
}
#endif
if ( (sock = socket( PF_INET, SOCK_STREAM, 0 )) < 0 )
printf( "Can't create socket: %s", strerror(errno) );
/* Let the kernel reuse the socket address. This lets us run
twice in a row, without waiting for the (ip, port) tuple
to time out. */
i = 1;
setsockopt( sock, SOL_SOCKET, SO_REUSEADDR, (char*)&i, sizeof(i) );
#if defined(__WIN32__)
u_long iMode = 1;
ioctlsocket(sock, FIONBIO, &iMode);
#else
fcntl( sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK); //dont know
#endif
address->sin_family = AF_INET;
address->sin_port = htons(port);
memset( &address->sin_addr, 0, sizeof(address->sin_addr) );
if ( bind( sock, (struct sockaddr *)address, sizeof(address) ) )
printf( "Can not bind socket: %s", strerror(errno) );
if ( listen(sock, 1) < 0)
{
cerr << "Can not listen on socket: " << strerror(errno) << endl;
}
fprintf( stderr, "Waiting on port %d for gdb client to connect...\n", port );
logFile=fopen("gdb.log","w");
fprintf(logFile,"Opening GDB Session 2\n");
}
GdbServer::~GdbServer() {
CLOSE_SOCK(conn);
CLOSE_SOCK(sock);
fprintf(logFile,"Closing GDB Session\n");
fclose(logFile);
#if defined(__WIN32__)
WSACleanup();
#endif
}
word GdbServer::avr_core_flash_read(int addr) {
return core->progmem[addr];
}
void GdbServer::avr_core_flash_write( unsigned int addr, word val) {
if (addr>= progSize) {
cerr << "try to write in flash after last valid address!" << endl;
exit(0);
}
core->progmem[addr]=val;
}
void GdbServer::avr_core_flash_write_hi8( unsigned int addr, byte val) {
if ((addr*2)>= progSize) {
cerr << "try to write in flash after last valid address!" << endl;
exit(0);
}
u16 tmp = (core->progmem[addr] & 0x00FF) | (val << 8);
core->progmem[addr] = tmp;
}
void GdbServer::avr_core_flash_write_lo8( unsigned int addr, byte val) {
if (addr>=progSize) {
cerr << "try to write in flash after last valid address!" << endl;
exit(0);
}
u16 tmp = (core->progmem[addr] & 0xFF00) | (val);
core->progmem[addr] = tmp;
}
void GdbServer::avr_core_remove_breakpoint(dword pc) {
Breakpoints::iterator ii;
if ((ii= find(BP.begin(), BP.end(), pc)) != BP.end())
BP.erase(ii);
}
void GdbServer::avr_core_insert_breakpoint(dword pc) {
BP.push_back(pc);
}
int GdbServer::signal_has_occurred(int signo) {(void)signo; return 0;}
void GdbServer::signal_watch_start(int signo){(void)signo;}
void GdbServer::signal_watch_stop(int signo){(void)signo;}
static char HEX_DIGIT[] = "0123456789abcdef";
/* Wrap read(2) so we can read a byte without having
to do a shit load of error checking every time. */
int GdbServer::gdb_read_byte( )
{
char c;
int res;
int cnt = MAX_READ_RETRY;
while (cnt--)
{
res = recv( conn, &c, 1, 0 );
#if defined(__WIN32__)
if (res == SOCKET_ERROR)
{
if (WSAGetLastError() == WSAEWOULDBLOCK)
/* fd was set to non-blocking and no data was available */
return -1;
printf( "read failed: %s", strerror(errno) );
}
#else
if (res<0)
{
if (errno == EAGAIN)
/* fd was set to non-blocking and no data was available */
return -1;
printf( "read failed: %s", strerror(errno) );
}
#endif
if (res == 0) {
printf( "gdb closed connection. Exiting...\n" );
exit(0);
}
return c;
}
printf( "Maximum read retries reached\n" );
exit(0);
return 0; /* make compiler happy */
}
/* Convert a hexidecimal digit to a 4 bit nibble. */
int GdbServer::hex2nib( char hex )
{
if ( (hex >= 'A') && (hex <= 'F') )
return (10 + (hex - 'A'));
else if ( (hex >= 'a') && (hex <= 'f') )
return (10 + (hex - 'a'));
else if ( (hex >= '0') && (hex <= '9') )
return (hex - '0');
/* Shouldn't get here unless the developer screwed up ;) */
printf( "Invalid hexidecimal digit: 0x%02x", hex );
return 0; /* make compiler happy */
}
/* Wrapper for write(2) which hides all the repetitive error
checking crap. */
void GdbServer::gdb_write( const void *buf, size_t count )
{
int res;
res = send( conn, (const char*)buf, count, 0 );
/* FIXME: should we try and catch interrupted system calls here? */
if (res < 0)
printf( "write failed: %s", strerror(errno) );
/* FIXME: if this happens a lot, we could try to resend the
unsent bytes. */
if ((unsigned int)res != count)
printf( "write only wrote %d of %ld bytes", res, count );
}
/* Use a single function for storing/getting the last reply message.
If reply is NULL, return pointer to the last reply saved.
Otherwise, make a copy of the buffer pointed to by reply. */
const char* GdbServer::gdb_last_reply( const char *reply )
{
if (reply == NULL)
{
if (last_reply == NULL)
return "";
else
return last_reply;
}
avr_free(last_reply);
last_reply = avr_strdup( reply );
if (last_reply == 0)
{
fprintf(stderr, "Failed to copy string %s\n",reply);
exit(0);
}
return last_reply;
}
/* Acknowledge a packet from GDB */
void GdbServer::gdb_send_ack( )
{
if (global_debug_on)
fprintf( stderr, " Ack -> gdb\n");
gdb_write( "+", 1 );
}
/* Send a reply to GDB. */
void GdbServer::gdb_send_reply( const char *reply )
{
int cksum = 0;
int bytes;
/* Save the reply to last reply so we can resend if need be. */
gdb_last_reply( reply );
if (global_debug_on)
fprintf( stderr, "Sent: $%s#", reply );
if (*reply == '\0')
{
gdb_write( "$#00", 4 );
if (global_debug_on)
fprintf( stderr, "%02x\n", cksum & 0xff );
}
else
{
memset( buf, '\0', sizeof(buf) );
buf[0] = '$';
bytes = 1;
while (*reply)
{
cksum += (unsigned char)*reply;
buf[bytes] = *reply;
bytes++;
reply++;
/* must account for "#cc" to be added */
if (bytes == (MAX_BUF-3))
{
/* FIXME: TRoth 2002/02/18 - splitting reply would be better */
printf( "buffer overflow" );
}
}
if (global_debug_on)
fprintf( stderr, "%02x\n", cksum & 0xff );
buf[bytes++] = '#';
buf[bytes++] = HEX_DIGIT[(cksum >> 4) & 0xf];
buf[bytes++] = HEX_DIGIT[cksum & 0xf];
gdb_write( buf, bytes );
}
}
/* GDB needs the 32 8-bit, gpw registers (r00 - r31), the
8-bit SREG, the 16-bit SP (stack pointer) and the 32-bit PC
(program counter). Thus need to send a reply with
r00, r01, ..., r31, SREG, SPL, SPH, PCL, PCH
Low bytes before High since AVR is little endian. */
void GdbServer::gdb_read_registers( )
{
int i;
dword val; /* ensure it's 32 bit value */
/* (32 gpwr, SREG, SP, PC) * 2 hex bytes + terminator */
size_t buf_sz = (32 + 1 + 2 + 4)*2 + 1;
char *buf;
buf = avr_new( char, buf_sz, true );
/* 32 gen purpose working registers */
for ( i=0; i<32; i++ )
{
val = core->r[i];
buf[i*2] = HEX_DIGIT[(val >> 4) & 0xf];
buf[i*2+1] = HEX_DIGIT[val & 0xf];
}
/* GDB thinks SREG is register number 32 */
val = core->SREG;
buf[i*2] = HEX_DIGIT[(val >> 4) & 0xf];
buf[i*2+1] = HEX_DIGIT[val & 0xf];
i++;
/* GDB thinks SP is register number 33 */
//val = avr_core_mem_read(core, SPL_ADDR);
val=core->SPL;
buf[i*2] = HEX_DIGIT[(val >> 4) & 0xf];
buf[i*2+1] = HEX_DIGIT[val & 0xf];
i++;
//val = avr_core_mem_read(core, SPH_ADDR);
val=core->SPH;
buf[i*2] = HEX_DIGIT[(val >> 4) & 0xf];
buf[i*2+1] = HEX_DIGIT[val & 0xf];
i++;
/* GDB thinks PC is register number 34.
GDB stores PC in a 32 bit value (only uses 23 bits though).
GDB thinks PC is bytes into flash, not words like in simulavr. */
val = core->pc * 2;
buf[i*2] = HEX_DIGIT[(val >> 4) & 0xf];
buf[i*2+1] = HEX_DIGIT[val & 0xf];
val >>= 8;
buf[i*2+2] = HEX_DIGIT[(val >> 4) & 0xf];
buf[i*2+3] = HEX_DIGIT[val & 0xf];
val >>= 8;
buf[i*2+4] = HEX_DIGIT[(val >> 4) & 0xf];
buf[i*2+5] = HEX_DIGIT[val & 0xf];
val >>= 8;
buf[i*2+6] = HEX_DIGIT[(val >> 4) & 0xf];
buf[i*2+7] = HEX_DIGIT[val & 0xf];
gdb_send_reply( buf );
avr_free(buf);
}
/* GDB is sending values to be written to the registers. Registers are the
same and in the same order as described in gdb_read_registers() above. */
void GdbServer::gdb_write_registers( char *pkt )
{
int i;
byte bval;
dword val; /* ensure it's a 32 bit value */
/* 32 gen purpose working registers */
for ( i=0; i<32; i++ )
{
bval = hex2nib(*pkt++) << 4;
bval += hex2nib(*pkt++);
core->r[i]=bval;
}
/* GDB thinks SREG is register number 32 */
bval = hex2nib(*pkt++) << 4;
bval += hex2nib(*pkt++);
core->SREG=bval;
/* GDB thinks SP is register number 33 */
bval = hex2nib(*pkt++) << 4;
bval += hex2nib(*pkt++);
core->SPL = bval;
bval = hex2nib(*pkt++) << 4;
bval += hex2nib(*pkt++);
core->SPH = bval;
/* GDB thinks PC is register number 34.
GDB stores PC in a 32 bit value (only uses 23 bits though).
GDB thinks PC is bytes into flash, not words like in simulavr.
Must cast to dword so as not to get mysterious truncation. */
val = ((dword)hex2nib(*pkt++)) << 4;
val += ((dword)hex2nib(*pkt++));
val += ((dword)hex2nib(*pkt++)) << 12;
val += ((dword)hex2nib(*pkt++)) << 8;
val += ((dword)hex2nib(*pkt++)) << 20;
val += ((dword)hex2nib(*pkt++)) << 16;
val += ((dword)hex2nib(*pkt++)) << 28;
val += ((dword)hex2nib(*pkt++)) << 24;
core->pc=val/2;
gdb_send_reply( "OK" );
}
/* Extract a hexidecimal number from the pkt. Keep scanning pkt until stop char
is reached or size of int is exceeded or a NULL is reached. pkt is modified
to point to stop char when done.
Use this function to extract a num with an arbitrary num of hex
digits. This should _not_ be used to extract n digits from a m len string
of digits (n <= m). */
int GdbServer::gdb_extract_hex_num( char **pkt, char stop )
{
int i = 0;
int num = 0;
char *p = *pkt;
int max_shifts = sizeof(int)*2-1; /* max number of nibbles to shift through */
while ( (*p != stop) && (*p != '\0') )
{
if (i > max_shifts)
printf( "number too large" );
num = (num << 4) | hex2nib(*p);
i++;
p++;
}
*pkt = p;
return num;
}
/* Read a single register. Packet form: 'pn' where n is a hex number with no
zero padding. */
void GdbServer::gdb_read_register( char *pkt )
{
int reg;
char reply[MAX_BUF];
memset(reply, '\0', sizeof(reply));
reg = gdb_extract_hex_num(&pkt, '\0');
if ( (reg >= 0) && (reg < 32) )
{ /* general regs */
byte val = core->r[reg];
snprintf( reply, sizeof(reply)-1, "%02x", val );
}
else if (reg == 32) /* sreg */
{
byte val = core->SREG;
snprintf( reply, sizeof(reply)-1, "%02x", val );
}
else if (reg == 33) /* SP */
{
byte spl, sph;
//spl = avr_core_mem_read( core, SPL_ADDR );
//sph = avr_core_mem_read( core, SPH_ADDR );
spl=core->SPL;
sph=core->SPH;
snprintf( reply, sizeof(reply)-1, "%02x%02x", spl, sph );
}
else if (reg == 34) /* PC */
{
dword val = core->pc * 2;
snprintf( reply, sizeof(reply)-1,
"%02x%02x" "%02x%02x",
val & 0xff, (val >> 8) & 0xff,
(val >> 16) & 0xff, (val >> 24) & 0xff );
}
else
{
printf( "Bad register value: %d\n", reg );
gdb_send_reply( "E00" );
return;
}
gdb_send_reply( reply );
}
/* Write a single register. Packet form: 'Pn=r' where n is a hex number with
no zero padding and r is two hex digits for each byte in register (target
byte order). */
void GdbServer::gdb_write_register( char *pkt )
{
int reg;
int val, hval;
dword dval;
reg = gdb_extract_hex_num(&pkt, '=');
pkt++; /* skip over '=' character */
/* extract the low byte of value from pkt */
val = hex2nib(*pkt++) << 4;
val += hex2nib(*pkt++);
if ( (reg >= 0) && (reg < 33) )
{
/* r0 to r31 and SREG */
if (reg == 32) /* gdb thinks SREG is register 32 */
{
core->SREG=val&0xff;
}
else
{
core->r[reg]=val&0xff;
}
}
else if (reg == 33)
{
/* SP is 2 bytes long so extract upper byte */
hval = hex2nib(*pkt++) << 4;
hval += hex2nib(*pkt++);
//avr_core_mem_write( core, SPL_ADDR, val & 0xff );
//avr_core_mem_write( core, SPH_ADDR, hval & 0xff );
core->SPL = (val&0xff);
core->SPH = (hval&0xff);
}
else if (reg == 34)
{
/* GDB thinks PC is register number 34.
GDB stores PC in a 32 bit value (only uses 23 bits though).
GDB thinks PC is bytes into flash, not words like in simulavr.
Must cast to dword so as not to get mysterious truncation. */
dval = (dword)val; /* we already read the first two nibbles */
dval += ((dword)hex2nib(*pkt++)) << 12;
dval += ((dword)hex2nib(*pkt++)) << 8;
dval += ((dword)hex2nib(*pkt++)) << 20;
dval += ((dword)hex2nib(*pkt++)) << 16;
dval += ((dword)hex2nib(*pkt++)) << 28;
dval += ((dword)hex2nib(*pkt++)) << 24;
core->pc=dval / 2;
}
else
{
printf( "Bad register value: %d\n", reg );
gdb_send_reply( "E00" );
return;
}
gdb_send_reply( "OK" );
}
/* Parse the pkt string for the addr and length.
a_end is first char after addr.
l_end is first char after len.
Returns number of characters to advance pkt. */
int GdbServer::gdb_get_addr_len( char *pkt, char a_end, char l_end, unsigned int *addr, int *len )
{
char *orig_pkt = pkt;
*addr = 0;
*len = 0;
/* Get the addr from the packet */
while (*pkt != a_end)
*addr = (*addr << 4) + hex2nib(*pkt++);
pkt++; /* skip over a_end */
/* Get the length from the packet */
while (*pkt != l_end)
*len = (*len << 4) + hex2nib(*pkt++);
pkt++; /* skip over l_end */
/* fprintf( stderr, "+++++++++++++ addr = 0x%08x\n", *addr ); */
/* fprintf( stderr, "+++++++++++++ len = %d\n", *len ); */
return (pkt - orig_pkt);
}
void GdbServer::gdb_read_memory( char *pkt )
{
unsigned int addr = 0;
int len = 0;
byte *buf;
byte bval;
word wval;
int i;
int is_odd_addr;
pkt += gdb_get_addr_len( pkt, ',', '\0', &addr, &len );
buf = avr_new( byte, (len*2)+1, true );
//if(addr>=0x804000) addr&=0xffff;
fprintf(logFile,"%x:",addr);
if ( (addr & MEM_SPACE_MASK) == EEPROM_OFFSET )
{
/* addressing eeprom */
addr = addr & ~MEM_SPACE_MASK; /* remove the offset bits */
//printf( "reading of eeprom not yet implemented: 0x%x.\n", addr );
//snprintf( (char*)buf, len*2, "E%02x", EIO );
for ( i=0; i<len; i++ )
{
bval = core->eeprom[addr+i];
buf[i*2] = HEX_DIGIT[bval >> 4];
buf[i*2+1] = HEX_DIGIT[bval & 0xf];
}
}
else if ( (addr & MEM_SPACE_MASK) == SRAM_OFFSET )
{
/* addressing sram */
addr = addr & ~MEM_SPACE_MASK; /* remove the offset bits */
if (addr >= (SRAMBASE+sramSize))
{
fprintf(logFile,"invalid ram");
if (global_debug_on)
printf("Sram address:%x invalid\n",addr);
snprintf( (char*)buf, len*2, "E%02x", EIO );
}
else
{
// Trying to access one of the registers (below IOBASE)
if (addr < IOBASE)
{
bval = core->r[addr];
buf[0] = HEX_DIGIT[bval >> 4];
buf[1] = HEX_DIGIT[bval & 0xf];
}
// Trying to access one of the IOs (below SRAMBASE)
else if (addr < SRAMBASE )
{
bval = core->io[addr - IOBASE];
buf[0] = HEX_DIGIT[bval >> 4];
buf[1] = HEX_DIGIT[bval & 0xf];
}
else
// Trying to access the generic SRAM memory
{
// Uzem has an array only for the generic SRAM, so, we need calculate the correct address
addr -= SRAMBASE;
for ( i=0; i<len; i++ )
{
if (global_debug_on)
printf("Reading sram address:%x\n",addr+i);
bval = core->sram[addr+i];
buf[i*2] = HEX_DIGIT[bval >> 4];
buf[i*2+1] = HEX_DIGIT[bval & 0xf];
}
}
}
}
else if ( (addr & MEM_SPACE_MASK) == FLASH_OFFSET )
{
/* addressing flash */
addr = addr & ~MEM_SPACE_MASK; /* remove the offset bits */
is_odd_addr = addr % 2;
i = 0;
if (is_odd_addr)
{
bval = avr_core_flash_read( addr/2 ) >> 8;
buf[i++] = HEX_DIGIT[bval >> 4];
buf[i++] = HEX_DIGIT[bval & 0xf];
addr++;
len--;
}
while (len > 1)
{
wval = avr_core_flash_read( addr/2 );
bval = wval & 0xff;
buf[i++] = HEX_DIGIT[bval >> 4];
buf[i++] = HEX_DIGIT[bval & 0xf];
bval = wval >> 8;
buf[i++] = HEX_DIGIT[bval >> 4];
buf[i++] = HEX_DIGIT[bval & 0xf];
len -= 2;
addr += 2;
}
if (len == 1)
{
bval = avr_core_flash_read( addr/2 ) & 0xff;
buf[i++] = HEX_DIGIT[bval >> 4];
buf[i++] = HEX_DIGIT[bval & 0xf];
}
}
else
{
/* gdb asked for memory space which doesn't exist */
printf( "Invalid memory address: 0x%x.\n", addr );
fprintf(logFile,"Invalid memory address: 0x%x.\n", addr);
snprintf( (char*)buf, len*2, "E%02x", EIO );
}
gdb_send_reply( (char*)buf );
avr_free(buf);
}
void GdbServer::gdb_write_memory( char *pkt )
{
unsigned int addr = 0;
int len = 0;
byte bval;
word wval;
int is_odd_addr;
unsigned int i;
char reply[10];
/* Set the default reply. */
strncpy( reply, "OK", sizeof(reply) );
pkt += gdb_get_addr_len( pkt, ',', ':', &addr, &len );
if ( (addr & MEM_SPACE_MASK) == EEPROM_OFFSET )
{
/* addressing eeprom */
addr = addr & ~MEM_SPACE_MASK; /* remove the offset bits */
// printf( "writing of eeprom not yet implemented: 0x%x.\n", addr );
// snprintf( reply, sizeof(reply), "E%02x", EIO );
while (len>0) {
bval = hex2nib(*pkt++) << 4;
bval += hex2nib(*pkt++);
len--;
core->eeprom[addr] = bval;
addr++;
}
}
else if ( (addr & MEM_SPACE_MASK) == SRAM_OFFSET )
{
/* addressing sram */
addr = addr & ~MEM_SPACE_MASK; /* remove the offset bits */
if (addr >= (SRAMBASE+sramSize))
{
if (global_debug_on)
printf("Sram address:%x invalid\n",addr);
snprintf( (char*)buf, len*2, "E%02x", EIO );
}
// Trying to access one of the registers (below IOBASE)
else if (addr < IOBASE)
{
bval = hex2nib(*pkt++) << 4;
bval += hex2nib(*pkt++);
core->r[addr]=bval;
}
// Trying to access one of the IOs (below SRAMBASE)
else if (addr < SRAMBASE )
{
addr -= IOBASE;
bval = hex2nib(*pkt++) << 4;
bval += hex2nib(*pkt++);
core->io[addr]=bval;
}
else
// Trying to access the generic SRAM memory
{
// Uzem has an array only for the generic SRAM, so, we need calculate the correct address
addr -= SRAMBASE;
for ( i=addr; i < addr+len; i++ )
{
bval = hex2nib(*pkt++) << 4;
bval += hex2nib(*pkt++);
core->sram[i]=bval;
}
}
}
else if ( (addr & MEM_SPACE_MASK) == FLASH_OFFSET )
{
/* addressing flash */
addr = addr & ~MEM_SPACE_MASK; /* remove the offset bits */
is_odd_addr = addr % 2;
if (is_odd_addr)
{
bval = hex2nib(*pkt++) << 4;
bval += hex2nib(*pkt++);
avr_core_flash_write_hi8(addr/2, bval);
len--;
addr++;
}
while (len > 1)
{
wval = hex2nib(*pkt++) << 4; /* low byte first */
wval += hex2nib(*pkt++);
wval += hex2nib(*pkt++) << 12; /* high byte last */
wval += hex2nib(*pkt++) << 8;
avr_core_flash_write( addr/2, wval);
len -= 2;
addr += 2;
}
if ( len == 1 )
{
/* one more byte to write */
bval = hex2nib(*pkt++) << 4;
bval += hex2nib(*pkt++);
avr_core_flash_write_lo8( addr/2, bval );
}
}
else
{
/* gdb asked for memory space which doesn't exist */
printf( "Invalid memory address: 0x%x.\n", addr );
snprintf( reply, sizeof(reply), "E%02x", EIO );
}
gdb_send_reply( reply );
}
/* Format of breakpoint commands (both insert and remove):
"z<t>,<addr>,<length>" - remove break/watch point
"Z<t>,<add>r,<length>" - insert break/watch point
In both cases t can be the following:
t = '0' - software breakpoint
t = '1' - hardware breakpoint
t = '2' - write watch point
t = '3' - read watch point
t = '4' - access watch point
addr is address.
length is in bytes
For a software breakpoint, length specifies the size of the instruction to
be patched. For hardware breakpoints and watchpoints, length specifies the
memory region to be monitored. To avoid potential problems, the operations
should be implemented in an idempotent way. -- GDB 5.0 manual. */
void GdbServer::gdb_break_point( char *pkt )
{
unsigned int addr = 0;
int len = 0;
char z = *(pkt-1); /* get char parser already looked at */
char t = *pkt++;
pkt++; /* skip over first ',' */
//cout << "###############################################" << endl;
gdb_get_addr_len( pkt, ',', '\0', &addr, &len );
switch (t) {
case '0': /* software breakpoint */
/* addr/2 since addr refers to PC */
if ( addr >= progSize )
{
printf( "Attempt to set break at invalid addr\n" );
gdb_send_reply( "E01" );
return;
}
if (z == 'z')
{
//cout << "Try to UNSET a software breakpoint" << endl;
//cout << "at address :" << addr << " with len " << len << endl;
avr_core_remove_breakpoint( addr/2 );
}
else
{
//cout << "Try to SET a software breakpoint" << endl;
//cout << "at address :" << addr << " with len " << len << endl;
avr_core_insert_breakpoint( addr/2 );
}
break;
case '1': /* hardware breakpoint */
//cout << "Try to set a hardware breakpoint" << endl;
//cout << "at address :" << addr << " with len " << len << endl;
gdb_send_reply( "" );
return;
break;
case '2': /* write watchpoint */
//cout << "Try to set a watchpoint" << endl;
//cout << "at address :" << addr << " with len " << len << endl;
gdb_send_reply( "" );
return;
break;
case '3': /* read watchpoint */
//cout << "Try to set a read watchpoint" << endl;
//cout << "at address :" << addr << " with len " << len << endl;
gdb_send_reply( "" );
return;
break;