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 / bprof.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-31  |  2.4 KB  |  103 lines

  1. /* Main source file for bprof. */
  2.  
  3. #include <std.h>
  4. #include <GetOpt.h>
  5. #include "String.sfpnt.VHMap.h"
  6. #include "bmonout.h"
  7. #include "execute.h"
  8. #include "sources.h"
  9.  
  10. int main(int argc, char** argv)
  11. {
  12.     String suffix(".bprof");    // Default suffix
  13.     dirset gooddirs;
  14.     gooddirs += ".";        // Always use pwd
  15.  
  16.     GetOpt getopt(argc, argv, "s:d:");
  17.     int option;
  18.     while ((option = getopt()) != EOF) {
  19.     switch (option) {
  20.     case 'd':
  21.         gooddirs += getopt.optarg;
  22.         break;
  23.     case 's':
  24.         suffix = getopt.optarg;
  25.         break;
  26.     case '?':
  27.         exit(1);
  28.     }
  29.     }
  30.  
  31.     // First non-option argument is executable
  32.     const char* filename;
  33.     if (getopt.optind < argc) {
  34.     filename = argv[getopt.optind++];
  35.     } else {
  36.     filename = "a.out";
  37.     }
  38.     executable aout(filename);
  39.     
  40.     // Other non-options args are bmon.out files
  41.     int numbmon = argc - getopt.optind;
  42.     bmonout** bmon;        // Array of bmonout pointers
  43.     if (numbmon) {
  44.     bmon = new bmonout * [numbmon];
  45.     for (int i = 0; i < numbmon; i++) {
  46.         bmon[i] = new bmonout(argv[getopt.optind]);
  47.         if (bmon[i]->mtime() < aout.mtime()) {
  48.         cerr << "Warning: executable is newer than " << argv[getopt.optind] << '\n';
  49.         }
  50.         getopt.optind++;
  51.     }
  52.     } else {
  53.     numbmon = 1;
  54.     bmon = new bmonout*[numbmon];
  55.     bmon[0] = new bmonout;
  56.     if (bmon[0]->mtime() < aout.mtime()) {
  57.         cerr << "Warning: executable is newer than bmon.out\n";
  58.     }
  59.     }
  60.  
  61.     StringsfpntVHMap source(0);
  62.  
  63.     do {
  64.     String cname;
  65.     if (aout.filename()[0] == '/') {
  66.         cname = aout.filename();
  67.     } else {
  68.         cname = aout.dirname();
  69.         cname += aout.filename();
  70.     }
  71.  
  72.     if (!source.contains(cname)) {
  73.         int lastslash = cname.index("/",-1);
  74.         String dir = cname.before(lastslash);
  75.         int really = gooddirs.contains(dir);
  76.         source[cname] = new sourcefile(cname, really);
  77.         if (aout.mtime() < source[cname]->mtime())
  78.         cerr << "Warning: source file " << cname << " newer than executable\n";
  79.     }
  80.  
  81.     sourcefile& sfile = *source[cname];
  82.     for (unsigned long i = aout.lowpc(); i < aout.highpc(); i++) {
  83.         unsigned int count = 0;
  84.         for (int j = 0; j < numbmon; j++) {
  85.         count += (*bmon[j])[i];
  86.         }
  87.         sfile[aout.lineno()] += count;
  88.     }
  89.     } while (aout.shift());
  90.  
  91.     for (int i = 0; i < numbmon; i++) {
  92.     delete bmon[i];
  93.     }
  94.     delete[] bmon;
  95.  
  96.     // Write the source files with data
  97.     for (Pix p = source.first(); p; source.next(p)) {
  98.     source.contents(p)->paste(suffix);
  99.     }
  100.  
  101.     return 0;
  102. }
  103.