The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 1 | // 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 | |
| 13 | typedef TraceReader<> TraceReaderType; |
| 14 | |
| 15 | #include "parse_options-inl.h" |
| 16 | #include "callstack.h" |
| 17 | |
Jack Veenstra | ad6f253 | 2009-05-09 11:44:53 -0700 | [diff] [blame] | 18 | static uint64_t debugTime; |
| 19 | static uint64_t dumpTime = 0; |
| 20 | |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 21 | class 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 | |
| 27 | typedef CallStack<MyFrame> CallStackType; |
| 28 | |
| 29 | void MyFrame::push(int stackLevel, uint64_t time, CallStackBase *base) |
| 30 | { |
Jack Veenstra | ad6f253 | 2009-05-09 11:44:53 -0700 | [diff] [blame] | 31 | if (dumpTime > 0) |
| 32 | return; |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 33 | 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 | |
| 39 | void MyFrame::pop(int stackLevel, uint64_t time, CallStackBase *base) |
| 40 | { |
Jack Veenstra | ad6f253 | 2009-05-09 11:44:53 -0700 | [diff] [blame] | 41 | if (dumpTime > 0) |
| 42 | return; |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 43 | 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 | |
| 49 | static const int kNumStackFrames = 500; |
| 50 | static const int kMaxThreads = (32 * 1024); |
| 51 | CallStackType *stacks[kMaxThreads]; |
| 52 | |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 53 | void Usage(const char *program) |
| 54 | { |
| 55 | fprintf(stderr, "Usage: %s [options] trace_name elf_file\n", |
| 56 | program); |
| 57 | OptionsUsage(); |
| 58 | } |
| 59 | |
Jack Veenstra | ad6f253 | 2009-05-09 11:44:53 -0700 | [diff] [blame] | 60 | bool 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 Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 79 | int main(int argc, char **argv) |
| 80 | { |
| 81 | ParseOptions(argc, argv); |
Jack Veenstra | ad6f253 | 2009-05-09 11:44:53 -0700 | [diff] [blame] | 82 | localParseOptions(argc, argv); |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 83 | 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 Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 94 | |
| 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 Veenstra | ad6f253 | 2009-05-09 11:44:53 -0700 | [diff] [blame] | 118 | |
| 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 Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 125 | } |
| 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 | } |