Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / lib / libc_r / uthread / uthread_info.c
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: 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 $
34  */
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <paths.h>
41 #include <pthread.h>
42 #include <unistd.h>
43 #include "pthread_private.h"
44
45 #ifndef NELEMENTS
46 #define NELEMENTS(arr)  (sizeof(arr) / sizeof(arr[0]))
47 #endif
48
49 static void     dump_thread(int fd, pthread_t pthread, int long_version);
50
51 __weak_reference(_pthread_set_name_np, pthread_set_name_np);
52
53 struct s_thread_info {
54         enum pthread_state state;
55         char           *name;
56 };
57
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"},
78         {PS_DEAD        , "Dead"},
79         {PS_DEADLOCK    , "Deadlocked"},
80         {PS_STATE_MAX   , "Not a real state!"}
81 };
82
83 void
84 _thread_dump_info(void)
85 {
86         char            s[512];
87         int             fd;
88         int             i;
89         pthread_t       pthread;
90         char            *tmpdir;
91         char            tmpfile[PATH_MAX];
92         pq_list_t       *pq_list;
93
94         if (issetugid() != 0 || (tmpdir = getenv("TMPDIR")) == NULL)
95                 tmpdir = _PATH_TMP;
96         for (i = 0; i < 100000; i++) {
97                 snprintf(tmpfile, sizeof(tmpfile), "%s/uthread.dump.%u.%i",
98                         tmpdir, getpid(), 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,
101                         0644)) < 0) {
102                                 /* Can't open the dump file. */
103                                 if (errno == EEXIST)
104                                         continue;
105                                 /*
106                                  * We only need to continue in case of
107                                  * EEXIT error. Most other error
108                                  * codes means that we will fail all
109                                  * the times.
110                                  */
111                                 return;
112                 } else {
113                         break;
114                 }
115         }
116         if (i==100000) {
117                 /* all 100000 possibilities are in use :( */
118                 return;
119         } else {
120                 /* Output a header for active threads: */
121                 strcpy(s, "\n\n=============\nACTIVE THREADS\n\n");
122                 __sys_write(fd, s, strlen(s));
123
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);
127                 }
128
129                 /* Output a header for ready threads: */
130                 strcpy(s, "\n\n=============\nREADY THREADS\n\n");
131                 __sys_write(fd, s, strlen(s));
132
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);
137                         }
138                 }
139
140                 /* Output a header for waiting threads: */
141                 strcpy(s, "\n\n=============\nWAITING THREADS\n\n");
142                 __sys_write(fd, s, strlen(s));
143
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);
147                 }
148
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));
152
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);
156                 }
157
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));
163                 } else {
164                         /* Output a header for dead threads: */
165                         strcpy(s, "\n\nDEAD THREADS\n\n");
166                         __sys_write(fd, s, strlen(s));
167
168                         /*
169                          * Enter a loop to report each thread in the global
170                          * dead thread list:
171                          */
172                         TAILQ_FOREACH(pthread, &_dead_list, dle) {
173                                 dump_thread(fd, pthread, /*long_version*/ 0);
174                         }
175                 }
176
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));
181
182                 /* Enter a loop to report file descriptor lock usage: */
183                 for (i = 0; i < _thread_dtablesize; i++) {
184                         /*
185                          * Check if memory is allocated for this file
186                          * descriptor:
187                          */
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));
202                         }
203                 }
204
205                 /* Close the dump file: */
206                 __sys_close(fd);
207         }
208 }
209
210 static void
211 dump_thread(int fd, pthread_t pthread, int long_version)
212 {
213         struct pthread  *curthread = _get_curthread();
214         char            s[512];
215         int             i;
216
217         /* Find the state: */
218         for (i = 0; i < NELEMENTS(thread_info) - 1; i++)
219                 if (thread_info[i].state == pthread->state)
220                         break;
221
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,
227             pthread->lineno);
228         __sys_write(fd, s, strlen(s));
229
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));
236                 }
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));
242                 }
243                 /* Process according to thread state: */
244                 switch (pthread->state) {
245                 /* File descriptor read lock wait: */
246                 case PS_FDLR_WAIT:
247                 case PS_FDLW_WAIT:
248                 case PS_FDR_WAIT:
249                 case PS_FDW_WAIT:
250                         /* Write the lock details: */
251                         snprintf(s, sizeof(s), "fd %d[%s:%d]",
252                             pthread->data.fd.fd,
253                             pthread->data.fd.fname,
254                             pthread->data.fd.branch);
255                         __sys_write(fd, s, strlen(s));
256                         break;
257                 case PS_SIGWAIT:
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));
264                         }
265                         snprintf(s, sizeof(s), "(lo)\n");
266                         __sys_write(fd, s, strlen(s));
267                         break;
268                 /*
269                  * Trap other states that are not explicitly
270                  * coded to dump information:
271                  */
272                 default:
273                         /* Nothing to do here. */
274                         break;
275                 }
276         }
277 }
278
279 /* Set the thread name for debug: */
280 void
281 _pthread_set_name_np(pthread_t thread, const char *name)
282 {
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. */
287                         free(thread->name);
288                 }
289                 thread->name = strdup(name);
290         }
291 }