os/kernelhwsrv/kerneltest/e32test/rm_debug/d_rmdebugserver.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2006-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
// Provides the debug agent server implementation.
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include <e32base.h>
sl@0
    19
#include <e32base_private.h>
sl@0
    20
#include <e32cons.h>
sl@0
    21
#include <trkkerneldriver.h>
sl@0
    22
#include "d_rmdebugserver.h"
sl@0
    23
#include "d_rmdebugclient.h"
sl@0
    24
#include "t_rmdebug.h"
sl@0
    25
sl@0
    26
sl@0
    27
CDebugServServer::CDebugServServer(CActive::TPriority aActiveObjectPriority)
sl@0
    28
   : CServer2(aActiveObjectPriority)
sl@0
    29
//
sl@0
    30
// Server constructor
sl@0
    31
//
sl@0
    32
	{
sl@0
    33
	}
sl@0
    34
sl@0
    35
CSession2* CDebugServServer::NewSessionL(const TVersion& /*aVersion*/, const RMessage2& /*aMessage*/) const
sl@0
    36
//
sl@0
    37
// Session constructor
sl@0
    38
//
sl@0
    39
	{
sl@0
    40
	// make sure the kernel side device driver is not already loaded
sl@0
    41
   TInt err;
sl@0
    42
	err = User::LoadLogicalDevice(KDebugDriverFileName);
sl@0
    43
	if ((KErrNone == err) || (KErrAlreadyExists == err))
sl@0
    44
       {
sl@0
    45
       return new(ELeave) CDebugServSession();
sl@0
    46
       }
sl@0
    47
   else
sl@0
    48
       {
sl@0
    49
       return (NULL);
sl@0
    50
       }
sl@0
    51
	}   
sl@0
    52
sl@0
    53
CDebugServSession::CDebugServSession()
sl@0
    54
// Session implementation
sl@0
    55
	{
sl@0
    56
   TInt err;
sl@0
    57
	TMetroTrkDriverInfo info;
sl@0
    58
   info.iUserLibraryEnd = 0;
sl@0
    59
   err = iKernelDriver.Open(info);
sl@0
    60
   if (KErrNone != err)
sl@0
    61
		{
sl@0
    62
 		User::Leave(err);
sl@0
    63
		}          
sl@0
    64
	}
sl@0
    65
sl@0
    66
CDebugServSession::~CDebugServSession()
sl@0
    67
//
sl@0
    68
// Session destructor
sl@0
    69
//
sl@0
    70
	{
sl@0
    71
	// stop the kernel side driver
sl@0
    72
	iKernelDriver.Close();
sl@0
    73
sl@0
    74
	User::FreeLogicalDevice(KDebugDriverName);
sl@0
    75
	}
sl@0
    76
sl@0
    77
sl@0
    78
void CDebugServSession::ServiceL(const RMessage2& aMessage)
sl@0
    79
//
sl@0
    80
// Session service handler
sl@0
    81
//
sl@0
    82
	{
sl@0
    83
	TInt res = KErrNone;
sl@0
    84
sl@0
    85
	switch(aMessage.Function())
sl@0
    86
		{
sl@0
    87
		case EDebugServResumeThread:
sl@0
    88
			res = ResumeThread(aMessage);
sl@0
    89
			break;
sl@0
    90
sl@0
    91
		case EDebugServSuspendThread:
sl@0
    92
			res = SuspendThread(aMessage);
sl@0
    93
			break;          
sl@0
    94
sl@0
    95
//		case EDebugServReadProcessInfo:
sl@0
    96
//			res = ReadProcessInfo(aMessage);
sl@0
    97
//			break;        
sl@0
    98
//
sl@0
    99
//		case EDebugServReadThreadInfo:
sl@0
   100
//			res = ReadThreadInfo(aMessage);
sl@0
   101
//			break;
sl@0
   102
sl@0
   103
		case EDebugServReadMemory:
sl@0
   104
			res = ReadMemory(aMessage);
sl@0
   105
			break;        
sl@0
   106
sl@0
   107
		case EDebugServWriteMemory:
sl@0
   108
			res = WriteMemory(aMessage);
sl@0
   109
			break;        
sl@0
   110
sl@0
   111
		default:
sl@0
   112
			User::Leave(KErrNotSupported);
sl@0
   113
			break;
sl@0
   114
		}
sl@0
   115
sl@0
   116
	aMessage.Complete(res);
sl@0
   117
	}
