浏览代码

out of process debugger: send only jit info over network

Nicolas Cannasse 8 年之前
父节点
当前提交
ccb98e77a6
共有 3 个文件被更改,包括 39 次插入78 次删除
  1. 30 68
      src/debugger.c
  2. 1 3
      src/hlmodule.h
  3. 8 7
      src/module.c

+ 30 - 68
src/debugger.c

@@ -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;
-}
+}

+ 1 - 3
src/hlmodule.h

@@ -106,6 +106,7 @@ hl_module *hl_module_alloc( hl_code *code );
 int hl_module_init( hl_module *m );
 void hl_module_free( hl_module *m );
 bool hl_module_debug( hl_module *m, int port, bool wait );
+void *hl_module_stack_top();
 
 jit_ctx *hl_jit_alloc();
 void hl_jit_free( jit_ctx *ctx );
@@ -113,6 +114,3 @@ void hl_jit_init( jit_ctx *ctx, hl_module *m );
 int hl_jit_init_callback( jit_ctx *ctx );
 int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f );
 void *hl_jit_code( jit_ctx *ctx, hl_module *m, int *codesize, hl_debug_infos **debug );
-
-int hl_module_capture_stack( void **stack, int size, void **stack_ptr );
-bool hl_module_resolve_pos( void *addr, int *fidx, int *fpos );

+ 8 - 7
src/module.c

@@ -34,7 +34,11 @@ extern void hl_callback_init( void *e );
 static hl_module *cur_module;
 static void *stack_top;
 
-bool hl_module_resolve_pos( void *addr, int *fidx, int *fpos ) {
+void *hl_module_stack_top() {
+	return stack_top;
+}
+
+static bool module_resolve_pos( void *addr, int *fidx, int *fpos ) {
 	int code_pos = ((int)(int_val)((unsigned char*)addr - (unsigned char*)cur_module->jit_code));
 	int min, max;
 	hl_debug_infos *dbg;
@@ -82,7 +86,7 @@ static uchar *module_resolve_symbol( void *addr, uchar *out, int *outSize ) {
 	int pos = 0;
 	int fidx, fpos;
 	hl_function *fdebug;
-	if( !hl_module_resolve_pos(addr,&fidx,&fpos) )
+	if( !module_resolve_pos(addr,&fidx,&fpos) )
 		return NULL;
 	// extract debug info
 	fdebug = cur_module->code->functions + fidx;
@@ -99,7 +103,8 @@ static uchar *module_resolve_symbol( void *addr, uchar *out, int *outSize ) {
 	return out;
 }
 
-int hl_module_capture_stack( void **stack, int size, void **stack_ptr ) {
+static int module_capture_stack( void **stack, int size ) {
+	void **stack_ptr = (void**)&stack;
 	void *stack_bottom = stack_ptr;
 	int count = 0;
 	unsigned char *code = cur_module->jit_code;
@@ -123,10 +128,6 @@ int hl_module_capture_stack( void **stack, int size, void **stack_ptr ) {
 	return count;
 }
 
-static int module_capture_stack( void **stack, int size ) {
-	return hl_module_capture_stack(stack,size,(void**)&stack);
-}
-
 static void hl_init_enum( hl_type_enum *e ) {
 	int i, j;
 	for(i=0;i<e->nconstructs;i++) {