bgpd: Add some peer_lock/unlock debug code
Finding memory leaks associated with the peer data structure
is incredibly hard, add some code to allow you to find this
leaked code when needed.
Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
diff --git a/bgpd/bgpd.c b/bgpd/bgpd.c
index 4b69313..f77aa72 100644
--- a/bgpd/bgpd.c
+++ b/bgpd/bgpd.c
@@ -745,10 +745,14 @@
/* increase reference count on a struct peer */
struct peer *
-peer_lock (struct peer *peer)
+peer_lock_with_caller (const char *name, struct peer *peer)
{
assert (peer && (peer->lock >= 0));
-
+
+#if 0
+ zlog_debug("%s peer_lock %p %d", name, peer, peer->lock);
+#endif
+
peer->lock++;
return peer;
@@ -758,30 +762,22 @@
* struct peer is freed and NULL returned if last reference
*/
struct peer *
-peer_unlock (struct peer *peer)
+peer_unlock_with_caller (const char *name, struct peer *peer)
{
assert (peer && (peer->lock > 0));
-
+
+#if 0
+ zlog_debug("%s peer_unlock %p %d", name, peer, peer->lock);
+#endif
+
peer->lock--;
if (peer->lock == 0)
{
-#if 0
- zlog_debug ("unlocked and freeing");
- zlog_backtrace (LOG_DEBUG);
-#endif
peer_free (peer);
return NULL;
}
-#if 0
- if (peer->lock == 1)
- {
- zlog_debug ("unlocked to 1");
- zlog_backtrace (LOG_DEBUG);
- }
-#endif
-
return peer;
}
diff --git a/bgpd/bgpd.h b/bgpd/bgpd.h
index 58d1eca..7ae0acb 100644
--- a/bgpd/bgpd.h
+++ b/bgpd/bgpd.h
@@ -851,8 +851,17 @@
extern struct peer_group *peer_group_get (struct bgp *, const char *);
extern struct peer *peer_lookup_with_open (union sockunion *, as_t, struct in_addr *,
int *);
-extern struct peer *peer_lock (struct peer *);
-extern struct peer *peer_unlock (struct peer *);
+
+/*
+ * Peers are incredibly easy to memory leak
+ * due to the various ways that they are actually used
+ * Provide some functionality to debug locks and unlocks
+ */
+extern struct peer *peer_lock_with_caller(const char *, struct peer *);
+extern struct peer *peer_unlock_with_caller(const char *, struct peer *);
+#define peer_unlock(A) peer_unlock_with_caller(__FUNCTION__, (A))
+#define peer_lock(B) peer_lock_with_caller(__FUNCTION__, (B))
+
extern bgp_peer_sort_t peer_sort (struct peer *peer);
extern int peer_active (struct peer *);
extern int peer_active_nego (struct peer *);