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