Given running application, I would like to extract information about currently registered complete handlers.
Handler has been registered by class A. For example:
boost::asio::async_read(s, b, boost::bind(&A::F, this->shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
Under debugger I have access to appropriate io_service variable. How to figure out (A::F, this, s, b) for operations, which have not yet completed.
I would like to slightly expand the scope of the question to cover alternatives for what I believe is the final goal: debugging asynchronous handlers.
To show debugging via examples, lets start with a basic UDP echo server that listens to port 4321:
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
using boost::asio::ip::udp;
class udp_echo
{
public:
udp_echo(boost::asio::io_service& service,
unsigned int port)
: socket_(service, udp::endpoint(udp::v4(), port))
{
socket_.async_receive_from(
boost::asio::buffer(buffer_), sender_,
boost::bind(&udp_echo::handle_receive, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void handle_receive(const boost::system::error_code& error,
std::size_t bytes_transferred)
{
socket_.async_send_to(
boost::asio::buffer(buffer_, bytes_transferred), sender_,
boost::bind(&udp_echo::handle_send, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void handle_send(const boost::system::error_code& error,
std::size_t bytes_transferred)
{
socket_.close();
}
private:
udp::socket socket_;
boost::array<char, 128> buffer_;
udp::endpoint sender_;
};
int main()
{
boost::asio::io_service service;
udp_echo echo(service, 4321);
service.run();
}
This simple program has a single asynchronous call chain:
udp_echo::udp_echo()
{
socket_.async_receive_from(...); --.
} |
.-----------------------'
v
void udp_echo::handle_receive(...)
{
socket_.async_send_to(...); ------.
} |
.-----------------------'
v
void udp_echo::handle_send()
{
socket_.close();
}
Boost 1.47 introduced handler tracking. Simply define BOOST_ASIO_ENABLE_HANDLER_TRACKING and Boost.Asio will write debug output, including timestamps, to the standard error stream. Running the programming, and sending "hello world" via UDP produced the following output:
@asio|1363273821.846895|0*1|[email protected]_receive_from // 1
@asio|1363273829.288883|>1|ec=system:0,bytes_transferred=12 // 2
@asio|1363273829.288931|1*2|[email protected]_send_to // 3
@asio|1363273829.289013|<1| // 4
@asio|1363273829.289026|>2|ec=system:0,bytes_transferred=12 // 5
@asio|1363273829.289035|2|[email protected] // 6
@asio|1363273829.289075|<2| // 7
It can be read line-by-line as:
socket.async_receive_from(), creating handler 1.socket.async_receive_from() with no error and 12 bytes have been received.socket.async_receive_from() invoked socket.async_send_to(), creating handler 2.socket.async_receive_from().socket.async_send_to() with no error and 12 bytes have been sent.socket.close().socket.async_send_to().And visually maps to the following:
udp_echo::udp_echo()
{
socket_.async_receive_from(...); --. // 1
} |
.-----------------------'
v
void udp_echo::handle_receive(...)
{ // 2
socket_.async_send_to(...); ------. // 3
} | // 4
.-----------------------'
v
void udp_echo::handle_send()
{ // 5
socket_.close(); // 6
} // 7
Debugging through GDB will require digging through multiple layers. It helps to have an understanding of some of the implementation details of Boost.Asio. Here are a few concepts:
io_service only contains handlers that are ready to run.reactor will generally contain work operations, and a handle to completion handlers that are not ready to run.reactor will register itself with the io_service.Here is a debug session:
(gdb) bt
#0 0x00ab1402 in __kernel_vsyscall ()
#1 0x00237ab8 in __epoll_wait_nocancel () from /lib/libc.so.6
#2 0x080519c3 in boost::asio::detail::epoll_reactor::run (this=0x80560b0,
block=true, ops=...)
at /opt/boost/include/boost/asio/detail/impl/epoll_reactor.ipp:392
#3 0x08051c2d in boost::asio::detail::task_io_service::do_run_one (
this=0x8056030, lock=..., this_thread=..., ec=...)
at /opt/boost/include/boost/asio/detail/impl/task_io_service.ipp:396
#4 0x08051e8a in boost::asio::detail::task_io_service::run (this=0x8056030,
ec=...)
at /opt/boost/include/boost/asio/detail/impl/task_io_service.ipp:153
#5 0x08051f50 in boost::asio::io_service::run (this=0xbfffe818)
at /opt/boost/include/boost/asio/impl/io_service.ipp:59
#6 0x08049a44 in main () at example.cpp:48
(gdb) frame 6
#6 0x08049a44 in main () at example.cpp:48
48 service.run();
First, the reactor service will need to be located. Downcasting will need to occur, so lets use the debugger to locate some types:
(gdb) p service.service_registry_.init_keytab init_key init_key<boost::asio::datagram_socket_service<boost::asio::ip::udp> > init_key<boost::asio::detail::epoll_reactor> init_key<boost::asio::detail::task_io_service>
Each key is associated with a specific service, and all services are maintained in a linked list within service.service_registry_. Type information is associated with them, allowing us to identify the desired service.
(gdb) set $service = service.service_registry_.first_service_
(gdb) p $service.key_.type_info_.__name
$1 = 0x8052b60
"N5boost4asio6detail14typeid_wrapperINS0_23datagram_socket_serviceINS0_2ip3udpEEEEE"
That is the boost::asio::datagram_socket_service<boost::asio::ip::udp>, so continue to the next:
(gdb) set $service = $service.next_
(gdb) p $service.key_.type_info_.__name
$2 = 0x8052cc0 "N5boost4asio6detail14typeid_wrapperINS1_13epoll_reactorEEE"
$service now points to the reactor service. Downcast the service, based on the init_key type argument:
(gdb) set $service = *('boost::asio::detail::epoll_reactor'*) $service
The outstanding handlers with work are in an linked list of operations within the reactor:
(gdb) set $ops = $service.registered_descriptors_.live_list_.op_queue_
(gdb) set $op = $ops.front_
(gdb) p *$op
$3 = {<boost::asio::detail::task_io_service_operation> = {next_ = 0x0,
func_ = 0x804c256
<boost::asio::detail::reactive_socket_recvfrom_op<
boost::asio::mutable_buffers_1, boost::asio::ip::basic_endpoint<
boost::asio::ip::udp>, boost::_bi::bind_t<void,
boost::_mfi::mf2<void, udp_echo, boost::system::error_code const&,
unsigned int>, boost::_bi::list3<boost::_bi::value<udp_echo*>,
boost::arg<1> (*)(), boost::arg<2> (*)()> > >::
do_complete(boost::asio::io_service::io_service_impl*,
boost::asio::detail::epoll_reactor::descriptor_state::operation*,
boost::system::error_code const&, size_t)>, task_result_ = 0}, ec_ = {
m_val = 11, m_cat = 0x13b2c8}, bytes_transferred_ = 0, perform_func_ =
0x80514c8 <boost::asio::detail::reactive_socket_recvfrom_op_base<
boost::asio::mutable_buffers_1,
boost::asio::ip::basic_endpoint<boost::asio::ip::udp>
>::do_perform(boost::asio::detail::reactor_op*)>}
Another downcast is required. Cast $op to the class for which the func_ member function pointer belongs.
(gdb) set $op = *('boost::asio::detail::reactive_socket_recvfrom_op<
boost::asio::mutable_buffers_1, boost::asio::ip::basic_endpoint<
boost::asio::ip::udp>, boost::_bi::bind_t<void, boost::_mfi::mf2<
void, udp_echo, boost::system::error_code const&, unsigned int>,
boost::_bi::list3<boost::_bi::value<udp_echo*>,
boost::arg<1> (*)(), boost::arg<2> (*)()> > >'*) $op
This operation contains the desired information.
The buffer:
(gdb) p $op.buffers_
$4 = {<boost::asio::mutable_buffer> = {data_ = 0xbfffe77c,
size_ = 128}, <No data fields>}
(gdb) p &echo.buffer_
$5 = (boost::array<char, 128u> *) 0xbfffe77c
The this instance:
(gdb) p $op.handler_.l_.a1_.t_
$6 = (udp_echo *) 0xbfffe768
(gdb) p &echo
$7 = (udp_echo *) 0xbfffe768
The member function pointer:
(gdb) p $op.handler_.f_.f_
$8 = (void (udp_echo::*)(udp_echo *, const boost::system::error_code &,
unsigned int)) 0x80505b0 <
udp_echo::handle_receive(boost::system::error_code const&, size_t)>
Socket information:
(gdb) p $op.socket_
$9 = 10
(gdb) p echo.socket_.implementation.socket_
$10 = 10
In this case, the operation only knows about the native socket representation (the file descriptor). One helpful way to determine what socket it is, is to query lsof.
$/usr/sbin/lsof -i -P | grep a.out
a.out 4265 ghost 10u IPv4 1166143 UDP *:4321
Thus, file descriptor 10 is listening on UDP 4321.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With