Added a debug mode that keeps track of where individual queuebufs are allocated, which is useful when debugging queuebuf leakage problems.

This commit is contained in:
adamdunkels 2010-11-25 08:43:59 +00:00
parent 880233a451
commit 2f66db3ded
2 changed files with 56 additions and 3 deletions

View File

@ -33,7 +33,7 @@
*
* This file is part of the Contiki operating system.
*
* $Id: queuebuf.c,v 1.4 2010/10/12 19:51:28 oliverschmidt Exp $
* $Id: queuebuf.c,v 1.5 2010/11/25 08:43:59 adamdunkels Exp $
*/
/**
@ -54,6 +54,12 @@
#endif
struct queuebuf {
#if QUEUEBUF_DEBUG
struct queuebuf *next;
const char *file;
int line;
clock_time_t time;
#endif /* QUEUEBUF_DEBUG */
uint16_t len;
uint8_t data[PACKETBUF_SIZE];
struct packetbuf_attr attrs[PACKETBUF_NUM_ATTRS];
@ -70,6 +76,11 @@ struct queuebuf_ref {
MEMB(bufmem, struct queuebuf, QUEUEBUF_NUM);
MEMB(refbufmem, struct queuebuf_ref, QUEUEBUF_REF_NUM);
#if QUEUEBUF_DEBUG
#include "lib/list.h"
LIST(queuebuf_list);
#endif /* QUEUEBUF_DEBUG */
#define DEBUG 0
#if DEBUG
#include <stdio.h>
@ -99,8 +110,13 @@ queuebuf_init(void)
#endif /* QUEUEBUF_STATS */
}
/*---------------------------------------------------------------------------*/
#if QUEUEBUF_DEBUG
struct queuebuf *
queuebuf_new_from_packetbuf_debug(const char *file, int line)
#else /* QUEUEBUF_DEBUG */
struct queuebuf *
queuebuf_new_from_packetbuf(void)
#endif /* QUEUEBUF_DEBUG */
{
struct queuebuf *buf;
struct queuebuf_ref *rbuf;
@ -121,6 +137,12 @@ queuebuf_new_from_packetbuf(void)
} else {
buf = memb_alloc(&bufmem);
if(buf != NULL) {
#if QUEUEBUF_DEBUG
list_add(queuebuf_list, buf);
buf->file = file;
buf->line = line;
buf->time = clock_time();
#endif /* QUEUEBUF_DEBUG */
#if QUEUEBUF_STATS
++queuebuf_len;
PRINTF("queuebuf len %d\n", queuebuf_len);
@ -149,6 +171,9 @@ queuebuf_free(struct queuebuf *buf)
--queuebuf_len;
printf("#A q=%d\n", queuebuf_len);
#endif /* QUEUEBUF_STATS */
#if QUEUEBUF_DEBUG
list_remove(queuebuf_list, buf);
#endif /* QUEUEBUF_DEBUG */
} else if(memb_inmemb(&refbufmem, buf)) {
memb_free(&refbufmem, buf);
#if QUEUEBUF_STATS
@ -206,4 +231,18 @@ queuebuf_attr(struct queuebuf *b, uint8_t type)
return b->attrs[type].val;
}
/*---------------------------------------------------------------------------*/
void
queuebuf_debug_print(void)
{
#if QUEUEBUF_DEBUG
struct queuebuf *q;
printf("queuebuf_list: ");
for(q = list_head(queuebuf_list); q != NULL;
q = list_item_next(q)) {
printf("%s,%d,%lu ", q->file, q->line, q->time);
}
printf("\n");
#endif /* QUEUEBUF_DEBUG */
}
/*---------------------------------------------------------------------------*/
/** @} */

View File

@ -41,7 +41,7 @@
*
* This file is part of the Contiki operating system.
*
* $Id: queuebuf.h,v 1.3 2010/10/12 19:51:28 oliverschmidt Exp $
* $Id: queuebuf.h,v 1.4 2010/11/25 08:43:59 adamdunkels Exp $
*/
/**
@ -62,13 +62,25 @@
#define QUEUEBUF_NUM 8
#endif
#ifdef QUEUEBUF_CONF_DEBUG
#define QUEUEBUF_DEBUG QUEUEBUF_CONF_DEBUG
#else /* QUEUEBUF_CONF_DEBUG */
#define QUEUEBUF_DEBUG 0
#endif /* QUEUEBUF_CONF_DEBUG */
struct queuebuf;
void queuebuf_init(void);
#if QUEUEBUF_DEBUG
struct queuebuf *queuebuf_new_from_packetbuf_debug(const char *file, int line);
#define queuebuf_new_from_packetbuf() queuebuf_new_from_packetbuf_debug(__FILE__, __LINE__)
#else /* QUEUEBUF_DEBUG */
struct queuebuf *queuebuf_new_from_packetbuf(void);
void queuebuf_free(struct queuebuf *b);
#endif /* QUEUEBUF_DEBUG */
void queuebuf_to_packetbuf(struct queuebuf *b);
void queuebuf_free(struct queuebuf *b);
void *queuebuf_dataptr(struct queuebuf *b);
int queuebuf_datalen(struct queuebuf *b);
@ -76,6 +88,8 @@ int queuebuf_datalen(struct queuebuf *b);
rimeaddr_t *queuebuf_addr(struct queuebuf *b, uint8_t type);
packetbuf_attr_t queuebuf_attr(struct queuebuf *b, uint8_t type);
void queuebuf_debug_print(void);
#endif /* __QUEUEBUF_H__ */
/** @} */