1 /* $OpenBSD: rf_debugprint.c,v 1.3 2002/12/16 07:01:03 tdeval Exp $ */
2 /* $NetBSD: rf_debugprint.c,v 1.3 1999/02/05 00:06:08 oster Exp $ */
3
4 /*
5 * Copyright (c) 1995 Carnegie-Mellon University.
6 * All rights reserved.
7 *
8 * Author: Mark Holland
9 *
10 * Permission to use, copy, modify and distribute this software and
11 * its documentation is hereby granted, provided that both the copyright
12 * notice and this permission notice appear in all copies of the
13 * software, derivative works or modified versions, and any portions
14 * thereof, and that both notices appear in supporting documentation.
15 *
16 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
17 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
18 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
19 *
20 * Carnegie Mellon requests users of this software to return to
21 *
22 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
23 * School of Computer Science
24 * Carnegie Mellon University
25 * Pittsburgh PA 15213-3890
26 *
27 * any improvements or extensions that they make and grant Carnegie the
28 * rights to redistribute these changes.
29 */
30
31 /*
32 * Code to do debug printfs. Calls to rf_debug_printf cause the corresponding
33 * information to be printed to a circular buffer rather than the screen.
34 * The point is to try and minimize the timing variations induced by the
35 * printfs, and to capture only the printf's immediately preceding a failure.
36 */
37
38 #include "rf_types.h"
39 #include "rf_threadstuff.h"
40 #include "rf_debugprint.h"
41 #include "rf_general.h"
42 #include "rf_options.h"
43
44 #include <sys/param.h>
45
46 struct RF_Entry_s {
47 char *cstring;
48 void *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
49 };
50
51 /* space for 1k lines */
52 #define BUFSHIFT 10
53 #define BUFSIZE (1<<BUFSHIFT)
54 #define BUFMASK (BUFSIZE-1)
55
56 static struct RF_Entry_s rf_debugprint_buf[BUFSIZE];
57 static int rf_debugprint_index = 0;
58 RF_DECLARE_STATIC_MUTEX(rf_debug_print_mutex);
59
60 int
rf_ConfigureDebugPrint(RF_ShutdownList_t ** listp)61 rf_ConfigureDebugPrint(RF_ShutdownList_t **listp)
62 {
63 int rc;
64
65 rc = rf_create_managed_mutex(listp, &rf_debug_print_mutex);
66 if (rc) {
67 RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n",
68 __FILE__, __LINE__, rc);
69 return (rc);
70 }
71
72 rf_clear_debug_print_buffer();
73 return (0);
74 }
75
76 void
rf_clear_debug_print_buffer(void)77 rf_clear_debug_print_buffer(void)
78 {
79 int i;
80
81 for (i = 0; i < BUFSIZE; i++)
82 rf_debugprint_buf[i].cstring = NULL;
83 rf_debugprint_index = 0;
84 }
85
86 void
rf_debug_printf(char * s,void * a1,void * a2,void * a3,void * a4,void * a5,void * a6,void * a7,void * a8)87 rf_debug_printf(char *s, void *a1, void *a2, void *a3, void *a4, void *a5,
88 void *a6, void *a7, void *a8)
89 {
90 int idx;
91
92 if (rf_debugPrintUseBuffer) {
93
94 RF_LOCK_MUTEX(rf_debug_print_mutex);
95 idx = rf_debugprint_index;
96 rf_debugprint_index = (rf_debugprint_index + 1) & BUFMASK;
97 RF_UNLOCK_MUTEX(rf_debug_print_mutex);
98
99 rf_debugprint_buf[idx].cstring = s;
100 rf_debugprint_buf[idx].a1 = a1;
101 rf_debugprint_buf[idx].a2 = a2;
102 rf_debugprint_buf[idx].a3 = a3;
103 rf_debugprint_buf[idx].a4 = a4;
104 rf_debugprint_buf[idx].a5 = a5;
105 rf_debugprint_buf[idx].a6 = a6;
106 rf_debugprint_buf[idx].a7 = a7;
107 rf_debugprint_buf[idx].a8 = a8;
108 } else {
109 printf(s, a1, a2, a3, a4, a5, a6, a7, a8);
110 }
111 }
112
113 void
rf_print_debug_buffer(void)114 rf_print_debug_buffer(void)
115 {
116 rf_spill_debug_buffer(NULL);
117 }
118
119 void
rf_spill_debug_buffer(char * fname)120 rf_spill_debug_buffer(char *fname)
121 {
122 int i;
123
124 if (!rf_debugPrintUseBuffer)
125 return;
126
127 RF_LOCK_MUTEX(rf_debug_print_mutex);
128
129 for (i = rf_debugprint_index + 1; i != rf_debugprint_index;
130 i = (i + 1) & BUFMASK)
131 if (rf_debugprint_buf[i].cstring)
132 printf(rf_debugprint_buf[i].cstring,
133 rf_debugprint_buf[i].a1, rf_debugprint_buf[i].a2,
134 rf_debugprint_buf[i].a3, rf_debugprint_buf[i].a4,
135 rf_debugprint_buf[i].a5, rf_debugprint_buf[i].a6,
136 rf_debugprint_buf[i].a7, rf_debugprint_buf[i].a8);
137
138 printf(rf_debugprint_buf[i].cstring,
139 rf_debugprint_buf[i].a1, rf_debugprint_buf[i].a2,
140 rf_debugprint_buf[i].a3, rf_debugprint_buf[i].a4,
141 rf_debugprint_buf[i].a5, rf_debugprint_buf[i].a6,
142 rf_debugprint_buf[i].a7, rf_debugprint_buf[i].a8);
143
144 RF_UNLOCK_MUTEX(rf_debug_print_mutex);
145 }
146