Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/nathanw_sa]: src/lib/libpthread Add a mechanisim for debugging that is l...



details:   https://anonhg.NetBSD.org/src/rev/f0430d938427
branches:  nathanw_sa
changeset: 504864:f0430d938427
user:      nathanw <nathanw%NetBSD.org@localhost>
date:      Tue Jul 17 20:22:40 2001 +0000

description:
Add a mechanisim for debugging that is less likely to change scheduling
behaviour than using printf (writing to a shared memory segment), and a
simple tool for dumping the buffer. Partly inspired by the kernel msgbuf
code.

diffstat:

 lib/libpthread/Makefile        |    6 +-
 lib/libpthread/debuglog.c      |   92 +++++++++++++++++++++++++++++
 lib/libpthread/pthread.c       |    6 +-
 lib/libpthread/pthread_debug.c |  129 +++++++++++++++++++++++++++++++++++++++++
 lib/libpthread/pthread_int.h   |   24 ++++++-
 5 files changed, 251 insertions(+), 6 deletions(-)

diffs (truncated from 322 to 300 lines):

diff -r fc415866569d -r f0430d938427 lib/libpthread/Makefile
--- a/lib/libpthread/Makefile   Tue Jul 17 20:18:39 2001 +0000
+++ b/lib/libpthread/Makefile   Tue Jul 17 20:22:40 2001 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile,v 1.8.2.3 2001/07/13 21:53:40 nathanw Exp $
+#      $NetBSD: Makefile,v 1.8.2.4 2001/07/17 20:22:40 nathanw Exp $
 #
 
 .if exists(${.CURDIR}/arch/${MACHINE_ARCH})
@@ -27,6 +27,7 @@
 LIB=   pthread
 SRCS=  pthread.c pthread_lock.c pthread_mutex.c pthread_run.c
 SRCS+= pthread_sa.c pthread_sig.c pthread_stack.c
+SRCS+=  pthread_debug.c
 SRCS+= sched.c
 # Architecture-dependent files
 SRCS+= pthread_switch.S _context_u.S
@@ -35,5 +36,8 @@
 INCS=  pthread.h pthread_types.h pthread_queue.h sched.h
 INCSDIR=/usr/include
 
+debuglog: debuglog.o
+       $(CC) -o debuglog debuglog.o -lpthread
+
 .include <bsd.lib.mk>
 # DO NOT DELETE
diff -r fc415866569d -r f0430d938427 lib/libpthread/debuglog.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/libpthread/debuglog.c Tue Jul 17 20:22:40 2001 +0000
@@ -0,0 +1,92 @@
+/*     $NetBSD: debuglog.c,v 1.1.2.1 2001/07/17 20:22:41 nathanw Exp $ */
+
+/*-
+ * Copyright (c) 2001 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Nathan J. Williams.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *        This product includes software developed by the NetBSD
+ *        Foundation, Inc. and its contributors.
+ * 4. Neither the name of The NetBSD Foundation nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <err.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/shm.h>
+
+#include "pthread.h"
+#include "pthread_int.h"
+
+
+void pthread__debuglog_read(void);
+
+
+static struct pthread_msgbuf* debugbuf;
+
+int
+main(int argc, char *argv[])
+{
+
+       debugbuf = pthread__debuglog_init();
+
+       pthread__debuglog_read();
+
+       return 0;
+}
+
+void
+pthread__debuglog_read(void)
+{
+
+       int readp, writep;
+
+       while (1) {
+
+               readp = debugbuf->msg_bufr;
+               writep = debugbuf->msg_bufw;
+
+               if (readp < writep) {
+                       printf("%.*s", writep - readp, 
+                           &debugbuf->msg_bufc[readp]);
+               } else if (readp > writep) {
+                       printf("%.*s", debugbuf->msg_bufs - readp, 
+                           &debugbuf->msg_bufc[readp]);
+                       printf("%.*s", writep, &debugbuf->msg_bufc[0]);
+               }
+
+               debugbuf->msg_bufr = writep;
+
+               sleep(1);
+       }
+}
+
+
+
diff -r fc415866569d -r f0430d938427 lib/libpthread/pthread.c
--- a/lib/libpthread/pthread.c  Tue Jul 17 20:18:39 2001 +0000
+++ b/lib/libpthread/pthread.c  Tue Jul 17 20:22:40 2001 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pthread.c,v 1.1.2.4 2001/07/17 20:18:39 nathanw Exp $  */
+/*     $NetBSD: pthread.c,v 1.1.2.5 2001/07/17 20:22:41 nathanw Exp $  */
 
 /*-
  * Copyright (c) 2001 The NetBSD Foundation, Inc.
@@ -81,6 +81,10 @@
        pthread_t first, idle;
        int i, ret;
 
+#ifdef PTHREAD__DEBUG
+       pthread__debug_init();
+#endif
+
        /* Basic data structure setup */
        pthread_attr_init(&pthread_default_attr);
        PTQ_INIT(&allqueue);
