00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef USE_PCH
00017 #include "sys.h"
00018 #endif
00019
00020 #ifdef CWDEBUG
00021
00022 #ifndef USE_PCH
00023 #include <cctype>
00024 #include <iomanip>
00025 #include <map>
00026 #include <string>
00027 #include <sstream>
00028 #include "debug.h"
00029 #ifdef USE_LIBCW
00030 #include <libcw/memleak.h>
00031 #endif
00032 #endif // USE_PCH
00033
00034 namespace debug {
00035 namespace channels {
00036 namespace dc {
00037
00038 #ifndef DOXYGEN
00039 #define DDCN(x) (x)
00040 #endif
00041
00042
00043 channel_ct persist DDCN("PERSIST");
00044 channel_ct exceptions DDCN("EXCEPTIONS");
00045 channel_ct objects DDCN("OBJECTS");
00046 channel_ct matcher DDCN("MATCHER");
00047 channel_ct expression DDCN("EXPRESSION");
00048 channel_ct maps DDCN("MAPS");
00049 channel_ct clientinput DDCN("CLIENTINPUT");
00050 channel_ct serverinput DDCN("SERVERINPUT");
00051
00052 }
00053 }
00054
00055
00056
00057 namespace {
00058
00059
00060
00061
00062
00063 typedef std::map<std::string, bool> rcfile_dc_states_type;
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 rcfile_dc_states_type rcfile_dc_states;
00074
00075
00076
00077
00078
00079
00080
00081 void set_state(char const* dc_label, bool is_on)
00082 {
00083 std::pair<rcfile_dc_states_type::iterator, bool> res =
00084 rcfile_dc_states.insert(rcfile_dc_states_type::value_type(std::string(dc_label), is_on));
00085 if (!res.second)
00086 Dout(dc::warning, "Calling set_state() more than once for the same label!");
00087 return;
00088 }
00089
00090
00091
00092
00093
00094
00095
00096
00097 void save_dc_states(void)
00098 {
00099
00100
00101
00102
00103 static bool second_time = false;
00104 if (second_time)
00105 {
00106 Dout(dc::warning, "Calling save_dc_states() more than once!");
00107 return;
00108 }
00109 second_time = true;
00110 ForAllDebugChannels( set_state(debugChannel.get_label(), debugChannel.is_on()) );
00111 }
00112
00113 }
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 bool is_on_in_rcfile(char const* dc_label)
00129 {
00130 rcfile_dc_states_type::const_iterator iter = rcfile_dc_states.find(std::string(dc_label));
00131 if (iter == rcfile_dc_states.end())
00132 {
00133 Dout(dc::warning, "is_on_in_rcfile(\"" << dc_label << "\"): \"" << dc_label << "\" is an unknown label!");
00134 return false;
00135 }
00136 return iter->second;
00137 }
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149 void init_thread(void)
00150 {
00151
00152 ForAllDebugChannels(
00153 if (!debugChannel.is_on() && is_on_in_rcfile(debugChannel.get_label()))
00154 debugChannel.on();
00155 );
00156
00157
00158 Debug( libcw_do.on() );
00159 #if LIBCWD_THREAD_SAFE
00160 Debug( libcw_do.set_ostream(&std::cout, &cout_mutex) );
00161 #else
00162 Debug( libcw_do.set_ostream(&std::cout) );
00163 #endif
00164
00165 static bool first_thread = true;
00166 if (!first_thread)
00167 {
00168
00169 char margin[12];
00170 sprintf(margin, "%-10lu ", pthread_self());
00171 Debug( libcw_do.margin().assign(margin, 11) );
00172 }
00173 }
00174
00175
00176
00177
00178
00179 void init(void)
00180 {
00181 #if CWDEBUG_ALLOC && defined(USE_LIBCW)
00182
00183
00184 {
00185 std::vector<std::pair<std::string, std::string> > hide_list;
00186 hide_list.push_back(std::pair<std::string, std::string>("libdl.so.2", "_dlerror_run"));
00187 hide_list.push_back(std::pair<std::string, std::string>("libstdc++.so.6", "__cxa_get_globals"));
00188
00189
00190 hide_list.push_back(std::pair<std::string, std::string>("libc.so.6", "dl_open_worker"));
00191 memleak_filter().hide_functions_matching(hide_list);
00192 }
00193 {
00194 std::vector<std::string> hide_list;
00195
00196 hide_list.push_back(std::string("ld-linux.so.2"));
00197 memleak_filter().hide_objectfiles_matching(hide_list);
00198 }
00199 memleak_filter().set_flags(libcwd::show_objectfile|libcwd::show_function);
00200 #endif
00201
00202
00203
00204 Debug(set_invisible_on());
00205
00206
00207
00208
00209
00210
00211 Debug(set_invisible_off());
00212
00213
00214
00215 Debug( check_configuration() );
00216
00217 Debug(
00218 libcw_do.on();
00219 ForAllDebugChannels(
00220 while (debugChannel.is_on())
00221 debugChannel.off()
00222 );
00223 read_rcfile();
00224 libcw_do.off()
00225 );
00226 save_dc_states();
00227
00228 init_thread();
00229 }
00230
00231
00232
00233
00234
00235
00236 void dump_hex(libcwd::channel_ct const& channel, unsigned char const* buf, size_t size)
00237 {
00238 for (size_t addr = 0; addr < size; addr += 16)
00239 {
00240 LibcwDoutScopeBegin(DEBUGCHANNELS, libcwd::libcw_do, channel)
00241 LibcwDoutStream << std::hex << std::setfill('0') << std::setw(4) << addr << " |";
00242 int offset;
00243 for (offset = 0; offset < 16 && addr + offset < size; ++offset)
00244 LibcwDoutStream << ' ' << std::hex << std::setfill('0') << std::setw(2) << (int)buf[addr + offset];
00245 for (; offset < 16; ++offset)
00246 LibcwDoutStream << " ";
00247 LibcwDoutStream << " | ";
00248 for (int offset = 0; offset < 16 && addr + offset < size; ++offset)
00249 {
00250 unsigned char c = buf[addr + offset];
00251 if (!std::isprint(c))
00252 c = '.';
00253 LibcwDoutStream << c;
00254 }
00255 LibcwDoutScopeEnd;
00256 }
00257 }
00258
00259 #if CWDEBUG_LOCATION
00260
00261
00262
00263
00264 std::string call_location(void const* return_addr)
00265 {
00266 libcwd::location_ct loc((char*)return_addr + libcwd::builtin_return_address_offset);
00267 std::ostringstream convert;
00268 convert << loc;
00269 return convert.str();
00270 }
00271 #endif
00272
00273 bool secondary_connection = false;
00274 int SecondaryConnection::S_count = 0;
00275
00276 }
00277
00278 #endif // CWDEBUG