Merge pull request #4540 from kumpera/android-changes-part1
[mono.git] / mono / metadata / w32process-unix-osx.c
1 /**
2  * \file
3  */
4
5 #include "w32process.h"
6 #include "w32process-unix-internals.h"
7
8 #ifdef USE_OSX_BACKEND
9
10 #include <errno.h>
11 #include <sys/time.h>
12 #include <sys/proc.h>
13 #include <sys/sysctl.h>
14 #include <sys/utsname.h>
15 #include <mach-o/dyld.h>
16 #include <mach-o/getsect.h>
17
18 /* sys/resource.h (for rusage) is required when using osx 10.3 (but not 10.4) */
19 #ifdef __APPLE__
20 #include <TargetConditionals.h>
21 #include <sys/resource.h>
22 #ifdef HAVE_LIBPROC_H
23 /* proc_name */
24 #include <libproc.h>
25 #endif
26 #endif
27
28 #include "utils/mono-logger-internals.h"
29
30 gchar*
31 mono_w32process_get_name (pid_t pid)
32 {
33         gchar *ret = NULL;
34
35 #if defined (__mono_ppc__) || !defined (TARGET_OSX)
36         size_t size;
37         struct kinfo_proc *pi;
38         gint mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid };
39
40         if (sysctl(mib, 4, NULL, &size, NULL, 0) < 0)
41                 return(ret);
42
43         if ((pi = g_malloc (size)) == NULL)
44                 return(ret);
45
46         if (sysctl (mib, 4, pi, &size, NULL, 0) < 0) {
47                 if (errno == ENOMEM) {
48                         g_free (pi);
49                         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Didn't allocate enough memory for kproc info", __func__);
50                 }
51                 return(ret);
52         }
53
54         if (strlen (pi->kp_proc.p_comm) > 0)
55                 ret = g_strdup (pi->kp_proc.p_comm);
56
57         g_free (pi);
58 #else
59         gchar buf[256];
60         gint res;
61
62         /* No proc name on OSX < 10.5 nor ppc nor iOS */
63         memset (buf, '\0', sizeof(buf));
64         res = proc_name (pid, buf, sizeof(buf));
65         if (res == 0) {
66                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: proc_name failed, error (%d) \"%s\"", __func__, errno, g_strerror (errno));
67                 return NULL;
68         }
69
70         // Fixes proc_name triming values to 15 characters #32539
71         if (strlen (buf) >= MAXCOMLEN - 1) {
72                 gchar path_buf [PROC_PIDPATHINFO_MAXSIZE];
73                 gchar *name_buf;
74                 gint path_len;
75
76                 memset (path_buf, '\0', sizeof(path_buf));
77                 path_len = proc_pidpath (pid, path_buf, sizeof(path_buf));
78
79                 if (path_len > 0 && path_len < sizeof(path_buf)) {
80                         name_buf = path_buf + path_len;
81                         for(;name_buf > path_buf; name_buf--) {
82                                 if (name_buf [0] == '/') {
83                                         name_buf++;
84                                         break;
85                                 }
86                         }
87
88                         if (memcmp (buf, name_buf, MAXCOMLEN - 1) == 0)
89                                 ret = g_strdup (name_buf);
90                 }
91         }
92
93         if (ret == NULL && strlen (buf) > 0)
94                 ret = g_strdup (buf);
95 #endif
96
97         return ret;
98 }
99
100 gchar*
101 mono_w32process_get_path (pid_t pid)
102 {
103 #if defined(__mono_ppc__) || !defined(TARGET_OSX)
104         return mono_w32process_get_name (pid);
105 #else
106         gchar buf [PROC_PIDPATHINFO_MAXSIZE];
107         gint res;
108
109         res = proc_pidpath (pid, buf, sizeof (buf));
110         if (res <= 0)
111                 return NULL;
112         if (buf [0] == '\0')
113                 return NULL;
114         return g_strdup (buf);
115 #endif
116 }
117
118 GSList*
119 mono_w32process_get_modules (pid_t pid)
120 {
121         GSList *ret = NULL;
122         MonoW32ProcessModule *mod;
123         guint32 count = _dyld_image_count ();
124         int i = 0;
125
126         for (i = 0; i < count; i++) {
127 #if SIZEOF_VOID_P == 8
128                 const struct mach_header_64 *hdr;
129                 const struct section_64 *sec;
130 #else
131                 const struct mach_header *hdr;
132                 const struct section *sec;
133 #endif
134                 const char *name;
135
136                 name = _dyld_get_image_name (i);
137 #if SIZEOF_VOID_P == 8
138                 hdr = (const struct mach_header_64*)_dyld_get_image_header (i);
139                 sec = getsectbynamefromheader_64 (hdr, SEG_DATA, SECT_DATA);
140 #else
141                 hdr = _dyld_get_image_header (i);
142                 sec = getsectbynamefromheader (hdr, SEG_DATA, SECT_DATA);
143 #endif
144
145                 /* Some dynlibs do not have data sections on osx (#533893) */
146                 if (sec == 0)
147                         continue;
148
149                 mod = g_new0 (MonoW32ProcessModule, 1);
150                 mod->address_start = GINT_TO_POINTER (sec->addr);
151                 mod->address_end = GINT_TO_POINTER (sec->addr+sec->size);
152                 mod->perms = g_strdup ("r--p");
153                 mod->address_offset = 0;
154                 mod->device = makedev (0, 0);
155                 mod->inode = i;
156                 mod->filename = g_strdup (name);
157
158                 if (g_slist_find_custom (ret, mod, mono_w32process_module_equals) == NULL) {
159                         ret = g_slist_prepend (ret, mod);
160                 } else {
161                         mono_w32process_module_free (mod);
162                 }
163         }
164
165         return g_slist_reverse (ret);
166 }
167
168 #endif