2 * Copyright 2003 Digi International (www.digi.com)
3 * Scott H Kilau <Scott_Kilau at digi dot com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
12 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 * PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * NOTE TO LINUX KERNEL HACKERS: DO NOT REFORMAT THIS CODE!
22 * This is shared code between Digi's CVS archive and the
23 * Linux Kernel sources.
24 * Changing the source just for reformatting needlessly breaks
25 * our CVS diff history.
27 * Send any bug fixes/changes to: Eng.Linux at digi dot com.
32 /* $Id: dgap_trace.c,v 1.1 2009/10/23 14:01:57 markh Exp $ */
34 #include <linux/kernel.h>
35 #include <linux/sched.h> /* For jiffies, task states */
36 #include <linux/interrupt.h> /* For tasklet and interrupt structs/defines */
37 #include <linux/vmalloc.h>
39 #include "dgap_driver.h"
41 #define TRC_TO_CONSOLE 1
43 /* file level globals */
44 static char *dgap_trcbuf; /* the ringbuffer */
46 #if defined(TRC_TO_KMEM)
47 static int dgap_trcbufi = 0; /* index of the tilde at the end of */
50 extern int dgap_trcbuf_size; /* size of the ringbuffer */
52 #if defined(TRC_TO_KMEM)
53 static DEFINE_SPINLOCK(dgap_tracef_lock);
58 #if !defined(TRC_TO_KMEM) && !defined(TRC_TO_CONSOLE)
59 void dgap_tracef(const char *fmt, ...)
64 #else /* !defined(TRC_TO_KMEM) && !defined(TRC_TO_CONSOLE) */
66 void dgap_tracef(const char *fmt, ...)
69 char buf[TRC_MAXMSG+1];
72 static int failed = FALSE;
73 # if defined(TRC_TO_KMEM)
79 # if defined(TRC_TO_KMEM)
80 DGAP_LOCK(dgap_tracef_lock, flags);
83 /* Format buf using fmt and arguments contained in ap. */
85 i = vsprintf(buf, fmt, ap);
89 # if defined(TRC_TO_KMEM)
94 * Now, in addition to (or instead of) printing this stuff out
95 * (which is a buffered operation), also tuck it away into a
96 * corner of memory which can be examined post-crash in kdb.
99 dgap_trcbuf = (char *) vmalloc(dgap_trcbuf_size);
102 printk("dgap: tracing init failed!\n");
106 memset(dgap_trcbuf, '\0', dgap_trcbuf_size);
110 printk("dgap: tracing enabled - " TRC_DTRC
112 (unsigned long)dgap_trcbuf,
116 # if defined(TRC_ON_OVERFLOW_WRAP_AROUND)
118 * This is the less CPU-intensive way to do things. We simply
119 * wrap around before we fall off the end of the buffer. A
120 * tilde (~) demarcates the current end of the trace.
122 * This method should be used if you are concerned about race
123 * conditions as it is less likely to affect the timing of
127 if (dgap_trcbufi + lenbuf >= dgap_trcbuf_size) {
128 /* We are wrapping, so wipe out the last tilde. */
129 dgap_trcbuf[dgap_trcbufi] = '\0';
130 /* put the new string at the beginning of the buffer */
134 strcpy(&dgap_trcbuf[dgap_trcbufi], buf);
135 dgap_trcbufi += lenbuf;
136 dgap_trcbuf[dgap_trcbufi] = '~';
138 # elif defined(TRC_ON_OVERFLOW_SHIFT_BUFFER)
140 * This is the more CPU-intensive way to do things. If we
141 * venture into the last 1/8 of the buffer, we shift the
142 * last 7/8 of the buffer forward, wiping out the first 1/8.
143 * Advantage: No wrap-around, only truncation from the
146 * This method should not be used if you are concerned about
147 * timing changes affecting the behaviour of the driver (ie,
150 strcpy(&dgap_trcbuf[dgap_trcbufi], buf);
151 dgap_trcbufi += lenbuf;
152 dgap_trcbuf[dgap_trcbufi] = '~';
153 dgap_trcbuf[dgap_trcbufi+1] = '\0';
155 /* If we're near the end of the trace buffer... */
156 if (dgap_trcbufi > (dgap_trcbuf_size/8)*7) {
157 /* Wipe out the first eighth to make some more room. */
158 strcpy(dgap_trcbuf, &dgap_trcbuf[dgap_trcbuf_size/8]);
159 dgap_trcbufi = strlen(dgap_trcbuf)-1;
160 /* Plop overflow message at the top of the buffer. */
161 bcopy(TRC_OVERFLOW, dgap_trcbuf, strlen(TRC_OVERFLOW));
164 # error "TRC_ON_OVERFLOW_WRAP_AROUND or TRC_ON_OVERFLOW_SHIFT_BUFFER?"
167 DGAP_UNLOCK(dgap_tracef_lock, flags);
169 # endif /* defined(TRC_TO_KMEM) */
172 #endif /* !defined(TRC_TO_KMEM) && !defined(TRC_TO_CONSOLE) */
181 void dgap_tracer_free(void)