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 $
42 #include "pthread_private.h"
45 #define NELEMENTS(arr) (sizeof(arr) / sizeof(arr[0]))
48 static void dump_thread(int fd, pthread_t pthread, int long_version);
50 __weak_reference(_pthread_set_name_np, pthread_set_name_np);
52 struct s_thread_info {
53 enum pthread_state state;
57 /* Static variables: */
58 static const struct s_thread_info thread_info[] = {
59 {PS_RUNNING , "Running"},
60 {PS_SIGTHREAD , "Waiting on signal thread"},
61 {PS_MUTEX_WAIT , "Waiting on a mutex"},
62 {PS_COND_WAIT , "Waiting on a condition variable"},
63 {PS_FDLR_WAIT , "Waiting for a file read lock"},
64 {PS_FDLW_WAIT , "Waiting for a file write lock"},
65 {PS_FDR_WAIT , "Waiting for read"},
66 {PS_FDW_WAIT , "Waiting for write"},
67 {PS_FILE_WAIT , "Waiting for FILE lock"},
68 {PS_POLL_WAIT , "Waiting on poll"},
69 {PS_SELECT_WAIT , "Waiting on select"},
70 {PS_SLEEP_WAIT , "Sleeping"},
71 {PS_WAIT_WAIT , "Waiting process"},
72 {PS_SIGSUSPEND , "Suspended, waiting for a signal"},
73 {PS_SIGWAIT , "Waiting for a signal"},
74 {PS_SPINBLOCK , "Waiting for a spinlock"},
75 {PS_JOIN , "Waiting to join"},
76 {PS_SUSPENDED , "Suspended"},
78 {PS_DEADLOCK , "Deadlocked"},
79 {PS_STATE_MAX , "Not a real state!"}
83 _thread_dump_info(void)
90 char tmpfile[PATH_MAX];
93 if (issetugid() != 0 || (tmpdir = getenv("TMPDIR")) == NULL)
95 for (i = 0; i < 100000; i++) {
96 snprintf(tmpfile, sizeof(tmpfile), "%s/uthread.dump.%u.%i",
98 /* Open the dump file for append and create it if necessary: */
99 if ((fd = __sys_open(tmpfile, O_RDWR | O_CREAT | O_EXCL,
101 /* Can't open the dump file. */
105 * We only need to continue in case of
106 * EEXIT error. Most other error
107 * codes means that we will fail all
116 /* all 100000 possibilities are in use :( */
119 /* Output a header for active threads: */
120 strcpy(s, "\n\n=============\nACTIVE THREADS\n\n");
121 __sys_write(fd, s, strlen(s));
123 /* Enter a loop to report each thread in the global list: */
124 TAILQ_FOREACH(pthread, &_thread_list, tle) {
125 dump_thread(fd, pthread, /*long_verson*/ 1);
128 /* Output a header for ready threads: */
129 strcpy(s, "\n\n=============\nREADY THREADS\n\n");
130 __sys_write(fd, s, strlen(s));
132 /* Enter a loop to report each thread in the ready queue: */
133 TAILQ_FOREACH (pq_list, &_readyq.pq_queue, pl_link) {
134 TAILQ_FOREACH(pthread, &pq_list->pl_head, pqe) {
135 dump_thread(fd, pthread, /*long_version*/ 0);
139 /* Output a header for waiting threads: */
140 strcpy(s, "\n\n=============\nWAITING THREADS\n\n");
141 __sys_write(fd, s, strlen(s));
143 /* Enter a loop to report each thread in the waiting queue: */
144 TAILQ_FOREACH (pthread, &_waitingq, pqe) {
145 dump_thread(fd, pthread, /*long_version*/ 0);
148 /* Output a header for threads in the work queue: */
149 strcpy(s, "\n\n=============\nTHREADS IN WORKQ\n\n");
150 __sys_write(fd, s, strlen(s));
152 /* Enter a loop to report each thread in the waiting queue: */
153 TAILQ_FOREACH (pthread, &_workq, qe) {
154 dump_thread(fd, pthread, /*long_version*/ 0);
157 /* Check if there are no dead threads: */
158 if (TAILQ_FIRST(&_dead_list) == NULL) {
159 /* Output a record: */
160 strcpy(s, "\n\nTHERE ARE NO DEAD THREADS\n");
161 __sys_write(fd, s, strlen(s));
163 /* Output a header for dead threads: */
164 strcpy(s, "\n\nDEAD THREADS\n\n");
165 __sys_write(fd, s, strlen(s));
168 * Enter a loop to report each thread in the global
171 TAILQ_FOREACH(pthread, &_dead_list, dle) {
172 dump_thread(fd, pthread, /*long_version*/ 0);
176 /* Output a header for file descriptors: */
177 snprintf(s, sizeof(s), "\n\n=============\nFILE DESCRIPTOR "
178 "TABLE (table size %d)\n\n", _thread_dtablesize);
179 __sys_write(fd, s, strlen(s));
181 /* Enter a loop to report file descriptor lock usage: */
182 for (i = 0; i < _thread_dtablesize; i++) {
184 * Check if memory is allocated for this file
187 if (_thread_fd_table[i] != NULL) {
188 /* Report the file descriptor lock status: */
189 snprintf(s, sizeof(s),
190 "fd[%3d] read owner %p count %d [%s:%d]\n"
191 " write owner %p count %d [%s:%d]\n",
192 i, _thread_fd_table[i]->r_owner,
193 _thread_fd_table[i]->r_lockcount,
194 _thread_fd_table[i]->r_fname,
195 _thread_fd_table[i]->r_lineno,
196 _thread_fd_table[i]->w_owner,
197 _thread_fd_table[i]->w_lockcount,
198 _thread_fd_table[i]->w_fname,
199 _thread_fd_table[i]->w_lineno);
200 __sys_write(fd, s, strlen(s));
204 /* Close the dump file: */
210 dump_thread(int fd, pthread_t pthread, int long_version)
212 struct pthread *curthread = _get_curthread();
216 /* Find the state: */
217 for (i = 0; i < NELEMENTS(thread_info) - 1; i++)
218 if (thread_info[i].state == pthread->state)
221 /* Output a record for the thread: */
222 snprintf(s, sizeof(s),
223 "--------------------\nThread %p (%s) prio %3d state %s [%s:%d]\n",
224 pthread, (pthread->name == NULL) ? "" : pthread->name,
225 pthread->active_priority, thread_info[i].name, pthread->fname,
227 __sys_write(fd, s, strlen(s));
229 if (long_version != 0) {
230 /* Check if this is the running thread: */
231 if (pthread == curthread) {
232 /* Output a record for the running thread: */
233 strcpy(s, "This is the running thread\n");
234 __sys_write(fd, s, strlen(s));
236 /* Check if this is the initial thread: */
237 if (pthread == _thread_initial) {
238 /* Output a record for the initial thread: */
239 strcpy(s, "This is the initial thread\n");
240 __sys_write(fd, s, strlen(s));
242 /* Process according to thread state: */
243 switch (pthread->state) {
244 /* File descriptor read lock wait: */
249 /* Write the lock details: */
250 snprintf(s, sizeof(s), "fd %d[%s:%d]",
252 pthread->data.fd.fname,
253 pthread->data.fd.branch);
254 __sys_write(fd, s, strlen(s));
257 snprintf(s, sizeof(s), "sigmask (hi)");
258 __sys_write(fd, s, strlen(s));
259 for (i = _SIG_WORDS - 1; i >= 0; i--) {
260 snprintf(s, sizeof(s), "%08x\n",
261 pthread->sigmask.__bits[i]);
262 __sys_write(fd, s, strlen(s));
264 snprintf(s, sizeof(s), "(lo)\n");
265 __sys_write(fd, s, strlen(s));
268 * Trap other states that are not explicitly
269 * coded to dump information:
272 /* Nothing to do here. */
278 /* Set the thread name for debug: */
280 _pthread_set_name_np(pthread_t thread, const char *name)
282 /* Check if the caller has specified a valid thread: */
283 if (thread != NULL && thread->magic == PTHREAD_MAGIC) {
284 if (thread->name != NULL) {
285 /* Free space for previous name. */
288 thread->name = strdup(name);