Skip to content

Commit e87c61c

Browse files
committed
added: reconnectable debuging session
1 parent 7178223 commit e87c61c

File tree

1 file changed

+40
-35
lines changed

1 file changed

+40
-35
lines changed

interpreter/src/debug/debug_thread.c

+40-35
Original file line numberDiff line numberDiff line change
@@ -564,40 +564,14 @@ static void process_message(debug_state_t *debug, char *msg, unsigned int bytes_
564564
}
565565
}
566566

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) {
592568
// Disable blocking reads and write in the client socket
593569
// Alternatively, a MSG_DONTWAIT could be used below
594570
fcntl(debug->client_fd, F_SETFL, O_NONBLOCK);
595571
fcntl(debug->state_change_pipes[0], F_SETFL, O_NONBLOCK);
596572

597573
printf("[INFO ] Client connected\n");
598574

599-
bh_buffer_init(&debug->send_buffer, bh_heap_allocator(), 1024);
600-
601575
struct pollfd poll_fds[2];
602576
poll_fds[0].fd = debug->state_change_pipes[0];
603577
poll_fds[0].events = POLLIN;
@@ -614,7 +588,7 @@ void *__debug_thread_entry(void * data) {
614588
// do anything.
615589
if (debug->threads[0]->ovm_state->call_depth <= 0) {
616590
debug->debug_thread_running = false;
617-
break;
591+
return;
618592
}
619593

620594
//
@@ -623,11 +597,12 @@ void *__debug_thread_entry(void * data) {
623597
i32 bytes_read = recv(debug->client_fd, command, 4096, 0);
624598
if (bytes_read == 0) {
625599
printf("[INFO ] OVM Debugger connection closed by peer.\n");
626-
debug->debug_thread_running = false;
600+
601+
// Resume all threads when the debugger detaches
627602
bh_arr_each(debug_thread_state_t *, pthread, debug->threads) {
628603
resume_thread(*pthread);
629604
}
630-
break;
605+
return;
631606
}
632607

633608
if (bytes_read == -1) {
@@ -636,13 +611,11 @@ void *__debug_thread_entry(void * data) {
636611

637612
case ECONNRESET:
638613
printf("[ERROR] OVM Debugger connection closed by peer.\n");
639-
debug->debug_thread_running = false;
640-
break;
614+
return;
641615

642616
default:
643617
printf("[ERROR] OVM Debugger crashed when reading from UNIX socket.\n");
644-
debug->debug_thread_running = false;
645-
break;
618+
return;
646619
}
647620
}
648621

@@ -700,9 +673,41 @@ void *__debug_thread_entry(void * data) {
700673
bh_arena_clear(&debug->tmp_arena);
701674
}
702675

703-
close(debug->client_fd);
704676
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);
705706

707+
close(debug->client_fd);
708+
}
709+
710+
close(debug->listen_socket_fd);
706711
unlink(local_addr.sun_path);
707712
return NULL;
708713
}

0 commit comments

Comments
 (0)