os/kernelhwsrv/kerneltest/e32test/debug/d_eventtracker.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// e32test\debug\d_eventtracker.cpp
sl@0
    15
// LDD-based debug agent used to track kernel events.  See
sl@0
    16
// t_eventtracker.cpp
sl@0
    17
// 
sl@0
    18
//
sl@0
    19
sl@0
    20
#include <kernel/kern_priv.h>
sl@0
    21
#include "reventtracker.h"
sl@0
    22
#include "d_eventtracker.h"
sl@0
    23
#include "nk_trace.h"
sl@0
    24
sl@0
    25
#ifdef __MARM__
sl@0
    26
#include <kernel/kdebug.h>
sl@0
    27
#endif //__MARM__
sl@0
    28
sl@0
    29
#ifdef _DEBUG
sl@0
    30
static const char KPanicCat[] = "D_EVENTTRACKER";
sl@0
    31
#endif // _DEBUG
sl@0
    32
_LIT(KClientPanicCat, "D_EVENTTRACKER");
sl@0
    33
sl@0
    34
DEventTracker* TheEventTracker;
sl@0
    35
sl@0
    36
//////////////////////////////////////////////////////////////////////////////
sl@0
    37
sl@0
    38
/** Data about objects being tracked.
sl@0
    39
	All tracked objects are kept in a tracking list.  The object address 
sl@0
    40
	is a key and so must be unique.
sl@0
    41
 */
sl@0
    42
sl@0
    43
TTrackedItem::TTrackedItem(const DBase* aObject)
sl@0
    44
	: iObject(aObject), iAccountedFor(EFalse)
sl@0
    45
	{
sl@0
    46
	}
sl@0
    47
	
sl@0
    48
sl@0
    49
/** Subclass for DObjects being tracked */
sl@0
    50
sl@0
    51
TTrackedObject::TTrackedObject(DObject* aObject, TObjectType aType)
sl@0
    52
	:	TTrackedItem(aObject),
sl@0
    53
		iType(aType)
sl@0
    54
	{
sl@0
    55
	aObject->FullName(iFullName);
sl@0
    56
	}
sl@0
    57
sl@0
    58
TBool TTrackedObject::CheckIntegrity(const TDesC& aName, TObjectType aType) const
sl@0
    59
	{
sl@0
    60
	TBool ok = EFalse;
sl@0
    61
	
sl@0
    62
	if (aType == iType)
sl@0
    63
		{
sl@0
    64
		if (aType == EThread || aType == EProcess)
sl@0
    65
			{
sl@0
    66
			ok = (iFullName == aName);
sl@0
    67
			}
sl@0
    68
		else
sl@0
    69
			{
sl@0
    70
			ok = ETrue;
sl@0
    71
			}
sl@0
    72
		}
sl@0
    73
sl@0
    74
	if (!ok)
sl@0
    75
		{
sl@0
    76
		Kern::Printf("EVENTTRACKER: container / tracking list mismatch (0x%08x)", iObject);
sl@0
    77
		Kern::Printf("EVENTTRACKER: \tcontainer: %S (type %d)", &aName, aType);
sl@0
    78
		Kern::Printf("EVENTTRACKER: \ttracking list: %S (type %d)", &iFullName, iType);
sl@0
    79
		}
sl@0
    80
		
sl@0
    81
	return ok;
sl@0
    82
	}
sl@0
    83
sl@0
    84
/** Subclass for DCodeSegs being tracked */
sl@0
    85
sl@0
    86
TTrackedCodeSeg::TTrackedCodeSeg(const DCodeSeg* aCodeSeg)
sl@0
    87
	:	TTrackedItem(aCodeSeg),
sl@0
    88
		iAccessCount(aCodeSeg ? aCodeSeg->iAccessCount : 0)
sl@0
    89
	{
sl@0
    90
	}
sl@0
    91
sl@0
    92
TBool TTrackedCodeSeg::CheckIntegrity(TInt aAccessCount) const
sl@0
    93
	{
sl@0
    94
	const TBool ok = (aAccessCount == iAccessCount);
sl@0
    95
sl@0
    96
	if (!ok)
sl@0
    97
		{
sl@0
    98
		Kern::Printf("EVENTTRACKER: code seg list / tracking list mismatch (0x%08x)", iObject);
sl@0
    99
		Kern::Printf("EVENTTRACKER: \tcode seg list: %d", aAccessCount);
sl@0
   100
		Kern::Printf("EVENTTRACKER: \ttracking list: %d", iAccessCount);
sl@0
   101
		}
sl@0
   102
		
sl@0
   103
	return ok;
sl@0
   104
	}
sl@0
   105
sl@0
   106
sl@0
   107
/** Event handler and container for all objects being tracked.  */
sl@0
   108
sl@0
   109
DEventTracker::DEventTracker()
sl@0
   110
	:	DKernelEventHandler(EventHandler, this)
sl@0
   111
	{
sl@0
   112
	__ASSERT_DEBUG(!TheEventTracker, Kern::Fault(KPanicCat, __LINE__));
sl@0
   113
	
sl@0
   114
	TheEventTracker = this;
sl@0
   115
	}
sl@0
   116
sl@0
   117
sl@0
   118
//
sl@0
   119
// If aUseHook is true, the event tracker hooks the stop-mode debugger 
sl@0
   120
// breakpoint in preference to adding itself to the kernel event handler
sl@0
   121
// queue.  In order to clean up on its destruction, it has to
sl@0
   122
// reset the breakpoint by installing a dummy nop breakpoint
sl@0
   123
