José Fonseca | 589082d | 2011-03-30 09:10:40 +0100 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
| 3 | * Copyright 2011 Jose Fonseca |
| 4 | * All Rights Reserved. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | * |
| 24 | **************************************************************************/ |
| 25 | |
José Fonseca | 589082d | 2011-03-30 09:10:40 +0100 | [diff] [blame] | 26 | |
José Fonseca | 8d53812 | 2011-06-09 00:29:19 +0100 | [diff] [blame] | 27 | #include <string.h> |
José Fonseca | 45b28c4 | 2011-05-05 01:08:00 +0100 | [diff] [blame] | 28 | #include <iostream> |
| 29 | |
José Fonseca | 32871ed | 2011-04-10 13:40:52 +0100 | [diff] [blame] | 30 | #include "retrace.hpp" |
José Fonseca | 589082d | 2011-03-30 09:10:40 +0100 | [diff] [blame] | 31 | |
José Fonseca | 589082d | 2011-03-30 09:10:40 +0100 | [diff] [blame] | 32 | |
José Fonseca | 32871ed | 2011-04-10 13:40:52 +0100 | [diff] [blame] | 33 | namespace retrace { |
| 34 | |
| 35 | |
| 36 | int verbosity = 0; |
| 37 | |
| 38 | |
José Fonseca | 8d53812 | 2011-06-09 00:29:19 +0100 | [diff] [blame] | 39 | void ignore(Trace::Call &call) { |
| 40 | (void)call; |
| 41 | } |
| 42 | |
José Fonseca | 45b28c4 | 2011-05-05 01:08:00 +0100 | [diff] [blame] | 43 | void retrace_unknown(Trace::Call &call) { |
| 44 | if (verbosity >= 0) { |
| 45 | std::cerr << call.no << ": warning: unknown call " << call.name() << "\n"; |
| 46 | } |
| 47 | } |
| 48 | |
José Fonseca | 2741ed8 | 2011-10-07 23:36:39 +0100 | [diff] [blame^] | 49 | inline void Retracer::addCallback(const Entry *entry) { |
| 50 | assert(entry->name); |
| 51 | assert(entry->callback); |
| 52 | map[entry->name] = entry->callback; |
| 53 | } |
José Fonseca | 8d53812 | 2011-06-09 00:29:19 +0100 | [diff] [blame] | 54 | |
José Fonseca | 2741ed8 | 2011-10-07 23:36:39 +0100 | [diff] [blame^] | 55 | |
| 56 | void Retracer::addCallbacks(const Entry *entries) { |
| 57 | while (entries->name && entries->callback) { |
| 58 | addCallback(entries++); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | |
| 63 | void Retracer::retrace(Trace::Call &call) { |
| 64 | Callback callback = 0; |
| 65 | |
| 66 | Trace::Id id = call.sig->id; |
| 67 | if (id >= callbacks.size()) { |
| 68 | callbacks.resize(id + 1); |
| 69 | callback = 0; |
| 70 | } else { |
| 71 | callback = callbacks[id]; |
José Fonseca | 8d53812 | 2011-06-09 00:29:19 +0100 | [diff] [blame] | 72 | } |
| 73 | |
José Fonseca | 2741ed8 | 2011-10-07 23:36:39 +0100 | [diff] [blame^] | 74 | if (!callback) { |
| 75 | Map::const_iterator it = map.find(call.name()); |
| 76 | if (it == map.end()) { |
| 77 | callback = &retrace_unknown; |
| 78 | } else { |
| 79 | callback = it->second; |
| 80 | } |
| 81 | callbacks[id] = callback; |
| 82 | } |
| 83 | |
| 84 | assert(callback); |
| 85 | assert(callbacks[id] == callback); |
| 86 | |
| 87 | callback(call); |
José Fonseca | 8d53812 | 2011-06-09 00:29:19 +0100 | [diff] [blame] | 88 | } |
| 89 | |
José Fonseca | 45b28c4 | 2011-05-05 01:08:00 +0100 | [diff] [blame] | 90 | |
José Fonseca | 32871ed | 2011-04-10 13:40:52 +0100 | [diff] [blame] | 91 | } /* namespace retrace */ |