os/kernelhwsrv/kernel/eka/debug/utrace/src/e32utrace.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2007-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 // Trace API
    15 //
    16 
    17 #include <e32utf.h>
    18 #include "traceutils.h"
    19 
    20 
    21 //Add the Doxygen macro so that Doxygen doesn't need to know about the SYMBIAN_TRACE_EXECUTABLE_IS_INCLUDED define.
    22 //This allows doxygen to pick up the comments for the enabled implementation of the trace API.
    23 #ifdef __DOXYGEN__
    24 	#define SYMBIAN_TRACE_EXECUTABLE_IS_INCLUDED
    25 #endif //__DOXYGEN__
    26 
    27 
    28 namespace UTF
    29 {
    30 
    31 
    32 /**
    33  * This method currently returns 0 until a fix is provided
    34  * for:
    35  * DPDEF110067	[Trace STP][ADP37] No Way for The Secondary Category to Be Set To Default
    36  * DEF119891	[TraceSTP] 1.4.1 Default the ModuleUid
    37  * This will in the future return the UID3 of the executable.
    38  *
    39  * @return Returns 0.
    40  */
    41 EXPORT_C TModuleUid TTraceContext::DefaultModuleUid()
    42 	{
    43 	return 0;
    44 	}
    45 
    46 
    47 /**
    48  * Check if thread identification will be added by default.
    49  */
    50 EXPORT_C THasThreadIdentification TTraceContext::HasThreadIdentification()  const
    51 	{
    52 	return iHasThreadIdentification;
    53 	};
    54 
    55 /**
    56  * Check if PC will be added by default.
    57  */
    58 EXPORT_C THasProgramCounter TTraceContext::HasProgramCounter()  const
    59 	{
    60 	return iHasProgramCounter;
    61 	};
    62 
    63 
    64 /**
    65  * Get the current classification
    66  *
    67  * @return The current classification of the trace point context.
    68  */
    69 EXPORT_C TClassification TTraceContext::Classification() const
    70 	{
    71 	return iClassification;
    72 	};
    73 
    74 /**
    75  * Get the current module Uid
    76  *
    77  * @return The currently set module Uid
    78  */
    79 EXPORT_C TModuleUid TTraceContext::ModuleUid() const
    80 	{
    81 	return iModuleUid;
    82 	}
    83 
    84 
    85 //--------------------- UTrace compiled in ------------------------
    86 #ifdef SYMBIAN_TRACE_EXECUTABLE_IS_INCLUDED
    87 
    88 
    89 
    90 // --------- printf ------------
    91 
    92 /**
    93 Prints a string by outputting a trace packet with the format id KFormatPrintf.
    94 
    95 If the specified string is too long to fit into a single trace packet
    96 a multipart trace is generated.
    97 
    98 @param aContext 	The trace packet context. @see TTraceContext
    99 @param aDes			The string. This can be of variable length.
   100 
   101 @return 			The trace packet was/was not output.
   102 
   103 @See BTrace::TMultipart
   104 */
   105 EXPORT_C TBool Print(const TTraceContext& aContext, const TDesC8& aDes)
   106 	{
   107 	if(WouldBeTracedNow(aContext))
   108 		{
   109 		GET_PC(pc);
   110 		return UTRACE_SECONDARY_ANY(aContext.Classification(), aContext.ModuleUid(), aContext.HasThreadIdentification(), aContext.HasProgramCounter(), pc, KFormatPrintf, aDes.Ptr(), aDes.Size());
   111 		}
   112 	return EFalse;
   113 	};
   114 
   115 
   116 #ifdef  __KERNEL_MODE__
   117 
   118 /**
   119 Prints a formatted string in kernel mode only by outputting a trace packet with the format id KFormatPrintf.
   120 
   121 The function uses Kern::AppendFormat() to do the formatting.
   122 
   123 Although it is safe to call this function from an ISR, it polls the output
   124 serial port and may take a long time to complete, invalidating any
   125 real-time guarantee.
   126 
   127 If called from an ISR, it is possible for output text to be intermingled
   128 with other output text if one set of output interrupts or preempts another.
   129 
   130 Some of the formatting options may not work inside an ISR.
   131 
   132 Be careful not to use a string that is too long to fit onto the stack.
   133 If the specified string is too long to fit into a single trace packet
   134 a multipart trace is generated.
   135 
   136 
   137 @param aContext 	The trace packet context. @see TTraceContext
   138 @param aFmt 		The format string. This must not be longer than 256 characters.
   139 @param ...			A variable number of arguments to be converted to text as dictated
   140 					by the format string.
   141 
   142 @return 			The trace packet was/was not output.
   143 
   144 @pre Calling thread can either be in a critical section or not.
   145 @pre Interrupts must be enabled.
   146 @pre Kernel must be unlocked
   147 @pre Call in any context.
   148 @pre Suitable for use in a device driver
   149 
   150 @see Kern::AppendFormat()
   151 @See BTrace::TMultipart
   152 
   153 Note: Until the Utrace packet format is changed this call will not add the
   154 FormatId to the trace packet for kernel side traces.
   155 */
   156 EXPORT_C TBool Printf(const TTraceContext& aContext, const char* aFmt, ...)
   157 	{
   158 	if(WouldBeTracedNow(aContext))
   159 		{
   160 		GET_PC(pc);
   161 		TBuf8<KMaxPrintfSize> buf;
   162 		VA_LIST list;
   163 		VA_START(list,aFmt);
   164 		Kern::AppendFormat(buf,aFmt,list);
   165 		return UTRACE_SECONDARY_ANY(aContext.Classification(), aContext.ModuleUid(), aContext.HasThreadIdentification(), aContext.HasProgramCounter(), pc, KFormatPrintf, buf.Ptr(), buf.Size());
   166 		}
   167 	return EFalse;
   168 	}
   169 
   170 /**
   171 Prints a formatted string by outputting a trace packet with the format id KFormatPrintf.
   172 
   173 If the specified string is too long to fit into a single trace packet
   174 a multipart trace is generated.
   175 
   176 @param aContext 	The trace packet context. @see TTraceContext
   177 @param aFmt 		The format string. This must not be longer than 256 characters.
   178 @param ...			A variable number of arguments to be converted to text as dictated
   179 					by the format string.
   180 
   181 @return 			The trace packet was/was not output.
   182 
   183 @See BTrace::TMultipart
   184 */
   185 EXPORT_C TBool Printf(const TTraceContext& aContext, TRefByValue<const TDesC8> aFmt,...)
   186 	{
   187 	if(WouldBeTracedNow(aContext))
   188 		{
   189 		GET_PC(pc);
   190 		TBuf8<KMaxPrintfSize> buf;
   191 		VA_LIST list;
   192 		VA_START(list,aFmt);
   193 		TDesC8 fmt = aFmt;
   194 		Kern::AppendFormat(buf,(char*)fmt.Ptr(),list);
   195 		return UTRACE_SECONDARY_ANY(aContext.Classification(), aContext.ModuleUid(), aContext.HasThreadIdentification(), aContext.HasProgramCounter(), pc, KFormatPrintf, buf.Ptr(), buf.Size());
   196 		}
   197 	return EFalse;
   198 	}
   199 
   200 #endif // __KERNEL_MODE__
   201 #ifndef __KERNEL_MODE__
   202 
   203 /**
   204 Prints a formatted string by outputting a trace packet with the format id KFormatPrintf.
   205 
   206 
   207 If the specified string is too long to fit into a single trace packet
   208 a multipart trace is generated.
   209 
   210 @param aContext 	The trace packet context. @see TTraceContext
   211 @param aFmt 		The format string. This must not be longer than 256 characters.
   212 @param ...			A variable number of arguments to be converted to text as dictated
   213 					by the format string.
   214 
   215 @return 			The trace packet was/was not output.
   216 
   217 @See BTrace::TMultipart
   218 */
   219 EXPORT_C TBool Printf(const TTraceContext& aContext, const char* aFmt, ...)
   220 	{
   221 	if(WouldBeTracedNow(aContext))
   222 		{
   223 		GET_PC(pc);
   224 		TTruncateOverflow8 overflow;
   225 		VA_LIST list;
   226 		VA_START(list,aFmt);
   227 		TPtrC8 fmt((const TText8*)aFmt);
   228 		TBuf8<KMaxPrintfSize> buf;
   229         // coverity[uninit_use_in_call]
   230 		buf.AppendFormatList(fmt,list,&overflow);
   231 		return UTRACE_SECONDARY_ANY(aContext.Classification(), aContext.ModuleUid(), aContext.HasThreadIdentification(), aContext.HasProgramCounter(), pc, KFormatPrintf, buf.Ptr(), buf.Size());
   232 		}
   233 	return EFalse;
   234 	};
   235 
   236 /**
   237 Prints a formatted string by outputting a trace packet with the format id KFormatPrintf.
   238 
   239 If the specified string is too long to fit into a single trace packet
   240 a multipart trace is generated.
   241 
   242 @param aContext 	The trace packet context. @see TTraceContext
   243 @param aFmt 		The format string. This must not be longer than 256 characters.
   244 @param ...			A variable number of arguments to be converted to text as dictated
   245 					by the format string.
   246 
   247 @return 			The trace packet was/was not output.
   248 
   249 @See BTrace::TMultipart
   250 */
   251 EXPORT_C TBool Printf(const TTraceContext& aContext, TRefByValue<const TDesC8> aFmt,...)
   252 	{
   253 	if(WouldBeTracedNow(aContext))
   254 		{
   255 		GET_PC(pc);
   256 		TTruncateOverflow8 overflow;
   257 		VA_LIST list;
   258 		VA_START(list,aFmt);
   259 		TBuf8<KMaxPrintfSize> buf;
   260         // coverity[uninit_use_in_call]
   261 		buf.AppendFormatList(aFmt,list,&overflow);
   262 		return UTRACE_SECONDARY_ANY(aContext.Classification(), aContext.ModuleUid(), aContext.HasThreadIdentification(), aContext.HasProgramCounter(), pc, KFormatPrintf, buf.Ptr(), buf.Size());
   263 		}
   264 	return EFalse;
   265 	}
   266 
   267 /**
   268 Prints a formatted string by outputting a trace packet with the format id
   269 KFormatPrintfUnicode for unicode strings and KFormatPrintf for other strings.
   270 
   271 If the specified string is too long to fit into a single trace packet
   272 a multipart trace is generated.
   273 
   274 @param aContext 	The trace packet context. @see TTraceContext
   275 @param aFmt 		The format string. This must not be longer than 256 characters.
   276 @param ...			A variable number of arguments to be converted to text as dictated
   277 					by the format string.
   278 
   279 @return 			The trace packet was/was not output.
   280 
   281 @See BTrace::TMultipart
   282 */
   283 EXPORT_C TBool Printf(const TTraceContext& aContext, TRefByValue<const TDesC16> aFmt,...)
   284 	{
   285 	if(WouldBeTracedNow(aContext))
   286 		{
   287 		GET_PC(pc);
   288 		TTruncateOverflow16 overflow;
   289 		VA_LIST list;
   290 		VA_START(list,aFmt);
   291 		TBuf<KMaxPrintfSize> buf;
   292 	    // coverity[uninit_use_in_call]
   293 		buf.AppendFormatList(aFmt,list,&overflow);
   294 		#ifndef _UNICODE
   295 		TPtr8 p(buf.Collapse());
   296 		return UTRACE_SECONDARY_ANY(aContext.Classification(), aContext.ModuleUid(), aContext.HasThreadIdentification(), aContext.HasProgramCounter(), myPc, KFormatPrintf, buf.PtrZ(), p.Size());
   297 		#else //_UNICODE
   298 		return UTRACE_SECONDARY_ANY(aContext.Classification(), aContext.ModuleUid(), aContext.HasThreadIdentification(), aContext.HasProgramCounter(), pc, KFormatPrintfUnicode, buf.PtrZ(), buf.Size());
   299 		#endif //_UNICODE
   300 		}
   301 	return EFalse;
   302 	};
   303 
   304 
   305 /**
   306 Prints a string by outputting a trace packet with the format id KFormatPrintf
   307 
   308 If the specified string is too long to fit into a single trace packet
   309 a multipart trace is generated.
   310 
   311 @param aContext 	The trace packet context. @see TTraceContext
   312 @param aDes			The string. This must not be longer than 256 characters.
   313 
   314 @return 			The trace packet was/was not output.
   315 
   316 @See BTrace::TMultipart
   317 */
   318 EXPORT_C TBool Print(const TTraceContext& aContext, const TDesC16& aDes)
   319 	{
   320 	if(WouldBeTracedNow(aContext))
   321 		{
   322 		GET_PC(pc);
   323 		return UTRACE_SECONDARY_ANY(aContext.Classification(), aContext.ModuleUid(), aContext.HasThreadIdentification(), aContext.HasProgramCounter(), pc, KFormatPrintfUnicode, aDes.Ptr(), aDes.Size());
   324 		}
   325 	return EFalse;
   326 	}
   327 #endif // __KERNEL_MODE__
   328 
   329 
   330 // --------- trace ------------
   331 
   332 /**
   333 Outputs a trace packet containing no payload data.
   334 
   335 @param aContext 	The trace packet context. @see TTraceContext
   336 @param aFormatId	A format identifier as specified by @see TFormatId
   337 
   338 @return		 		The trace packet was/was not output.
   339 */
   340 EXPORT_C TBool Trace(const TTraceContext& aContext, TFormatId aFormatId)
   341 	{
   342 	GET_PC(pc);
   343 	return UTRACE_SECONDARY_0(aContext.Classification(),aContext.ModuleUid(),aContext.HasThreadIdentification(),aContext.HasProgramCounter(), pc, aFormatId);
   344 	}
   345 
   346 /**
   347 Outputs a trace packet containing 4 bytes of data.
   348 
   349 @param aContext 	The trace packet context. @see TTraceContext
   350 @param aFormatId	A format identifier as specified by @see TFormatId
   351 @param aData		4 bytes of data
   352 
   353 @return 		The trace packet was/was not output.
   354 */
   355 EXPORT_C TBool Trace(const TTraceContext& aContext, TFormatId aFormatId, TUint32 aData)
   356 	{
   357 	GET_PC(pc);
   358 	return UTRACE_SECONDARY_1(aContext.Classification(),aContext.ModuleUid(),aContext.HasThreadIdentification(),aContext.HasProgramCounter(),pc,aFormatId,aData);
   359 	}
   360 
   361 /**
   362 Outputs a trace packet containing 8 bytes of data.
   363 
   364 @param aContext 	The trace packet context. @see TTraceContext
   365 @param aFormatId	A format identifier as specified by @see TFormatId
   366 @param aData1		4 bytes of data
   367 @param aData2		4 bytes of data
   368 
   369 @return 		The trace packet was/was not output.
   370 */
   371 EXPORT_C TBool Trace(const TTraceContext& aContext, TFormatId aFormatId, TUint32 aData1, TUint32 aData2)
   372 	{
   373 	GET_PC(pc);
   374 	TUint32 packet[2];
   375 	packet[0] = aData1;
   376 	packet[1] = aData2;
   377 	return UTRACE_SECONDARY_ANY(aContext.Classification(), aContext.ModuleUid(), aContext.HasThreadIdentification(), aContext.HasProgramCounter(), pc, aFormatId, &packet, 8);
   378 	}
   379 
   380 /**
   381 Outputs a trace packet containing variable length data.
   382 
   383 If the specified data is too big to fit into a single
   384 trace packet a multipart trace is generated.
   385 
   386 
   387 @param aContext 	The trace packet context. @see TTraceContext
   388 @param aFormatId	A format identifier as specified by @see TFormatId
   389 @param aData		Address of additional data to add to trace packet.
   390 					Must be word aligned, i.e. a multiple of 4.
   391 @param aSize		Number of bytes of additional data.
   392 
   393 @return 			The trace packet was/was not output.
   394 
   395 @See BTrace::TMultipart
   396 */
   397 EXPORT_C TBool Trace(const TTraceContext& aContext, TFormatId aFormatId, const TAny* aData, TInt aSize)
   398 	{
   399 	if(WouldBeTracedNow(aContext))
   400 		{
   401 		GET_PC(pc);
   402 		return UTRACE_SECONDARY_ANY(aContext.Classification(), aContext.ModuleUid(), aContext.HasThreadIdentification(), aContext.HasProgramCounter(), pc, aFormatId, aData, aSize);
   403 		}
   404 	return EFalse;
   405 	}
   406 
   407 /**
   408 Outputs a trace packet containing 4 bytes of data.
   409 
   410 @param aContext 	The trace packet context. @see TTraceContext
   411 @param aFormatId	A format identifier as specified by @see TFormatId
   412 @param aData		4 bytes of data
   413 
   414 @return 		The trace packet was/was not output.
   415 */
   416 EXPORT_C TBool Trace(const TTraceContext& aContext, const TFormatId aFormatId, const TUint16 aData)
   417 	{
   418 	GET_PC(pc);
   419 	return UTRACE_SECONDARY_1(aContext.Classification(),aContext.ModuleUid(),aContext.HasThreadIdentification(),aContext.HasProgramCounter(),pc,aFormatId,aData);
   420 	}
   421 
   422 /**
   423 Outputs a trace packet containing 4 bytes of data.
   424 
   425 @param aContext 	The trace packet context. @see TTraceContext
   426 @param aFormatId	A format identifier as specified by @see TFormatId
   427 @param aData		4 bytes of data
   428 
   429 @return 		The trace packet was/was not output.
   430 */
   431 EXPORT_C TBool Trace(const TTraceContext& aContext, const TFormatId aFormatId, const TUint8 aData)
   432 	{
   433 	GET_PC(pc);
   434 	return UTRACE_SECONDARY_1(aContext.Classification(),aContext.ModuleUid(),aContext.HasThreadIdentification(),aContext.HasProgramCounter(),pc,aFormatId,aData);
   435 	}
   436 
   437 #ifndef __KERNEL_MODE__
   438 /**
   439 Outputs a trace packet containing variable length data.
   440 
   441 If the specified data is too big to fit into a single
   442 trace record a multipart trace is generated.
   443 
   444 @param aContext 	Attributes of the trace point.
   445 @param aFormatId	A format identifier as specified by @see TFormatId
   446 @param aData		Additional data to add to trace packet.
   447 					Must be word aligned, i.e. a multiple of 4.
   448 
   449 @return 			The trace packet was/was not logged.
   450 
   451 @See BTrace::TMultipart
   452 */
   453 EXPORT_C TBool Trace(const TTraceContext& aContext, const TFormatId aFormatId, const TDesC16& aData)
   454 	{
   455 	if(WouldBeTracedNow(aContext))
   456 		{
   457 		GET_PC(pc);
   458 		return UTRACE_SECONDARY_ANY(aContext.Classification(), aContext.ModuleUid(), aContext.HasThreadIdentification(), aContext.HasProgramCounter(), pc, aFormatId, aData.Ptr(), aData.Size());
   459 		}
   460 	return EFalse;
   461 	}
   462 #endif //__KERNEL_MODE__
   463 
   464 /**
   465 Outputs a trace packet containing variable length data.
   466 
   467 If the specified data is too big to fit into a single
   468 trace record a multipart trace is generated.
   469 
   470 @param aContext 	Attributes of the trace point.
   471 @param aFormatId	A format identifier as specified by @see TFormatId
   472 @param aData		Additional data to add to trace packet.
   473 					Must be word aligned, i.e. a multiple of 4.
   474 
   475 @return 			The trace packet was/was not logged.
   476 
   477 @See BTrace::TMultipart
   478 */
   479 EXPORT_C TBool Trace(const TTraceContext& aContext, const TFormatId aFormatId, const TDesC8& aData)
   480 	{
   481 	if(WouldBeTracedNow(aContext))
   482 		{
   483 		GET_PC(pc);
   484 		return UTRACE_SECONDARY_ANY(aContext.Classification(), aContext.ModuleUid(), aContext.HasThreadIdentification(), aContext.HasProgramCounter(), pc, aFormatId, aData.Ptr(), aData.Size());
   485 		}
   486 	return EFalse;
   487 	}
   488 
   489 
   490 
   491 /**
   492  * Check whether a trace packet would be traced or not.
   493  *
   494  * @param aContext The context of the trace packet(s) to be checked.
   495  * @return Returns whether the trace packet would be traced or not.
   496  * Note: The value should never be stored since the filters can be changed without warning.
   497  */
   498 EXPORT_C TBool WouldBeTracedNow(const TTraceContext& aContext)
   499 	{
   500 	return BTrace::CheckFilter2(aContext.Classification(), aContext.ModuleUid());
   501 	};
   502 
   503 //--------------------- UTrace compiled out ------------------------
   504 
   505 #else //SYMBIAN_TRACE_EXECUTABLE_IS_INCLUDED
   506 
   507 //--------printf
   508 EXPORT_C TBool Printf(const TTraceContext&, const char*, ...) { return EFalse; }
   509 EXPORT_C TBool Print(const TTraceContext&, const TDesC8&) { return EFalse; }
   510 EXPORT_C TBool Printf(const TTraceContext&, TRefByValue<const TDesC8> ,...) { return EFalse; }
   511 #ifndef  __KERNEL_MODE__
   512 EXPORT_C TBool Printf(const TTraceContext&, TRefByValue<const TDesC16>,...) { return EFalse; }
   513 EXPORT_C TBool Print(const TTraceContext&, const TDesC16&) { return EFalse; }
   514 #endif //__KERNEL_MODE__
   515 
   516 //--------trace
   517 EXPORT_C TBool Trace(const TTraceContext&, TFormatId) { return EFalse; }
   518 EXPORT_C TBool Trace(const TTraceContext&, TFormatId, TUint32) { return EFalse; }
   519 EXPORT_C TBool Trace(const TTraceContext&, TFormatId, TUint32, TUint32) { return EFalse; }
   520 EXPORT_C TBool Trace(const TTraceContext&, TFormatId, const TAny*, TInt) { return EFalse; }
   521 
   522 EXPORT_C TBool Trace(const TTraceContext&, const TFormatId, const TUint8) { return EFalse; }
   523 EXPORT_C TBool Trace(const TTraceContext&, const TFormatId, const TUint16) { return EFalse; }
   524 EXPORT_C TBool Trace(const TTraceContext&, const TFormatId, const TDesC8&) { return EFalse; }
   525 #ifndef __KERNEL_MODE__
   526 EXPORT_C TBool Trace(const TTraceContext&, const TFormatId, const TDesC16&) { return EFalse; }
   527 #endif
   528 EXPORT_C TBool WouldBeTracedNow(const TTraceContext&) { return EFalse; }
   529 
   530 
   531 #endif //SYMBIAN_TRACE_EXECUTABLE_IS_INCLUDED
   532 
   533 }//namespace UTF
   534 
   535