util.C

Go to the documentation of this file.
00001 /*
00002  * See the dyninst/COPYRIGHT file for copyright information.
00003  * 
00004  * We provide the Paradyn Tools (below described as "Paradyn")
00005  * on an AS IS basis, and do not warrant its validity or performance.
00006  * We reserve the right to update, modify, or discontinue this
00007  * software at any time.  We shall have no obligation to supply such
00008  * updates or modifications or any other form of support to you.
00009  * 
00010  * By your use of Paradyn, you understand and agree that we (or any
00011  * other person or entity with proprietary rights in Paradyn) are
00012  * under no obligation to provide either maintenance services,
00013  * update services, notices of latent defects, or correction of
00014  * defects for Paradyn.
00015  * 
00016  * This library is free software; you can redistribute it and/or
00017  * modify it under the terms of the GNU Lesser General Public
00018  * License as published by the Free Software Foundation; either
00019  * version 2.1 of the License, or (at your option) any later version.
00020  * 
00021  * This library is distributed in the hope that it will be useful,
00022  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00024  * Lesser General Public License for more details.
00025  * 
00026  * You should have received a copy of the GNU Lesser General Public
00027  * License along with this library; if not, write to the Free Software
00028  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00029  */
00030 #if defined(os_windows)
00031 #include "common/h/ntHeaders.h"
00032 #endif
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035 #include <string>
00036 #include <map>
00037 #include "dynutil/h/dyntypes.h"
00038 
00039 using namespace std;
00040 
00041 namespace Dyninst {
00042 
00043 COMMON_EXPORT unsigned addrHashCommon(const Address &addr)
00044 {
00045    // inspired by hashs of string class
00046 
00047    register unsigned result = 5381;
00048 
00049    register Address accumulator = addr;
00050    while (accumulator > 0) {
00051       // We use 3 bits at a time from the address
00052       result = (result << 4) + result + (accumulator & 0x07);
00053       accumulator >>= 3;
00054    }
00055 
00056    return result;
00057 }
00058 
00059 COMMON_EXPORT unsigned addrHash(const Address & iaddr)
00060 {
00061    return Dyninst::addrHashCommon(iaddr);
00062 }
00063 
00064 COMMON_EXPORT unsigned ptrHash(const void * iaddr)
00065 {
00066    return Dyninst::addrHashCommon((Address)iaddr);
00067 }
00068 
00069 COMMON_EXPORT unsigned ptrHash(void * iaddr)
00070 {
00071    return Dyninst::addrHashCommon((Address)iaddr);
00072 }
00073 
00074 COMMON_EXPORT unsigned addrHash4(const Address &iaddr)
00075 {
00076    // call when you know that the low 2 bits are 0 (meaning they contribute
00077    // nothing to an even hash distribution)
00078    return Dyninst::addrHashCommon(iaddr >> 2);
00079 }
00080 
00081 COMMON_EXPORT unsigned addrHash16(const Address &iaddr)
00082 {
00083    // call when you know that the low 4 bits are 0 (meaning they contribute
00084    // nothing to an even hash distribution)
00085    return Dyninst::addrHashCommon(iaddr >> 4);
00086 }
00087 
00088 // string hash grabbed from pdstring
00089 unsigned stringhash(const std::string &s)
00090 {
00091    const char *str = s.c_str();
00092    if (!str)
00093       return 1; // 0 is reserved for unhashed key
00094 
00095    unsigned h = 5381;
00096    while (*str) {
00097       h = (h << 5) + h + (unsigned) (*str);
00098       str++;
00099    }
00100    return h==0 ? 1 : h; // 0 is reserved for unhashed key
00101 }
00102 
00103 std::string itos(int in)
00104 {
00105   char buf[16];
00106   snprintf(buf, 16, "%d", in);
00107   return std::string(buf);
00108 }
00109 
00110 std::string utos(unsigned in)
00111 {
00112   char buf[16];
00113   snprintf(buf, 16, "%u", in);
00114   return std::string(buf);
00115 }
00116 
00117 
00118 // This function will match string s against pattern p.
00119 // Asterisks match 0 or more wild characters, and a question
00120 // mark matches exactly one wild character.  In other words,
00121 // the asterisk is the equivalent of the regex ".*" and the
00122 // question mark is the equivalent of "."
00123 
00124 bool pattern_match( const char *p, const char *s, bool checkCase ) 
00125 {
00126    //const char *p = ptrn;
00127    //char *s = str;
00128 
00129    while ( true ) {
00130 
00131       // If at the end of the pattern, it matches if also at the end of the string
00132 
00133       if ( *p == '\0' )
00134          return ( *s == '\0' );
00135 
00136       // Process a '*'
00137 
00138       if ( *p == MULTIPLE_WILDCARD_CHAR ) {
00139          ++p;
00140 
00141          // If at the end of the pattern, it matches
00142          if ( *p == '\0' )
00143             return true;
00144 
00145          // Try to match the remaining pattern for each remaining substring of s
00146          for (; *s != '\0'; ++s )
00147             if ( pattern_match( p, s, checkCase ) )
00148                return true;
00149          // Failed
00150          return false;
00151       }
00152 
00153       // If at the end of the string (and at this point, not of the pattern), it fails
00154       if( *s == '\0' )
00155          return false;
00156 
00157       // Check if this character matches
00158       bool matchChar = false;
00159       if ( *p == WILDCARD_CHAR || *p == *s )
00160          matchChar = true;
00161       else if ( !checkCase ) {
00162          if ( *p >= 'A' && *p <= 'Z' && *s == ( *p + ( 'a' - 'A' ) ) )
00163             matchChar = true;
00164          else if ( *p >= 'a' && *p <= 'z' && *s == ( *p - ( 'a' - 'A' ) ) )
00165             matchChar = true;
00166       }
00167 
00168       if ( matchChar ) {
00169          ++p;
00170          ++s;
00171          continue;
00172       }
00173 
00174       // Did not match
00175       return false;
00176    }
00177 }
00178 
00179 bool wildcardEquiv(const std::string &us, const std::string &them, bool checkCase ) 
00180 {
00181    if ( us == them )
00182       return true;
00183    else
00184       return pattern_match( us.c_str(), them.c_str(), checkCase );
00185 }
00186 
00187 
00188 const char *platform_string()
00189 {
00190     const char *plat_str = getenv("PLATFORM");
00191     if (plat_str)
00192         return plat_str;
00193 
00194 #if defined (arch_x86)
00195 #if defined (os_linux)
00196     return "i386-unknown-linux2.4";
00197 #elif defined (os_windows)
00198     return "i386-unknown-nt4.0";
00199 #endif
00200 #elif defined (arch_x86_64)
00201 #if defined (os_linux)
00202     return "x86_64-unknown-linux2.4";
00203 #elif defined (os_windows)
00204     return "x86_64-unknown-nt4.0";
00205 #endif
00206 #elif defined (arch_power)
00207 #if defined (os_aix)
00208     return "rs6000-ibm-aix5.1";
00209 #elif defined (os_linux)
00210 #if defined (arch_64bit)
00211     return "ppc64_linux";
00212 #else
00213     return "ppc32_linux";
00214 #endif
00215 #endif
00216 #endif
00217     return "bad_platform";
00218 }
00219 
00220 
00221 //SymElf code is exclusively linked in each component, but we still want to share
00222 //the cache information.  Thus the cache will live in libcommon.
00223 class SymElf;
00224 
00225 map<string, SymElf *> *getSymelfCache() {
00226    static map<string, SymElf *> elfmap;
00227    return &elfmap;
00228 }
00229 
00230 } // namespace Dyninst
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 12 Jul 2013 for SymtabAPI by  doxygen 1.6.1