1
1
#include " debugger.h"
2
2
3
- #include < inttypes .h>
3
+ #include < unistd .h>
4
4
5
+ #include < algorithm>
6
+ #include < cinttypes>
5
7
#include < cstring>
6
- #include < unistd.h>
7
8
8
9
#include " ../Memory/mem.h"
9
10
#include " ../Utils//util.h"
10
11
#include " ../Utils/macros.h"
11
- #include " ../WARDuino.h"
12
12
13
13
// Debugger
14
14
@@ -180,8 +180,8 @@ bool Debugger::checkDebugMessages(Module *m, RunningState *program_state) {
180
180
break ;
181
181
case interruptOffset: {
182
182
free (interruptData);
183
- printf ( " here " );
184
- dprintf ( this -> socket , " \" { \" offset \" : \" %p \" } \"\n " , (void *)m->bytes );
183
+ dprintf ( this -> socket , " \" { \" offset \" : \" %p \" } \"\n " ,
184
+ (void *)m->bytes );
185
185
break ;
186
186
}
187
187
case interruptRecvState: {
@@ -199,7 +199,7 @@ bool Debugger::checkDebugMessages(Module *m, RunningState *program_state) {
199
199
free (interruptData);
200
200
debug (" sending %s!\n " , receivingData ? " ack" : " done" );
201
201
dprintf (this ->socket , " %s!\n " , receivingData ? " ack" : " done" );
202
- if (!this ->receivingData ){
202
+ if (!this ->receivingData ) {
203
203
debug (" receiving state done\n " );
204
204
}
205
205
}
@@ -216,37 +216,32 @@ bool Debugger::checkDebugMessages(Module *m, RunningState *program_state) {
216
216
}
217
217
218
218
// Private methods
219
- void Debugger::printValue (StackValue *v, int idx, bool end = false ){
220
- char buff[256 ];
221
-
222
- switch (v->value_type ) {
223
- case I32:
224
- snprintf (buff, 255 , R"( "type":"i32","value":%)" PRIi32,
225
- v->value .uint32 );
226
- break ;
227
- case I64:
228
- snprintf (buff, 255 , R"( "type":"i64","value":%)" PRIi64,
229
- v->value .uint64 );
230
- break ;
231
- case F32:
232
- snprintf (buff, 255 , R"( "type":"F32","value":%.7f)" ,
233
- v->value .f32 );
234
- break ;
235
- case F64:
236
- snprintf (buff, 255 , R"( "type":"F64","value":%.7f)" ,
237
- v->value .f64 );
238
- break ;
239
- default :
240
- snprintf (buff, 255 ,
241
- R"( "type":"%02x","value":"%)" PRIx64 " \" " ,
242
- v->value_type , v->value .uint64 );
243
- }
244
- dprintf (this ->socket , R"( {"idx":%d,%s}%s)" , idx, buff,
245
- end ? " " : " ," );
219
+ void Debugger::printValue (StackValue *v, int idx, bool end = false ) {
220
+ char buff[256 ];
246
221
222
+ switch (v->value_type ) {
223
+ case I32:
224
+ snprintf (buff, 255 , R"( "type":"i32","value":%)" PRIi32,
225
+ v->value .uint32 );
226
+ break ;
227
+ case I64:
228
+ snprintf (buff, 255 , R"( "type":"i64","value":%)" PRIi64,
229
+ v->value .uint64 );
230
+ break ;
231
+ case F32:
232
+ snprintf (buff, 255 , R"( "type":"F32","value":%.7f)" , v->value .f32 );
233
+ break ;
234
+ case F64:
235
+ snprintf (buff, 255 , R"( "type":"F64","value":%.7f)" , v->value .f64 );
236
+ break ;
237
+ default :
238
+ snprintf (buff, 255 , R"( "type":"%02x","value":"%)" PRIx64 " \" " ,
239
+ v->value_type , v->value .uint64 );
240
+ }
241
+ dprintf (this ->socket , R"( {"idx":%d,%s}%s)" , idx, buff, end ? " " : " ," );
247
242
}
248
243
249
- uint8_t * Debugger::findOpcode (Module *m, Block *block){
244
+ uint8_t *Debugger::findOpcode (Module *m, Block *block) {
250
245
auto find =
251
246
std::find_if (std::begin (m->block_lookup ), std::end (m->block_lookup ),
252
247
[&](const std::pair<uint8_t *, Block *> &pair) {
@@ -263,7 +258,6 @@ uint8_t * Debugger::findOpcode(Module *m, Block *block){
263
258
return opcode;
264
259
}
265
260
266
-
267
261
void Debugger::handleInterruptRUN (Module *m, RunningState *program_state) {
268
262
dprintf (this ->socket , " GO!\n " );
269
263
if (*program_state == WARDUINOpause && this ->isBreakpoint (m->pc_ptr )) {
@@ -511,12 +505,12 @@ void Debugger::woodDump(Module *m) {
511
505
size_t i = 0 ;
512
506
for (auto bp : this ->breakpoints ) {
513
507
dprintf (this ->socket , R"( "%p"%s)" , bp,
514
- (++i < this ->breakpoints .size ()) ? " ," : " " );
508
+ (++i < this ->breakpoints .size ()) ? " ," : " " );
515
509
}
516
510
dprintf (this ->socket , " ]," );
517
511
518
512
// printf("asked for stack\n");
519
- // stack
513
+ // stack
520
514
dprintf (this ->socket , " \" stack\" :[" );
521
515
char _value_str[256 ];
522
516
for (int i = 0 ; i <= m->sp ; i++) {
@@ -531,7 +525,8 @@ void Debugger::woodDump(Module *m) {
531
525
Frame *f = &m->callstack [i];
532
526
uint8_t *block_key =
533
527
f->block ->block_type == 0 ? nullptr : findOpcode (m, f->block );
534
- dprintf (this ->socket ,
528
+ dprintf (
529
+ this ->socket ,
535
530
R"( {"type":%u,"fidx":"0x%x","sp":%d,"fp":%d,"block_key":"%p", "ra":"%p", "idx":%d}%s)" ,
536
531
f->block ->block_type , f->block ->fidx , f->sp , f->fp , block_key,
537
532
static_cast <void *>(f->ra_ptr ), i, (i < m->csp ) ? " ," : " " );
@@ -542,30 +537,31 @@ void Debugger::woodDump(Module *m) {
542
537
dprintf (this ->socket , " ],\" globals\" :[" );
543
538
for (uint32_t i = 0 ; i < m->global_count ; i++) {
544
539
auto v = m->globals + i;
545
- printValue (v, i, i == (m->global_count - 1 ));
540
+ printValue (v, i, i == (m->global_count - 1 ));
546
541
}
547
542
dprintf (this ->socket , " ]" ); // closing globals
548
543
549
544
// printf("asked for table\n");
550
545
dprintf (this ->socket , " ,\" table\" :{\" max\" :%d, \" init\" :%d, \" elements\" :[" ,
551
- m->table .maximum , m->table .initial );
546
+ m->table .maximum , m->table .initial );
552
547
553
- // write(this->socket, m->table.entries, sizeof(uint32_t) * m->table.size);
548
+ // write(this->socket, m->table.entries, sizeof(uint32_t) * m->table.size);
554
549
dprintf (this ->socket , " ]}" ); // closing table
555
550
556
551
// printf("asked for mem\n");
557
552
// memory
558
553
uint32_t total_elems =
559
554
m->memory .pages * (uint32_t )PAGE_SIZE; // TODO debug PAGE_SIZE
560
- dprintf (this ->socket , " ,\" memory\" :{\" pages\" :%d,\" max\" :%d,\" init\" :%d,\" bytes\" :[" ,
561
- m->memory .pages , m->memory .maximum , m->memory .initial );
555
+ dprintf (this ->socket ,
556
+ " ,\" memory\" :{\" pages\" :%d,\" max\" :%d,\" init\" :%d,\" bytes\" :[" ,
557
+ m->memory .pages , m->memory .maximum , m->memory .initial );
562
558
563
- // write(this->socket, m->memory.bytes, total_elems * sizeof(uint8_t));
559
+ // write(this->socket, m->memory.bytes, total_elems * sizeof(uint8_t));
564
560
dprintf (this ->socket , " ]}" ); // closing memory
565
561
566
-
567
562
// printf("asked for br_table\n");
568
- dprintf (this ->socket , " ,\" br_table\" :{\" size\" :\" 0x%x\" ,\" labels\" :[" , BR_TABLE_SIZE);
563
+ dprintf (this ->socket , " ,\" br_table\" :{\" size\" :\" 0x%x\" ,\" labels\" :[" ,
564
+ BR_TABLE_SIZE);
569
565
write (this ->socket , m->br_table , BR_TABLE_SIZE * sizeof (uint32_t ));
570
566
dprintf (this ->socket , " ]}}\n " );
571
567
}
@@ -649,10 +645,10 @@ void Debugger::freeState(Module *m, uint8_t *interruptData) {
649
645
debug (" max %d init %d current page %d\n " , m->memory .maximum ,
650
646
m->memory .initial , pages);
651
647
printf (" max %d init %d current page %d\n " , m->memory .maximum ,
652
- m->memory .initial , pages);
648
+ m->memory .initial , pages);
653
649
// if(pages !=m->memory.pages){
654
650
// if(m->memory.pages !=0)
655
- if (m->memory .bytes != nullptr ){
651
+ if (m->memory .bytes != nullptr ) {
656
652
free (m->memory .bytes );
657
653
}
658
654
m->memory .bytes =
@@ -687,7 +683,7 @@ bool Debugger::saveState(Module *m, uint8_t *interruptData) {
687
683
switch (*program_state++) {
688
684
case pcState: { // PC
689
685
m->pc_ptr = (uint8_t *)readPointer (&program_state);
690
- /* printf("reciving pc %p\n", static_cast<void*>(m->pc_ptr)); */
686
+ /* printf("receiving pc %p\n", static_cast<void*>(m->pc_ptr)); */
691
687
break ;
692
688
}
693
689
case breakpointsState: { // breakpoints
@@ -729,10 +725,11 @@ bool Debugger::saveState(Module *m, uint8_t *interruptData) {
729
725
printf (" non function block\n " );
730
726
uint8_t *block_key =
731
727
(uint8_t *)readPointer (&program_state);
732
- /* printf("block_key=%p\n", static_cast<void *>(block_key)); */
728
+ /* printf("block_key=%p\n", static_cast<void
729
+ * *>(block_key)); */
733
730
f->block = m->block_lookup [block_key];
734
- if (f->block == nullptr ){
735
- FATAL (" block_lookup cannot be nullptr\n " );
731
+ if (f->block == nullptr ) {
732
+ FATAL (" block_lookup cannot be nullptr\n " );
736
733
}
737
734
}
738
735
}
0 commit comments