1 // Copyright (c) 2006-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". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // Purpose: The DProcessTracker object tracks which processes are being |
|
15 // debugged. The DProcessTracker class uses a DTargetProcess object for |
|
16 // each process being debugged. |
|
17 // Note: Although TheDProcessTracker object is a global, it will be unique |
|
18 // as only the Debug Security Server can load and use rm_debug.ldd. |
|
19 // |
|
20 // |
|
21 |
|
22 #include <e32def.h> |
|
23 #include <e32def_private.h> |
|
24 #include <e32cmn.h> |
|
25 #include <e32cmn_private.h> |
|
26 #include <kernel/kernel.h> |
|
27 #include <kernel/kern_priv.h> |
|
28 |
|
29 #include <rm_debug_api.h> |
|
30 #include "debug_logging.h" |
|
31 #include "d_process_tracker.h" |
|
32 #include "debug_utils.h" |
|
33 |
|
34 // Global Run-mode debugged process tracking object |
|
35 DProcessTracker TheDProcessTracker; |
|
36 |
|
37 // ctor |
|
38 DProcessTracker::DProcessTracker() |
|
39 { |
|
40 } |
|
41 |
|
42 /** |
|
43 * dtor |
|
44 * @internalTechnology |
|
45 */ |
|
46 DProcessTracker::~DProcessTracker() |
|
47 { |
|
48 // Forget about all the iProcesses |
|
49 iProcesses.ResetAndDestroy(); |
|
50 } |
|
51 |
|
52 /** |
|
53 * @internalTechnology |
|
54 * |
|
55 * Creates and stores an internal mapping of debug agent to debugged process. |
|
56 * Note that an individual process may be mapped to a number of debug agents. |
|
57 * |
|
58 * @param aProcessName - The fullly qualified path of the debugged process. E.g. z:\sys\bin\hello_world.exe |
|
59 * @param aAgentId - The process id of the debug agent which is attaching to aProcessName, as returned by RProcess.Id() |
|
60 * @return KErrNone if there are no errors. KErrArgument if the processname is too long/short for a valid filepath. |
|
61 * KErrNoMemory if there is insufficient memory. |
|
62 */ |
|
63 TInt DProcessTracker::AttachProcess(const TDesC8& aProcessName,TUint64 aAgentId) |
|
64 { |
|
65 LOG_MSG("DProcessTracker::AttachProcess()"); |
|
66 |
|
67 // Valid ProcessName? |
|
68 if (aProcessName.Length() < 1 || aProcessName.Length() >= KMaxPath) |
|
69 { |
|
70 return KErrArgument; |
|
71 } |
|
72 |
|
73 // Create an DTargetProcess to store |
|
74 DTargetProcess* tmpProcess = new DTargetProcess; |
|
75 if (tmpProcess == 0) |
|
76 { |
|
77 return KErrNoMemory; |
|
78 } |
|
79 LOG_MSG2(" AttachProcess: < new DTargetProcess=0x%08x", tmpProcess ); |
|
80 |
|
81 // Set the name |
|
82 TInt err = KErrNone; |
|
83 err = tmpProcess->SetProcessName(aProcessName); |
|
84 if (err != KErrNone) |
|
85 { |
|
86 LOG_MSG2(" AttachProcess: < SetProcessName returned %d", err ); |
|
87 return err; |
|
88 } |
|
89 |
|
90 // Is this process being debugged (ie already attached?) |
|
91 TInt index; |
|
92 TBool found = EFalse; |
|
93 |
|
94 TInt numberOfProcesses = iProcesses.Count(); |
|
95 for(index=0; index<numberOfProcesses; index++) |
|
96 { |
|
97 const TPtr8& tmpPtr8(iProcesses[index]->ProcessName() ); |
|
98 |
|
99 if ( tmpPtr8.CompareF(aProcessName) == 0) |
|
100 { |
|
101 LOG_MSG3(" Proc count=%d, found proc in iProcesses at %d. Count=%d", |
|
102 index, iProcesses.Count() ); |
|
103 found = ETrue; |
|
104 break; |
|
105 } |
|
106 } |
|
107 |
|
108 if (found) |
|
109 { |
|
110 // Yes, it is being debugged |
|
111 |
|
112 // Add the agent to the list of agents for this process |
|
113 LOG_MSG3(" > AddAgent(agent id %d) to existing iProcesses[%d]", I64LOW(aAgentId), index ); |
|
114 |
|
115 iProcesses[index]->AddAgent(aAgentId); |
|
116 |
|
117 return KErrNone; |
|
118 } |
|
119 else |
|
120 { |
|
121 // No, it is not being debugged |
|
122 |
|
123 // Add the agent to the list of agents for this process |
|
124 LOG_MSG2(" > AddAgent(agent %d) to new proc at index 0", I64LOW(aAgentId) ); |
|
125 |
|
126 tmpProcess->AddAgent(aAgentId); |
|
127 |
|
128 // Add the process to the list of processes being debugged |
|
129 return iProcesses.Insert(tmpProcess,0); |
|
130 } |
|
131 } |
|
132 |
|
133 /** |
|
134 * @internalTechnology |
|
135 * |
|
136 * Removes a previously created mapping between a debug agent and a debugged process, |
|
137 * as created by AttachProcess. |
|
138 * |
|
139 * @param aProcessName - The fully qualified path of the debugged process. E.g. z:\sys\bin\hello_world.exe |
|
140 * @param aAgentId - The process id of the debug agent which is attaching to aProcessName, as returned by RProcess.Id() |
|
141 * @return KErrNone if there are no problems. KErrArgument if the processname is too long/short for a valid filepath. |
|
142 * KErrNotFound if the mapping does not exist (and therefore cannot be removed). |
|
143 */ |
|
144 TInt DProcessTracker::DetachProcess(const TDesC8& aProcessName, TUint64 aAgentId) |
|
145 { |
|
146 // Valid ProcessName? |
|
147 if (aProcessName.Length() < 1 || aProcessName.Length() >= KMaxPath) |
|
148 { |
|
149 return KErrArgument; |
|
150 }; |
|
151 |
|
152 // Are we debugging this process? |
|
153 TInt i; |
|
154 TBool found = EFalse; |
|
155 DTargetProcess* foundProcess = 0; |
|
156 |
|
157 TInt numberOfProcesses = iProcesses.Count(); |
|
158 for(i=0; i<numberOfProcesses; i++) |
|
159 { |
|
160 foundProcess = iProcesses[i]; |
|
161 |
|
162 const TPtr8& tmpPtr8( foundProcess->ProcessName() ); |
|
163 |
|
164 if ( tmpPtr8.CompareF(aProcessName) == 0) |
|
165 { |
|
166 found = ETrue; |
|
167 break; |
|
168 } |
|
169 } |
|
170 |
|
171 if (found == EFalse) |
|
172 { |
|
173 return KErrNotFound; |
|
174 } |
|
175 |
|
176 // remove the agent from the process |
|
177 iProcesses[i]->RemoveAgent(aAgentId); |
|
178 |
|
179 // Found it, are there any more attached agents, or suspended threads in the process? |
|
180 if ((iProcesses[i]->AgentCount() == 0) && !iProcesses[i]->HasSuspendedThreads() ) |
|
181 { |
|
182 // Delete the process as no more agents are still attached |
|
183 delete iProcesses[i]; |
|
184 |
|
185 // Remove the now obsolete pointer from our array. |
|
186 iProcesses.Remove(i); |
|
187 } |
|
188 |
|
189 return KErrNone; |
|
190 } |
|
191 |
|
192 /** |
|
193 * @internalTechnology |
|
194 * |
|
195 * Detachs a debug agent from every process being debugged. Used when a debug agent is being detached |
|
196 * from the debug security server and has not supplied a specific process name from which to detach. |
|
197 */ |
|
198 TInt DProcessTracker::DetachAgent(const TUint64 aAgentId) |
|
199 { |
|
200 // Remove this agent from all the processes being tracked. |
|
201 TInt numberOfProcesses = iProcesses.Count(); |
|
202 for(TInt i=0; i<numberOfProcesses; i++) |
|
203 { |
|
204 // remove the agent from the process (we don't care about the return code) |
|
205 iProcesses[i]->RemoveAgent(aAgentId); |
|
206 } |
|
207 |
|
208 // Increment down through the array as we then don't have to worry about |
|
209 // missing entries which have been shifted after deletes. |
|
210 // The initial value of i correspnds to the index of the final element |
|
211 // in the array. |
|
212 for(TInt i = iProcesses.Count()-1; i>=0; i--) |
|
213 { |
|
214 if (iProcesses[i]->AgentCount() == 0) |
|
215 { |
|
216 // No agents remain for this process. Delete the |
|
217 // process object and remove the pointer from the array |
|
218 delete iProcesses[i]; |
|
219 iProcesses.Remove(i); |
|
220 } |
|
221 } |
|
222 return KErrNone; |
|
223 } |
|
224 |
|
225 /** |
|
226 * @internalTechnology |
|
227 * |
|
228 * Returns a pointer to a DTargetProcess object representing the mapping of a debugged process |
|
229 * with all the relevant debug agents interested in that process, as determined |
|
230 * by AttachProcess. |
|
231 * |
|
232 * @param aProcessName - The fully qualified path of the debugged process. E.g. z:\sys\bin\hello_world.exe |
|
233 * @return DTargetProcess* pointer to an object representing the internal mapping of a process to all associated |
|
234 * debug agents. Returns 0 if the mapping cannot be found or the aProcessName is invalid. |
|
235 */ |
|
236 DTargetProcess* DProcessTracker::FindProcess(const TDesC8& aProcessName) |
|
237 { |
|
238 // Valid ProcessName? |
|
239 if (aProcessName.Length() < 1 || aProcessName.Length() >= KMaxPath) |
|
240 { |
|
241 return 0; // not found |
|
242 } |
|
243 |
|
244 // Can we find this in the array? |
|
245 TInt i; |
|
246 TBool found = EFalse; |
|
247 DTargetProcess* foundProcess = 0; |
|
248 |
|
249 TInt numberOfProcesses = iProcesses.Count(); |
|
250 for(i=0; i<numberOfProcesses; i++) |
|
251 { |
|
252 foundProcess = iProcesses[i]; |
|
253 |
|
254 const TPtr8& tmpPtr8( foundProcess->ProcessName() ); |
|
255 |
|
256 if ( tmpPtr8.CompareF(aProcessName) == 0) |
|
257 { |
|
258 found = ETrue; |
|
259 break; |
|
260 } |
|
261 } |
|
262 |
|
263 if (found == EFalse) |
|
264 { |
|
265 LOG_EVENT_MSG("DProcessTracker::FindProcess, not found" ); |
|
266 return 0; // not found |
|
267 } |
|
268 |
|
269 return foundProcess; |
|
270 } |
|
271 |
|
272 /** |
|
273 * @internalTechnology |
|
274 * |
|
275 * Returns a pointer to a DTargetProcess object representing the mapping of a debugged process |
|
276 * with all the relevant debug agents interested in that process, as determined |
|
277 * by AttachProcess. |
|
278 * |
|
279 * Note: This does not attempt an exact match, because the AddProcess event does not provide |
|
280 * a fully-qualified path, it provides something like [t_rmdebug_security0.exe]. |
|
281 * |
|
282 * So for the purposes of dealing with this event, we need a "fuzzier" match which does not use the complete |
|
283 * path. |
|
284 * |
|
285 * @param aProcessName - The fully qualified path of the debugged process. E.g. z:\sys\bin\hello_world.exe |
|
286 * @return DTargetProcess* pointer to an object representing the internal mapping of a process to all associated |
|
287 * debug agents. Returns 0 if the mapping cannot be found or the aProcessName is invalid. |
|
288 */ |
|
289 DTargetProcess* DProcessTracker::FuzzyFindProcess(const TDesC8& aProcessName) |
|
290 { |
|
291 // Valid ProcessName? |
|
292 if (aProcessName.Length() < 1 || aProcessName.Length() >= KMaxPath) |
|
293 { |
|
294 return 0; // not found |
|
295 } |
|
296 |
|
297 // Can we find this in the array? |
|
298 TBool found = EFalse; |
|
299 DTargetProcess* foundProcess = 0; |
|
300 const TChar KBackSlash('\\'); |
|
301 |
|
302 TInt numberOfProcesses = iProcesses.Count(); |
|
303 for(TInt i=0; i < numberOfProcesses; i++) |
|
304 { |
|
305 foundProcess = iProcesses[i]; |
|
306 |
|
307 TInt procListBackSlash = foundProcess->ProcessName().LocateReverse( KBackSlash ); |
|
308 if( procListBackSlash == KErrNotFound ) |
|
309 { |
|
310 procListBackSlash = 0; |
|
311 } |
|
312 else |
|
313 { |
|
314 //Now move to the char after the backlash |
|
315 procListBackSlash++; |
|
316 } |
|
317 |
|
318 TInt eventBackSlash = aProcessName.LocateReverse( KBackSlash ); |
|
319 if( eventBackSlash == KErrNotFound ) |
|
320 { |
|
321 eventBackSlash = 0; |
|
322 } |
|
323 else |
|
324 { |
|
325 //Now move to the char after the backlash |
|
326 eventBackSlash++; |
|
327 } |
|
328 |
|
329 if( ( procListBackSlash == 0 ) && ( eventBackSlash == 0 ) ) |
|
330 { |
|
331 //There were no backslashes on either name, so no point in continuing |
|
332 break; |
|
333 } |
|
334 |
|
335 TPtrC8 eventCleanName( aProcessName.Mid( eventBackSlash ) ); |
|
336 TPtrC8 procListCleanName( foundProcess->ProcessName().Mid( procListBackSlash ) ); |
|
337 |
|
338 if ( eventCleanName.CompareF( procListCleanName ) == 0 ) |
|
339 { |
|
340 LOG_MSG2("DProcessTracker::FuzzyFindProcess() found a match : process list[%d]", i ); |
|
341 found = ETrue; |
|
342 break; |
|
343 } |
|
344 } |
|
345 |
|
346 if (found == EFalse) |
|
347 { |
|
348 return 0; // not found |
|
349 } |
|
350 |
|
351 return foundProcess; |
|
352 } |
|
353 |
|
354 TBool DProcessTracker::CheckSuspended(DThread* aTargetThread) const |
|
355 { |
|
356 //get the file name and return if NULL |
|
357 HBuf* name = GetFileName(aTargetThread); |
|
358 if(!name) |
|
359 { |
|
360 return EFalse; |
|
361 } |
|
362 |
|
363 //iterate through the processes trying to match the name, and check suspended if found |
|
364 TInt numberOfProcesses = iProcesses.Count(); |
|
365 for(TInt i=0; i < numberOfProcesses; i++) |
|
366 { |
|
367 if(iProcesses[i]->ProcessName().CompareF(*name) == 0) |
|
368 { |
|
369 return iProcesses[i]->CheckSuspended(aTargetThread); |
|
370 } |
|
371 } |
|
372 |
|
373 //couldn't find the process so return EFalse |
|
374 return EFalse; |
|
375 } |
|
376 |
|
377 TBool DProcessTracker::CheckSuspended(const TUint64 aTargetThreadId) const |
|
378 { |
|
379 //get a handle to the thread and return false if it's NULL |
|
380 DThread* thread = DebugUtils::OpenThreadHandle(aTargetThreadId); |
|
381 if(!thread) |
|
382 { |
|
383 return EFalse; |
|
384 } |
|
385 |
|
386 //check if the thread's suspended and then close the thread handle and return |
|
387 TBool suspended = CheckSuspended(thread); |
|
388 thread->Close(NULL); |
|
389 return suspended; |
|
390 } |
|
391 |
|
392 /** |
|
393 Attempts to suspend the specified thread |
|
394 |
|
395 @param aTargetThread thread to suspend |
|
396 |
|
397 @return KErrNone on success, KErrAlreadyExists if the thread is already suspended, |
|
398 or one of the other system wide error codes |
|
399 */ |
|
400 TInt DProcessTracker::SuspendThread(DThread* aTargetThread, TBool aFreezeThread) |
|
401 { |
|
402 LOG_MSG3("DProcessTracker::SuspendThread() Requesting suspend for: 0x%08x, freeze thread: %d", aTargetThread->iId, aFreezeThread?1:0); |
|
403 |
|
404 //get the file name and return if NULL |
|
405 HBuf* name = GetFileName(aTargetThread); |
|
406 if(!name) |
|
407 { |
|
408 return KErrNotFound; |
|
409 } |
|
410 |
|
411 //iterate through the processes trying to match the name, try to suspend the thread if found |
|
412 TInt numberOfProcesses = iProcesses.Count(); |
|
413 for(TInt i=0; i < numberOfProcesses; i++) |
|
414 { |
|
415 if(iProcesses[i]->ProcessName().CompareF(*name) == 0) |
|
416 { |
|
417 return iProcesses[i]->SuspendThread(aTargetThread, aFreezeThread); |
|
418 } |
|
419 } |
|
420 |
|
421 //couldn't find process so return error |
|
422 return KErrPermissionDenied; |
|
423 } |
|
424 |
|
425 void DProcessTracker::FSWait() |
|
426 { |
|
427 TInt numberOfProcesses = iProcesses.Count(); |
|
428 for(TInt i=0; i < numberOfProcesses; i++) |
|
429 { |
|
430 iProcesses[i]->FSWait(); |
|
431 } |
|
432 } |
|
433 |
|
434 /** |
|
435 Attempts to resume the specified thread |
|
436 |
|
437 @param aTargetThread thread to resume |
|
438 |
|
439 @return KErrNone on success, KErrInUse if the thread is not suspended, |
|
440 or one of the other system wide error codes |
|
441 */ |
|
442 TInt DProcessTracker::ResumeThread(DThread* aTargetThread) |
|
443 { |
|
444 LOG_MSG2("DProcessTracker::ResumeThread() Requesting resume for: 0x%08x", aTargetThread->iId); |
|
445 |
|
446 //get the file name and return if NULL |
|
447 HBuf* name = GetFileName(aTargetThread); |
|
448 if(!name) |
|
449 { |
|
450 return KErrNotFound; |
|
451 } |
|
452 |
|
453 //iterate through the processes trying to match the name, try to resume the thread if found |
|
454 TInt numberOfProcesses = iProcesses.Count(); |
|
455 for(TInt i=0; i < numberOfProcesses; i++) |
|
456 { |
|
457 if(iProcesses[i]->ProcessName().CompareF(*name) == 0) |
|
458 { |
|
459 return iProcesses[i]->ResumeThread(aTargetThread); |
|
460 } |
|
461 } |
|
462 |
|
463 //couldn't find process so return error |
|
464 return KErrPermissionDenied; |
|
465 } |
|
466 |
|
467 /** |
|
468 Get a thread's originating file name |
|
469 |
|
470 @param aThread the thread to get the file name for |
|
471 |
|
472 @return a pointer to the thread's file name, if there are problems accessing |
|
473 the file name then NULL will be returned |
|
474 */ |
|
475 HBuf* DProcessTracker::GetFileName(DThread* aThread) const |
|
476 { |
|
477 //check if the thread is NULL and return if so |
|
478 if(!aThread) |
|
479 { |
|
480 return NULL; |
|
481 } |
|
482 |
|
483 //get the owning process and return if it is NULL |
|
484 DProcess* process = aThread->iOwningProcess; |
|
485 if(!process) |
|
486 { |
|
487 return NULL; |
|
488 } |
|
489 |
|
490 //get the process' code seg and return if it is NULL |
|
491 DCodeSeg* codeSeg = process->iCodeSeg; |
|
492 if(!codeSeg) |
|
493 { |
|
494 return NULL; |
|
495 } |
|
496 |
|
497 //return the code seg's stored file name (which could theoretically be NULL) |
|
498 return codeSeg->iFileName; |
|
499 } |
|
500 |
|