1 /* 2 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by John Birrell. 16 * 4. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * $FreeBSD$ 33 */ 34 #include <stdio.h> 35 #include <stdlib.h> 36 #include <fcntl.h> 37 #include <string.h> 38 #include <unistd.h> 39 #include <pthread.h> 40 #include <errno.h> 41 #include "thr_private.h" 42 43 #ifndef NELEMENTS 44 #define NELEMENTS(arr) (sizeof(arr) / sizeof(arr[0])) 45 #endif 46 47 static void dump_thread(int fd, pthread_t pthread, int long_version); 48 49 __weak_reference(_pthread_set_name_np, pthread_set_name_np); 50 51 struct s_thread_info { 52 enum pthread_state state; 53 char *name; 54 }; 55 56 /* Static variables: */ 57 static const struct s_thread_info thread_info[] = { 58 {PS_RUNNING , "Running"}, 59 {PS_MUTEX_WAIT , "Waiting on a mutex"}, 60 {PS_COND_WAIT , "Waiting on a condition variable"}, 61 {PS_SLEEP_WAIT , "Sleeping"}, 62 {PS_WAIT_WAIT , "Waiting process"}, 63 {PS_JOIN , "Waiting to join"}, 64 {PS_DEAD , "Dead"}, 65 {PS_DEADLOCK , "Deadlocked"}, 66 {PS_STATE_MAX , "Not a real state!"} 67 }; 68 69 void 70 _thread_dump_info(void) 71 { 72 char s[512]; 73 int fd; 74 int i; 75 pthread_t pthread; 76 char tmpfile[128]; 77 78 for (i = 0; i < 100000; i++) { 79 snprintf(tmpfile, sizeof(tmpfile), "/tmp/uthread.dump.%u.%i", 80 getpid(), i); 81 /* Open the dump file for append and create it if necessary: */ 82 if ((fd = __sys_open(tmpfile, O_RDWR | O_CREAT | O_EXCL, 83 0666)) < 0) { 84 /* Can't open the dump file. */ 85 if (errno == EEXIST) 86 continue; 87 /* 88 * We only need to continue in case of 89 * EEXIT error. Most other error 90 * codes means that we will fail all 91 * the times. 92 */ 93 return; 94 } else { 95 break; 96 } 97 } 98 if (i==100000) { 99 /* all 100000 possibilities are in use :( */ 100 return; 101 } else { 102 /* Output a header for active threads: */ 103 strcpy(s, "\n\n=============\nACTIVE THREADS\n\n"); 104 __sys_write(fd, s, strlen(s)); 105 106 /* Enter a loop to report each thread in the global list: */ 107 TAILQ_FOREACH(pthread, &_thread_list, tle) { 108 dump_thread(fd, pthread, /*long_verson*/ 1); 109 } 110 111 /* Check if there are no dead threads: */ 112 DEAD_LIST_LOCK; 113 if (TAILQ_FIRST(&_dead_list) == NULL) { 114 /* Output a record: */ 115 strcpy(s, "\n\nTHERE ARE NO DEAD THREADS\n"); 116 __sys_write(fd, s, strlen(s)); 117 } else { 118 /* Output a header for dead threads: */ 119 strcpy(s, "\n\nDEAD THREADS\n\n"); 120 __sys_write(fd, s, strlen(s)); 121 122 /* 123 * Enter a loop to report each thread in the global 124 * dead thread list: 125 */ 126 TAILQ_FOREACH(pthread, &_dead_list, dle) { 127 dump_thread(fd, pthread, /*long_version*/ 0); 128 } 129 } 130 DEAD_LIST_UNLOCK; 131 132 /* Close the dump file: */ 133 __sys_close(fd); 134 } 135 } 136 137 static void 138 dump_thread(int fd, pthread_t pthread, int long_version) 139 { 140 char s[512]; 141 int i; 142 143 /* Find the state: */ 144 for (i = 0; i < NELEMENTS(thread_info) - 1; i++) 145 if (thread_info[i].state == pthread->state) 146 break; 147 148 /* Output a record for the thread: */ 149 snprintf(s, sizeof(s), 150 "--------------------\nThread %p (%s) prio %3d state %s [%s:%d]\n", 151 pthread, (pthread->name == NULL) ? "" : pthread->name, 152 pthread->active_priority, thread_info[i].name, pthread->fname, 153 pthread->lineno); 154 __sys_write(fd, s, strlen(s)); 155 156 if (long_version != 0) { 157 /* Check if this is the running thread: */ 158 if (pthread == curthread) { 159 /* Output a record for the running thread: */ 160 strcpy(s, "This is the running thread\n"); 161 __sys_write(fd, s, strlen(s)); 162 } 163 /* Check if this is the initial thread: */ 164 if (pthread == _thread_initial) { 165 /* Output a record for the initial thread: */ 166 strcpy(s, "This is the initial thread\n"); 167 __sys_write(fd, s, strlen(s)); 168 } 169 /* Process according to thread state: */ 170 switch (pthread->state) { 171 /* 172 * Trap other states that are not explicitly 173 * coded to dump information: 174 */ 175 default: 176 /* Nothing to do here. */ 177 break; 178 } 179 } 180 } 181 182 /* Set the thread name for debug: */ 183 void 184 _pthread_set_name_np(pthread_t thread, const char *name) 185 { 186 /* Check if the caller has specified a valid thread: */ 187 if (thread != NULL && thread->magic == PTHREAD_MAGIC) { 188 if (thread->name != NULL) { 189 /* Free space for previous name. */ 190 free(thread->name); 191 } 192 thread->name = strdup(name); 193 } 194 } 195