userlibandfileserver/fileserver/ftrace/d_ftrace.cpp
changeset 0 a41df078684a
child 4 56f325a607ea
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "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 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 // D_FTRACE.CPP
       
    18 //
       
    19 //
       
    20 //
       
    21 #include "plat_priv.h"
       
    22 #include <kernel/kernel.h>
       
    23 
       
    24 #include "f32trace.h"
       
    25 
       
    26 #define __DLOGICAL_CHANNEL_BASE__
       
    27 
       
    28 #ifdef __DLOGICAL_CHANNEL_BASE__
       
    29 DMutex* TheTraceMutex = NULL;
       
    30 _LIT(KLitTraceMutexName, "FTRACE_MUTEX");
       
    31 #else
       
    32 TDynamicDfcQue* gDfcQ;
       
    33 const TInt KDFTraceThreadPriority = 27;
       
    34 _LIT(KDFTraceThread,"DFTraceThread");
       
    35 #endif
       
    36 
       
    37 const TInt KMajorVersionNumber=1;
       
    38 const TInt KMinorVersionNumber=0;
       
    39 const TInt KBuildVersionNumber=0;
       
    40 
       
    41 
       
    42 class DLddFactoryFTrace : public DLogicalDevice
       
    43 	{
       
    44 public:
       
    45 	DLddFactoryFTrace();
       
    46 	virtual ~DLddFactoryFTrace();
       
    47 	virtual TInt Install();
       
    48 	virtual void GetCaps(TDes8 &aDes) const;
       
    49 	virtual TInt Create(DLogicalChannelBase*& aChannel); 	//overriding pure virtual
       
    50 	};
       
    51 
       
    52 #ifdef __DLOGICAL_CHANNEL_BASE__
       
    53 class DLddFTrace : public DLogicalChannelBase
       
    54 #else
       
    55 class DLddFTrace : public DLogicalChannel
       
    56 #endif
       
    57 	{
       
    58 public:
       
    59 	DLddFTrace();
       
    60 	~DLddFTrace();
       
    61 protected:
       
    62 	virtual TInt DoCreate(TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
       
    63 
       
    64 #ifdef __DLOGICAL_CHANNEL_BASE__
       
    65 	virtual TInt Request(TInt aReqNo, TAny* a1, TAny* a2);
       
    66 #else
       
    67 	virtual void HandleMsg(class TMessageBase *);
       
    68 #endif
       
    69 
       
    70 private:
       
    71 	void DoCancel(TInt aReqNo);
       
    72 	TInt DoRequest(TInt aReqNo, TRequestStatus* aStatus, TAny* a1, TAny* a2);
       
    73 	TInt DoControl(TInt aFunction, TAny* a1, TAny* a2);
       
    74 
       
    75 private:
       
    76 	DThread* iClient;
       
    77     };
       
    78 
       
    79 DECLARE_STANDARD_LDD()
       
    80 	{
       
    81 #ifdef __DLOGICAL_CHANNEL_BASE__
       
    82 	TInt r = Kern::MutexCreate(TheTraceMutex,  KLitTraceMutexName, KMutexOrdNone);
       
    83 	if (r != KErrNone)
       
    84 		return NULL;
       
    85 #endif
       
    86 
       
    87 	return new DLddFactoryFTrace;
       
    88 	}
       
    89 
       
    90 DLddFactoryFTrace::DLddFactoryFTrace()
       
    91 	{
       
    92 
       
    93     iParseMask=KDeviceAllowUnit;  // Pass stack number as unit
       
    94 	iUnitsMask=0xffffffff;
       
    95 	iVersion=TVersion(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber);
       
    96 	}
       
    97 
       
    98 TInt DLddFactoryFTrace::Create(DLogicalChannelBase*& aChannel)
       
    99 	{
       
   100 	aChannel=new DLddFTrace;
       
   101 	return aChannel ? KErrNone : KErrNoMemory;
       
   102 	}
       
   103 
       
   104 TInt DLddFactoryFTrace::Install()
       
   105 	{
       
   106 #ifndef __DLOGICAL_CHANNEL_BASE__
       
   107 	// Allocate a kernel thread to run the DFC 
       
   108 	TInt r = Kern::DynamicDfcQCreate(gDfcQ, KDFTraceThreadPriority, KDFTraceThread);
       
   109 	if (r != KErrNone)
       
   110 		return r; 	
       
   111 #endif
       
   112 
       
   113     TPtrC name=_L("FTrace");
       
   114 	return(SetName(&name));
       
   115 	}
       
   116 
       
   117 void DLddFactoryFTrace::GetCaps(TDes8& /*aDes*/) const
       
   118 	{
       
   119 	}
       
   120 
       
   121 DLddFactoryFTrace::~DLddFactoryFTrace()
       
   122 	{
       
   123 #ifndef __DLOGICAL_CHANNEL_BASE__
       
   124 	if (gDfcQ)
       
   125 		gDfcQ->Destroy();
       
   126 #endif
       
   127 	}
       
   128 
       
   129 DLddFTrace::DLddFTrace()
       
   130 	{
       
   131 	iClient=&Kern::CurrentThread();
       
   132 	((DObject*)iClient)->Open();	// can't fail since thread is running
       
   133     }
       
   134 
       
   135 DLddFTrace::~DLddFTrace()
       
   136 	{
       
   137   	Kern::SafeClose((DObject*&)iClient,NULL);
       
   138     }
       
   139 
       
   140 TInt DLddFTrace::DoCreate(TInt /*aUnit*/, const TDesC8* /*aInfo*/, const TVersion& aVer)
       
   141 	{
       
   142 
       
   143 	if (!Kern::QueryVersionSupported(TVersion(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber),aVer))
       
   144 		return(KErrNotSupported);
       
   145 
       
   146 #ifndef __DLOGICAL_CHANNEL_BASE__
       
   147 	SetDfcQ(gDfcQ);
       
   148 	iMsgQ.Receive();
       
   149 #endif
       
   150 
       
   151     return(KErrNone);
       
   152 	}
       
   153 
       
   154 void DLddFTrace::DoCancel(TInt /*aReqNo*/)
       
   155 	{
       
   156 	}
       
   157 
       
   158 #ifdef __DLOGICAL_CHANNEL_BASE__
       
   159 TInt DLddFTrace::Request(TInt aReqNo, TAny* a1, TAny* a2)
       
   160 	{
       
   161 	NKern::ThreadEnterCS();
       
   162 	Kern::MutexWait(*TheTraceMutex);
       
   163 	TInt r = DoControl(aReqNo, a1, a2);
       
   164 	Kern::MutexSignal(*TheTraceMutex);
       
   165 	NKern::ThreadLeaveCS();
       
   166 
       
   167 	return r;
       
   168 	}
       
   169 
       
   170 #else
       
   171 
       
   172 void DLddFTrace::HandleMsg(TMessageBase* aMsg)
       
   173     {
       
   174     TThreadMessage& m=*(TThreadMessage*)aMsg;
       
   175     TInt id=m.iValue;
       
   176     
       
   177 	if (id==(TInt)ECloseMsg)
       
   178 		{
       
   179 		m.Complete(KErrNone, EFalse);
       
   180 		return;
       
   181 		}
       
   182     else if (id==KMaxTInt)
       
   183 		{
       
   184 		// DoCancel
       
   185 		m.Complete(KErrNone,ETrue);
       
   186 		return;
       
   187 		}
       
   188 
       
   189     if (id<0)
       
   190 		{
       
   191 		// DoRequest
       
   192 		TRequestStatus* pS=(TRequestStatus*)m.Ptr0();
       
   193 		
       
   194 		// WDP FIXME change this to use the Kern::RequestComplete() API which doesn't take a thread pointer
       
   195 		// when this becomes available
       
   196     	Kern::RequestComplete(iClient, pS, KErrNotSupported);
       
   197 		m.Complete(KErrNotSupported, ETrue);
       
   198 		}
       
   199     else
       
   200 		{
       
   201 		// DoControl
       
   202 		TInt r=DoControl(id, m.Ptr0(), m.Ptr1());
       
   203 		m.Complete(r,ETrue);
       
   204 		}
       
   205 	}
       
   206 #endif	// __DLOGICAL_CHANNEL_BASE__
       
   207 
       
   208 const TUint KTraceBufferSize = 4096;
       
   209 TUint8 gTraceBuffer[KTraceBufferSize];
       
   210 
       
   211 
       
   212 
       
   213 #define MIN(a,b)			((a) < (b) ? (a) : (b))
       
   214 
       
   215 TInt DLddFTrace::DoControl(TInt aFunction, TAny* a1, TAny* a2)
       
   216 //
       
   217 // Mostly requests (but some kernel server async ones)
       
   218 //
       
   219 	{
       
   220 	TInt r=KErrNotSupported;
       
   221 	switch (aFunction)
       
   222 		{
       
   223         case RFTrace::ETraceMultiple:
       
   224             {
       
   225 			typedef struct {
       
   226 				TClassification iCategory;
       
   227 				TUint8 iPadding1[sizeof(TUint) - sizeof(TClassification)];
       
   228 
       
   229 				TFormatId iFormatId;
       
   230 				TUint8 iPadding2[sizeof(TUint) - sizeof(TFormatId)];
       
   231 
       
   232 				TUint32 iUid;
       
   233 				TInt iDescriptorCount;
       
   234 				} TraceArgs;
       
   235 
       
   236 			TraceArgs args={0};
       
   237 
       
   238 #ifdef __DLOGICAL_CHANNEL_BASE__
       
   239 			XTRAP(r, XT_DEFAULT, kumemget32(&args, a1, sizeof(args)));
       
   240 			if (r != KErrNone)
       
   241 				return r;
       
   242 #else
       
   243 			r = Kern::ThreadRawRead(iClient, a1, &args, sizeof(args));
       
   244 			if (r != KErrNone)
       
   245 				return r;
       
   246 #endif
       
   247 
       
   248 
       
   249 			// current descriptor - MUST be either a TPtr8 or a TBuf8<4>
       
   250 			TUint32 desc[2] = {0, 0};	
       
   251 			TUint32& desLength = desc[0];
       
   252 
       
   253 			TUint offset = 0;
       
   254 
       
   255 			*((TUint*) (gTraceBuffer+offset)) = args.iFormatId;
       
   256 			offset+= sizeof(TUint);
       
   257 
       
   258 			TDesC8* des = (TDesC8*) ((TUint8*) a2);
       
   259 			const TInt desSize = sizeof(TPtrC8);
       
   260 			for (TInt n=0; n< args.iDescriptorCount; n++, des = (TDesC8*) (((TUint8*) des) + desSize) )
       
   261 				{
       
   262 
       
   263 #ifdef __DLOGICAL_CHANNEL_BASE__
       
   264 				XTRAP(r, XT_DEFAULT, kumemget32(desc, des, sizeof(desc)));
       
   265 #else
       
   266 				r = Kern::ThreadRawRead(iClient, des, desc, sizeof(desc));
       
   267 				if (r != KErrNone)
       
   268 					return r;
       
   269 #endif
       
   270 				TUint32 desType = desLength >> KShiftDesType;
       
   271 				desLength &= (TUint) (KMaskDesLength);
       
   272 				if (desType == EPtrC)
       
   273 					{
       
   274 					*((TUint*) (gTraceBuffer+offset)) = desLength;
       
   275 					desLength = (desLength+3)&~3;
       
   276 					offset+= sizeof(TUint);
       
   277 					}
       
   278 				else if (desType == EBufC)
       
   279 					{
       
   280 					*((TUint*) (gTraceBuffer+offset)) = desc[1];
       
   281 					offset+= sizeof(TUint);
       
   282 					if (desLength > 4)
       
   283 						return KErrArgument;
       
   284 					desLength = 0;
       
   285 					continue;
       
   286 					}
       
   287 				else
       
   288 					return KErrArgument;
       
   289 
       
   290 				TUint len = MIN(KTraceBufferSize - offset, desLength);
       
   291 #ifdef __DLOGICAL_CHANNEL_BASE__
       
   292 				XTRAP(r, XT_DEFAULT, kumemget(gTraceBuffer+offset, (const TUint8*) desc[1], len));
       
   293 #else
       
   294 				TPtr8 dest(gTraceBuffer+offset, len, len);
       
   295 				r = Kern::ThreadDesRead(iClient, des, dest, 0, KChunkShiftBy0);
       
   296 				if (r != KErrNone)
       
   297 					return r;
       
   298 #endif
       
   299 				offset+= len;
       
   300 				
       
   301 				}
       
   302 
       
   303 			BTrace::OutFilteredBig 
       
   304 				(BTRACE_HEADER_C(8,args.iCategory, 0), args.iUid, gTraceBuffer, offset);
       
   305 
       
   306 			r=KErrNone;
       
   307 			break;
       
   308             }
       
   309 		}
       
   310 	return(r);
       
   311 	}
       
   312