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