home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / bprof-1.1 / bprof-1 / bprof / bmonout.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-30  |  1.9 KB  |  80 lines

  1. #include <std.h>
  2. #include <iostream.h>
  3. #include <sys/stat.h>
  4. #include <new.h>
  5. #include "bmonout.h"
  6.  
  7. const char* bmonout::nowname = 0;
  8.  
  9. void bmonout::newhandler(void)
  10. {
  11.     cerr << nowname << " probably has the wrong format\n";
  12.     exit(1);
  13. }
  14.  
  15. /* Note: bmon.c uses unsigned longs as addresses, while all this uses
  16.    unsigned ints.  I suppose one should really use caddr_t (usually
  17.    char *), but the kernel thinks that eip is an unsigned long. */
  18.  
  19. bmonout::bmonout(const char* file)
  20. {
  21.     int fd = open(file, O_RDONLY);
  22.     if (fd == -1) {
  23.     perror(file);
  24.     exit(1);
  25.     }
  26.  
  27.     stat flstat;
  28.     if (stat(file, &flstat) == -1) {
  29.     perror("Cannot stat bmon file");
  30.     exit(1);
  31.     }
  32.     size_t filesize = flstat.st_size/(2*sizeof(unsigned int));
  33.     if (!filesize ||
  34.     filesize*2*sizeof(unsigned int) != flstat.st_size) {
  35.     cerr << file << " has a wrong length\n";
  36.     exit(1);
  37.     }
  38.     _mtime = flstat.st_mtime;
  39.     
  40.     read(fd, &lowpc, sizeof(lowpc));
  41.     lseek(fd, -2*sizeof(unsigned int), SEEK_END);
  42.     read(fd, &highpc, sizeof(highpc));
  43.     unsigned int diff = highpc - lowpc + 1;
  44.  
  45.     /* If this is the wrong file, highpc and lowpc can be anything and
  46.        the new will probably fail miserably.  So if it fails suspect a
  47.        wrong file. */
  48.     nowname = file;
  49.     set_new_handler(newhandler);
  50.     count = new unsigned int[diff];
  51.     set_new_handler(0);
  52.     memset(count, 0, diff * sizeof(unsigned int));
  53.  
  54.     lseek(fd, 0, SEEK_SET);
  55.     unsigned int ticks[2];
  56.     for (int i = 0; i < filesize; i++) {
  57.     read(fd, ticks, sizeof(ticks));
  58.     int index = ticks[0] - lowpc;
  59.     if (index < 0 || index >= diff) {
  60.         cerr << file << " has the wrong format\n";
  61.         exit(1);
  62.     }
  63.     count[ticks[0]-lowpc] = ticks[1];
  64.     }
  65.  
  66.     close(fd);
  67. }
  68.  
  69. bmonout::~bmonout(void)
  70. {
  71.     delete[] count;
  72. }
  73.  
  74. unsigned int bmonout::operator[](unsigned int pc) const
  75. {
  76.     if (pc < lowpc || pc > highpc)
  77.     return 0;
  78.     return count[pc-lowpc];
  79. }
  80.