// handler, which is cut-and-pasted from kdebug.dll in order to
sl@0
   124
// avoid a dependency on kdebug.dll.  In order to use the event
sl@0
   125
// tracker using the stop-mode debugger breakpoint rather than
sl@0
   126
// the kernel event handler queue, kdebug.dll must be present in
sl@0
   127
// the ROM
sl@0
   128
//
sl@0
   129
TInt DEventTracker::Create(DLogicalDevice* aDevice, TBool aUseHook)
sl@0
   130
	{
sl@0
   131
	TInt err = aDevice->Open();
sl@0
   132
sl@0
   133
	if (err)
sl@0
   134
		{
sl@0
   135
		return err;
sl@0
   136
		}
sl@0
   137
	
sl@0
   138
	iDevice = aDevice;
sl@0
   139
sl@0
   140
	err = Kern::MutexCreate(iLock, _L("EventHandlerLock"), KMutexOrdNone);
sl@0
   141
sl@0
   142
	if (!err)
sl@0
   143
		{
sl@0
   144
		if (aUseHook)
sl@0
   145
			{
sl@0
   146
			// Find debugger info, if any
sl@0
   147
			DDebuggerInfo* const debugInfo = Kern::SuperPage().iDebuggerInfo;
sl@0
   148
sl@0
   149
			// Test stop-mode breakpoint if available
sl@0
   150
			if (debugInfo)
sl@0
   151
				{
sl@0
   152
#ifdef __MARM__
sl@0
   153
				// Receive all events
sl@0
   154
				for (TInt i = 0; i < ((EEventLimit + 31) >> 5); ++i)
sl@0
   155
					{
sl@0
   156
					debugInfo->iEventMask[i] = 0xffffffffu;
sl@0
   157
					}
sl@0
   158
				
sl@0
   159
				__KTRACE_OPT(KDEBUGGER, Kern::Printf("EVENTTRACKER: Copying breakpoint (0x%x) into handler (0x%x), size %d", &BranchToEventHandler, debugInfo->iEventHandlerBreakpoint, BreakPointSize()));
sl@0
   160
sl@0
   161
				// Set up breakpoint to call handler
sl@0
   162
				memcpy((TAny*)debugInfo->iEventHandlerBreakpoint, (TAny*) &BranchToEventHandler, BreakPointSize());
sl@0
   163
#else // !__MARM__
sl@0
   164
				err = KErrNotFound;
sl@0
   165
#endif // __MARM__
sl@0
   166
				}
sl@0
   167
			else
sl@0
   168
				{
sl@0
   169
				err = KErrNotFound;
sl@0
   170
				}
sl@0
   171
			}
sl@0
   172
		else
sl@0
   173
			{
sl@0
   174
			err = Add();
sl@0
   175
			}
sl@0
   176
		}
sl@0
   177
	
sl@0
   178
	return err;
sl@0
   179
	}
sl@0
   180
sl@0
   181
sl@0
   182
DEventTracker::~DEventTracker()
sl@0
   183
	{
sl@0
   184
#ifdef __MARM__
sl@0
   185
	// Remove breakpoint, if any
sl@0
   186
	DDebuggerInfo* const debugInfo = Kern::SuperPage().iDebuggerInfo;
sl@0
   187
	if (debugInfo)
sl@0
   188
		{
sl@0
   189
		CopyDummyHandler(debugInfo->iEventHandlerBreakpoint);
sl@0
   190
		}
sl@0
   191
#endif //__MARM__
sl@0
   192
sl@0
   193
	// clean-up tracking list
sl@0
   194
	SDblQueLink* link = iItems.GetFirst();
sl@0
   195
	while (link)
sl@0
   196
		{
sl@0
   197
		delete _LOFF(link, TTrackedItem, iLink);
sl@0
   198
		link = iItems.GetFirst();
sl@0
   199
		}
sl@0
   200
sl@0
   201
	if (iLock)
sl@0
   202
		{
sl@0
   203
		iLock->Close(NULL);
sl@0
   204
		}
sl@0
   205
sl@0
   206
	if (iDevice)
sl@0
   207
		{
sl@0
   208
		iDevice->Close(NULL);
sl@0
   209
		}
sl@0
   210
		
sl@0
   211
	TheEventTracker = NULL;
sl@0
   212
	}
sl@0
   213
sl@0
   214
sl@0
   215
TInt DEventTracker::Start()
sl@0
   216
	{
sl@0
   217
	TInt err = AddExistingObjects();
sl@0
   218
	
sl@0
   219
	if (!err)
sl@0
   220
		{
sl@0
   221
		iTracking = ETrue;
sl@0
   222
		}
sl@0
   223
	
sl@0
   224
	return err;
sl@0
   225
	}
sl@0
   226
sl@0
   227
sl@0
   228
TInt DEventTracker::Stop()
sl@0
   229
	{
sl@0
   230
	NKern::ThreadEnterCS();
sl@0
   231
	Kern::MutexWait(*iLock);
sl@0
   232
sl@0
   233
	iTracking = EFalse;
sl@0
   234
sl@0
   235
	Kern::MutexSignal(*iLock);
sl@0
   236
	NKern::ThreadLeaveCS();
sl@0
   237
sl@0
   238
	DumpCounters();
sl@0
   239
sl@0
   240
	return CheckIntegrity();
sl@0
   241
	}
sl@0
   242
sl@0
   243
sl@0
   244
TUint DEventTracker::EventHandler(TKernelEvent aType, TAny* a1, TAny* a2, TAny* aThis)
sl@0
   245
	{
sl@0
   246
	return ((DEventTracker*)aThis)->HandleEvent(aType, a1, a2);
sl@0
   247
	}
