Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed by
DebugTracer.h@4:5d1359a283bc, 2008-11-27 (annotated)
- Committer:
- simon.ford@mbed.co.uk
- Date:
- Thu Nov 27 16:23:24 2008 +0000
- Revision:
- 4:5d1359a283bc
- Parent:
- 1:6b7f447ca868
New version of framework: vectors, environment, platform, base and file system
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| simon.ford@mbed.co.uk | 0:82220227f4fa | 1 | /* mbed Microcontroller Library - DebugTracer |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 2 | * Copyright (c) 2007-2008, sford |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 3 | */ |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 4 | |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 5 | #ifndef MBED_DEBUGTRACER_H |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 6 | #define MBED_DEBUGTRACER_H |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 7 | |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 8 | #include "Base.h" |
| simon.ford@mbed.co.uk | 4:5d1359a283bc | 9 | #include <cstdio> |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 10 | |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 11 | #define MAX_TRACER_DEPTH 32 |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 12 | |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 13 | namespace mbed { |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 14 | |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 15 | //#define METHOD(name) |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 16 | //#define FUNCTION(name) |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 17 | #define METHOD(name) DebugTracer _tracer_instance(name, this) |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 18 | #define FUNCTION(name) DebugTracer _tracer_instance(name) |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 19 | |
| simon.ford@mbed.co.uk | 4:5d1359a283bc | 20 | /* Class DebugTracer |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 21 | * A code instrumentation utility |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 22 | * |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 23 | * This object adds a function name or methodname/object instance |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 24 | * to a stack on construction, and removes it on destruvtion. It |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 25 | * can therefore be used at the start of functions to trace entry, exit |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 26 | * and the call graph/stack. |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 27 | * |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 28 | * Wrapped in macros and with altered implementations, the same instrumentation can: |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 29 | * - be #defined away to nothing |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 30 | * - provide the call stack on encountering an error |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 31 | * - trace the runtime call graph (for the whole program run, or programatically on/off) |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 32 | * |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 33 | * |
| simon.ford@mbed.co.uk | 4:5d1359a283bc | 34 | * Example |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 35 | * |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 36 | * Function example |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 37 | * > void foo(int x) { FUNCTION("foo"); |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 38 | * > // normal code |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 39 | * |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 40 | * Class method example |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 41 | * > void Foo::bar(int x) { METHOD("bar"); |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 42 | * > // normal code |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 43 | */ |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 44 | class DebugTracer { |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 45 | |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 46 | public: |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 47 | |
| simon.ford@mbed.co.uk | 4:5d1359a283bc | 48 | /* Constructor DebugTracer |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 49 | * Record the method and object instance it is called on |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 50 | * to the call stack |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 51 | */ |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 52 | DebugTracer(char* method, Base* object = 0) { |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 53 | _functions[_depth] = method; |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 54 | _objects[_depth] = object; |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 55 | _depth++; |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 56 | } |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 57 | |
| simon.ford@mbed.co.uk | 4:5d1359a283bc | 58 | /* Destructor ~DebugTracer |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 59 | * Pop from the call stack |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 60 | */ |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 61 | ~DebugTracer() { |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 62 | _depth--; |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 63 | } |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 64 | |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 65 | static void stack() { |
| simon.ford@mbed.co.uk | 4:5d1359a283bc | 66 | std::fprintf(stderr, "Trace (depth = %d):\n", _depth); |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 67 | for(int i=0; i<_depth; i++) { |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 68 | // indent |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 69 | for(int j=0; j<i; j++) { |
| simon.ford@mbed.co.uk | 4:5d1359a283bc | 70 | std::fprintf(stderr, " "); |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 71 | } |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 72 | if(_objects[i]) { |
| simon.ford@mbed.co.uk | 4:5d1359a283bc | 73 | // std::fprintf(stderr, "%s::", _objects[i]->type()); |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 74 | } |
| simon.ford@mbed.co.uk | 4:5d1359a283bc | 75 | std::fprintf(stderr, "%s\n", _functions[i]); |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 76 | } |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 77 | } |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 78 | |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 79 | static int enabled; |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 80 | |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 81 | protected: |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 82 | |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 83 | static Base* _objects[MAX_TRACER_DEPTH]; |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 84 | static char* _functions[MAX_TRACER_DEPTH]; |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 85 | static int _depth; |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 86 | |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 87 | }; |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 88 | |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 89 | } // namespace mbed |
| simon.ford@mbed.co.uk | 0:82220227f4fa | 90 | |
| simon.ford@mbed.co.uk | 1:6b7f447ca868 | 91 | #endif |
| simon.ford@mbed.co.uk | 1:6b7f447ca868 | 92 |
