Explorar o código

started debugger

Nicolas Cannasse %!s(int64=8) %!d(string=hai) anos
pai
achega
4dc6ace409
Modificáronse 10 ficheiros con 164 adicións e 10 borrados
  1. 1 0
      hl.vcxproj
  2. 1 0
      hl.vcxproj.filters
  3. 1 0
      libhl.vcxproj
  4. 3 0
      libhl.vcxproj.filters
  5. 3 4
      libs/ui/ui_win.c
  6. 60 0
      src/debugger.c
  7. 7 0
      src/hl.h
  8. 1 0
      src/hlmodule.h
  9. 30 6
      src/main.c
  10. 57 0
      src/std/thread.c

+ 1 - 0
hl.vcxproj

@@ -238,6 +238,7 @@
   <ItemGroup>
     <ClCompile Include="src\callback.c" />
     <ClCompile Include="src\code.c" />
+    <ClCompile Include="src\debugger.c" />
     <ClCompile Include="src\jit.c" />
     <ClCompile Include="src\main.c" />
     <ClCompile Include="src\module.c" />

+ 1 - 0
hl.vcxproj.filters

@@ -6,6 +6,7 @@
     <ClCompile Include="src\module.c" />
     <ClCompile Include="src\jit.c" />
     <ClCompile Include="src\callback.c" />
+    <ClCompile Include="src\debugger.c" />
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="src\hlmodule.h" />

+ 1 - 0
libhl.vcxproj

@@ -262,6 +262,7 @@
     <ClCompile Include="src\std\socket.c" />
     <ClCompile Include="src\std\string.c" />
     <ClCompile Include="src\std\sys.c" />
+    <ClCompile Include="src\std\thread.c" />
     <ClCompile Include="src\std\types.c" />
     <ClCompile Include="src\std\ucs2.c" />
   </ItemGroup>

+ 3 - 0
libhl.vcxproj.filters

@@ -94,6 +94,9 @@
     <ClCompile Include="src\std\random.c">
       <Filter>std</Filter>
     </ClCompile>
+    <ClCompile Include="src\std\thread.c">
+      <Filter>std</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="include\pcre\config.h">

+ 3 - 4
libs/ui/ui_win.c

@@ -198,7 +198,7 @@ typedef struct {
 	int ticks;
 } vsentinel;
 
-static DWORD WINAPI sentinel_loop( vsentinel *s ) {
+static void sentinel_loop( vsentinel *s ) {
 	int time_ms = (int)((s->timeout * 1000.) / 16.);
 	while( true ) {
 		int k = 0;
@@ -229,7 +229,6 @@ static DWORD WINAPI sentinel_loop( vsentinel *s ) {
 			}
 		}
 	}
-	return 0;
 }
 
 HL_PRIM vsentinel *HL_NAME(ui_start_sentinel)( double timeout, vclosure *c ) {
@@ -240,9 +239,9 @@ HL_PRIM vsentinel *HL_NAME(ui_start_sentinel)( double timeout, vclosure *c ) {
 #	endif
 	s->timeout = timeout;
 	s->ticks = 0;
-	s->original = OpenThread(THREAD_ALL_ACCESS,FALSE,GetCurrentThreadId());
+	s->original = hl_thread_current();
 	s->callback = c->fun;
-	s->thread = CreateThread(NULL,0,sentinel_loop,s,0,NULL);
+	s->thread = hl_thread_start(sentinel_loop,s,false);
 	return s;
 }
 

+ 60 - 0
src/debugger.c

@@ -0,0 +1,60 @@
+/*
+ * Copyright (C)2015-2016 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+#include <hl.h>
+#include <hlmodule.h>
+
+struct _hl_socket;
+typedef struct _hl_socket hl_socket;
+HL_API void hl_socket_init();
+HL_API hl_socket *hl_socket_new( bool udp );
+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 );
+
+static hl_socket *debug_socket;
+
+static void hl_debug_loop( hl_module *m ) {
+	while( true ) {
+		hl_socket *s = hl_socket_accept(debug_socket);
+		printf("CONNECTED\n");
+		hl_socket_close(s);
+	}
+}
+
+bool hl_module_debug( hl_module *m, int port ) {
+	hl_socket *s;
+	hl_socket_init();
+	s = hl_socket_new(false);
+	if( s == NULL ) return false;
+	if( !hl_socket_bind(s,0x0100007F/*127.0.0.1*/,port) || !hl_socket_listen(s, 10) ) {
+		hl_socket_close(s);
+		return false;
+	}
+	debug_socket = s;
+	if( !hl_thread_start(hl_debug_loop, m, false) ) {
+		hl_socket_close(s);
+		return false;
+	}
+	return true;
+}

+ 7 - 0
src/hl.h

@@ -480,6 +480,13 @@ HL_API void *hl_wrapper_call( void *value, void **args, vdynamic *ret );
 HL_API void *hl_dyn_call_obj( vdynamic *obj, hl_type *ft, int hfield, void **args, vdynamic *ret );
 HL_API vdynamic *hl_dyn_call( vclosure *c, vdynamic **args, int nargs );
 