sl@0
   248
sl@0
   249
sl@0
   250
TUint DEventTracker::HandleEvent(TKernelEvent aType, TAny* a1, TAny* a2)
sl@0
   251
	{ 
sl@0
   252
	__KTRACE_OPT(KDEBUGGER, Kern::Printf("EVENTTRACKER: Handling event type 0x%x", aType));
sl@0
   253
sl@0
   254
	Kern::MutexWait(*iLock);
sl@0
   255
sl@0
   256
	if (iTracking)
sl@0
   257
		{
sl@0
   258
		++iCounters[aType];
sl@0
   259
sl@0
   260
		switch (aType)
sl@0
   261
			{
sl@0
   262
		case EEventAddProcess:
sl@0
   263
			AddObject(EProcess, (DObject*)a1);
sl@0
   264
			break;
sl@0
   265
		case EEventUpdateProcess:
sl@0
   266
			// could be renaming or chunk addition/deletion
sl@0
   267
			UpdateObject(EProcess, (DObject*)a1, EFalse);
sl@0
   268
			break;
sl@0
   269
		case EEventRemoveProcess:
sl@0
   270
			RemoveObject(EProcess, (DObject*)a1);
sl@0
   271
			break;
sl@0
   272
		case EEventAddThread:
sl@0
   273
			AddObject(EThread, (DObject*)a1);
sl@0
   274
			break;
sl@0
   275
		case EEventUpdateThread:
sl@0
   276
			UpdateObject(EThread, (DObject*)a1, ETrue);
sl@0
   277
			break;
sl@0
   278
		case EEventRemoveThread:
sl@0
   279
			RemoveObject(EThread, (DObject*)a1);
sl@0
   280
			break;
sl@0
   281
		case EEventAddLibrary:
sl@0
   282
			{
sl@0
   283
			DLibrary* pL = (DLibrary*)a1;
sl@0
   284
			if (pL->iMapCount == 1)
sl@0
   285
				AddObject(ELibrary, pL);
sl@0
   286
			}
sl@0
   287
			break;
sl@0
   288
		case EEventRemoveLibrary:
sl@0
   289
			{
sl@0
   290
			DLibrary* pL = (DLibrary*)a1;
sl@0
   291
			if (pL->iMapCount == 0)
sl@0
   292
				RemoveObject(ELibrary, pL);
sl@0
   293
			}
sl@0
   294
			break;
sl@0
   295
		case EEventNewChunk:
sl@0
   296
			AddObject(EChunk, (DObject*)a1);
sl@0
   297
			break;
sl@0
   298
		case EEventDeleteChunk:
sl@0
   299
			RemoveObject(EChunk, (DObject*)a1);
sl@0
   300
			break;
sl@0
   301
		case EEventAddCodeSeg:
sl@0
   302
			{
sl@0
   303
			AddCodeSeg((DCodeSeg*)a1, (DProcess*)a2);
sl@0
   304
			}
sl@0
   305
			break;
sl@0
   306
		case EEventRemoveCodeSeg:
sl@0
   307
			{
sl@0
   308
			RemoveCodeSeg((DCodeSeg*)a1, (DProcess*)a2);
sl@0
   309
			}
sl@0
   310
			break;
sl@0
   311
		case EEventLoadedProcess:
sl@0
   312
			{
sl@0
   313
			__KTRACE_OPT(KDEBUGGER, Kern::Printf("EVENTTRACKER: Process %O loaded", a1));
sl@0
   314
			ProcessLoaded((DProcess*)a1);
sl@0
   315
			}
sl@0
   316
			break;
sl@0
   317
		case EEventUnloadingProcess:
sl@0
   318
			__KTRACE_OPT(KDEBUGGER, Kern::Printf("EVENTTRACKER: Process %O unloaded", a1));
sl@0
   319
			break;
sl@0
   320
		default:
sl@0
   321
			// no-op
sl@0
   322
			__KTRACE_OPT(KDEBUGGER, Kern::Printf("EVENTTRACKER: Handling default case"));
sl@0
   323
			break;
sl@0
   324
			}
sl@0
   325
		}
sl@0
   326
sl@0
   327
	Kern::MutexSignal(*iLock);
sl@0
   328
sl@0
   329
	// Allow other handlers to see this event
sl@0
   330
	return DKernelEventHandler::ERunNext;
sl@0
   331
	}
sl@0
   332
sl@0
   333
sl@0
   334
void DEventTracker::AddObject(TObjectType aType, DObject* aObject)
sl@0
   335
	{
sl@0
   336
	TTrackedObject* trackedObject = (TTrackedObject*)LookupItem(aObject);
sl@0
   337
sl@0
   338
	if (trackedObject)
sl@0
   339
		{
sl@0
   340
		Kern::Printf("EVENTTRACKER: Found orphaned object %O in tracking list while adding new object", aObject);
sl@0
   341
		++iErrorCount;
sl@0
   342
		return;
sl@0
   343
		}
sl@0
   344
sl@0
   345
	NKern::ThreadEnterCS();
sl@0
   346
	trackedObject = new TTrackedObject(aObject, aType);
sl@0
   347
	NKern::ThreadLeaveCS();
sl@0
   348
sl@0
   349
	if (trackedObject)
sl@0
   350
		{
sl@0
   351
		__KTRACE_OPT(KDEBUGGER, Kern::Printf("EVENTTRACKER: Adding %O (type %d) to tracking list", aObject, aType));
sl@0
   352
		__KTRACE_OPT(KDEBUGGER, Kern::Printf("EVENTTRACKER: DBase ptr == 0x%x", trackedObject->iObject));
sl@0
   353
		iItems.Add(&trackedObject->iLink);
sl@0
   354
		}
sl@0
   355
	else
sl@0
   356
		{
sl@0
   357
		iOOM = ETrue;
sl@0
   358
		++iErrorCount;
sl@0
   359
		}
sl@0
   360
	}
