thr_info.c revision 112918
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: head/lib/libthr/thread/thr_info.c 112918 2003-04-01 03:46:29Z jeff $
33 */
34#include <stdio.h>
35#include <stdlib.h>
36#include <fcntl.h>
37#include <string.h>
38#include <unistd.h>
39#include <pthread.h>
40#include <errno.h>
41#include "thr_private.h"
42
43#ifndef NELEMENTS
44#define NELEMENTS(arr)	(sizeof(arr) / sizeof(arr[0]))
45#endif
46
47static void	dump_thread(int fd, pthread_t pthread, int long_version);
48
49__weak_reference(_pthread_set_name_np, pthread_set_name_np);
50
51struct s_thread_info {
52	enum pthread_state state;
53	char           *name;
54};
55
56/* Static variables: */
57static const struct s_thread_info thread_info[] = {
58	{PS_RUNNING	, "Running"},
59	{PS_MUTEX_WAIT	, "Waiting on a mutex"},
60	{PS_COND_WAIT	, "Waiting on a condition variable"},
61	{PS_SLEEP_WAIT	, "Sleeping"},
62	{PS_WAIT_WAIT	, "Waiting process"},
63	{PS_JOIN	, "Waiting to join"},
64	{PS_DEAD	, "Dead"},
65	{PS_DEADLOCK	, "Deadlocked"},
66	{PS_STATE_MAX	, "Not a real state!"}
67};
68
69void
70_thread_dump_info(void)
71{
72	char            s[512];
73	int             fd;
74	int             i;
75	pthread_t       pthread;
76	char		tmpfile[128];
77
78	for (i = 0; i < 100000; i++) {
79		snprintf(tmpfile, sizeof(tmpfile), "/tmp/uthread.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		/* Output a header for 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			dump_thread(fd, pthread, /*long_verson*/ 1);
109		}
110
111		/* Check if there are no dead threads: */
112		if (TAILQ_FIRST(&_dead_list) == NULL) {
113			/* Output a record: */
114			strcpy(s, "\n\nTHERE ARE NO DEAD THREADS\n");
115			__sys_write(fd, s, strlen(s));
116		} else {
117			/* Output a header for dead threads: */
118			strcpy(s, "\n\nDEAD THREADS\n\n");
119			__sys_write(fd, s, strlen(s));
120
121			/*
122			 * Enter a loop to report each thread in the global
123			 * dead thread list:
124			 */
125			TAILQ_FOREACH(pthread, &_dead_list, dle) {
126				dump_thread(fd, pthread, /*long_version*/ 0);
127			}
128		}
129
130		/* Close the dump file: */
131		__sys_close(fd);
132	}
133}
134
135static void
136dump_thread(int fd, pthread_t pthread, int long_version)
137{
138	struct pthread	*curthread = _get_curthread();
139	char		s[512];
140	int		i;
141
142	/* Find the state: */
143	for (i = 0; i < NELEMENTS(thread_info) - 1; i++)
144		if (thread_info[i].state == pthread->state)
145			break;
146
147	/* Output a record for the thread: */
148	snprintf(s, sizeof(s),
149	    "--------------------\nThread %p (%s) prio %3d state %s [%s:%d]\n",
150	    pthread, (pthread->name == NULL) ? "" : pthread->name,
151	    pthread->active_priority, thread_info[i].name, pthread->fname,
152	    pthread->lineno);
153	__sys_write(fd, s, strlen(s));
154
155	if (long_version != 0) {
156		/* Check if this is the running thread: */
157		if (pthread == curthread) {
158			/* Output a record for the running thread: */
159			strcpy(s, "This is the running thread\n");
160			__sys_write(fd, s, strlen(s));
161		}
162		/* Check if this is the initial thread: */
163		if (pthread == _thread_initial) {
164			/* Output a record for the initial thread: */
165			strcpy(s, "This is the initial thread\n");
166			__sys_write(fd, s, strlen(s));
167		}
168		/* Process according to thread state: */
169		switch (pthread->state) {
170		/*
171		 * Trap other states that are not explicitly
172		 * coded to dump information:
173		 */
174		default:
175			/* Nothing to do here. */
176			break;
177		}
178	}
179}
180
181/* Set the thread name for debug: */
182void
183_pthread_set_name_np(pthread_t thread, const char *name)
184{
185	/* Check if the caller has specified a valid thread: */
186	if (thread != NULL && thread->magic == PTHREAD_MAGIC) {
187		if (thread->name != NULL) {
188			/* Free space for previous name. */
189			free(thread->name);
190		}
191		thread->name = strdup(name);
192	}
193}
194