Andreas Schuldei <andreas@schuldei.org> writes:
> I am looking for a straight forward example/tutorial of kvm
> programming.
>
> I looked at ps and top src and do NOT consider them easy to
> understand.
>
> I want to understand the organisation and structure of the
> returned data and how to use it.
You really, really don't want to use kvm.
But if you really need it here is a hack i sometimes use to extract stuff
from the kernel if it's not available through sysctl.
The argument is the symbol in kernel memory you want to look at. I assume that
it's size is sizeof(long).
//art
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <err.h>
#include <nlist.h>
#include <fcntl.h>
#include <kvm.h>
int
main(int argc, char **argv)
{
struct nlist nl[2];
kvm_t *kd;
long i;
if (argc != 2)
err(1, "two arguments required");
nl[0].n_name = argv[1];
nl[1].n_name = NULL;
if ((kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "foo")) == NULL)
err(1, "kvm_open");
if (kvm_nlist(kd, nl) != 0)
errx(1, "kvm_nlist: %s", kvm_geterr(kd));
if (kvm_read(kd, nl[0].n_value, &i, sizeof(i)) != sizeof(i))
errx(1, "kvm_read: %s", kvm_geterr(kd));
kvm_close(kd);
printf("%s: %ld\n", nl[0].n_name, i);
return 0;
}
|