history.hpp (5026B)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | /* Relacy Race Detector * Copyright (c) 2008-2010, Dmitry S. Vyukov * All rights reserved. * This software is provided AS-IS with no warranty, either express or implied. * This software is distributed under a license and may not be copied, * modified or distributed except as expressly authorized under the * terms of the license contained in the file LICENSE.TXT in this distribution. */ #ifndef RL_HISTORY_HPP #define RL_HISTORY_HPP #ifdef _MSC_VER # pragma once #endif #include "base.hpp" namespace rl { typedef void (*event_output_f)(std::ostream& s, void const* ev); typedef void (*event_dtor_f)(void* ev); struct history_entry { thread_id_t thread_index_; debug_info info_; void* ev_; event_output_f output_; event_dtor_f dtor_; history_entry(thread_id_t thread_index, debug_info_param info, void* ev, event_output_f output, event_dtor_f dtor) : thread_index_(thread_index) , info_(info) , ev_(ev) , output_(output) , dtor_(dtor) { } }; template<typename T> void event_output(std::ostream& s, void const* ev) { static_cast<T const*>(ev)->output(s); } template<typename T> void event_dtor(void* ev) { delete static_cast<T*>(ev); } struct user_event { char const* desc_; void output(std::ostream& s) const { s << desc_; } }; inline string strip_path(char const* filename) { char const* slash = strrchr(filename, '\\'); if (slash) return slash + 1; else return filename; } inline std::ostream& operator << (std::ostream& ss, debug_info_param info) { /* char const* func = info; char const* file = info + strlen(info) + 1; char const* line = file + strlen(file) + 1; */ #ifdef RL_MSVC_OUTPUT ss << info.file_ << "(" << info.line_ << ") : "; #else ss << info.func_ << ", " << strip_path(info.file_) << "(" << info.line_ << ")"; #endif return ss; } class history_mgr : nocopy<> { public: history_mgr(std::ostream& stream, thread_id_t thread_count) : thread_count_(thread_count) , out_stream_(stream) { } ~history_mgr() { clear(); } template<typename event_t> void exec_log(thread_id_t th, debug_info_param info, event_t const& ev, bool output_history) { exec_history_.push_back(history_entry(th, info, new event_t(ev), &event_output<event_t>, &event_dtor<event_t>)); if (output_history) { output(exec_history_.size() - 1); } } void print_exec_history(bool output_history) { size_t const buf_size = 4096; char buf [buf_size + 1]; size_t const count = exec_history_.size(); if (false == output_history) { sprintf(buf, "execution history (%u):\n", (unsigned)count); out_stream_ << buf; #if defined(_MSC_VER) && defined(RL_MSVC_OUTPUT) OutputDebugStringA(buf); #endif for (size_t i = 0; i != count; ++i) { output(i); } } out_stream_ << "\n"; #if defined(_MSC_VER) && defined(RL_MSVC_OUTPUT) OutputDebugStringA("\n"); #endif for (thread_id_t th = 0; th != thread_count_; ++th) { sprintf(buf, "thread %u:\n", th); out_stream_ << buf; #if defined(_MSC_VER) && defined(RL_MSVC_OUTPUT) OutputDebugStringA(buf); #endif for (size_t i = 0; i != count; ++i) { if (exec_history_[i].thread_index_ == th) { output(i); } } out_stream_ << "\n"; #if defined(_MSC_VER) && defined(RL_MSVC_OUTPUT) OutputDebugStringA("\n"); #endif } } void clear() { for (size_t i = 0; i != exec_history_.size(); ++i) { history_entry const& ent = exec_history_[i]; ent.dtor_(ent.ev_); } exec_history_.clear(); } private: vector<history_entry>::type exec_history_; thread_id_t thread_count_; std::ostream& out_stream_; void output(size_t i) { std::ostringstream stream; history_entry const& ent = exec_history_[i]; #ifdef RL_MSVC_OUTPUT { stream << ent.info_ << "[" << i << "] " << ent.thread_index_ << ": "; ent.output_(stream, ent.ev_); stream << std::endl; } #else stream << "[" << (unsigned)i << "] " << ent.thread_index_ << ": "; ent.output_(stream, ent.ev_); stream << ", in " << ent.info_ << std::endl; #endif out_stream_ << stream.str(); #if defined(_MSC_VER) && defined(RL_MSVC_OUTPUT) OutputDebugStringA(stream.str().c_str()); #endif } }; } #endif |