|
@@ -30,86 +30,50 @@ HL_API bool hl_socket_bind( hl_socket *s, int host, int port );
|
|
|
HL_API bool hl_socket_listen( hl_socket *s, int n );
|
|
|
HL_API void hl_socket_close( hl_socket *s );
|
|
|
HL_API hl_socket *hl_socket_accept( hl_socket *s );
|
|
|
-HL_API bool hl_socket_set_timeout( hl_socket *s, double t );
|
|
|
-HL_API void hl_sys_sleep( double t );
|
|
|
HL_API int hl_socket_send( hl_socket *s, vbyte *buf, int pos, int len );
|
|
|
HL_API int hl_socket_recv( hl_socket *s, vbyte *buf, int pos, int len );
|
|
|
+HL_API void hl_sys_sleep( double t );
|
|
|
|
|
|
-static hl_thread *main_thread = NULL;
|
|
|
static hl_socket *debug_socket = NULL;
|
|
|
static hl_socket *client_socket = NULL;
|
|
|
static bool debugger_connected = false;
|
|
|
-static bool kill_on_debug_exit = false;
|
|
|
-
|
|
|
-typedef enum {
|
|
|
- Run = 0,
|
|
|
- Pause = 1,
|
|
|
- Resume = 2,
|
|
|
- Stop = 3,
|
|
|
- Stack = 4,
|
|
|
-} Command;
|
|
|
|
|
|
-static void dbg_send( Command cmd ) {
|
|
|
- vbyte c = (vbyte)cmd;
|
|
|
- hl_socket_send(client_socket, &c, 0, 1);
|
|
|
+#define send hl_send_data
|
|
|
+static void send( void *ptr, int size ) {
|
|
|
+ hl_socket_send(client_socket, ptr, 0, size);
|
|
|
}
|
|
|
|
|
|
-#define STACK_SIZE 256
|
|
|
-
|
|
|
static void hl_debug_loop( hl_module *m ) {
|
|
|
- vbyte cmd;
|
|
|
- void *stack[STACK_SIZE];
|
|
|
- hl_thread_registers *regs = (hl_thread_registers*)malloc(sizeof(int_val) * hl_thread_context_size());
|
|
|
- int i_esp = hl_thread_context_index("esp");
|
|
|
-// int eip = hl_thread_context_index("eip");
|
|
|
+ void *stack_top = hl_module_stack_top();
|
|
|
+ int flags = 0;
|
|
|
+# ifdef HL_64
|
|
|
+ flags |= 1;
|
|
|
+# endif
|
|
|
+ if( sizeof(bool) == 4 ) flags |= 2;
|
|
|
while( true ) {
|
|
|
+ int i;
|
|
|
+ vbyte cmd;
|
|
|
hl_socket *s = hl_socket_accept(debug_socket);
|
|
|
client_socket = s;
|
|
|
- while( true ) {
|
|
|
- if( hl_socket_recv(s,&cmd,0,1) != 1 ) {
|
|
|
- hl_socket_close(s);
|
|
|
- if( kill_on_debug_exit ) exit(-9);
|
|
|
- break;
|
|
|
- }
|
|
|
- switch( cmd ) {
|
|
|
- case Run:
|
|
|
- debugger_connected = true;
|
|
|
- break;
|
|
|
- case Pause:
|
|
|
- hl_thread_pause(main_thread, true);
|
|
|
- break;
|
|
|
- case Resume:
|
|
|
- hl_thread_pause(main_thread, false);
|
|
|
- break;
|
|
|
- case Stack:
|
|
|
- if( !hl_thread_get_context(main_thread, regs) )
|
|
|
- exit(-10);
|
|
|
- {
|
|
|
- int i;
|
|
|
- int size = hl_module_capture_stack(stack,STACK_SIZE,(void**)regs[i_esp]);
|
|
|
- hl_socket_send(s,(vbyte*)&size,0,4);
|
|
|
- for(i=0;i<size;i++) {
|
|
|
- struct {
|
|
|
- int fidx;
|
|
|
- int fpos;
|
|
|
- } inf;
|
|
|
- if( !hl_module_resolve_pos(stack[i],&inf.fidx,&inf.fpos) ) {
|
|
|
- inf.fidx = -1;
|
|
|
- inf.fpos = -1;
|
|
|
- }
|
|
|
- hl_socket_send(s,(vbyte*)&inf,0,8);
|
|
|
- }
|
|
|
- }
|
|
|
- break;
|
|
|
- case Stop:
|
|
|
- exit(-9);
|
|
|
- break;
|
|
|
- default:
|
|
|
- fprintf(stderr,"Unknown debug command [%d]\n",cmd);
|
|
|
- break;
|
|
|
- }
|
|
|
+ send("HLD0",4);
|
|
|
+ send(&flags,4);
|
|
|
+ send(&stack_top,sizeof(void*));
|
|
|
+ send(&m->jit_debug,sizeof(void*));
|
|
|
+ send(&m->codesize,4);
|
|
|
+ send(&m->code->nfunctions,4);
|
|
|
+ for(i=0;i<m->code->nfunctions;i++) {
|
|
|
+ hl_function *f = m->code->functions + i;
|
|
|
+ hl_debug_infos *d = m->jit_debug + i;
|
|
|
+ send(&f->nops,4);
|
|
|
+ send(&d->start,4);
|
|
|
+ send(&d->large,1);
|
|
|
+ send(&d->offsets, (d->large ? sizeof(int) : sizeof(unsigned short)) * (f->nops + 1));
|
|
|
}
|
|
|
+ // wait answer
|
|
|
+ hl_socket_recv(s,&cmd,0,1);
|
|
|
hl_socket_close(s);
|
|
|
+ debugger_connected = true;
|
|
|
+ client_socket = NULL;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -124,9 +88,7 @@ bool hl_module_debug( hl_module *m, int port, bool wait ) {
|
|
|
}
|
|
|
hl_add_root(&debug_socket);
|
|
|
hl_add_root(&client_socket);
|
|
|
- main_thread = hl_thread_current();
|
|
|
debug_socket = s;
|
|
|
- if( wait ) kill_on_debug_exit = true;
|
|
|
if( !hl_thread_start(hl_debug_loop, m, false) ) {
|
|
|
hl_socket_close(s);
|
|
|
return false;
|
|
@@ -136,4 +98,4 @@ bool hl_module_debug( hl_module *m, int port, bool wait ) {
|
|
|
hl_sys_sleep(0.01);
|
|
|
}
|
|
|
return true;
|
|
|
-}
|
|
|
+}
|