sl@0
   118
sl@0
   119
sl@0
   120
sl@0
   121
TInt CDebugServSession::SuspendThread(const RMessage2& aMessage)
sl@0
   122
//
sl@0
   123
// Session suspend thread
sl@0
   124
//
sl@0
   125
	{
sl@0
   126
	TInt err;
sl@0
   127
sl@0
   128
	err = iKernelDriver.SuspendThread(aMessage.Int0());
sl@0
   129
sl@0
   130
	return err;
sl@0
   131
	}
sl@0
   132
sl@0
   133
TInt CDebugServSession::ResumeThread(const RMessage2& aMessage)
sl@0
   134
//
sl@0
   135
// Server resume thread
sl@0
   136
//
sl@0
   137
	{
sl@0
   138
	TInt err;
sl@0
   139
sl@0
   140
	err = iKernelDriver.ResumeThread(aMessage.Int0());
sl@0
   141
sl@0
   142
	return err;
sl@0
   143
	}
sl@0
   144
sl@0
   145
//TInt CDebugServSession::ReadProcessInfo(const RMessage2& aMessage)
sl@0
   146
////
sl@0
   147
//// Server read process information
sl@0
   148
////
sl@0
   149
//	{
sl@0
   150
//	TInt err;
sl@0
   151
//	TProcessInfo procinfo;
sl@0
   152
//	TMetroTrkTaskInfo processInfo(0);
sl@0
   153
//
sl@0
   154
//	err = iKernelDriver.GetProcessInfo(aMessage.Int0(), processInfo);
sl@0
   155
//
sl@0
   156
//	if (KErrNone == err)
sl@0
   157
//		{
sl@0
   158
//		procinfo.iProcessID = processInfo.iId;
sl@0
   159
//		procinfo.iPriority = processInfo.iPriority;
sl@0
   160
//		procinfo.iName.Copy(processInfo.iName);
sl@0
   161
//
sl@0
   162
//		TPckgBuf<TProcessInfo> p(procinfo);
sl@0
   163
//		aMessage.WriteL(1,p);        
sl@0
   164
//		}
sl@0
   165
//
sl@0
   166
//	return err;
sl@0
   167
//	}
sl@0
   168
//
sl@0
   169
//TInt CDebugServSession::ReadThreadInfo(const RMessage2& aMessage)
sl@0
   170
////
sl@0
   171
//// Server read thread information
sl@0
   172
////
sl@0
   173
//	{
sl@0
   174
//	TInt err;
sl@0
   175
//	TThreadInfo thrdinfo;
sl@0
   176
//	TMetroTrkTaskInfo threadInfo(aMessage.Int1()); // Sets OtherID to the second input parameter in aMessage
sl@0
   177
//
sl@0
   178
//	// aMessage.Int0 is the index into the thread list for the process
sl@0
   179
//	err = iKernelDriver.GetThreadInfo(aMessage.Int0(), threadInfo);	   
sl@0
   180
//
sl@0
   181
//	if (KErrNone == err)
sl@0
   182
//		{
sl@0
   183
//		thrdinfo.iThreadID = threadInfo.iId;
sl@0
   184
//		thrdinfo.iPriority = threadInfo.iPriority;
sl@0
   185
//		thrdinfo.iName.Copy(threadInfo.iName);
sl@0
   186
