memspy/Driver/Kernel/Source/MemSpyDriverOSAdaption.cpp
branchRCL_3
changeset 44 52e343bb8f80
parent 43 ca8a1b6995f6
child 49 7fdc9a71d314
--- a/memspy/Driver/Kernel/Source/MemSpyDriverOSAdaption.cpp	Tue Aug 31 16:45:49 2010 +0300
+++ b/memspy/Driver/Kernel/Source/MemSpyDriverOSAdaption.cpp	Wed Sep 01 12:37:10 2010 +0100
@@ -22,11 +22,21 @@
 #include <nk_plat.h>
 
 #ifdef __MARM__
+
 #include <arm.h>
+// Necessary when accessing data members by steam via offsets in order
+// to prevent potential unaligned data aborts
+
+#ifdef __CC_ARM
+#define UNALIGNED_DATA_MEMBER __packed
+#endif /* __CC_ARM */
+
+#endif /* __MARM__ */
+
+#ifndef UNALIGNED_DATA_MEMBER
+#define UNALIGNED_DATA_MEMBER
 #endif
 
-// I've removed UNALIGNED_DATA_MEMBER in preference for just using memcpy to get round the potential unaligned access. -TomS
-
 // User includes
 #include "MemSpyDriverLog.h"
 #include "MemSpyDriverPAndS.h"
@@ -154,9 +164,10 @@
     {
     DThread* dThread = &aObject;
     TUint32 pTarget = reinterpret_cast<TUint32>( dThread ) + iOffset_ExitType;
-	TUint8 exitType = *reinterpret_cast<TUint8*>(pTarget);
-    TRACE( Kern::Printf( "DMemSpyDriverOSAdaptionDThread::GetExitType() - aObject: 0x%08x, ret: %d", &aObject, (TInt)exitType ) );
-    return (TExitType)exitType;
+    UNALIGNED_DATA_MEMBER TExitType* pRet = reinterpret_cast< TExitType* >( pTarget );
+    TRACE( Kern::Printf( "DMemSpyDriverOSAdaptionDThread::GetExitType() - aObject: 0x%08x, ret: 0x%08x", &aObject, pRet ) );
+    TRACE( Kern::Printf( "DMemSpyDriverOSAdaptionDThread::GetExitType() - value: %d", *pRet ) );
+    return *pRet;
     }
 
 
@@ -164,11 +175,10 @@
     {
     DThread* dThread = &aObject;
     TUint32 pTarget = reinterpret_cast<TUint32>( dThread ) + iOffset_SupervisorStackBase;
-
-	TUint32 ret;
-	memcpy(&ret, (const TAny*)pTarget, sizeof(TUint32));
-    TRACE( Kern::Printf( "DMemSpyDriverOSAdaptionDThread::GetSupervisorStackBase() - aObject: 0x%08x, ret: 0x%08x", &aObject, ret ) );
-    return ret;
+    UNALIGNED_DATA_MEMBER TUint32* pRet = reinterpret_cast< TUint32* >( pTarget );
+    TRACE( Kern::Printf( "DMemSpyDriverOSAdaptionDThread::GetSupervisorStackBase() - aObject: 0x%08x, ret: 0x%08x", &aObject, pRet ) );
+    TRACE( Kern::Printf( "DMemSpyDriverOSAdaptionDThread::GetSupervisorStackBase() - 0x%08x: %d", *pRet ) );
+    return *pRet;
     }
 
 
@@ -176,11 +186,10 @@
     {
     DThread* dThread = &aObject;
     TUint32 pTarget = reinterpret_cast<TUint32>( dThread ) + iOffset_SupervisorStackSize;
-	
-	TInt ret;
-	memcpy(&ret, (const TAny*)pTarget, sizeof(TInt));
-    TRACE( Kern::Printf( "DMemSpyDriverOSAdaptionDThread::GetSupervisorStackSize() - aObject: 0x%08x, ret: %d", &aObject, ret ) );
-    return ret;
+    UNALIGNED_DATA_MEMBER TInt* pRet = reinterpret_cast< TInt* >( pTarget );
+    TRACE( Kern::Printf( "DMemSpyDriverOSAdaptionDThread::GetSupervisorStackSize() - aObject: 0x%08x, ret: 0x%08x", &aObject, pRet ) );
+    TRACE( Kern::Printf( "DMemSpyDriverOSAdaptionDThread::GetSupervisorStackSize() - value: %d", *pRet ) );
+    return *pRet;
     }
 
 
@@ -439,23 +448,9 @@
     }
 
 
-DThread* DMemSpyDriverOSAdaptionDProcess::OpenFirstThread( DProcess& aProcess ) const
+DThread* DMemSpyDriverOSAdaptionDProcess::GetFirstThread( DProcess& aObject ) const
     {
-	// It appears that the system lock needs to be held while manipulating the iThreadQ
-	DThread* result = NULL;
-	NKern::LockSystem();
-	// We don't use DProcess::FirstThread() as that doesn't appear to do any checking of whether the list is empty, ie if there are no threads at all
-	SDblQueLink* threadLink = aProcess.iThreadQ.First();
-	if (threadLink != NULL && threadLink != &aProcess.iThreadQ.iA)
-		{
-		result = _LOFF(threadLink,DThread,iProcessLink);
-		if (result->Open() != KErrNone)
-			{
-			result = NULL;
-			}
-		}
-	NKern::UnlockSystem();
-    return result;
+    return aObject.FirstThread();
     }
 
 
@@ -550,11 +545,6 @@
     return (TUint8*)aObject.iDataBssStackChunk;
     }
 
-TBool DMemSpyDriverOSAdaptionDProcess::IsKernProcess(DProcess& aProcess) const
-	{
-	// The kernel process always has pid 1
-	return GetId(aProcess) == 1;
-	}
 
 
 
@@ -576,32 +566,9 @@
     }
 
 
-TUint8* DMemSpyDriverOSAdaptionDChunk::GetBase( DChunk& aChunk ) const
+TUint8* DMemSpyDriverOSAdaptionDChunk::GetBase( DChunk& aObject ) const
     {
-    TUint8* base = aChunk.Base();
-	if (base == 0)
-		{
-		// Under flexible memory model, DChunk::Base() will return NULL (for non-fixed chunks anyway, and that means most of them)
-		// A more useful thing to return is the base address in the owning process
-		DProcess* proc = GetOwningProcess(aChunk);
-		NKern::ThreadEnterCS();
-		if (proc && proc->Open() == KErrNone)
-			{
-			// Probably shouldn't call ChunkUserBase for a non-user-owned chunk
-			if (!OSAdaption().DProcess().IsKernProcess(*proc))
-				{
-				DThread* firstThread = OSAdaption().DProcess().OpenFirstThread(*proc);
-				if (firstThread)
-					{
-					base = Kern::ChunkUserBase(&aChunk, firstThread);
-					firstThread->Close(NULL);
-					}
-				}
-			proc->Close(NULL);
-			}
-		NKern::ThreadLeaveCS();
-		}
-	return base; 
+    return aObject.Base();
     }