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. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include "pthread_private.h"
42 #define NELEMENTS(arr) (sizeof(arr) / sizeof(arr[0]))
45 static void dump_thread(int fd, pthread_t pthread, int long_version);
47 __weak_reference(_pthread_set_name_np, pthread_set_name_np);
49 struct s_thread_info {
50 enum pthread_state state;
54 /* Static variables: */
55 static const struct s_thread_info thread_info[] = {
56 {PS_RUNNING , "Running"},
57 {PS_SIGTHREAD , "Waiting on signal thread"},
58 {PS_MUTEX_WAIT , "Waiting on a mutex"},
59 {PS_COND_WAIT , "Waiting on a condition variable"},
60 {PS_FDLR_WAIT , "Waiting for a file read lock"},
61 {PS_FDLW_WAIT , "Waiting for a file write lock"},
62 {PS_FDR_WAIT , "Waiting for read"},
63 {PS_FDW_WAIT , "Waiting for write"},
64 {PS_FILE_WAIT , "Waiting for FILE lock"},
65 {PS_POLL_WAIT , "Waiting on poll"},
66 {PS_SELECT_WAIT , "Waiting on select"},
67 {PS_SLEEP_WAIT , "Sleeping"},
68 {PS_WAIT_WAIT , "Waiting process"},
69 {PS_SIGSUSPEND , "Suspended, waiting for a signal"},
70 {PS_SIGWAIT , "Waiting for a signal"},
71 {PS_SPINBLOCK , "Waiting for a spinlock"},
72 {PS_JOIN , "Waiting to join"},
73 {PS_SUSPENDED , "Suspended"},
75 {PS_DEADLOCK , "Deadlocked"},
76 {PS_STATE_MAX , "Not a real state!"}
80 _thread_dump_info(void)
87 char tmpfile[PATH_MAX];
90 if (issetugid() != 0 || (tmpdir = getenv("TMPDIR")) == NULL)
92 for (i = 0; i < 100000; i++) {
93 snprintf(tmpfile, sizeof(tmpfile), "%s/uthread.dump.%u.%i",
95 /* Open the dump file for append and create it if necessary: */
96 if ((fd = __sys_open(tmpfile, O_RDWR | O_CREAT | O_EXCL,
98 /* Can't open the dump file. */
102 * We only need to continue in case of
103 * EEXIST error. Most other error
104 * codes means that we will fail all
113 /* all 100000 possibilities are in use :( */
116 /* Output a header for active threads: */
117 strcpy(s, "\n\n=============\nACTIVE THREADS\n\n");
118 __sys_write(fd, s, strlen(s));
120 /* Enter a loop to report each thread in the global list: */
121 TAILQ_FOREACH(pthread, &_thread_list, tle) {
122 dump_thread(fd, pthread, /*long_verson*/ 1);
125 /* Output a header for ready threads: */
126 strcpy(s, "\n\n=============\nREADY THREADS\n\n");
127 __sys_write(fd, s, strlen(s));
129 /* Enter a loop to report each thread in the ready queue: */
130 TAILQ_FOREACH (pq_list, &_readyq.pq_queue, pl_link) {
131 TAILQ_FOREACH(pthread, &pq_list->pl_head, pqe) {
132 dump_thread(fd, pthread, /*long_version*/ 0);
136 /* Output a header for waiting threads: */
137 strcpy(s, "\n\n=============\nWAITING THREADS\n\n");
138 __sys_write(fd, s, strlen(s));
140 /* Enter a loop to report each thread in the waiting queue: */
141 TAILQ_FOREACH (pthread, &_waitingq, pqe) {
142 dump_thread(fd, pthread, /*long_version*/ 0);
145 /* Output a header for threads in the work queue: */
146 strcpy(s, "\n\n=============\nTHREADS IN WORKQ\n\n");
147 __sys_write(fd, s, strlen(s));
149 /* Enter a loop to report each thread in the waiting queue: */
150 TAILQ_FOREACH (pthread, &_workq, qe) {
151 dump_thread(fd, pthread, /*long_version*/ 0);
154 /* Check if there are no dead threads: */
155 if (TAILQ_FIRST(&_dead_list) == NULL) {
156 /* Output a record: */
157 strcpy(s, "\n\nTHERE ARE NO DEAD THREADS\n");
158 __sys_write(fd, s, strlen(s));
160 /* Output a header for dead threads: */
161 strcpy(s, "\n\nDEAD THREADS\n\n");
162 __sys_write(fd, s, strlen(s));
165 * Enter a loop to report each thread in the global
168 TAILQ_FOREACH(pthread, &_dead_list, dle) {
169 dump_thread(fd, pthread, /*long_version*/ 0);
173 /* Output a header for file descriptors: */
174 snprintf(s, sizeof(s), "\n\n=============\nFILE DESCRIPTOR "
175 "TABLE (table size %d)\n\n", _thread_dtablesize);
176 __sys_write(fd, s, strlen(s));
178 /* Enter a loop to report file descriptor lock usage: */
179 for (i = 0; i < _thread_dtablesize; i++) {
181 * Check if memory is allocated for this file
184 if (_thread_fd_table[i] != NULL) {
185 /* Report the file descriptor lock status: */
186 snprintf(s, sizeof(s),
187 "fd[%3d] read owner %p count %d [%s:%d]\n"
188 " write owner %p count %d [%s:%d]\n",
189 i, _thread_fd_table[i]->r_owner,
190 _thread_fd_table[i]->r_lockcount,
191 _thread_fd_table[i]->r_fname,
192 _thread_fd_table[i]->r_lineno,
193 _thread_fd_table[i]->w_owner,
194 _thread_fd_table[i]->w_lockcount,
195 _thread_fd_table[i]->w_fname,
196 _thread_fd_table[i]->w_lineno);
197 __sys_write(fd, s, strlen(s));
201 /* Close the dump file: */
207 dump_thread(int fd, pthread_t pthread, int long_version)
209 struct pthread *curthread = _get_curthread();
213 /* Find the state: */
214 for (i = 0; i < NELEMENTS(thread_info) - 1; i++)
215 if (thread_info[i].state == pthread->state)
218 /* Output a record for the thread: */
219 snprintf(s, sizeof(s),
220 "--------------------\nThread %p (%s) prio %3d state %s",
221 pthread, (pthread->name == NULL) ? "" : pthread->name,
222 pthread->active_priority, thread_info[i].name);
223 __sys_write(fd, s, strlen(s));
224 /* And now where it is. */
225 if (pthread->fname != NULL) {
226 snprintf(s, sizeof(s), " [%s:%d]", pthread->fname,
228 __sys_write(fd, s, strlen(s));
230 __sys_write(fd, "\n", 1);
232 if (long_version != 0) {
233 /* Check if this is the running thread: */
234 if (pthread == curthread) {
235 /* Output a record for the running thread: */
236 strcpy(s, "This is the running thread\n");
237 __sys_write(fd, s, strlen(s));
239 /* Check if this is the initial thread: */
240 if (pthread == _thread_initial) {
241 /* Output a record for the initial thread: */
242 strcpy(s, "This is the initial thread\n");
243 __sys_write(fd, s, strlen(s));
245 /* Process according to thread state: */
246 switch (pthread->state) {
247 /* File descriptor read lock wait: */
252 /* Write the lock details: */
253 snprintf(s, sizeof(s), "fd %d[%s:%d]",
255 pthread->data.fd.fname,
256 pthread->data.fd.branch);
257 __sys_write(fd, s, strlen(s));
260 snprintf(s, sizeof(s), "sigmask (hi)");
261 __sys_write(fd, s, strlen(s));
262 for (i = _SIG_WORDS - 1; i >= 0; i--) {
263 snprintf(s, sizeof(s), "%08x\n",
264 pthread->sigmask.__bits[i]);
265 __sys_write(fd, s, strlen(s));
267 snprintf(s, sizeof(s), "(lo)\n");
268 __sys_write(fd, s, strlen(s));
271 * Trap other states that are not explicitly
272 * coded to dump information:
275 /* Nothing to do here. */
281 /* Set the thread name for debug: */
283 _pthread_set_name_np(pthread_t thread, const char *name)
285 /* Check if the caller has specified a valid thread: */
286 if (thread != NULL && thread->magic == PTHREAD_MAGIC) {
287 if (thread->name != NULL) {
288 /* Free space for previous name. */
291 thread->name = strdup(name);