xref: /netbsd-src/sys/dev/raidframe/rf_debugprint.c (revision dc306354b0b29af51801a7632f1e95265a68cd81)
1 /*	$NetBSD: rf_debugprint.c,v 1.1 1998/11/13 04:20:28 oster Exp $	*/
2 /*
3  * Copyright (c) 1995 Carnegie-Mellon University.
4  * All rights reserved.
5  *
6  * Author: Mark Holland
7  *
8  * Permission to use, copy, modify and distribute this software and
9  * its documentation is hereby granted, provided that both the copyright
10  * notice and this permission notice appear in all copies of the
11  * software, derivative works or modified versions, and any portions
12  * thereof, and that both notices appear in supporting documentation.
13  *
14  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
16  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17  *
18  * Carnegie Mellon requests users of this software to return to
19  *
20  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
21  *  School of Computer Science
22  *  Carnegie Mellon University
23  *  Pittsburgh PA 15213-3890
24  *
25  * any improvements or extensions that they make and grant Carnegie the
26  * rights to redistribute these changes.
27  */
28 
29 /*
30  * Code to do debug printfs. Calls to rf_debug_printf cause the corresponding
31  * information to be printed to a circular buffer rather than the screen.
32  * The point is to try and minimize the timing variations induced by the
33  * printfs, and to capture only the printf's immediately preceding a failure.
34  */
35 
36 /* :
37  * Log: rf_debugprint.c,v
38  * Revision 1.13  1996/08/07 21:08:31  jimz
39  * remove bogus ; from mutex decl
40  *
41  * Revision 1.12  1996/06/10  11:55:47  jimz
42  * Straightened out some per-array/not-per-array distinctions, fixed
43  * a couple bugs related to confusion. Added shutdown lists. Removed
44  * layout shutdown function (now subsumed by shutdown lists).
45  *
46  * Revision 1.11  1996/06/05  18:06:02  jimz
47  * Major code cleanup. The Great Renaming is now done.
48  * Better modularity. Better typing. Fixed a bunch of
49  * synchronization bugs. Made a lot of global stuff
50  * per-desc or per-array. Removed dead code.
51  *
52  * Revision 1.10  1996/05/30  23:22:16  jimz
53  * bugfixes of serialization, timing problems
54  * more cleanup
55  *
56  * Revision 1.9  1996/05/27  18:56:37  jimz
57  * more code cleanup
58  * better typing
59  * compiles in all 3 environments
60  *
61  * Revision 1.8  1996/05/23  00:33:23  jimz
62  * code cleanup: move all debug decls to rf_options.c, all extern
63  * debug decls to rf_options.h, all debug vars preceded by rf_
64  *
65  * Revision 1.7  1996/05/20  16:16:06  jimz
66  * switch to rf_{mutex,cond}_{init,destroy}
67  *
68  * Revision 1.6  1996/05/18  19:51:34  jimz
69  * major code cleanup- fix syntax, make some types consistent,
70  * add prototypes, clean out dead code, et cetera
71  *
72  * Revision 1.5  1995/12/01  16:00:45  root
73  * added copyright info
74  *
75  */
76 
77 #include "rf_types.h"
78 #include "rf_threadstuff.h"
79 #include "rf_debugprint.h"
80 #include "rf_general.h"
81 #include "rf_options.h"
82 
83 #include <sys/param.h>
84 
85 struct RF_Entry_s {
86    char *cstring;
87    void *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
88 };
89 
90 /* space for 1k lines */
91 #define BUFSHIFT 10
92 #define BUFSIZE  (1<<BUFSHIFT)
93 #define BUFMASK  (BUFSIZE-1)
94 
95 static struct RF_Entry_s rf_debugprint_buf[BUFSIZE];
96 static int rf_debugprint_index = 0;
97 RF_DECLARE_STATIC_MUTEX(rf_debug_print_mutex)
98 
99 int rf_ConfigureDebugPrint(listp)
100   RF_ShutdownList_t  **listp;
101 {
102   int rc;
103 
104   rc = rf_create_managed_mutex(listp, &rf_debug_print_mutex);
105   if (rc) {
106     RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
107       __LINE__, rc);
108     return(rc);
109   }
110   rf_clear_debug_print_buffer();
111   return(0);
112 }
113 
114 void rf_clear_debug_print_buffer()
115 {
116   int i;
117 
118   for (i=0; i<BUFSIZE; i++)
119     rf_debugprint_buf[i].cstring = NULL;
120   rf_debugprint_index = 0;
121 }
122 
123 void rf_debug_printf(s,a1,a2,a3,a4,a5,a6,a7,a8)
124 char *s;
125 void *a1,*a2,*a3,*a4,*a5,*a6,*a7,*a8;
126 {
127   int idx;
128 
129   if (rf_debugPrintUseBuffer) {
130 
131     RF_LOCK_MUTEX(rf_debug_print_mutex);
132     idx = rf_debugprint_index;
133     rf_debugprint_index = (rf_debugprint_index+1) & BUFMASK;
134     RF_UNLOCK_MUTEX(rf_debug_print_mutex);
135 
136     rf_debugprint_buf[idx].cstring = s;
137     rf_debugprint_buf[idx].a1 = a1;
138     rf_debugprint_buf[idx].a2 = a2;
139     rf_debugprint_buf[idx].a3 = a3;
140     rf_debugprint_buf[idx].a4 = a4;
141     rf_debugprint_buf[idx].a5 = a5;
142     rf_debugprint_buf[idx].a6 = a6;
143     rf_debugprint_buf[idx].a7 = a7;
144     rf_debugprint_buf[idx].a8 = a8;
145   }
146   else {
147     printf(s,a1,a2,a3,a4,a5,a6,a7,a8);
148   }
149 }
150 
151 void rf_print_debug_buffer()
152 {
153   rf_spill_debug_buffer(NULL);
154 }
155 
156 void rf_spill_debug_buffer(fname)
157   char  *fname;
158 {
159   int i;
160 #ifndef KERNEL
161   FILE *fp;
162 #endif /* !KERNEL */
163 
164   if (!rf_debugPrintUseBuffer)
165     return;
166 
167   RF_LOCK_MUTEX(rf_debug_print_mutex);
168 #ifndef KERNEL
169   fp = (fname) ? fopen(fname,"w") : stdout;
170   if (!fp) {printf("Unable to open file %s for writing\n",fname); return;}
171   for (i=rf_debugprint_index+1; i != rf_debugprint_index; i = (i+1)&BUFMASK) if (rf_debugprint_buf[i].cstring)
172     fprintf(fp,rf_debugprint_buf[i].cstring,rf_debugprint_buf[i].a1,rf_debugprint_buf[i].a2,rf_debugprint_buf[i].a3,
173 	    rf_debugprint_buf[i].a4,rf_debugprint_buf[i].a5,rf_debugprint_buf[i].a6,rf_debugprint_buf[i].a7,rf_debugprint_buf[i].a8);
174   fprintf(fp,rf_debugprint_buf[i].cstring,rf_debugprint_buf[i].a1,rf_debugprint_buf[i].a2,rf_debugprint_buf[i].a3,
175 	  rf_debugprint_buf[i].a4,rf_debugprint_buf[i].a5,rf_debugprint_buf[i].a6,rf_debugprint_buf[i].a7,rf_debugprint_buf[i].a8);
176   fclose(fp);
177 #else /* !KERNEL */
178   for (i=rf_debugprint_index+1; i != rf_debugprint_index; i = (i+1)&BUFMASK) if (rf_debugprint_buf[i].cstring)
179     printf(rf_debugprint_buf[i].cstring,rf_debugprint_buf[i].a1,rf_debugprint_buf[i].a2,rf_debugprint_buf[i].a3,
180 	   rf_debugprint_buf[i].a4,rf_debugprint_buf[i].a5,rf_debugprint_buf[i].a6,rf_debugprint_buf[i].a7,rf_debugprint_buf[i].a8);
181   printf(rf_debugprint_buf[i].cstring,rf_debugprint_buf[i].a1,rf_debugprint_buf[i].a2,rf_debugprint_buf[i].a3,
182 	 rf_debugprint_buf[i].a4,rf_debugprint_buf[i].a5,rf_debugprint_buf[i].a6,rf_debugprint_buf[i].a7,rf_debugprint_buf[i].a8);
183 #endif /* !KERNEL */
184   RF_UNLOCK_MUTEX(rf_debug_print_mutex);
185 }
186