Skip to content

Commit 3a42bf8

Browse files
authored
Furi, USB, BLE, Debug: various bug fixes and improvements (#4114)
* Furi, USB, BLE: extra stack space for some threads, small code cleanup. * Furi: thread watermark check on exit, explicitly crash if built with LIB_DEBUG=1 * Debug: color logging in apps/furi gdb helper, check and show crash message in gdb console.
1 parent 4895ae5 commit 3a42bf8

File tree

5 files changed

+87
-23
lines changed

5 files changed

+87
-23
lines changed

applications/main/gpio/usb_uart_bridge.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ static int32_t usb_uart_worker(void* context) {
183183
usb_uart->usb_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
184184

185185
usb_uart->tx_thread =
186-
furi_thread_alloc_ex("UsbUartTxWorker", 512, usb_uart_tx_thread, usb_uart);
186+
furi_thread_alloc_ex("UsbUartTxWorker", 768, usb_uart_tx_thread, usb_uart);
187187

188188
usb_uart_vcp_init(usb_uart, usb_uart->cfg.vcp_ch);
189189
usb_uart_serial_init(usb_uart, usb_uart->cfg.uart_ch);
@@ -288,8 +288,6 @@ static int32_t usb_uart_worker(void* context) {
288288
usb_uart_update_ctrl_lines(usb_uart);
289289
}
290290
}
291-
usb_uart_vcp_deinit(usb_uart, usb_uart->cfg.vcp_ch);
292-
usb_uart_serial_deinit(usb_uart);
293291

294292
furi_hal_gpio_init(USB_USART_DE_RE_PIN, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
295293

@@ -302,6 +300,9 @@ static int32_t usb_uart_worker(void* context) {
302300
furi_thread_join(usb_uart->tx_thread);
303301
furi_thread_free(usb_uart->tx_thread);
304302

303+
usb_uart_vcp_deinit(usb_uart, usb_uart->cfg.vcp_ch);
304+
usb_uart_serial_deinit(usb_uart);
305+
305306
furi_stream_buffer_free(usb_uart->rx_stream);
306307
furi_mutex_free(usb_uart->usb_mutex);
307308
furi_semaphore_free(usb_uart->tx_sem);

furi/core/thread.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
#define THREAD_MAX_STACK_SIZE (UINT16_MAX * sizeof(StackType_t))
2525

26+
#define THREAD_STACK_WATERMARK_MIN (256u)
27+
2628
typedef struct {
2729
FuriThreadStdoutWriteCallback write_callback;
2830
FuriString* buffer;
@@ -115,6 +117,18 @@ static void furi_thread_body(void* context) {
115117

116118
furi_check(!thread->is_service, "Service threads MUST NOT return");
117119

120+
size_t stack_watermark = furi_thread_get_stack_space(thread);
121+
if(stack_watermark < THREAD_STACK_WATERMARK_MIN) {
122+
#ifdef FURI_DEBUG
123+
furi_crash("Stack watermark is dangerously low");
124+
#endif
125+
FURI_LOG_E( //-V779
126+
thread->name ? thread->name : "Thread",
127+
"Stack watermark is too low %zu < " STRINGIFY(
128+
THREAD_STACK_WATERMARK_MIN) ". Increase stack size.",
129+
stack_watermark);
130+
}
131+
118132
if(thread->heap_trace_enabled == true) {
119133
furi_delay_ms(33);
120134
thread->heap_size = memmgr_heap_get_thread_memory((FuriThreadId)thread);

scripts/debug/flipperapps.py

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,33 @@
77
import gdb
88

99

10+
class bcolors:
11+
HEADER = "\033[95m"
12+
OKBLUE = "\033[94m"
13+
OKCYAN = "\033[96m"
14+
OKGREEN = "\033[92m"
15+
WARNING = "\033[93m"
16+
FAIL = "\033[91m"
17+
ENDC = "\033[0m"
18+
BOLD = "\033[1m"
19+
UNDERLINE = "\033[4m"
20+
21+
22+
LOG_PREFIX = "[FURI]"
23+
24+
25+
def error(line):
26+
print(f"{bcolors.FAIL}{LOG_PREFIX} {line}{bcolors.ENDC}")
27+
28+
29+
def warning(line):
30+
print(f"{bcolors.WARNING}{LOG_PREFIX} {line}{bcolors.ENDC}")
31+
32+
33+
def info(line):
34+
print(f"{bcolors.OKGREEN}{LOG_PREFIX} {line}{bcolors.ENDC}")
35+
36+
1037
def get_file_crc32(filename):
1138
with open(filename, "rb") as f:
1239
return zlib.crc32(f.read())
@@ -39,20 +66,20 @@ def get_original_elf_path(self) -> str:
3966
def is_debug_available(self) -> bool:
4067
have_debug_info = bool(self.debug_link_elf and self.debug_link_crc)
4168
if not have_debug_info:
42-
print("No debug info available for this app")
69+
warning("No debug info available for this app")
4370
return False
4471
debug_elf_path = self.get_original_elf_path()
4572
debug_elf_crc32 = get_file_crc32(debug_elf_path)
4673
if self.debug_link_crc != debug_elf_crc32:
47-
print(
74+
warning(
4875
f"Debug info ({debug_elf_path}) CRC mismatch: {self.debug_link_crc:08x} != {debug_elf_crc32:08x}, rebuild app"
4976
)
5077
return False
5178
return True
5279

5380
def get_gdb_load_command(self) -> str:
5481
load_path = self.get_original_elf_path()
55-
print(f"Loading debug information from {load_path}")
82+
info(f"Loading debug information from {load_path}")
5683
load_command = (
5784
f"add-symbol-file -readnow {load_path} 0x{self.text_address:08x} "
5885
)
@@ -121,12 +148,12 @@ def invoke(self, arg, from_tty):
121148
AppState.DEBUG_ELF_ROOT = arg
122149
try:
123150
global helper
124-
print(f"Set '{arg}' as debug info lookup path for Flipper external apps")
151+
info(f"Set '{arg}' as debug info lookup path for Flipper external apps")
125152
helper.attach_to_fw()
126153
gdb.events.stop.connect(helper.handle_stop)
127154
gdb.events.gdb_exiting.connect(helper.handle_exit)
128155
except gdb.error as e:
129-
print(f"Support for Flipper external apps debug is not available: {e}")
156+
error(f"Support for Flipper external apps debug is not available: {e}")
130157

131158

132159
class FlipperAppStateHelper:
@@ -148,13 +175,29 @@ def _exec_gdb_command(self, command: str) -> bool:
148175
gdb.execute(command)
149176
return True
150177
except gdb.error as e:
151-
print(f"Failed to execute GDB command '{command}': {e}")
178+
error(f"Failed to execute GDB command '{command}': {e}")
152179
return False
153180

181+
def _get_crash_message(self):
182+
message = self.app_check_message.value()
183+
if message == 1:
184+
return "furi_assert failed"
185+
elif message == 2:
186+
return "furi_check failed"
187+
else:
188+
return message
189+
154190
def _sync_apps(self) -> None:
191+
crash_message = self._get_crash_message()
192+
if crash_message:
193+
crash_message = f"! System crashed: {crash_message} !"
194+
error("!" * len(crash_message))
195+
error(crash_message)
196+
error("!" * len(crash_message))
197+
155198
self.set_debug_mode(True)
156199
if not (app_list := self.app_list_ptr.value()):
157-
print("Reset app loader state")
200+
info("Reset app loader state")
158201
for app in self._current_apps:
159202
self._exec_gdb_command(app.get_gdb_unload_command())
160203
self._current_apps = []
@@ -167,22 +210,23 @@ def _sync_apps(self) -> None:
167210

168211
for app in self._current_apps.copy():
169212
if app.entry_address not in loaded_apps:
170-
print(f"Application {app.name} is no longer loaded")
213+
warning(f"Application {app.name} is no longer loaded")
171214
if not self._exec_gdb_command(app.get_gdb_unload_command()):
172-
print(f"Failed to unload debug info for {app.name}")
215+
error(f"Failed to unload debug info for {app.name}")
173216
self._current_apps.remove(app)
174217

175218
for entry_point, app in loaded_apps.items():
176219
if entry_point not in set(app.entry_address for app in self._current_apps):
177220
new_app_state = AppState.from_gdb(app)
178-
print(f"New application loaded. Adding debug info")
221+
warning(f"New application loaded. Adding debug info")
179222
if self._exec_gdb_command(new_app_state.get_gdb_load_command()):
180223
self._current_apps.append(new_app_state)
181224
else:
182-
print(f"Failed to load debug info for {new_app_state}")
225+
error(f"Failed to load debug info for {new_app_state}")
183226

184227
def attach_to_fw(self) -> None:
185-
print("Attaching to Flipper firmware")
228+
info("Attaching to Flipper firmware")
229+
self.app_check_message = gdb.lookup_global_symbol("__furi_check_message")
186230
self.app_list_ptr = gdb.lookup_global_symbol(
187231
"flipper_application_loaded_app_list"
188232
)
@@ -200,10 +244,10 @@ def set_debug_mode(self, mode: bool) -> None:
200244
try:
201245
gdb.execute(f"set variable furi_hal_debug_gdb_session_active = {int(mode)}")
202246
except gdb.error as e:
203-
print(f"Failed to set debug mode: {e}")
247+
error(f"Failed to set debug mode: {e}")
204248

205249

206250
# Init additional 'fap-set-debug-elf-root' command and set up hooks
207251
SetFapDebugElfRoot()
208252
helper = FlipperAppStateHelper()
209-
print("Support for Flipper external apps debug is loaded")
253+
info("Support for Flipper external apps debug is loaded")

targets/f7/ble_glue/ble_event_thread.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ void ble_event_thread_stop(void) {
9090
void ble_event_thread_start(void) {
9191
furi_check(event_thread == NULL);
9292

93-
event_thread = furi_thread_alloc_ex("BleEventWorker", 1024, ble_event_thread, NULL);
93+
event_thread = furi_thread_alloc_ex("BleEventWorker", 1280, ble_event_thread, NULL);
9494
furi_thread_set_priority(event_thread, FuriThreadPriorityHigh);
9595
furi_thread_start(event_thread);
9696
}

targets/f7/furi_hal/furi_hal_usb_cdc.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,11 @@ static void cdc_on_suspend(usbd_device* dev);
392392

393393
static usbd_respond cdc_ep_config(usbd_device* dev, uint8_t cfg);
394394
static usbd_respond cdc_control(usbd_device* dev, usbd_ctlreq* req, usbd_rqc_callback* callback);
395+
395396
static usbd_device* usb_dev;
396-
static FuriHalUsbInterface* cdc_if_cur = NULL;
397-
static bool connected = false;
398-
static CdcCallbacks* callbacks[IF_NUM_MAX] = {NULL};
397+
static volatile FuriHalUsbInterface* cdc_if_cur = NULL;
398+
static volatile bool connected = false;
399+
static volatile CdcCallbacks* callbacks[IF_NUM_MAX] = {NULL};
399400
static void* cb_ctx[IF_NUM_MAX];
400401

401402
FuriHalUsbInterface usb_cdc_single = {
@@ -506,17 +507,21 @@ uint8_t furi_hal_cdc_get_ctrl_line_state(uint8_t if_num) {
506507
void furi_hal_cdc_send(uint8_t if_num, uint8_t* buf, uint16_t len) {
507508
if(if_num == 0) {
508509
usbd_ep_write(usb_dev, CDC0_TXD_EP, buf, len);
509-
} else {
510+
} else if(if_num == 1) {
510511
usbd_ep_write(usb_dev, CDC1_TXD_EP, buf, len);
512+
} else {
513+
furi_crash();
511514
}
512515
}
513516

514517
int32_t furi_hal_cdc_receive(uint8_t if_num, uint8_t* buf, uint16_t max_len) {
515518
int32_t len = 0;
516519
if(if_num == 0) {
517520
len = usbd_ep_read(usb_dev, CDC0_RXD_EP, buf, max_len);
518-
} else {
521+
} else if(if_num == 1) {
519522
len = usbd_ep_read(usb_dev, CDC1_RXD_EP, buf, max_len);
523+
} else {
524+
furi_crash();
520525
}
521526
return (len < 0) ? 0 : len;
522527
}

0 commit comments

Comments
 (0)