Author: axeld Date: 2010-08-27 09:32:41 +0200 (Fri, 27 Aug 2010) New Revision: 38386 Changeset: http://dev.haiku-os.org/changeset/38386 Modified: haiku/trunk/src/add-ons/kernel/network/stack/net_socket.cpp Log: * Added some optional debug output. * Check for errors using != instead of < - the functions are not supposed to return anything above B_OK. * Use the stack-wide ENABLE_DEBUGGER_COMMANDS instead of our own local solution. Modified: haiku/trunk/src/add-ons/kernel/network/stack/net_socket.cpp =================================================================== --- haiku/trunk/src/add-ons/kernel/network/stack/net_socket.cpp 2010-08-27 00:58:34 UTC (rev 38385) +++ haiku/trunk/src/add-ons/kernel/network/stack/net_socket.cpp 2010-08-27 07:32:41 UTC (rev 38386) @@ -37,7 +37,12 @@ #include "utility.h" -#define ADD_DEBUGGER_COMMANDS +//#define TRACE_SOCKET +#ifdef TRACE_SOCKET +# define TRACE(x...) dprintf(STACK_DEBUG_PREFIX x) +#else +# define TRACE(x...) ; +#endif struct net_socket_private; @@ -110,6 +115,8 @@ net_socket_private::~net_socket_private() { + TRACE("delete net_socket %p\n", this); + if (parent != NULL) panic("socket still has a parent!"); @@ -186,11 +193,20 @@ socket->protocol = protocol; status_t status = get_domain_protocols(socket); - if (status < B_OK) { + if (status != B_OK) { delete socket; return status; } + TRACE("create net_socket %p (%u.%u.%u):\n", socket, socket->family, + socket->type, socket->protocol); + +#ifdef TRACE_SOCKET + net_protocol* current = socket->first_protocol; + for (int i = 0; current != NULL; current = current->next, i++) + TRACE(" [%d] %p %s\n", i, current, current->module->info.name); +#endif + *_socket = socket; return B_OK; } @@ -315,8 +331,9 @@ } -#ifdef ADD_DEBUGGER_COMMANDS +#if ENABLE_DEBUGGER_COMMANDS + static void print_socket_line(net_socket_private* socket, const char* prefix) { @@ -395,9 +412,10 @@ return 0; } -#endif // ADD_DEBUGGER_COMMANDS +#endif // ENABLE_DEBUGGER_COMMANDS + // #pragma mark - @@ -406,11 +424,11 @@ { net_socket_private* socket; status_t status = create_socket(family, type, protocol, &socket); - if (status < B_OK) + if (status != B_OK) return status; status = socket->first_info->open(socket->first_protocol); - if (status < B_OK) { + if (status != B_OK) { delete socket; return status; } @@ -462,7 +480,7 @@ if (socket->address.ss_len == 0) { // try to bind first status_t status = socket_bind(socket, NULL, 0); - if (status < B_OK) + if (status != B_OK) return status; } @@ -585,8 +603,7 @@ { status_t status = socket->first_info->read_data(socket->first_protocol, length, flags, _buffer); - - if (status < B_OK) + if (status != B_OK) return status; if (*_buffer && length < (*_buffer)->size) { @@ -650,10 +667,10 @@ { net_socket_private* socket = (net_socket_private*)_socket; - // During destruction, the socket might still be accessible over its endpoint - // protocol. We need to make sure the endpoint cannot acquire the socket - // anymore -- while not obvious, the endpoint protocol is responsible for the - // proper locking here. + // During destruction, the socket might still be accessible over its + // endpoint protocol. We need to make sure the endpoint cannot acquire the + // socket anymore -- while not obvious, the endpoint protocol is responsible + // for the proper locking here. if (socket->CountReferences() == 0) return false; @@ -675,6 +692,8 @@ { net_socket_private* parent = (net_socket_private*)_parent; + TRACE("%s(%p)\n", __FUNCTION__, parent); + MutexLocker locker(parent->lock); // We actually accept more pending connections to compensate for those @@ -686,7 +705,7 @@ net_socket_private* socket; status_t status = create_socket(parent->family, parent->type, parent->protocol, &socket); - if (status < B_OK) + if (status != B_OK) return status; // inherit parent's properties @@ -794,6 +813,8 @@ { net_socket_private* socket = (net_socket_private*)_socket; + TRACE("socket_connected(%p)\n", socket); + WeakReference<net_socket_private> parent = socket->parent; if (parent.Get() == NULL) return B_BAD_VALUE; @@ -820,6 +841,8 @@ { net_socket_private* socket = (net_socket_private*)_socket; + TRACE("socket_aborted(%p)\n", socket); + WeakReference<net_socket_private> parent = socket->parent; if (parent.Get() == NULL) return B_BAD_VALUE; @@ -853,7 +876,7 @@ mutex_unlock(&socket->lock); - if (status < B_OK) + if (status != B_OK) return status; // check if the event is already present @@ -947,7 +970,7 @@ net_socket* accepted; status_t status = socket->first_info->accept(socket->first_protocol, &accepted); - if (status < B_OK) + if (status != B_OK) return status; if (address && *_addressLength > 0) { @@ -979,7 +1002,7 @@ if (socket->address.ss_len != 0) { status_t status = socket->first_info->unbind(socket->first_protocol, (sockaddr*)&socket->address); - if (status < B_OK) + if (status != B_OK) return status; } @@ -988,7 +1011,7 @@ status_t status = socket->first_info->bind(socket->first_protocol, (sockaddr*)address); - if (status < B_OK) { + if (status != B_OK) { // clear address again, as binding failed socket->address.ss_len = 0; } @@ -1007,7 +1030,7 @@ if (socket->address.ss_len == 0) { // try to bind first status_t status = socket_bind(socket, NULL, 0); - if (status < B_OK) + if (status != B_OK) return status; } @@ -1201,7 +1224,7 @@ status_t status = socket->first_info->read_data( socket->first_protocol, totalLength, flags, &buffer); - if (status < B_OK) + if (status != B_OK) return status; // process ancillary data @@ -1440,7 +1463,7 @@ status = socket->first_info->send_data(socket->first_protocol, buffer); } - if (status < B_OK) { + if (status != B_OK) { size_t sizeAfterSend = buffer->size; gNetBufferModule.free(buffer); @@ -1666,7 +1689,7 @@ new (&sSocketList) SocketList; mutex_init(&sSocketLock, "socket list"); -#ifdef ADD_DEBUGGER_COMMANDS +#if ENABLE_DEBUGGER_COMMANDS add_debugger_command("sockets", dump_sockets, "lists all sockets"); add_debugger_command("socket", dump_socket, "dumps a socket"); #endif @@ -1676,7 +1699,7 @@ ASSERT(sSocketList.IsEmpty()); mutex_destroy(&sSocketLock); -#ifdef ADD_DEBUGGER_COMMANDS +#if ENABLE_DEBUGGER_COMMANDS remove_debugger_command("socket", dump_socket); remove_debugger_command("sockets", dump_sockets); #endif