sl@0
   361
sl@0
   362
sl@0
   363
void DEventTracker::RemoveObject(TObjectType aType, DObject* aObject)
sl@0
   364
	{
sl@0
   365
	TTrackedObject* const trackedObject = (TTrackedObject*)LookupItem(aObject);
sl@0
   366
sl@0
   367
	if (trackedObject)
sl@0
   368
		{
sl@0
   369
 		TFullName name;
sl@0
   370
		aObject->FullName(name);
sl@0
   371
		if (!trackedObject->CheckIntegrity(name, aType))
sl@0
   372
			{
sl@0
   373
			++iErrorCount;
sl@0
   374
			}
sl@0
   375
		__KTRACE_OPT(KDEBUGGER, Kern::Printf("EVENTTRACKER: Removing %S (type %d) from tracking list", &name, aType));
sl@0
   376
		trackedObject->iLink.Deque();
sl@0
   377
sl@0
   378
		NKern::ThreadEnterCS();
sl@0
   379
		delete trackedObject;
sl@0
   380
		NKern::ThreadLeaveCS();
sl@0
   381
		}
sl@0
   382
	else
sl@0
   383
		{
sl@0
   384
		Kern::Printf("EVENTTRACKER: %O (type %d) removed but not in tracking list", aObject, aType);
sl@0
   385
		++iErrorCount;
sl@0
   386
		}
sl@0
   387
	}
sl@0
   388
sl@0
   389
sl@0
   390
void DEventTracker::UpdateObject(TObjectType aType, DObject* aObject, TBool aMustBeRenamed)
sl@0
   391
	{
sl@0
   392
	TTrackedObject* const trackedObject = (TTrackedObject*)LookupItem(aObject);
sl@0
   393
sl@0
   394
	if (trackedObject)
sl@0
   395
		{
sl@0
   396
		TFullName newName;
sl@0
   397
		aObject->FullName(newName);
sl@0
   398
		if (newName != trackedObject->iFullName)
sl@0
   399
			{
sl@0
   400
			__KTRACE_OPT(KDEBUGGER, Kern::Printf("EVENTTRACKER: Renaming %S --> %S (type %d)", 
sl@0
   401
						&trackedObject->iFullName, &newName));
sl@0
   402
			trackedObject->iFullName = newName;
sl@0
   403
			}
sl@0
   404
		else if (aMustBeRenamed)
sl@0
   405
			{
sl@0
   406
			Kern::Printf("EVENTTRACKER: %O (type %d) renamed with same name", aObject, aType);
sl@0
   407
			++iErrorCount;
sl@0
   408
			}
sl@0
   409
		}
sl@0
   410
	else
sl@0
   411
		{
sl@0
   412
		Kern::Printf("EVENTTRACKER: %O (type %d) updated but not in tracking list", aObject, aType);
sl@0
   413
		Kern::Printf("EVENTTRACKER: DBase ptr == 0x%x", (DBase*)aObject);
sl@0
   414
		++iErrorCount;
sl@0
   415
		}
sl@0
   416
	}
sl@0
   417
sl@0
   418
void DEventTracker::AddCodeSeg(DCodeSeg* aCodeSeg, DProcess* aProcess)
sl@0
   419
	{
sl@0
   420
	TTrackedCodeSeg* trackedCodeSeg = (TTrackedCodeSeg*)LookupItem(aCodeSeg);
sl@0
   421
sl@0
   422
	if (trackedCodeSeg)
sl@0
   423
		{
sl@0
   424
		if (aProcess && (aProcess->iTempCodeSeg == aCodeSeg))
sl@0
   425
			{
sl@0
   426
			// This is the exe code seg for a loading process
sl@0
   427
			// and hence the access count is currently
sl@0
   428
			// incremented by one
sl@0
   429
			++trackedCodeSeg->iAccessCount;
sl@0
   430
			}
sl@0
   431
sl@0
   432
		if (trackedCodeSeg->iAccessCount != aCodeSeg->iAccessCount)
sl@0
   433
			{
sl@0
   434
			Kern::Printf(
sl@0
   435
				"EVENTTRACKER: Access count for %C (%d) does not match the tracking list (%d)",
sl@0
   436
				aCodeSeg,
sl@0
   437
				aCodeSeg->iAccessCount,
sl@0
   438
				trackedCodeSeg->iAccessCount
sl@0
   439
				);
sl@0
   440
			++iErrorCount;
sl@0
   441
			return;
sl@0
   442
			}
sl@0
   443
		}
sl@0
   444
sl@0
   445
	if (!trackedCodeSeg)
sl@0
   446
		{
sl@0
   447
		NKern::ThreadEnterCS();
sl@0
   448
		trackedCodeSeg = new TTrackedCodeSeg(aCodeSeg);
sl@0
   449
		NKern::ThreadLeaveCS();
sl@0
   450
sl@0
   451
		if (trackedCodeSeg)
sl@0
   452
			{
sl@0
   453
			__KTRACE_OPT(KDEBUGGER, Kern::Printf("EVENTTRACKER: Adding %C to tracking list", aCodeSeg));
sl@0
   454
			iItems.Add(&trackedCodeSeg->iLink);
sl@0
   455
			}
sl@0
   456
		}
sl@0
   457
	else // trackedCodeSeg
sl@0
   458
		{
sl@0
   459
		if (aProcess)
sl@0
   460
			{
sl@0
   461
			__KTRACE_OPT(KDEBUGGER, Kern::Printf("EVENTTRACKER: Updating access count for %C (%d), attaching to process %O", aCodeSeg, aCodeSeg->iAccessCount, aProcess));
sl@0
   462
			}
sl@0
   463
		else // !aProcess
sl@0
   464
			{
sl@0
   465
			Kern::Printf("EVENTTRACKER: Found orphaned code seg %C in tracking list while adding new code seg", aCodeSeg);
sl@0
   466
			++iErrorCount;
sl@0
   467
			}
sl@0
   468
		}
sl@0
   469
sl@0
   470
	if (!trackedCodeSeg)
sl@0
   471
		{
sl@0
   472
		iOOM = ETrue;
sl@0
   473
		++iErrorCount;
sl@0
   474
		}
sl@0
   475
	}
