2 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
32 * $FreeBSD: src/lib/libc_r/uthread/uthread_info.c,v 1.14.2.9 2003/02/15 05:35:31 kris Exp $
33 * $DragonFly: src/lib/libc_r/uthread/uthread_info.c,v 1.2 2003/06/17 04:26:48 dillon Exp $
43 #include "pthread_private.h"
46 #define NELEMENTS(arr) (sizeof(arr) / sizeof(arr[0]))
49 static void dump_thread(int fd, pthread_t pthread, int long_version);
51 __weak_reference(_pthread_set_name_np, pthread_set_name_np);
53 struct s_thread_info {
54 enum pthread_state state;
58 /* Static variables: */
59 static const struct s_thread_info thread_info[] = {
60 {PS_RUNNING , "Running"},
61 {PS_SIGTHREAD , "Waiting on signal thread"},
62 {PS_MUTEX_WAIT , "Waiting on a mutex"},
63 {PS_COND_WAIT , "Waiting on a condition variable"},
64 {PS_FDLR_WAIT , "Waiting for a file read lock"},
65 {PS_FDLW_WAIT , "Waiting for a file write lock"},
66 {PS_FDR_WAIT , "Waiting for read"},
67 {PS_FDW_WAIT , "Waiting for write"},
68 {PS_FILE_WAIT , "Waiting for FILE lock"},
69 {PS_POLL_WAIT , "Waiting on poll"},
70 {PS_SELECT_WAIT , "Waiting on select"},
71 {PS_SLEEP_WAIT , "Sleeping"},
72 {PS_WAIT_WAIT , "Waiting process"},
73 {PS_SIGSUSPEND , "Suspended, waiting for a signal"},
74 {PS_SIGWAIT , "Waiting for a signal"},
75 {PS_SPINBLOCK , "Waiting for a spinlock"},
76 {PS_JOIN , "Waiting to join"},
77 {PS_SUSPENDED , "Suspended"},
79 {PS_DEADLOCK , "Deadlocked"},
80 {PS_STATE_MAX , "Not a real state!"}
84 _thread_dump_info(void)
91 char tmpfile[PATH_MAX];
94 if (issetugid() != 0 || (tmpdir = getenv("TMPDIR")) == NULL)
96 for (i = 0; i < 100000; i++) {
97 snprintf(tmpfile, sizeof(tmpfile), "%s/uthread.dump.%u.%i",
99 /* Open the dump file for append and create it if necessary: */
100 if ((fd = __sys_open(tmpfile, O_RDWR | O_CREAT | O_EXCL,
102 /* Can't open the dump file. */
106 * We only need to continue in case of
107 * EEXIT error. Most other error
108 * codes means that we will fail all
117 /* all 100000 possibilities are in use :( */
120 /* Output a header for active threads: */
121 strcpy(s, "\n\n=============\nACTIVE THREADS\n\n");
122 __sys_write(fd, s, strlen(s));
124 /* Enter a loop to report each thread in the global list: */
125 TAILQ_FOREACH(pthread, &_thread_list, tle) {
126 dump_thread(fd, pthread, /*long_verson*/ 1);
129 /* Output a header for ready threads: */
130 strcpy(s, "\n\n=============\nREADY THREADS\n\n");
131 __sys_write(fd, s, strlen(s));
133 /* Enter a loop to report each thread in the ready queue: */
134 TAILQ_FOREACH (pq_list, &_readyq.pq_queue, pl_link) {
135 TAILQ_FOREACH(pthread, &pq_list->pl_head, pqe) {
136 dump_thread(fd, pthread, /*long_version*/ 0);
140 /* Output a header for waiting threads: */
141 strcpy(s, "\n\n=============\nWAITING THREADS\n\n");
142 __sys_write(fd, s, strlen(s));
144 /* Enter a loop to report each thread in the waiting queue: */
145 TAILQ_FOREACH (pthread, &_waitingq, pqe) {
146 dump_thread(fd, pthread, /*long_version*/ 0);
149 /* Output a header for threads in the work queue: */
150 strcpy(s, "\n\n=============\nTHREADS IN WORKQ\n\n");
151 __sys_write(fd, s, strlen(s));
153 /* Enter a loop to report each thread in the waiting queue: */
154 TAILQ_FOREACH (pthread, &_workq, qe) {
155 dump_thread(fd, pthread, /*long_version*/ 0);
158 /* Check if there are no dead threads: */
159 if (TAILQ_FIRST(&_dead_list) == NULL) {
160 /* Output a record: */
161 strcpy(s, "\n\nTHERE ARE NO DEAD THREADS\n");
162 __sys_write(fd, s, strlen(s));
164 /* Output a header for dead threads: */
165 strcpy(s, "\n\nDEAD THREADS\n\n");
166 __sys_write(fd, s, strlen(s));
169 * Enter a loop to report each thread in the global
172 TAILQ_FOREACH(pthread, &_dead_list, dle) {
173 dump_thread(fd, pthread, /*long_version*/ 0);
177 /* Output a header for file descriptors: */
178 snprintf(s, sizeof(s), "\n\n=============\nFILE DESCRIPTOR "
179 "TABLE (table size %d)\n\n", _thread_dtablesize);
180 __sys_write(fd, s, strlen(s));
182 /* Enter a loop to report file descriptor lock usage: */
183 for (i = 0; i < _thread_dtablesize; i++) {
185 * Check if memory is allocated for this file
188 if (_thread_fd_table[i] != NULL) {
189 /* Report the file descriptor lock status: */
190 snprintf(s, sizeof(s),
191 "fd[%3d] read owner %p count %d [%s:%d]\n"
192 " write owner %p count %d [%s:%d]\n",
193 i, _thread_fd_table[i]->r_owner,
194 _thread_fd_table[i]->r_lockcount,
195 _thread_fd_table[i]->r_fname,
196 _thread_fd_table[i]->r_lineno,
197 _thread_fd_table[i]->w_owner,
198 _thread_fd_table[i]->w_lockcount,
199 _thread_fd_table[i]->w_fname,
200 _thread_fd_table[i]->w_lineno);
201 __sys_write(fd, s, strlen(s));
205 /* Close the dump file: */
211 dump_thread(int fd, pthread_t pthread, int long_version)
213 struct pthread *curthread = _get_curthread();
217 /* Find the state: */
218 for (i = 0; i < NELEMENTS(thread_info) - 1; i++)
219 if (thread_info[i].state == pthread->state)
222 /* Output a record for the thread: */
223 snprintf(s, sizeof(s),
224 "--------------------\nThread %p (%s) prio %3d state %s [%s:%d]\n",
225 pthread, (pthread->name == NULL) ? "" : pthread->name,
226 pthread->active_priority, thread_info[i].name, pthread->fname,
228 __sys_write(fd, s, strlen(s));
230 if (long_version != 0) {
231 /* Check if this is the running thread: */
232 if (pthread == curthread) {
233 /* Output a record for the running thread: */
234 strcpy(s, "This is the running thread\n");
235 __sys_write(fd, s, strlen(s));
237 /* Check if this is the initial thread: */
238 if (pthread == _thread_initial) {
239 /* Output a record for the initial thread: */
240 strcpy(s, "This is the initial thread\n");
241 __sys_write(fd, s, strlen(s));
243 /* Process according to thread state: */
244 switch (pthread->state) {
245 /* File descriptor read lock wait: */
250 /* Write the lock details: */
251 snprintf(s, sizeof(s), "fd %d[%s:%d]",
253 pthread->data.fd.fname,
254 pthread->data.fd.branch);
255 __sys_write(fd, s, strlen(s));
258 snprintf(s, sizeof(s), "sigmask (hi)");
259 __sys_write(fd, s, strlen(s));
260 for (i = _SIG_WORDS - 1; i >= 0; i--) {
261 snprintf(s, sizeof(s), "%08x\n",
262 pthread->sigmask.__bits[i]);
263 __sys_write(fd, s, strlen(s));
265 snprintf(s, sizeof(s), "(lo)\n");
266 __sys_write(fd, s, strlen(s));
269 * Trap other states that are not explicitly
270 * coded to dump information:
273 /* Nothing to do here. */
279 /* Set the thread name for debug: */
281 _pthread_set_name_np(pthread_t thread, const char *name)
283 /* Check if the caller has specified a valid thread: */
284 if (thread != NULL && thread->magic == PTHREAD_MAGIC) {
285 if (thread->name != NULL) {
286 /* Free space for previous name. */
289 thread->name = strdup(name);