Bring back changes made for vmmap now that it's back in.

This commit is contained in:
ajacoutot 2012-03-11 08:45:24 +00:00
parent f6267ae965
commit 1c9e2a7cd0
2 changed files with 235 additions and 10 deletions

View File

@ -1,11 +1,11 @@
# $OpenBSD: Makefile,v 1.93 2012/01/09 16:45:20 robert Exp $
# $OpenBSD: Makefile,v 1.94 2012/03/11 08:45:24 ajacoutot Exp $
COMMENT= portable library for obtaining system information
GNOME_VERSION= 2.28.4
GNOME_PROJECT= libgtop
PKGNAME= libgtop2-${VERSION}
REVISION= 5
REVISION= 6
SHARED_LIBS= gtop-2.0 9.0 # .9.0

View File

@ -1,4 +1,4 @@
$OpenBSD: patch-sysdeps_openbsd_procmap_c,v 1.6 2012/01/22 11:55:10 ajacoutot Exp $
$OpenBSD: patch-sysdeps_openbsd_procmap_c,v 1.7 2012/03/11 08:45:24 ajacoutot Exp $
From b11aa1517e203e36a8901f03ea57c50a25dfbfc7 Mon Sep 17 00:00:00 2001
From: Antoine Jacoutot <ajacoutot@gnome.org>
@ -11,28 +11,148 @@ Date: Sun Sep 18 13:41:20 2011 +0200
Extend comment a bit regarding processes dying before we get to kvm_getproc2()
--- sysdeps/openbsd/procmap.c.orig Sun Jul 24 21:13:56 2011
+++ sysdeps/openbsd/procmap.c Mon Jan 9 18:37:24 2012
@@ -64,13 +64,17 @@ _glibtop_init_proc_map_p (glibtop *server)
+++ sysdeps/openbsd/procmap.c Sun Mar 11 09:41:59 2012
@@ -1,4 +1,4 @@
-/* $OpenBSD: patch-sysdeps_openbsd_procmap_c,v 1.7 2012/03/11 08:45:24 ajacoutot Exp $ */
+/* $OpenBSD: patch-sysdeps_openbsd_procmap_c,v 1.7 2012/03/11 08:45:24 ajacoutot Exp $ */
/* Copyright (C) 1998 Joshua Sled
This file is part of LibGTop 1.0.
@@ -29,6 +29,7 @@
#include <glibtop_suid.h>
#include <kvm.h>
+#include <stdlib.h>
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/resource.h>
@@ -56,6 +57,12 @@ static const unsigned long _glibtop_sysdeps_map_entry
(1L << GLIBTOP_MAP_ENTRY_OFFSET) + (1L << GLIBTOP_MAP_ENTRY_PERM) +
(1L << GLIBTOP_MAP_ENTRY_INODE) + (1L << GLIBTOP_MAP_ENTRY_DEVICE);
+/* Local helper functions. */
+
+ssize_t load_vmmap_entries(glibtop*, unsigned long, struct vm_map_entry**,
+ struct vm_map_entry*);
+void unload_vmmap_entries(struct vm_map_entry *);
+
/* Init function. */
void
@@ -64,22 +71,107 @@ _glibtop_init_proc_map_p (glibtop *server)
server->sysdeps.proc_map = _glibtop_sysdeps_proc_map;
}
-/* Provides detailed information about a process. */
+/*
+ * Download vmmap_entries from the kernel into our address space.
+ * We fix up the addr tree while downloading.
+ *
+ * Returns: the size of the tree on succes, or -1 on failure.
+ * On failure, *rptr needs to be passed to unload_vmmap_entries to free
+ * the lot.
+ */
+ssize_t
+load_vmmap_entries(glibtop *server, unsigned long kptr,
+ struct vm_map_entry **rptr, struct vm_map_entry *parent)
+{
+ struct vm_map_entry *entry;
+ unsigned long left_kptr, right_kptr;
+ ssize_t left_sz;
+ ssize_t right_sz;
+ if (kptr == 0)
+ return 0;
+
+ /* Need space. */
+ entry = malloc(sizeof(*entry));
+ if (entry == NULL)
+ return -1;
+
+ /* Download entry at kptr. */
+ if (kvm_read (server->machine.kd, kptr,
+ (char *)entry, sizeof(*entry)) != sizeof(*entry)) {
+ free(entry);
+ return -1;
+ }
+
+ /*
+ * Update addr pointers to have sane values in this address space.
+ * We save the kernel pointers in {left,right}_kptr, so we have them
+ * available to download children.
+ */
+ left_kptr = (unsigned long) RB_LEFT(entry, daddrs.addr_entry);
+ right_kptr = (unsigned long) RB_RIGHT(entry, daddrs.addr_entry);
+ RB_LEFT(entry, daddrs.addr_entry) =
+ RB_RIGHT(entry, daddrs.addr_entry) = NULL;
+ /* Fill in parent pointer. */
+ RB_PARENT(entry, daddrs.addr_entry) = parent;
+
+ /*
+ * Consistent state reached, fill in *rptr.
+ */
+ *rptr = entry;
+
+ /*
+ * Download left, right.
+ * On failure, our map is in a state that can be handled by
+ * unload_vmmap_entries.
+ */
+ left_sz = load_vmmap_entries(server, left_kptr,
+ &RB_LEFT(entry, daddrs.addr_entry), entry);
+ if (left_sz == -1)
+ return -1;
+ right_sz = load_vmmap_entries(server, right_kptr,
+ &RB_RIGHT(entry, daddrs.addr_entry), entry);
+ if (right_sz == -1)
+ return -1;
+
+ return 1 + left_sz + right_sz;
+}
+
+/*
+ * Free the vmmap entries in the given tree.
+ */
+void
+unload_vmmap_entries(struct vm_map_entry *entry)
+{
+ if (entry == NULL)
+ return;
+
+ unload_vmmap_entries(RB_LEFT(entry, daddrs.addr_entry));
+ unload_vmmap_entries(RB_RIGHT(entry, daddrs.addr_entry));
+ free(entry);
+}
+
+/*
+ * Provides detailed information about a process.
+ * Due to the fact we are only requested info about one process, it's possible
+ * the process has been reaped before we get to kvm_getprocs. Tough luck.
+ */
+
glibtop_map_entry *
glibtop_get_proc_map_p (glibtop *server, glibtop_proc_map *buf,
pid_t pid)
{
- struct kinfo_proc2 *pinfo;
- struct vm_map_entry entry, *first;
+ struct kinfo_proc *pinfo;
struct vm_map_entry entry, *first;
+ struct vm_map_entry *entry;
+ struct uvm_map_addr root;
struct vmspace vmspace;
struct vnode vnode;
@@ -91,7 +95,7 @@ glibtop_get_proc_map_p (glibtop *server, glibtop_proc_
struct inode inode;
+ ssize_t nentries;
GArray *maps = g_array_sized_new(FALSE, FALSE,
sizeof(glibtop_map_entry),
100);
- int count, i = 0;
- int update = 0;
+ int count = 0;
glibtop_init_p (server, (1L << GLIBTOP_SYSDEPS_PROC_MAP), 0);
@@ -91,7 +183,7 @@ glibtop_get_proc_map_p (glibtop *server, glibtop_proc_
glibtop_suid_enter (server);
/* Get the process data */
@ -41,7 +161,32 @@ Extend comment a bit regarding processes dying before we get to kvm_getproc2()
if ((pinfo == NULL) || (count < 1)) {
glibtop_warn_io_r (server, "kvm_getprocs (%d)", pid);
return (glibtop_map_entry*) g_array_free(maps, TRUE);
@@ -124,7 +128,8 @@ glibtop_get_proc_map_p (glibtop *server, glibtop_proc_
@@ -104,16 +196,18 @@ glibtop_get_proc_map_p (glibtop *server, glibtop_proc_
(char *) &vmspace, sizeof (vmspace)) != sizeof (vmspace))
glibtop_error_io_r (server, "kvm_read (vmspace)");
- first = vmspace.vm_map.header.next;
-
- if (kvm_read (server->machine.kd,
- (unsigned long) vmspace.vm_map.header.next,
- (char *) &entry, sizeof (entry)) != sizeof (entry))
+ RB_INIT(&root);
+ nentries = load_vmmap_entries(server,
+ (unsigned long) RB_ROOT(&vmspace.vm_map.addr),
+ &RB_ROOT(&root), NULL);
+ if (nentries == -1) {
+ unload_vmmap_entries(RB_ROOT(&root));
glibtop_error_io_r (server, "kvm_read (entry)");
+ }
/* Allocate space. */
- buf->number = vmspace.vm_map.nentries;
+ buf->number = nentries;
buf->size = sizeof (glibtop_map_entry);
buf->total = buf->number * buf->size;
@@ -124,34 +218,26 @@ glibtop_get_proc_map_p (glibtop *server, glibtop_proc_
/* I tested this a few times with `mmap'; as soon as you write
* to the mmap'ed area, the object type changes from OBJT_VNODE
@ -49,5 +194,85 @@ Extend comment a bit regarding processes dying before we get to kvm_getproc2()
+ * to OBJT_DEFAULT so it seems this really works.
+ */
do {
- do {
+ RB_FOREACH(entry, uvm_map_addr, &root) {
glibtop_map_entry *mentry;
unsigned long inum, dev;
guint len;
- if (update) {
- if (kvm_read (server->machine.kd,
- (unsigned long) entry.next,
- &entry, sizeof (entry)) != sizeof (entry))
- glibtop_error_io_r (server, "kvm_read (entry)");
- } else {
- update = 1;
- }
-
- if (UVM_ET_ISSUBMAP (&entry))
+ if (UVM_ET_ISSUBMAP(entry))
continue;
-
- if (!entry.object.uvm_obj)
+ if (!entry->object.uvm_obj)
continue;
/* We're only interested in vnodes */
if (kvm_read (server->machine.kd,
- (unsigned long) entry.object.uvm_obj,
+ (unsigned long) entry->object.uvm_obj,
&vnode, sizeof (vnode)) != sizeof (vnode)) {
glibtop_warn_io_r (server, "kvm_read (vnode)");
+ unload_vmmap_entries(RB_ROOT(&root));
return (glibtop_map_entry*) g_array_free(maps, TRUE);
}
@@ -176,21 +262,21 @@ glibtop_get_proc_map_p (glibtop *server, glibtop_proc_
mentry->flags = _glibtop_sysdeps_map_entry;
- mentry->start = (guint64) entry.start;
- mentry->end = (guint64) entry.end;
- mentry->offset = (guint64) entry.offset;
+ mentry->start = (guint64) entry->start;
+ mentry->end = (guint64) entry->end;
+ mentry->offset = (guint64) entry->offset;
mentry->device = (guint64) dev;
mentry->inode = (guint64) inum;
mentry->perm = (guint64) 0;
- if (entry.protection & VM_PROT_READ)
+ if (entry->protection & VM_PROT_READ)
mentry->perm |= GLIBTOP_MAP_PERM_READ;
- if (entry.protection & VM_PROT_WRITE)
+ if (entry->protection & VM_PROT_WRITE)
mentry->perm |= GLIBTOP_MAP_PERM_WRITE;
- if (entry.protection & VM_PROT_EXECUTE)
+ if (entry->protection & VM_PROT_EXECUTE)
mentry->perm |= GLIBTOP_MAP_PERM_EXECUTE;
- } while (entry.next != first);
+ }
buf->flags = _glibtop_sysdeps_proc_map;
@@ -198,5 +284,18 @@ glibtop_get_proc_map_p (glibtop *server, glibtop_proc_
buf->size = sizeof (glibtop_map_entry);
buf->total = buf->number * buf->size;
+ unload_vmmap_entries(RB_ROOT(&root));
return (glibtop_map_entry*) g_array_free(maps, FALSE);
}
+
+/*
+ * Don't implement address comparison.
+ */
+static __inline int
+no_impl(void *p, void *q)
+{
+ abort(); /* Should not be called. */
+ return 0;
+}
+
+RB_GENERATE(uvm_map_addr, vm_map_entry, daddrs.addr_entry, no_impl);