sl@0
   476
sl@0
   477
void DEventTracker::ProcessLoaded(DProcess* aProcess)
sl@0
   478
	{
sl@0
   479
	if (aProcess->iCodeSeg)
sl@0
   480
		{
sl@0
   481
		TTrackedCodeSeg* trackedCodeSeg = (TTrackedCodeSeg*)LookupItem(aProcess->iCodeSeg);
sl@0
   482
sl@0
   483
		if (trackedCodeSeg)
sl@0
   484
			{
sl@0
   485
			// This is the exe code seg for a process that
sl@0
   486
			// has completed loading and hence the access
sl@0
   487
			// count has just been decremented by one
sl@0
   488
			--trackedCodeSeg->iAccessCount;
sl@0
   489
			}
sl@0
   490
		}
sl@0
   491
	}
sl@0
   492
sl@0
   493
void DEventTracker::RemoveCodeSeg(DCodeSeg* aCodeSeg, DProcess* aProcess)
sl@0
   494
	{
sl@0
   495
	TTrackedCodeSeg* const trackedCodeSeg = (TTrackedCodeSeg*)LookupItem(aCodeSeg);
sl@0
   496
sl@0
   497
	if (trackedCodeSeg)
sl@0
   498
		{
sl@0
   499
		if (!trackedCodeSeg->CheckIntegrity(aCodeSeg->iAccessCount))
sl@0
   500
			{
sl@0
   501
			++iErrorCount;
sl@0
   502
			}
sl@0
   503
sl@0
   504
		if (aCodeSeg->iAccessCount == 1)
sl@0
   505
			{
sl@0
   506
			__KTRACE_OPT(KDEBUGGER, Kern::Printf("EVENTTRACKER: Removing %C from tracking list, process %O", aCodeSeg, aProcess));
sl@0
   507
			trackedCodeSeg->iLink.Deque();
sl@0
   508
sl@0
   509
			NKern::ThreadEnterCS();
sl@0
   510
			delete trackedCodeSeg;
sl@0
   511
			NKern::ThreadLeaveCS();
sl@0
   512
			}
sl@0
   513
		}
sl@0
   514
	else
sl@0
   515
		{
sl@0
   516
		Kern::Printf("EVENTTRACKER: %C removed but not in tracking list.  Removing from process %O", aCodeSeg, aProcess);
sl@0
   517
		++iErrorCount;
sl@0
   518
		}
sl@0
   519
	}
sl@0
   520
sl@0
   521
sl@0
   522
/** Add all objects from relevant containers into the tracking list.  */
sl@0
   523
sl@0
   524
TInt DEventTracker::AddExistingObjects()
sl@0
   525
	{
sl@0
   526
	// Tracking can be started only after all containers read to avoid 
sl@0
   527
	// race conditions.
sl@0
   528
	__ASSERT_DEBUG(!iTracking, Kern::Fault(KPanicCat, __LINE__));
sl@0
   529
sl@0
   530
	TInt err = KErrNone;
sl@0
   531
	Kern::Printf("Adding processes");
sl@0
   532
	err = AddObjectsFromContainer(EProcess);
sl@0
   533
	if (err)
sl@0
   534
		{
sl@0
   535
		return err;
sl@0
   536
		}
sl@0
   537
	Kern::Printf("Adding threads");
sl@0
   538
	err = AddObjectsFromContainer(EThread);
sl@0
   539
	if (err)
sl@0
   540
		{
sl@0
   541
		return err;
sl@0
   542
		}
sl@0
   543
	Kern::Printf("Adding libraries");
sl@0
   544
	err = AddObjectsFromContainer(ELibrary);
sl@0
   545
	if (err)
sl@0
   546
		{
sl@0
   547
		return err;
sl@0
   548
		}
sl@0
   549
	Kern::Printf("Adding chunks");
sl@0
   550
	err = AddObjectsFromContainer(EChunk);
sl@0
   551
	if (err)
sl@0
   552
		{
sl@0
   553
		return err;
sl@0
   554
		}
sl@0
   555
	Kern::Printf("Adding LDDs");
sl@0
   556
	err = AddObjectsFromContainer(ELogicalDevice);
sl@0
   557
	if (err)
sl@0
   558
		{
sl@0
   559
		return err;
sl@0
   560
		}
sl@0
   561
	Kern::Printf("Adding PDDs");
sl@0
   562
	err = AddObjectsFromContainer(EPhysicalDevice);
sl@0
   563
	if (err)
sl@0
   564
		{
sl@0
   565
		return err;
sl@0
   566
		}
sl@0
   567
	Kern::Printf("Adding code segs");
sl@0
   568
	return AddCodeSegsFromList();
sl@0
   569
	}
