sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi.instr/src/com/nokia/carbide/cpp/pi/instr/BinaryReader.java
changeset 5 844b047e260d
parent 2 b9ab3b238396
child 12 ae255c9aa552
equal deleted inserted replaced
4:615035072f7e 5:844b047e260d
    80     return findSequence(sample,(ProcessedBinary)null);
    80     return findSequence(sample,(ProcessedBinary)null);
    81   }
    81   }
    82 
    82 
    83   public BinaryReaderResult findSequence(IttSample sample, Binary b)
    83   public BinaryReaderResult findSequence(IttSample sample, Binary b)
    84   {
    84   {
    85     if (this.processedFiles.containsKey(b.binaryName))
    85     if (this.processedFiles.containsKey(b.getBinaryName()))
    86     {
    86     {
    87       ProcessedBinary pf = (ProcessedBinary)this.processedFiles.get(b.binaryName);
    87       ProcessedBinary pf = (ProcessedBinary)this.processedFiles.get(b.getBinaryName());
    88       return this.findSequence(sample,pf);
    88       return this.findSequence(sample,pf);
    89     }
    89     }
    90     return null;
    90     return null;
    91   }
    91   }
    92   
    92   
   101   	return null;
   101   	return null;
   102   }
   102   }
   103   
   103   
   104   public boolean checkSampleInBinary(IttSample sample,Binary binary,int differencesAllowed)
   104   public boolean checkSampleInBinary(IttSample sample,Binary binary,int differencesAllowed)
   105   {
   105   {
   106   	ProcessedBinary pb = this.getProcessedBinaryForName(binary.binaryName);
   106   	ProcessedBinary pb = this.getProcessedBinaryForName(binary.getBinaryName());
   107   	if (pb == null)
   107   	if (pb == null)
   108   	{
   108   	{
   109   		System.out.println(Messages.getString("BinaryReader.cannotFindBinary")+binary.binaryName); //$NON-NLS-1$
   109   		System.out.println(Messages.getString("BinaryReader.cannotFindBinary")+binary.getBinaryName()); //$NON-NLS-1$
   110 		return false;
   110 		return false;
   111   	}
   111   	}
   112   	
   112   	
   113   	if (binary.startAddress <= sample.programCounter &&
   113   	if (binary.getStartAddress() <= sample.programCounter &&
   114   	   binary.startAddress+binary.length >= sample.programCounter)
   114   	   binary.getStartAddress()+binary.getLength() >= sample.programCounter)
   115   	{
   115   	{
   116   		// the program counter value indicates that the sample
   116   		// the program counter value indicates that the sample
   117   		// is within the binary
   117   		// is within the binary
   118   		long[] reversedInstructions = sample.reversedInstructions();
   118   		long[] reversedInstructions = sample.reversedInstructions();
   119   		int matches = 0;
   119   		int matches = 0;
   120   		for (int i=0;i<reversedInstructions.length;i++)
   120   		for (int i=0;i<reversedInstructions.length;i++)
   121   		{
   121   		{
   122   			
   122   			
   123   			long instruction = reversedInstructions[i];
   123   			long instruction = reversedInstructions[i];
   124   			long offset = (long)(sample.programCounter-binary.startAddress);
   124   			long offset = (long)(sample.programCounter-binary.getStartAddress());
   125 
   125 
   126   			if (offset < binary.length)
   126   			if (offset < binary.getLength())
   127   			{
   127   			{
   128   				// add the amount of bytes that match in the next 4 bytes
   128   				// add the amount of bytes that match in the next 4 bytes
   129   				matches += this.testNextFourBytes((int)offset+(i*4),instruction,pb);
   129   				matches += this.testNextFourBytes((int)offset+(i*4),instruction,pb);
   130   			}
   130   			}
   131   		}	
   131   		}	
   183           {
   183           {
   184             int i = indicesToCheck[k];
   184             int i = indicesToCheck[k];
   185 
   185 
   186             int match = 0;
   186             int match = 0;
   187             
   187             
   188             if (i < pf.length - 5)
   188             if (i < pf.getLength() - 5)
   189             {
   189             {
   190               if (pf.data[i] == (byte) first) match++;
   190               if (pf.data[i] == (byte) first) match++;
   191               
   191               
   192               if (pf.data[i + 1] == (byte) second) match++;
   192               if (pf.data[i + 1] == (byte) second) match++;
   193               
   193               
   217                         // search pattern
   217                         // search pattern
   218                         if ( (checksum & 0xffffffff) == (sample.checksum & 0xffffffff) )
   218                         if ( (checksum & 0xffffffff) == (sample.checksum & 0xffffffff) )
   219                         {
   219                         {
   220 
   220 
   221                           // add this location to possible binaries
   221                           // add this location to possible binaries
   222                           Binary b = new Binary(pf.binaryName);
   222                           Binary b = new Binary(pf.getBinaryName());
   223                           b.length = pf.length;
   223                           b.setLength(pf.getLength());
   224                           b.offsetToCodeStart = pf.offsetToCodeStart;
   224                           b.setOffsetToCodeStart(pf.getOffsetToCodeStart());
   225                           b.type = pf.type;
   225                           b.setType(pf.getType());
   226 
   226 
   227                           // binary start address is the this address - this offset
   227                           // binary start address is the this address - this offset
   228                           b.startAddress = sample.programCounter-i;
   228                           b.setStartAddress(sample.programCounter-i);
   229                           possibleBinaries.add(b);
   229                           possibleBinaries.add(b);
   230 
   230 
   231                           //System.out.println (	"CHECKSUM MATCH at "+pf.binaryName+"@0x"+Long.toHexString(b.startAddress-b.offsetToCodeStart)+
   231                           //System.out.println (	"CHECKSUM MATCH at "+pf.binaryName+"@0x"+Long.toHexString(b.startAddress-b.offsetToCodeStart)+
   232                           //						" pos:"+i+"!! Checksum:"+checksum+" "+pf.file.getAbsolutePath());
   232                           //						" pos:"+i+"!! Checksum:"+checksum+" "+pf.file.getAbsolutePath());
   233 
   233 
   238                         else
   238                         else
   239                         {
   239                         {
   240 
   240 
   241                           // add this location to possible binaries
   241                           // add this location to possible binaries
   242 
   242 
   243                           Binary b = new Binary(pf.binaryName);
   243                           Binary b = new Binary(pf.getBinaryName());
   244                           b.length = pf.length;
   244                           b.setLength(pf.getLength());
   245                           b.offsetToCodeStart = pf.offsetToCodeStart;
   245                           b.setOffsetToCodeStart(pf.getOffsetToCodeStart());
   246                           b.type = pf.type;
   246                           b.setType(pf.getType());
   247 
   247 
   248                           // binary start address is this address - this offset
   248                           // binary start address is this address - this offset
   249                           b.startAddress = sample.programCounter-i;
   249                           b.setStartAddress(sample.programCounter-i);
   250                           possibleBinaries.add(b);
   250                           possibleBinaries.add(b);
   251 
   251 
   252                           //System.out.println(	"MATCH at "+pf.binaryName+"@0x"+Long.toHexString(b.startAddress-b.offsetToCodeStart)+
   252                           //System.out.println(	"MATCH at "+pf.binaryName+"@0x"+Long.toHexString(b.startAddress-b.offsetToCodeStart)+
   253                           //						" pos:"+i+"!! "+pf.file.getAbsolutePath());
   253                           //						" pos:"+i+"!! "+pf.file.getAbsolutePath());
   254                           
   254                           
   272   }
   272   }
   273 
   273 
   274 
   274 
   275   public void printBinaryFromOffset(ProcessedBinary pf, int offset, int length)
   275   public void printBinaryFromOffset(ProcessedBinary pf, int offset, int length)
   276   {
   276   {
   277     if (pf.length < offset+length) return;
   277     if (pf.getLength() < offset+length) return;
   278     int c = 0;
   278     int c = 0;
   279 
   279 
   280     for (int i=offset;i<offset+length;i++)
   280     for (int i=offset;i<offset+length;i++)
   281     {
   281     {
   282       String hex = Integer.toHexString((int)pf.data[i] & 0xff);
   282       String hex = Integer.toHexString((int)pf.data[i] & 0xff);
   299       int third = (int) ((data & (long)0x0000ff00) >> 8);
   299       int third = (int) ((data & (long)0x0000ff00) >> 8);
   300       int fourth = (int) ((data & (long)0x000000ff));
   300       int fourth = (int) ((data & (long)0x000000ff));
   301 
   301 
   302       int matches = 0;
   302       int matches = 0;
   303       
   303       
   304       if (pf.length > offset+4)
   304       if (pf.getLength() > offset+4)
   305       {
   305       {
   306         if (pf.data[offset] == (byte)first) matches++; 
   306         if (pf.data[offset] == (byte)first) matches++; 
   307         
   307         
   308         if (pf.data[offset+1] == (byte)second) matches++;
   308         if (pf.data[offset+1] == (byte)second) matches++;
   309         
   309         
   334     {
   334     {
   335       ProcessedBinary pf = (ProcessedBinary)this.processedFiles.get(fileName);
   335       ProcessedBinary pf = (ProcessedBinary)this.processedFiles.get(fileName);
   336       if (length == -1)
   336       if (length == -1)
   337         {
   337         {
   338           startOffset = 0;
   338           startOffset = 0;
   339           length = pf.length;
   339           length = pf.getLength();
   340         }
   340         }
   341 
   341 
   342       for (int i=startOffset;i<startOffset+length;i++)
   342       for (int i=startOffset;i<startOffset+length;i++)
   343       {
   343       {
   344         String hex = Integer.toHexString((int)pf.data[i] & 0xff);
   344         String hex = Integer.toHexString((int)pf.data[i] & 0xff);
   410         File f = (File) enumer.nextElement();
   410         File f = (File) enumer.nextElement();
   411         String name = f.getAbsolutePath();
   411         String name = f.getAbsolutePath();
   412         name = name.substring(name.indexOf(File.separator),name.length());
   412         name = name.substring(name.indexOf(File.separator),name.length());
   413 
   413 
   414         if (!(this.processedFiles.containsKey(name) &&
   414         if (!(this.processedFiles.containsKey(name) &&
   415               (((ProcessedBinary)this.processedFiles.get(name)).length == f.length())))
   415               (((ProcessedBinary)this.processedFiles.get(name)).getLength() == f.length())))
   416         {
   416         {
   417           try
   417           try
   418           {
   418           {
   419             ProcessedBinary pf = processFile(f);
   419             ProcessedBinary pf = processFile(f);
   420             //System.out.println(f.getName());
   420             //System.out.println(f.getName());
   421             this.processedFiles.put(pf.binaryName, pf);
   421             this.processedFiles.put(pf.getBinaryName(), pf);
   422             length += pf.length;
   422             length += pf.getLength();
   423             addedFiles++;
   423             addedFiles++;
   424           }
   424           }
   425           catch (Exception e)
   425           catch (Exception e)
   426           {
   426           {
   427             e.printStackTrace();
   427             e.printStackTrace();