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