sl@0
   570
sl@0
   571
/** Add all objects from specified container into tracking list.  */
sl@0
   572
sl@0
   573
TInt DEventTracker::AddObjectsFromContainer(TObjectType aType)
sl@0
   574
	{
sl@0
   575
	DObjectCon* const container = Kern::Containers()[aType];
sl@0
   576
sl@0
   577
	NKern::ThreadEnterCS();
sl@0
   578
	container->Wait();
sl@0
   579
	
sl@0
   580
	const TInt count = container->Count();
sl@0
   581
	TInt err = KErrNone;
sl@0
   582
	
sl@0
   583
	for (TInt i = 0; (i < count && err == KErrNone); ++i)
sl@0
   584
		{
sl@0
   585
		DObject* const object = (*container)[i];
sl@0
   586
		if (object->Open() == KErrNone)
sl@0
   587
			{
sl@0
   588
			AddObject(aType, object);
sl@0
   589
			if (iOOM)
sl@0
   590
				{
sl@0
   591
				err = KErrNoMemory;
sl@0
   592
				}
sl@0
   593
			object->Close(NULL);
sl@0
   594
			}
sl@0
   595
		}
sl@0
   596
sl@0
   597
	container->Signal();
sl@0
   598
	NKern::ThreadLeaveCS();
sl@0
   599
sl@0
   600
	return err;
sl@0
   601
	}
sl@0
   602
sl@0
   603
TInt DEventTracker::AddCodeSegsFromList()
sl@0
   604
	{
sl@0
   605
	Kern::AccessCode();
sl@0
   606
sl@0
   607
	const SDblQueLink* const anchor = &Kern::CodeSegList()->iA;
sl@0
   608
	for (SDblQueLink* link = Kern::CodeSegList()->First(); link != anchor; link = link->iNext)
sl@0
   609
		{
sl@0
   610
		DCodeSeg* const codeSeg = _LOFF(link, DCodeSeg, iLink);
sl@0
   611
		AddCodeSeg(codeSeg, NULL);
sl@0
   612
		}
sl@0
   613
sl@0
   614
	Kern::EndAccessCode();
sl@0
   615
sl@0
   616
	return KErrNone;
sl@0
   617
	}
sl@0
   618
sl@0
   619
sl@0
   620
/** Check that tracking list matches existing objects. 
sl@0
   621
	@return number of discrepancies found
sl@0
   622
 */
sl@0
   623
sl@0
   624
TInt DEventTracker::CheckIntegrity()
sl@0
   625
	{
sl@0
   626
	// Tracking must be stopped to avoid race conditions.
sl@0
   627
	__ASSERT_DEBUG(!iTracking, Kern::Fault(KPanicCat, __LINE__));
sl@0
   628
sl@0
   629
	if (iOOM)
sl@0
   630
		{
sl@0
   631
		Kern::Printf("EVENTTRACKER: OOM during tracking");
sl@0
   632
		}
sl@0
   633
sl@0
   634
	CheckContainerIntegrity(EProcess);
sl@0
   635
	CheckContainerIntegrity(EThread);
sl@0
   636
	CheckContainerIntegrity(ELibrary);
sl@0
   637
	CheckContainerIntegrity(EChunk);
sl@0
   638
	CheckContainerIntegrity(ELogicalDevice);
sl@0
   639
	CheckContainerIntegrity(EPhysicalDevice);
sl@0
   640
	CheckCodeSegListIntegrity();
sl@0
   641
sl@0
   642
	CheckAllAccountedFor();
sl@0
   643
sl@0
   644
	if (iErrorCount)
sl@0
   645
		{
sl@0
   646
		Kern::Printf("EVENTTRACKER: %d error(s) found", iErrorCount);
sl@0
   647
		return KErrGeneral;
sl@0
   648
		}
sl@0
   649
sl@0
   650
	return KErrNone;
sl@0
   651
	}
sl@0
   652
sl@0
   653
/** Check all objects in specified container are in tracking list.  */
sl@0
   654
sl@0
   655
void DEventTracker::CheckContainerIntegrity(TObjectType aType)
sl@0
   656
	{
sl@0
   657
	DObjectCon* const container = Kern::Containers()[aType];
sl@0
   658
sl@0
   659
	NKern::ThreadEnterCS();
sl@0
   660
	container->Wait();
sl@0
   661
sl@0
   662
	const TInt count = container->Count();
sl@0
   663
	
sl@0
   664
	for (TInt i = 0; i < count; ++i)
sl@0
   665
		{
sl@0
   666
		DObject* const object = (*container)[i];
sl@0
   667
		if (object->Open() == KErrNone)
sl@0
   668
			{
sl@0
   669
			TFullName name;
sl@0
   670
			object->FullName(name);
sl@0
   671
sl@0
   672
			TTrackedObject* const trackedObject = (TTrackedObject*)LookupItem(object);
sl@0
   673
			
sl@0
   674
			if (trackedObject)
sl@0
   675
				{
sl@0
   676
				trackedObject->iAccountedFor = ETrue;
sl@0
   677
				if (!trackedObject->CheckIntegrity(name, aType))
sl@0
   678
					{
sl@0
   679
					++iErrorCount;
sl@0
   680
					}
sl@0
   681
				}
sl@0
   682
			else
sl@0
   683
				{
sl@0
   684
				Kern::Printf("EVENTTRACKER: %S (type %d) is in container but not in tracking list", &name, aType);
sl@0
   685
				++iErrorCount;
sl@0
   686
				}
sl@0
   687
sl@0
   688
			object->Close(NULL);
sl@0
   689
			}
sl@0
   690
		}
sl@0
   691
sl@0
   692
	container->Signal();
sl@0
   693
	NKern::ThreadLeaveCS();
sl@0
   694
	}
