libraries/ltkutils/src/symbolics.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // symbolics.cpp
       
     2 // 
       
     3 // Copyright (c) 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "Eclipse Public License v1.0"
       
     6 // which accompanies this distribution, and is available
       
     7 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 // 
       
     9 // Initial Contributors:
       
    10 // Accenture - Initial contribution
       
    11 //
       
    12 #include <fshell/bsym.h>
       
    13 #include <fshell/ltkutils.h>
       
    14 #include <fshell/stringhash.h>
       
    15 #include <fshell/iocli.h>
       
    16 #include "bsymtree.h"
       
    17 
       
    18 using namespace LtkUtils;
       
    19 using namespace IoUtils;
       
    20 
       
    21 EXPORT_C CSymbolics::CSymbolics(RFs& aFs)
       
    22 	: iFs(aFs)
       
    23 	{
       
    24 	}
       
    25 
       
    26 EXPORT_C CSymbolics::~CSymbolics()
       
    27 	{
       
    28 	iBsyms.ResetAndDestroy();
       
    29 	TStringHashIter<CMapFile*> iter(iCodeSegHash);
       
    30 	while (iter.NextValue() != NULL)
       
    31 		{
       
    32 		CMapFile** file = iter.CurrentValue();
       
    33 		delete *file;
       
    34 		}
       
    35 	iCodeSegHash.Close();
       
    36 	delete iTabCompleteTree;
       
    37 	delete iTabCompleteCodeseg;
       
    38 	}
       
    39 
       
    40 EXPORT_C void CSymbolics::AddBsymFileL(const TDesC& aFileName)
       
    41 	{
       
    42 	CBsymFile* file = CBsymFile::NewL(iFs, aFileName);
       
    43 	TInt err = iBsyms.Append(file);
       
    44 	if (err)
       
    45 		{
       
    46 		delete file;
       
    47 		User::Leave(err);
       
    48 		}
       
    49 	}
       
    50 
       
    51 EXPORT_C void CSymbolics::AddMapFileL(const TDesC& aFileName)
       
    52 	{
       
    53 	CMapFile* file = CMapFile::NewL(iFs, aFileName);
       
    54 	CleanupStack::PushL(file);
       
    55 	AddMapFileL(file);
       
    56 	CleanupStack::Pop(file);
       
    57 	}
       
    58 
       
    59 EXPORT_C void CSymbolics::AddMapFileL(CMapFile* aMapFile)
       
    60 	{
       
    61 	aMapFile->GetFileNameL(iTempString);
       
    62 	TParsePtrC parse(iTempString);
       
    63 	TPtrC name = parse.Name(); // Don't use NameAndExt, that will include the .map!
       
    64 	TPtr namePtr((TUint16*)name.Ptr(), name.Length(), name.Length()); // Nasty but TParsePtr won't give us a non-const Name().
       
    65 	namePtr.LowerCase();
       
    66 	User::LeaveIfError(iCodeSegHash.Insert(namePtr, aMapFile));
       
    67 	}
       
    68 
       
    69 EXPORT_C void CSymbolics::SetFallbackMapFileDirL(const TDesC& aDir)
       
    70 	{
       
    71 	iFallbackMapFileDir = aDir;
       
    72 	}
       
    73 
       
    74 EXPORT_C TPtrC CSymbolics::LookupL(TUint32 aRomAddress)
       
    75 	{
       
    76 	for (TInt i = 0; i < iBsyms.Count(); i++)
       
    77 		{
       
    78 		TPtrC res = iBsyms[i]->LookupL(aRomAddress);
       
    79 		if (res.Length()) return res;
       
    80 		}
       
    81 	return TPtrC();
       
    82 	}
       
    83 
       
    84 EXPORT_C TPtrC CSymbolics::LookupL(const TDesC& aCodeseg, TUint32 aOffset)
       
    85 	{
       
    86 	CMapFile* mapFile = FindOrLoadMapFileL(aCodeseg);
       
    87 
       
    88 	iTempString.Zero();
       
    89 	if (mapFile)
       
    90 		{
       
    91 		mapFile->Lookup(aOffset, iTempString);
       
    92 		if (iTempString.Length())
       
    93 			{
       
    94 			iTempString.AppendFormat(_L(" (%S)"), &aCodeseg);
       
    95 			}
       
    96 		}
       
    97 	else
       
    98 		{
       
    99 		// Try BSYMs in case it's in ROFS
       
   100 		for (TInt i = 0; i < iBsyms.Count(); i++)
       
   101 			{
       
   102 			TPtrC res = iBsyms[i]->LookupL(aCodeseg, aOffset);
       
   103 			}
       
   104 		}
       
   105 	return TPtrC(iTempString);
       
   106 	}
       
   107 
       
   108 EXPORT_C void CSymbolics::CompleteL(const TDesC& aCodeseg, TDes& aSymbolName, CDesC16Array& aSuggestions)
       
   109 	{
       
   110 	RNode* tree = TreeForCodesegL(aCodeseg);
       
   111 	if (tree)
       
   112 		{
       
   113 		tree->CompleteL(aSymbolName, aSuggestions);
       
   114 		}
       
   115 	}
       
   116 
       
   117 RNode* CSymbolics::TreeForCodesegL(const TDesC& aCodeseg)
       
   118 	{
       
   119 	iTempString.Copy(aCodeseg);
       
   120 	iTempString.LowerCase();
       
   121 	if (iTabCompleteCodeseg && *iTabCompleteCodeseg != iTempString)
       
   122 		{
       
   123 		// If iTabCompleteCodeseg is set it means that iTabCompleteTree (if it's non-null) matches.
       
   124 		delete iTabCompleteCodeseg;
       
   125 		iTabCompleteCodeseg = NULL;
       
   126 		delete iTabCompleteTree;
       
   127 		iTabCompleteTree = NULL;
       
   128 		}
       
   129 	if (!iTabCompleteCodeseg) iTabCompleteCodeseg = iTempString.AllocL();
       
   130 
       
   131 	if (iTabCompleteTree == NULL)
       
   132 		{
       
   133 		for (TInt i = 0; i < iBsyms.Count(); i++)
       
   134 			{
       
   135 			RNode* tree = iBsyms[i]->CreateCompletionTreeL(*iTabCompleteCodeseg);
       
   136 			if (tree)
       
   137 				{
       
   138 				iTabCompleteTree = tree;
       
   139 				break;
       
   140 				}
       
   141 			}
       
   142 		}
       
   143 	// Try the map files now
       
   144 	if (iTabCompleteTree == NULL)
       
   145 		{
       
   146 		CMapFile* mapFile = FindOrLoadMapFileL(aCodeseg);
       
   147 		if (mapFile)
       
   148 			{
       
   149 			iTabCompleteTree = mapFile->CreateCompletionTreeL();
       
   150 			}
       
   151 		}
       
   152 	return iTabCompleteTree;
       
   153 	}
       
   154 
       
   155 EXPORT_C TUint32 CSymbolics::CodesegOffsetFromSymbolNameL(const TDesC& aCodeseg, const TDesC& aSymbolName)
       
   156 	{
       
   157 	RNode* tree = TreeForCodesegL(aCodeseg);
       
   158 	if (!tree) User::Leave(KErrNotFound);
       
   159 	return tree->ValueForStringL(aSymbolName);
       
   160 	}
       
   161 
       
   162 CMapFile* CSymbolics::FindOrLoadMapFileL(const TDesC& aCodeseg)
       
   163 	{
       
   164 	iTempString.Copy(aCodeseg);
       
   165 	iTempString.LowerCase();
       
   166 
       
   167 	// If it's already loaded, return it
       
   168 	CMapFile* mapFile = iCodeSegHash.FindPtr(iTempString);
       
   169 	if (mapFile) return mapFile;
       
   170 
       
   171 	if (iFallbackMapFileDir.Length() == 0) return NULL;
       
   172 	// Try loading the mapfile
       
   173 	TFileName2 fn = iFallbackMapFileDir;
       
   174 	fn.AppendComponentL(iTempString);
       
   175 	fn.Append(_L(".map"));
       
   176 	TEntry e;
       
   177 	if (iFs.Entry(fn, e) == KErrNone)
       
   178 		{
       
   179 		TRAPD(err, AddMapFileL(fn));
       
   180 		StaticLeaveIfErr(err, _L("Could not load map file %S"), &fn);
       
   181 		// AddMapFileL overwrites iTempString, so restore it (nasty but I object to wasting buffers on the stack...)
       
   182 		iTempString.Copy(aCodeseg);
       
   183 		iTempString.LowerCase();
       
   184 		return iCodeSegHash.FindPtr(iTempString);
       
   185 		}
       
   186 	return NULL;
       
   187 	}