openbsd-misc
[Top] [All Lists]

Re: kvm programming tutorial

To: Andreas Schuldei <andreas@schuldei.org>
Subject: Re: kvm programming tutorial
From: Artur Grabowski <art@blahonga.org>
Date: 17 Feb 2001 20:48:05 +0100
Cc: misc <misc@openbsd.org>
In-reply-to: Andreas Schuldei's message of "Sat, 17 Feb 2001 13:26:04 +0100"
References: <20010217132604.A22843@sigrid.schuldei.com>
Sender: owner-misc@openbsd.org
User-agent: Gnus/5.0807 (Gnus v5.8.7) Emacs/20.7
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;
}

<Prev in Thread] Current Thread [Next in Thread>