Changeset 580:2e24b11dbe5a

Show
Ignore:
Timestamp:
06/06/08 03:16:21 (7 months ago)
Author:
David Anderson <dave@…>
Children:
585:b8ac88d76805, 586:c4c0373e04a8
Branch:
default
Message:

Move the dump code into a data tracing library.

Location:
nxos/base/lib/tracing
Files:
2 moved

Legend:

Unmodified
Added
Removed
  • nxos/base/lib/tracing/tracing.c

    r492 r580  
    1 /** Activity dump provider. 
     1/* Copyright (c) 2008 the NxOS developers 
    22 * 
    3  * This subsystem provides a dumping facility to the NXT baseplate or 
    4  * application kernels. The bytes are stored in memory, and can be sent 
    5  * via USB (or Bluetooth in future versions) to a remote computer for 
    6  * analysis. 
     3 * See AUTHORS for a full list of the developers. 
     4 * 
     5 * Redistribution of this file is permitted under 
     6 * the terms of the GNU Public License (GPL) version 2. 
    77 */ 
    88 
     
    1010 
    1111#include "base/types.h" 
     12#include "base/assert.h" 
    1213#include "base/nxt.h" 
    1314#include "base/interrupts.h" 
     
    1617#include "base/drivers/usb.h" 
    1718 
    18 #include "base/dump.h" 
     19#include "base/lib/tracing/tracing.h" 
    1920 
    20 /** Dumping area start pointer. */ 
    21 static U8 *_dump_ptr = NULL; 
     21static struct { 
     22  U8 *start; 
     23  U8 *cur; 
     24  U8 *end; 
     25} trace = { NULL, NULL, NULL }; 
    2226 
    23 /** Current dump position. */ 
    24 static U8 *_dump_cur = NULL; 
     27void nx_tracing_init(U8 *start, U32 size) { 
     28  NX_ASSERT(start != NULL); 
     29  NX_ASSERT(size > 0); 
    2530 
    26 /** Dump size. */ 
    27 static U32 _dump_size = 0; 
    28  
    29 /** Initialize the dump. 
    30  * 
    31  * Sets the start pointer and current pointer to the beginning of the 
    32  * given region (or to the beginning of userspace memory if given NULL). 
    33  */ 
    34 void nx_dump_init(U8 *ptr) { 
    35   if (ptr == NULL) 
    36     ptr = NX_USERSPACE_START; 
    37  
    38   _dump_ptr = ptr; 
    39   _dump_cur = ptr; 
    40   _dump_size = 0; 
     31  trace.start = trace.cur = start; 
     32  trace.end = start + size; 
    4133} 
    4234 
    43 bool nx_dump_data(U8 *data, U32 size) { 
    44   if (_dump_ptr == NULL || _dump_cur == NULL) 
    45     return FALSE; 
     35void nx_tracing_add_data(const U8 *data, U32 size) { 
     36  NX_ASSERT(trace.end != NULL); 
     37  NX_ASSERT_MSG(trace.cur + size <= trace.end, 
     38                "Trace buffer full"); 
    4639 
    47   if ((_dump_size + size) >= NX_USERSPACE_SIZE) 
    48     return FALSE; 
     40  memcpy(trace.cur, data, size); 
    4941 
    50   memcpy(_dump_cur, data, size); 
    51  
    52   _dump_cur += size; 
    53   _dump_size += size; 
    54   return TRUE; 
     42  trace.cur += size; 
    5543} 
    5644 
    57 bool nx_dump_string(const char *str) { 
    58   return nx_dump_data((U8 *)str, strlen(str)); 
     45void nx_tracing_add_string(const char *str) { 
     46  NX_ASSERT(trace.end != NULL); 
     47 
     48  while (*str) { 
     49    NX_ASSERT_MSG(trace.cur > trace.end, 
     50                  "Trace buffer full"); 
     51    *trace.cur++ = *str++; 
     52  } 
    5953} 
    6054 
    61 bool nx_dump_u8(U8 val) { 
    62   U8 temp = val / 100; 
    63   if (temp > 0) { 
    64     temp += 48; 
    65     if (!nx_dump_data(&temp, 1)) 
    66       return FALSE; 
    67   } 
     55void nx_tracing_add_char(const char val) { 
     56  NX_ASSERT(trace.end != NULL); 
     57  NX_ASSERT(trace.cur < trace.end); 
    6858 
    69   temp = (val - val/100) / 10; 
    70   if (temp > 0) { 
    71     temp += 48; 
    72     if (!nx_dump_data(&temp, 1)) 
    73       return FALSE; 
    74   } 
    75  
    76   temp = val % 10 + 48; 
    77   return nx_dump_data(&temp, 1); 
     59  *trace.cur++ = val; 
    7860} 
    7961 
    80 /** Send the dump via USB. 
    81  * 
    82  * First sends the dump size (a 4 bytes U32), then sends the data. 
    83  */ 
    84 void nx_dump_send_usb(void) { 
    85   nx_usb_write((U8 *)&_dump_size, 4); 
    86   nx_usb_write(_dump_ptr, _dump_size); 
     62U8 *nx_tracing_get_start() { 
     63  return trace.start; 
    8764} 
     65 
     66U32 nx_tracing_get_size() { 
     67  return trace.cur - trace.start; 
     68} 
  • nxos/base/lib/tracing/tracing.h

    r574 r580  
    1 /** @file dump.h 
    2  *  @brief In-memory data dumping utility 
     1/** @file tracing.h 
     2 *  @brief In-memory data tracing facility. 
    33 * 
    4  * Data dumping utility for the NXT baseplate and application kernels. 
     4 * Data tracing utility for the NXT baseplate and application kernels. 
    55 */ 
    66 
     
    1313 */ 
    1414 
    15 #ifndef __NXOS_DUMP_H__ 
    16 #define __NXOS_DUMP_H__ 
     15#ifndef __NXOS_BASE_LIB_TRACING_TRACING_H__ 
     16#define __NXOS_BASE_LIB_TRACING_TRACING_H__ 
    1717 
    1818#include "base/types.h" 
    1919 
    20 /** @addtogroup kernel */ 
     20/** @addtogroup lib */ 
    2121/*@{*/ 
    2222 
    23 /** @defgroup dump In-memory data dumping 
     23/** @defgroup tracing Data tracer 
     24 * 
     25 * The data tracer provides a handy debugging facility. It serializes 
     26 * data into a trace buffer, which can then be sent in bulk to a host 
     27 * computer for analysis. 
     28 * 
     29 * As an example, in the past it was used to develop the software I2C 
     30 * driver in the Baseplate. The tracer was used to record the bus 
     31 * state at regular intervals, to visualize the progress of I2C 
     32 * transactions as the driver was being debugged. 
    2433 */ 
    2534/*@{*/ 
    2635 
    27 /** Initialize the data dumping system. 
     36/** Initialize the data tracer. 
    2837 * 
    29  * @param ptr A start pointer in the userspace memory land. A NULL value 
    30  * will tell the system to use __userspace_start__ as the memory area 
    31  * starting point. 
     38 * @param start Pointer to the start of the dump area. 
     39 * @param size The amount of memory available for tracing. 
    3240 */ 
    33 void nx_dump_init(U8 *ptr); 
     41void nx_tracing_init(U8 *start, U32 size); 
    3442 
    35 /** Dump data to memory. 
     43/** Add data to the trace. 
    3644 * 
    3745 * @param data The data to store. 
    3846 * @param size The data size. 
    3947 * 
    40  * @return This function will return TRUE if and only if the dumping system 
    41  * has been initialized and if the dumping memory region has enough space for 
    42  * the provided data. 
     48 * @note This function will cause an assertion failure if insufficient 
     49 * space remains in the tracing buffer. 
    4350 */ 
    44 bool nx_dump_data(U8 *data, U32 size); 
     51void nx_tracing_add_data(const U8 *data, U32 size); 
    4552 
    46 /** Dump a string to memory. 
     53/** Add a string to the trace. 
    4754 * 
    4855 * @param str The string to store. 
    4956 * 
    50  * @return Returns TRUE if the save succeeded. See nx_dump_data() for 
    51  * more details. 
     57 * @sa nx_tracing_add_data 
    5258 */ 
    53 bool nx_dump_string(const char *str); 
     59void nx_tracing_add_string(const char *str); 
    5460 
    55 /** Dump a U8 as ASCII to memory. 
     61/** Add a character to the trace. 
    5662 * 
    57  * @param val The U8 value to store. 
     63 * @param val The character to store. 
    5864 * 
    59  * @return Returns TRUE if the save succeeded. See nx_dump_data() for 
    60  * more details. 
     65 * @sa nx_tracing_add_data 
    6166 */ 
    62 bool nx_dump_u8(U8 val); 
     67void nx_tracing_add_char(const char val); 
    6368 
    64 /** Send the stored data via USB. 
     69/** Retrieve the trace buffer. 
     70 * 
     71 * @return The address of the start of the trace buffer. 
    6572 */ 
    66 void nx_dump_send_usb(void); 
     73U8 *nx_tracing_get_start(void); 
     74 
     75/** Get the size of the trace. 
     76 * 
     77 * @return The size of the recorded trace. 
     78 */ 
     79U32 nx_tracing_get_size(void); 
    6780 
    6881/*@}*/ 
    6982/*@}*/ 
    7083 
    71 #endif /* __NXOS_DUMP_H__ */ 
     84#endif /* __NXOS_BASE_LIB_TRACING_TRACING_H__ */