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
38 #include "thr_private.h"
41 #define NELEMENTS(arr) (sizeof(arr) / sizeof(arr[0]))
44 LT10_COMPAT_PRIVATE(_pthread_set_name_np);
45 LT10_COMPAT_DEFAULT(pthread_set_name_np);
47 static void dump_thread(int fd, pthread_t pthread, int long_version);
49 __weak_reference(_pthread_set_name_np, pthread_set_name_np);
51 struct s_thread_info {
52 enum pthread_state state;
56 /* Static variables: */
57 static const struct s_thread_info thread_info[] = {
58 {PS_RUNNING , "Running"},
59 {PS_LOCKWAIT , "Waiting on an internal lock"},
60 {PS_MUTEX_WAIT , "Waiting on a mutex"},
61 {PS_COND_WAIT , "Waiting on a condition variable"},
62 {PS_SLEEP_WAIT , "Sleeping"},
63 {PS_SIGSUSPEND , "Suspended, waiting for a signal"},
64 {PS_SIGWAIT , "Waiting for a signal"},
65 {PS_JOIN , "Waiting to join"},
66 {PS_SUSPENDED , "Suspended"},
68 {PS_DEADLOCK , "Deadlocked"},
69 {PS_STATE_MAX , "Not a real state!"}
73 _thread_dump_info(void)
75 char s[512], tmpfile[128];
79 for (i = 0; i < 100000; i++) {
80 snprintf(tmpfile, sizeof(tmpfile), "/tmp/pthread.dump.%u.%i",
82 /* Open the dump file for append and create it if necessary: */
83 if ((fd = __sys_open(tmpfile, O_RDWR | O_CREAT | O_EXCL,
85 /* Can't open the dump file. */
89 * We only need to continue in case of
90 * EEXIT error. Most other error
91 * codes means that we will fail all
100 /* all 100000 possibilities are in use :( */
103 /* Dump the active threads. */
104 strcpy(s, "\n\n========\nACTIVE THREADS\n\n");
105 __sys_write(fd, s, strlen(s));
107 /* Enter a loop to report each thread in the global list: */
108 TAILQ_FOREACH(pthread, &_thread_list, tle) {
109 if (pthread->state != PS_DEAD)
110 dump_thread(fd, pthread, /*long_verson*/ 1);
114 * Dump the ready threads.
115 * XXX - We can't easily do this because the run queues
118 strcpy(s, "\n\n========\nREADY THREADS - unimplemented\n\n");
119 __sys_write(fd, s, strlen(s));
123 * Dump the waiting threads.
124 * XXX - We can't easily do this because the wait queues
127 strcpy(s, "\n\n========\nWAITING THREADS - unimplemented\n\n");
128 __sys_write(fd, s, strlen(s));
130 /* Close the dump file. */
136 dump_thread(int fd, pthread_t pthread, int long_version)
138 struct pthread *curthread = _get_curthread();
142 /* Find the state: */
143 for (i = 0; i < NELEMENTS(thread_info) - 1; i++)
144 if (thread_info[i].state == pthread->state)
147 /* Output a record for the thread: */
148 snprintf(s, sizeof(s),
149 "--------------------\n"
150 "Thread %p (%s), scope %s, prio %3d, blocked %s, state %s [%s:%d]\n",
151 pthread, (pthread->name == NULL) ? "" : pthread->name,
152 pthread->attr.flags & PTHREAD_SCOPE_SYSTEM ? "system" : "process",
153 pthread->active_priority, (pthread->blocked != 0) ? "yes" : "no",
154 thread_info[i].name, pthread->fname, pthread->lineno);
155 __sys_write(fd, s, strlen(s));
157 if (long_version != 0) {
158 /* Check if this is the running thread: */
159 if (pthread == curthread) {
160 /* Output a record for the running thread: */
161 strcpy(s, "This is the running thread\n");
162 __sys_write(fd, s, strlen(s));
164 /* Check if this is the initial thread: */
165 if (pthread == _thr_initial) {
166 /* Output a record for the initial thread: */
167 strcpy(s, "This is the initial thread\n");
168 __sys_write(fd, s, strlen(s));
171 /* Process according to thread state: */
172 switch (pthread->state) {
174 snprintf(s, sizeof(s), "sigmask (hi) ");
175 __sys_write(fd, s, strlen(s));
176 for (i = _SIG_WORDS - 1; i >= 0; i--) {
177 snprintf(s, sizeof(s), "%08x ",
178 pthread->sigmask.__bits[i]);
179 __sys_write(fd, s, strlen(s));
181 snprintf(s, sizeof(s), "(lo)\n");
182 __sys_write(fd, s, strlen(s));
184 snprintf(s, sizeof(s), "waitset (hi) ");
185 __sys_write(fd, s, strlen(s));
186 for (i = _SIG_WORDS - 1; i >= 0; i--) {
187 snprintf(s, sizeof(s), "%08x ",
188 pthread->data.sigwait->waitset->__bits[i]);
189 __sys_write(fd, s, strlen(s));
191 snprintf(s, sizeof(s), "(lo)\n");
192 __sys_write(fd, s, strlen(s));
195 * Trap other states that are not explicitly
196 * coded to dump information:
199 snprintf(s, sizeof(s), "sigmask (hi) ");
200 __sys_write(fd, s, strlen(s));
201 for (i = _SIG_WORDS - 1; i >= 0; i--) {
202 snprintf(s, sizeof(s), "%08x ",
203 pthread->sigmask.__bits[i]);
204 __sys_write(fd, s, strlen(s));
206 snprintf(s, sizeof(s), "(lo)\n");
207 __sys_write(fd, s, strlen(s));
213 /* Set the thread name for debug: */
215 _pthread_set_name_np(pthread_t thread, char *name)
217 struct pthread *curthread = _get_curthread();
222 new_name = strdup(name);
223 /* Add a reference to the target thread. */
224 if (_thr_ref_add(curthread, thread, 0) != 0) {
229 THR_THREAD_LOCK(curthread, thread);
230 prev_name = thread->name;
231 thread->name = new_name;
232 THR_THREAD_UNLOCK(curthread, thread);
233 _thr_ref_delete(curthread, thread);
234 if (prev_name != NULL) {
235 /* Free space for previous name. */
241 /* XXX - Should return error code. */