sl@0
   695
sl@0
   696
void DEventTracker::CheckCodeSegListIntegrity()
sl@0
   697
	{
sl@0
   698
	Kern::AccessCode();
sl@0
   699
sl@0
   700
	const SDblQueLink* const anchor = &Kern::CodeSegList()->iA;
sl@0
   701
	for (SDblQueLink* link = Kern::CodeSegList()->First(); link != anchor; link = link->iNext)
sl@0
   702
		{
sl@0
   703
		DCodeSeg* const codeSeg = _LOFF(link, DCodeSeg, iLink);
sl@0
   704
		TTrackedCodeSeg* const trackedCodeSeg = (TTrackedCodeSeg*)LookupItem(codeSeg);
sl@0
   705
sl@0
   706
		if (trackedCodeSeg)
sl@0
   707
			{
sl@0
   708
			trackedCodeSeg->iAccountedFor = ETrue;
sl@0
   709
			if (!trackedCodeSeg->CheckIntegrity(codeSeg->iAccessCount))
sl@0
   710
				{
sl@0
   711
				++iErrorCount;
sl@0
   712
				}
sl@0
   713
			}
sl@0
   714
		else
sl@0
   715
			{
sl@0
   716
			Kern::Printf("EVENTTRACKER: %C is in global list but not in tracking list", codeSeg);
sl@0
   717
			++iErrorCount;
sl@0
   718
			}
sl@0
   719
		}
sl@0
   720
sl@0
   721
	Kern::EndAccessCode();
sl@0
   722
	}
sl@0
   723
sl@0
   724
sl@0
   725
/** Check that all objects in tracking list have been accounted for. */
sl@0
   726
void DEventTracker::CheckAllAccountedFor()
sl@0
   727
	{
sl@0
   728
	const SDblQueLink* link = iItems.GetFirst();
sl@0
   729
	while (link)
sl@0
   730
		{
sl@0
   731
		TTrackedItem* const item = _LOFF(link, TTrackedItem, iLink);
sl@0
   732
		if (!item->iAccountedFor)
sl@0
   733
			{
sl@0
   734
			Kern::Printf(
sl@0
   735
				"EVENTTRACKER: 0x%x is in tracking list but not in container / list", 
sl@0
   736
				&item->iObject
sl@0
   737
				);
sl@0
   738
			++iErrorCount;
sl@0
   739
			}
sl@0
   740
		link = iItems.GetFirst();
sl@0
   741
		}
sl@0
   742
	}
sl@0
   743
sl@0
   744
/** Look for specified object in the tracking list. 
sl@0
   745
	@pre iLock held
sl@0
   746
	@post iLock held
sl@0
   747
 */
sl@0
   748
TTrackedItem* DEventTracker::LookupItem(DBase* aItem) const
sl@0
   749
	{
sl@0
   750
	const SDblQueLink* const anchor = &iItems.iA;
sl@0
   751
	
sl@0
   752
	for (SDblQueLink* link = iItems.First(); link != anchor; link = link->iNext)
sl@0
   753
		{
sl@0
   754
		TTrackedItem* const item = _LOFF(link, TTrackedItem, iLink);
sl@0
   755
sl@0
   756
		if (item->iObject == aItem)
sl@0
   757
			{
sl@0
   758
			return item;
sl@0
   759
			}
sl@0
   760
		}
sl@0
   761
sl@0
   762
	return NULL;
sl@0
   763
	}
sl@0
   764
sl@0
   765
sl@0
   766
void DEventTracker::DumpCounters() const
sl@0
   767
	{
sl@0
   768
	static const char* const KEventName[] = 
sl@0
   769
		{
sl@0
   770
		"SwExc           ",
sl@0
   771
		"HwExc           ",
sl@0
   772
		"AddProcess      ",
sl@0
   773
		"UpdateProcess   ",
sl@0
   774
		"RemoveProcess   ",
sl@0
   775
		"AddThread       ",
sl@0
   776
		"StartThread     ",
sl@0
   777
		"UpdateThread    ",
sl@0
   778
		"KillThread      ",
sl@0
   779
		"RemoveThread    ",
sl@0
   780
		"NewChunk        ",
sl@0
   781
		"UpdateChunk     ",
sl@0
   782
		"DeleteChunk     ",
sl@0
   783
		"AddLibrary      ",
sl@0
   784
		"RemoveLibrary   ",
sl@0
   785
		"LoadLdd         ",
sl@0
   786
		"UnloadLdd       ",
sl@0
   787
		"LoadPdd         ",
sl@0
   788
		"UnloadPdd       ",
sl@0
   789
		"UserTrace       ",
sl@0
   790
		"AddCodeSeg      ",
sl@0
   791
		"RemoveCodeSeg   ",
sl@0
   792
		"LoadedProcess   ",
sl@0
   793
		"UnloadingProcess"
sl@0
   794
		};
sl@0
   795
sl@0
   796
	Kern::Printf("EVENT USAGE STATISTICS:");
sl@0
   797
sl@0
   798
	for (TInt i = 0; i < EEventLimit; ++i)
sl@0
   799
		{
sl@0
   800
		Kern::Printf("\t%s\t\t %d times", KEventName[i], iCounters[i]);
sl@0
   801
		}
sl@0
   802
	}
