Inferno  0.2
hit_count.cpp
Go to the documentation of this file.
00001 #include "hit_count.hpp"
00002 #include "read_args.hpp"
00003 
00004 HitCount HitCount::instance; 
00005 
00006 bool HitCount::enable = false; ///< call HitCount::Enable(true) to begin counting hits
00007 
00008 bool operator<( const HitCount::Category &l, const HitCount::Category &r )
00009 {
00010     // prioritise the comparisons in a way that makes for a nice dump when dumped in order
00011     for( int i=0; i<ReadArgs::hits_format.size(); i++ )
00012     {
00013         switch( ReadArgs::hits_format[i] )
00014         {
00015             case 'S':
00016             if( l.step != r.step )
00017                 return l.step < r.step;
00018             break;
00019             
00020             case 'I':
00021             case 'N':
00022             if( l.instance != r.instance )
00023                 return l.instance < r.instance;
00024             break;
00025             
00026             case 'F':
00027             if( l.file != r.file )
00028                 return l.file < r.file;
00029             break;
00030             
00031             case 'L':
00032             if( l.line != r.line )
00033                 return l.line < r.line;
00034             break;
00035             
00036             case 'M':
00037             if( l.function != r.function )
00038                 return l.function < r.function;
00039             break;
00040             
00041             default: // lower case letter have no effect
00042                 break;
00043         }
00044     }
00045     return false;
00046 }
00047 
00048 void HitCount::Usage()
00049 {
00050     fprintf(stderr,
00051             "Hit count format string:\n\n"
00052             "Letters after -th correspond to fields displayed in the dump.\n"
00053             "Dump is sorted according to the capital letters.\n"
00054             "left-most fields take precidence. Letters are:\n\n"
00055             "S Step number\n"
00056             "I Instance address\n"
00057             "F source file name\n"
00058             "L line number in source\n"
00059             "M function/method name\n"
00060             "n number of hits in this category\n\n");
00061     exit(1);
00062 }
00063 
00064 void HitCount::Check()
00065 {
00066     if( ReadArgs::hits_format == string("?") )
00067     {
00068         Usage();
00069     }
00070 }
00071 
00072 void HitCount::Dump()
00073 {
00074     FOREACH( pc p, counter )
00075     {
00076         for( int i=0; i<ReadArgs::hits_format.size(); i++ )
00077         {
00078             switch( ReadArgs::hits_format[i] )
00079             {
00080                 case 'S':
00081                 if( p.first.step>=0 )
00082                     printf("step %d", p.first.step );                   
00083                 else
00084                     printf("pre/post");
00085                 break;
00086                 
00087                 case 'I':
00088                 printf("%s", p.first.instance->GetAddr().c_str() );                   
00089                 break;
00090                 
00091                 case 'N':
00092                 printf("%s", p.first.instance->GetName().c_str() );                   
00093                 break;
00094 
00095                 case 'F':
00096                 printf("%s", p.first.file.c_str() );
00097                 break;
00098                 
00099                 case 'L':
00100                 printf(":%d", p.first.line );         
00101                 break;
00102                 
00103                 case 'M':
00104                 printf("in %s", p.first.function.c_str() );    
00105                 break;
00106                 
00107                 case 'n':
00108                 printf("%u hits", p.second );  
00109                 break;
00110                 
00111                 default:
00112                 Usage();
00113                 break;
00114             }
00115             if( i+1<ReadArgs::hits_format.size() )            
00116                 printf(" ");            
00117         }
00118         printf("\n");               
00119     }                 
00120 }
00121 
00122 void HitCount::Enable( bool e )
00123 {
00124     enable = e;
00125 }
00126 
00127