5 #include "w32process.h"
6 #include "w32process-unix-internals.h"
13 #include <sys/sysctl.h>
14 #if !defined(__OpenBSD__)
15 #include <sys/utsname.h>
17 #if defined(__FreeBSD__)
18 #include <sys/user.h> /* struct kinfo_proc */
22 #include "utils/mono-logger-internals.h"
23 #include "icall-decl.h"
26 mono_w32process_get_name (pid_t pid
)
30 struct kinfo_proc
*pi
;
33 #if defined(__FreeBSD__)
36 mib
[2] = KERN_PROC_PID
;
38 if (sysctl(mib
, 4, NULL
, &size
, NULL
, 0) < 0) {
39 mono_trace (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER_PROCESS
, "%s: sysctl() failed: %d", __func__
, errno
);
43 if ((pi
= g_malloc (size
)) == NULL
)
46 if (sysctl (mib
, 4, pi
, &size
, NULL
, 0) < 0) {
47 if (errno
== ENOMEM
) {
49 mono_trace (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER_PROCESS
, "%s: Didn't allocate enough memory for kproc info", __func__
);
54 ret
= strlen (pi
->ki_comm
) > 0 ? g_strdup (pi
->ki_comm
) : NULL
;
57 #elif defined(__OpenBSD__)
60 mib
[2] = KERN_PROC_PID
;
62 mib
[4] = sizeof(struct kinfo_proc
);
66 if (sysctl(mib
, 6, NULL
, &size
, NULL
, 0) < 0) {
67 mono_trace (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER_PROCESS
, "%s: sysctl() failed: %d", __func__
, errno
);
71 if ((pi
= g_malloc (size
)) == NULL
)
74 mib
[5] = (int)(size
/ sizeof(struct kinfo_proc
));
76 if ((sysctl (mib
, 6, pi
, &size
, NULL
, 0) < 0) ||
77 (size
!= sizeof (struct kinfo_proc
))) {
78 if (errno
== ENOMEM
) {
85 ret
= strlen (pi
->p_comm
) > 0 ? g_strdup (pi
->p_comm
) : NULL
;
94 mono_w32process_get_path (pid_t pid
)
96 #if defined (__OpenBSD__)
97 // No KERN_PROC_PATHNAME on OpenBSD
98 return mono_w32process_get_name (pid
);
100 gsize path_len
= PATH_MAX
+ 1;
101 gchar path
[PATH_MAX
+ 1];
104 #if defined (__NetBSD__)
105 mib
[1] = KERN_PROC_ARGS
;
107 mib
[3] = KERN_PROC_PATHNAME
;
110 mib
[2] = KERN_PROC_PATHNAME
;
113 if (sysctl (mib
, 4, path
, &path_len
, NULL
, 0) < 0) {
114 mono_trace (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER_PROCESS
, "%s: sysctl() failed: %d", __func__
, errno
);
117 return g_strdup (path
);
123 mono_w32process_get_modules_callback (struct dl_phdr_info
*info
, gsize size
, gpointer ptr
)
125 if (size
< offsetof (struct dl_phdr_info
, dlpi_phnum
) + sizeof (info
->dlpi_phnum
))
128 struct dl_phdr_info
*cpy
= g_calloc (1, sizeof(struct dl_phdr_info
));
132 memcpy(cpy
, info
, sizeof(*info
));
134 g_ptr_array_add ((GPtrArray
*)ptr
, cpy
);
140 mono_w32process_get_modules (pid_t pid
)
143 MonoW32ProcessModule
*mod
;
144 GPtrArray
*dlarray
= g_ptr_array_new();
147 if (dl_iterate_phdr (mono_w32process_get_modules_callback
, dlarray
) < 0)
150 for (i
= 0; i
< dlarray
->len
; i
++) {
151 struct dl_phdr_info
*info
= g_ptr_array_index (dlarray
, i
);
153 mod
= g_new0 (MonoW32ProcessModule
, 1);
154 mod
->address_start
= (gpointer
)(info
->dlpi_addr
+ info
->dlpi_phdr
[0].p_vaddr
);
155 mod
->address_end
= (gpointer
)(info
->dlpi_addr
+ info
->dlpi_phdr
[info
->dlpi_phnum
- 1].p_vaddr
);
156 mod
->perms
= g_strdup ("r--p");
157 mod
->address_offset
= 0;
159 mod
->filename
= g_strdup (info
->dlpi_name
);
161 mono_trace (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER_PROCESS
, "%s: inode=%d, filename=%s, address_start=%p, address_end=%p",
162 __func__
, mod
->inode
, mod
->filename
, mod
->address_start
, mod
->address_end
);
166 if (g_slist_find_custom (ret
, mod
, mono_w32process_module_equals
) == NULL
) {
167 ret
= g_slist_prepend (ret
, mod
);
169 mono_w32process_module_free (mod
);
173 g_ptr_array_free (dlarray
, TRUE
);
175 return g_slist_reverse (ret
);