Merge pull request #960 from ermshiperete/ShowHelp
[mono.git] / mono / utils / mono-proclib.c
index 01b8c1bb50c6f8b1098c1764758f5b5aac28c48e..1303b5b4d448498e2d6afea298f4a5599b047e8b 100644 (file)
@@ -16,6 +16,7 @@
 
 #ifdef HOST_WIN32
 #include <windows.h>
+#include <process.h>
 #endif
 
 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
@@ -104,6 +105,10 @@ mono_process_list (int *size)
        if (size)
                *size = res;
        return buf;
+#elif defined(__HAIKU__)
+       /* FIXME: Add back the code from 9185fcc305e43428d0f40f3ee37c8a405d41c9ae */
+       g_assert_not_reached ();
+       return NULL;
 #else
        const char *name;
        void **buf = NULL;
@@ -407,15 +412,21 @@ get_pid_status_item (int pid, const char *item, MonoProcessError *error, int mul
        struct task_basic_info t_info;
        mach_msg_type_number_t th_count = TASK_BASIC_INFO_COUNT;
 
-       if (task_for_pid (mach_task_self (), pid, &task) != KERN_SUCCESS)
-               RET_ERROR (MONO_PROCESS_ERROR_NOT_FOUND);
+       if (pid == getpid ()) {
+               /* task_for_pid () doesn't work on ios, even for the current process */
+               task = mach_task_self ();
+       } else {
+               if (task_for_pid (mach_task_self (), pid, &task) != KERN_SUCCESS)
+                       RET_ERROR (MONO_PROCESS_ERROR_NOT_FOUND);
+       }
        
        if (task_info (task, TASK_BASIC_INFO, (task_info_t)&t_info, &th_count) != KERN_SUCCESS) {
-               mach_port_deallocate (mach_task_self (), task);
+               if (pid != getpid ())
+                       mach_port_deallocate (mach_task_self (), task);
                RET_ERROR (MONO_PROCESS_ERROR_OTHER);
        }
 
-       if (strcmp (item, "VmRSS") == 0 || strcmp (item, "VmHWM") == 0)
+       if (strcmp (item, "VmRSS") == 0 || strcmp (item, "VmHWM") == 0 || strcmp (item, "VmData") == 0)
                ret = t_info.resident_size;
        else if (strcmp (item, "VmSize") == 0 || strcmp (item, "VmPeak") == 0)
                ret = t_info.virtual_size;
@@ -424,7 +435,8 @@ get_pid_status_item (int pid, const char *item, MonoProcessError *error, int mul
        else
                ret = 0;
 
-       mach_port_deallocate (mach_task_self (), task);
+       if (pid != getpid ())
+               mach_port_deallocate (mach_task_self (), task);
        
        return ret;
 #else
@@ -501,6 +513,18 @@ mono_process_get_data (gpointer pid, MonoProcessData data)
        return mono_process_get_data_with_error (pid, data, &error);
 }
 
+int
+mono_process_current_pid ()
+{
+#if defined(HAVE_UNISTD_H)
+       return (int) getpid ();
+#elif defined(HOST_WIN32)
+       return (int) GetCurrentProcessId ();
+#else
+#error getpid
+#endif
+}
+
 /**
  * mono_cpu_count:
  *
@@ -561,7 +585,7 @@ get_cpu_times (int cpu_id, gint64 *user, gint64 *systemt, gint64 *irq, gint64 *s
        char buf [256];
        char *s;
        int hz = get_user_hz ();
-       guint64 user_ticks, nice_ticks, system_ticks, idle_ticks, iowait_ticks, irq_ticks, sirq_ticks;
+       guint64 user_ticks = 0, nice_ticks = 0, system_ticks = 0, idle_ticks = 0, irq_ticks = 0, sirq_ticks = 0;
        FILE *f = fopen ("/proc/stat", "r");
        if (!f)
                return;
@@ -583,7 +607,7 @@ get_cpu_times (int cpu_id, gint64 *user, gint64 *systemt, gint64 *irq, gint64 *s
                nice_ticks = strtoull (data, &data, 10);
                system_ticks = strtoull (data, &data, 10);
                idle_ticks = strtoull (data, &data, 10);
-               iowait_ticks = strtoull (data, &data, 10);
+               /* iowait_ticks = strtoull (data, &data, 10); */
                irq_ticks = strtoull (data, &data, 10);
                sirq_ticks = strtoull (data, &data, 10);
                break;