Update contrib.
1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32\drivers\trace\btrace.cpp
18 #include <kernel/kern_priv.h>
20 #include "drivers/btrace.h"
22 #if defined(__EPOC32__) && defined(__CPU_X86)
26 TBTraceBufferK Buffer;
28 TBool ChannelOpen = EFalse;
30 const TUint KCopyBufferMaxSize = 0x10000;
32 TInt TBTraceBufferK::Create(TInt aSize)
36 TUint pageSize = Kern::RoundToPageSize(1);
37 aSize = (aSize+pageSize-1)&-(TInt)pageSize;
39 TUint recordOffsets = aSize+pageSize;
40 TUint recordOffsetsSize = Kern::RoundToPageSize(aSize>>2);
41 TUint copyBuffer = recordOffsets+recordOffsetsSize+pageSize;
42 TUint copyBufferSize = Kern::RoundToPageSize(aSize>>2);
43 if(copyBufferSize>KCopyBufferMaxSize)
44 copyBufferSize = KCopyBufferMaxSize;
45 TUint chunkSize = copyBuffer+copyBufferSize+pageSize;
48 TChunkCreateInfo info;
49 info.iType = TChunkCreateInfo::ESharedKernelSingle;
50 info.iMaxSize = chunkSize;
52 // we want full caching, no execute, default sharing
53 new (&info.iMapAttr) TMappingAttributes2(EMemAttNormalCached, EFalse, ETrue);
55 info.iOwnsMemory = ETrue; // Use memory from system's free pool
56 info.iDestroyedDfc = NULL;
58 TInt r = Kern::ChunkCreate(info, iBufferChunk, iAddress, mapAttr);
60 r = Kern::ChunkCommit(iBufferChunk, 0, aSize);
62 r = Kern::ChunkCommit(iBufferChunk, recordOffsets, recordOffsetsSize);
64 r = Kern::ChunkCommit(iBufferChunk, copyBuffer, copyBufferSize);
73 // Initialise state...
74 iStart = sizeof(TBTraceBuffer);
76 iRecordOffsets = (TUint8*)(iAddress+recordOffsets);
78 TBTraceBuffer* userBuffer = (TBTraceBuffer*)iAddress;
79 userBuffer->iRecordOffsets = recordOffsets;
80 userBuffer->iCopyBuffer = copyBuffer;
81 userBuffer->iCopyBufferSize = copyBufferSize;
86 TInt irq = NKern::DisableAllInterrupts();
88 iTimestamp2Enabled = EFalse;
89 BTrace::SetHandlers(TBTraceBufferK::Trace,TBTraceBufferK::ControlFunction,iOldBTraceHandler,iOldBTraceControl);
91 NKern::RestoreInterrupts(irq);
98 void TBTraceBufferK::Close()
101 if(iOldBTraceHandler)
103 BTrace::THandler handler;
104 BTrace::TControlFunction control;
105 BTrace::SetHandlers(iOldBTraceHandler,iOldBTraceControl,handler,control);
106 iOldBTraceHandler = NULL;
107 iOldBTraceControl = NULL;
109 TSpinLock* sl = BTrace::LockPtr();
110 TInt irq = sl->LockIrqSave(); // guarantees handler can't run at the same time
111 DChunk* chunk = iBufferChunk;
114 sl->UnlockIrqRestore(irq);
116 TInt irq = NKern::DisableAllInterrupts();
117 if(iOldBTraceHandler)
119 BTrace::THandler handler;
120 BTrace::TControlFunction control;
121 BTrace::SetHandlers(iOldBTraceHandler,iOldBTraceControl,handler,control);
122 iOldBTraceHandler = NULL;
123 iOldBTraceControl = NULL;
125 DChunk* chunk = iBufferChunk;
128 NKern::RestoreInterrupts(irq);
131 Kern::ChunkClose(chunk);
138 Helper functions for encoding pseudo- floating point values recoverable by:
140 int exponent = (signed char)(encoded_val >> 24);
141 int mantissa = encoded_val & 0xFFFFFF;
142 double val = mantissa * pow(2, exponent);
144 TUint EncodeFloatesque(TUint64 val64, TInt exponent)
146 // Lose precision until it fits in 24 bits
148 while (val64>=0x1000000)
150 round_up = (TInt)(val64&1);
157 if (val64>=0x1000000)
164 // Return 8-bit exponent and 24-bit mantissa
165 return (TUint)(val64 | (((unsigned char)exponent)<<24));
168 TUint EncodeReciprocal(TUint val)
170 if (val==0) return val;
172 // Get reciprocal * 2^64
178 return EncodeFloatesque(val64, -64);
181 TUint EncodePostDiv(TUint val, TUint divisor)
185 val64 = val64 / divisor;
186 return EncodeFloatesque(val64, -32);
189 void BTracePrimeMetatrace()
192 TUint period1 = EncodeReciprocal(NKern::TimestampFrequency());
193 TUint period2 = period1 + (32u<<24); // timestamp2 period is 2^32 * timestamp1 period
194 BTrace12(BTrace::EMetaTrace, BTrace::EMetaTraceTimestampsInfo, period1, period2, 1);
196 TUint period1 = EncodeReciprocal(NKern::FastCounterFrequency());
197 TUint period2 = EncodePostDiv(NKern::TickPeriod(), 1000000);
198 BTrace12(BTrace::EMetaTrace, BTrace::EMetaTraceTimestampsInfo, period1, period2, 0);
202 void TBTraceBufferK::Reset(TUint aMode)
205 TSpinLock* sl = BTrace::LockPtr();
207 TInt irq = __SPIN_LOCK_IRQSAVE(*sl); // guarantees handler can't run at the same time
209 TBTraceBuffer* userBuffer = (TBTraceBuffer*)iAddress;
210 userBuffer->iStart = iStart;
211 userBuffer->iEnd = iEnd;
212 userBuffer->iHead = iHead;
213 userBuffer->iTail = iHead;
214 userBuffer->iGeneration = 0;
215 userBuffer->iMode = aMode;
216 __SPIN_UNLOCK_IRQRESTORE(*sl,irq);
219 if (BTrace::CheckFilter(BTrace::EMetaTrace))
220 BTracePrimeMetatrace();
226 TInt TBTraceBufferK::RequestData(TInt aSize, TDfc* aDfc)
231 TSpinLock* sl = BTrace::LockPtr();
233 TInt irq = __SPIN_LOCK_IRQSAVE(*sl); // guarantees handler can't run
234 TBTraceBuffer* userBuffer = (TBTraceBuffer*)iAddress;
237 __SPIN_UNLOCK_IRQRESTORE(*sl,irq);
240 TInt dif = userBuffer->iTail-iHead;
242 aSize = 0; // we need no more bytes because all bytes to end of buffer are available
244 aSize += dif; // number of bytes extra we need
247 iRequestDataSize = aSize;
250 __SPIN_UNLOCK_IRQRESTORE(*sl,irq);
252 return KErrCompletion;
257 #ifndef BTRACE_DRIVER_MACHINE_CODED
259 TBool TBTraceBufferK::Trace_Impl(TUint32 aHeader,TUint32 aHeader2,const TUint32 aContext,const TUint32 a1,const TUint32 a2,const TUint32 a3,const TUint32 aExtra, const TUint32 aPc, TBool aIncTimestamp2)
262 TInt irq = NKern::DisableAllInterrupts();
267 // Header 2 always present and contains CPU number
268 // If Header2 not originally there, add 4 to size
269 if (!(aHeader&(BTrace::EHeader2Present<<BTrace::EFlagsIndex*8)))
270 aHeader += (4<<BTrace::ESizeIndex*8) + (BTrace::EHeader2Present<<BTrace::EFlagsIndex*8), aHeader2=0;
271 aHeader2 = (aHeader2 &~ BTrace::ECpuIdMask) | (NKern::CurrentCpu()<<20);
273 #ifdef BTRACE_INCLUDE_TIMESTAMPS
274 // Add timestamp to trace...
276 aHeader += 8<<BTrace::ESizeIndex*8;
277 aHeader |= BTrace::ETimestampPresent<<BTrace::EFlagsIndex*8 | BTrace::ETimestamp2Present<<BTrace::EFlagsIndex*8;
278 TUint64 timeStamp = NKern::Timestamp();
279 #elif defined(__EPOC32__) && defined(__CPU_X86)
280 aHeader += 8<<BTrace::ESizeIndex*8;
281 aHeader |= BTrace::ETimestampPresent<<BTrace::EFlagsIndex*8 | BTrace::ETimestamp2Present<<BTrace::EFlagsIndex*8;
282 TUint64 timeStamp = X86::Timestamp();
284 TUint32 timeStamp = NKern::FastCounter();
285 TUint32 timeStamp2 = 0;
288 timeStamp2 = NKern::TickCount();
289 aHeader += 8<<BTrace::ESizeIndex*8;
290 aHeader |= (BTrace::ETimestampPresent | BTrace::ETimestamp2Present) << BTrace::EFlagsIndex*8;
294 aHeader += 4<<BTrace::ESizeIndex*8;
295 aHeader |= BTrace::ETimestampPresent<<BTrace::EFlagsIndex*8;
299 TUint size = (aHeader+3)&0xfc;
301 TBTraceBufferK& buffer = Buffer;
302 TLinAddr address = buffer.iAddress;
303 TBTraceBuffer& user_buffer = *(TBTraceBuffer*)address;
304 ++user_buffer.iGeneration; // atomic not required since only driver modifies iGeneration
306 __e32_memory_barrier();
308 TUint start = buffer.iStart;
309 TUint end = buffer.iEnd;
310 TUint orig_head = buffer.iHead;
311 TInt requestDataSize = buffer.iRequestDataSize;
312 TUint8* recordOffsets = buffer.iRecordOffsets;
313 TUint32 orig_tail = user_buffer.iTail;
314 TUint32 newHead, head, tail;
316 if(!(user_buffer.iMode&RBTrace::EEnable))
321 tail = orig_tail &~ 1;
326 newHead = start+size;
327 if(head<tail || tail<newHead+1)
329 if(!(user_buffer.iMode&RBTrace::EFreeRunning))
331 user_buffer.iWrap = head;
333 tail = newHead+(recordOffsets[newHead>>2]<<2);
336 user_buffer.iWrap = head;
339 else if(head<tail && tail<=newHead)
343 TUint wrap = user_buffer.iWrap;
344 if(!(user_buffer.iMode&RBTrace::EFreeRunning))
346 if(newHead<end && newHead<wrap)
348 tail = newHead+(recordOffsets[newHead>>2]<<2);
349 if(tail>=end || tail>=wrap)
356 *(TUint32*)(address+tail) |= BTrace::EMissingRecord<<(BTrace::EFlagsIndex*8);
357 if (!__e32_atomic_cas_ord32(&user_buffer.iTail, &orig_tail, tail|1))
358 goto retry; // go round again if user side has already updated the tail pointer
361 buffer.iRequestDataSize = requestDataSize-size;
364 recordOffsets += head>>2;
366 TUint32* dst = (TUint32*)((TUint)address+head);
367 size >>= 2; // we are now counting words, not bytes
369 // store first word of trace...
374 w |= BTrace::EMissingRecord<<(BTrace::EFlagsIndex*8);
376 *recordOffsets++ = (TUint8)size;
381 if(aHeader&(BTrace::EHeader2Present<<(BTrace::EFlagsIndex*8)))
385 *recordOffsets++ = (TUint8)size;
390 #ifdef BTRACE_INCLUDE_TIMESTAMPS
391 // store timestamp...
392 #if defined(__SMP__) || (defined(__EPOC32__) && defined(__CPU_X86))
393 *recordOffsets++ = (TUint8)size;
395 *dst++ = TUint32(timeStamp);
396 *recordOffsets++ = (TUint8)size;
398 *dst++ = TUint32(timeStamp>>32);
400 *recordOffsets++ = (TUint8)size;
405 *recordOffsets++ = (TUint8)size;
412 if(aHeader&(BTrace::EContextIdPresent<<(BTrace::EFlagsIndex*8)))
415 *recordOffsets++ = (TUint8)size;
420 if(aHeader&(BTrace::EPcPresent<<(BTrace::EFlagsIndex*8)))
423 *recordOffsets++ = (TUint8)size;
428 if(aHeader&(BTrace::EExtraPresent<<(BTrace::EFlagsIndex*8)))
431 *recordOffsets++ = (TUint8)size;
436 // store remainding words of trace...
440 *recordOffsets++ = (TUint8)size;
446 *recordOffsets++ = (TUint8)size;
454 *recordOffsets++ = (TUint8)size;
463 *recordOffsets++ = (TUint8)size;
473 buffer.iHead = newHead;
475 __e32_memory_barrier(); // make sure written data is observed before head pointer update
477 user_buffer.iHead = newHead;
480 TDfc* dfc = (TDfc*)buffer.iWaitingDfc;
481 if(dfc && buffer.iRequestDataSize<=0)
483 buffer.iWaitingDfc = NULL;
489 __e32_memory_barrier();
491 ++user_buffer.iGeneration; // atomic not required since only driver modifies iGeneration
493 NKern::RestoreInterrupts(irq);
499 buffer.iRequestDataSize = 0;
500 buffer.iDropped = ETrue;
502 __e32_memory_barrier();
504 ++user_buffer.iGeneration; // atomic not required since only driver modifies iGeneration
506 NKern::RestoreInterrupts(irq);
512 __e32_memory_barrier();
514 ++user_buffer.iGeneration; // atomic not required since only driver modifies iGeneration
516 NKern::RestoreInterrupts(irq);
521 TBool TBTraceBufferK::TraceWithTimestamp2(TUint32 aHeader,TUint32 aHeader2,const TUint32 aContext,const TUint32 a1,const TUint32 a2,const TUint32 a3,const TUint32 aExtra,const TUint32 aPc)
523 return Trace_Impl(aHeader, aHeader2, aContext, a1, a2, a3, aExtra, aPc, ETrue);
526 TBool TBTraceBufferK::Trace(TUint32 aHeader,TUint32 aHeader2,const TUint32 aContext,const TUint32 a1,const TUint32 a2,const TUint32 a3,const TUint32 aExtra,const TUint32 aPc)
528 return Trace_Impl(aHeader, aHeader2, aContext, a1, a2, a3, aExtra, aPc, EFalse);
531 #endif // BTRACE_DRIVER_MACHINE_CODED
534 TInt TBTraceBufferK::ControlFunction(BTrace::TControl aFunction, TAny* aArg1, TAny* aArg2)
538 case BTrace::ECtrlSystemCrashed:
540 ((TBTraceBuffer*)Buffer.iAddress)->iMode = 0; // turn off trace
543 case BTrace::ECtrlCrashReadFirst:
544 Buffer.iCrashReadPart = 0;
546 case BTrace::ECtrlCrashReadNext:
547 Buffer.CrashRead(*(TUint8**)aArg1,*(TUint*)aArg2);
548 ++Buffer.iCrashReadPart;
552 return KErrNotSupported;
557 void TBTraceBufferK::CrashRead(TUint8*& aData, TUint& aSize)
559 // start by assuming no data...
563 TBTraceBuffer* userBuffer = (TBTraceBuffer*)iAddress;
565 return; // no trace buffer, so end...
568 TUint tail = userBuffer->iTail;
569 TUint8* data = (TUint8*)userBuffer;
573 // data is in one part...
574 if(iCrashReadPart==0)
579 // else no more parts
583 // data is in two parts...
584 if(iCrashReadPart==0)
588 aSize = userBuffer->iWrap-tail;
590 else if(iCrashReadPart==1)
596 // else no more parts
605 class DBTraceFactory : public DLogicalDevice
608 virtual TInt Install();
609 virtual void GetCaps(TDes8& aDes) const;
610 virtual TInt Create(DLogicalChannelBase*& aChannel);
614 class DBTraceChannel : public DLogicalChannelBase
618 virtual ~DBTraceChannel();
619 // Inherited from DObject
620 virtual TInt RequestUserHandle(DThread* aThread, TOwnerType aType);
621 // Inherited from DLogicalChannelBase
622 virtual TInt DoCreate(TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
623 virtual TInt Request(TInt aReqNo, TAny* a1, TAny* a2);
625 static void WaitCallback(TAny* aSelf);
628 TClientRequest* iWaitRequest;
633 TUint32* iFilter2Set;
634 TBool iTimestamp2Enabled;
642 TInt DBTraceFactory::Install()
644 return SetName(&RBTrace::Name());
647 void DBTraceFactory::GetCaps(TDes8& aDes) const
649 Kern::InfoCopy(aDes,0,0);
652 TInt DBTraceFactory::Create(DLogicalChannelBase*& aChannel)
654 aChannel=new DBTraceChannel();
660 void syncDfcFn(TAny* aPtr)
662 NKern::FSSignal((NFastSemaphore*)aPtr);
665 void Sync(TDfcQue* aDfcQ)
668 TDfc dfc(&syncDfcFn, &s, aDfcQ, 0);
677 DBTraceChannel::DBTraceChannel()
678 : iWaitDfc(WaitCallback,this,Kern::DfcQue1(),7)
682 DBTraceChannel::~DBTraceChannel()
685 Buffer.iWaitingDfc = NULL;
687 Sync(Kern::DfcQue1());
690 Kern::QueueRequestComplete(iClient, iWaitRequest, KErrCancel); // does nothing if request not pending
691 Kern::DestroyClientRequest(iWaitRequest);
694 __e32_atomic_swp_ord32(&ChannelOpen, 0);
697 TInt DBTraceChannel::DoCreate(TInt /*aUnit*/, const TDesC8* /*aInfo*/, const TVersion& /*aVer*/)
699 // _LIT_SECURITY_POLICY_C2(KSecurityPolicy,ECapabilityReadDeviceData,ECapabilityWriteDeviceData);
700 // if(!KSecurityPolicy().CheckPolicy(&Kern::CurrentThread(),__PLATSEC_DIAGNOSTIC_STRING("Checked by BTRACE")))
701 // return KErrPermissionDenied;
702 iClient = &Kern::CurrentThread();
703 TInt r = Kern::CreateClientRequest(iWaitRequest);
706 if (__e32_atomic_swp_ord32(&ChannelOpen, 1))
713 TInt DBTraceChannel::RequestUserHandle(DThread* aThread, TOwnerType aType)
715 if (aType!=EOwnerThread || aThread!=iClient)
716 return KErrAccessDenied;
720 void DBTraceChannel::WaitCallback(TAny* aSelf)
722 DBTraceChannel& c = *(DBTraceChannel*)aSelf;
723 Kern::QueueRequestComplete(c.iClient, c.iWaitRequest, KErrNone);
726 TInt DBTraceChannel::Request(TInt aReqNo, TAny* a1, TAny* a2)
729 TBTraceBufferK& buffer = Buffer;
733 case RBTrace::EOpenBuffer:
734 NKern::ThreadEnterCS();
735 if(!Buffer.iBufferChunk)
736 r = buffer.Create(0x100000);
740 r = Kern::MakeHandleAndOpen(NULL, buffer.iBufferChunk);
741 NKern::ThreadLeaveCS();
744 case RBTrace::EResizeBuffer:
745 NKern::ThreadEnterCS();
747 r = buffer.Create((TInt)a1);
748 NKern::ThreadLeaveCS();
751 case RBTrace::ESetFilter:
753 TInt old = BTrace::SetFilter((BTrace::TCategory)(TInt)a1,(TInt)a2);
754 if((TInt)a2==1 && old==0) // filter turned on?
756 if ((TInt)a1==BTrace::EMetaTrace)
757 BTracePrimeMetatrace();
758 BTrace::Prime((TInt)a1); // prime this trace category
763 case RBTrace::ESetFilter2:
764 return BTrace::SetFilter2((TUint32)a1,(TBool)a2);
766 case RBTrace::ESetFilter2Array:
768 NKern::ThreadEnterCS();
770 TInt size = (TInt)a2*sizeof(TUint32);
771 TUint32* buffer = (TUint32*)Kern::Alloc(size);
772 iFilter2Set = buffer;
773 NKern::ThreadLeaveCS();
776 kumemget32(buffer,a1,size);
777 r = BTrace::SetFilter2(buffer,(TInt)a2);
778 NKern::ThreadEnterCS();
781 NKern::ThreadLeaveCS();
785 case RBTrace::ESetFilter2Global:
786 BTrace::SetFilter2((TBool)a1);
789 case RBTrace::EGetFilter2Part1:
791 NKern::ThreadEnterCS();
795 TInt globalFilter = 0;
796 iFilter2Count = BTrace::Filter2(iFilter2,globalFilter);
797 NKern::ThreadLeaveCS();
798 kumemput32(a2,&globalFilter,sizeof(TBool));
799 return iFilter2Count;
802 case RBTrace::EGetFilter2Part2:
803 if((TInt)a2!=iFilter2Count)
806 kumemput32(a1,iFilter2,iFilter2Count*sizeof(TUint32));
807 NKern::ThreadEnterCS();
811 NKern::ThreadLeaveCS();
814 case RBTrace::ERequestData:
815 if (iWaitRequest->SetStatus((TRequestStatus*)a1) != KErrNone)
816 Kern::PanicCurrentThread(RBTrace::Name(),RBTrace::ERequestAlreadyPending);
817 r = buffer.RequestData((TInt)a2,&iWaitDfc);
820 iWaitRequest->Reset();
821 TRequestStatus* s = (TRequestStatus*)a1;
822 if (r==KErrCompletion)
824 Kern::RequestComplete(s, r);
828 case RBTrace::ECancelRequestData:
829 buffer.iWaitingDfc = NULL;
831 Kern::QueueRequestComplete(iClient, iWaitRequest, KErrCancel);
834 case RBTrace::ESetSerialPortOutput:
836 TUint mode = Kern::ESerialOutNever+(TUint)a1;
837 mode = Kern::SetTextTraceMode(mode,Kern::ESerialOutMask);
838 mode &= Kern::ESerialOutMask;
839 return mode-Kern::ESerialOutNever;
842 case RBTrace::ESetTimestamp2Enabled:
844 TBool old = iTimestamp2Enabled;
845 iTimestamp2Enabled = (TBool)a1;
846 BTrace::TControlFunction oldControl;
847 BTrace::THandler oldHandler;
848 BTrace::THandler handler = iTimestamp2Enabled ? TBTraceBufferK::TraceWithTimestamp2 : TBTraceBufferK::Trace;
849 BTrace::SetHandlers(handler,TBTraceBufferK::ControlFunction,oldHandler,oldControl);
856 return KErrNotSupported;
860 DECLARE_EXTENSION_LDD()
862 return new DBTraceFactory;
866 DECLARE_STANDARD_EXTENSION()
868 DECLARE_EXTENSION_WITH_PRIORITY(KExtensionMaximumPriority)
871 TSuperPage& superPage = Kern::SuperPage();
872 TInt bufferSize = superPage.iInitialBTraceBuffer;
874 bufferSize = 0x10000;
875 TInt r=Buffer.Create(bufferSize);
877 Buffer.Reset(superPage.iInitialBTraceMode);