2 #include "w32process.h"
3 #include "w32process-unix-internals.h"
9 #include <sys/sysctl.h>
10 #if !defined(__OpenBSD__)
11 #include <sys/utsname.h>
13 #if defined(__FreeBSD__)
14 #include <sys/user.h> /* struct kinfo_proc */
19 #include "utils/mono-logger-internals.h"
22 mono_w32process_get_name (pid_t pid
)
26 struct kinfo_proc
*pi
;
29 #if defined(__FreeBSD__)
32 mib
[2] = KERN_PROC_PID
;
34 if (sysctl(mib
, 4, NULL
, &size
, NULL
, 0) < 0) {
35 mono_trace (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: sysctl() failed: %d", __func__
, errno
);
39 if ((pi
= g_malloc (size
)) == NULL
)
42 if (sysctl (mib
, 4, pi
, &size
, NULL
, 0) < 0) {
43 if (errno
== ENOMEM
) {
45 mono_trace (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Didn't allocate enough memory for kproc info", __func__
);
50 ret
= strlen (pi
->ki_comm
) > 0 ? g_strdup (pi
->ki_comm
) : NULL
;
53 #elif defined(__OpenBSD__)
56 mib
[2] = KERN_PROC_PID
;
58 mib
[4] = sizeof(struct kinfo_proc
);
62 if (sysctl(mib
, 6, NULL
, &size
, NULL
, 0) < 0) {
63 mono_trace (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: sysctl() failed: %d", __func__
, errno
);
67 if ((pi
= g_malloc (size
)) == NULL
)
70 mib
[5] = (int)(size
/ sizeof(struct kinfo_proc
));
72 if ((sysctl (mib
, 6, pi
, &size
, NULL
, 0) < 0) ||
73 (size
!= sizeof (struct kinfo_proc
))) {
74 if (errno
== ENOMEM
) {
81 ret
= strlen (pi
->p_comm
) > 0 ? g_strdup (pi
->p_comm
) : NULL
;
90 mono_w32process_get_path (pid_t pid
)
92 return mono_w32process_get_name (pid
);
96 mono_w32process_get_modules_callback (struct dl_phdr_info
*info
, gsize size
, gpointer ptr
)
98 if (size
< offsetof (struct dl_phdr_info
, dlpi_phnum
) + sizeof (info
->dlpi_phnum
))
101 struct dl_phdr_info
*cpy
= g_calloc (1, sizeof(struct dl_phdr_info
));
105 memcpy(cpy
, info
, sizeof(*info
));
107 g_ptr_array_add ((GPtrArray
*)ptr
, cpy
);
113 mono_w32process_get_modules (pid_t pid
)
116 MonoW32ProcessModule
*mod
;
117 GPtrArray
*dlarray
= g_ptr_array_new();
120 if (dl_iterate_phdr (mono_w32process_get_modules_callback
, dlarray
) < 0)
123 for (i
= 0; i
< dlarray
->len
; i
++) {
124 struct dl_phdr_info
*info
= g_ptr_array_index (dlarray
, i
);
126 mod
= g_new0 (MonoW32ProcessModule
, 1);
127 mod
->address_start
= (gpointer
)(info
->dlpi_addr
+ info
->dlpi_phdr
[0].p_vaddr
);
128 mod
->address_end
= (gpointer
)(info
->dlpi_addr
+ info
->dlpi_phdr
[info
->dlpi_phnum
- 1].p_vaddr
);
129 mod
->perms
= g_strdup ("r--p");
130 mod
->address_offset
= 0;
132 mod
->filename
= g_strdup (info
->dlpi_name
);
134 mono_trace (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: inode=%d, filename=%s, address_start=%p, address_end=%p",
135 __func__
, mod
->inode
, mod
->filename
, mod
->address_start
, mod
->address_end
);
139 if (g_slist_find_custom (ret
, mod
, mono_w32process_module_equals
) == NULL
) {
140 ret
= g_slist_prepend (ret
, mod
);
142 mono_w32process_module_free (mod
);
146 g_ptr_array_free (dlarray
, TRUE
);
148 return g_slist_reverse (ret
);