os/kernelhwsrv/kerneltest/e32utils/analyse/codespace.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32utils/analyse/codespace.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,203 @@
     1.4 +// Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include <crtdbg.h>
    1.20 +#include "analyse.h"
    1.21 +#include "codespace.h"
    1.22 +
    1.23 +#ifdef __MSVCDOTNET__
    1.24 +#pragma warning(push, 3)	// cannot compile MSVC's STL at warning level 4
    1.25 +#pragma warning(disable: 4786 4710 4530)
    1.26 +#include <strstream>
    1.27 +#include <iomanip>
    1.28 +#else //!__MSVCDOTNET__
    1.29 +#include <strstrea.h>
    1.30 +#include <iomanip.h>
    1.31 +#endif //__MSVCDOTNET__
    1.32 +
    1.33 +#include <algorithm>
    1.34 +
    1.35 +// class CodeSpace
    1.36 +
    1.37 +int CodeSpace::Size() const
    1.38 +	{
    1.39 +	return 1;
    1.40 +	}
    1.41 +
    1.42 +int CodeSpace::Bucket(PC) const
    1.43 +	{
    1.44 +	return KOtherBucket;
    1.45 +	}
    1.46 +
    1.47 +const char* CodeSpace::Name(int) const
    1.48 +	{
    1.49 +	return "<other>";
    1.50 +	}
    1.51 +
    1.52 +CodeSpace::TOrder CodeSpace::Ordering() const
    1.53 +	{
    1.54 +	return ERandom;
    1.55 +	}
    1.56 +
    1.57 +
    1.58 +// class AddressCodeSpace
    1.59 +
    1.60 +AddressCodeSpace::AddressCodeSpace(PC aBase, PC aLimit, unsigned aBucketSize, TType aType)
    1.61 +	:iBase(aBase), iType(aType)
    1.62 +	{
    1.63 +	if (aBucketSize < 1)
    1.64 +		aBucketSize = 1;
    1.65 +	unsigned shift;
    1.66 +	for (shift = 0; (aBucketSize >> shift) != 1; ++shift)
    1.67 +		;
    1.68 +	iBucketShift = shift;
    1.69 +	iBuckets = (aLimit - aBase + (1u << shift) - 1) >> shift;
    1.70 +	}
    1.71 +
    1.72 +int AddressCodeSpace::Size() const
    1.73 +	{
    1.74 +	return iBuckets + 1;
    1.75 +	}
    1.76 +
    1.77 +int AddressCodeSpace::Bucket(PC aPc) const
    1.78 +	{
    1.79 +	if (aPc >= iBase)
    1.80 +		{
    1.81 +		unsigned bucket = (aPc - iBase) >> iBucketShift;
    1.82 +		if (bucket < iBuckets)
    1.83 +			return bucket + 1;
    1.84 +		}
    1.85 +	return KOtherBucket;
    1.86 +	}
    1.87 +
    1.88 +const char* AddressCodeSpace::Name(int aBucket) const
    1.89 +	{
    1.90 +	if (aBucket == KOtherBucket)
    1.91 +		return CodeSpace::Name(aBucket);
    1.92 +
    1.93 +	unsigned offset = ((aBucket - 1) << iBucketShift);
    1.94 +	strstream s(iBuffer, sizeof(iBuffer), ios::out);
    1.95 +	s << hex << setfill('0');
    1.96 +	if (iType == EAbsolute)
    1.97 +		s << setw(8) << iBase + offset;
    1.98 +	else
    1.99 +		s << "+ " << setw(4) << offset;
   1.100 +	s << setfill(' ') << '\0';
   1.101 +	return iBuffer;
   1.102 +	}
   1.103 +
   1.104 +CodeSpace::TOrder AddressCodeSpace::Ordering() const
   1.105 +	{
   1.106 +	return (iType == EAbsolute) ? EOrdered : ELinear;
   1.107 +	}
   1.108 +
   1.109 +
   1.110 +// class MappedCodeSpace
   1.111 +
   1.112 +MappedCodeSpace::MappedCodeSpace(const SymbolFile& aSymbols, MappedCodeSpace::Partition& aPartition)
   1.113 +	{
   1.114 +	aPartition.iCodeSpace = this;
   1.115 +	aSymbols.Parse(aPartition);
   1.116 +	}
   1.117 +
   1.118 +MappedCodeSpace::MappedCodeSpace() 
   1.119 +	{}
   1.120 +
   1.121 +const MappedCodeSpace::Element* MappedCodeSpace::Find(PC aPc) const
   1.122 +//
   1.123 +// Find and return the element which contains this PC value
   1.124 +// If not mapped return 0
   1.125 +//
   1.126 +	{
   1.127 +	Map::const_iterator e = iMap.upper_bound(aPc);
   1.128 +
   1.129 +	// ignore deleted segments
   1.130 +	for(;e != iMap.end() && aPc >= e->second.iBase;e++)
   1.131 +		if (!e->second.iUnloaded)
   1.132 +			return &e->second;
   1.133 +	return 0;
   1.134 +	}
   1.135 +
   1.136 +std::pair<const char*,unsigned> MappedCodeSpace::Lookup(PC aPc) const
   1.137 +	{
   1.138 +	const Element* e = Find(aPc);
   1.139 +	if (e == 0)
   1.140 +		return std::pair<const char*,unsigned>(0, aPc);
   1.141 +	return std::pair<const char*,unsigned>(e->iName, aPc - e->iBase);
   1.142 +	}
   1.143 +
   1.144 +int MappedCodeSpace::Size() const
   1.145 +	{
   1.146 +	return iMap.size() + 1;
   1.147 +	}
   1.148 +
   1.149 +int MappedCodeSpace::Bucket(PC aPc) const
   1.150 +	{
   1.151 +	const Element* e = Find(aPc);
   1.152 +	return (e == 0) ? KOtherBucket : e->iBucket + 1;
   1.153 +	}
   1.154 +
   1.155 +const char* MappedCodeSpace::Name(int aBucket) const
   1.156 +	{
   1.157 +	return (aBucket == KOtherBucket) ? CodeSpace::Name(aBucket) : iNames[aBucket - 1];
   1.158 +	}
   1.159 +
   1.160 +void MappedCodeSpace::Add(PC aBase, PC aLimit, const char* aName)
   1.161 +	{
   1.162 +	// insert
   1.163 +	iMap.insert(Map::value_type(aLimit, Element(aBase, aLimit, aName)));
   1.164 +	}
   1.165 +
   1.166 +void MappedCodeSpace::Done(PC aFirstPc, PC aLastPc, int aModuleId)
   1.167 +	{
   1.168 +	if (!aFirstPc)
   1.169 +		{
   1.170 +		iNames.clear();
   1.171 +		for (Map::iterator p = iMap.begin(), e = iMap.end(); p != e; ++p)
   1.172 +			{
   1.173 +			p->second.iBucket = iNames.size();
   1.174 +			iNames.push_back(p->second.iName);
   1.175 +			}
   1.176 +		}
   1.177 +	else
   1.178 +		{
   1.179 +		Map::iterator p = iMap.find(aFirstPc);
   1.180 +		_ASSERT(p != iMap.end());
   1.181 +		for (Map::iterator e = iMap.end(); p != e && p->first <= aLastPc; ++p)
   1.182 +			{
   1.183 +			if (p->second.iUnloaded) 
   1.184 +				continue;
   1.185 +
   1.186 +			bool bFound = false;
   1.187 +			NamesMap::iterator pMap = iNamesMap.find(p->second.iName);
   1.188 +			if (pMap != iNamesMap.end())
   1.189 +				for(;!strcmp(p->second.iName, pMap->first);pMap++)
   1.190 +					if (pMap->second.iId == aModuleId)
   1.191 +						{
   1.192 +						bFound = true;
   1.193 +						break;
   1.194 +						}
   1.195 +			if (bFound)
   1.196 +				p->second.iBucket = pMap->second.iIndex;
   1.197 +			else
   1.198 +				{
   1.199 +				p->second.iBucket = iNames.size();
   1.200 +				iNames.push_back(p->second.iName);
   1.201 +				iNamesMap.insert(NamesMap::value_type(p->second.iName, IdNames(aModuleId, p->second.iBucket)));
   1.202 +				}
   1.203 +			}
   1.204 +		}
   1.205 +	}
   1.206 +