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