1 /* $NetBSD: rf_debugprint.c,v 1.5 2001/11/13 07:11:13 lukem 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@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 <sys/cdefs.h> 37 __KERNEL_RCSID(0, "$NetBSD: rf_debugprint.c,v 1.5 2001/11/13 07:11:13 lukem Exp $"); 38 39 #include <dev/raidframe/raidframevar.h> 40 41 #include "rf_threadstuff.h" 42 #include "rf_debugprint.h" 43 #include "rf_general.h" 44 #include "rf_options.h" 45 46 #include <sys/param.h> 47 48 struct RF_Entry_s { 49 char *cstring; 50 void *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8; 51 }; 52 /* space for 1k lines */ 53 #define BUFSHIFT 10 54 #define BUFSIZE (1<<BUFSHIFT) 55 #define BUFMASK (BUFSIZE-1) 56 57 static struct RF_Entry_s rf_debugprint_buf[BUFSIZE]; 58 static int rf_debugprint_index = 0; 59 RF_DECLARE_STATIC_MUTEX(rf_debug_print_mutex) 60 int rf_ConfigureDebugPrint(listp) 61 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", __FILE__, 68 __LINE__, rc); 69 return (rc); 70 } 71 rf_clear_debug_print_buffer(); 72 return (0); 73 } 74 75 void 76 rf_clear_debug_print_buffer() 77 { 78 int i; 79 80 for (i = 0; i < BUFSIZE; i++) 81 rf_debugprint_buf[i].cstring = NULL; 82 rf_debugprint_index = 0; 83 } 84 85 void 86 rf_debug_printf(s, a1, a2, a3, a4, a5, a6, a7, a8) 87 char *s; 88 void *a1, *a2, *a3, *a4, *a5, *a6, *a7, *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 114 rf_print_debug_buffer() 115 { 116 rf_spill_debug_buffer(NULL); 117 } 118 119 void 120 rf_spill_debug_buffer(fname) 121 char *fname; 122 { 123 int i; 124 125 if (!rf_debugPrintUseBuffer) 126 return; 127 128 RF_LOCK_MUTEX(rf_debug_print_mutex); 129 130 for (i = rf_debugprint_index + 1; i != rf_debugprint_index; i = (i + 1) & BUFMASK) 131 if (rf_debugprint_buf[i].cstring) 132 printf(rf_debugprint_buf[i].cstring, rf_debugprint_buf[i].a1, rf_debugprint_buf[i].a2, rf_debugprint_buf[i].a3, 133 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); 134 printf(rf_debugprint_buf[i].cstring, rf_debugprint_buf[i].a1, rf_debugprint_buf[i].a2, rf_debugprint_buf[i].a3, 135 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); 136 RF_UNLOCK_MUTEX(rf_debug_print_mutex); 137 } 138