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