handler_tracking.ipp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. //
  2. // detail/impl/handler_tracking.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_DETAIL_IMPL_HANDLER_TRACKING_IPP
  11. #define BOOST_ASIO_DETAIL_IMPL_HANDLER_TRACKING_IPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if defined(BOOST_ASIO_CUSTOM_HANDLER_TRACKING)
  17. // The handler tracking implementation is provided by the user-specified header.
  18. #elif defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  19. #include <cstdarg>
  20. #include <cstdio>
  21. #include <boost/asio/detail/handler_tracking.hpp>
  22. #if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
  23. # include <boost/asio/time_traits.hpp>
  24. #elif defined(BOOST_ASIO_HAS_CHRONO)
  25. # include <boost/asio/detail/chrono.hpp>
  26. # include <boost/asio/detail/chrono_time_traits.hpp>
  27. # include <boost/asio/wait_traits.hpp>
  28. #endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
  29. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  30. # include <boost/asio/detail/socket_types.hpp>
  31. #elif !defined(BOOST_ASIO_WINDOWS)
  32. # include <unistd.h>
  33. #endif // !defined(BOOST_ASIO_WINDOWS)
  34. #include <boost/asio/detail/push_options.hpp>
  35. namespace boost {
  36. namespace asio {
  37. namespace detail {
  38. struct handler_tracking_timestamp
  39. {
  40. uint64_t seconds;
  41. uint64_t microseconds;
  42. handler_tracking_timestamp()
  43. {
  44. #if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
  45. boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1));
  46. boost::posix_time::time_duration now =
  47. boost::posix_time::microsec_clock::universal_time() - epoch;
  48. #elif defined(BOOST_ASIO_HAS_CHRONO)
  49. typedef chrono_time_traits<chrono::system_clock,
  50. boost::asio::wait_traits<chrono::system_clock> > traits_helper;
  51. traits_helper::posix_time_duration now(
  52. chrono::system_clock::now().time_since_epoch());
  53. #endif
  54. seconds = static_cast<uint64_t>(now.total_seconds());
  55. microseconds = static_cast<uint64_t>(now.total_microseconds() % 1000000);
  56. }
  57. };
  58. struct handler_tracking::tracking_state
  59. {
  60. static_mutex mutex_;
  61. uint64_t next_id_;
  62. tss_ptr<completion>* current_completion_;
  63. };
  64. handler_tracking::tracking_state* handler_tracking::get_state()
  65. {
  66. static tracking_state state = { BOOST_ASIO_STATIC_MUTEX_INIT, 1, 0 };
  67. return &state;
  68. }
  69. void handler_tracking::init()
  70. {
  71. static tracking_state* state = get_state();
  72. state->mutex_.init();
  73. static_mutex::scoped_lock lock(state->mutex_);
  74. if (state->current_completion_ == 0)
  75. state->current_completion_ = new tss_ptr<completion>;
  76. }
  77. void handler_tracking::creation(execution_context&,
  78. handler_tracking::tracked_handler& h,
  79. const char* object_type, void* object,
  80. uintmax_t /*native_handle*/, const char* op_name)
  81. {
  82. static tracking_state* state = get_state();
  83. static_mutex::scoped_lock lock(state->mutex_);
  84. h.id_ = state->next_id_++;
  85. lock.unlock();
  86. handler_tracking_timestamp timestamp;
  87. uint64_t current_id = 0;
  88. if (completion* current_completion = *state->current_completion_)
  89. current_id = current_completion->id_;
  90. write_line(
  91. #if defined(BOOST_ASIO_WINDOWS)
  92. "@asio|%I64u.%06I64u|%I64u*%I64u|%.20s@%p.%.50s\n",
  93. #else // defined(BOOST_ASIO_WINDOWS)
  94. "@asio|%llu.%06llu|%llu*%llu|%.20s@%p.%.50s\n",
  95. #endif // defined(BOOST_ASIO_WINDOWS)
  96. timestamp.seconds, timestamp.microseconds,
  97. current_id, h.id_, object_type, object, op_name);
  98. }
  99. handler_tracking::completion::completion(
  100. const handler_tracking::tracked_handler& h)
  101. : id_(h.id_),
  102. invoked_(false),
  103. next_(*get_state()->current_completion_)
  104. {
  105. *get_state()->current_completion_ = this;
  106. }
  107. handler_tracking::completion::~completion()
  108. {
  109. if (id_)
  110. {
  111. handler_tracking_timestamp timestamp;
  112. write_line(
  113. #if defined(BOOST_ASIO_WINDOWS)
  114. "@asio|%I64u.%06I64u|%c%I64u|\n",
  115. #else // defined(BOOST_ASIO_WINDOWS)
  116. "@asio|%llu.%06llu|%c%llu|\n",
  117. #endif // defined(BOOST_ASIO_WINDOWS)
  118. timestamp.seconds, timestamp.microseconds,
  119. invoked_ ? '!' : '~', id_);
  120. }
  121. *get_state()->current_completion_ = next_;
  122. }
  123. void handler_tracking::completion::invocation_begin()
  124. {
  125. handler_tracking_timestamp timestamp;
  126. write_line(
  127. #if defined(BOOST_ASIO_WINDOWS)
  128. "@asio|%I64u.%06I64u|>%I64u|\n",
  129. #else // defined(BOOST_ASIO_WINDOWS)
  130. "@asio|%llu.%06llu|>%llu|\n",
  131. #endif // defined(BOOST_ASIO_WINDOWS)
  132. timestamp.seconds, timestamp.microseconds, id_);
  133. invoked_ = true;
  134. }
  135. void handler_tracking::completion::invocation_begin(
  136. const boost::system::error_code& ec)
  137. {
  138. handler_tracking_timestamp timestamp;
  139. write_line(
  140. #if defined(BOOST_ASIO_WINDOWS)
  141. "@asio|%I64u.%06I64u|>%I64u|ec=%.20s:%d\n",
  142. #else // defined(BOOST_ASIO_WINDOWS)
  143. "@asio|%llu.%06llu|>%llu|ec=%.20s:%d\n",
  144. #endif // defined(BOOST_ASIO_WINDOWS)
  145. timestamp.seconds, timestamp.microseconds,
  146. id_, ec.category().name(), ec.value());
  147. invoked_ = true;
  148. }
  149. void handler_tracking::completion::invocation_begin(
  150. const boost::system::error_code& ec, std::size_t bytes_transferred)
  151. {
  152. handler_tracking_timestamp timestamp;
  153. write_line(
  154. #if defined(BOOST_ASIO_WINDOWS)
  155. "@asio|%I64u.%06I64u|>%I64u|ec=%.20s:%d,bytes_transferred=%I64u\n",
  156. #else // defined(BOOST_ASIO_WINDOWS)
  157. "@asio|%llu.%06llu|>%llu|ec=%.20s:%d,bytes_transferred=%llu\n",
  158. #endif // defined(BOOST_ASIO_WINDOWS)
  159. timestamp.seconds, timestamp.microseconds,
  160. id_, ec.category().name(), ec.value(),
  161. static_cast<uint64_t>(bytes_transferred));
  162. invoked_ = true;
  163. }
  164. void handler_tracking::completion::invocation_begin(
  165. const boost::system::error_code& ec, int signal_number)
  166. {
  167. handler_tracking_timestamp timestamp;
  168. write_line(
  169. #if defined(BOOST_ASIO_WINDOWS)
  170. "@asio|%I64u.%06I64u|>%I64u|ec=%.20s:%d,signal_number=%d\n",
  171. #else // defined(BOOST_ASIO_WINDOWS)
  172. "@asio|%llu.%06llu|>%llu|ec=%.20s:%d,signal_number=%d\n",
  173. #endif // defined(BOOST_ASIO_WINDOWS)
  174. timestamp.seconds, timestamp.microseconds,
  175. id_, ec.category().name(), ec.value(), signal_number);
  176. invoked_ = true;
  177. }
  178. void handler_tracking::completion::invocation_begin(
  179. const boost::system::error_code& ec, const char* arg)
  180. {
  181. handler_tracking_timestamp timestamp;
  182. write_line(
  183. #if defined(BOOST_ASIO_WINDOWS)
  184. "@asio|%I64u.%06I64u|>%I64u|ec=%.20s:%d,%.50s\n",
  185. #else // defined(BOOST_ASIO_WINDOWS)
  186. "@asio|%llu.%06llu|>%llu|ec=%.20s:%d,%.50s\n",
  187. #endif // defined(BOOST_ASIO_WINDOWS)
  188. timestamp.seconds, timestamp.microseconds,
  189. id_, ec.category().name(), ec.value(), arg);
  190. invoked_ = true;
  191. }
  192. void handler_tracking::completion::invocation_end()
  193. {
  194. if (id_)
  195. {
  196. handler_tracking_timestamp timestamp;
  197. write_line(
  198. #if defined(BOOST_ASIO_WINDOWS)
  199. "@asio|%I64u.%06I64u|<%I64u|\n",
  200. #else // defined(BOOST_ASIO_WINDOWS)
  201. "@asio|%llu.%06llu|<%llu|\n",
  202. #endif // defined(BOOST_ASIO_WINDOWS)
  203. timestamp.seconds, timestamp.microseconds, id_);
  204. id_ = 0;
  205. }
  206. }
  207. void handler_tracking::operation(execution_context&,
  208. const char* object_type, void* object,
  209. uintmax_t /*native_handle*/, const char* op_name)
  210. {
  211. static tracking_state* state = get_state();
  212. handler_tracking_timestamp timestamp;
  213. unsigned long long current_id = 0;
  214. if (completion* current_completion = *state->current_completion_)
  215. current_id = current_completion->id_;
  216. write_line(
  217. #if defined(BOOST_ASIO_WINDOWS)
  218. "@asio|%I64u.%06I64u|%I64u|%.20s@%p.%.50s\n",
  219. #else // defined(BOOST_ASIO_WINDOWS)
  220. "@asio|%llu.%06llu|%llu|%.20s@%p.%.50s\n",
  221. #endif // defined(BOOST_ASIO_WINDOWS)
  222. timestamp.seconds, timestamp.microseconds,
  223. current_id, object_type, object, op_name);
  224. }
  225. void handler_tracking::reactor_registration(execution_context& /*context*/,
  226. uintmax_t /*native_handle*/, uintmax_t /*registration*/)
  227. {
  228. }
  229. void handler_tracking::reactor_deregistration(execution_context& /*context*/,
  230. uintmax_t /*native_handle*/, uintmax_t /*registration*/)
  231. {
  232. }
  233. void handler_tracking::reactor_events(execution_context& /*context*/,
  234. uintmax_t /*native_handle*/, unsigned /*events*/)
  235. {
  236. }
  237. void handler_tracking::reactor_operation(
  238. const tracked_handler& h, const char* op_name,
  239. const boost::system::error_code& ec)
  240. {
  241. handler_tracking_timestamp timestamp;
  242. write_line(
  243. #if defined(BOOST_ASIO_WINDOWS)
  244. "@asio|%I64u.%06I64u|.%I64u|%s,ec=%.20s:%d\n",
  245. #else // defined(BOOST_ASIO_WINDOWS)
  246. "@asio|%llu.%06llu|.%llu|%s,ec=%.20s:%d\n",
  247. #endif // defined(BOOST_ASIO_WINDOWS)
  248. timestamp.seconds, timestamp.microseconds,
  249. h.id_, op_name, ec.category().name(), ec.value());
  250. }
  251. void handler_tracking::reactor_operation(
  252. const tracked_handler& h, const char* op_name,
  253. const boost::system::error_code& ec, std::size_t bytes_transferred)
  254. {
  255. handler_tracking_timestamp timestamp;
  256. write_line(
  257. #if defined(BOOST_ASIO_WINDOWS)
  258. "@asio|%I64u.%06I64u|.%I64u|%s,ec=%.20s:%d,bytes_transferred=%I64u\n",
  259. #else // defined(BOOST_ASIO_WINDOWS)
  260. "@asio|%llu.%06llu|.%llu|%s,ec=%.20s:%d,bytes_transferred=%llu\n",
  261. #endif // defined(BOOST_ASIO_WINDOWS)
  262. timestamp.seconds, timestamp.microseconds,
  263. h.id_, op_name, ec.category().name(), ec.value(),
  264. static_cast<uint64_t>(bytes_transferred));
  265. }
  266. void handler_tracking::write_line(const char* format, ...)
  267. {
  268. using namespace std; // For sprintf (or equivalent).
  269. va_list args;
  270. va_start(args, format);
  271. char line[256] = "";
  272. #if defined(BOOST_ASIO_HAS_SECURE_RTL)
  273. int length = vsprintf_s(line, sizeof(line), format, args);
  274. #else // defined(BOOST_ASIO_HAS_SECURE_RTL)
  275. int length = vsprintf(line, format, args);
  276. #endif // defined(BOOST_ASIO_HAS_SECURE_RTL)
  277. va_end(args);
  278. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  279. wchar_t wline[256] = L"";
  280. mbstowcs_s(0, wline, sizeof(wline) / sizeof(wchar_t), line, length);
  281. ::OutputDebugStringW(wline);
  282. #elif defined(BOOST_ASIO_WINDOWS)
  283. HANDLE stderr_handle = ::GetStdHandle(STD_ERROR_HANDLE);
  284. DWORD bytes_written = 0;
  285. ::WriteFile(stderr_handle, line, length, &bytes_written, 0);
  286. #else // defined(BOOST_ASIO_WINDOWS)
  287. ::write(STDERR_FILENO, line, length);
  288. #endif // defined(BOOST_ASIO_WINDOWS)
  289. }
  290. } // namespace detail
  291. } // namespace asio
  292. } // namespace boost
  293. #include <boost/asio/detail/pop_options.hpp>
  294. #endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  295. #endif // BOOST_ASIO_DETAIL_IMPL_HANDLER_TRACKING_IPP