rf_debugprint.c revision 1.4 1 /* $NetBSD: rf_debugprint.c,v 1.4 2001/10/04 15:58:52 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 (at) 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 #include <dev/raidframe/raidframevar.h>
37
38 #include "rf_threadstuff.h"
39 #include "rf_debugprint.h"
40 #include "rf_general.h"
41 #include "rf_options.h"
42
43 #include <sys/param.h>
44
45 struct RF_Entry_s {
46 char *cstring;
47 void *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
48 };
49 /* space for 1k lines */
50 #define BUFSHIFT 10
51 #define BUFSIZE (1<<BUFSHIFT)
52 #define BUFMASK (BUFSIZE-1)
53
54 static struct RF_Entry_s rf_debugprint_buf[BUFSIZE];
55 static int rf_debugprint_index = 0;
56 RF_DECLARE_STATIC_MUTEX(rf_debug_print_mutex)
57 int rf_ConfigureDebugPrint(listp)
58 RF_ShutdownList_t **listp;
59 {
60 int rc;
61
62 rc = rf_create_managed_mutex(listp, &rf_debug_print_mutex);
63 if (rc) {
64 RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
65 __LINE__, rc);
66 return (rc);
67 }
68 rf_clear_debug_print_buffer();
69 return (0);
70 }
71
72 void
73 rf_clear_debug_print_buffer()
74 {
75 int i;
76
77 for (i = 0; i < BUFSIZE; i++)
78 rf_debugprint_buf[i].cstring = NULL;
79 rf_debugprint_index = 0;
80 }
81
82 void
83 rf_debug_printf(s, a1, a2, a3, a4, a5, a6, a7, a8)
84 char *s;
85 void *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
86 {
87 int idx;
88
89 if (rf_debugPrintUseBuffer) {
90
91 RF_LOCK_MUTEX(rf_debug_print_mutex);
92 idx = rf_debugprint_index;
93 rf_debugprint_index = (rf_debugprint_index + 1) & BUFMASK;
94 RF_UNLOCK_MUTEX(rf_debug_print_mutex);
95
96 rf_debugprint_buf[idx].cstring = s;
97 rf_debugprint_buf[idx].a1 = a1;
98 rf_debugprint_buf[idx].a2 = a2;
99 rf_debugprint_buf[idx].a3 = a3;
100 rf_debugprint_buf[idx].a4 = a4;
101 rf_debugprint_buf[idx].a5 = a5;
102 rf_debugprint_buf[idx].a6 = a6;
103 rf_debugprint_buf[idx].a7 = a7;
104 rf_debugprint_buf[idx].a8 = a8;
105 } else {
106 printf(s, a1, a2, a3, a4, a5, a6, a7, a8);
107 }
108 }
109
110 void
111 rf_print_debug_buffer()
112 {
113 rf_spill_debug_buffer(NULL);
114 }
115
116 void
117 rf_spill_debug_buffer(fname)
118 char *fname;
119 {
120 int i;
121
122 if (!rf_debugPrintUseBuffer)
123 return;
124
125 RF_LOCK_MUTEX(rf_debug_print_mutex);
126
127 for (i = rf_debugprint_index + 1; i != rf_debugprint_index; i = (i + 1) & BUFMASK)
128 if (rf_debugprint_buf[i].cstring)
129 printf(rf_debugprint_buf[i].cstring, rf_debugprint_buf[i].a1, rf_debugprint_buf[i].a2, rf_debugprint_buf[i].a3,
130 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);
131 printf(rf_debugprint_buf[i].cstring, rf_debugprint_buf[i].a1, rf_debugprint_buf[i].a2, rf_debugprint_buf[i].a3,
132 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);
133 RF_UNLOCK_MUTEX(rf_debug_print_mutex);
134 }
135