Home | History | Annotate | Line # | Download | only in libutil
kinfo_getvmmap.c revision 1.1
      1  1.1  christos #include <sys/cdefs.h>
      2  1.1  christos #ifdef __FBSDID
      3  1.1  christos __FBSDID("$FreeBSD: head/lib/libutil/kinfo_getvmmap.c 186512 2008-12-27 11:12:23Z rwatson $");
      4  1.1  christos #endif
      5  1.1  christos __RCSID("$NetBSD: kinfo_getvmmap.c,v 1.1 2015/09/24 14:39:20 christos Exp $");
      6  1.1  christos 
      7  1.1  christos #include <sys/param.h>
      8  1.1  christos #include <sys/user.h>
      9  1.1  christos #include <sys/sysctl.h>
     10  1.1  christos #include <stdlib.h>
     11  1.1  christos #include <string.h>
     12  1.1  christos #include <util.h>
     13  1.1  christos 
     14  1.1  christos struct kinfo_vmentry *
     15  1.1  christos kinfo_getvmmap(pid_t pid, size_t *cntp)
     16  1.1  christos {
     17  1.1  christos 	int mib[5];
     18  1.1  christos 	int error;
     19  1.1  christos 	size_t len;
     20  1.1  christos 	struct kinfo_vmentry *kiv;
     21  1.1  christos 
     22  1.1  christos 	*cntp = 0;
     23  1.1  christos 	len = 0;
     24  1.1  christos 	mib[0] = CTL_VM;
     25  1.1  christos 	mib[1] = VM_PROC;
     26  1.1  christos 	mib[2] = VM_PROC_MAP;
     27  1.1  christos 	mib[3] = pid;
     28  1.1  christos 	mib[4] = sizeof(*kiv);
     29  1.1  christos 
     30  1.1  christos 	error = sysctl(mib, __arraycount(mib), NULL, &len, NULL, 0);
     31  1.1  christos 	if (error)
     32  1.1  christos 		return NULL;
     33  1.1  christos 
     34  1.1  christos 	len = len * 4 / 3;
     35  1.1  christos 
     36  1.1  christos 	kiv = malloc(len);
     37  1.1  christos 	if (kiv == NULL)
     38  1.1  christos 		return NULL;
     39  1.1  christos 
     40  1.1  christos 	error = sysctl(mib, __arraycount(mib), kiv, &len, NULL, 0);
     41  1.1  christos 	if (error) {
     42  1.1  christos 		free(kiv);
     43  1.1  christos 		return NULL;
     44  1.1  christos 	}
     45  1.1  christos 
     46  1.1  christos 	*cntp = len / sizeof(*kiv);
     47  1.1  christos 	return kiv;	/* Caller must free() return value */
     48  1.1  christos }
     49