commands/ffstrace/ffstrace.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // ffstrace.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 
       
    13 #include <fshell/extrabtrace.h>
       
    14 #include <fshell/btrace_parser.h>
       
    15 
       
    16 #include <fshell/ioutils.h>
       
    17 #include <fshell/common.mmh>
       
    18 
       
    19 using namespace IoUtils;
       
    20 
       
    21 class CCmdFfstrace : public CCommandBase, public MBtraceObserver
       
    22 	{
       
    23 public:
       
    24 	static CCommandBase* NewLC();
       
    25 	~CCmdFfstrace();
       
    26 private:
       
    27 	CCmdFfstrace();
       
    28 	void HandleBtraceFrameL(const TBtraceFrame& aFrame);
       
    29 private: // From CCommandBase.
       
    30 	virtual const TDesC& Name() const;
       
    31 	virtual void DoRunL();
       
    32 	virtual void ArgumentsL(RCommandArgumentList& aArguments);
       
    33 	virtual void OptionsL(RCommandOptionList& aOptions);
       
    34 private:
       
    35 	enum TCmd
       
    36 		{
       
    37 		EMonitor,
       
    38 		ELoad,
       
    39 		EUnload,
       
    40 		};
       
    41 	TCmd iCommand;
       
    42 private:
       
    43 	CBtraceReader* iBtraceReader;
       
    44 	};
       
    45 
       
    46 EXE_BOILER_PLATE(CCmdFfstrace)
       
    47 
       
    48 CCommandBase* CCmdFfstrace::NewLC()
       
    49 	{
       
    50 	CCmdFfstrace* self = new(ELeave) CCmdFfstrace();
       
    51 	CleanupStack::PushL(self);
       
    52 	self->BaseConstructL();
       
    53 	return self;
       
    54 	}
       
    55 
       
    56 CCmdFfstrace::~CCmdFfstrace()
       
    57 	{
       
    58 	delete iBtraceReader;
       
    59 	}
       
    60 
       
    61 CCmdFfstrace::CCmdFfstrace()
       
    62 	: CCommandBase(EManualComplete)
       
    63 	{
       
    64 	}
       
    65 
       
    66 const TDesC& CCmdFfstrace::Name() const
       
    67 	{
       
    68 	_LIT(KName, "ffstrace");	
       
    69 	return KName;
       
    70 	}
       
    71 
       
    72 void CCmdFfstrace::ArgumentsL(RCommandArgumentList& aArguments)
       
    73 	{
       
    74 	aArguments.AppendEnumL((TInt&)iCommand, _L("command"));
       
    75 	}
       
    76 
       
    77 void CCmdFfstrace::OptionsL(RCommandOptionList& /*aOptions*/)
       
    78 	{
       
    79 	//TODO: aOptions.AppendBoolL(iOpt, _L("example_opt"));
       
    80 	//TODO: Also remember to update the CIF file for any options you add.
       
    81 	}
       
    82 
       
    83 void CCmdFfstrace::DoRunL()
       
    84 	{
       
    85 	_LIT(KPluginDll, "ffstraceplugin.fxt");
       
    86 	_LIT(KFfsTracerPluginName, "FfsTracePlugin");
       
    87 	if (iCommand == EMonitor || iCommand == ELoad)
       
    88 		{
       
    89 		TInt err = FsL().AddPlugin(KPluginDll);
       
    90 		if (err && err != KErrAlreadyExists) LeaveIfErr(err, _L("Couldn't load filesystem plugin %S"), &KPluginDll);
       
    91 
       
    92 		err = FsL().MountPlugin(KFfsTracerPluginName);
       
    93 		//TODO handle this being called repeatedly
       
    94 		LeaveIfErr(err, _L("Couldn't mount filesystem plugin %S"), &KFfsTracerPluginName);
       
    95 		}
       
    96 	else if (iCommand == EUnload)
       
    97 		{
       
    98 		FsL().DismountPlugin(KFfsTracerPluginName);
       
    99 		LeaveIfErr(FsL().RemovePlugin(KFfsTracerPluginName), _L("No plugin to unload"));
       
   100 		}
       
   101 
       
   102 	if (iCommand == EMonitor)
       
   103 		{
       
   104 		iBtraceReader = CBtraceReader::NewL(CBtraceReader::EFlushOnBtraceThreshold, 1024*1024, 512*1024);
       
   105 		iBtraceReader->AddObserverL(BTrace::EFfsTrace, *this);
       
   106 		iBtraceReader->SetMultipartReassemblyL(10);
       
   107 		TBtraceTickCount now;
       
   108 		now.SetToNow();
       
   109 		iBtraceReader->Start(now, 1000000);
       
   110 		}
       
   111 	else
       
   112 		{
       
   113 		Complete(KErrNone);
       
   114 		}
       
   115 	}
       
   116 
       
   117 void CCmdFfstrace::HandleBtraceFrameL(const TBtraceFrame& aFrame)
       
   118 	{
       
   119 	if (aFrame.iCategory == BTrace::EFfsTrace)
       
   120 		{
       
   121 		TInt fn = aFrame.iSubCategory & ~BTrace::EFfsPost;
       
   122 		TBool post = aFrame.iSubCategory & BTrace::EFfsPost;
       
   123 		TUint threadId = *(TUint*)aFrame.iData.Ptr();
       
   124 		TPtrC name((TUint16*)(aFrame.iData.Ptr()+4), (aFrame.iData.Size()-4)/2);
       
   125 		TPtrC newName;
       
   126 		// Not everything gets as far as post - if an error occurs during the pre checking for eg
       
   127 		if (post) Printf(_L("-"));
       
   128 		else Printf(_L("+"));
       
   129 
       
   130 		TBool twoNames = (fn == BTrace::EFfsRename || fn == BTrace::EFfsFileRename);
       
   131 		if (twoNames)
       
   132 			{
       
   133 			TInt separator = name.Locate(0);
       
   134 			if (separator != KErrNotFound)
       
   135 				{
       
   136 				newName.Set(name.Mid(separator+1));
       
   137 				name.Set(name.Left(separator));
       
   138 				}
       
   139 			}
       
   140 
       
   141 		switch (fn)
       
   142 			{
       
   143 			case BTrace::EFfsDelete:
       
   144 				Printf(_L("Delete %S"), &name);
       
   145 				break;
       
   146 			case BTrace::EFfsFileOpen:
       
   147 				Printf(_L("Open %S"), &name);
       
   148 				break;
       
   149 			case BTrace::EFfsFileCreate:
       
   150 				Printf(_L("Create %S"), &name);
       
   151 				break;
       
   152 			case BTrace::EFfsFileSubClose:
       
   153 				Printf(_L("Close %S"), &name);
       
   154 				break;
       
   155 			case BTrace::EFfsFileReplace:
       
   156 				Printf(_L("Replace %S"), &name);
       
   157 				break;
       
   158 			case BTrace::EFfsFileTemp:
       
   159 				Printf(_L("Open temp %S"), &name);
       
   160 				break;
       
   161 			case BTrace::EFfsRename:
       
   162 			case BTrace::EFfsFileRename:
       
   163 				Printf(_L("Rename from %S to %S"), &name, &newName);
       
   164 				break;
       
   165 			case BTrace::EFfsEntry:
       
   166 				Printf(_L("RFs::Entry %S"), &name);
       
   167 				break;
       
   168 			default:
       
   169 				Printf(_L("Event %d"), fn);
       
   170 				break;
       
   171 			}
       
   172 		Printf(_L(" from thread %u"), threadId);
       
   173 		RThread thread;
       
   174 		TInt err = thread.Open(threadId);
       
   175 		if (err == KErrNone)
       
   176 			{
       
   177 			Printf(_L(" "));
       
   178 			TFullName name = thread.FullName();
       
   179 			Write(name);
       
   180 			thread.Close();
       
   181 			}
       
   182 		Printf(_L("\r\n"));
       
   183 		}
       
   184 	}