@@ -564,40 +564,14 @@ static void process_message(debug_state_t *debug, char *msg, unsigned int bytes_
564
564
}
565
565
}
566
566
567
- void * __debug_thread_entry (void * data ) {
568
- debug_state_t * debug = data ;
569
- debug -> debug_thread_running = true;
570
-
571
- // Set up socket listener
572
- // Wait for initial connection/handshake before entering loop.
573
-
574
- debug -> listen_socket_fd = socket (AF_UNIX , SOCK_STREAM , 0 );
575
-
576
- struct sockaddr_un local_addr , remote_addr ;
577
- local_addr .sun_family = AF_UNIX ;
578
- strcpy (local_addr .sun_path , debug -> listen_path ); // TODO: Make this dynamic so mulitple servers can exist at a time.
579
- unlink (local_addr .sun_path ); // TODO: Remove this line for the same reason.
580
- int len = strlen (local_addr .sun_path ) + 1 + sizeof (local_addr .sun_family );
581
- bind (debug -> listen_socket_fd , (struct sockaddr * )& local_addr , len );
582
-
583
- //
584
- // Currently, there can only be 1 connected debugger instance at a time.
585
- listen (debug -> listen_socket_fd , 1 );
586
-
587
- len = sizeof (struct sockaddr_un );
588
- debug -> client_fd = accept (debug -> listen_socket_fd , (void * restrict)& remote_addr , (socklen_t * restrict)& len );
589
-
590
- close (debug -> listen_socket_fd );
591
-
567
+ static void debug_session_handler (debug_state_t * debug ) {
592
568
// Disable blocking reads and write in the client socket
593
569
// Alternatively, a MSG_DONTWAIT could be used below
594
570
fcntl (debug -> client_fd , F_SETFL , O_NONBLOCK );
595
571
fcntl (debug -> state_change_pipes [0 ], F_SETFL , O_NONBLOCK );
596
572
597
573
printf ("[INFO ] Client connected\n" );
598
574
599
- bh_buffer_init (& debug -> send_buffer , bh_heap_allocator (), 1024 );
600
-
601
575
struct pollfd poll_fds [2 ];
602
576
poll_fds [0 ].fd = debug -> state_change_pipes [0 ];
603
577
poll_fds [0 ].events = POLLIN ;
@@ -614,7 +588,7 @@ void *__debug_thread_entry(void * data) {
614
588
// do anything.
615
589
if (debug -> threads [0 ]-> ovm_state -> call_depth <= 0 ) {
616
590
debug -> debug_thread_running = false;
617
- break ;
591
+ return ;
618
592
}
619
593
620
594
//
@@ -623,11 +597,12 @@ void *__debug_thread_entry(void * data) {
623
597
i32 bytes_read = recv (debug -> client_fd , command , 4096 , 0 );
624
598
if (bytes_read == 0 ) {
625
599
printf ("[INFO ] OVM Debugger connection closed by peer.\n" );
626
- debug -> debug_thread_running = false;
600
+
601
+ // Resume all threads when the debugger detaches
627
602
bh_arr_each (debug_thread_state_t * , pthread , debug -> threads ) {
628
603
resume_thread (* pthread );
629
604
}
630
- break ;
605
+ return ;
631
606
}
632
607
633
608
if (bytes_read == -1 ) {
@@ -636,13 +611,11 @@ void *__debug_thread_entry(void * data) {
636
611
637
612
case ECONNRESET :
638
613
printf ("[ERROR] OVM Debugger connection closed by peer.\n" );
639
- debug -> debug_thread_running = false;
640
- break ;
614
+ return ;
641
615
642
616
default :
643
617
printf ("[ERROR] OVM Debugger crashed when reading from UNIX socket.\n" );
644
- debug -> debug_thread_running = false;
645
- break ;
618
+ return ;
646
619
}
647
620
}
648
621
@@ -700,9 +673,41 @@ void *__debug_thread_entry(void * data) {
700
673
bh_arena_clear (& debug -> tmp_arena );
701
674
}
702
675
703
- close (debug -> client_fd );
704
676
printf ("[INFO ] Session closed\n" );
677
+ }
678
+
679
+ void * __debug_thread_entry (void * data ) {
680
+ debug_state_t * debug = data ;
681
+ debug -> debug_thread_running = true;
682
+
683
+ // Set up socket listener
684
+ // Wait for initial connection/handshake before entering loop.
685
+
686
+ debug -> listen_socket_fd = socket (AF_UNIX , SOCK_STREAM , 0 );
687
+
688
+ struct sockaddr_un local_addr , remote_addr ;
689
+ local_addr .sun_family = AF_UNIX ;
690
+ strcpy (local_addr .sun_path , debug -> listen_path ); // TODO: Make this dynamic so mulitple servers can exist at a time.
691
+ unlink (local_addr .sun_path ); // TODO: Remove this line for the same reason.
692
+ int len = strlen (local_addr .sun_path ) + 1 + sizeof (local_addr .sun_family );
693
+ bind (debug -> listen_socket_fd , (struct sockaddr * )& local_addr , len );
694
+
695
+ //
696
+ // Currently, there can only be 1 connected debugger instance at a time.
697
+ listen (debug -> listen_socket_fd , 16 );
698
+
699
+ bh_buffer_init (& debug -> send_buffer , bh_heap_allocator (), 1024 );
700
+
701
+ while (debug -> debug_thread_running ) {
702
+ len = sizeof (struct sockaddr_un );
703
+ debug -> client_fd = accept (debug -> listen_socket_fd , (void * restrict)& remote_addr , (socklen_t * restrict)& len );
704
+
705
+ debug_session_handler (debug );
705
706
707
+ close (debug -> client_fd );
708
+ }
709
+
710
+ close (debug -> listen_socket_fd );
706
711
unlink (local_addr .sun_path );
707
712
return NULL ;
708
713
}
0 commit comments