]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - lib/libc_r/uthread/uthread_info.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.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. 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.
16  *
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
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <paths.h>
37 #include <pthread.h>
38 #include <unistd.h>
39 #include "pthread_private.h"
40
41 #ifndef NELEMENTS
42 #define NELEMENTS(arr)  (sizeof(arr) / sizeof(arr[0]))
43 #endif
44
45 static void     dump_thread(int fd, pthread_t pthread, int long_version);
46
47 __weak_reference(_pthread_set_name_np, pthread_set_name_np);
48
49 struct s_thread_info {
50         enum pthread_state state;
51         char           *name;
52 };
53
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"},
74         {PS_DEAD        , "Dead"},
75         {PS_DEADLOCK    , "Deadlocked"},
76         {PS_STATE_MAX   , "Not a real state!"}
77 };
78
79 void
80 _thread_dump_info(void)
81 {
82         char            s[512];
83         int             fd;
84         int             i;
85         pthread_t       pthread;
86         char            *tmpdir;
87         char            tmpfile[PATH_MAX];
88         pq_list_t       *pq_list;
89
90         if (issetugid() != 0 || (tmpdir = getenv("TMPDIR")) == NULL)
91                 tmpdir = _PATH_TMP;
92         for (i = 0; i < 100000; i++) {
93                 snprintf(tmpfile, sizeof(tmpfile), "%s/uthread.dump.%u.%i",
94                         tmpdir, getpid(), 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,
97                         0644)) < 0) {
98                                 /* Can't open the dump file. */
99                                 if (errno == EEXIST)
100                                         continue;
101                                 /*
102                                  * We only need to continue in case of
103                                  * EEXIST error. Most other error
104                                  * codes means that we will fail all
105                                  * the time.
106                                  */
107                                 return;
108                 } else {
109                         break;
110                 }
111         }
112         if (i==100000) {
113                 /* all 100000 possibilities are in use :( */
114                 return;
115         } else {
116                 /* Output a header for active threads: */
117                 strcpy(s, "\n\n=============\nACTIVE THREADS\n\n");
118                 __sys_write(fd, s, strlen(s));
119
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);
123                 }
124
125                 /* Output a header for ready threads: */
126                 strcpy(s, "\n\n=============\nREADY THREADS\n\n");
127                 __sys_write(fd, s, strlen(s));
128
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);
133                         }
134                 }
135
136                 /* Output a header for waiting threads: */
137                 strcpy(s, "\n\n=============\nWAITING THREADS\n\n");
138                 __sys_write(fd, s, strlen(s));
139
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);
143                 }
144
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));
148
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);
152                 }
153
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));
159                 } else {
160                         /* Output a header for dead threads: */
161                         strcpy(s, "\n\nDEAD THREADS\n\n");
162                         __sys_write(fd, s, strlen(s));
163
164                         /*
165                          * Enter a loop to report each thread in the global
166                          * dead thread list:
167                          */
168                         TAILQ_FOREACH(pthread, &_dead_list, dle) {
169                                 dump_thread(fd, pthread, /*long_version*/ 0);
170                         }
171                 }
172
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));
177
178                 /* Enter a loop to report file descriptor lock usage: */
179                 for (i = 0; i < _thread_dtablesize; i++) {
180                         /*
181                          * Check if memory is allocated for this file
182                          * descriptor:
183                          */
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));
198                         }
199                 }
200
201                 /* Close the dump file: */
202                 __sys_close(fd);
203         }
204 }
205
206 static void
207 dump_thread(int fd, pthread_t pthread, int long_version)
208 {
209         struct pthread  *curthread = _get_curthread();
210         char            s[512];
211         int             i;
212
213         /* Find the state: */
214         for (i = 0; i < NELEMENTS(thread_info) - 1; i++)
215                 if (thread_info[i].state == pthread->state)
216                         break;
217
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,
227                     pthread->lineno);
228                 __sys_write(fd, s, strlen(s));
229         }
230         __sys_write(fd, "\n", 1);
231
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));
238                 }
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));
244                 }
245                 /* Process according to thread state: */
246                 switch (pthread->state) {
247                 /* File descriptor read lock wait: */
248                 case PS_FDLR_WAIT:
249                 case PS_FDLW_WAIT:
250                 case PS_FDR_WAIT:
251                 case PS_FDW_WAIT:
252                         /* Write the lock details: */
253                         snprintf(s, sizeof(s), "fd %d[%s:%d]",
254                             pthread->data.fd.fd,
255                             pthread->data.fd.fname,
256                             pthread->data.fd.branch);
257                         __sys_write(fd, s, strlen(s));
258                         break;
259                 case PS_SIGWAIT:
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));
266                         }
267                         snprintf(s, sizeof(s), "(lo)\n");
268                         __sys_write(fd, s, strlen(s));
269                         break;
270                 /*
271                  * Trap other states that are not explicitly
272                  * coded to dump information:
273                  */
274                 default:
275                         /* Nothing to do here. */
276                         break;
277                 }
278         }
279 }
280
281 /* Set the thread name for debug: */
282 void
283 _pthread_set_name_np(pthread_t thread, const char *name)
284 {
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. */
289                         free(thread->name);
290                 }
291                 thread->name = strdup(name);
292         }
293 }