rf_debugprint.c revision 1.2 1 /* $NetBSD: rf_debugprint.c,v 1.2 1999/01/26 02:33:54 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 "rf_types.h"
37 #include "rf_threadstuff.h"
38 #include "rf_debugprint.h"
39 #include "rf_general.h"
40 #include "rf_options.h"
41
42 #include <sys/param.h>
43
44 struct RF_Entry_s {
45 char *cstring;
46 void *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
47 };
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
58 int rf_ConfigureDebugPrint(listp)
59 RF_ShutdownList_t **listp;
60 {
61 int rc;
62
63 rc = rf_create_managed_mutex(listp, &rf_debug_print_mutex);
64 if (rc) {
65 RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
66 __LINE__, rc);
67 return(rc);
68 }
69 rf_clear_debug_print_buffer();
70 return(0);
71 }
72
73 void 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 rf_debug_printf(s,a1,a2,a3,a4,a5,a6,a7,a8)
83 char *s;
84 void *a1,*a2,*a3,*a4,*a5,*a6,*a7,*a8;
85 {
86 int idx;
87
88 if (rf_debugPrintUseBuffer) {
89
90 RF_LOCK_MUTEX(rf_debug_print_mutex);
91 idx = rf_debugprint_index;
92 rf_debugprint_index = (rf_debugprint_index+1) & BUFMASK;
93 RF_UNLOCK_MUTEX(rf_debug_print_mutex);
94
95 rf_debugprint_buf[idx].cstring = s;
96 rf_debugprint_buf[idx].a1 = a1;
97 rf_debugprint_buf[idx].a2 = a2;
98 rf_debugprint_buf[idx].a3 = a3;
99 rf_debugprint_buf[idx].a4 = a4;
100 rf_debugprint_buf[idx].a5 = a5;
101 rf_debugprint_buf[idx].a6 = a6;
102 rf_debugprint_buf[idx].a7 = a7;
103 rf_debugprint_buf[idx].a8 = a8;
104 }
105 else {
106 printf(s,a1,a2,a3,a4,a5,a6,a7,a8);
107 }
108 }
109
110 void rf_print_debug_buffer()
111 {
112 rf_spill_debug_buffer(NULL);
113 }
114
115 void rf_spill_debug_buffer(fname)
116 char *fname;
117 {
118 int i;
119
120 if (!rf_debugPrintUseBuffer)
121 return;
122
123 RF_LOCK_MUTEX(rf_debug_print_mutex);
124
125 for (i=rf_debugprint_index+1; i != rf_debugprint_index; i = (i+1)&BUFMASK) if (rf_debugprint_buf[i].cstring)
126 printf(rf_debugprint_buf[i].cstring,rf_debugprint_buf[i].a1,rf_debugprint_buf[i].a2,rf_debugprint_buf[i].a3,
127 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);
128 printf(rf_debugprint_buf[i].cstring,rf_debugprint_buf[i].a1,rf_debugprint_buf[i].a2,rf_debugprint_buf[i].a3,
129 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);
130 RF_UNLOCK_MUTEX(rf_debug_print_mutex);
131 }
132