|
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: Definitions for the class CustomUser. |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <f32file.h> |
|
19 #include <utf.h> |
|
20 #include "customuser.h" |
|
21 #include "analyzetoolmainallocator.h" |
|
22 #include "analyzetoolallocator.h" |
|
23 #include "atlog.h" |
|
24 #include "analyzetoolmemoryallocator.h" |
|
25 #include "analyzetoolpanics.pan" |
|
26 #include "atstorageservercommon.h" |
|
27 #include "atdriveinfo.h" |
|
28 #include <analyzetool/analyzetooltraceconstants.h> |
|
29 |
|
30 #ifdef USE_CLEANER_DLL |
|
31 // Global variable to detect dll attach & detach in process. |
|
32 // Note! This is initialized after SetupThreadHeap so its not usable there. |
|
33 // This is used to store the main thread id and track when the process ends |
|
34 // to load the cleaner dll with call back feature to cleanup allocator at the |
|
35 // last possible phase. |
|
36 #include <analyzetool/analyzetoolcleaner.h> |
|
37 |
|
38 // CONSTANTS |
|
39 const TInt KAToolCleanerOrdinal = 1; |
|
40 |
|
41 class TAnalyzeToolGlobalTracker : public TAnalyzeToolCleanerBase |
|
42 { |
|
43 public: |
|
44 /* Main thread id */ |
|
45 TThreadId iMainId; |
|
46 |
|
47 /* Inform if panic occured */ |
|
48 TBool iPanic; |
|
49 |
|
50 // ----------------------------------------------------------------------------- |
|
51 // TAnalyzeToolGlobalTracker::TAnalyzeToolGlobalTracker() |
|
52 // C++ default constructor |
|
53 // ----------------------------------------------------------------------------- |
|
54 // |
|
55 TAnalyzeToolGlobalTracker() |
|
56 { |
|
57 LOGSTR1( "ATMH TAnalyzeToolGlobalTracker::TAnalyzeToolGlobalTracker()" ); |
|
58 |
|
59 iPanic = EFalse; // no panic occured |
|
60 iMainId = RThread().Id(); // set main thread id |
|
61 LOGSTR2( "ATMH TAnalyzeToolGlobalTracker() > Main id set: %d", |
|
62 iMainId.operator TUint() ); |
|
63 } |
|
64 |
|
65 // ----------------------------------------------------------------------------- |
|
66 // TAnalyzeToolGlobalTracker::~TAnalyzeToolGlobalTracker() |
|
67 // Destructor. |
|
68 // ----------------------------------------------------------------------------- |
|
69 // |
|
70 ~TAnalyzeToolGlobalTracker() |
|
71 { |
|
72 LOGSTR1( "ATMH TAnalyzeToolGlobalTracker::~TAnalyzeToolGlobalTracker()" ); |
|
73 |
|
74 // We dont load dll if panic has happened (uninstallation has been done). |
|
75 if ( iPanic ) |
|
76 { |
|
77 LOGSTR1( "ATMH ~TAnalyzeToolGlobalTracker > Panic set not loading cleaner dll." ); |
|
78 return; |
|
79 } |
|
80 |
|
81 LOGSTR1( "ATMH ~TAnalyzeToolGlobalTracker > about to load cleaner dll" ); |
|
82 // Load cleaner library and set a call back to our cleanup |
|
83 RLibrary lib; |
|
84 TInt error( lib.Load( KATCleanerDllName ) ); |
|
85 if ( error == KErrNone ) |
|
86 { |
|
87 // Set address to point to ourself |
|
88 TLibraryFunction func = lib.Lookup( KAToolCleanerOrdinal ); // Ordinal 1 of the dll |
|
89 ATCLEANERTABLE* cleaner = (ATCLEANERTABLE*) func(); // Use function to get address |
|
90 cleaner->At( 0 ) = (TUint32) this; // Set address |
|
91 LOGSTR1( "ATMH ~TAnalyzeToolGlobalTracker() > cleaner dll loaded and call back set" ); |
|
92 } |
|
93 else |
|
94 { |
|
95 // Error loading cleanup dll |
|
96 LOGSTR2( "ATMH ~TAnalyzeToolGlobalTracker() > cleaner dll load error(%i) uninstalling allocator now!", |
|
97 error ); |
|
98 Cleanup(); |
|
99 } |
|
100 } |
|
101 |
|
102 // ----------------------------------------------------------------------------- |
|
103 // TAnalyzeToolGlobalTracker::Cleanup() |
|
104 // |
|
105 // ----------------------------------------------------------------------------- |
|
106 // |
|
107 void Cleanup() |
|
108 { |
|
109 LOGSTR1( "ATMH TAnalyzeToolGlobalTracker::Cleanup() - allocator uninstall" ); |
|
110 |
|
111 // Uninstall allocator |
|
112 ( (RAnalyzeToolMemoryAllocator&) User::Allocator() ).Uninstall(); |
|
113 } |
|
114 |
|
115 }; |
|
116 |
|
117 // Global variable definition. |
|
118 TAnalyzeToolGlobalTracker gGlobalTracker; |
|
119 #endif |
|
120 |
|
121 // CONSTANTS |
|
122 // When needed, update the version number directly inside _LIT macro. |
|
123 // Constant for the atool API(staticlib) version. |
|
124 _LIT( KAtoolApiVersion, "1.7.5" ); |
|
125 |
|
126 // Version number buffer length |
|
127 const TInt KAtoolVersionNumberLength = 10; |
|
128 |
|
129 // Wrong version error code |
|
130 const TInt KAtoolVersionError = -1999; |
|
131 |
|
132 // Version number separator |
|
133 _LIT( KVersionSeparator, ";" ); |
|
134 |
|
135 // Incorrect version error strings |
|
136 _LIT( KIncorrectText, "ERROR_OCCURED INCORRECT_ATOOL_VERSION [API v.%S][ATOOL v.%S]" ); |
|
137 _LIT( KIncorrectTextTrace, "PCSS " ); |
|
138 |
|
139 // ----------------------------------------------------------------------------- |
|
140 // CustomUser::Panic() |
|
141 // Overloaded User::Panic() function |
|
142 // ----------------------------------------------------------------------------- |
|
143 // |
|
144 EXPORT_C void CustomUser::Panic( const TDesC& aCategory, TInt aReason ) |
|
145 { |
|
146 LOGSTR3( "ATMH CustomUser::Panic() %S %i", &aCategory, aReason ); |
|
147 |
|
148 #ifdef USE_CLEANER_DLL |
|
149 // Set global tracker that panic has happened. |
|
150 gGlobalTracker.iPanic = ETrue; |
|
151 #endif |
|
152 |
|
153 // Uninstall thread's RAllocator |
|
154 ( (RAnalyzeToolMemoryAllocator&) User::Allocator() ).Uninstall(); |
|
155 |
|
156 // Call the "real" User::Panic() |
|
157 User::Panic( aCategory, aReason ); |
|
158 } |
|
159 |
|
160 // ----------------------------------------------------------------------------- |
|
161 // CustomUser::Exit() |
|
162 // Overloaded User::Exit() function |
|
163 // ----------------------------------------------------------------------------- |
|
164 // |
|
165 EXPORT_C void CustomUser::Exit( TInt aReason ) |
|
166 { |
|
167 LOGSTR3( "ATMH CustomUser::Exit() %i %i", aReason, RThread().Id().Id() ); |
|
168 |
|
169 if ( aReason != KAtoolVersionError ) |
|
170 { |
|
171 #ifdef USE_CLEANER_DLL |
|
172 // Only uninstall allocator if its not the process main/first thread. |
|
173 LOGSTR3( "ATMH CustomUser::Exit() - Thread id: %d - Main Id: %d", |
|
174 RThread().Id().operator TUint(), gGlobalTracker.iMainId.operator TUint() ); |
|
175 |
|
176 if ( RThread().Id() != gGlobalTracker.iMainId ) |
|
177 { |
|
178 LOGSTR2("ATMH CustomUser::Exit() - Calling allocator uninstall in thread: %d" , RThread().Id().operator TUint() ); |
|
179 ( (RAnalyzeToolMemoryAllocator&) User::Allocator() ).Uninstall(); |
|
180 } |
|
181 #else |
|
182 // Uninstall thread's RAllocator |
|
183 ( (RAnalyzeToolMemoryAllocator&) User::Allocator() ).Uninstall(); |
|
184 LOGSTR1( "ATMH CustomUser::Exit() - about to User::Exit" ); |
|
185 #endif |
|
186 } |
|
187 |
|
188 // Call the "real" User::Exit() |
|
189 User::Exit( aReason ); |
|
190 } |
|
191 |
|
192 // ----------------------------------------------------------------------------- |
|
193 // CustomUser::SetCritical() |
|
194 // Overloaded User::SetCritical() function which returns |
|
195 // KErrNone, if successful; KErrArgument, if EAllThreadsCritical is |
|
196 // passed - this is a state associated with a process, and you use |
|
197 // User::SetProcessCritical() to set it. |
|
198 // ----------------------------------------------------------------------------- |
|
199 // |
|
200 EXPORT_C TInt CustomUser::SetCritical( User::TCritical aCritical ) |
|
201 { |
|
202 LOGSTR1( "ATMH CustomUser::SetCritical()" ); |
|
203 // Check the given User::TCritical type |
|
204 if ( aCritical == User::EAllThreadsCritical ) |
|
205 { |
|
206 return KErrArgument; |
|
207 } |
|
208 else |
|
209 { |
|
210 return KErrNone; |
|
211 } |
|
212 } |
|
213 |
|
214 // ----------------------------------------------------------------------------- |
|
215 // CustomUser::SetProcessCritical() |
|
216 // Overloaded User::SetProcessCritical() function |
|
217 // KErrNone, if successful; KErrArgument, if either EProcessCritical or |
|
218 // EProcessPermanent is passed - these are states associated with a |
|
219 // thread, and you use User::SetCritical() to set them. |
|
220 // ----------------------------------------------------------------------------- |
|
221 // |
|
222 EXPORT_C TInt CustomUser::SetProcessCritical( User::TCritical aCritical ) |
|
223 { |
|
224 LOGSTR1( "ATMH CustomUser::SetProcessCritical()" ); |
|
225 // Check the given User::TCritical type |
|
226 if ( aCritical == User::EProcessCritical || |
|
227 User::EProcessPermanent == aCritical ) |
|
228 { |
|
229 return KErrArgument; |
|
230 } |
|
231 else |
|
232 { |
|
233 return KErrNone; |
|
234 } |
|
235 } |
|
236 |
|
237 // ----------------------------------------------------------------------------- |
|
238 // CustomUser::SetupThreadHeap() |
|
239 // Overloaded UserHeap::SetupThreadHeap function |
|
240 // ----------------------------------------------------------------------------- |
|
241 // |
|
242 EXPORT_C TInt CustomUser::SetupThreadHeap( TBool aNotFirst, |
|
243 SStdEpocThreadCreateInfo& aInfo, const TFileName& aFileName, |
|
244 TUint32 aLogOption, TUint32 aIsDebug, const TATVersion& aVersion, |
|
245 TUint32 aAllocCallStackSize, TUint32 aFreeCallStackSize, |
|
246 TRefByValue<const TDesC> aFmt, ... ) |
|
247 { |
|
248 LOGSTR1( "ATMH CustomUser::SetupThreadHeap()" ); |
|
249 LOGSTR2( "ATMH > Thread id(%d)", RThread().Id().operator TUint() ); |
|
250 |
|
251 // Add handling of the argument list here. |
|
252 |
|
253 TInt ret( KErrNone ); |
|
254 // Check version number |
|
255 TBuf<KAtoolVersionNumberLength> atoolVer; |
|
256 if ( CheckVersion( aVersion, atoolVer ) != KErrNone ) |
|
257 { |
|
258 LOGSTR1( "ATMH > Wrong API version > Inform user and Exit." ); |
|
259 ReportIncorrectVersion( aLogOption, aFileName, atoolVer ); |
|
260 return KAtoolVersionError; |
|
261 } |
|
262 |
|
263 // Check is this shared heap |
|
264 if ( aInfo.iAllocator == NULL ) |
|
265 { |
|
266 LOGSTR1( "ATMH creating a new heap" ); |
|
267 // RAllocator is NULL so heap is not shared, creating a new heap |
|
268 ret = UserHeap::SetupThreadHeap( aNotFirst, aInfo ); |
|
269 __ASSERT_ALWAYS( KErrNone == ret, AssertPanic( EFailedToCreateHeap ) ); |
|
270 |
|
271 #if ( SYMBIAN_VERSION_SUPPORT >= SYMBIAN_3 ) |
|
272 #ifndef __WINS__ |
|
273 // Set dummy Tls value |
|
274 TAny* dummyPtr( NULL ); |
|
275 TInt setErr( UserSvr::DllSetTls( KDummyHandle, dummyPtr ) ); |
|
276 LOGSTR2( "ATMH > Set Tls err(%i)", setErr ); |
|
277 #endif |
|
278 #endif |
|
279 // Install the RAllocator |
|
280 aInfo.iAllocator = &InstallAllocator( aNotFirst, aFileName, aLogOption, aIsDebug, |
|
281 aAllocCallStackSize, aFreeCallStackSize ); |
|
282 } |
|
283 else |
|
284 { |
|
285 LOGSTR1( "ATMH sharing the heap" ); |
|
286 // The heap is shared. Acquire pointer to the original heap |
|
287 RAnalyzeToolMemoryAllocator* allocator = |
|
288 (RAnalyzeToolMemoryAllocator*) aInfo.iAllocator; |
|
289 // Share the heap |
|
290 allocator->ShareHeap(); |
|
291 // Switch thread heap |
|
292 User::SwitchAllocator( allocator ); |
|
293 } |
|
294 return ret; |
|
295 } |
|
296 |
|
297 // ----------------------------------------------------------------------------- |
|
298 // CustomUser::InstallAllocator |
|
299 // Installs the RAllocator |
|
300 // ----------------------------------------------------------------------------- |
|
301 // |
|
302 //lint -e{429} suppress "Custodial pointer 'allocator' has not been freed or returned" |
|
303 EXPORT_C RAllocator& CustomUser::InstallAllocator( TBool aNotFirst, |
|
304 const TFileName& aFileName, TUint32 aLogOption, TUint32 aIsDebug, |
|
305 TUint32 aAllocCallStackSize, TUint32 aFreeCallStackSize ) |
|
306 { |
|
307 LOGSTR1( "ATMH CustomUser::InstallAllocator()" ); |
|
308 |
|
309 // Open handle to the device driver |
|
310 RAnalyzeTool analyzetool; |
|
311 TInt error = analyzetool.Open(); |
|
312 |
|
313 // Check if the device driver has already loaded |
|
314 if ( KErrNone == error ) |
|
315 { |
|
316 LOGSTR1( "ATMH CustomUser::InstallAllocator() - analyzetool.Open() returned KErrNone" ); |
|
317 // The device driver has already loaded |
|
318 // Get pointer to the main thread allocator |
|
319 TMainThreadParamsBuf params; |
|
320 params().iProcessId = RProcess().Id().operator TUint(); |
|
321 error = analyzetool.MainThreadAlloctor( params ); |
|
322 |
|
323 __ASSERT_ALWAYS( KErrNone == error, AssertPanic( ECantOpenHandle ) ); |
|
324 |
|
325 // Close handle to the device driver |
|
326 analyzetool.Close(); |
|
327 |
|
328 // Is this the first thread of the program |
|
329 if ( params().iAlone ) |
|
330 { |
|
331 LOGSTR1( "ATMH CustomUser::InstallAllocator() - first thread of the program" ); |
|
332 // Only one thread in the program. Must be main thread |
|
333 RAnalyzeToolMainAllocator* allocator = |
|
334 new RAnalyzeToolMainAllocator( aNotFirst, aFileName, aLogOption, |
|
335 aIsDebug, aAllocCallStackSize, aFreeCallStackSize ); |
|
336 |
|
337 __ASSERT_ALWAYS( allocator != NULL, AssertPanic( ENoMemory ) ); |
|
338 |
|
339 // Change threads allocator |
|
340 User::SwitchAllocator( allocator ); |
|
341 |
|
342 // Return reference to the RAllocator |
|
343 return *allocator; |
|
344 } |
|
345 // This is not the first thread. A new thread with a new heap created |
|
346 else |
|
347 { |
|
348 LOGSTR1( "ATMH CustomUser::InstallAllocator() - create a new allocator for the new thread" ); |
|
349 // Create new RAllocator with handles from the main thread |
|
350 RAnalyzeToolAllocator* allocator = new RAnalyzeToolAllocator( |
|
351 aNotFirst, |
|
352 ((RAnalyzeToolMainAllocator*)params().iAllocator)->StorageServer(), |
|
353 ((RAnalyzeToolMainAllocator*)params().iAllocator)->Codeblocks(), |
|
354 ((RAnalyzeToolMainAllocator*)params().iAllocator)->Mutex(), |
|
355 ((RAnalyzeToolMainAllocator*)params().iAllocator)->ProcessId(), |
|
356 ((RAnalyzeToolMainAllocator*)params().iAllocator)->AnalyzeTool(), |
|
357 ((RAnalyzeToolMainAllocator*)params().iAllocator)->StorageServerOpen(), |
|
358 ((RAnalyzeToolMainAllocator*)params().iAllocator)->LogOption(), |
|
359 ((RAnalyzeToolMainAllocator*)params().iAllocator)->AllocMaxCallStack(), |
|
360 ((RAnalyzeToolMainAllocator*)params().iAllocator)->FreeMaxCallStack() ); |
|
361 |
|
362 __ASSERT_ALWAYS( allocator != NULL, AssertPanic( ENoMemory ) ); |
|
363 |
|
364 // Change threads allocator |
|
365 User::SwitchAllocator( allocator ); |
|
366 |
|
367 // Return reference to the RAllocator |
|
368 return *allocator; |
|
369 } |
|
370 } |
|
371 // The device driver does not exists so this must be the first thread |
|
372 else |
|
373 { |
|
374 LOGSTR1( "ATMH CustomUser::InstallAllocator() - analyzetool.Open() returned error, creating DD" ); |
|
375 RAnalyzeToolMainAllocator* allocator = |
|
376 new RAnalyzeToolMainAllocator( aNotFirst, aFileName, aLogOption, aIsDebug, |
|
377 aAllocCallStackSize, aFreeCallStackSize ); |
|
378 |
|
379 __ASSERT_ALWAYS( allocator != NULL, AssertPanic( ENoMemory ) ); |
|
380 |
|
381 // Change threads allocator |
|
382 User::SwitchAllocator( allocator ); |
|
383 |
|
384 // Return reference to the RAllocator |
|
385 return *allocator; |
|
386 } |
|
387 } |
|
388 |
|
389 // ----------------------------------------------------------------------------- |
|
390 // CustomUser::CheckVersion |
|
391 // Check atool version |
|
392 // ----------------------------------------------------------------------------- |
|
393 // |
|
394 TInt CustomUser::CheckVersion( const TATVersion& aVersion, TDes& aToolVersion ) |
|
395 { |
|
396 LOGSTR2( "ATMH CustomUser::CheckVersion(), aVersion( %S )", &aVersion ); |
|
397 |
|
398 TFileName version; |
|
399 version.Copy( aVersion ); |
|
400 TBuf<KAtoolVersionNumberLength> apiVer; |
|
401 |
|
402 // Find separator place |
|
403 TInt findplace( version.Find( KVersionSeparator() ) ); |
|
404 // Parse API version first [x.x.x;x.x.x] |
|
405 if ( findplace >= 0 && findplace <= apiVer.MaxLength() ) |
|
406 { |
|
407 apiVer.Copy( version.Mid( 0, findplace ) ); |
|
408 version.Delete( 0, findplace + KVersionSeparator().Length() ); |
|
409 } |
|
410 |
|
411 if ( version.Length() <= aToolVersion.MaxLength() ) |
|
412 { |
|
413 aToolVersion.Copy( version ); |
|
414 if ( aToolVersion.Compare( KAtoolApiVersion ) == KErrNone && |
|
415 apiVer.Length() == 0 ) |
|
416 { |
|
417 // Support 1.5.0 version (Version info: [1.5.0]) |
|
418 apiVer.Copy( version ); |
|
419 } |
|
420 } |
|
421 |
|
422 LOGSTR3( "ATMH > API version( %S ), ATOOL version( %S )", |
|
423 &apiVer, &aToolVersion ); |
|
424 |
|
425 // Check version numbers |
|
426 if ( apiVer.Compare( KAtoolApiVersion ) == KErrNone ) |
|
427 { |
|
428 return KErrNone; |
|
429 } |
|
430 return KErrCancel; |
|
431 } |
|
432 |
|
433 // ----------------------------------------------------------------------------- |
|
434 // CustomUser::ReportIncorrectVersion |
|
435 // Function for showing incorrect version information |
|
436 // ----------------------------------------------------------------------------- |
|
437 // |
|
438 void CustomUser::ReportIncorrectVersion( const TUint32 aLogOption, |
|
439 const TFileName& aFileName, const TDes& aToolVersion ) |
|
440 { |
|
441 LOGSTR2( "ATMH CustomUser::ReportIncorrectVersion(), aFileName( %S )", |
|
442 &aFileName ); |
|
443 |
|
444 switch ( aLogOption ) |
|
445 { |
|
446 case EATLogToFile: |
|
447 { |
|
448 LOGSTR1( "ATMH ReportIncorrectVersion > EATLogToFile" ); |
|
449 |
|
450 // A handle to a file server session. |
|
451 RFs fs; |
|
452 // Creates and opens a file, |
|
453 // and performs all operations on a single open file. |
|
454 RFile file; |
|
455 // Create full path buffer |
|
456 TBuf<KMaxFileName> logFileBuf; |
|
457 // Connects a client to the file server. |
|
458 TInt err( fs.Connect() ); |
|
459 |
|
460 if ( !err ) |
|
461 { |
|
462 err = TATDriveInfo::CreatePath( logFileBuf, aFileName, fs ); |
|
463 |
|
464 // Replace file if exists |
|
465 if ( err && err != KErrAlreadyExists ) |
|
466 { |
|
467 LOGSTR2( "ATMH > TATDriveInfo::CreatePath() err( %i )", err ); |
|
468 return; |
|
469 } |
|
470 |
|
471 // Replace file if exists (drive C) |
|
472 err = file.Replace( fs, logFileBuf, EFileWrite ); |
|
473 |
|
474 // Write to file |
|
475 if ( !err ) |
|
476 { |
|
477 err = file.Write( KDataFileVersion ); |
|
478 // Error msg buffer |
|
479 TBuf8<KMaxFileName> msg; |
|
480 // Write the error code to the buffer |
|
481 logFileBuf.Format( KIncorrectText, &KAtoolApiVersion, &aToolVersion ); |
|
482 CnvUtfConverter::ConvertFromUnicodeToUtf8( msg, logFileBuf ); |
|
483 err = file.Write( msg ); |
|
484 } |
|
485 // Closes the file. |
|
486 file.Close(); |
|
487 } |
|
488 |
|
489 LOGSTR2( "ATMH > File err( %i )", err ); |
|
490 // Closes the handle. |
|
491 fs.Close(); |
|
492 } |
|
493 break; |
|
494 |
|
495 case EATUseDefault: |
|
496 case EATLogToTrace: |
|
497 { |
|
498 LOGSTR1( "ATMH > ReportIncorrectVersion > EATLogToTrace" ); |
|
499 // Error msg buffer |
|
500 TBuf<KMaxFileName> msg; |
|
501 msg.Copy( KIncorrectTextTrace ); |
|
502 msg.Append( KIncorrectText ); |
|
503 TBuf<KMaxFileName> traceMsg; |
|
504 // Write the error code to the buffer |
|
505 traceMsg.Format( msg, &KAtoolApiVersion, &aToolVersion ); |
|
506 RDebug::Print( traceMsg ); |
|
507 } |
|
508 break; |
|
509 |
|
510 default: |
|
511 { |
|
512 LOGSTR1( "ATMH > ReportIncorrectVersion > default" ); |
|
513 } |
|
514 break; |
|
515 } |
|
516 } |
|
517 |
|
518 // End of File |