//		thrdinfo.iOwningProcessID = threadInfo.iOtherId;
sl@0
   187
//
sl@0
   188
//		TPckgBuf<TThreadInfo> p(thrdinfo);
sl@0
   189
//
sl@0
   190
//		// Write out the results to the third argument passed in (pointer to the threadinfo structure)
sl@0
   191
//		aMessage.WriteL(2,p);           
sl@0
   192
//		}
sl@0
   193
//
sl@0
   194
//	return err;
sl@0
   195
//	}
sl@0
   196
sl@0
   197
TInt CDebugServSession::ReadMemory(const RMessage2& aMessage)
sl@0
   198
//
sl@0
   199
// Server read process memory
sl@0
   200
//
sl@0
   201
	{   
sl@0
   202
	TInt err;
sl@0
   203
	TUint32 threadId = aMessage.Int0();
sl@0
   204
	TPckgBuf<TMemoryInfo> pckg = *(TPckgBuf<TMemoryInfo> *)(aMessage.Ptr1());
sl@0
   205
	TMemoryInfo* InputMemoryInfo = &pckg();
sl@0
   206
sl@0
   207
	TPtr8 *ptrtst = InputMemoryInfo->iDataPtr;
sl@0
   208
sl@0
   209
	err = iKernelDriver.ReadMemory(threadId, InputMemoryInfo->iAddress, InputMemoryInfo->iSize, *ptrtst);
sl@0
   210
sl@0
   211
	return err;
sl@0
   212
	}
sl@0
   213
sl@0
   214
TInt CDebugServSession::WriteMemory(const RMessage2& aMessage)
sl@0
   215
//
sl@0
   216
// Server write process memory
sl@0
   217
//
sl@0
   218
	{
sl@0
   219
	TInt err;
sl@0
   220
	TUint32 threadId = aMessage.Int0();
sl@0
   221
	TPckgBuf<TMemoryInfo> pckg = *(TPckgBuf<TMemoryInfo> *)(aMessage.Ptr1());
sl@0
   222
	TMemoryInfo* InputMemoryInfo = &pckg();
sl@0
   223
sl@0
   224
	TPtr8 *ptrtst = InputMemoryInfo->iDataPtr;
sl@0
   225
sl@0
   226
	err = iKernelDriver.WriteMemory(threadId, InputMemoryInfo->iAddress, InputMemoryInfo->iSize, *ptrtst);
sl@0
   227
sl@0
   228
	return err;
sl@0
   229
	}
sl@0
   230
sl@0
   231
sl@0
   232
GLDEF_C TInt CDebugServServer::ThreadFunction(TAny*)
sl@0
   233
//
sl@0
   234
// Server thread function, continues until active scheduler stops
sl@0
   235
//
sl@0
   236
	{
sl@0
   237
	CTrapCleanup* cleanup=CTrapCleanup::New();
sl@0
   238
	if (cleanup == NULL)
sl@0
   239
		{
sl@0
   240
		User::Leave(KErrNoMemory);
sl@0
   241
		}
sl@0
   242
sl@0
   243
	CActiveScheduler *pA=new CActiveScheduler;
sl@0
   244
	CDebugServServer *pS=new CDebugServServer(EPriorityStandard);
sl@0
   245
sl@0
   246
	CActiveScheduler::Install(pA);
sl@0
   247
sl@0
   248
	TInt err = pS->Start(KDebugServerName);
sl@0
   249
	if (err != KErrNone)
sl@0
   250
		{
sl@0
   251
		User::Leave(KErrNone);
sl@0
   252
		}
sl@0
   253
sl@0
   254
	RThread::Rendezvous(KErrNone);
sl@0
   255
sl@0
   256
	CActiveScheduler::Start();
sl@0
   257
sl@0
   258
	delete pS;
sl@0
   259
	delete pA;
sl@0
   260
	delete cleanup;
sl@0
   261
sl@0
   262
	return (KErrNone);
sl@0
   263
	}
