os/kernelhwsrv/kerneltest/e32utils/analyse/codespace.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of the License "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 #include <crtdbg.h>
    17 #include "analyse.h"
    18 #include "codespace.h"
    19 
    20 #ifdef __MSVCDOTNET__
    21 #pragma warning(push, 3)	// cannot compile MSVC's STL at warning level 4
    22 #pragma warning(disable: 4786 4710 4530)
    23 #include <strstream>
    24 #include <iomanip>
    25 #else //!__MSVCDOTNET__
    26 #include <strstrea.h>
    27 #include <iomanip.h>
    28 #endif //__MSVCDOTNET__
    29 
    30 #include <algorithm>
    31 
    32 // class CodeSpace
    33 
    34 int CodeSpace::Size() const
    35 	{
    36 	return 1;
    37 	}
    38 
    39 int CodeSpace::Bucket(PC) const
    40 	{
    41 	return KOtherBucket;
    42 	}
    43 
    44 const char* CodeSpace::Name(int) const
    45 	{
    46 	return "<other>";
    47 	}
    48 
    49 CodeSpace::TOrder CodeSpace::Ordering() const
    50 	{
    51 	return ERandom;
    52 	}
    53 
    54 
    55 // class AddressCodeSpace
    56 
    57 AddressCodeSpace::AddressCodeSpace(PC aBase, PC aLimit, unsigned aBucketSize, TType aType)
    58 	:iBase(aBase), iType(aType)
    59 	{
    60 	if (aBucketSize < 1)
    61 		aBucketSize = 1;
    62 	unsigned shift;
    63 	for (shift = 0; (aBucketSize >> shift) != 1; ++shift)
    64 		;
    65 	iBucketShift = shift;
    66 	iBuckets = (aLimit - aBase + (1u << shift) - 1) >> shift;
    67 	}
    68 
    69 int AddressCodeSpace::Size() const
    70 	{
    71 	return iBuckets + 1;
    72 	}
    73 
    74 int AddressCodeSpace::Bucket(PC aPc) const
    75 	{
    76 	if (aPc >= iBase)
    77 		{
    78 		unsigned bucket = (aPc - iBase) >> iBucketShift;
    79 		if (bucket < iBuckets)
    80 			return bucket + 1;
    81 		}
    82 	return KOtherBucket;
    83 	}
    84 
    85 const char* AddressCodeSpace::Name(int aBucket) const
    86 	{
    87 	if (aBucket == KOtherBucket)
    88 		return CodeSpace::Name(aBucket);
    89 
    90 	unsigned offset = ((aBucket - 1) << iBucketShift);
    91 	strstream s(iBuffer, sizeof(iBuffer), ios::out);
    92 	s << hex << setfill('0');
    93 	if (iType == EAbsolute)
    94 		s << setw(8) << iBase + offset;
    95 	else
    96 		s << "+ " << setw(4) << offset;
    97 	s << setfill(' ') << '\0';
    98 	return iBuffer;
    99 	}
   100 
   101 CodeSpace::TOrder AddressCodeSpace::Ordering() const
   102 	{
   103 	return (iType == EAbsolute) ? EOrdered : ELinear;
   104 	}
   105 
   106 
   107 // class MappedCodeSpace
   108 
   109 MappedCodeSpace::MappedCodeSpace(const SymbolFile& aSymbols, MappedCodeSpace::Partition& aPartition)
   110 	{
   111 	aPartition.iCodeSpace = this;
   112 	aSymbols.Parse(aPartition);
   113 	}
   114 
   115 MappedCodeSpace::MappedCodeSpace() 
   116 	{}
   117 
   118 const MappedCodeSpace::Element* MappedCodeSpace::Find(PC aPc) const
   119 //
   120 // Find and return the element which contains this PC value
   121 // If not mapped return 0
   122 //
   123 	{
   124 	Map::const_iterator e = iMap.upper_bound(aPc);
   125 
   126 	// ignore deleted segments
   127 	for(;e != iMap.end() && aPc >= e->second.iBase;e++)
   128 		if (!e->second.iUnloaded)
   129 			return &e->second;
   130 	return 0;
   131 	}
   132 
   133 std::pair<const char*,unsigned> MappedCodeSpace::Lookup(PC aPc) const
   134 	{
   135 	const Element* e = Find(aPc);
   136 	if (e == 0)
   137 		return std::pair<const char*,unsigned>(0, aPc);
   138 	return std::pair<const char*,unsigned>(e->iName, aPc - e->iBase);
   139 	}
   140 
   141 int MappedCodeSpace::Size() const
   142 	{
   143 	return iMap.size() + 1;
   144 	}
   145 
   146 int MappedCodeSpace::Bucket(PC aPc) const
   147 	{
   148 	const Element* e = Find(aPc);
   149 	return (e == 0) ? KOtherBucket : e->iBucket + 1;
   150 	}
   151 
   152 const char* MappedCodeSpace::Name(int aBucket) const
   153 	{
   154 	return (aBucket == KOtherBucket) ? CodeSpace::Name(aBucket) : iNames[aBucket - 1];
   155 	}
   156 
   157 void MappedCodeSpace::Add(PC aBase, PC aLimit, const char* aName)
   158 	{
   159 	// insert
   160 	iMap.insert(Map::value_type(aLimit, Element(aBase, aLimit, aName)));
   161 	}
   162 
   163 void MappedCodeSpace::Done(PC aFirstPc, PC aLastPc, int aModuleId)
   164 	{
   165 	if (!aFirstPc)
   166 		{
   167 		iNames.clear();
   168 		for (Map::iterator p = iMap.begin(), e = iMap.end(); p != e; ++p)
   169 			{
   170 			p->second.iBucket = iNames.size();
   171 			iNames.push_back(p->second.iName);
   172 			}
   173 		}
   174 	else
   175 		{
   176 		Map::iterator p = iMap.find(aFirstPc);
   177 		_ASSERT(p != iMap.end());
   178 		for (Map::iterator e = iMap.end(); p != e && p->first <= aLastPc; ++p)
   179 			{
   180 			if (p->second.iUnloaded) 
   181 				continue;
   182 
   183 			bool bFound = false;
   184 			NamesMap::iterator pMap = iNamesMap.find(p->second.iName);
   185 			if (pMap != iNamesMap.end())
   186 				for(;!strcmp(p->second.iName, pMap->first);pMap++)
   187 					if (pMap->second.iId == aModuleId)
   188 						{
   189 						bFound = true;
   190 						break;
   191 						}
   192 			if (bFound)
   193 				p->second.iBucket = pMap->second.iIndex;
   194 			else
   195 				{
   196 				p->second.iBucket = iNames.size();
   197 				iNames.push_back(p->second.iName);
   198 				iNamesMap.insert(NamesMap::value_type(p->second.iName, IdNames(aModuleId, p->second.iBucket)));
   199 				}
   200 			}
   201 		}
   202 	}
   203