Changeset 580:2e24b11dbe5a
- 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:
-
Legend:
- Unmodified
- Added
- Removed
-
|
r492
|
r580
|
|
| 1 | | /** Activity dump provider. |
| | 1 | /* Copyright (c) 2008 the NxOS developers |
| 2 | 2 | * |
| 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. |
| 7 | 7 | */ |
| 8 | 8 | |
| … |
… |
|
| 10 | 10 | |
| 11 | 11 | #include "base/types.h" |
| | 12 | #include "base/assert.h" |
| 12 | 13 | #include "base/nxt.h" |
| 13 | 14 | #include "base/interrupts.h" |
| … |
… |
|
| 16 | 17 | #include "base/drivers/usb.h" |
| 17 | 18 | |
| 18 | | #include "base/dump.h" |
| | 19 | #include "base/lib/tracing/tracing.h" |
| 19 | 20 | |
| 20 | | /** Dumping area start pointer. */ |
| 21 | | static U8 *_dump_ptr = NULL; |
| | 21 | static struct { |
| | 22 | U8 *start; |
| | 23 | U8 *cur; |
| | 24 | U8 *end; |
| | 25 | } trace = { NULL, NULL, NULL }; |
| 22 | 26 | |
| 23 | | /** Current dump position. */ |
| 24 | | static U8 *_dump_cur = NULL; |
| | 27 | void nx_tracing_init(U8 *start, U32 size) { |
| | 28 | NX_ASSERT(start != NULL); |
| | 29 | NX_ASSERT(size > 0); |
| 25 | 30 | |
| 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; |
| 41 | 33 | } |
| 42 | 34 | |
| 43 | | bool nx_dump_data(U8 *data, U32 size) { |
| 44 | | if (_dump_ptr == NULL || _dump_cur == NULL) |
| 45 | | return FALSE; |
| | 35 | void 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"); |
| 46 | 39 | |
| 47 | | if ((_dump_size + size) >= NX_USERSPACE_SIZE) |
| 48 | | return FALSE; |
| | 40 | memcpy(trace.cur, data, size); |
| 49 | 41 | |
| 50 | | memcpy(_dump_cur, data, size); |
| 51 | | |
| 52 | | _dump_cur += size; |
| 53 | | _dump_size += size; |
| 54 | | return TRUE; |
| | 42 | trace.cur += size; |
| 55 | 43 | } |
| 56 | 44 | |
| 57 | | bool nx_dump_string(const char *str) { |
| 58 | | return nx_dump_data((U8 *)str, strlen(str)); |
| | 45 | void 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 | } |
| 59 | 53 | } |
| 60 | 54 | |
| 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 | | } |
| | 55 | void nx_tracing_add_char(const char val) { |
| | 56 | NX_ASSERT(trace.end != NULL); |
| | 57 | NX_ASSERT(trace.cur < trace.end); |
| 68 | 58 | |
| 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; |
| 78 | 60 | } |
| 79 | 61 | |
| 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); |
| | 62 | U8 *nx_tracing_get_start() { |
| | 63 | return trace.start; |
| 87 | 64 | } |
| | 65 | |
| | 66 | U32 nx_tracing_get_size() { |
| | 67 | return trace.cur - trace.start; |
| | 68 | } |
-
|
r574
|
r580
|
|
| 1 | | /** @file dump.h |
| 2 | | * @brief In-memory data dumping utility |
| | 1 | /** @file tracing.h |
| | 2 | * @brief In-memory data tracing facility. |
| 3 | 3 | * |
| 4 | | * Data dumping utility for the NXT baseplate and application kernels. |
| | 4 | * Data tracing utility for the NXT baseplate and application kernels. |
| 5 | 5 | */ |
| 6 | 6 | |
| … |
… |
|
| 13 | 13 | */ |
| 14 | 14 | |
| 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__ |
| 17 | 17 | |
| 18 | 18 | #include "base/types.h" |
| 19 | 19 | |
| 20 | | /** @addtogroup kernel */ |
| | 20 | /** @addtogroup lib */ |
| 21 | 21 | /*@{*/ |
| 22 | 22 | |
| 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. |
| 24 | 33 | */ |
| 25 | 34 | /*@{*/ |
| 26 | 35 | |
| 27 | | /** Initialize the data dumping system. |
| | 36 | /** Initialize the data tracer. |
| 28 | 37 | * |
| 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. |
| 32 | 40 | */ |
| 33 | | void nx_dump_init(U8 *ptr); |
| | 41 | void nx_tracing_init(U8 *start, U32 size); |
| 34 | 42 | |
| 35 | | /** Dump data to memory. |
| | 43 | /** Add data to the trace. |
| 36 | 44 | * |
| 37 | 45 | * @param data The data to store. |
| 38 | 46 | * @param size The data size. |
| 39 | 47 | * |
| 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. |
| 43 | 50 | */ |
| 44 | | bool nx_dump_data(U8 *data, U32 size); |
| | 51 | void nx_tracing_add_data(const U8 *data, U32 size); |
| 45 | 52 | |
| 46 | | /** Dump a string to memory. |
| | 53 | /** Add a string to the trace. |
| 47 | 54 | * |
| 48 | 55 | * @param str The string to store. |
| 49 | 56 | * |
| 50 | | * @return Returns TRUE if the save succeeded. See nx_dump_data() for |
| 51 | | * more details. |
| | 57 | * @sa nx_tracing_add_data |
| 52 | 58 | */ |
| 53 | | bool nx_dump_string(const char *str); |
| | 59 | void nx_tracing_add_string(const char *str); |
| 54 | 60 | |
| 55 | | /** Dump a U8 as ASCII to memory. |
| | 61 | /** Add a character to the trace. |
| 56 | 62 | * |
| 57 | | * @param val The U8 value to store. |
| | 63 | * @param val The character to store. |
| 58 | 64 | * |
| 59 | | * @return Returns TRUE if the save succeeded. See nx_dump_data() for |
| 60 | | * more details. |
| | 65 | * @sa nx_tracing_add_data |
| 61 | 66 | */ |
| 62 | | bool nx_dump_u8(U8 val); |
| | 67 | void nx_tracing_add_char(const char val); |
| 63 | 68 | |
| 64 | | /** Send the stored data via USB. |
| | 69 | /** Retrieve the trace buffer. |
| | 70 | * |
| | 71 | * @return The address of the start of the trace buffer. |
| 65 | 72 | */ |
| 66 | | void nx_dump_send_usb(void); |
| | 73 | U8 *nx_tracing_get_start(void); |
| | 74 | |
| | 75 | /** Get the size of the trace. |
| | 76 | * |
| | 77 | * @return The size of the recorded trace. |
| | 78 | */ |
| | 79 | U32 nx_tracing_get_size(void); |
| 67 | 80 | |
| 68 | 81 | /*@}*/ |
| 69 | 82 | /*@}*/ |
| 70 | 83 | |
| 71 | | #endif /* __NXOS_DUMP_H__ */ |
| | 84 | #endif /* __NXOS_BASE_LIB_TRACING_TRACING_H__ */ |