sl@0
   264
sl@0
   265
sl@0
   266
sl@0
   267
EXPORT_C TInt StartThread(RThread& aServerThread)
sl@0
   268
//
sl@0
   269
// Start the server thread
sl@0
   270
//
sl@0
   271
	{
sl@0
   272
	TInt res=KErrNone;
sl@0
   273
sl@0
   274
	TFindServer finddebugserver(KDebugServerName);
sl@0
   275
	TFullName name;
sl@0
   276
sl@0
   277
	if (finddebugserver.Next(name) != KErrNone)
sl@0
   278
		{
sl@0
   279
		res = aServerThread.Create( KDebugServerName,
sl@0
   280
									CDebugServServer::ThreadFunction,
sl@0
   281
									KDefaultStackSize,
sl@0
   282
									KDefaultHeapSize,
sl@0
   283
									KDefaultHeapSize,
sl@0
   284
									NULL
sl@0
   285
									);
sl@0
   286
sl@0
   287
		if (res == KErrNone)
sl@0
   288
			{
sl@0
   289
			TRequestStatus rendezvousStatus;
sl@0
   290
sl@0
   291
			aServerThread.SetPriority(EPriorityNormal);
sl@0
   292
			aServerThread.Rendezvous(rendezvousStatus);
sl@0
   293
			aServerThread.Resume();
sl@0
   294
			User::WaitForRequest(rendezvousStatus);
sl@0
   295
			}                                 
sl@0
   296
		else
sl@0
   297
			{
sl@0
   298
			aServerThread.Close();
sl@0
   299
			}
sl@0
   300
		}
sl@0
   301
sl@0
   302
	return res;
sl@0
   303
	}
sl@0
   304
sl@0
   305
sl@0
   306
sl@0
   307
RDebugServSession::RDebugServSession()
sl@0
   308
//
sl@0
   309
// Server session constructor
sl@0
   310
//
sl@0
   311
	{
sl@0
   312
	}
sl@0
   313
sl@0
   314
TInt RDebugServSession::Open()
sl@0
   315
//
sl@0
   316
// Session open
sl@0
   317
//
sl@0
   318
	{
sl@0
   319
	TInt r = StartThread(iServerThread);
sl@0
   320
	if (r == KErrNone)
sl@0
   321
		{
sl@0
   322
		r=CreateSession(KDebugServerName, Version(), KDefaultMessageSlots);
sl@0
   323
		}
sl@0
   324
sl@0
   325
	return r;
sl@0
   326
	}
sl@0
   327
sl@0
   328
sl@0
   329
TVersion RDebugServSession::Version(void) const
sl@0
   330
//
sl@0
   331
// Session version
sl@0
   332
//
sl@0
   333
	{
sl@0
   334
	return (TVersion(KDebugServMajorVersionNumber, KDebugServMinorVersionNumber, KDebugServBuildVersionNumber));
sl@0
   335
	}
sl@0
   336
sl@0
   337
TInt RDebugServSession::SuspendThread(const TInt aThreadID)
sl@0
   338
//
sl@0
   339
// Session suspend thread request
sl@0
   340
//
sl@0
   341
	{
sl@0
   342
	TIpcArgs args(aThreadID);
sl@0
   343
	TInt res;
sl@0
   344
	res = SendReceive(EDebugServSuspendThread, args);
sl@0
   345
sl@0
   346
	return res;
sl@0
   347
	}
sl@0
   348
sl@0
   349
TInt RDebugServSession::ResumeThread(const TInt aThreadID)
sl@0
   350
//
sl@0
   351
// Session resume thread request
sl@0
   352
//
sl@0
   353
	{
sl@0
   354
	TIpcArgs args(aThreadID);
sl@0
   355
	TInt res;
sl@0
   356
	res = SendReceive(EDebugServResumeThread, args);
sl@0
   357
sl@0
   358
	return res;
sl@0
   359
	}
