blob: 35ee61c3aeb3171519b4f09a590e05c255b1b71d [file] [log] [blame]
The Android Open Source Project52d4c302009-03-03 19:29:09 -08001// Copyright 2006 The Android Open Source Project
2
3#include <stdio.h>
4#include <unistd.h>
5#include <stdlib.h>
6#include <inttypes.h>
7#include <assert.h>
8#include "trace_reader.h"
9#include "bitvector.h"
10#include "parse_options.h"
11#include "armdis.h"
12
13typedef TraceReader<> TraceReaderType;
14
15#include "parse_options-inl.h"
16#include "callstack.h"
17
Jack Veenstraad6f2532009-05-09 11:44:53 -070018static uint64_t debugTime;
19static uint64_t dumpTime = 0;
20
The Android Open Source Project52d4c302009-03-03 19:29:09 -080021class MyFrame : public StackFrame<symbol_type> {
22 public:
23 void push(int stackLevel, uint64_t time, CallStackBase *base);
24 void pop(int stackLevel, uint64_t time, CallStackBase *base);
25};
26
27typedef CallStack<MyFrame> CallStackType;
28
29void MyFrame::push(int stackLevel, uint64_t time, CallStackBase *base)
30{
Jack Veenstraad6f2532009-05-09 11:44:53 -070031 if (dumpTime > 0)
32 return;
The Android Open Source Project52d4c302009-03-03 19:29:09 -080033 printf("%llu en thr %d %3d", time, base->getId(), stackLevel);
34 for (int ii = 0; ii < stackLevel; ++ii)
35 printf(".");
36 printf(" 0x%08x %s\n", addr, function->name);
37}
38
39void MyFrame::pop(int stackLevel, uint64_t time, CallStackBase *base)
40{
Jack Veenstraad6f2532009-05-09 11:44:53 -070041 if (dumpTime > 0)
42 return;
The Android Open Source Project52d4c302009-03-03 19:29:09 -080043 printf("%llu x thr %d %3d", time, base->getId(), stackLevel);
44 for (int ii = 0; ii < stackLevel; ++ii)
45 printf(".");
46 printf(" 0x%08x %s\n", addr, function->name);
47}
48
49static const int kNumStackFrames = 500;
50static const int kMaxThreads = (32 * 1024);
51CallStackType *stacks[kMaxThreads];
52
The Android Open Source Project52d4c302009-03-03 19:29:09 -080053void Usage(const char *program)
54{
55 fprintf(stderr, "Usage: %s [options] trace_name elf_file\n",
56 program);
57 OptionsUsage();
58}
59
Jack Veenstraad6f2532009-05-09 11:44:53 -070060bool localParseOptions(int argc, char **argv)
61{
62 bool err = false;
63 while (!err) {
64 int opt = getopt(argc, argv, "+d:");
65 if (opt == -1)
66 break;
67 switch (opt) {
68 case 'd':
69 dumpTime = strtoull(optarg, NULL, 0);
70 break;
71 default:
72 err = true;
73 break;
74 }
75 }
76 return err;
77}
78
The Android Open Source Project52d4c302009-03-03 19:29:09 -080079int main(int argc, char **argv)
80{
81 ParseOptions(argc, argv);
Jack Veenstraad6f2532009-05-09 11:44:53 -070082 localParseOptions(argc, argv);
The Android Open Source Project52d4c302009-03-03 19:29:09 -080083 if (argc - optind != 2) {
84 Usage(argv[0]);
85 exit(1);
86 }
87
88 char *qemu_trace_file = argv[optind++];
89 char *elf_file = argv[optind++];
90 TraceReaderType *trace = new TraceReaderType;
91 trace->Open(qemu_trace_file);
92 trace->ReadKernelSymbols(elf_file);
93 trace->SetRoot(root);
The Android Open Source Project52d4c302009-03-03 19:29:09 -080094
95 BBEvent event;
96 while (1) {
97 BBEvent ignored;
98 symbol_type *function;
99
100 if (GetNextValidEvent(trace, &event, &ignored, &function))
101 break;
102 if (event.bb_num == 0)
103 break;
104
105 // Get the stack for the current thread
106 CallStackType *pStack = stacks[event.pid];
107
108 // If the stack does not exist, then allocate a new one.
109 if (pStack == NULL) {
110 pStack = new CallStackType(event.pid, kNumStackFrames, trace);
111 stacks[event.pid] = pStack;
112 }
113 if (debugTime != 0 && event.time >= debugTime)
114 printf("debug time: %lld\n", debugTime);
115
116 // Update the stack
117 pStack->updateStack(&event, function);
Jack Veenstraad6f2532009-05-09 11:44:53 -0700118
119 // If the user requested a stack dump at a certain time,
120 // and we are at that time, then dump the stack and exit.
121 if (dumpTime > 0 && event.time >= dumpTime) {
122 pStack->showStack(stdout);
123 break;
124 }
The Android Open Source Project52d4c302009-03-03 19:29:09 -0800125 }
126
127 for (int ii = 0; ii < kMaxThreads; ++ii) {
128 if (stacks[ii])
129 stacks[ii]->popAll(event.time);
130 }
131
132 delete trace;
133 return 0;
134}