Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / mini / mini-windows.c
index 2aa3a7b75d1bc21d177500be8388a7448a6991fd..efb4739cde2c95866f9dcb34a2643fcf933a76d3 100644 (file)
@@ -1,5 +1,6 @@
-/*
- * mini-posix.c: POSIX signal handling support for Mono.
+/**
+ * \file
+ * POSIX signal handling support for Mono.
  *
  * Authors:
  *   Mono Team (mono-list@lists.ximian.com)
@@ -13,6 +14,8 @@
 #include <config.h>
 #include <signal.h>
 #include <math.h>
+#include <conio.h>
+#include <assert.h>
 
 #include <mono/metadata/assembly.h>
 #include <mono/metadata/loader.h>
@@ -24,8 +27,6 @@
 #include <mono/metadata/threads.h>
 #include <mono/metadata/appdomain.h>
 #include <mono/metadata/debug-helpers.h>
-#include <mono/io-layer/io-layer.h>
-#include "mono/metadata/profiler.h"
 #include <mono/metadata/profiler-private.h>
 #include <mono/metadata/mono-config.h>
 #include <mono/metadata/environment.h>
 #include <mmsystem.h>
 #endif
 
+#define MONO_HANDLER_DELIMITER ','
+#define MONO_HANDLER_DELIMITER_LEN G_N_ELEMENTS(MONO_HANDLER_DELIMITER)-1
+
+#define MONO_HANDLER_ATEXIT_WAIT_KEYPRESS "atexit-waitkeypress"
+#define MONO_HANDLER_ATEXIT_WAIT_KEYPRESS_LEN G_N_ELEMENTS(MONO_HANDLER_ATEXIT_WAIT_KEYPRESS)-1
+
+// Typedefs used to setup handler table.
+typedef void (*handler)(void);
+
+typedef struct {
+       const char * cmd;
+       const int cmd_len;
+       handler handler;
+} HandlerItem;
+
+#if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
+/**
+* atexit_wait_keypress:
+*
+* This function is installed as an atexit function making sure that the console is not terminated before the end user has a chance to read the result.
+* This can be handy in debug scenarios (running from within the debugger) since an exit of the process will close the console window
+* without giving the end user a chance to look at the output before closed.
+*/
+static void
+atexit_wait_keypress (void)
+{
+
+       fflush (stdin);
+
+       printf ("Press any key to continue . . . ");
+       fflush (stdout);
+
+       _getch ();
+
+       return;
+}
+
+/**
+* install_atexit_wait_keypress:
+*
+* This function installs the wait keypress exit handler.
+*/
+static void
+install_atexit_wait_keypress (void)
+{
+       atexit (atexit_wait_keypress);
+       return;
+}
+
+#else
+
+/**
+* install_atexit_wait_keypress:
+*
+* Not supported on WINAPI family.
+*/
+static void
+install_atexit_wait_keypress (void)
+{
+       return;
+}
+
+#endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
+
+// Table describing handlers that can be installed at process startup. Adding a new handler can be done by adding a new item to the table together with an install handler function.
+const HandlerItem g_handler_items[] = { { MONO_HANDLER_ATEXIT_WAIT_KEYPRESS, MONO_HANDLER_ATEXIT_WAIT_KEYPRESS_LEN, install_atexit_wait_keypress },
+                                       { NULL, 0, NULL } };
+
+/**
+ * get_handler_arg_len:
+ * @handlers: Get length of next handler.
+ *
+ * This function calculates the length of next handler included in argument.
+ *
+ * Returns: The length of next handler, if available.
+ */
+static size_t
+get_next_handler_arg_len (const char *handlers)
+{
+       assert (handlers != NULL);
+
+       size_t current_len = 0;
+       const char *handler = strchr (handlers, MONO_HANDLER_DELIMITER);
+       if (handler != NULL) {
+               // Get length of next handler arg.
+               current_len = (handler - handlers);
+       } else {
+               // Consume rest as length of next handler arg.
+               current_len = strlen (handlers);
+       }
+
+       return current_len;
+}
+
+/**
+ * install_custom_handler:
+ * @handlers: Handlers included in --handler argument, example "atexit-waitkeypress,someothercmd,yetanothercmd".
+ * @handler_arg_len: Output, length of consumed handler.
+ *
+ * This function installs the next handler included in @handlers parameter.
+ *
+ * Returns: TRUE on successful install, FALSE on failure or unrecognized handler.
+ */
+static gboolean
+install_custom_handler (const char *handlers, size_t *handler_arg_len)
+{
+       gboolean result = FALSE;
+
+       assert (handlers != NULL);
+       assert (handler_arg_len);
+
+       *handler_arg_len = get_next_handler_arg_len (handlers);
+       for (int current_item = 0; current_item < G_N_ELEMENTS (g_handler_items); ++current_item) {
+               const HandlerItem * handler_item = &g_handler_items [current_item];
+
+               if (handler_item->cmd == NULL)
+                       continue;
+
+               if (*handler_arg_len == handler_item->cmd_len && strncmp (handlers, handler_item->cmd, *handler_arg_len) == 0) {
+                       assert (handler_item->handler != NULL);
+                       handler_item->handler ();
+                       result = TRUE;
+                       break;
+               }
+       }
+       return result;
+}
+
 void
 mono_runtime_install_handlers (void)
 {
@@ -69,6 +198,44 @@ mono_runtime_install_handlers (void)
 #endif
 }
 
+gboolean
+mono_runtime_install_custom_handlers (const char *handlers)
+{
+       gboolean result = FALSE;
+
+       assert (handlers != NULL);
+       while (*handlers != '\0') {
+               size_t handler_arg_len = 0;
+
+               result = install_custom_handler (handlers, &handler_arg_len);
+               handlers += handler_arg_len;
+
+               if (*handlers == MONO_HANDLER_DELIMITER)
+                       handlers++;
+               if (!result)
+                       break;
+       }
+
+       return result;
+}
+
+void
+mono_runtime_install_custom_handlers_usage (void)
+{
+       fprintf (stdout,
+                "Custom Handlers:\n"
+                "   --handlers=HANDLERS            Enable handler support, HANDLERS is a comma\n"
+                "                                  separated list of available handlers to install.\n"
+                "\n"
+#if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
+                "HANDLERS is composed of:\n"
+                "    atexit-waitkeypress           Install an atexit handler waiting for a keypress\n"
+                "                                  before exiting process.\n");
+#else
+                "No handlers supported on current platform.\n");
+#endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
+}
+
 void
 mono_runtime_cleanup_handlers (void)
 {
@@ -87,7 +254,7 @@ mono_runtime_cleanup_handlers (void)
 gboolean
 MONO_SIG_HANDLER_SIGNATURE (mono_chain_signal)
 {
-       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
        jit_tls->mono_win_chained_exception_needs_run = TRUE;
        return TRUE;
 }
@@ -103,11 +270,15 @@ thread_timer_expired (HANDLE thread)
 
        context.ContextFlags = CONTEXT_CONTROL;
        if (GetThreadContext (thread, &context)) {
+               guchar *ip;
+
 #ifdef _WIN64
-               mono_profiler_stat_hit ((guchar *) context.Rip, &context);
+               ip = (guchar *) context.Rip;
 #else
-               mono_profiler_stat_hit ((guchar *) context.Eip, &context);
+               ip = (guchar *) context.Eip;
 #endif
+
+               MONO_PROFILER_RAISE (sample_hit, (ip, &context));
        }
 }