osdebug.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2005 by egnite Software GmbH. All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
00021  * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  *
00032  */
00033 
00034 /*
00035  * $Log: osdebug.c,v $
00036  * Revision 1.8  2008/08/11 07:00:34  haraldkipp
00037  * BSD types replaced by stdint types (feature request #1282721).
00038  *
00039  * Revision 1.7  2005/07/26 15:50:00  haraldkipp
00040  * Cygwin support added.
00041  *
00042  * Revision 1.6  2005/07/12 14:07:14  freckle
00043  * removed unnecessary critical sections
00044  *
00045  * Revision 1.5  2005/06/12 16:55:24  haraldkipp
00046  * Timer pool has been removed from the kernel.
00047  *
00048  * Revision 1.4  2004/04/07 12:13:58  haraldkipp
00049  * Matthias Ringwald's *nix emulation added
00050  *
00051  * Revision 1.3  2004/03/19 09:05:12  jdubiec
00052  * Fixed format strings declarations for AVR.
00053  *
00054  * Revision 1.2  2004/03/16 16:48:45  haraldkipp
00055  * Added Jan Dubiec's H8/300 port.
00056  *
00057  * Revision 1.1.1.1  2003/05/09 14:41:52  haraldkipp
00058  * Initial using 3.2.1
00059  *
00060  * Revision 1.8  2003/04/21 17:09:01  harald
00061  * *** empty log message ***
00062  *
00063  * Revision 1.7  2003/02/04 18:15:57  harald
00064  * Version 3 released
00065  *
00066  * Revision 1.6  2002/06/26 17:29:44  harald
00067  * First pre-release with 2.4 stack
00068  *
00069  */
00070 
00071 #include <compiler.h>
00072 #include <sys/thread.h>
00073 #include <sys/timer.h>
00074 #include <sys/event.h>
00075 #include <sys/heap.h>
00076 
00077 #include <sys/osdebug.h>
00078 
00079 #if defined(__arm__) || defined(__m68k__) || defined(__H8300H__) || defined(__H8300S__) || defined(__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
00080 #define ARCH_32BIT
00081 #endif
00082 
00083 
00084 FILE *__os_trs;
00085 uint_fast8_t __os_trf;
00086 
00087 FILE *__heap_trs;
00088 uint_fast8_t __heap_trf;
00089 
00090 static char *states[] = { "TRM", "RUN", "RDY", "SLP" };
00091 
00092 #ifdef ARCH_32BIT
00093 /*                              12345678 12345678 1234 123 12345678 12345678 12345678 1234556789 */
00094 static prog_char qheader[] = "\nHandle   Name     Prio Sta Queue    Timer    StackPtr FreeMem\n";
00095 #else
00096 /*                              1234 12345678 1234 123 1234 1234 1234 12345 */
00097 static prog_char qheader[] = "\nHndl Name     Prio Sta QUE  Timr StkP FreeMem\n";
00098 #endif
00099 
00109 void NutDumpThreadQueue(FILE * stream, NUTTHREADINFO * tdp)
00110 {
00111 #ifdef ARCH_32BIT
00112     static prog_char fmt[] = "%08lX %-8s %4u %s %08lX %08lX %08lX %9lu %s\n";
00113 #else
00114     static prog_char fmt[] = "%04X %-8s %4u %s %04X %04X %04X %5u %s\n";
00115 #endif
00116 
00117     if (tdp == SIGNALED)
00118         fputs("SIGNALED\n", stream);
00119     else {
00120         while (tdp) {
00121 #if defined(__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
00122             fprintf_P(stream, fmt, (uptr_t) tdp, tdp->td_name, tdp->td_priority,
00123                       states[tdp->td_state], (uptr_t) tdp->td_queue, (uptr_t) tdp->td_timer, tdp->td_cs_level, 0, "--");
00124 #else
00125             fprintf_P(stream, fmt, (uptr_t) tdp, tdp->td_name, tdp->td_priority,
00126                       states[tdp->td_state], (uptr_t) tdp->td_queue,
00127                       (uptr_t) tdp->td_timer, tdp->td_sp,
00128                       (uptr_t) tdp->td_sp - (uptr_t) tdp->td_memory,
00129                       *((uint32_t *) tdp->td_memory) != DEADBEEF
00130                       && *((uint32_t *) (tdp->td_memory + 4)) != DEADBEEF
00131                       && *((uint32_t *) (tdp->td_memory + 8)) != DEADBEEF
00132                       && *((uint32_t *) (tdp->td_memory + 12)) != DEADBEEF ? "FAIL" : "OK");
00133 #endif
00134             tdp = tdp->td_qnxt;
00135 
00136         }
00137     }
00138 }
00139 
00148 void NutDumpThreadList(FILE * stream)
00149 {
00150 
00151 #ifdef ARCH_32BIT
00152     static prog_char fmt1[] = "%08lX %-8s %4u %s %08lX %08lX %08lX %9lu %s";
00153     static prog_char fmt2[] = " %08lX";
00154 #else
00155     static prog_char fmt1[] = "%04X %-8s %4u %s %04X %04X %04X %5u %s";
00156     static prog_char fmt2[] = " %04X";
00157 #endif
00158     NUTTHREADINFO *tqp;
00159     NUTTHREADINFO *tdp;
00160 
00161     fputs_P(qheader, stream);
00162 
00163     tdp = nutThreadList;
00164     while (tdp) {
00165 #if defined(__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
00166         fprintf_P(stream, fmt1, (uptr_t) tdp, tdp->td_name, tdp->td_priority,
00167                   states[tdp->td_state], (uptr_t) tdp->td_queue, (uptr_t) tdp->td_timer, tdp->td_cs_level, 0, "--");
00168 #else
00169         fprintf_P(stream, fmt1, (uptr_t) tdp, tdp->td_name, tdp->td_priority,
00170                   states[tdp->td_state], (uptr_t) tdp->td_queue,
00171                   (uptr_t) tdp->td_timer, tdp->td_sp,
00172                   (uptr_t) tdp->td_sp - (uptr_t) tdp->td_memory, *((uint32_t *) tdp->td_memory) != DEADBEEF ? "FAIL" : "OK");
00173 #endif
00174         if (tdp->td_queue) {
00175             tqp = *(NUTTHREADINFO **) (tdp->td_queue);
00176             if (tqp == SIGNALED)
00177                 fputs("SIGNALED", stream);
00178             else {
00179                 while (tqp) {
00180                     fprintf_P(stream, fmt2, (uptr_t) tqp);
00181                     tqp = tqp->td_qnxt;
00182                 }
00183             }
00184         }
00185         fputc('\n', stream);
00186         tdp = tdp->td_next;
00187     }
00188 }
00189 
00198 void NutDumpTimerList(FILE * stream)
00199 {
00200 
00201     static prog_char wname[] = "NutThreadWake";
00202     static prog_char tname[] = "NutEventTimeout";
00203 #ifdef ARCH_32BIT
00204     static prog_char theader[] = "Address  Ticks  Left Callback\n";
00205     static prog_char fmt1[] = "%08lX%6lu%6lu ";
00206     static prog_char fmt2[] = "%09lX";
00207     static prog_char fmt3[] = "(%08lX)\n";
00208 #else
00209     static prog_char theader[] = "Addr Ticks  Left Callback\n";
00210     static prog_char fmt1[] = "%04X%6lu%6lu ";
00211     static prog_char fmt2[] = "%05lX";
00212     static prog_char fmt3[] = "(%04X)\n";
00213 #endif
00214 
00215     NUTTIMERINFO *tnp;
00216     if ((tnp = nutTimerList) != 0) {
00217         fputs_P(theader, stream);
00218         while (tnp) {
00219             fprintf_P(stream, fmt1, (uptr_t) tnp, tnp->tn_ticks, tnp->tn_ticks_left);
00220             if (tnp->tn_callback == NutThreadWake)
00221                 fputs_P(wname, stream);
00222             else if (tnp->tn_callback == NutEventTimeout)
00223                 fputs_P(tname, stream);
00224             else
00225                 fprintf_P(stream, fmt2, (uint32_t) ((uptr_t) tnp->tn_callback) << 1);
00226             fprintf_P(stream, fmt3, (uptr_t) tnp->tn_arg);
00227             tnp = tnp->tn_next;
00228         }
00229     }
00230 }
00231 
00239 void NutTraceOs(FILE * stream, uint8_t flags)
00240 {
00241     if (stream)
00242         __os_trs = stream;
00243     if (__os_trs)
00244         __os_trf = flags;
00245     else
00246         __os_trf = 0;
00247 }
00248 
00255 void NutDumpHeap(FILE * stream)
00256 {
00257 
00258 #ifdef ARCH_32BIT
00259     static prog_char fmt1[] = "%08lx %9ld\n";
00260     static prog_char fmt2[] = "%lu counted, but %lu reported\n";
00261     static prog_char fmt3[] = "%lu bytes free\n";
00262 #else
00263     static prog_char fmt1[] = "%04x %5d\n";
00264     static prog_char fmt2[] = "%u counted, but %u reported\n";
00265     static prog_char fmt3[] = "%u bytes free\n";
00266 #endif
00267     HEAPNODE *node;
00268     size_t sum = 0;
00269     size_t avail;
00270 
00271     fputc('\n', stream);
00272     for (node = heapFreeList; node; node = node->hn_next) {
00273         sum += node->hn_size;
00274         fprintf_P(stream, fmt1, (uptr_t) node, node->hn_size);
00275         /* TODO: Remove hardcoded RAMSTART and RAMEND */
00276         if ((uptr_t) node < 0x60 || (uptr_t) node > 0x7fff)
00277             break;
00278     }
00279     if ((avail = NutHeapAvailable()) != sum)
00280         fprintf_P(stream, fmt2, sum, avail);
00281     else
00282         fprintf_P(stream, fmt3, avail);
00283 }
00284 
00292 void NutTraceHeap(FILE * stream, uint8_t flags)
00293 {
00294     if (stream)
00295         __heap_trs = stream;
00296     if (__heap_trs)
00297         __heap_trf = flags;
00298     else
00299         __heap_trf = 0;
00300 }

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/