Skip to content

Commit bdf4979

Browse files
committed
pbdrv/uart_debug_first_port: Log on panic.
It's helpful when you crash to see what happens right before you crash. For example, if you add a log message "about to dereference sketchy pointer" and then you get a segfault, you'd love to know whether that message occurred. This change makes it so that at least on EV3, we will. This also adds "pb_log" as an alias for pbdrv_uart_debug_printf. Usually, platforms make it easy to log debug messages, and we should be no different. C.f. `ESP_LOGI`, Zephyr's `LOG_ERR`, glog's `LOG(WARNING)` etc. Tested by adding the following code to pbdrv_init. ``` pb_log("test\n"); lwrb_t* rb = (lwrb_t*)0x1281441490; pb_log("%p\n", lwrb_get_free(rb)); ``` Before this, the "test" would not appear before the panic. After, it does. If a panic occurs while some messages are pending in the uart send state, some bytes may be duplicated on the log.
1 parent 350db6e commit bdf4979

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

lib/pbio/drv/uart/uart_debug_first_port.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ static lwrb_t ring_buffer;
2222

2323
static pbdrv_uart_dev_t *debug_uart = NULL;
2424

25+
lwrb_t* pbdrv_uart_debug_get_ring_buffer(void) {
26+
if (!lwrb_is_ready(&ring_buffer)) {
27+
return NULL;
28+
}
29+
return &ring_buffer;
30+
}
31+
2532
/**
2633
* Formats and stores a string in the UART debug ring buffer.
2734
*

lib/pbio/drv/uart/uart_debug_first_port.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <stdbool.h>
99
#include <stdarg.h>
1010

11+
#include <lwrb/lwrb.h>
12+
1113
#include <pbdrv/config.h>
1214

1315
#if PBDRV_CONFIG_UART_DEBUG_FIRST_PORT
@@ -19,6 +21,8 @@ bool pbdrv_uart_debug_is_done(void);
1921

2022
void pbdrv_uart_debug_init(void);
2123

24+
lwrb_t* pbdrv_uart_debug_get_ring_buffer(void);
25+
2226
#else // PBDRV_CONFIG_UART_DEBUG_FIRST_PORT
2327

2428
#define pbdrv_uart_debug_printf(...)
@@ -28,6 +32,13 @@ void pbdrv_uart_debug_init(void);
2832

2933
#define pbdrv_uart_debug_init()
3034

35+
static inline lwrb_t* pbdrv_uart_debug_get_ring_buffer(void) {
36+
return NULL;
37+
}
38+
3139
#endif // PBDRV_CONFIG_UART_DEBUG_FIRST_PORT
3240

41+
// Convenient shorthand for pbdrv_uart_debug_printf.
42+
#define pb_log(...) pbdrv_uart_debug_printf(__VA_ARGS__)
43+
3344
#endif // _INTERNAL_PBDRV_UART_DEBUG_FIRST_PORT_H_

lib/pbio/platform/ev3/platform.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
#include "../../drv/pwm/pwm_ev3.h"
8282
#include "../../drv/reset/reset_ev3.h"
8383
#include "../../drv/uart/uart_ev3.h"
84+
#include "../../drv/uart/uart_debug_first_port.h"
8485

8586
enum {
8687
LED_DEV_0_STATUS,
@@ -443,6 +444,15 @@ void ev3_panic_handler(int except_type, ev3_panic_ctx *except_data) {
443444
UARTConfigSetExpClk(SOC_UART_1_REGS, SOC_UART_1_MODULE_FREQ, 115200, UART_WORDL_8BITS, UART_OVER_SAMP_RATE_13);
444445
UARTFIFOEnable(SOC_UART_1_REGS);
445446

447+
// Drain whatever is still left in the UART debug FIFO.
448+
lwrb_t* rb = pbdrv_uart_debug_get_ring_buffer();
449+
if (rb != NULL) {
450+
uint8_t byte;
451+
while (lwrb_read(rb, &byte, 1) == 1) {
452+
UARTCharPut(SOC_UART_1_REGS, byte);
453+
}
454+
}
455+
446456
panic_puts("********************************************************************************\r\n");
447457
panic_puts("* Pybricks on EV3 Panic *\r\n");
448458
panic_puts("********************************************************************************\r\n");

0 commit comments

Comments
 (0)