diff -r fc415866569d -r f0430d938427 lib/libpthread/pthread_debug.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/libpthread/pthread_debug.c    Tue Jul 17 20:22:40 2001 +0000
@@ -0,0 +1,129 @@
+/*     $NetBSD: pthread_debug.c,v 1.1.2.1 2001/07/17 20:22:41 nathanw Exp $    */
+
+/*-
+ * Copyright (c) 2001 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Nathan J. Williams.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *        This product includes software developed by the NetBSD
+ *        Foundation, Inc. and its contributors.
+ * 4. Neither the name of The NetBSD Foundation nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+#include <sys/shm.h>
+
+#include "pthread.h"
+#include "pthread_int.h"
+
+#ifdef PTHREAD__DEBUG
+
+static struct pthread_msgbuf* debugbuf;
+
+void pthread__debug_init(void)
+{
+       time_t t;
+
+       t = time(NULL);
+       debugbuf = pthread__debuglog_init();
+       DPRINTF(("Started debugging %s at %s\n", getprogname(), 
+           ctime(&t)));
+}
+
+struct pthread_msgbuf *
+pthread__debuglog_init(void)
+{
+       int debugshmid;
+       void *debuglog;
+       struct pthread_msgbuf* buf;
+
+       debugshmid = shmget(PTHREAD__DEBUG_SHMKEY, PTHREAD__DEBUG_SHMSIZE,
+           IPC_CREAT | S_IRWXU);
+       
+       if (debugshmid == -1)
+               err(1, "Couldn't get shared debug log: %s\n", strerror(errno));
+
+       debuglog = shmat(debugshmid, 0, 0);
+
+       if (debuglog == (void *)-1)
+               err(1, "Couldn't map shared debug log (ID %d): %s\n",
+                   strerror(errno));
+
+       buf = (struct pthread_msgbuf *)debuglog;
+
+       if (buf->msg_magic != BUF_MAGIC) {
+               /* Initialize */
+               buf->msg_magic = BUF_MAGIC;
+               buf->msg_bufw = 0;
+               buf->msg_bufr = 0;
+               buf->msg_bufs = PTHREAD__DEBUG_SHMSIZE;
+       }
+
+       return buf;
+}
+
+void
+pthread__debuglog_printf(const char *fmt, ...)
+{
+       static char tmpbuf[200];
+       long len, cplen, diff1, diff2;
+       va_list ap;
+
+       va_start(ap, fmt);
+       len = vsnprintf(tmpbuf, 200, fmt, ap);
+       va_end(ap);
+
+       diff1 = debugbuf->msg_bufw - debugbuf->msg_bufr;
+
+       if (debugbuf->msg_bufw + len > debugbuf->msg_bufs) {
+               cplen = debugbuf->msg_bufs - debugbuf->msg_bufw;
+               memcpy(&debugbuf->msg_bufc[debugbuf->msg_bufw],
+                   tmpbuf, cplen);
+               memcpy(&debugbuf->msg_bufc[0], tmpbuf + cplen, len - cplen);
+               debugbuf->msg_bufw = len - cplen;
+       } else {
+               memcpy(&debugbuf->msg_bufc[debugbuf->msg_bufw],
+                   tmpbuf, len);
+               debugbuf->msg_bufw += len;
+       }
+
+       diff2 = debugbuf->msg_bufw - debugbuf->msg_bufr;
+       
+       /* Check if we've lapped the read pointer and if so advance it. */
+       if (((diff1 < 0) && (diff2 >= 0)) || ((diff1 >= 0) && (diff2 < 0)))
+               debugbuf->msg_bufr = debugbuf->msg_bufw;
+
+}
+#endif
diff -r fc415866569d -r f0430d938427 lib/libpthread/pthread_int.h
--- a/lib/libpthread/pthread_int.h      Tue Jul 17 20:18:39 2001 +0000
+++ b/lib/libpthread/pthread_int.h      Tue Jul 17 20:22:40 2001 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pthread_int.h,v 1.1.2.5 2001/07/17 20:18:39 nathanw Exp $      */
+/*     $NetBSD: pthread_int.h,v 1.1.2.6 2001/07/17 20:22:41 nathanw Exp $      */
 
 /*-
  * Copyright (c) 2001 The NetBSD Foundation, Inc.
@@ -206,18 +206,34 @@
 
 #ifdef PTHREAD__DEBUG
 
+#define PTHREAD__DEBUG_SHMKEY  (0x000f)
+#define PTHREAD__DEBUG_SHMSIZE (1<<18)
+
 extern int pthread__debug_counters[PTHREADD_NCOUNTERS];
 
 #define PTHREADD_ADD(x) (pthread__debug_counters[(x)]++)
 
+#define DPRINTF(x) pthread__debuglog_printf x
+
+struct pthread_msgbuf {
+#define BUF_MAGIC      0x090976
+       int     msg_magic;



Home | Main Index | Thread Index | Old Index