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 | |
Ryan C. Gordon | 2dad4b4 | 2012-01-08 01:32:41 -0500 | [diff] [blame] | 30 | #include "os_time.hpp" |
José Fonseca | 32871ed | 2011-04-10 13:40:52 +0100 | [diff] [blame] | 31 | #include "retrace.hpp" |
José Fonseca | 589082d | 2011-03-30 09:10:40 +0100 | [diff] [blame] | 32 | |
José Fonseca | 52ea995 | 2015-02-09 12:50:53 +0000 | [diff] [blame] | 33 | #ifdef _WIN32 |
| 34 | #include <dxerr.h> |
| 35 | #endif |
| 36 | |
José Fonseca | 589082d | 2011-03-30 09:10:40 +0100 | [diff] [blame] | 37 | |
José Fonseca | 32871ed | 2011-04-10 13:40:52 +0100 | [diff] [blame] | 38 | namespace retrace { |
| 39 | |
| 40 | |
José Fonseca | 8a4baf8 | 2014-05-15 23:04:58 +0100 | [diff] [blame] | 41 | trace::DumpFlags dumpFlags = trace::DUMP_FLAG_THREAD_IDS; |
José Fonseca | f992905 | 2013-06-09 09:27:13 +0100 | [diff] [blame] | 42 | |
| 43 | |
José Fonseca | b1bb3c2 | 2011-10-08 20:23:18 +0100 | [diff] [blame] | 44 | static bool call_dumped = false; |
| 45 | |
| 46 | |
José Fonseca | b4a3d14 | 2011-10-27 07:43:19 +0100 | [diff] [blame] | 47 | static void dumpCall(trace::Call &call) { |
José Fonseca | b1bb3c2 | 2011-10-08 20:23:18 +0100 | [diff] [blame] | 48 | if (verbosity >= 0 && !call_dumped) { |
José Fonseca | f992905 | 2013-06-09 09:27:13 +0100 | [diff] [blame] | 49 | trace::dump(call, std::cout, dumpFlags); |
José Fonseca | b1bb3c2 | 2011-10-08 20:23:18 +0100 | [diff] [blame] | 50 | std::cout.flush(); |
| 51 | call_dumped = true; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | |
José Fonseca | b4a3d14 | 2011-10-27 07:43:19 +0100 | [diff] [blame] | 56 | std::ostream &warning(trace::Call &call) { |
José Fonseca | b1bb3c2 | 2011-10-08 20:23:18 +0100 | [diff] [blame] | 57 | dumpCall(call); |
| 58 | |
| 59 | std::cerr << call.no << ": "; |
| 60 | std::cerr << "warning: "; |
| 61 | |
| 62 | return std::cerr; |
| 63 | } |
| 64 | |
| 65 | |
José Fonseca | 64add5f | 2014-09-05 11:53:11 +0100 | [diff] [blame] | 66 | #ifdef _WIN32 |
| 67 | void |
| 68 | failed(trace::Call &call, HRESULT hr) |
| 69 | { |
| 70 | std::ostream &os = warning(call); |
| 71 | |
| 72 | os << "failed with 0x" << std::hex << hr << std::dec; |
| 73 | |
José Fonseca | 52ea995 | 2015-02-09 12:50:53 +0000 | [diff] [blame] | 74 | LPCSTR lpszErrorString = DXGetErrorStringA(hr); |
| 75 | assert(lpszErrorString); |
| 76 | os << " (" << lpszErrorString << "): "; |
| 77 | |
| 78 | char szErrorDesc[512]; |
| 79 | DXGetErrorDescriptionA(hr, szErrorDesc, sizeof szErrorDesc); |
| 80 | os << szErrorDesc; |
José Fonseca | 64add5f | 2014-09-05 11:53:11 +0100 | [diff] [blame] | 81 | |
| 82 | os << "\n"; |
| 83 | } |
| 84 | #endif /* _WIN32 */ |
| 85 | |
| 86 | |
José Fonseca | facdb35 | 2014-09-05 13:22:01 +0100 | [diff] [blame] | 87 | void |
| 88 | checkMismatch(trace::Call &call, const char *expr, trace::Value *traceValue, long actualValue) |
| 89 | { |
| 90 | assert(traceValue); |
| 91 | long traceIntValue = traceValue->toSInt(); |
| 92 | if (traceIntValue == actualValue) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | std::ostream &os = warning(call); |
| 97 | os << "mismatch in " << expr << ": expected " << traceIntValue << " but got " << actualValue << "\n"; |
| 98 | } |
| 99 | |
| 100 | |
José Fonseca | b4a3d14 | 2011-10-27 07:43:19 +0100 | [diff] [blame] | 101 | void ignore(trace::Call &call) { |
José Fonseca | 8d53812 | 2011-06-09 00:29:19 +0100 | [diff] [blame] | 102 | (void)call; |
| 103 | } |
| 104 | |
José Fonseca | b4a3d14 | 2011-10-27 07:43:19 +0100 | [diff] [blame] | 105 | void unsupported(trace::Call &call) { |
José Fonseca | f5cda41 | 2011-10-09 17:27:23 +0100 | [diff] [blame] | 106 | warning(call) << "unsupported " << call.name() << " call\n"; |
José Fonseca | 45b28c4 | 2011-05-05 01:08:00 +0100 | [diff] [blame] | 107 | } |
| 108 | |
José Fonseca | 2741ed8 | 2011-10-07 23:36:39 +0100 | [diff] [blame] | 109 | inline void Retracer::addCallback(const Entry *entry) { |
| 110 | assert(entry->name); |
| 111 | assert(entry->callback); |
| 112 | map[entry->name] = entry->callback; |
| 113 | } |
José Fonseca | 8d53812 | 2011-06-09 00:29:19 +0100 | [diff] [blame] | 114 | |
José Fonseca | 2741ed8 | 2011-10-07 23:36:39 +0100 | [diff] [blame] | 115 | |
| 116 | void Retracer::addCallbacks(const Entry *entries) { |
| 117 | while (entries->name && entries->callback) { |
| 118 | addCallback(entries++); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | |
José Fonseca | b4a3d14 | 2011-10-27 07:43:19 +0100 | [diff] [blame] | 123 | void Retracer::retrace(trace::Call &call) { |
José Fonseca | b1bb3c2 | 2011-10-08 20:23:18 +0100 | [diff] [blame] | 124 | call_dumped = false; |
| 125 | |
José Fonseca | 2741ed8 | 2011-10-07 23:36:39 +0100 | [diff] [blame] | 126 | Callback callback = 0; |
| 127 | |
José Fonseca | b4a3d14 | 2011-10-27 07:43:19 +0100 | [diff] [blame] | 128 | trace::Id id = call.sig->id; |
José Fonseca | 2741ed8 | 2011-10-07 23:36:39 +0100 | [diff] [blame] | 129 | if (id >= callbacks.size()) { |
| 130 | callbacks.resize(id + 1); |
| 131 | callback = 0; |
| 132 | } else { |
| 133 | callback = callbacks[id]; |
José Fonseca | 8d53812 | 2011-06-09 00:29:19 +0100 | [diff] [blame] | 134 | } |
| 135 | |
José Fonseca | 2741ed8 | 2011-10-07 23:36:39 +0100 | [diff] [blame] | 136 | if (!callback) { |
| 137 | Map::const_iterator it = map.find(call.name()); |
| 138 | if (it == map.end()) { |
José Fonseca | f5cda41 | 2011-10-09 17:27:23 +0100 | [diff] [blame] | 139 | callback = &unsupported; |
José Fonseca | 2741ed8 | 2011-10-07 23:36:39 +0100 | [diff] [blame] | 140 | } else { |
| 141 | callback = it->second; |
| 142 | } |
| 143 | callbacks[id] = callback; |
| 144 | } |
| 145 | |
| 146 | assert(callback); |
| 147 | assert(callbacks[id] == callback); |
| 148 | |
José Fonseca | e4c0d58 | 2012-10-24 13:34:27 +0100 | [diff] [blame] | 149 | if (verbosity >= 1) { |
| 150 | if (verbosity >= 2 || |
| 151 | (!(call.flags & trace::CALL_FLAG_VERBOSE) && |
| 152 | callback != &ignore)) { |
| 153 | dumpCall(call); |
| 154 | } |
| 155 | } |
| 156 | |
James Benton | 6d92327 | 2012-07-25 13:48:20 +0100 | [diff] [blame] | 157 | callback(call); |
José Fonseca | 8d53812 | 2011-06-09 00:29:19 +0100 | [diff] [blame] | 158 | } |
| 159 | |
José Fonseca | 45b28c4 | 2011-05-05 01:08:00 +0100 | [diff] [blame] | 160 | |
José Fonseca | 32871ed | 2011-04-10 13:40:52 +0100 | [diff] [blame] | 161 | } /* namespace retrace */ |