userlibandfileserver/fileserver/ftrace/d_ftrace.cpp
changeset 31 56f325a607ea
parent 0 a41df078684a
child 258 880ff05ad710
equal deleted inserted replaced
15:4122176ea935 31:56f325a607ea
    21 #include "plat_priv.h"
    21 #include "plat_priv.h"
    22 #include <kernel/kernel.h>
    22 #include <kernel/kernel.h>
    23 
    23 
    24 #include "f32trace.h"
    24 #include "f32trace.h"
    25 
    25 
    26 #define __DLOGICAL_CHANNEL_BASE__
       
    27 
       
    28 #ifdef __DLOGICAL_CHANNEL_BASE__
       
    29 DMutex* TheTraceMutex = NULL;
    26 DMutex* TheTraceMutex = NULL;
    30 _LIT(KLitTraceMutexName, "FTRACE_MUTEX");
    27 _LIT(KLitTraceMutexName, "FTRACE_MUTEX");
    31 #else
       
    32 TDynamicDfcQue* gDfcQ;
       
    33 const TInt KDFTraceThreadPriority = 27;
       
    34 _LIT(KDFTraceThread,"DFTraceThread");
       
    35 #endif
       
    36 
    28 
    37 const TInt KMajorVersionNumber=1;
    29 const TInt KMajorVersionNumber=1;
    38 const TInt KMinorVersionNumber=0;
    30 const TInt KMinorVersionNumber=0;
    39 const TInt KBuildVersionNumber=0;
    31 const TInt KBuildVersionNumber=0;
    40 
    32 
    47 	virtual TInt Install();
    39 	virtual TInt Install();
    48 	virtual void GetCaps(TDes8 &aDes) const;
    40 	virtual void GetCaps(TDes8 &aDes) const;
    49 	virtual TInt Create(DLogicalChannelBase*& aChannel); 	//overriding pure virtual
    41 	virtual TInt Create(DLogicalChannelBase*& aChannel); 	//overriding pure virtual
    50 	};
    42 	};
    51 
    43 
    52 #ifdef __DLOGICAL_CHANNEL_BASE__
       
    53 class DLddFTrace : public DLogicalChannelBase
    44 class DLddFTrace : public DLogicalChannelBase
    54 #else
       
    55 class DLddFTrace : public DLogicalChannel
       
    56 #endif
       
    57 	{
    45 	{
    58 public:
    46 public:
    59 	DLddFTrace();
    47 	DLddFTrace();
    60 	~DLddFTrace();
    48 	~DLddFTrace();
    61 protected:
    49 protected:
    62 	virtual TInt DoCreate(TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
    50 	virtual TInt DoCreate(TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
    63 
    51 
    64 #ifdef __DLOGICAL_CHANNEL_BASE__
       
    65 	virtual TInt Request(TInt aReqNo, TAny* a1, TAny* a2);
    52 	virtual TInt Request(TInt aReqNo, TAny* a1, TAny* a2);
    66 #else
       
    67 	virtual void HandleMsg(class TMessageBase *);
       
    68 #endif
       
    69 
    53 
    70 private:
    54 private:
    71 	void DoCancel(TInt aReqNo);
    55 	void DoCancel(TInt aReqNo);
    72 	TInt DoRequest(TInt aReqNo, TRequestStatus* aStatus, TAny* a1, TAny* a2);
    56 	TInt DoRequest(TInt aReqNo, TRequestStatus* aStatus, TAny* a1, TAny* a2);
    73 	TInt DoControl(TInt aFunction, TAny* a1, TAny* a2);
    57 	TInt DoControl(TInt aFunction, TAny* a1, TAny* a2);
    74 
    58 
    75 private:
    59 private:
    76 	DThread* iClient;
       
    77     };
    60     };
    78 
    61 
    79 DECLARE_STANDARD_LDD()
    62 DECLARE_STANDARD_LDD()
    80 	{
    63 	{
    81 #ifdef __DLOGICAL_CHANNEL_BASE__
       
    82 	TInt r = Kern::MutexCreate(TheTraceMutex,  KLitTraceMutexName, KMutexOrdNone);
    64 	TInt r = Kern::MutexCreate(TheTraceMutex,  KLitTraceMutexName, KMutexOrdNone);
    83 	if (r != KErrNone)
    65 	if (r != KErrNone)
    84 		return NULL;
    66 		return NULL;
    85 #endif
       
    86 
    67 
    87 	return new DLddFactoryFTrace;
    68 	return new DLddFactoryFTrace;
    88 	}
    69 	}
    89 
    70 
    90 DLddFactoryFTrace::DLddFactoryFTrace()
    71 DLddFactoryFTrace::DLddFactoryFTrace()
   101 	return aChannel ? KErrNone : KErrNoMemory;
    82 	return aChannel ? KErrNone : KErrNoMemory;
   102 	}
    83 	}
   103 
    84 
   104 TInt DLddFactoryFTrace::Install()
    85 TInt DLddFactoryFTrace::Install()
   105 	{
    86 	{
   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");
    87     TPtrC name=_L("FTrace");
   114 	return(SetName(&name));
    88 	return(SetName(&name));
   115 	}
    89 	}
   116 
    90 
   117 void DLddFactoryFTrace::GetCaps(TDes8& /*aDes*/) const
    91 void DLddFactoryFTrace::GetCaps(TDes8& /*aDes*/) const
   118 	{
    92 	{
   119 	}
    93 	}
   120 
    94 
   121 DLddFactoryFTrace::~DLddFactoryFTrace()
    95 DLddFactoryFTrace::~DLddFactoryFTrace()
   122 	{
    96 	{
   123 #ifndef __DLOGICAL_CHANNEL_BASE__
       
   124 	if (gDfcQ)
       
   125 		gDfcQ->Destroy();
       
   126 #endif
       
   127 	}
    97 	}
   128 
    98 
   129 DLddFTrace::DLddFTrace()
    99 DLddFTrace::DLddFTrace()
   130 	{
   100 	{
   131 	iClient=&Kern::CurrentThread();
       
   132 	((DObject*)iClient)->Open();	// can't fail since thread is running
       
   133     }
   101     }
   134 
   102 
   135 DLddFTrace::~DLddFTrace()
   103 DLddFTrace::~DLddFTrace()
   136 	{
   104 	{
   137   	Kern::SafeClose((DObject*&)iClient,NULL);
       
   138     }
   105     }
   139 
   106 
   140 TInt DLddFTrace::DoCreate(TInt /*aUnit*/, const TDesC8* /*aInfo*/, const TVersion& aVer)
   107 TInt DLddFTrace::DoCreate(TInt /*aUnit*/, const TDesC8* /*aInfo*/, const TVersion& aVer)
   141 	{
   108 	{
   142 
   109 
   143 	if (!Kern::QueryVersionSupported(TVersion(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber),aVer))
   110 	if (!Kern::QueryVersionSupported(TVersion(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber),aVer))
   144 		return(KErrNotSupported);
   111 		return(KErrNotSupported);
   145 
   112 
   146 #ifndef __DLOGICAL_CHANNEL_BASE__
       
   147 	SetDfcQ(gDfcQ);
       
   148 	iMsgQ.Receive();
       
   149 #endif
       
   150 
       
   151     return(KErrNone);
   113     return(KErrNone);
   152 	}
   114 	}
   153 
   115 
   154 void DLddFTrace::DoCancel(TInt /*aReqNo*/)
   116 void DLddFTrace::DoCancel(TInt /*aReqNo*/)
   155 	{
   117 	{
   156 	}
   118 	}
   157 
   119 
   158 #ifdef __DLOGICAL_CHANNEL_BASE__
       
   159 TInt DLddFTrace::Request(TInt aReqNo, TAny* a1, TAny* a2)
   120 TInt DLddFTrace::Request(TInt aReqNo, TAny* a1, TAny* a2)
   160 	{
   121 	{
   161 	NKern::ThreadEnterCS();
   122 	NKern::ThreadEnterCS();
   162 	Kern::MutexWait(*TheTraceMutex);
   123 	Kern::MutexWait(*TheTraceMutex);
   163 	TInt r = DoControl(aReqNo, a1, a2);
   124 	TInt r = DoControl(aReqNo, a1, a2);
   165 	NKern::ThreadLeaveCS();
   126 	NKern::ThreadLeaveCS();
   166 
   127 
   167 	return r;
   128 	return r;
   168 	}
   129 	}
   169 
   130 
   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 
   131 
   208 const TUint KTraceBufferSize = 4096;
   132 const TUint KTraceBufferSize = 4096;
   209 TUint8 gTraceBuffer[KTraceBufferSize];
   133 TUint8 gTraceBuffer[KTraceBufferSize];
   210 
       
   211 
   134 
   212 
   135 
   213 #define MIN(a,b)			((a) < (b) ? (a) : (b))
   136 #define MIN(a,b)			((a) < (b) ? (a) : (b))
   214 
   137 
   215 TInt DLddFTrace::DoControl(TInt aFunction, TAny* a1, TAny* a2)
   138 TInt DLddFTrace::DoControl(TInt aFunction, TAny* a1, TAny* a2)
   233 				TInt iDescriptorCount;
   156 				TInt iDescriptorCount;
   234 				} TraceArgs;
   157 				} TraceArgs;
   235 
   158 
   236 			TraceArgs args={0};
   159 			TraceArgs args={0};
   237 
   160 
   238 #ifdef __DLOGICAL_CHANNEL_BASE__
       
   239 			XTRAP(r, XT_DEFAULT, kumemget32(&args, a1, sizeof(args)));
   161 			XTRAP(r, XT_DEFAULT, kumemget32(&args, a1, sizeof(args)));
   240 			if (r != KErrNone)
   162 			if (r != KErrNone)
   241 				return r;
   163 				return r;
   242 #else
       
   243 			r = Kern::ThreadRawRead(iClient, a1, &args, sizeof(args));
       
   244 			if (r != KErrNone)
       
   245 				return r;
       
   246 #endif
       
   247 
       
   248 
   164 
   249 			// current descriptor - MUST be either a TPtr8 or a TBuf8<4>
   165 			// current descriptor - MUST be either a TPtr8 or a TBuf8<4>
   250 			TUint32 desc[2] = {0, 0};	
   166 			TUint32 desc[2] = {0, 0};
   251 			TUint32& desLength = desc[0];
   167 			TUint32& desLength = desc[0];
   252 
   168 
   253 			TUint offset = 0;
   169 			TUint offset = 0;
   254 
   170 
   255 			*((TUint*) (gTraceBuffer+offset)) = args.iFormatId;
   171 			*((TUint*) (gTraceBuffer+offset)) = args.iFormatId;
   258 			TDesC8* des = (TDesC8*) ((TUint8*) a2);
   174 			TDesC8* des = (TDesC8*) ((TUint8*) a2);
   259 			const TInt desSize = sizeof(TPtrC8);
   175 			const TInt desSize = sizeof(TPtrC8);
   260 			for (TInt n=0; n< args.iDescriptorCount; n++, des = (TDesC8*) (((TUint8*) des) + desSize) )
   176 			for (TInt n=0; n< args.iDescriptorCount; n++, des = (TDesC8*) (((TUint8*) des) + desSize) )
   261 				{
   177 				{
   262 
   178 
   263 #ifdef __DLOGICAL_CHANNEL_BASE__
       
   264 				XTRAP(r, XT_DEFAULT, kumemget32(desc, des, sizeof(desc)));
   179 				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;
   180 				TUint32 desType = desLength >> KShiftDesType;
   271 				desLength &= (TUint) (KMaskDesLength);
   181 				desLength &= (TUint) (KMaskDesLength);
   272 				if (desType == EPtrC)
   182 				if (desType == EPtrC)
   273 					{
   183 					{
   274 					*((TUint*) (gTraceBuffer+offset)) = desLength;
   184 					*((TUint*) (gTraceBuffer+offset)) = desLength;
   286 					}
   196 					}
   287 				else
   197 				else
   288 					return KErrArgument;
   198 					return KErrArgument;
   289 
   199 
   290 				TUint len = MIN(KTraceBufferSize - offset, desLength);
   200 				TUint len = MIN(KTraceBufferSize - offset, desLength);
   291 #ifdef __DLOGICAL_CHANNEL_BASE__
       
   292 				XTRAP(r, XT_DEFAULT, kumemget(gTraceBuffer+offset, (const TUint8*) desc[1], len));
   201 				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;
   202 				offset+= len;
   300 				
   203 
   301 				}
   204 				}
   302 
   205 
   303 			BTrace::OutFilteredBig 
   206 			BTrace::OutFilteredBig
   304 				(BTRACE_HEADER_C(8,args.iCategory, 0), args.iUid, gTraceBuffer, offset);
   207 				(BTRACE_HEADER_C(8,args.iCategory, 0), args.iUid, gTraceBuffer, offset);
   305 
   208 
   306 			r=KErrNone;
   209 			r=KErrNone;
   307 			break;
   210 			break;
   308             }
   211             }