aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Kelly <ckelly@ozmodevices.com>2012-02-20 16:12:35 -0500
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2012-02-24 12:26:52 -0500
commit066b2229410f2f58fa91baedd22b4dcf048e28dd (patch)
treef2ce1ae3878d72254c6eec9b9e04f38859e09a65
parent56ff32fe1cf24d624ca4a68a409753fb3fd593f6 (diff)
staging: ozwpan: Added debug support
Added tracing facilities and also memory allocation and URB tracking. This is for debugging purposes and is all optional and can be switched out at compile time. Signed-off-by: Chris Kelly <ckelly@ozmodevices.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-rw-r--r--drivers/staging/ozwpan/ozalloc.c107
-rw-r--r--drivers/staging/ozwpan/ozalloc.h28
-rw-r--r--drivers/staging/ozwpan/oztrace.c36
-rw-r--r--drivers/staging/ozwpan/oztrace.h35
-rw-r--r--drivers/staging/ozwpan/ozurbparanoia.c53
-rw-r--r--drivers/staging/ozwpan/ozurbparanoia.h19
6 files changed, 278 insertions, 0 deletions
diff --git a/drivers/staging/ozwpan/ozalloc.c b/drivers/staging/ozwpan/ozalloc.c
new file mode 100644
index 00000000000..fe3cd406e4f
--- /dev/null
+++ b/drivers/staging/ozwpan/ozalloc.c
@@ -0,0 +1,107 @@
1/* -----------------------------------------------------------------------------
2 * Copyright (c) 2011 Ozmo Inc
3 * Released under the GNU General Public License Version 2 (GPLv2).
4 * This file contains debug allocation and free functions. These are turned on
5 * by the configuration switch WANT_DEBUG_KMALLOC. This flags should be turned
6 * off in the release version but facilitate memory leak and corruption during
7 * development.
8 * -----------------------------------------------------------------------------
9 */
10#include <linux/module.h>
11#include "ozconfig.h"
12#include "ozalloc.h"
13#include "oztrace.h"
14#ifdef WANT_DEBUG_KMALLOC
15/*------------------------------------------------------------------------------
16 */
17#define MAGIC_1 0x12848796
18#define MAGIC_2 0x87465920
19#define MAGIC_3 0x80288264
20/*------------------------------------------------------------------------------
21 */
22struct oz_alloc_hdr {
23 int size;
24 int line;
25 unsigned magic;
26 struct list_head link;
27};
28/*------------------------------------------------------------------------------
29 */
30static unsigned long g_total_alloc_size;
31static int g_alloc_count;
32static DEFINE_SPINLOCK(g_alloc_lock);
33static LIST_HEAD(g_alloc_list);
34/*------------------------------------------------------------------------------
35 * Context: any
36 */
37void *oz_alloc_debug(size_t size, gfp_t flags, int line)
38{
39 struct oz_alloc_hdr *hdr = (struct oz_alloc_hdr *)
40 kmalloc(size + sizeof(struct oz_alloc_hdr) +
41 sizeof(unsigned), flags);
42 if (hdr) {
43 unsigned long irq_state;
44 hdr->size = size;
45 hdr->line = line;
46 hdr->magic = MAGIC_1;
47 *(unsigned *)(((u8 *)(hdr + 1)) + size) = MAGIC_2;
48 spin_lock_irqsave(&g_alloc_lock, irq_state);
49 g_total_alloc_size += size;
50 g_alloc_count++;
51 list_add_tail(&hdr->link, &g_alloc_list);
52 spin_unlock_irqrestore(&g_alloc_lock, irq_state);
53 return hdr + 1;
54 }
55 return 0;
56}
57/*------------------------------------------------------------------------------
58 * Context: any
59 */
60void oz_free_debug(void *p)
61{
62 if (p) {
63 struct oz_alloc_hdr *hdr = (struct oz_alloc_hdr *)
64 (((unsigned char *)p) - sizeof(struct oz_alloc_hdr));
65 if (hdr->magic == MAGIC_1) {
66 unsigned long irq_state;
67 if (*(unsigned *)(((u8 *)(hdr + 1)) + hdr->size)
68 != MAGIC_2) {
69 oz_trace("oz_free_debug: Corrupted beyond"
70 " %p size %d\n", hdr+1, hdr->size);
71 return;
72 }
73 spin_lock_irqsave(&g_alloc_lock, irq_state);
74 g_total_alloc_size -= hdr->size;
75 g_alloc_count--;
76 list_del(&hdr->link);
77 spin_unlock_irqrestore(&g_alloc_lock, irq_state);
78 hdr->magic = MAGIC_3;
79 kfree(hdr);
80 } else {
81 oz_trace("oz_free_debug: Invalid magic number %u\n",
82 hdr->magic);
83 }
84 }
85}
86/*------------------------------------------------------------------------------
87 * Context: process
88 */
89void oz_trace_leaks(void)
90{
91#ifdef WANT_TRACE
92 struct list_head *e;
93 oz_trace("Total alloc size:%ld Alloc count:%d\n",
94 g_total_alloc_size, g_alloc_count);
95 if (g_alloc_count)
96 oz_trace("Trace of leaks.\n");
97 else
98 oz_trace("No memory leaks.\n");
99 list_for_each(e, &g_alloc_list) {
100 struct oz_alloc_hdr *hdr =
101 container_of(e, struct oz_alloc_hdr, link);
102 oz_trace("LEAK size %d line %d\n", hdr->size, hdr->line);
103 }
104#endif /* #ifdef WANT_TRACE */
105}
106#endif /* #ifdef WANT_DEBUG_KMALLOC */
107
diff --git a/drivers/staging/ozwpan/ozalloc.h b/drivers/staging/ozwpan/ozalloc.h
new file mode 100644
index 00000000000..11a7a164288
--- /dev/null
+++ b/drivers/staging/ozwpan/ozalloc.h
@@ -0,0 +1,28 @@
1/* -----------------------------------------------------------------------------
2 * Copyright (c) 2011 Ozmo Inc
3 * Released under the GNU General Public License Version 2 (GPLv2).
4 * -----------------------------------------------------------------------------
5 */
6#ifndef _OZALLOC_H
7#define _OZALLOC_H
8
9#include <linux/slab.h>
10
11#ifdef WANT_DEBUG_KMALLOC
12
13void *oz_alloc_debug(size_t size, gfp_t flags, int line);
14void oz_free_debug(void *p);
15void oz_trace_leaks(void);
16#define oz_alloc(__s, __f) oz_alloc_debug(__s, __f, __LINE__)
17#define oz_free oz_free_debug
18
19#else
20
21
22#define oz_alloc kmalloc
23#define oz_free kfree
24#define oz_trace_leaks()
25
26#endif /* #ifdef WANT_DEBUG_KMALLOC */
27
28#endif /* _OZALLOC_H */
diff --git a/drivers/staging/ozwpan/oztrace.c b/drivers/staging/ozwpan/oztrace.c
new file mode 100644
index 00000000000..353ead24fd7
--- /dev/null
+++ b/drivers/staging/ozwpan/oztrace.c
@@ -0,0 +1,36 @@
1/* -----------------------------------------------------------------------------
2 * Copyright (c) 2011 Ozmo Inc
3 * Released under the GNU General Public License Version 2 (GPLv2).
4 * -----------------------------------------------------------------------------
5 */
6#include "ozconfig.h"
7#include "oztrace.h"
8
9#ifdef WANT_VERBOSE_TRACE
10unsigned long trace_flags =
11 0
12#ifdef WANT_TRACE_STREAM
13 | OZ_TRACE_STREAM
14#endif /* WANT_TRACE_STREAM */
15#ifdef WANT_TRACE_URB
16 | OZ_TRACE_URB
17#endif /* WANT_TRACE_URB */
18
19#ifdef WANT_TRACE_CTRL_DETAIL
20 | OZ_TRACE_CTRL_DETAIL
21#endif /* WANT_TRACE_CTRL_DETAIL */
22
23#ifdef WANT_TRACE_HUB
24 | OZ_TRACE_HUB
25#endif /* WANT_TRACE_HUB */
26
27#ifdef WANT_TRACE_RX_FRAMES
28 | OZ_TRACE_RX_FRAMES
29#endif /* WANT_TRACE_RX_FRAMES */
30
31#ifdef WANT_TRACE_TX_FRAMES
32 | OZ_TRACE_TX_FRAMES
33#endif /* WANT_TRACE_TX_FRAMES */
34 ;
35#endif /* WANT_VERBOSE_TRACE */
36
diff --git a/drivers/staging/ozwpan/oztrace.h b/drivers/staging/ozwpan/oztrace.h
new file mode 100644
index 00000000000..8293b24c5a7
--- /dev/null
+++ b/drivers/staging/ozwpan/oztrace.h
@@ -0,0 +1,35 @@
1/* -----------------------------------------------------------------------------
2 * Copyright (c) 2011 Ozmo Inc
3 * Released under the GNU General Public License Version 2 (GPLv2).
4 * -----------------------------------------------------------------------------
5 */
6#ifndef _OZTRACE_H_
7#define _OZTRACE_H_
8#include "ozconfig.h"
9
10#define TRACE_PREFIX KERN_ALERT "OZWPAN: "
11
12#ifdef WANT_TRACE
13#define oz_trace(...) printk(TRACE_PREFIX __VA_ARGS__)
14#ifdef WANT_VERBOSE_TRACE
15extern unsigned long trace_flags;
16#define oz_trace2(_flag, ...) \
17 do { if (trace_flags & _flag) printk(TRACE_PREFIX __VA_ARGS__); \
18 } while (0)
19#else
20#define oz_trace2(...)
21#endif /* #ifdef WANT_VERBOSE_TRACE */
22#else
23#define oz_trace(...)
24#define oz_trace2(...)
25#endif /* #ifdef WANT_TRACE */
26
27#define OZ_TRACE_STREAM 0x1
28#define OZ_TRACE_URB 0x2
29#define OZ_TRACE_CTRL_DETAIL 0x4
30#define OZ_TRACE_HUB 0x8
31#define OZ_TRACE_RX_FRAMES 0x10
32#define OZ_TRACE_TX_FRAMES 0x20
33
34#endif /* Sentry */
35
diff --git a/drivers/staging/ozwpan/ozurbparanoia.c b/drivers/staging/ozwpan/ozurbparanoia.c
new file mode 100644
index 00000000000..55b9afbbe47
--- /dev/null
+++ b/drivers/staging/ozwpan/ozurbparanoia.c
@@ -0,0 +1,53 @@
1/* -----------------------------------------------------------------------------
2 * Copyright (c) 2011 Ozmo Inc
3 * Released under the GNU General Public License Version 2 (GPLv2).
4 * -----------------------------------------------------------------------------
5 */
6#include <linux/usb.h>
7#include "ozconfig.h"
8#ifdef WANT_URB_PARANOIA
9#include "ozurbparanoia.h"
10#include "oztrace.h"
11/*-----------------------------------------------------------------------------
12 */
13#define OZ_MAX_URBS 1000
14struct urb *g_urb_memory[OZ_MAX_URBS];
15int g_nb_urbs;
16DEFINE_SPINLOCK(g_urb_mem_lock);
17/*-----------------------------------------------------------------------------
18 */
19void oz_remember_urb(struct urb *urb)
20{
21 unsigned long irq_state;
22 spin_lock_irqsave(&g_urb_mem_lock, irq_state);
23 if (g_nb_urbs < OZ_MAX_URBS) {
24 g_urb_memory[g_nb_urbs++] = urb;
25 oz_trace("%lu: urb up = %d %p\n", jiffies, g_nb_urbs, urb);
26 } else {
27 oz_trace("ERROR urb buffer full\n");
28 }
29 spin_unlock_irqrestore(&g_urb_mem_lock, irq_state);
30}
31/*------------------------------------------------------------------------------
32 */
33int oz_forget_urb(struct urb *urb)
34{
35 unsigned long irq_state;
36 int i;
37 int rc = -1;
38 spin_lock_irqsave(&g_urb_mem_lock, irq_state);
39 for (i = 0; i < g_nb_urbs; i++) {
40 if (g_urb_memory[i] == urb) {
41 rc = 0;
42 if (--g_nb_urbs > i)
43 memcpy(&g_urb_memory[i], &g_urb_memory[i+1],
44 (g_nb_urbs - i) * sizeof(struct urb *));
45 oz_trace("%lu: urb down = %d %p\n",
46 jiffies, g_nb_urbs, urb);
47 }
48 }
49 spin_unlock_irqrestore(&g_urb_mem_lock, irq_state);
50 return rc;
51}
52#endif /* #ifdef WANT_URB_PARANOIA */
53
diff --git a/drivers/staging/ozwpan/ozurbparanoia.h b/drivers/staging/ozwpan/ozurbparanoia.h
new file mode 100644
index 00000000000..00f5a3a81bc
--- /dev/null
+++ b/drivers/staging/ozwpan/ozurbparanoia.h
@@ -0,0 +1,19 @@
1#ifndef _OZURBPARANOIA_H
2#define _OZURBPARANOIA_H
3/* -----------------------------------------------------------------------------
4 * Released under the GNU General Public License Version 2 (GPLv2).
5 * Copyright (c) 2011 Ozmo Inc
6 * -----------------------------------------------------------------------------
7 */
8
9#ifdef WANT_URB_PARANOIA
10void oz_remember_urb(struct urb *urb);
11int oz_forget_urb(struct urb *urb);
12#else
13#define oz_remember_urb(__x)
14#define oz_forget_urb(__x) 0
15#endif /* WANT_URB_PARANOIA */
16
17
18#endif /* _OZURBPARANOIA_H */
19