From 2f5b1d1570111e50c4639380cdc0d731decf8f67 Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Tue, 5 May 2015 15:09:59 +0300 Subject: [PATCH] net: Add a way to see who is trying to alloc/dealloc a net_buf Change-Id: Ib7813bc5809f85f27aa4fe06583b64461741466a Signed-off-by: Jukka Rissanen --- include/net/net_buf.h | 23 +++++++++++++++++++++++ net/ip/net_buf.c | 24 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/include/net/net_buf.h b/include/net/net_buf.h index 489eae0e647..c1ac84e2ca1 100644 --- a/include/net/net_buf.h +++ b/include/net/net_buf.h @@ -46,6 +46,11 @@ #include "contiki/ip/uip.h" #include "contiki/packetbuf.h" +#if defined(CONFIG_NETWORKING_WITH_LOGGING) +#undef DEBUG_NET_BUFS +#define DEBUG_NET_BUFS +#endif + struct net_context; /*! The default MTU is 1280 (minimum IPv6 packet size) + LL header @@ -172,7 +177,13 @@ struct net_buf { * * @return Network buffer if successful, NULL otherwise. */ +/* Get buffer from the available buffers pool */ +#ifdef DEBUG_NET_BUFS +#define net_buf_get(context) net_buf_get_debug(context, __FUNCTION__, __LINE__) +struct net_buf *net_buf_get_debug(struct net_context *context, const char *caller, int line); +#else struct net_buf *net_buf_get(struct net_context *context); +#endif /*! * @brief Get buffer from pool but also reserve headroom for @@ -185,7 +196,13 @@ struct net_buf *net_buf_get(struct net_context *context); * * @return Network buffer if successful, NULL otherwise. */ +/* Same as net_buf_get, but also reserve headroom for potential headers */ +#ifdef DEBUG_NET_BUFS +#define net_buf_get_reserve(res) net_buf_get_reserve_debug(res,__FUNCTION__,__LINE__) +struct net_buf *net_buf_get_reserve_debug(uint16_t reserve_head, const char *caller, int line); +#else struct net_buf *net_buf_get_reserve(uint16_t reserve_head); +#endif /*! * @brief Place buffer back into the available buffers pool. @@ -197,7 +214,13 @@ struct net_buf *net_buf_get_reserve(uint16_t reserve_head); * @param buf Network buffer to release. * */ +/* Place buffer back into the available buffers pool */ +#ifdef DEBUG_NET_BUFS +#define net_buf_put(buf) net_buf_put_debug(buf, __FUNCTION__, __LINE__) +void net_buf_put_debug(struct net_buf *buf, const char *caller, int line); +#else void net_buf_put(struct net_buf *buf); +#endif /*! * @brief Prepare data to be added at the end of the buffer. diff --git a/net/ip/net_buf.c b/net/ip/net_buf.c index 7d1da9f7510..db1944f4c04 100644 --- a/net/ip/net_buf.c +++ b/net/ip/net_buf.c @@ -53,13 +53,21 @@ extern struct net_tuple *net_context_get_tuple(struct net_context *context); static struct net_buf buffers[NUM_BUFS]; static struct nano_fifo free_bufs; +#ifdef DEBUG_NET_BUFS +struct net_buf *net_buf_get_reserve_debug(uint16_t reserve_head, const char *caller, int line) +#else struct net_buf *net_buf_get_reserve(uint16_t reserve_head) +#endif { struct net_buf *buf; buf = nano_fifo_get(&free_bufs); if (!buf) { +#ifdef DEBUG_NET_BUFS + NET_ERR("Failed to get free buffer (%s():%d)\n", caller, line); +#else NET_ERR("Failed to get free buffer\n"); +#endif return NULL; } @@ -71,7 +79,11 @@ struct net_buf *net_buf_get_reserve(uint16_t reserve_head) return buf; } +#ifdef DEBUG_NET_BUFS +struct net_buf *net_buf_get_debug(struct net_context *context, const char *caller, int line) +#else struct net_buf *net_buf_get(struct net_context *context) +#endif { struct net_buf *buf; struct net_tuple *tuple; @@ -94,7 +106,11 @@ struct net_buf *net_buf_get(struct net_context *context) break; } +#ifdef DEBUG_NET_BUFS + buf = net_buf_get_reserve_debug(reserve, caller, line); +#else buf = net_buf_get_reserve(reserve); +#endif if (!buf) { return buf; } @@ -104,9 +120,17 @@ struct net_buf *net_buf_get(struct net_context *context) return buf; } +#ifdef DEBUG_NET_BUFS +void net_buf_put_debug(struct net_buf *buf, const char *caller, int line) +#else void net_buf_put(struct net_buf *buf) +#endif { +#ifdef DEBUG_NET_BUFS + NET_DBG("buf %p (%s():%d)\n", buf, caller, line); +#else NET_DBG("buf %p\n", buf); +#endif nano_fifo_put(&free_bufs, buf); }