|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "MemSpyDriverSuspensionManager.h" |
|
19 |
|
20 // System includes |
|
21 #include <memspy/driver/memspydriverobjectsshared.h> |
|
22 |
|
23 // Shared includes |
|
24 #include "MemSpyDriverUtils.h" |
|
25 #include "MemSpyDriverDevice.h" |
|
26 #include "MemSpyDriverOSAdaption.h" |
|
27 |
|
28 // Constants |
|
29 const TInt KSuspendResumeCount = 1; |
|
30 |
|
31 |
|
32 |
|
33 DMemSpySuspensionManager::DMemSpySuspensionManager( DMemSpyDriverDevice& aDevice ) |
|
34 : iDevice( aDevice ) |
|
35 { |
|
36 } |
|
37 |
|
38 |
|
39 DMemSpySuspensionManager::~DMemSpySuspensionManager() |
|
40 { |
|
41 TRACE( Kern::Printf("DMemSpySuspensionManager::~DMemSpySuspensionManager() - START")); |
|
42 |
|
43 if ( iAlreadySuspended ) |
|
44 { |
|
45 TRACE( Kern::Printf("DMemSpySuspensionManager::~DMemSpySuspensionManager() - resuming threads in process with id: %d", iSuspendedProcessId )); |
|
46 DoResumeAllThreadsInProcess( iSuspendedProcessId ); |
|
47 } |
|
48 if ( iTempObj != NULL ) |
|
49 { |
|
50 Kern::SafeClose( iTempObj, NULL ); |
|
51 } |
|
52 |
|
53 TRACE( Kern::Printf("DMemSpySuspensionManager::~DMemSpySuspensionManager() - END")); |
|
54 } |
|
55 |
|
56 |
|
57 TInt DMemSpySuspensionManager::Construct() |
|
58 { |
|
59 return KErrNone; |
|
60 } |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 TBool DMemSpySuspensionManager::IsSuspended( TUint aPid ) const |
|
73 { |
|
74 TBool ret = iAlreadySuspended; |
|
75 // |
|
76 if ( ret ) |
|
77 { |
|
78 ret = ( aPid == iSuspendedProcessId ); |
|
79 } |
|
80 // |
|
81 TRACE( Kern::Printf("DMemSpySuspensionManager::IsSuspended() - iAlreadySuspended: %d, iSuspendedProcessId: %d, aPid: %d, ret: %d", iAlreadySuspended, iSuspendedProcessId, aPid, ret ) ); |
|
82 return ret; |
|
83 } |
|
84 |
|
85 |
|
86 TBool DMemSpySuspensionManager::IsSuspended( DThread& aThread ) const |
|
87 { |
|
88 TBool ret = EFalse; |
|
89 // |
|
90 const TUint tid = iDevice.OSAdaption().DThread().GetId( aThread ); |
|
91 DProcess* process = iDevice.OSAdaption().DThread().GetOwningProcess( aThread ); |
|
92 TRACE( Kern::Printf("DMemSpySuspensionManager::IsSuspended() - START - iSuspendedProcessId: %d, aThread: (%d) %O, process: 0x%08x", iSuspendedProcessId, tid, &aThread, process ) ); |
|
93 // |
|
94 if ( process ) |
|
95 { |
|
96 ret = IsSuspended( *process ); |
|
97 } |
|
98 // |
|
99 TRACE( Kern::Printf("DMemSpySuspensionManager::IsSuspended() - END - ret: %d", ret ) ); |
|
100 return ret; |
|
101 } |
|
102 |
|
103 |
|
104 TBool DMemSpySuspensionManager::IsSuspended( DProcess& aProcess ) const |
|
105 { |
|
106 const TUint pid = iDevice.OSAdaption().DProcess().GetId( aProcess ); |
|
107 TRACE( Kern::Printf("DMemSpySuspensionManager::IsSuspended() - START - iSuspendedProcessId: %d, aProcess: (%d / %d) %O", iSuspendedProcessId, pid, aProcess.iId, &aProcess ) ); |
|
108 // |
|
109 const TBool ret = IsSuspended( pid ); |
|
110 // |
|
111 TRACE( Kern::Printf("DMemSpySuspensionManager::IsSuspended() - END - ret: %d", ret ) ); |
|
112 return ret; |
|
113 } |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 TInt DMemSpySuspensionManager::SuspendAllThreadsInProcess( TUint aPid, DThread& aClientThread ) |
|
131 { |
|
132 // Suspend all threads in the process |
|
133 TRACE( Kern::Printf("DMemSpySuspensionManager::SuspendAllThreadsInProcess() - START - id: %8d, iSuspendedProcessId: %8d, iAlreadySuspended: %d", aPid, iSuspendedProcessId, iAlreadySuspended )); |
|
134 if ( iAlreadySuspended && aPid != iSuspendedProcessId ) |
|
135 { |
|
136 Kern::Printf("DMemSpySuspensionManager::SuspendAllThreadsInProcess() - END - trying to suspend multiple processes!"); |
|
137 MemSpyDriverUtils::PanicThread( aClientThread, EPanicAttemptingToSuspendMultipleProcesses ); |
|
138 return KErrNone; |
|
139 } |
|
140 else if ( IsProcessTheClientThread( aPid, aClientThread ) ) |
|
141 { |
|
142 Kern::Printf("DMemSpySuspensionManager::SuspendAllThreadsInProcess() - END - trying to suspend client thread! - request ignored"); |
|
143 return KErrLocked; |
|
144 } |
|
145 |
|
146 TInt r = KErrNone; |
|
147 TRACE( Kern::Printf("DMemSpySuspensionManager::SuspendAllThreadsInProcess - iAlreadySuspended: %d", iAlreadySuspended)); |
|
148 if ( !iAlreadySuspended ) |
|
149 { |
|
150 r = DoSuspendAllThreadsInProcess( aPid, &aClientThread ); |
|
151 if (r != KErrNone) |
|
152 { |
|
153 TRACE( Kern::Printf("DMemSpySuspensionManager::SuspendAllThreadsInProcess() - END - process not found") ); |
|
154 return r; |
|
155 } |
|
156 |
|
157 // To ensure we clean up in case of user-side problem... |
|
158 iSuspendedProcessId = aPid; |
|
159 iAlreadySuspended = ETrue; |
|
160 iSuspendCount = 1; |
|
161 } |
|
162 else |
|
163 { |
|
164 // Just increment the count |
|
165 ++iSuspendCount; |
|
166 } |
|
167 |
|
168 TRACE( Kern::Printf("DMemSpySuspensionManager::SuspendAllThreadsInProcess() - END - iSuspendCount: %d, iSuspendedProcessId: %d", iSuspendCount, iSuspendedProcessId )); |
|
169 return iSuspendCount; |
|
170 } |
|
171 |
|
172 |
|
173 TInt DMemSpySuspensionManager::ResumeAllThreadsInProcess( TUint aPid, DThread& aClientThread ) |
|
174 { |
|
175 TRACE( Kern::Printf("DMemSpySuspensionManager::ResumeAllThreadsInProcess() - START - id: %8d, iSuspendedProcessId: %8d, iAlreadySuspended: %d", aPid, iSuspendedProcessId, iAlreadySuspended)); |
|
176 if ( !iAlreadySuspended ) |
|
177 { |
|
178 // Nothing suspended - don't panic |
|
179 TRACE( Kern::Printf("DMemSpySuspensionManager::ResumeAllThreadsInProcess() - END - nothing suspended, ignoring client request")); |
|
180 return KErrNone; |
|
181 } |
|
182 else if ( iAlreadySuspended && aPid != iSuspendedProcessId ) |
|
183 { |
|
184 Kern::Printf("DMemSpySuspensionManager::ResumeAllThreadsInProcess() - END - trying to resume incorrect process!"); |
|
185 MemSpyDriverUtils::PanicThread( aClientThread, EPanicAttemptingToResumeNonSuspendedProcess ); |
|
186 return KErrNone; |
|
187 } |
|
188 else if ( IsProcessTheClientThread( aPid, aClientThread ) ) |
|
189 { |
|
190 Kern::Printf("DMemSpySuspensionManager::ResumeAllThreadsInProcess() - END - trying to resume client thread! - request ignored"); |
|
191 return KErrLocked; |
|
192 } |
|
193 |
|
194 TRACE( Kern::Printf("DMemSpySuspensionManager::ResumeAllThreadsInProcess")); |
|
195 |
|
196 TInt r = KErrNone; |
|
197 if ( --iSuspendCount <= 0 ) |
|
198 { |
|
199 r = DoResumeAllThreadsInProcess( aPid, &aClientThread ); |
|
200 if (r != KErrNone) |
|
201 { |
|
202 Kern::Printf("DMemSpySuspensionManager::ResumeAllThreadsInProcess() - END - process not found"); |
|
203 return r; |
|
204 } |
|
205 |
|
206 // No longer need to clean up |
|
207 iAlreadySuspended = EFalse; |
|
208 iSuspendedProcessId = 0; |
|
209 iSuspendCount = 0; |
|
210 } |
|
211 else |
|
212 { |
|
213 // No action needed - we've already decremented the counter |
|
214 } |
|
215 |
|
216 TRACE( Kern::Printf("DMemSpySuspensionManager::ResumeAllThreadsInProcess() - END - iSuspendCount: %d", iSuspendCount)); |
|
217 return iSuspendCount; |
|
218 } |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 TInt DMemSpySuspensionManager::DoSuspendAllThreadsInProcess( TUint aPid, DThread* /*aClientThread*/ ) |
|
248 { |
|
249 TRACE( Kern::Printf("DMemSpySuspensionManager::DoSuspendAllThreadsInProcess() - START - aPid: %d", aPid)); |
|
250 TInt r = OpenTempObject( aPid, EProcess ); |
|
251 if (r == KErrNone) |
|
252 { |
|
253 DProcess* process = (DProcess*) iTempObj; |
|
254 TFullName processName; |
|
255 process->FullName( processName ); |
|
256 processName.Append('*'); |
|
257 |
|
258 DObjectCon* container = Kern::Containers()[EThread]; |
|
259 NKern::ThreadEnterCS(); |
|
260 container->Wait(); |
|
261 // |
|
262 TFullName result; |
|
263 #ifdef MCL_FIND_HANDLES |
|
264 TFindHandle findHandle; |
|
265 #else |
|
266 TInt findHandle = 0; |
|
267 #endif |
|
268 r = container->FindByFullName( findHandle, processName, result ); |
|
269 while( r == KErrNone ) |
|
270 { |
|
271 DThread* thread = (DThread*) container->At( findHandle ); |
|
272 if ( thread ) |
|
273 { |
|
274 const TUint tid = iDevice.OSAdaption().DThread().GetId( *thread ); |
|
275 TRACE( Kern::Printf("DMemSpySuspensionManager::DoSuspendAllThreadsInProcess - suspending thread: (%d), %O", tid, thread )); |
|
276 // |
|
277 Kern::ThreadSuspend( *thread, KSuspendResumeCount ); |
|
278 } |
|
279 // |
|
280 r = container->FindByFullName( findHandle, processName, result ); |
|
281 } |
|
282 // |
|
283 container->Signal(); |
|
284 NKern::ThreadLeaveCS(); |
|
285 |
|
286 CloseTempObject(); |
|
287 r = KErrNone; |
|
288 } |
|
289 |
|
290 TRACE( Kern::Printf("DMemSpySuspensionManager::DoSuspendAllThreadsInProcess() - END - ret: %d", r)); |
|
291 return r; |
|
292 } |
|
293 |
|
294 |
|
295 TInt DMemSpySuspensionManager::DoResumeAllThreadsInProcess( TUint aPid, DThread* aClientThread ) |
|
296 { |
|
297 TRACE( Kern::Printf("DMemSpySuspensionManager::DoResumeAllThreadsInProcess() - START - aPid: %d", aPid)); |
|
298 TInt r = OpenTempObject( aPid, EProcess ); |
|
299 if (r == KErrNone) |
|
300 { |
|
301 // Check that this process is suspended |
|
302 DProcess* process = (DProcess*) iTempObj; |
|
303 const TUint pid = iDevice.OSAdaption().DProcess().GetId( *process ); |
|
304 if ( !aClientThread || ( aClientThread && CheckProcessSuspended( pid, *aClientThread ) ) ) |
|
305 { |
|
306 // Resume all threads in the process |
|
307 TFullName processName; |
|
308 process->FullName( processName ); |
|
309 processName.Append('*'); |
|
310 |
|
311 DObjectCon* container = Kern::Containers()[EThread]; |
|
312 NKern::ThreadEnterCS(); |
|
313 container->Wait(); |
|
314 // |
|
315 TFullName result; |
|
316 #ifdef MCL_FIND_HANDLES |
|
317 TFindHandle findHandle; |
|
318 #else |
|
319 TInt findHandle = 0; |
|
320 #endif |
|
321 r = container->FindByFullName( findHandle, processName, result ); |
|
322 while( r == KErrNone ) |
|
323 { |
|
324 DThread* thread = (DThread*) container->At( findHandle ); |
|
325 TRACE( Kern::Printf("DMemSpySuspensionManager::DoResumeAllThreadsInProcess - resuming thread: %lS", &result)); |
|
326 // |
|
327 Kern::ThreadResume(*thread); |
|
328 r = container->FindByFullName( findHandle, processName, result ); |
|
329 } |
|
330 // |
|
331 container->Signal(); |
|
332 NKern::ThreadLeaveCS(); |
|
333 |
|
334 r = KErrNone; |
|
335 } |
|
336 else |
|
337 { |
|
338 TRACE( Kern::Printf("DMemSpySuspensionManager::DoResumeAllThreadsInProcess - parent process not suspended => KErrAccessDenied")); |
|
339 r = KErrAccessDenied; |
|
340 } |
|
341 |
|
342 CloseTempObject(); |
|
343 } |
|
344 |
|
345 TRACE( Kern::Printf("DMemSpySuspensionManager::DoResumeAllThreadsInProcess() - END - ret: %d", r)); |
|
346 return r; |
|
347 } |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 TBool DMemSpySuspensionManager::IsProcessTheClientThread( TUint aPid, DThread& aClientThread ) const |
|
366 { |
|
367 TBool ret = EFalse; |
|
368 DProcess& clientProcess = *iDevice.OSAdaption().DThread().GetOwningProcess( aClientThread ); |
|
369 const TUint pid = iDevice.OSAdaption().DProcess().GetId( clientProcess ); |
|
370 if ( pid == aPid ) |
|
371 { |
|
372 ret = ETrue; |
|
373 } |
|
374 // |
|
375 return ret; |
|
376 } |
|
377 |
|
378 |
|
379 TBool DMemSpySuspensionManager::CheckProcessSuspended( TUint aExpectedPid, DThread& aClientThread ) const |
|
380 { |
|
381 TBool suspended = ETrue; |
|
382 // |
|
383 if ( !iAlreadySuspended ) |
|
384 { |
|
385 MemSpyDriverUtils::PanicThread( aClientThread, EPanicThreadsInProcessNotSuspended ); |
|
386 } |
|
387 else if ( aExpectedPid != iSuspendedProcessId ) |
|
388 { |
|
389 MemSpyDriverUtils::PanicThread( aClientThread, EPanicWrongProcessSuspended ); |
|
390 } |
|
391 // |
|
392 return suspended; |
|
393 } |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 |
|
401 |
|
402 |
|
403 |
|
404 |
|
405 |
|
406 |
|
407 |
|
408 |
|
409 |
|
410 |
|
411 |
|
412 |
|
413 TInt DMemSpySuspensionManager::OpenTempObject(TUint aId, TObjectType aType) |
|
414 { |
|
415 __ASSERT_DEBUG( aType == EProcess || aType == EThread, MemSpyDriverUtils::Fault( __LINE__ ) ); |
|
416 __ASSERT_DEBUG( !iTempObj, MemSpyDriverUtils::Fault( __LINE__ ) ); |
|
417 |
|
418 TRACE( Kern::Printf("DMemSpySuspensionManager::OpenTempObject() - START - aId: %d, aType: %d", aId, aType )); |
|
419 DObjectCon* pC = Kern::Containers()[aType]; |
|
420 NKern::ThreadEnterCS(); |
|
421 pC->Wait(); |
|
422 iTempObj = (aType == EProcess) ? (DObject*)Kern::ProcessFromId(aId) : (DObject*)Kern::ThreadFromId(aId); |
|
423 TRACE( Kern::Printf("DMemSpySuspensionManager::OpenTempObject() - kernel obj from id returned: 0x%08x %O", iTempObj, iTempObj )); |
|
424 // |
|
425 TBool openedOkay = EFalse; |
|
426 TInt r = KErrNone; |
|
427 // |
|
428 if ( iTempObj ) |
|
429 { |
|
430 r = iTempObj->Open(); |
|
431 TRACE( Kern::Printf("DMemSpySuspensionManager::OpenTempObject() - open returned: %d", r )); |
|
432 // |
|
433 if ( r == KErrNone ) |
|
434 { |
|
435 openedOkay = ETrue; |
|
436 if ( aType == EProcess ) |
|
437 { |
|
438 // Check the process is still alive |
|
439 DProcess* process = (DProcess*) iTempObj; |
|
440 const TExitType exitType = iDevice.OSAdaption().DProcess().GetExitType( *process ); |
|
441 if ( exitType != EExitPending ) |
|
442 { |
|
443 r = KErrDied; |
|
444 TRACE( Kern::Printf("DMemSpySuspensionManager::OpenTempObject() - PROCESS IS DEAD!" )); |
|
445 } |
|
446 } |
|
447 else if (aType == EThread ) |
|
448 { |
|
449 // Check the thread is still alive |
|
450 DThread* thread = (DThread*) iTempObj; |
|
451 const TExitType exitType = iDevice.OSAdaption().DThread().GetExitType( *thread ); |
|
452 if ( exitType != EExitPending ) |
|
453 { |
|
454 r = KErrDied; |
|
455 TRACE( Kern::Printf("DMemSpySuspensionManager::OpenTempObject() - THREAD IS DEAD!" )); |
|
456 } |
|
457 } |
|
458 } |
|
459 |
|
460 } |
|
461 else if ( ! iTempObj ) |
|
462 { |
|
463 r = KErrNotFound; |
|
464 TRACE( Kern::Printf("DMemSpySuspensionManager::OpenTempObject() - ENTITY NOT FOUND!" )); |
|
465 } |
|
466 // |
|
467 pC->Signal(); |
|
468 TRACE( Kern::Printf("DMemSpySuspensionManager::OpenTempObject() - signalled container..." )); |
|
469 NKern::ThreadLeaveCS(); |
|
470 TRACE( Kern::Printf("DMemSpySuspensionManager::OpenTempObject() - left CS..." )); |
|
471 // |
|
472 if ( r != KErrNone && iTempObj ) |
|
473 { |
|
474 TRACE( Kern::Printf("DMemSpySuspensionManager::OpenTempObject() - ERROR CASE - closing temp object (r: %d, openedOkay: %d)...", r, openedOkay )); |
|
475 // |
|
476 if ( openedOkay ) |
|
477 { |
|
478 CloseTempObject(); |
|
479 } |
|
480 else |
|
481 { |
|
482 NKern::SafeSwap( NULL, (TAny*&) iTempObj ); |
|
483 } |
|
484 // |
|
485 TRACE( Kern::Printf("DMemSpySuspensionManager::OpenTempObject() - ERROR CASE - closed temp object" )); |
|
486 } |
|
487 // |
|
488 TRACE( Kern::Printf("DMemSpySuspensionManager::OpenTempObject() - END - r: %d", r )); |
|
489 return r; |
|
490 } |
|
491 |
|
492 |
|
493 void DMemSpySuspensionManager::CloseTempObject() |
|
494 { |
|
495 TRACE( Kern::Printf("DMemSpySuspensionManager::CloseTempObject() - START - iTempObj: 0x%08x %O", iTempObj, iTempObj )); |
|
496 |
|
497 __ASSERT_DEBUG( iTempObj, MemSpyDriverUtils::Fault( __LINE__ ) ); |
|
498 if ( iTempObj ) |
|
499 { |
|
500 NKern::ThreadEnterCS(); |
|
501 Kern::SafeClose( iTempObj, NULL ); |
|
502 NKern::ThreadLeaveCS(); |
|
503 } |
|
504 |
|
505 TRACE( Kern::Printf("DMemSpySuspensionManager::CloseTempObject() - END" )); |
|
506 } |
|
507 |
|
508 |