2023-03-13 10:03:40 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
* Copyright (C) 2023 Bernd Herzog
|
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "debug.hpp"
|
|
|
|
|
|
|
|
#include <ch.h>
|
|
|
|
#include <hal.h>
|
|
|
|
#include <string>
|
|
|
|
|
2023-06-13 02:14:26 -04:00
|
|
|
#include "log_file.hpp"
|
2023-03-13 10:03:40 -04:00
|
|
|
#include "portapack.hpp"
|
2023-03-18 17:41:26 -04:00
|
|
|
#include "string_format.hpp"
|
2023-06-07 11:33:32 -04:00
|
|
|
#include "ui_styles.hpp"
|
2023-08-27 01:33:27 -04:00
|
|
|
#include "irq_controls.hpp"
|
2023-06-07 11:33:32 -04:00
|
|
|
|
|
|
|
using namespace ui;
|
2023-03-13 10:03:40 -04:00
|
|
|
|
2023-06-13 02:14:26 -04:00
|
|
|
#define DEBUG_LOG_FILE "debug_log.txt"
|
|
|
|
LogFile* pg_debug_log = nullptr;
|
|
|
|
void __debug_log(const std::string& msg) {
|
|
|
|
static LogFile s_log;
|
|
|
|
if (pg_debug_log == nullptr) {
|
|
|
|
delete_file(DEBUG_LOG_FILE);
|
|
|
|
s_log.append(DEBUG_LOG_FILE);
|
|
|
|
pg_debug_log = &s_log;
|
|
|
|
}
|
|
|
|
|
|
|
|
pg_debug_log->write_entry(msg);
|
|
|
|
}
|
|
|
|
|
2023-08-27 01:33:27 -04:00
|
|
|
void runtime_error(uint8_t source);
|
2023-03-13 10:03:40 -04:00
|
|
|
std::string number_to_hex_string(uint32_t);
|
2023-05-18 16:16:05 -04:00
|
|
|
void draw_line(int32_t, const char*, regarm_t);
|
2023-08-30 00:26:58 -04:00
|
|
|
void draw_stack_dump();
|
2023-03-13 10:03:40 -04:00
|
|
|
static bool error_shown = false;
|
2023-08-30 00:26:58 -04:00
|
|
|
uint32_t fault_address{0};
|
2023-03-13 10:03:40 -04:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
void draw_guru_meditation_header(uint8_t source, const char* hint) {
|
2023-06-07 11:33:32 -04:00
|
|
|
Painter painter;
|
2023-03-13 10:03:40 -04:00
|
|
|
|
|
|
|
painter.fill_rectangle(
|
2023-05-18 16:16:05 -04:00
|
|
|
{0, 0, portapack::display.width(), portapack::display.height()},
|
2023-06-07 11:33:32 -04:00
|
|
|
Color::red());
|
2023-03-13 10:03:40 -04:00
|
|
|
|
|
|
|
constexpr int border = 8;
|
|
|
|
painter.fill_rectangle(
|
2023-05-18 16:16:05 -04:00
|
|
|
{border, border, portapack::display.width() - (border * 2), portapack::display.height() - (border * 2)},
|
2023-06-07 11:33:32 -04:00
|
|
|
Color::black());
|
2023-03-13 10:03:40 -04:00
|
|
|
|
|
|
|
// NOTE: in situations like a hard fault it seems not possible to write strings longer than 16 characters.
|
2023-06-07 11:33:32 -04:00
|
|
|
painter.draw_string({48, 24}, Styles::white, "M? Guru");
|
|
|
|
painter.draw_string({48 + 8 * 8, 24}, Styles::white, "Meditation");
|
2023-03-13 10:03:40 -04:00
|
|
|
|
2023-08-27 01:33:27 -04:00
|
|
|
if (source == CORTEX_M0) {
|
2023-06-07 11:33:32 -04:00
|
|
|
painter.draw_string({48 + 8, 24}, Styles::white, "0");
|
2023-08-30 00:26:58 -04:00
|
|
|
painter.draw_string({24, 320 - 32}, Styles::white, "Press DFU for Stack Dump");
|
2023-08-27 01:33:27 -04:00
|
|
|
}
|
2023-05-18 16:16:05 -04:00
|
|
|
|
2023-03-13 10:03:40 -04:00
|
|
|
if (source == CORTEX_M4)
|
2023-06-07 11:33:32 -04:00
|
|
|
painter.draw_string({48 + 8, 24}, Styles::white, "4");
|
2023-05-18 16:16:05 -04:00
|
|
|
|
2023-06-07 11:33:32 -04:00
|
|
|
painter.draw_string({15, 55}, Styles::white, "Hint: ");
|
|
|
|
painter.draw_string({15 + 8 * 8, 55}, Styles::white, hint);
|
2023-03-13 10:03:40 -04:00
|
|
|
}
|
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
void draw_guru_meditation(uint8_t source, const char* hint) {
|
|
|
|
if (error_shown == false) {
|
2023-03-13 10:03:40 -04:00
|
|
|
error_shown = true;
|
|
|
|
draw_guru_meditation_header(source, hint);
|
|
|
|
}
|
|
|
|
|
2023-08-27 01:33:27 -04:00
|
|
|
runtime_error(source);
|
2023-03-13 10:03:40 -04:00
|
|
|
}
|
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
void draw_guru_meditation(uint8_t source, const char* hint, struct extctx* ctxp, uint32_t cfsr = 0) {
|
|
|
|
if (error_shown == false) {
|
2023-03-13 10:03:40 -04:00
|
|
|
error_shown = true;
|
|
|
|
draw_guru_meditation_header(source, hint);
|
|
|
|
|
|
|
|
if (ctxp != nullptr) {
|
|
|
|
int i = 0;
|
|
|
|
draw_line(80 + i++ * 20, "r0:", ctxp->r0);
|
|
|
|
draw_line(80 + i++ * 20, "r1:", ctxp->r1);
|
|
|
|
draw_line(80 + i++ * 20, "r2:", ctxp->r2);
|
|
|
|
draw_line(80 + i++ * 20, "r3:", ctxp->r3);
|
|
|
|
draw_line(80 + i++ * 20, "r12:", ctxp->r12);
|
|
|
|
|
|
|
|
// NOTE: run for M0
|
|
|
|
// # arm-none-eabi-gdb --batch --eval-command="x/8i 0x<link_register_value_here>" firmware/application/application.elf
|
|
|
|
// or for M4
|
|
|
|
// # arm-none-eabi-gdb --batch --eval-command="x/8i 0x<link_register_value_here>" firmware/baseband/<image_name_here>.elf
|
|
|
|
// to see whats causing the fault.
|
|
|
|
draw_line(80 + i++ * 20, "lr:", ctxp->lr_thd);
|
|
|
|
draw_line(80 + i++ * 20, "pc:", ctxp->pc);
|
2023-08-30 00:26:58 -04:00
|
|
|
fault_address = (uint32_t)ctxp->pc;
|
2023-03-18 17:41:26 -04:00
|
|
|
|
|
|
|
// see SCB_CFSR_* in libopencm3/cm3/scb.h for details
|
|
|
|
if (cfsr != 0)
|
2023-05-18 16:16:05 -04:00
|
|
|
draw_line(80 + i++ * 20, "cfsr:", (void*)cfsr);
|
2023-03-13 10:03:40 -04:00
|
|
|
}
|
|
|
|
}
|
2023-05-18 16:16:05 -04:00
|
|
|
|
2023-08-27 01:33:27 -04:00
|
|
|
runtime_error(source);
|
2023-03-13 10:03:40 -04:00
|
|
|
}
|
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
void draw_line(int32_t y_offset, const char* label, regarm_t value) {
|
2023-06-07 11:33:32 -04:00
|
|
|
Painter painter;
|
|
|
|
|
|
|
|
painter.draw_string({15, y_offset}, Styles::white, label);
|
|
|
|
painter.draw_string({15 + 8 * 8, y_offset}, Styles::white, to_string_hex((uint32_t)value, 8));
|
2023-03-13 10:03:40 -04:00
|
|
|
}
|
|
|
|
|
2023-08-27 01:33:27 -04:00
|
|
|
void runtime_error(uint8_t source) {
|
|
|
|
LED led = (source == CORTEX_M0) ? hackrf::one::led_rx : hackrf::one::led_tx;
|
|
|
|
|
2023-03-13 10:03:40 -04:00
|
|
|
led.off();
|
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
while (true) {
|
2023-03-13 10:03:40 -04:00
|
|
|
volatile size_t n = 1000000U;
|
2023-05-18 16:16:05 -04:00
|
|
|
while (n--)
|
|
|
|
;
|
2023-03-13 10:03:40 -04:00
|
|
|
led.toggle();
|
2023-08-27 01:33:27 -04:00
|
|
|
|
2023-08-30 00:26:58 -04:00
|
|
|
// Stack dump will cover entire screen, so wait for DFU button press to attempt it
|
|
|
|
if (swizzled_switches() & (1 << (int)Switch::Dfu)) {
|
|
|
|
draw_stack_dump();
|
2023-08-27 01:33:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-30 00:26:58 -04:00
|
|
|
// This function should only be called with interrupts disabled due to reading swizzled_switches()
|
|
|
|
// Using the stack while dumping the stack isn't ideal, but hopefully anything imporant is still on the call stack.
|
|
|
|
void draw_stack_dump() {
|
|
|
|
Painter painter;
|
|
|
|
uint32_t* p{&__process_stack_base__};
|
|
|
|
constexpr int border{8};
|
|
|
|
int x, y;
|
|
|
|
int n{0};
|
|
|
|
bool clear_rect{true};
|
|
|
|
bool first_pass{true};
|
|
|
|
|
|
|
|
// NOTE: Stays in this loop forever unless LEFT button pressed
|
|
|
|
while (1) {
|
|
|
|
if (clear_rect) {
|
|
|
|
painter.fill_rectangle(
|
|
|
|
{border, border, portapack::display.width() - (border * 2), portapack::display.height() - (border * 2)},
|
|
|
|
Color::black());
|
|
|
|
x = y = border;
|
|
|
|
clear_rect = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip past unused stack words on 1st pass
|
|
|
|
if (first_pass) {
|
|
|
|
first_pass = false;
|
|
|
|
while ((*p == CRT0_STACKS_FILL_PATTERN) && (p < &__process_stack_end__))
|
|
|
|
p++;
|
|
|
|
|
|
|
|
auto stack_space_left = p - &__process_stack_base__;
|
|
|
|
|
|
|
|
// NOTE: in situations like a hard fault it seems not possible to write strings longer than 16 characters.
|
|
|
|
painter.draw_string({x, y}, Styles::white_small, to_string_hex((uint32_t)&__process_stack_base__, 8));
|
|
|
|
x += 8 * 5;
|
|
|
|
painter.draw_string({x, y}, Styles::white_small, " M0 STACK free=");
|
|
|
|
x += 15 * 5;
|
|
|
|
painter.draw_string({x, y}, Styles::white_small, to_string_dec_uint(stack_space_left));
|
|
|
|
x = border;
|
|
|
|
y += 8;
|
|
|
|
|
|
|
|
// align subsequent lines to start on 16-byte boundaries
|
|
|
|
p -= (stack_space_left & 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
// print one page of stack dump
|
|
|
|
while ((p < &__process_stack_end__) && (y < portapack::display.height() - border - 8)) {
|
|
|
|
// show address if start of line
|
|
|
|
if (n++ == 0) {
|
|
|
|
painter.draw_string({x, y}, Styles::white_small, to_string_hex((uint32_t)p, 8) + ":");
|
|
|
|
x += 9 * 5 - 2; // note: saving 2 pixels here to prevent reaching right border
|
|
|
|
}
|
|
|
|
|
|
|
|
// show stack word -- highlight if a possible code address (low bit will be set too for !thumb) or actual fault address
|
|
|
|
bool code_addr = (*p > 0x1400) && (*p < 0x80000) && (((*p) & 0x1) == 0x1); // approximate address range of code .text region in ROM
|
|
|
|
Style style = (fault_address && *p == fault_address) ? Styles::bg_yellow_small : (code_addr ? Styles::bg_white_small : Styles::white_small);
|
|
|
|
painter.draw_string({x, y}, style, " " + to_string_hex(*p, 8));
|
|
|
|
x += 9 * 5;
|
|
|
|
|
|
|
|
// new line?
|
|
|
|
if (n == 4) {
|
|
|
|
n = 0;
|
|
|
|
x = border;
|
|
|
|
y += 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
// point to next stack word
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Out of room on the screen or end of stack - allow Up/Down paging.
|
|
|
|
// First wait for button release from previous press.
|
|
|
|
// NOTE: can't call swizzle_switches() with interrupted enabled!
|
|
|
|
while (swizzled_switches() & ((1 << (int)Switch::Right) | (1 << (int)Switch::Left) | (1 << (int)Switch::Down) | (1 << (int)Switch::Up) | (1 << (int)Switch::Sel) | (1 << (int)Switch::Dfu)))
|
|
|
|
;
|
|
|
|
|
|
|
|
painter.draw_string({border, portapack::display.height() - border - 8}, Styles::white_small, "Use UP/DOWN key");
|
|
|
|
|
|
|
|
// Wait for button press.
|
|
|
|
while (1) {
|
|
|
|
auto switches = swizzled_switches();
|
|
|
|
|
|
|
|
// If LEFT button pressed, exit
|
|
|
|
if (switches & (1 << (int)Switch::Left))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If DOWN pressed; continue to show next page
|
|
|
|
if (switches & (1 << (int)Switch::Down))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// If UP pressed, scroll back by one page
|
|
|
|
if (switches & (1 << (int)Switch::Up)) {
|
|
|
|
// Back up pointer by offset on this screen plus one full screen of 8 pixels per line & 4 words per line
|
|
|
|
// (underflow can't happen here due to stack address range in RAM)
|
|
|
|
p -= ((y - border) + (portapack::display.height() - (border * 2 + 1 * 8))) / (8 / 4);
|
|
|
|
if (p < &__process_stack_base__)
|
|
|
|
p = &__process_stack_base__;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If SEL button pressed, try writing to file (likely to hang in fault condition but heh)
|
|
|
|
if (switches & (1 << (int)Switch::Sel))
|
|
|
|
stack_dump();
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear screen to allow new range to be displayed, if we're not at the end
|
|
|
|
if (p < &__process_stack_end__)
|
|
|
|
clear_rect = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disk I/O in this function doesn't work after a fault
|
2023-08-27 01:33:27 -04:00
|
|
|
// Using the stack while dumping the stack isn't ideal, but hopefully anything imporant is still on the call stack.
|
|
|
|
bool stack_dump() {
|
2023-08-30 00:26:58 -04:00
|
|
|
Painter painter;
|
2023-08-27 01:33:27 -04:00
|
|
|
std::string debug_dir = "DEBUG";
|
|
|
|
std::filesystem::path filename{};
|
|
|
|
File stack_dump_file{};
|
|
|
|
bool error;
|
|
|
|
std::string str;
|
|
|
|
uint32_t* p;
|
2023-08-30 00:26:58 -04:00
|
|
|
int n{0};
|
|
|
|
bool data_found{false};
|
2023-08-27 01:33:27 -04:00
|
|
|
|
|
|
|
make_new_directory(debug_dir);
|
|
|
|
filename = next_filename_matching_pattern(debug_dir + "/STACK_DUMP_????.TXT");
|
|
|
|
error = filename.empty();
|
|
|
|
if (!error)
|
|
|
|
error = stack_dump_file.create(filename) != 0;
|
|
|
|
if (error) {
|
|
|
|
painter.draw_string({16, 320 - 32}, ui::Styles::red, "ERROR DUMPING " + filename.filename().string() + "!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-30 00:26:58 -04:00
|
|
|
for (p = &__process_stack_base__; p < &__process_stack_end__; p++) {
|
|
|
|
// skip past unused stack words
|
2023-08-27 01:33:27 -04:00
|
|
|
if (!data_found) {
|
|
|
|
if (*p == CRT0_STACKS_FILL_PATTERN)
|
|
|
|
continue;
|
|
|
|
else {
|
|
|
|
data_found = true;
|
|
|
|
auto stack_space_left = p - &__process_stack_base__;
|
2023-08-30 00:26:58 -04:00
|
|
|
stack_dump_file.write_line(to_string_hex((uint32_t)&__process_stack_base__, 8) + ": Unused words " + to_string_dec_uint(stack_space_left));
|
2023-08-27 01:33:27 -04:00
|
|
|
|
|
|
|
// align subsequent lines to start on 16-byte boundaries
|
|
|
|
p -= (stack_space_left & 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-30 00:26:58 -04:00
|
|
|
// write address
|
2023-08-27 01:33:27 -04:00
|
|
|
if (n++ == 0) {
|
|
|
|
str = to_string_hex((uint32_t)p, 8) + ":";
|
|
|
|
stack_dump_file.write(str.data(), 9);
|
|
|
|
}
|
|
|
|
|
2023-08-30 00:26:58 -04:00
|
|
|
// write stack dword
|
2023-08-27 01:33:27 -04:00
|
|
|
str = " " + to_string_hex(*p, 8);
|
|
|
|
stack_dump_file.write(str.data(), 9);
|
|
|
|
|
|
|
|
if (n == 4) {
|
|
|
|
stack_dump_file.write("\r\n", 2);
|
|
|
|
n = 0;
|
|
|
|
}
|
2023-03-13 10:03:40 -04:00
|
|
|
}
|
2023-08-27 01:33:27 -04:00
|
|
|
|
|
|
|
painter.draw_string({16, 320 - 32}, ui::Styles::green, filename.filename().string() + " dumped!");
|
|
|
|
return true;
|
2023-03-13 10:03:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#if CH_DBG_ENABLED
|
|
|
|
void port_halt(void) {
|
|
|
|
port_disable();
|
|
|
|
|
|
|
|
if (dbg_panic_msg == nullptr)
|
|
|
|
dbg_panic_msg = "system halted";
|
|
|
|
|
|
|
|
draw_guru_meditation(CORTEX_M0, dbg_panic_msg);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
CH_IRQ_HANDLER(MemManageVector) {
|
|
|
|
#if CH_DBG_ENABLED
|
|
|
|
chDbgPanic("MemManage");
|
|
|
|
#else
|
|
|
|
chSysHalt();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
CH_IRQ_HANDLER(BusFaultVector) {
|
|
|
|
#if CH_DBG_ENABLED
|
|
|
|
chDbgPanic("BusFault");
|
|
|
|
#else
|
|
|
|
chSysHalt();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
CH_IRQ_HANDLER(UsageFaultVector) {
|
|
|
|
#if CH_DBG_ENABLED
|
|
|
|
chDbgPanic("UsageFault");
|
|
|
|
#else
|
|
|
|
chSysHalt();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
CH_IRQ_HANDLER(HardFaultVector) {
|
|
|
|
#if CH_DBG_ENABLED
|
|
|
|
CH_IRQ_PROLOGUE();
|
2023-05-18 16:16:05 -04:00
|
|
|
struct extctx* ctxp;
|
2023-03-13 10:03:40 -04:00
|
|
|
port_lock_from_isr();
|
|
|
|
|
|
|
|
if ((uint32_t)_saved_lr & 0x04)
|
2023-05-18 16:16:05 -04:00
|
|
|
ctxp = (struct extctx*)__get_PSP();
|
2023-03-13 10:03:40 -04:00
|
|
|
else
|
2023-05-18 16:16:05 -04:00
|
|
|
ctxp = (struct extctx*)__get_MSP();
|
2023-03-13 10:03:40 -04:00
|
|
|
|
|
|
|
port_disable();
|
2023-03-18 17:41:26 -04:00
|
|
|
|
|
|
|
auto stack_space_left = get_free_stack_space();
|
|
|
|
if (stack_space_left < 16)
|
|
|
|
draw_guru_meditation(CORTEX_M0, "Stack Overflow", ctxp);
|
|
|
|
|
2023-03-13 10:03:40 -04:00
|
|
|
draw_guru_meditation(CORTEX_M0, "Hard Fault", ctxp);
|
|
|
|
|
|
|
|
CH_IRQ_EPILOGUE();
|
|
|
|
#else
|
|
|
|
chSysHalt();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* extern "C" */
|