memspyui/ui/hb/src/enginewrapper.cpp
changeset 35 98924d2efce9
parent 19 4b22a598b890
equal deleted inserted replaced
34:e0ec97ec3cc4 35:98924d2efce9
    13 *
    13 *
    14 * Description:
    14 * Description:
    15 * 
    15 * 
    16 */
    16 */
    17 
    17 
       
    18 #include <QMessageBox>
       
    19 
       
    20 #include <memspy/engine/memspyenginehelpersysmemtrackerconfig.h>
       
    21 
    18 #include "enginewrapper.h"
    22 #include "enginewrapper.h"
    19 #include <QMessageBox>
    23 
       
    24 // CONSTANTS
       
    25 
       
    26 const QString OUTPUT_MODE = "output/mode";
       
    27 const QString OUTPUT_PATH = "output/path";
       
    28 const QString SWMT_PERIOD = "swmt/period";
       
    29 const QString SWMT_MODE = "swmt/mode";
       
    30 const QString SWMT_CATEGORIES = "swmt/categories";
       
    31 const QString HEAP_DUMPS_MODE = "heapdumps/mode";
       
    32 
       
    33 QString MemSpyProcess::exitInfo() const
       
    34 {
       
    35     QChar exitType[] = {'K', 'T', 'P', 'R'};
       
    36     QString result = QString("[%1]").arg(exitType[mProcess->ExitType()]);
       
    37     
       
    38     if (mProcess->ExitType() != EExitKill && mProcess->ExitType() != EExitPending) {
       
    39         
       
    40         QString exitCategory = QString((QChar*) mProcess->ExitCategory().Ptr(), mProcess->ExitCategory().Length());
       
    41         result.append(QString(" %1-%2").arg(exitCategory).arg(mProcess->ExitReason()));
       
    42     }
       
    43     
       
    44     return result;
       
    45 }
       
    46 
       
    47 
       
    48 MemSpyDwoProgressTracker::MemSpyDwoProgressTracker(RMemSpySession &session) :
       
    49 	CActive(EPriorityStandard), mSession(session)
       
    50 {
       
    51 	CActiveScheduler::Add(this);
       
    52 }
       
    53 
       
    54 MemSpyDwoProgressTracker::~MemSpyDwoProgressTracker()
       
    55 {
       
    56 	Cancel();
       
    57 }
       
    58 
       
    59 void MemSpyDwoProgressTracker::start()
       
    60 {
       
    61 	mSession.NotifyDeviceWideOperationProgress(mProgress, iStatus);
       
    62 	
       
    63 	SetActive();
       
    64 }
       
    65 
       
    66 void MemSpyDwoProgressTracker::cancel()
       
    67 {
       
    68 	Cancel();
       
    69 }
       
    70 
       
    71 // Event handler method.
       
    72  
       
    73 void MemSpyDwoProgressTracker::RunL()
       
    74     {
       
    75  
       
    76     // If an error occurred handle it in RunError().
       
    77     User::LeaveIfError(iStatus.Int());
       
    78  
       
    79     // Resubmit the request immediately
       
    80     
       
    81     mSession.NotifyDeviceWideOperationProgress(mProgress, iStatus);
       
    82  
       
    83     SetActive();
       
    84  
       
    85     emit progress(mProgress.Progress(), QString((QChar*) mProgress.Description().Ptr(), mProgress.Description().Length()));
       
    86     }
       
    87  
       
    88 void MemSpyDwoProgressTracker::DoCancel()
       
    89 { 
       
    90 	// this is not yet implemented, as it is not required in current use cases
       
    91 }
       
    92  
       
    93 TInt MemSpyDwoProgressTracker::RunError(TInt aError)
       
    94 { 
       
    95 	// KErrNotReady and KErrCancel errors are OK, they just notify 
       
    96 	// us about the outstanding notification request that won't be 
       
    97 	// processed.
       
    98 	Q_UNUSED(aError);
       
    99 	
       
   100     return KErrNone;
       
   101 }
       
   102 
       
   103 MemSpyDwoTracker::MemSpyDwoTracker(RMemSpySession &session, DeviceWideOperation operation) :
       
   104 	CActive(EPriorityStandard), 
       
   105 	mSession(session),
       
   106 	mProgressTracker(new MemSpyDwoProgressTracker(session)),
       
   107 	mOperation(operation)
       
   108 {
       
   109 	CActiveScheduler::Add(this);
       
   110 	connect(mProgressTracker, SIGNAL(progress(int,QString)), this, SIGNAL(progress(int,QString)));
       
   111 }
       
   112 
       
   113 MemSpyDwoTracker::~MemSpyDwoTracker()
       
   114 {
       
   115 	Cancel();
       
   116 	
       
   117 	delete mProgressTracker;
       
   118 }
       
   119 
       
   120 void MemSpyDwoTracker::start()
       
   121 {
       
   122 	void (RMemSpySession::*functions[])(TRequestStatus&) = { 
       
   123 			&RMemSpySession::OutputPhoneInfo,
       
   124 			&RMemSpySession::OutputDetailedPhoneInfo,
       
   125 			&RMemSpySession::OutputHeapInfo,
       
   126 			&RMemSpySession::OutputCompactHeapInfo,
       
   127 			&RMemSpySession::OutputHeapCellListing,
       
   128 			&RMemSpySession::OutputHeapData,
       
   129 			&RMemSpySession::OutputStackInfo,
       
   130 			&RMemSpySession::OutputCompactStackInfo,
       
   131 			&RMemSpySession::OutputUserStackData,
       
   132 			&RMemSpySession::OutputKernelStackData };
       
   133 	
       
   134 	(mSession.*functions[mOperation])(iStatus);
       
   135 	
       
   136 	mProgressTracker->start();
       
   137 	
       
   138 	SetActive();
       
   139 }
       
   140 
       
   141 void MemSpyDwoTracker::cancel()
       
   142 {
       
   143 	Cancel();
       
   144 }
       
   145 
       
   146 // Event handler method.
       
   147  
       
   148 void MemSpyDwoTracker::RunL()
       
   149     {
       
   150  
       
   151     // If an error occurred handle it in RunError().
       
   152     User::LeaveIfError(iStatus.Int());
       
   153  
       
   154     // Operation has finished successfully 
       
   155     emit finished(0);
       
   156     }
       
   157  
       
   158 void MemSpyDwoTracker::DoCancel()
       
   159 {
       
   160 	// Cancel progress tracker
       
   161 	mProgressTracker->cancel();
       
   162 	
       
   163 	mSession.CancelDeviceWideOperationL();
       
   164 }
       
   165  
       
   166 TInt MemSpyDwoTracker::RunError(TInt aError)
       
   167 { 
       
   168 	// Emit the finished signal to notify user 
       
   169 	// operation was canceled
       
   170 	emit finished(aError);
       
   171 	
       
   172     return KErrNone;
       
   173 }
       
   174 
       
   175 MemSpyAsyncTracker::MemSpyAsyncTracker(RMemSpySession& session, void (RMemSpySession::*function)(TRequestStatus&)) :
       
   176 	CActive(EPriorityStandard), 
       
   177 	mFunction(function),
       
   178 	mSession(session)
       
   179 {
       
   180 	CActiveScheduler::Add(this);
       
   181 }
       
   182 
       
   183 void MemSpyAsyncTracker::RunL()
       
   184 { 
       
   185     // If an error occurred handle it in RunError().
       
   186     User::LeaveIfError(iStatus.Int());
       
   187  
       
   188     // Operation has finished successfully 
       
   189     emit finished(0);
       
   190 }
       
   191 
       
   192 void MemSpyAsyncTracker::DoCancel()
       
   193 {
       
   194 	// nothing to do here
       
   195 }
       
   196 
       
   197 TInt MemSpyAsyncTracker::RunError(TInt aError)
       
   198 { 
       
   199 	// Emit the finished signal to notify user 
       
   200 	// that operation was canceled
       
   201 	emit finished(aError);
       
   202 	
       
   203     return KErrNone;
       
   204 }
       
   205 
       
   206 void MemSpyAsyncTracker::start()
       
   207 {
       
   208 	(mSession.*mFunction)(iStatus);
       
   209 	
       
   210 	SetActive();
       
   211 }
       
   212 
       
   213 MemSpySettings::MemSpySettings() : 
       
   214 	QSettings("Nokia", "MemSpy") 
       
   215 {
       
   216 }
       
   217 
       
   218 OutputMode MemSpySettings::outputMode() const
       
   219 {
       
   220 	return static_cast<OutputMode>(value(OUTPUT_MODE, 0).toInt());
       
   221 }
       
   222 
       
   223 void MemSpySettings::setOutputMode(OutputMode mode)
       
   224 {
       
   225 	setValue(OUTPUT_MODE, mode);
       
   226 }
       
   227 	
       
   228 QString MemSpySettings::outputPath() const
       
   229 {
       
   230 	return value(OUTPUT_PATH).toString();
       
   231 }
       
   232 
       
   233 void MemSpySettings::setOutputPath(const QString& path)
       
   234 {
       
   235 	setValue(OUTPUT_PATH, path);
       
   236 }
       
   237 
       
   238 int MemSpySettings::swmtTimerPeriod() const
       
   239 {
       
   240 	return value(SWMT_PERIOD, 30).toInt();
       
   241 }
       
   242 
       
   243 void MemSpySettings::setSwmtMode(SwmtMode mode)
       
   244 {
       
   245 	setValue(SWMT_MODE, mode);
       
   246 }
       
   247 
       
   248 SwmtMode MemSpySettings::swmtMode() const
       
   249 {
       
   250 	return static_cast<SwmtMode>(value(SWMT_MODE, 0).toInt());
       
   251 }
       
   252 
       
   253 void MemSpySettings::setSwmtTimerPeriod(int period)
       
   254 {
       
   255 	setValue(SWMT_PERIOD, period);
       
   256 }
       
   257 
       
   258 
       
   259 QVariantList MemSpySettings::swmtCategories() const
       
   260 {
       
   261 	return value(SWMT_CATEGORIES).toList();
       
   262 }
       
   263 
       
   264 void MemSpySettings::setSwmtCategories(const QVariantList& categories)
       
   265 {
       
   266 	setValue(SWMT_CATEGORIES, categories);
       
   267 }
       
   268 
       
   269 HeapDumpsMode MemSpySettings::heapDumpsMode() const
       
   270 {
       
   271 	return static_cast<HeapDumpsMode>(value(HEAP_DUMPS_MODE).toInt());
       
   272 }
       
   273 
       
   274 void MemSpySettings::setHeapDumpsMode(HeapDumpsMode mode)
       
   275 {
       
   276 	setValue(HEAP_DUMPS_MODE, mode);
       
   277 }
       
   278 
       
   279 
       
   280 EngineWrapper::EngineWrapper() :
       
   281 	mSwmtRunning(false)
       
   282 {
       
   283 }
    20 
   284 
    21 EngineWrapper::~EngineWrapper()
   285 EngineWrapper::~EngineWrapper()
    22 {
   286 {
    23 	mSession.Close();
   287 	mSession.Close();
    24 }
   288 }
    26 bool EngineWrapper::initialize()
   290 bool EngineWrapper::initialize()
    27 {
   291 {
    28 	return mSession.Connect() == KErrNone;
   292 	return mSession.Connect() == KErrNone;
    29 }
   293 }
    30 
   294 
       
   295 MemSpySettings& EngineWrapper::settings()
       
   296 {
       
   297 	return mSettings;
       
   298 }
       
   299 
       
   300 const MemSpySettings& EngineWrapper::settings() const
       
   301 {
       
   302 	return mSettings;
       
   303 }
       
   304 
       
   305 
       
   306 
    31 QList<MemSpyProcess*> EngineWrapper::getProcesses()
   307 QList<MemSpyProcess*> EngineWrapper::getProcesses()
    32 {
   308 {
    33 	QList<MemSpyProcess*> result;
   309 	QList<MemSpyProcess*> result;
    34 	
   310 	
    35 	RArray<CMemSpyApiProcess*> proc;
   311 	RArray<CMemSpyApiProcess*> proc;
    36 	TRAPD(error, mSession.GetProcessesL(proc));
   312 	
    37 	if (error == KErrNone)
   313 	QT_TRAP_THROWING(mSession.GetProcessesL(proc));
    38 		for(TInt i=0; i<proc.Count(); i++)
   314 	
    39 			result.append(new MemSpyProcess(proc[i]));
   315 	for (TInt i=0; i<proc.Count(); i++)
       
   316         result.append(new MemSpyProcess(proc[i]));
    40 	
   317 	
    41 	return result;
   318 	return result;
    42 }
   319 }
    43 
   320 
    44 QList<MemSpyThread*> EngineWrapper::getThreads(ProcessId processId)
   321 QList<MemSpyThread*> EngineWrapper::getThreads(ProcessId processId)
    45 {
   322 {
    46 	QList<MemSpyThread*> result;
   323 	QList<MemSpyThread*> result;
    47 	
   324 	
    48 	RArray<CMemSpyApiThread*> proc;
   325 	RArray<CMemSpyApiThread*> proc;
    49 	TRAPD(error, mSession.GetThreadsL(processId, proc));
   326 	
    50 	if (error == KErrNone)
   327 	QT_TRAP_THROWING(mSession.GetThreadsL(processId, proc));
    51 		for(TInt i=0; i<proc.Count(); i++)
   328 	
    52 			result.append(new MemSpyThread(proc[i]));
   329     for (TInt i=0; i<proc.Count(); i++)
       
   330         result.append(new MemSpyThread(proc[i]));
    53 	
   331 	
    54 	return result;
   332 	return result;
    55 }
   333 }
    56 
   334 
    57 QList<MemSpyThreadInfoItem*> EngineWrapper::getThreadInfo(ThreadId threadId, ThreadInfoType type)
   335 QList<MemSpyThreadInfoItem*> EngineWrapper::getThreadInfo(ThreadId threadId, ThreadInfoType type)
    58 {
   336 {
    59 	QList<MemSpyThreadInfoItem*> result;
   337 	QList<MemSpyThreadInfoItem*> result;
    60 	RArray<CMemSpyApiThreadInfoItem*> threadInfo;
   338 	RArray<CMemSpyApiThreadInfoItem*> threadInfo;
    61 	TRAPD(error, mSession.GetThreadInfoItems(threadInfo, threadId, 
   339 	qt_symbian_throwIfError(mSession.GetThreadInfoItems(threadInfo, threadId, 
    62 			static_cast<TMemSpyThreadInfoItemType>(type)));
   340 			static_cast<TMemSpyThreadInfoItemType>(type)));
    63 	if (error == KErrNone)
   341 	
    64 		for (TInt i=0; i<threadInfo.Count(); i++)
   342 	for (TInt i=0; i<threadInfo.Count(); i++)
    65 			result.append(new MemSpyThreadInfoItem(threadInfo[i]));
   343 		result.append(new MemSpyThreadInfoItem(threadInfo[i]));
    66 	
   344 	
    67 	return result;
   345 	return result;
    68 }
   346 }
    69 
   347 
    70 void EngineWrapper::setThreadPriority(ThreadId threadId, ThreadPriority priority)
   348 void EngineWrapper::setThreadPriority(ThreadId threadId, ThreadPriority priority)
    75 QList<MemSpyKernelObjectType*> EngineWrapper::getKernelObjectTypes()
   353 QList<MemSpyKernelObjectType*> EngineWrapper::getKernelObjectTypes()
    76 {
   354 {
    77 	QList<MemSpyKernelObjectType*> result;
   355 	QList<MemSpyKernelObjectType*> result;
    78 		
   356 		
    79 	RArray<CMemSpyApiKernelObject*> types;
   357 	RArray<CMemSpyApiKernelObject*> types;
    80 	TInt error = mSession.GetKernelObjects(types);
   358 	qt_symbian_throwIfError(mSession.GetKernelObjects(types));
    81 	if (error == KErrNone)
   359 	
    82 		for(TInt i=0; i<types.Count(); i++)
   360 	for(TInt i=0; i<types.Count(); i++)
    83 			result.append(new MemSpyKernelObjectType(types[i]));
   361 		result.append(new MemSpyKernelObjectType(types[i]));
    84 	
   362 	
    85 	return result;
   363 	return result;
    86 }
   364 }
    87 
   365 
    88 QList<MemSpyKernelObject*> EngineWrapper::getKernelObjects(int type)
   366 QList<MemSpyKernelObject*> EngineWrapper::getKernelObjects(int type)
    89 {
   367 {
    90 	QList<MemSpyKernelObject*> result;
   368 	QList<MemSpyKernelObject*> result;
    91 		
   369 		
    92 	RArray<CMemSpyApiKernelObjectItem*> objects;
   370 	RArray<CMemSpyApiKernelObjectItem*> objects;
    93 	TInt error = mSession.GetKernelObjectItems(objects, 
   371 	qt_symbian_throwIfError(mSession.GetKernelObjectItems(objects, 
    94 			static_cast<TMemSpyDriverContainerType>(type));
   372 			static_cast<TMemSpyDriverContainerType>(type)));
    95 	if (error == KErrNone)
   373 	
    96 		for(TInt i=0; i<objects.Count(); i++)
   374 	for(TInt i=0; i<objects.Count(); i++)
    97 			result.append(new MemSpyKernelObject(objects[i]));
   375 		result.append(new MemSpyKernelObject(objects[i]));
    98 	
   376 	
    99 	return result;
   377 	return result;
   100 }
   378 }
       
   379 
       
   380 MemSpyDwoTracker* EngineWrapper::createDeviceWideOperation(DeviceWideOperation operation)
       
   381 {
       
   382 	return new MemSpyDwoTracker(mSession, operation);
       
   383 }
       
   384 
       
   385 MemSpyKernelHeapDumpTracker* EngineWrapper::createKernelHeapDumpTracker()
       
   386 {
       
   387 	return new MemSpyKernelHeapDumpTracker(mSession);
       
   388 }
       
   389 
       
   390 MemSpySwmtDumpTracker* EngineWrapper::createSwmtDumpTracker()
       
   391 {
       
   392 	return new MemSpySwmtDumpTracker(mSession);
       
   393 }
       
   394 
       
   395 void EngineWrapper::setSwmtSettings(SwmtMode mode, const QVariantList& categories)
       
   396 {
       
   397 	int bits = 0;
       
   398 	bool heapDumps = false;
       
   399 	switch (mode)
       
   400 	{
       
   401 	case SwmtModeBasic:
       
   402 		bits = TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryUserHeap | 
       
   403 			TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryUserStacks |
       
   404 			TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryGlobalData |
       
   405 			TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategorySystemMemory;
       
   406 		
       
   407 		break;
       
   408 		
       
   409 	case SwmtModeFull:
       
   410 		bits = TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryAll;
       
   411 		heapDumps = true;
       
   412 		
       
   413 		break;
       
   414 		
       
   415 	case SwmtModeCustom:
       
   416 		bits = 0;
       
   417 		// this needs to be in sync. with swmt view categories
       
   418 		TMemSpyEngineHelperSysMemTrackerConfig::TMemSpyEngineSysMemTrackerCategories cats[] = {
       
   419             TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryFileServerCache,
       
   420             //TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryBitmapHandles,
       
   421             TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryUserHeap,
       
   422             //TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryKernelHeap,
       
   423             TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryLocalChunks,
       
   424             TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryGlobalChunks,
       
   425             TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryRAMDrive,
       
   426             //TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryUserStacks,
       
   427             //TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryGlobalData,
       
   428             TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryRAMLoadedCode,
       
   429             TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryKernelHandles,
       
   430             TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryOpenFiles,
       
   431             TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryDiskusage,
       
   432             TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategorySystemMemory,
       
   433             TMemSpyEngineHelperSysMemTrackerConfig::EMemSpyEngineSysMemTrackerCategoryWindowGroups
       
   434 		};
       
   435 
       
   436 		foreach (const QVariant& bit, categories)
       
   437 			bits |= cats[bit.toInt()];
       
   438 		
       
   439 		break;
       
   440 	}
       
   441 	
       
   442 	TRAP_IGNORE(mSession.SetSwmtCategoriesL(bits));
       
   443 	TRAP_IGNORE(mSession.SetSwmtHeapDumpsEnabledL(heapDumps));
       
   444 }
       
   445 
       
   446 bool EngineWrapper::isSwmtRunning()
       
   447 {
       
   448 	return mSwmtRunning;
       
   449 }
       
   450 	
       
   451 void EngineWrapper::startSwmt(int period)
       
   452 {
       
   453 	mSwmtRunning = true;
       
   454 	updateOutputSettings();
       
   455 	TRAP_IGNORE(mSession.StartSwmtTimerL(period));
       
   456 }
       
   457 
       
   458 void EngineWrapper::stopSwmt()
       
   459 {
       
   460 	mSwmtRunning = false;
       
   461 	TRAP_IGNORE(mSession.StopSwmtTimerL());
       
   462 }
       
   463 
       
   464 void EngineWrapper::forceSwmtDump()
       
   465 {
       
   466 	updateOutputSettings();	
       
   467 	TRAP_IGNORE(mSession.ForceSwmtUpdateL());
       
   468 }
       
   469 
       
   470 void EngineWrapper::outputKernelHeapData()
       
   471 {
       
   472 	updateOutputSettings();
       
   473 	TRAP_IGNORE(mSession.OutputKernelHeapDataL());
       
   474 }
       
   475 
       
   476 int EngineWrapper::outputThreadHeapData(const QString& filter)
       
   477 {
       
   478 	TPtrC customFilterDesc(static_cast<const TUint16*>(filter.utf16()), filter.length());
       
   479 	
       
   480 	TRAPD(err, mSession.OutputThreadHeapDataL(customFilterDesc));
       
   481 	
       
   482 	return err;
       
   483 }
       
   484 
       
   485 void EngineWrapper::updateOutputSettings()
       
   486 {
       
   487 	switch (mSettings.outputMode()) {
       
   488 		case OutputModeTrace:
       
   489 			TRAP_IGNORE(mSession.SwitchOutputToTraceL());
       
   490 			break;
       
   491 		
       
   492 		case OutputModeFile:
       
   493 		
       
   494 			if (mSettings.outputPath().isEmpty()) {
       
   495 				TRAP_IGNORE(mSession.SwitchOutputToFileL(KNullDesC));
       
   496 			
       
   497 			} else {
       
   498 				
       
   499 				QString root = mSettings.outputPath();
       
   500 				TPtrC rootDesc (static_cast<const TUint16*>(root.utf16()), root.length());
       
   501 			
       
   502 				TRAP_IGNORE(mSession.SwitchOutputToFileL(rootDesc));
       
   503 			}
       
   504 	
       
   505 	}
       
   506 }