Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 3 Aug 2001 14:16:17 -0700
From:      Tabor Kelly <pdxmax@dsl-only.net>
To:        freebsd-hackers@freebsd.org
Subject:   problems with kvm_nlist()
Message-ID:  <146809363.20010803141617@dsl-only.net>

next in thread | raw e-mail | index | archive | help

[-- Attachment #1 --]
I am using kvm_nlist() in one of my programs. I have written a smaller
test program for use here. Either kvm_nlist() has a bug or I am very
confused.

According to kvm_nlist(3), kvm_nlist should go through an array of
nlist structures and fill out each structure with that symbol's
information. Well, as far as I can tell, it fills out all of the
structures with the information from the symbol in the first
structure.

Has anybody else seen this behavior? Attached is a file named
test.cpp that should reproduce this behavior. If you want to play with
this behavior, change what structure is pointed to when calling
kvm_nlist on line 38 of the program source.

Also, be aware that you will have to make the following modifications
to the file permissions after you compile it (if you don't want to run
it as root):

# chown root:kmem a.out
# chmod g+s a.out

Thank You,

Tabor Kelly
[-- Attachment #2 --]
//test.cpp
//compile with gcc test.cpp -lstdc++ -lkvm

#include <fcntl.h>
#include <iostream>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <sys/dkstat.h>
#include <kvm.h>
#include <nlist.h>
#include <stdio.h>

main()
{
  int i = NULL;
  int o = NULL;
  char errorBuff[_POSIX2_LINE_MAX];
  kvm_t *pKvm = NULL;
  struct nlist structNlist[7];
  bzero(&structNlist[0], (sizeof(struct nlist) * 7));

  structNlist[0].n_name = "_ccpu";
  structNlist[1].n_name = "_cp_time";
  structNlist[2].n_name = "_averunnable";
  structNlist[3].n_name = "_bufspace";
  structNlist[4].n_name = "_cnt";
  structNlist[5].n_name = "_nextpid";

  pKvm = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, &errorBuff[0]);
  if (pKvm == NULL)
  {
	cout << "Error: kvm_openfiles() failed!\n";
	cout << "\t\"" << errorBuff << "\"\n";
	return 1;
  }

  o = kvm_nlist(pKvm, &structNlist[0]); //change which item is pointed to for interesting results.
  if (o < 0)
  {
	cout << "Error: kvm_nlist() failed!\n";
	cout << "\t\"" << kvm_geterr(pKvm) << "\"\n";
	return 1;
  }

  cout << o << " items in the list were bad\n";


  for(i = 0; i < 6; i++)
  {
	cout << "structNlist[" << i << "].n_name: " << structNlist[i].n_name
		 << "\n";
	cout << "structNlist[" << i << "].n_type: " << structNlist[i].n_type
		 << "\n";
	cout << "structNlist[" << i << "].n_value: " << structNlist[0].n_value
		 << "\n";
  }

  kvm_close(pKvm);
}

Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?146809363.20010803141617>