sl@0
   803
sl@0
   804
#ifdef __MARM__
sl@0
   805
void DEventTracker::CopyDummyHandler(TLinAddr aLinAddr)
sl@0
   806
	{
sl@0
   807
	const TUint handlerSize = DummyHandlerSize();
sl@0
   808
sl@0
   809
	// Copy the breakpoint-able handler into RAM by copying from the one (possibly) in ROM
sl@0
   810
	memcpy((TAny*)aLinAddr, (TAny*) &DummyHandler, handlerSize);
sl@0
   811
	__KTRACE_OPT(KBOOT, Kern::Printf("Breakpoint-able handler copied from 0x%x to (va) 0x%x, size %d", &DummyHandler, aLinAddr, handlerSize));
sl@0
   812
	}
sl@0
   813
#endif
sl@0
   814
	
sl@0
   815
//////////////////////////////////////////////////////////////////////////////
sl@0
   816
sl@0
   817
class DTestChannel : public DLogicalChannelBase
sl@0
   818
	{
sl@0
   819
public:
sl@0
   820
	virtual ~DTestChannel();
sl@0
   821
protected:
sl@0
   822
	// from DLogicalChannelBase
sl@0
   823
	virtual TInt DoCreate(TInt aUnit, const TDesC8* aInfo, const TVersion& aVer);
sl@0
   824
	virtual TInt Request(TInt aFunction, TAny* a1, TAny* a2);
sl@0
   825
private:
sl@0
   826
	DEventTracker* iHandler;
sl@0
   827
	};
sl@0
   828
sl@0
   829
sl@0
   830
// called in thread critical section
sl@0
   831
TInt DTestChannel::DoCreate(TInt aUnit, const TDesC8* /*aInfo*/, const TVersion& /*aVer*/)
sl@0
   832
	{
sl@0
   833
	if((TUint)aUnit>=2)
sl@0
   834
		return KErrNotSupported;
sl@0
   835
sl@0
   836
	TBool useHook = aUnit;
sl@0
   837
sl@0
   838
	iHandler = new DEventTracker;
sl@0
   839
sl@0
   840
	if (!iHandler)
sl@0
   841
		{
sl@0
   842
		return KErrNoMemory;
sl@0
   843
		}
sl@0
   844
sl@0
   845
	return iHandler->Create(iDevice, useHook);
sl@0
   846
	}
sl@0
   847
sl@0
   848
// called in thread critical section
sl@0
   849
DTestChannel::~DTestChannel()
sl@0
   850
	{
sl@0
   851
	if (iHandler)
sl@0
   852
		{
sl@0
   853
		iHandler->Close();
sl@0
   854
		}
sl@0
   855
	}
sl@0
   856
sl@0
   857
sl@0
   858
TInt DTestChannel::Request(TInt aFunction, TAny* /*a1*/, TAny* /*a2*/)
sl@0
   859
	{
sl@0
   860
	TInt r = KErrNone;
sl@0
   861
	switch (aFunction)
sl@0
   862
		{
sl@0
   863
	case REventTracker::EStart:
sl@0
   864
		iHandler->Start();
sl@0
   865
		break;
sl@0
   866
	case REventTracker::EStop:
sl@0
   867
		iHandler->Stop();
sl@0
   868
		break;
sl@0
   869
	default:
sl@0
   870
		Kern::PanicCurrentThread(KClientPanicCat, __LINE__);
sl@0
   871
		break;
sl@0
   872
		}
sl@0
   873
	return r;
sl@0
   874
	}
sl@0
   875
sl@0
   876
sl@0
   877
//////////////////////////////////////////////////////////////////////////////
sl@0
   878
sl@0
   879
class DTestFactory : public DLogicalDevice
sl@0
   880
	{
sl@0
   881
public:
sl@0
   882
	DTestFactory();
sl@0
   883
	// from DLogicalDevice
sl@0
   884
	virtual TInt Install();
sl@0
   885
	virtual void GetCaps(TDes8& aDes) const;
sl@0
   886
	virtual TInt Create(DLogicalChannelBase*& aChannel);
sl@0
   887
	};
sl@0
   888
sl@0
   889
DTestFactory::DTestFactory()
sl@0
   890
    {
sl@0
   891
    iVersion = REventTracker::Version();
sl@0
   892
    iParseMask = KDeviceAllowUnit;
sl@0
   893
    iUnitsMask = 0x3;
sl@0
   894
    }
sl@0
   895
sl@0
   896
TInt DTestFactory::Create(DLogicalChannelBase*& aChannel)
sl@0
   897
    {
sl@0
   898
	aChannel = new DTestChannel;
sl@0
   899
	return (aChannel ? KErrNone : KErrNoMemory);
sl@0
   900
    }
sl@0
   901
sl@0
   902
TInt DTestFactory::Install()
sl@0
   903
    {
sl@0
   904
    return SetName(&KTestLddName);
sl@0
   905
    }
sl@0
   906
sl@0
   907
void DTestFactory::GetCaps(TDes8& /*aDes*/) const
sl@0
   908
    {
sl@0
   909
    }
sl@0
   910
sl@0
   911
//////////////////////////////////////////////////////////////////////////////
sl@0
   912
sl@0
   913
DECLARE_STANDARD_LDD()
sl@0
   914
	{
sl@0
   915
    return new DTestFactory;
sl@0
   916
	}