2 #include "w32process.h"
3 #include "w32process-unix-internals.h"
10 #include <sys/sysctl.h>
11 #include <sys/utsname.h>
12 #include <mach-o/dyld.h>
13 #include <mach-o/getsect.h>
15 /* sys/resource.h (for rusage) is required when using osx 10.3 (but not 10.4) */
17 #include <TargetConditionals.h>
18 #include <sys/resource.h>
25 #include "utils/mono-logger-internals.h"
28 mono_w32process_get_name (pid_t pid)
32 #if defined (__mono_ppc__) || !defined (TARGET_OSX)
34 struct kinfo_proc *pi;
35 gint mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid };
37 if (sysctl(mib, 4, NULL, &size, NULL, 0) < 0)
40 if ((pi = g_malloc (size)) == NULL)
43 if (sysctl (mib, 4, pi, &size, NULL, 0) < 0) {
44 if (errno == ENOMEM) {
46 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Didn't allocate enough memory for kproc info", __func__);
51 if (strlen (pi->kp_proc.p_comm) > 0)
52 ret = g_strdup (pi->kp_proc.p_comm);
58 /* No proc name on OSX < 10.5 nor ppc nor iOS */
59 memset (buf, '\0', sizeof(buf));
60 proc_name (pid, buf, sizeof(buf));
62 // Fixes proc_name triming values to 15 characters #32539
63 if (strlen (buf) >= MAXCOMLEN - 1) {
64 gchar path_buf [PROC_PIDPATHINFO_MAXSIZE];
68 memset (path_buf, '\0', sizeof(path_buf));
69 path_len = proc_pidpath (pid, path_buf, sizeof(path_buf));
71 if (path_len > 0 && path_len < sizeof(path_buf)) {
72 name_buf = path_buf + path_len;
73 for(;name_buf > path_buf; name_buf--) {
74 if (name_buf [0] == '/') {
80 if (memcmp (buf, name_buf, MAXCOMLEN - 1) == 0)
81 ret = g_strdup (name_buf);
85 if (ret == NULL && strlen (buf) > 0)
93 mono_w32process_get_path (pid_t pid)
95 #if defined(__mono_ppc__) || !defined(TARGET_OSX)
96 return mono_w32process_get_name (pid);
98 gchar buf [PROC_PIDPATHINFO_MAXSIZE];
101 res = proc_pidpath (pid, buf, sizeof (buf));
106 return g_strdup (buf);
111 mono_w32process_get_modules (pid_t pid)
114 MonoW32ProcessModule *mod;
115 guint32 count = _dyld_image_count ();
118 for (i = 0; i < count; i++) {
119 #if SIZEOF_VOID_P == 8
120 const struct mach_header_64 *hdr;
121 const struct section_64 *sec;
123 const struct mach_header *hdr;
124 const struct section *sec;
128 name = _dyld_get_image_name (i);
129 #if SIZEOF_VOID_P == 8
130 hdr = (const struct mach_header_64*)_dyld_get_image_header (i);
131 sec = getsectbynamefromheader_64 (hdr, SEG_DATA, SECT_DATA);
133 hdr = _dyld_get_image_header (i);
134 sec = getsectbynamefromheader (hdr, SEG_DATA, SECT_DATA);
137 /* Some dynlibs do not have data sections on osx (#533893) */
141 mod = g_new0 (MonoW32ProcessModule, 1);
142 mod->address_start = GINT_TO_POINTER (sec->addr);
143 mod->address_end = GINT_TO_POINTER (sec->addr+sec->size);
144 mod->perms = g_strdup ("r--p");
145 mod->address_offset = 0;
146 mod->device = makedev (0, 0);
148 mod->filename = g_strdup (name);
150 if (g_slist_find_custom (ret, mod, mono_w32process_module_equals) == NULL) {
151 ret = g_slist_prepend (ret, mod);
153 mono_w32process_module_free (mod);
157 return g_slist_reverse (ret);