commands/getstack/getstack.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // getstack.cpp
       
     2 // 
       
     3 // Copyright (c) 2007 - 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 
       
    13 #include "getstack.h"
       
    14 #include <fshell/memoryaccess.h>
       
    15 
       
    16 
       
    17 CCommandBase* CCmdGetStack::NewLC()
       
    18 	{
       
    19 	CCmdGetStack* self = new(ELeave) CCmdGetStack();
       
    20 	CleanupStack::PushL(self);
       
    21 	self->BaseConstructL();
       
    22 	return self;
       
    23 	}
       
    24 
       
    25 CCmdGetStack::~CCmdGetStack()
       
    26 	{
       
    27 	}
       
    28 
       
    29 CCmdGetStack::CCmdGetStack()
       
    30 	{
       
    31 	}
       
    32 
       
    33 const TDesC& CCmdGetStack::Name() const
       
    34 	{
       
    35 	_LIT(KName, "getstack");
       
    36 	return KName;
       
    37 	}
       
    38 
       
    39 void CCmdGetStack::DoRunL()
       
    40 	{
       
    41 	RMemoryAccess memoryAccess;
       
    42 	TInt err = RMemoryAccess::LoadDriver();
       
    43 	if ((err == KErrNone) || (err == KErrAlreadyExists))
       
    44 		{
       
    45 		User::LeaveIfError(memoryAccess.Open());
       
    46 		CleanupClosePushL(memoryAccess);
       
    47 		}
       
    48 	else
       
    49 		{
       
    50 		User::LeaveIfError(err);
       
    51 		}
       
    52 	RThread thread;
       
    53 	User::LeaveIfError(thread.Open(iThreadId));
       
    54 	CleanupClosePushL(thread);
       
    55 
       
    56 	RFile file;
       
    57 	User::LeaveIfError(file.Create(FsL(), iFileName, EFileWrite | EFileStream));
       
    58 	CleanupClosePushL(file);
       
    59 
       
    60 	TThreadStackInfo stackInfo;
       
    61 	User::LeaveIfError(thread.StackInfo(stackInfo));
       
    62 	const TInt stackSize = stackInfo.iBase - stackInfo.iLimit;
       
    63 
       
    64 	const TInt KBufSize = 4096; // The largest amount RMemoryAccess allows us to copy in one go.
       
    65 	HBufC8* stackBuf = HBufC8::NewLC(KBufSize);
       
    66 	TPtr8 stackBufPtr(stackBuf->Des());
       
    67 
       
    68 	TThreadMemoryAccessParamsBuf accessParamsBuf;
       
    69 	TThreadMemoryAccessParams& accessParams = accessParamsBuf();
       
    70 	accessParams.iId = (TInt)thread.Id().Id();
       
    71 
       
    72 	TInt numBytesRead = 0;
       
    73 	while (numBytesRead < stackSize)
       
    74 		{
       
    75 		accessParams.iAddr = (TUint8*)stackInfo.iLimit + numBytesRead;
       
    76 		accessParams.iSize = Min(KBufSize, stackSize - numBytesRead);
       
    77 		stackBufPtr.Zero();
       
    78 		User::LeaveIfError(memoryAccess.GetThreadMem(accessParamsBuf, stackBufPtr));
       
    79 		User::LeaveIfError(file.Write(*stackBuf));
       
    80 		numBytesRead += stackBuf->Length();
       
    81 		}
       
    82 
       
    83 	CleanupStack::PopAndDestroy(4, &memoryAccess);
       
    84 	}
       
    85 
       
    86 void CCmdGetStack::ArgumentsL(RCommandArgumentList& aArguments)
       
    87 	{
       
    88 	_LIT(KArgFileName, "file_name");
       
    89 	aArguments.AppendFileNameL(iFileName, KArgFileName);
       
    90 
       
    91 	_LIT(KArgThreadId, "thread_id");
       
    92 	aArguments.AppendUintL(iThreadId, KArgThreadId);
       
    93 	}
       
    94 
       
    95 
       
    96 #ifdef EXE_BUILD
       
    97 EXE_BOILER_PLATE(CCmdGetStack)
       
    98 #endif
       
    99