WARNS level 4 cleanup.
[dragonfly.git] / lib / libthread_xu / thread / thr_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  * $DragonFly: src/lib/libthread_xu/thread/thr_info.c,v 1.4 2006/04/06 13:03:09 davidxu Exp $
33  */
34
35 #include "namespace.h"
36 #include <machine/tls.h>
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <fcntl.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <pthread.h>
44 #include <errno.h>
45 #include "un-namespace.h"
46
47 #include "thr_private.h"
48
49 #ifndef NELEMENTS
50 #define NELEMENTS(arr)  (sizeof(arr) / sizeof(arr[0]))
51 #endif
52
53 static void     dump_thread(int fd, pthread_t pthread, int long_version);
54
55 struct s_thread_info {
56         enum pthread_state state;
57         char           *name;
58 };
59
60 /* Static variables: */
61 static const struct s_thread_info thread_info[] = {
62         {PS_RUNNING     , "Running"},
63         {PS_MUTEX_WAIT  , "Waiting on a mutex"},
64         {PS_JOIN        , "Waiting to join"},
65         {PS_SUSPENDED   , "Suspended"},
66         {PS_DEAD        , "Dead"},
67         {PS_DEADLOCK    , "Deadlocked"},
68         {PS_STATE_MAX   , "Not a real state!"}
69 };
70
71 void
72 _thread_dump_info(void)
73 {
74         char s[512], tmpfile[128];
75         pthread_t pthread;
76         int fd, i;
77
78         for (i = 0; i < 100000; i++) {
79                 snprintf(tmpfile, sizeof(tmpfile), "/tmp/pthread.dump.%u.%i",
80                         getpid(), i);
81                 /* Open the dump file for append and create it if necessary: */
82                 if ((fd = __sys_open(tmpfile, O_RDWR | O_CREAT | O_EXCL,
83                         0666)) < 0) {
84                                 /* Can't open the dump file. */
85                                 if (errno == EEXIST)
86                                         continue;
87                                 /*
88                                  * We only need to continue in case of
89                                  * EEXIT error. Most other error
90                                  * codes means that we will fail all
91                                  * the times.
92                                  */
93                                 return;
94                 } else {
95                         break;
96                 }
97         }
98         if (i==100000) {
99                 /* all 100000 possibilities are in use :( */
100                 return;
101         } else {
102                 /* Dump the active threads. */
103                 strcpy(s, "\n\n========\nACTIVE THREADS\n\n");
104                 __sys_write(fd, s, strlen(s));
105
106                 /* Enter a loop to report each thread in the global list: */
107                 TAILQ_FOREACH(pthread, &_thread_list, tle) {
108                         if (pthread->state != PS_DEAD)
109                                 dump_thread(fd, pthread, /*long_verson*/ 1);
110                 }
111
112                 /*
113                  * Dump the ready threads.
114                  * XXX - We can't easily do this because the run queues
115                  *       are per-KSEG.
116                  */
117                 strcpy(s, "\n\n========\nREADY THREADS - unimplemented\n\n");
118                 __sys_write(fd, s, strlen(s));
119
120
121                 /*
122                  * Dump the waiting threads.
123                  * XXX - We can't easily do this because the wait queues
124                  *       are per-KSEG.
125                  */
126                 strcpy(s, "\n\n========\nWAITING THREADS - unimplemented\n\n");
127                 __sys_write(fd, s, strlen(s));
128
129                 /* Close the dump file. */
130                 __sys_close(fd);
131         }
132 }
133
134 static void
135 dump_thread(int fd, pthread_t pthread, int long_version)
136 {
137         struct pthread *curthread = tls_get_curthread();
138         char s[512];
139         int i;
140
141         /* Find the state: */
142         for (i = 0; i < NELEMENTS(thread_info) - 1; i++)
143                 if (thread_info[i].state == pthread->state)
144                         break;
145
146         /* Output a record for the thread: */
147         snprintf(s, sizeof(s),
148             "--------------------\n"
149             "Thread %p (%s), scope %s, prio %3d, state %s [%s:%d]\n",
150             pthread, (pthread->name == NULL) ? "" : pthread->name,
151             pthread->attr.flags & PTHREAD_SCOPE_SYSTEM ? "system" : "process",
152             pthread->active_priority,
153             thread_info[i].name, pthread->fname, pthread->lineno);
154         __sys_write(fd, s, strlen(s));
155
156         if (long_version != 0) {
157                 /* Check if this is the running thread: */
158                 if (pthread == curthread) {
159                         /* Output a record for the running thread: */
160                         strcpy(s, "This is the running thread\n");
161                         __sys_write(fd, s, strlen(s));
162                 }
163                 /* Check if this is the initial thread: */
164                 if (pthread == _thr_initial) {
165                         /* Output a record for the initial thread: */
166                         strcpy(s, "This is the initial thread\n");
167                         __sys_write(fd, s, strlen(s));
168                 }
169         
170                 /* Process according to thread state: */
171                 switch (pthread->state) {
172                 /*
173                  * Trap other states that are not explicitly
174                  * coded to dump information:
175                  */
176                 default:
177                         snprintf(s, sizeof(s), "sigmask (hi) ");
178                         __sys_write(fd, s, strlen(s));
179                         for (i = _SIG_WORDS - 1; i >= 0; i--) {
180                                 snprintf(s, sizeof(s), "%08x ",
181                                     pthread->sigmask.__bits[i]);
182                                 __sys_write(fd, s, strlen(s));
183                         }
184                         snprintf(s, sizeof(s), "(lo)\n");
185                         __sys_write(fd, s, strlen(s));
186                         break;
187                 }
188         }
189 }
190
191 /* Set the thread name for debug: */
192 void
193 _pthread_set_name_np(pthread_t thread, char *name)
194 {
195         /* Check if the caller has specified a valid thread: */
196         if (thread != NULL && thread->magic == THR_MAGIC) {
197                 if (thread->name != NULL) {
198                         /* Free space for previous name. */
199                         free(thread->name);
200                 }
201                 thread->name = strdup(name);
202         }
203 }
204
205 __strong_reference(_pthread_set_name_np, pthread_set_name_np);