Back to index

lightning-sunbird  0.9+nobinonly
prdump.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "primpl.h"
00039 
00040 #if defined(WIN95)
00041 /*
00042 ** Some local variables report warnings on Win95 because the code paths 
00043 ** using them are conditioned on HAVE_CUSTOME_USER_THREADS.
00044 ** The pragma suppresses the warning.
00045 ** 
00046 */
00047 #pragma warning(disable : 4101)
00048 #endif
00049 
00050 /* XXX use unbuffered nspr stdio */
00051 
00052 PRFileDesc *_pr_dumpOut;
00053 
00054 PRUint32 _PR_DumpPrintf(PRFileDesc *fd, const char *fmt, ...)
00055 {
00056     char buf[100];
00057     PRUint32 nb;
00058     va_list ap;
00059 
00060     va_start(ap, fmt);
00061     nb = PR_vsnprintf(buf, sizeof(buf), fmt, ap);
00062     va_end(ap);
00063     PR_Write(fd, buf, nb);
00064 
00065     return nb;
00066 }
00067 
00068 void _PR_DumpThread(PRFileDesc *fd, PRThread *thread)
00069 {
00070 
00071 #ifndef _PR_GLOBAL_THREADS_ONLY
00072     _PR_DumpPrintf(fd, "%05d[%08p] pri=%2d flags=0x%02x",
00073                    thread->id, thread, thread->priority, thread->flags);
00074     switch (thread->state) {
00075       case _PR_RUNNABLE:
00076       case _PR_RUNNING:
00077         break;
00078       case _PR_LOCK_WAIT:
00079         _PR_DumpPrintf(fd, " lock=%p", thread->wait.lock);
00080         break;
00081       case _PR_COND_WAIT:
00082         _PR_DumpPrintf(fd, " condvar=%p sleep=%lldms",
00083                        thread->wait.cvar, thread->sleep);
00084         break;
00085       case _PR_SUSPENDED:
00086         _PR_DumpPrintf(fd, " suspended");
00087         break;
00088     }
00089     PR_Write(fd, "\n", 1);
00090 #endif
00091 
00092     /* Now call dump routine */
00093     if (thread->dump) {
00094        thread->dump(fd, thread, thread->dumpArg);
00095     }
00096 }
00097 
00098 static void DumpThreadQueue(PRFileDesc *fd, PRCList *list)
00099 {
00100 #ifndef _PR_GLOBAL_THREADS_ONLY
00101     PRCList *q;
00102 
00103     q = list->next;
00104     while (q != list) {
00105         PRThread *t = _PR_THREAD_PTR(q);
00106         _PR_DumpThread(fd, t);
00107         q = q->next;
00108     }
00109 #endif
00110 }
00111 
00112 void _PR_DumpThreads(PRFileDesc *fd)
00113 {
00114     PRThread *t;
00115     PRIntn i;
00116 
00117     _PR_DumpPrintf(fd, "Current Thread:\n");
00118     t = _PR_MD_CURRENT_THREAD();
00119     _PR_DumpThread(fd, t);
00120 
00121     _PR_DumpPrintf(fd, "Runnable Threads:\n");
00122     for (i = 0; i < 32; i++) {
00123         DumpThreadQueue(fd, &_PR_RUNQ(t->cpu)[i]);
00124     }
00125 
00126     _PR_DumpPrintf(fd, "CondVar timed wait Threads:\n");
00127     DumpThreadQueue(fd, &_PR_SLEEPQ(t->cpu));
00128 
00129     _PR_DumpPrintf(fd, "CondVar wait Threads:\n");
00130     DumpThreadQueue(fd, &_PR_PAUSEQ(t->cpu));
00131 
00132     _PR_DumpPrintf(fd, "Suspended Threads:\n");
00133     DumpThreadQueue(fd, &_PR_SUSPENDQ(t->cpu));
00134 }
00135 
00136 PR_IMPLEMENT(void) PR_ShowStatus(void)
00137 {
00138     PRIntn is;
00139 
00140     if ( _PR_MD_CURRENT_THREAD()
00141     && !_PR_IS_NATIVE_THREAD(_PR_MD_CURRENT_THREAD())) _PR_INTSOFF(is);
00142     _pr_dumpOut = _pr_stderr;
00143     _PR_DumpThreads(_pr_dumpOut);
00144     if ( _PR_MD_CURRENT_THREAD()
00145     && !_PR_IS_NATIVE_THREAD(_PR_MD_CURRENT_THREAD())) _PR_FAST_INTSON(is);
00146 }
00147 
00148 PR_IMPLEMENT(void)
00149 PR_SetThreadDumpProc(PRThread* thread, PRThreadDumpProc dump, void *arg)
00150 {
00151     thread->dump = dump;
00152     thread->dumpArg = arg;
00153 }