sl@0
   360
sl@0
   361
sl@0
   362
//TInt RDebugServSession::ReadProcessInfo(const TInt aIndex, TProcessInfo* aInfo)
sl@0
   363
////
sl@0
   364
//// Session read process information request
sl@0
   365
////
sl@0
   366
//	{
sl@0
   367
//	TPckgBuf<TProcessInfo> pckg;
sl@0
   368
//	pckg = *aInfo;
sl@0
   369
//
sl@0
   370
//	TIpcArgs args(aIndex, &pckg);
sl@0
   371
//
sl@0
   372
//	TInt res;
sl@0
   373
//
sl@0
   374
//	res = SendReceive(EDebugServReadProcessInfo, args);
sl@0
   375
//
sl@0
   376
//	*aInfo = pckg();
sl@0
   377
//
sl@0
   378
//	return res;
sl@0
   379
//
sl@0
   380
//	}
sl@0
   381
//
sl@0
   382
//TInt RDebugServSession::ReadThreadInfo(const TInt aIndex, const TInt aProc, TThreadInfo* aInfo)
sl@0
   383
////
sl@0
   384
//// Session read thread information request
sl@0
   385
////
sl@0
   386
//	{
sl@0
   387
//	TPckgBuf<TThreadInfo> pckg;
sl@0
   388
//	pckg = *aInfo;
sl@0
   389
//
sl@0
   390
//	TIpcArgs args(aIndex, aProc, &pckg);
sl@0
   391
//
sl@0
   392
//	TInt res;
sl@0
   393
//
sl@0
   394
//	res = SendReceive(EDebugServReadThreadInfo, args);
sl@0
   395
//
sl@0
   396
//	*aInfo = pckg();
sl@0
   397
//
sl@0
   398
//	return res;
sl@0
   399
//
sl@0
   400
//	}
sl@0
   401
sl@0
   402
sl@0
   403
TInt RDebugServSession::ReadMemory(const TUint32 aThreadID, TMemoryInfo* aInfo)
sl@0
   404
//
sl@0
   405
// Session read thread memory request
sl@0
   406
//
sl@0
   407
	{
sl@0
   408
	TPckgBuf<TMemoryInfo> pckg;
sl@0
   409
	pckg = *aInfo;
sl@0
   410
sl@0
   411
	TIpcArgs args(aThreadID, &pckg);
sl@0
   412
sl@0
   413
	TInt res;
sl@0
   414
sl@0
   415
	res = SendReceive(EDebugServReadMemory, args);
sl@0
   416
sl@0
   417
	*aInfo = pckg();
sl@0
   418
sl@0
   419
	return res;
sl@0
   420
sl@0
   421
	}
sl@0
   422
sl@0
   423
sl@0
   424
TInt RDebugServSession::WriteMemory(const TUint32 aThreadID, TMemoryInfo* aInfo)
sl@0
   425
//
sl@0
   426
// Session write thread memory request
sl@0
   427
//
sl@0
   428
	{
sl@0
   429
	TPckgBuf<TMemoryInfo> pckg;
sl@0
   430
	pckg = *aInfo;
sl@0
   431
sl@0
   432
	TIpcArgs args(aThreadID, &pckg);
sl@0
   433
sl@0
   434
	TInt res;
sl@0
   435
sl@0
   436
	res = SendReceive(EDebugServWriteMemory, args);
sl@0
   437
sl@0
   438
	return res;
sl@0
   439
	}
sl@0
   440
sl@0
   441
sl@0
   442
sl@0
   443
TInt RDebugServSession::Close()
sl@0
   444
//
sl@0
   445
// Session close the session and thread
sl@0
   446
//
sl@0
   447
	{
sl@0
   448
	RSessionBase::Close();
sl@0
   449
	iServerThread.Close();
sl@0
   450
sl@0
   451
	return KErrNone;
sl@0
   452
	}
sl@0
   453