+// ----------------------- THREADS --------------------------------------------------
+
+struct _hl_thread;
+typedef struct _hl_thread hl_thread;
+HL_API hl_thread *hl_thread_start( void *callback, void *param, bool withGC );
+HL_API hl_thread *hl_thread_current();
+
 // ----------------------- ALLOC --------------------------------------------------
 
 #define MEM_HAS_PTR(kind)	(!((kind)&2))

+ 1 - 0
src/hlmodule.h

@@ -105,6 +105,7 @@ const char* hl_op_name( int op );
 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 );
 
 jit_ctx *hl_jit_alloc();
 void hl_jit_free( jit_ctx *ctx );

+ 30 - 6
src/main.c

@@ -33,10 +33,16 @@
 typedef uchar pchar;
 #define pprintf(str,file)	uprintf(USTR(str),file)
 #define pfopen(file,ext) _wfopen(file,USTR(ext))
+#define pcompare wcscmp
+#define ptoi(s)	wcstol(s,NULL,10) 
+#define PSTR(x) USTR(x)
 #else
 typedef char pchar;
 #define pprintf printf
 #define pfopen fopen
+#define pcompare strcmp
+#define ptoi atoi
+#define PSTR(x) x
 #endif
 
 extern void *hl_callback( void *f, hl_type *t, void **args, vdynamic *ret );
@@ -81,23 +87,37 @@ static int throw_handler( int code ) {
 #endif
 
 #ifdef HL_WIN
-int wmain(int argc, uchar *argv[]) {
+int wmain(int argc, pchar *argv[]) {
 #else
-int main(int argc, char *argv[]) {
+int main(int argc, pchar *argv[]) {
 #endif
+	pchar *file = NULL;
+	int debug_port = -1;
 	struct {
 		hl_code *code;
 		hl_module *m;
 		vdynamic *exc;
 	} ctx;
 	hl_trap_ctx trap;
-	if( argc == 1 ) {
-		printf("HL/JIT %d.%d.%d (c)2015-2016 Haxe Foundation\n  Usage : hl <file>\n",HL_VERSION>>8,(HL_VERSION>>4)&15,HL_VERSION&15);
+	argv++;
+	argc--;
+	while( argc-- ) {
+		pchar *arg = *argv++;
+		if( pcompare(arg,PSTR("--debug")) == 0 ) {
+			if( argc-- == 0 ) break;
+			debug_port = ptoi(*argv++);
+			continue;
+		}
+		file = arg;
+		break;
+	}
+	if( file == NULL ) {
+		printf("HL/JIT %d.%d.%d (c)2015-2016 Haxe Foundation\n  Usage : hl [--debug <port>] <file>\n",HL_VERSION>>8,(HL_VERSION>>4)&15,HL_VERSION&15);
 		return 1;
 	}
 	hl_global_init(&ctx);
-	hl_sys_init((void**)(argv + 2),argc - 2);
-	ctx.code = load_code(argv[1]);
+	hl_sys_init((void**)argv,argc);
+	ctx.code = load_code(file);
 	if( ctx.code == NULL )
 		return 1;
 	ctx.m = hl_module_alloc(ctx.code);
@@ -106,6 +126,10 @@ int main(int argc, char *argv[]) {
 	if( !hl_module_init(ctx.m) )
 		return 3;
 	hl_code_free(ctx.code);
+	if( debug_port > 0 && !hl_module_debug(ctx.m,debug_port) ) {
+		fprintf(stderr,"Could not start debugger on port %d",debug_port);
+		return 4;
+	}
 	hl_trap(trap, ctx.exc, on_exception);
 #	ifdef HL_VCC
 	__try {

+ 57 - 0
src/std/thread.c

@@ -0,0 +1,57 @@
+/*
+ * Copyright (C)2005-2016 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+#include <hl.h>
+
+#ifndef HL_WIN
+#	include <pthread.h>
+#endif
+
+HL_PRIM hl_thread *hl_thread_current() {
+#	ifdef HL_WIN
+	return (hl_thread*)GetCurrentThreadId();
+#	else
+	return (hl_thread*)pthread_self();
+#	endif
+}
+
+HL_PRIM hl_thread *hl_thread_start( void *callback, void *param, bool withGC ) {
+	if( withGC ) hl_error("Threads with garbage collector are currently not supported");
+#	ifdef HL_WIN
+	DWORD tid;
+	HANDLE h = CreateThread(NULL,0,callback,param,0,&tid);
+	if( h == NULL )
+		return NULL;
+	CloseHandle(h);
+	return (hl_thread*)tid;
+#	else
+	pthread_t t;
+	pthread_attr_t attr;
+	pthread_attr_init(&attr);
+	pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
+	if( pthread_create(&t,&attr,callback,param) != 0 ) {
+		pthread_attr_destroy(&attr);
+		return NULL;
+	}
+	pthread_attr_destroy(&attr);
+	return (hl_thread*)t;
+#	endif
+}