xref: /freebsd/lib/libthr/thread/thr_info.c (revision 77b7cdf1999ee965ad494fddd184b18f532ac91a)
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 		if (TAILQ_FIRST(&_dead_list) == NULL) {
113 			/* Output a record: */
114 			strcpy(s, "\n\nTHERE ARE NO DEAD THREADS\n");
115 			__sys_write(fd, s, strlen(s));
116 		} else {
117 			/* Output a header for dead threads: */
118 			strcpy(s, "\n\nDEAD THREADS\n\n");
119 			__sys_write(fd, s, strlen(s));
120 
121 			/*
122 			 * Enter a loop to report each thread in the global
123 			 * dead thread list:
124 			 */
125 			TAILQ_FOREACH(pthread, &_dead_list, dle) {
126 				dump_thread(fd, pthread, /*long_version*/ 0);
127 			}
128 		}
129 
130 		/* Close the dump file: */
131 		__sys_close(fd);
132 	}
133 }
134 
135 static void
136 dump_thread(int fd, pthread_t pthread, int long_version)
137 {
138 	char		s[512];
139 	int		i;
140 
141 	/* Find the state: */
142 	for (i = 0; i < NELEMENTS(thread_info) - 1; i++)
143 		if (thread_info[i].state == pthread->state)
144 			break;
145 
146 	/* Output a record for the thread: */
147 	snprintf(s, sizeof(s),
148 	    "--------------------\nThread %p (%s) prio %3d state %s [%s:%d]\n",
149 	    pthread, (pthread->name == NULL) ? "" : pthread->name,
150 	    pthread->active_priority, thread_info[i].name, pthread->fname,
151 	    pthread->lineno);
152 	__sys_write(fd, s, strlen(s));
153 
154 	if (long_version != 0) {
155 		/* Check if this is the running thread: */
156 		if (pthread == curthread) {
157 			/* Output a record for the running thread: */
158 			strcpy(s, "This is the running thread\n");
159 			__sys_write(fd, s, strlen(s));
160 		}
161 		/* Check if this is the initial thread: */
162 		if (pthread == _thread_initial) {
163 			/* Output a record for the initial thread: */
164 			strcpy(s, "This is the initial thread\n");
165 			__sys_write(fd, s, strlen(s));
166 		}
167 		/* Process according to thread state: */
168 		switch (pthread->state) {
169 		/*
170 		 * Trap other states that are not explicitly
171 		 * coded to dump information:
172 		 */
173 		default:
174 			/* Nothing to do here. */
175 			break;
176 		}
177 	}
178 }
179 
180 /* Set the thread name for debug: */
181 void
182 _pthread_set_name_np(pthread_t thread, const char *name)
183 {
184 	/* Check if the caller has specified a valid thread: */
185 	if (thread != NULL && thread->magic == PTHREAD_MAGIC) {
186 		if (thread->name != NULL) {
187 			/* Free space for previous name. */
188 			free(thread->name);
189 		}
190 		thread->name = strdup(name);
191 	}
192 }
193