Additional fixes to Windows log profiler. (#43170)
authormonojenkins <jo.shields+jenkins@xamarin.com>
Tue, 13 Oct 2020 14:46:45 +0000 (10:46 -0400)
committerGitHub <noreply@github.com>
Tue, 13 Oct 2020 14:46:45 +0000 (16:46 +0200)
* Disable popen/pclose.
* Disable reporting using |mprof-report on Windows due to limited piping support.
* Switch to inet_pton instead of deprecated inet_addr.
* Fix Windows socket server since FD is handle and can be bigger than FD_SETSIZE.

Co-authored-by: lateralusX <lateralusX@users.noreply.github.com>
src/mono/mono/metadata/profiler.c
src/mono/mono/profiler/helper.c
src/mono/mono/profiler/log-args.c
src/mono/mono/profiler/log.c

index 8fe24a7..70dd5a5 100644 (file)
@@ -14,6 +14,7 @@
 #include <mono/utils/mono-dl.h>
 #include <mono/utils/mono-error-internals.h>
 #include <mono/utils/mono-logger-internals.h>
+#include <mono/utils/w32subset.h>
 
 MonoProfilerState mono_profiler_state;
 
@@ -148,7 +149,11 @@ mono_profiler_load (const char *desc)
        mname = libname = NULL;
 
        if (!desc || !strcmp ("default", desc))
+#if HAVE_API_SUPPORT_WIN32_PIPE_OPEN_CLOSE && !defined (HOST_WIN32)
                desc = "log:report";
+#else
+               desc = "log";
+#endif
 
        if ((col = strchr (desc, ':')) != NULL) {
                mname = (char *) g_memdup (desc, col - desc + 1);
index d906561..0283672 100644 (file)
@@ -88,10 +88,12 @@ mono_profhelper_add_to_fd_set (fd_set *set, int fd, int *max_fd)
         * the profiler really can't function, and we're better off printing an
         * error and exiting.
         */
+#ifndef HOST_WIN32
        if (fd >= FD_SETSIZE) {
                mono_profiler_printf_err ("File descriptor is out of bounds for fd_set: %d", fd);
                exit (1);
        }
+#endif
 
        FD_SET (fd, set);
 
index e4e183a..baa444c 100644 (file)
@@ -1,6 +1,7 @@
 #include <config.h>
 #include <mono/utils/mono-logger-internals.h>
 #include <mono/utils/mono-proclib.h>
+#include <mono/utils/w32subset.h>
 #include "log.h"
 
 #ifdef HAVE_UNISTD_H
@@ -80,7 +81,11 @@ parse_arg (const char *arg, ProfilerConfig *config)
        } else if (match_option (arg, "nodefaults", NULL)) {
                mono_profiler_printf_err ("The nodefaults option can only be used as the first argument.");
        } else if (match_option (arg, "report", NULL)) {
+#if HAVE_API_SUPPORT_WIN32_PIPE_OPEN_CLOSE && !defined (HOST_WIN32)
                config->do_report = TRUE;
+#else
+               mono_profiler_printf_err ("'report' argument not supported on platform.");
+#endif
        } else if (match_option (arg, "debug", NULL)) {
                config->do_debug = TRUE;
        } else if (match_option (arg, "heapshot", &val)) {
index 440b1ff..bb247f5 100644 (file)
@@ -49,6 +49,7 @@
 #include <mono/utils/mono-error-internals.h>
 #include <mono/utils/mono-publib.h>
 #include <mono/utils/os-event.h>
+#include <mono/utils/w32subset.h>
 #include "log.h"
 #include "helper.h"
 
@@ -310,7 +311,9 @@ struct _MonoProfiler {
        LARGE_INTEGER pcounter_freq;
 #endif
 
+#if HAVE_API_SUPPORT_WIN32_PIPE_OPEN_CLOSE && !defined (HOST_WIN32)
        int pipe_output;
+#endif
        int command_port;
        int server_socket;
 
@@ -2926,8 +2929,8 @@ signal_helper_thread (char c)
        if (client_socket != INVALID_SOCKET) {
                struct sockaddr_in client_addr;
                client_addr.sin_family = AF_INET;
-               client_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
                client_addr.sin_port = htons(log_profiler.command_port);
+               inet_pton (client_addr.sin_family, "127.0.0.1", &client_addr.sin_addr);
 
                gulong non_blocking = 1;
                ioctlsocket (client_socket, FIONBIO, &non_blocking);
@@ -3064,9 +3067,11 @@ log_shutdown (MonoProfiler *prof)
        if (prof->gzfile)
                gzclose (prof->gzfile);
 #endif
+#if HAVE_API_SUPPORT_WIN32_PIPE_OPEN_CLOSE && !defined (HOST_WIN32)
        if (prof->pipe_output)
                pclose (prof->file);
        else
+#endif
                fclose (prof->file);
 
        mono_conc_hashtable_destroy (prof->method_table);
@@ -3300,9 +3305,11 @@ helper_thread (void *arg)
                        int fd = accept (log_profiler.server_socket, NULL, NULL);
 
                        if (fd != -1) {
+#ifndef HOST_WIN32
                                if (fd >= FD_SETSIZE)
                                        mono_profhelper_close_socket_fd (fd);
                                else
+#endif
                                        g_array_append_val (command_sockets, fd);
                        }
                }
@@ -4102,8 +4109,12 @@ create_profiler (const char *args, const char *filename, GPtrArray *filters)
                }
        }
        if (*nf == '|') {
+#if HAVE_API_SUPPORT_WIN32_PIPE_OPEN_CLOSE && !defined (HOST_WIN32)
                log_profiler.file = popen (nf + 1, "w");
                log_profiler.pipe_output = 1;
+#else
+               mono_profiler_printf_err ("Platform doesn't support popen");
+#endif
        } else if (*nf == '#') {
                int fd = strtol (nf + 1, NULL, 10);
                log_profiler.file = fdopen (fd, "a");