ij-16.png   inspectorJ -- JavaTM Profiler
sf project site browse source checkout source
SourceForge.net Logo



src/inspectorj/agent/agentutils.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   inspectorJ - java profiler                                            *
00003  *   Copyright (C) 2007 by James May
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00027 #include "agentutils.h"
00028 
00029 static QString SINGLE_STAR("*");
00030 static QString DOUBLE_STAR("**");
00031 static QString DOLLAR_SIGN("$");
00032 static QString DOT(".");
00033 static QString JNI_PACKAGE_SEPARATOR("/");
00034 static QString WILDCARD("[^/]*");
00035 static QString GREEDY_WILDCARD(".*");
00036 static QString GREEDY_WILDCARD_TEMP("GREEDY_WILDCARD_TEMP");
00037 
00038 
00051 void createAgentClassFilter(QRegExp &regexp, QString filterString) {
00052     filterString
00053         .replace(DOT, JNI_PACKAGE_SEPARATOR)
00054         .replace(DOUBLE_STAR, GREEDY_WILDCARD_TEMP)
00055         .replace(SINGLE_STAR, WILDCARD)
00056         .replace(GREEDY_WILDCARD_TEMP, GREEDY_WILDCARD)
00057         .append(DOLLAR_SIGN);
00058     regexp.setPattern(filterString);
00059 }
00060 
00069 char* getToken(char *str, char *seps, char *buf, int max)
00070 {
00071     int len;
00072     
00073     buf[0] = 0;
00074     if ( str==NULL || str[0]==0 ) {
00075         return NULL;
00076     }
00077     str += strspn(str, seps);
00078     if ( str[0]==0 ) {
00079         return NULL;
00080     }
00081     len = (int)strcspn(str, seps);
00082     if ( len >= max ) {
00083         return NULL;
00084     }
00085     (void)strncpy(buf, str, len);
00086     buf[len] = 0;
00087     return str+len;
00088 }
00089 
00090 /* Determines if a class/method is specified by a list item 
00091  *   item       String that represents a pattern to match
00092  *                If it starts with a '*', then any class is allowed
00093  *                If it ends with a '*', then any method is allowed
00094  *   cname      Class name, e.g. "java.lang.Object"
00095  *   mname      Method name, e.g. "<init>"
00096  *  Returns 1(true) or 0(false).
00097  */
00098 //static int
00099 //covered_by_list_item(char *item, char *cname, char *mname)
00100 //{
00101 //    int      len;
00102 //    
00103 //    len = (int)strlen(item);
00104 //    if ( item[0]=='*' ) {
00105 //      if ( strncmp(mname, item+1, len-1)==0 ) {
00106 //          return 1;
00107 //      }
00108 //    } else if ( item[len-1]=='*' ) {
00109 //      if ( strncmp(cname, item, len-1)==0 ) {
00110 //          return 1;
00111 //      }
00112 //    } else {
00113 //      int cname_len;
00114 //      
00115 //      cname_len = (int)strlen(cname);
00116 //      if ( strncmp(cname, item, (len>cname_len?cname_len:len))==0 ) {
00117 //          if ( cname_len >= len ) {
00118 //              /* No method name supplied in item, we must have matched */
00119 //              return 1;
00120 //          } else {
00121 //              int mname_len;
00122 //              
00123 //              mname_len = (int)strlen(mname);
00124 //              item += cname_len+1;
00125 //              len -= cname_len+1;
00126 //              if ( strncmp(mname, item, (len>mname_len?mname_len:len))==0 ) {
00127 //                  return 1;
00128 //              }
00129 //          }
00130 //      }
00131 //    }
00132 //    return 0;
00133 //}
00134 
00135 /* Determines if a class/method is specified by this list
00136  *   cname      Class name, e.g. "java.lang.Object"
00137  *   mname      Method name, e.g. "<init>"
00138  *   classFilters       Empty or an explicit list for inclusion
00139  *  Returns true or false.
00140  */
00141 bool
00142 coveredByList(char *cname, char *mname, QList<QRegExp> *classFilters)
00143 {    
00144     QList<QRegExp>::iterator iter;
00145     iter = classFilters->begin();
00146     if (iter == classFilters->end()) {            
00147         // no filters evidently so match any class
00148         return true;
00149     } else {
00150         QString className(cname);
00151         while (iter != classFilters->end()) {
00152             QRegExp regexp;
00153             regexp = *iter;            
00154             if (className.contains(regexp)) {
00155                 return true;
00156             }
00157             ++iter;
00158         }
00159     }
00160     return false;
00161 }
00162 
00163 /* Determines which class and methods we are interested in 
00164  *   cname              Class name, e.g. "java.lang.Object"
00165  *   mname              Method name, e.g. "<init>"
00166  *   classFilters       Empty or an explicit list for inclusion
00167  *  Returns true or false.
00168  */
00169 bool
00170 interested(char *cname, char *mname, QList<QRegExp> *classFilters)
00171 {    
00172     if ( classFilters->isEmpty() ) { 
00173         return true;
00174     } else {
00175         return coveredByList(cname, mname, classFilters);
00176     }
00177 }
00178 
00179 /* Default log level = info */
00180 int inspectorj::agent::AgentLogger::loglevel = 2;
00181 
00182 /* ------------------------------------------------------------------- */

Generated on Sun Aug 19 17:07:52 2007 for inspectorJ by  doxygen 1.5.1