Fork of the official mbed C/C++ SDK provides the software platform and libraries to build your applications. The fork has the documentation converted to Doxygen format

Dependents:   NervousPuppySprintOne NervousPuppySprint2602 Robot WarehouseBot1 ... more

Fork of mbed by mbed official

Committer:
simon.ford@mbed.co.uk
Date:
Tue Apr 08 14:12:21 2008 +0000
Revision:
0:82220227f4fa
Child:
1:6b7f447ca868
A first develoment release of the mbed libraries

Who changed what in which revision?

UserRevisionLine numberNew 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 0:82220227f4fa 9 #include "stdio.h"
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 0:82220227f4fa 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 0:82220227f4fa 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 0:82220227f4fa 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 0:82220227f4fa 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 0:82220227f4fa 66 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 0:82220227f4fa 70 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 0:82220227f4fa 73 fprintf(stderr, "%s::", _objects[i]->type());
simon.ford@mbed.co.uk 0:82220227f4fa 74 }
simon.ford@mbed.co.uk 0:82220227f4fa 75 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 0:82220227f4fa 91 #endif