home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.wizards
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!uwm.edu!ux1.cso.uiuc.edu!news.iastate.edu!john
- From: john@iastate.edu (John Hascall)
- Subject: Re: NLIST Discussion
- Message-ID: <C04uFD.Kw1@news.iastate.edu>
- Sender: news@news.iastate.edu (USENET News System)
- Organization: Iowa State University, Ames, IA
- References: <72555@cup.portal.com>
- Date: Thu, 31 Dec 1992 16:42:47 GMT
- Lines: 80
-
- Dave-Nuttall@cup.portal.com (David Dave Nuttall) writes:
- }I need to locate reference material which discusses the function
- }"nlist". If you have anysuggestions, please post or e-mail me.
-
- The nlist function is really quite simple to use. Basically all it does is
- read the symbols of out a (non-stripped) image. Your system should have a
- "man nlist" and in include file (say, /usr/include/nlist.h) which should
- give details. Here is a fairly simple example of its use:
-
- #include <stdio.h>
- #include <nlist.h>
- #include <sys/ioctl.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <sys/fixpoint.h>
-
- struct nlist Nl[] = /* an array of the symbols that you */
- { /* are interested in the value of */
- { "_avenrun" }, /* name of a symbol (_ optional?) */
- #define X_AVENRUN 0 /* index of sym (coding convention) */
- { NULL }, /* NULL to terminate the list */
- };
-
- /*
- * get load average (most recent) as an integer
- * this version for DECstations which store the load avg as 3 FIXEDPOINTS
- */
- int loadavg()
- {
- static int kmfd = -1; /* file descriptor for /dev/kmem */
- long avenrun[3]; /* really fixedpoint, look like long */
-
- /*
- * To get the value of avenrun we will need to read it from
- * /dev/kmem (once we find out where it is with the nlist()
- * function). Since we get called often lets just open it once.
- * We also only need get the Nlist once.
- */
- if (kmfd < 0) {
- /*
- * Must be the first time through, open /dev/kmem
- * and mark it close on exec so our child processes
- * close it when they exec.
- */
- if ((kmfd = open("/dev/kmem", O_RDONLY, 0)) < 0) return (-1);
- (void)ioctl(kmfd, FIOCLEX, NULL);
-
- /*
- * get the Nlist out of /vmunix
- * (something more clever might allow the name as an arg)
- */
- nlist("/vmunix", Nl);
- }
-
- /*
- * If the first entry of the Nlist array has n_type of 0 (zero)
- * then it failed (probably because it was stripped).
- */
- if (Nl[0].n_type == 0) return (-1);
-
- /*
- * all is good, Nl[X_AVEBRUN].n_value tells us where to seek
- * in /dev/kmem to read the current value of the variable
- * We go ahead and read all 3 entries of the array even though
- * we'll only use the zeroth.
- */
- if ((lseek(kmfd, (off_t)Nl[X_AVENRUN].n_value, SEEK_SET) == -1) ||
- (read(kmfd, (char *)avenrun, sizeof(avenrun))) != sizeof(avenrun)) {
- return (-1);
- }
- /*
- * convert/round it to the form we want (an integer)
- */
- return ((int)(FIX_TO_DBL(avenrun[0]) + 0.5));
- }
- --
- John Hascall ``An ill-chosen word is the fool's messenger.''
- Systmes Software Engineer
- Project Vincent
- Iowa State University Computation Center + Ames, IA 50011 + 515/294-9551
-