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