1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #include "primpl.h"
7 
8 #if defined(WIN95)
9 /*
10 ** Some local variables report warnings on Win95 because the code paths
11 ** using them are conditioned on HAVE_CUSTOME_USER_THREADS.
12 ** The pragma suppresses the warning.
13 **
14 */
15 #pragma warning(disable : 4101)
16 #endif
17 
18 /* XXX use unbuffered nspr stdio */
19 
20 PRFileDesc *_pr_dumpOut;
21 
_PR_DumpPrintf(PRFileDesc * fd,const char * fmt,...)22 PRUint32 _PR_DumpPrintf(PRFileDesc *fd, const char *fmt, ...)
23 {
24     char buf[100];
25     PRUint32 nb;
26     va_list ap;
27 
28     va_start(ap, fmt);
29     nb = PR_vsnprintf(buf, sizeof(buf), fmt, ap);
30     va_end(ap);
31     PR_Write(fd, buf, nb);
32 
33     return nb;
34 }
35 
_PR_DumpThread(PRFileDesc * fd,PRThread * thread)36 void _PR_DumpThread(PRFileDesc *fd, PRThread *thread)
37 {
38 
39 #ifndef _PR_GLOBAL_THREADS_ONLY
40     _PR_DumpPrintf(fd, "%05d[%08p] pri=%2d flags=0x%02x",
41                    thread->id, thread, thread->priority, thread->flags);
42     switch (thread->state) {
43         case _PR_RUNNABLE:
44         case _PR_RUNNING:
45             break;
46         case _PR_LOCK_WAIT:
47             _PR_DumpPrintf(fd, " lock=%p", thread->wait.lock);
48             break;
49         case _PR_COND_WAIT:
50             _PR_DumpPrintf(fd, " condvar=%p sleep=%lldms",
51                            thread->wait.cvar, thread->sleep);
52             break;
53         case _PR_SUSPENDED:
54             _PR_DumpPrintf(fd, " suspended");
55             break;
56     }
57     PR_Write(fd, "\n", 1);
58 #endif
59 
60     /* Now call dump routine */
61     if (thread->dump) {
62         thread->dump(fd, thread, thread->dumpArg);
63     }
64 }
65 
DumpThreadQueue(PRFileDesc * fd,PRCList * list)66 static void DumpThreadQueue(PRFileDesc *fd, PRCList *list)
67 {
68 #ifndef _PR_GLOBAL_THREADS_ONLY
69     PRCList *q;
70 
71     q = list->next;
72     while (q != list) {
73         PRThread *t = _PR_THREAD_PTR(q);
74         _PR_DumpThread(fd, t);
75         q = q->next;
76     }
77 #endif
78 }
79 
_PR_DumpThreads(PRFileDesc * fd)80 void _PR_DumpThreads(PRFileDesc *fd)
81 {
82     PRThread *t;
83     PRIntn i;
84 
85     _PR_DumpPrintf(fd, "Current Thread:\n");
86     t = _PR_MD_CURRENT_THREAD();
87     _PR_DumpThread(fd, t);
88 
89     _PR_DumpPrintf(fd, "Runnable Threads:\n");
90     for (i = 0; i < PR_ARRAY_SIZE(_PR_RUNQ(t->cpu)); i++) {
91         DumpThreadQueue(fd, &_PR_RUNQ(t->cpu)[i]);
92     }
93 
94     _PR_DumpPrintf(fd, "CondVar timed wait Threads:\n");
95     DumpThreadQueue(fd, &_PR_SLEEPQ(t->cpu));
96 
97     _PR_DumpPrintf(fd, "CondVar wait Threads:\n");
98     DumpThreadQueue(fd, &_PR_PAUSEQ(t->cpu));
99 
100     _PR_DumpPrintf(fd, "Suspended Threads:\n");
101     DumpThreadQueue(fd, &_PR_SUSPENDQ(t->cpu));
102 }
103 
PR_ShowStatus(void)104 PR_IMPLEMENT(void) PR_ShowStatus(void)
105 {
106     PRIntn is;
107 
108     if ( _PR_MD_CURRENT_THREAD()
109          && !_PR_IS_NATIVE_THREAD(_PR_MD_CURRENT_THREAD())) {
110         _PR_INTSOFF(is);
111     }
112     _pr_dumpOut = _pr_stderr;
113     _PR_DumpThreads(_pr_dumpOut);
114     if ( _PR_MD_CURRENT_THREAD()
115          && !_PR_IS_NATIVE_THREAD(_PR_MD_CURRENT_THREAD())) {
116         _PR_FAST_INTSON(is);
117     }
118 }
119 
120 PR_IMPLEMENT(void)
PR_SetThreadDumpProc(PRThread * thread,PRThreadDumpProc dump,void * arg)121 PR_SetThreadDumpProc(PRThread* thread, PRThreadDumpProc dump, void *arg)
122 {
123     thread->dump = dump;
124     thread->dumpArg = arg;
125 }
126