1 /* //device/tools/dmtracedump/CreateTrace.c
3 ** Copyright 2006, The Android Open Source Project
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
9 ** http://www.apache.org/licenses/LICENSE-2.0
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
19 * Create a test file in the format required by dmtrace.
22 #include "Profile.h" // from VM header
36 * Values from the header of the data file.
38 typedef struct DataHeader {
46 int versionNumber = VERSION;
49 DataHeader header = { 0x574f4c53, VERSION, sizeof(DataHeader), 0LL };
51 char *versionHeader = "*version\n";
52 char *clockDef = "clock=thread-cpu\n";
61 char *keyEnd = "*end\n";
63 typedef struct dataRecord {
66 unsigned int action; /* 0=entry, 1=exit, 2=exception exit */
71 unsigned int methodId;
79 typedef struct stack {
84 /* Mac OS doesn't have strndup(), so implement it here.
86 char *strndup(const char *src, size_t len)
88 char *dest = (char *) malloc(len + 1);
89 strncpy(dest, src, len);
95 * Parse the input file. It looks something like this:
96 * # This is a comment line
102 * where the first column is the time, the second column is the thread id,
103 * and the third column is the method (actually just the class name). The
104 * number of spaces between the 2nd and 3rd columns is the indentation and
105 * determines the call stack. Each called method must be indented by one
106 * more space. In the example above, A is called at time 4, A calls B at
107 * time 6, B returns at time 8, and A returns at time 10. Thread 1 is the
108 * only thread that is running.
110 * An alternative file format leaves out the first two columns:
116 * In this file format, the thread id is always 1, and the time starts at
117 * 2 and increments by 2 for each line.
119 void parseInputFile(const char *inputFileName)
121 unsigned int time = 0, threadId;
128 FILE *inputFp = fopen(inputFileName, "r");
129 if (inputFp == NULL) {
130 perror(inputFileName);
134 /* Count the number of lines in the buffer */
139 while (fgets(buf, BUF_SIZE, inputFp)) {
145 int time = strtoul(cp, &cp, 0);
148 int threadId = strtoul(cp, &cp, 0);
149 if (maxThreadId < threadId)
150 maxThreadId = threadId;
153 while (isspace(*indentEnd))
155 if (indentEnd - cp + 1 > maxFrames)
156 maxFrames = indentEnd - cp + 1;
158 int numThreads = maxThreadId + 1;
160 /* Add space for a sentinel record at the end */
162 records = (dataRecord *) malloc(sizeof(dataRecord) * numRecords);
163 callStack = (stack *) malloc(sizeof(stack) * numThreads);
165 for (ii = 0; ii < numThreads; ++ii) {
166 callStack[ii].frames = NULL;
167 callStack[ii].indentLevel = 0;
171 while (fgets(buf, BUF_SIZE, inputFp)) {
179 /* Skip lines that start with '#' */
183 /* Get time and thread id */
185 /* If the line does not begin with a digit, then fill in
186 * default values for the time and threadId.
191 time = strtoul(cp, &cp, 0);
194 threadId = strtoul(cp, &cp, 0);
198 // Allocate space for the thread stack, if necessary
199 if (callStack[threadId].frames == NULL) {
201 stk = (dataRecord **) malloc(sizeof(dataRecord *) * maxFrames);
202 callStack[threadId].frames = stk;
204 indentLevel = callStack[threadId].indentLevel;
207 while (isspace(*cp)) {
210 indent = cp - save_cp + 1;
211 records[nextRecord].time = time;
212 records[nextRecord].threadId = threadId;
218 /* Remove trailing spaces */
224 records[nextRecord].fullName = strndup(save_cp, len);
226 /* Parse the name to support "class.method signature" */
227 records[nextRecord].className = NULL;
228 records[nextRecord].methodName = NULL;
229 records[nextRecord].signature = NULL;
230 cp = strchr(save_cp, '.');
234 records[nextRecord].className = strndup(save_cp, len);
236 cp = strchr(save_cp, ' ');
238 cp = strchr(save_cp, '\n');
239 if (cp && cp > save_cp) {
241 records[nextRecord].methodName = strndup(save_cp, len);
243 cp = strchr(save_cp, ' ');
245 cp = strchr(save_cp, '\n');
246 if (cp && cp > save_cp) {
248 records[nextRecord].signature = strndup(save_cp, len);
254 printf("Indent: %d; IndentLevel: %d; Line: %s", indent, indentLevel, buf);
258 if (indent == indentLevel + 1) { // Entering a method
260 printf(" Entering %s\n", records[nextRecord].fullName);
261 callStack[threadId].frames[indentLevel] = &records[nextRecord];
262 } else if (indent == indentLevel) { // Exiting a method
263 // Exiting method must be currently on top of stack (unless stack is empty)
264 if (callStack[threadId].frames[indentLevel - 1] == NULL) {
266 printf(" Exiting %s (past bottom of stack)\n", records[nextRecord].fullName);
267 callStack[threadId].frames[indentLevel - 1] = &records[nextRecord];
270 if (indentLevel < 1) {
271 fprintf(stderr, "Error: line %d: %s", linenum, buf);
272 fprintf(stderr, " expected positive (>0) indentation, found %d\n",
276 char *name = callStack[threadId].frames[indentLevel - 1]->fullName;
277 if (strcmp(name, records[nextRecord].fullName) == 0) {
279 printf(" Exiting %s\n", name);
281 } else { // exiting method doesn't match stack's top method
282 fprintf(stderr, "Error: line %d: %s", linenum, buf);
283 fprintf(stderr, " expected exit from %s\n",
284 callStack[threadId].frames[indentLevel - 1]->fullName);
289 if (nextRecord != 0) {
290 fprintf(stderr, "Error: line %d: %s", linenum, buf);
291 fprintf(stderr, " expected indentation %d [+1], found %d\n",
292 indentLevel, indent);
297 printf(" Nonzero indent at first record\n");
298 printf(" Entering %s\n", records[nextRecord].fullName);
301 // This is the first line of data, so we allow a larger
302 // initial indent. This allows us to test popping off more
303 // frames than we entered.
304 indentLevel = indent - 1;
305 callStack[threadId].frames[indentLevel] = &records[nextRecord];
312 records[nextRecord].action = action;
313 callStack[threadId].indentLevel = indentLevel;
318 /* Mark the last record with a sentinel */
319 memset(&records[nextRecord], 0, sizeof(dataRecord));
324 * Write values to the binary data file.
326 void write2LE(FILE* fp, unsigned short val)
328 putc(val & 0xff, fp);
332 void write4LE(FILE* fp, unsigned int val)
334 putc(val & 0xff, fp);
335 putc((val >> 8) & 0xff, fp);
336 putc((val >> 16) & 0xff, fp);
337 putc((val >> 24) & 0xff, fp);
340 void write8LE(FILE* fp, unsigned long long val)
342 putc(val & 0xff, fp);
343 putc((val >> 8) & 0xff, fp);
344 putc((val >> 16) & 0xff, fp);
345 putc((val >> 24) & 0xff, fp);
346 putc((val >> 32) & 0xff, fp);
347 putc((val >> 40) & 0xff, fp);
348 putc((val >> 48) & 0xff, fp);
349 putc((val >> 56) & 0xff, fp);
352 void writeDataRecord(FILE *dataFp, int threadId, unsigned int methodVal,
353 unsigned int elapsedTime)
355 if (versionNumber == 1)
356 putc(threadId, dataFp);
358 write2LE(dataFp, threadId);
359 write4LE(dataFp, methodVal);
360 write4LE(dataFp, elapsedTime);
363 void writeDataHeader(FILE *dataFp)
368 gettimeofday(&tv, &tz);
369 unsigned long long startTime = tv.tv_sec;
370 startTime = (startTime << 32) | tv.tv_usec;
371 header.version = versionNumber;
372 write4LE(dataFp, header.magic);
373 write2LE(dataFp, header.version);
374 write2LE(dataFp, header.offsetToData);
375 write8LE(dataFp, startTime);
378 void writeKeyMethods(FILE *keyFp)
380 dataRecord *pRecord, *pNext;
381 char *methodStr = "*methods\n";
382 fwrite(methodStr, strlen(methodStr), 1, keyFp);
384 /* Assign method ids in multiples of 4 */
385 unsigned int methodId = 0;
386 for (pRecord = records; pRecord->fullName; ++pRecord) {
387 if (pRecord->methodId)
389 unsigned int id = ++methodId << 2;
390 pRecord->methodId = id;
392 /* Assign this id to all the other records that have the
395 for (pNext = pRecord + 1; pNext->fullName; ++pNext) {
398 if (strcmp(pRecord->fullName, pNext->fullName) == 0)
399 pNext->methodId = id;
401 if (pRecord->className == NULL || pRecord->methodName == NULL) {
402 fprintf(keyFp, "0x%x %s m ()\n",
403 pRecord->methodId, pRecord->fullName);
404 } else if (pRecord->signature == NULL) {
405 fprintf(keyFp, "0x%x %s %s ()\n",
406 pRecord->methodId, pRecord->className,
407 pRecord->methodName);
409 fprintf(keyFp, "0x%x %s %s %s\n",
410 pRecord->methodId, pRecord->className,
411 pRecord->methodName, pRecord->signature);
416 void writeKeys(FILE *keyFp)
418 fprintf(keyFp, "%s%d\n%s", versionHeader, versionNumber, clockDef);
419 fwrite(keyThreads, strlen(keyThreads), 1, keyFp);
420 writeKeyMethods(keyFp);
421 fwrite(keyEnd, strlen(keyEnd), 1, keyFp);
424 void writeDataRecords(FILE *dataFp)
428 for (pRecord = records; pRecord->fullName; ++pRecord) {
429 unsigned int val = METHOD_COMBINE(pRecord->methodId, pRecord->action);
430 writeDataRecord(dataFp, pRecord->threadId, val, pRecord->time);
434 void writeTrace(const char* traceFileName)
436 FILE *fp = fopen(traceFileName, "w");
438 perror(traceFileName);
443 writeDataRecords(fp);
447 int parseOptions(int argc, char **argv)
451 int opt = getopt(argc, argv, "v:d");
456 versionNumber = strtoul(optarg, NULL, 0);
457 if (versionNumber != 1 && versionNumber != 2) {
458 fprintf(stderr, "Error: version number (%d) must be 1 or 2\n",
474 int main(int argc, char** argv)
477 char *traceFileName = NULL;
480 if (parseOptions(argc, argv) || argc - optind != 2) {
481 fprintf(stderr, "Usage: %s [-v version] [-d] input_file trace_prefix\n",
486 inputFile = argv[optind++];
487 parseInputFile(inputFile);
488 traceFileName = argv[optind++];
490 writeTrace(traceFileName);