Update contrib.
1 // Copyright (c) 2005-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32utils\trace\btrace.cpp
20 #include <d32btrace.h>
22 CConsoleBase* Console;
26 #define ASSERT(c) (void)((c)||(AssertFailed(__LINE__)))
28 TInt AssertFailed(TInt aLine)
30 _LIT(KPanicCategory,"ASSERT");
31 User::Panic(KPanicCategory,aLine);
36 int strlen(const char* string)
39 while(string[len]) ++len;
44 class TDesTruncate : public TDes8Overflow { void Overflow(TDes8&) {}; } IgnoreOverflow;
46 void printf(const char* aFormat,...)
48 TPtrC8 formatDes((TUint8*)aFormat,strlen(aFormat));
50 VA_START(list, aFormat);
52 // coverity[uninit_use_in_call]
53 ((TDes8&)buffer).AppendFormatList(formatDes, list, &IgnoreOverflow);
54 Console->Write(((TDes8&)buffer).Expand());
60 TRequestStatus keyStat;
61 Console->Read(keyStat);
62 User::WaitForRequest(keyStat);
63 return Console->KeyCode();
67 TInt getch(TInt aTimeout)
69 TRequestStatus keyStat;
70 Console->Read(keyStat);
72 TInt r=timer.CreateLocal();
74 TRequestStatus timerStat;
75 timer.After(timerStat,aTimeout*1000000);
76 User::WaitForRequest(timerStat,keyStat);
78 if(keyStat!=KRequestPending)
79 key = Console->KeyCode();
81 Console->ReadCancel();
82 User::WaitForAnyRequest();
87 int Main(int argc, char** argv);
90 void exit_btrace(int result)
99 TFileName exeFile = RProcess().FileName();
100 TRAPD(r, Console = Console::NewL(exeFile,TSize(KConsFullScreen,KConsFullScreen)));
103 // get command-line...
105 ASSERT(clDes.Create(User::CommandLineLength()+1)==KErrNone);
106 User::CommandLine(clDes);
107 char* cl = (char*)clDes.Collapse().PtrZ(); // convert to null terminated C string
110 RPointerArray<TAny> argArray;
111 ASSERT(KErrNone==argArray.Append(exeFile.Collapse().Ptr())); // first arg is program name
114 while((unsigned)(*cl-1)<(unsigned)' ') ++cl; // skip whitespace
116 ASSERT(KErrNone==argArray.Append(cl));
117 while(*++cl>' ') {}; // skip non-whitespace
119 *cl++ = 0; // add null terminator to arg
123 return Main(argArray.Count(),(char**)&argArray[0]);
129 getch(5); // pause for a while
136 char* PrimaryFilterArg = NULL;
137 bool SetFilter2 = false;
138 RArray<TUint32> Filter2;
139 bool SetMode = false;
141 bool SetBufferSize = false;
142 unsigned BufferSize = 0;
143 bool DumpToDebugPort = false;
144 TInt AnalysisLevel = 0;
145 bool Analyse = false;
146 bool DiscardOldData = true;
154 TUint oldMode = Trace.Mode();
155 Trace.SetMode(0); // turn off trace capture while we dump
159 while((size=Trace.GetData(data))!=0)
163 TInt r=File.Write(TPtrC8(data,size));
166 printf("Error writing to file (1). (Code %d)",r);
176 RDebug::RawPrint(TPtrC8(data,s));
184 // Flush the file here so we can be sure to detect whether the btrace data
185 // has been written successfully to the file.
188 // Flush the file here so we can be sure to detect whether the btrace data
189 // has been written successfully to the file.
190 TInt r = File.Flush();
193 printf("Error writing to file (2). (Code %d)",r);
199 Trace.SetMode(oldMode);
203 bool SetFilter(char* args);
204 void DoAnalyse(TInt aAnalysisLevel);
210 Trace.ResizeBuffer(BufferSize*1024);
215 if (PrimaryFilterArg)
216 SetFilter(PrimaryFilterArg);
220 TInt r = Trace.SetFilter2(&Filter2[0], Filter2.Count());
223 printf("Error setting secondary filter. (%d)",r);
227 Trace.Empty(); // discard old data
230 if(FileName || DumpToDebugPort)
234 DoAnalyse(AnalysisLevel);
240 unsigned int ParseDecimal(char*& args)
244 while((d=*args-'0')<10u)
253 bool SetFilter(char* args)
256 // Turn everything off and discard old data
259 Trace.SetFilter(i,0);
264 // Iterate over comma-seperated filter numbers
265 bool set_metatrace = false;
266 while((unsigned)(*args-'0')<10u)
268 unsigned int i = ParseDecimal(args);
271 printf("Primary filter value out of range");
276 set_metatrace = true;
277 Trace.SetFilter(BTrace::EMetaTrace,1);
279 Trace.SetFilter(i,1);
287 bool ParseFilter2(char* args)
289 while((unsigned)(*args-'0') < 10u)
291 unsigned int i = ParseDecimal(args);
292 TInt r = Filter2.Append(i);
295 printf("Error parsing secondary filter. (%d)",r);
303 return SetFilter2=(*args==0);
307 bool ParseMode(char* args)
309 Mode = ParseDecimal(args);
310 return SetMode=(*args==0);
314 bool ParseBufferSize(char* args)
316 BufferSize = ParseDecimal(args);
317 return SetBufferSize=(*args==0);
321 bool ParseAnalyse(char* args)
323 AnalysisLevel = ParseDecimal(args);
324 return Analyse=(*args==0);
328 int CreateFile(char* name)
332 printf("Too many arguments");
337 unsigned nameLen = strlen(name);
338 if(nameLen>(unsigned)KMaxFileName)
340 printf("File name too long.\n");
350 printf("Couldn't connect to file server. (%d)\n",r);
355 TBuf8<KMaxFileName*2> fn = TPtrC8((TUint8*)name,nameLen);
356 r = File.Replace(Fs,fn.Expand(),EFileWrite);
359 printf("Couldn't create file: %s. (%d)\n",name,r);
369 printf("Usage: BTRACE [options] [filename]\n");
371 printf("Options:\n");
372 printf("-fLIST Set primary filter to a LIST of comma separated category numbers.\n");
373 printf(" This argument may be used more than once e.g. -f1,22,3 -f44 \n");
374 printf("-sLIST Set secondary filter to a LIST of comma separated UID values.\n");
375 printf(" This argument may be used more than once e.g. -s1221,22,343243 -s3242344 \n");
376 printf("-mN Set capture mode to value N (See RBTrace::TMode)\n");
377 printf("-bN Set capture buffer size to N kBytes\n");
378 printf("-d Dump contents of trace buffer to debug port\n");
379 printf("-tsNAME Output a test measurement start trace with text NAME. This text\n");
380 printf(" may be between 0 and 80 non-whitespace characters.\n");
381 printf("-te Output a test measurement end trace\n");
382 printf("-aLEVEL Analyse trace buffer and produce a report. UNSUPPORTED!\n");
383 printf("-k Keep old contents of trace buffer when enabling a new filter\n");
384 printf("filename File to dump contents of trace buffer to.\n");
390 int Main(int argc, char** argv)
395 TInt r = Trace.Open();
398 printf("Couldn't open BTrace driver. (Code %d)",r);
416 case 'f': PrimaryFilterArg = a+2; r = true; break;
417 case 's': r=ParseFilter2(a+2); break;
418 case 'm': r=ParseMode(a+2); break;
419 case 'b': r=ParseBufferSize(a+2); break;
420 case 'd': r=a[2]==0; DumpToDebugPort = true; break;
426 if (cch > KMaxBTraceDataArray)
427 cch = KMaxBTraceDataArray;
428 int buff[KMaxBTraceDataArray/sizeof(int)];
430 memcpy(buff, a, cch);
431 BTraceContextN(BTrace::EMetaTrace, BTrace::EMetaTraceMeasurementStart, 0,0, buff, cch);
436 BTraceContext8(BTrace::EMetaTrace, BTrace::EMetaTraceMeasurementEnd, 0,0);
441 case 'a': r=ParseAnalyse(a+2); break;
442 case 'k': DiscardOldData = false; r = true; break;
448 printf("Bad command line argument: %s",a);