Skip to content

Commit

Permalink
GDB and exception handling improvements (#1655)
Browse files Browse the repository at this point in the history
Makefile changes:
* Remove esp-gdbstub as third-party module
* Remove all GDB stuff from `Makefile` - gdbstub is compiled into application, not framework
* gdbstub can be used with release or debug builds.
* Add support in all makefiles for assembly code (both .s and .S files)
* Remove all COM port settings from `Makefile` - application code does all this now
* Bring `Makefile-project.mk`inline with `Makefile-rboot.mk` (compiler warnings, etc.)
* Add new 'gdb' phony target to make it easier to start debugger with correct port, baud rate and other settings

HardwareSerial
* Fix so that end() and systemDebugOutput() don't screw up initialisation of other serial port(s)
* Put callback handling code into static methods - saves IRAM and simplifies code
* Revise interrupt callback to prevent task queue flooding, e.g. on receive overflow
* Add `getStatus()` method to report serial errors

HardwareTimer
* Make HardwareTimer mode selectable (maskable or NMI)

user_main.cpp
* Don't change serial port baud rates - do that in gdb_init() instead which is compiled into application

system/crash_handler.c
* Remove stack dumping code and all serial port access - moved into gdb_hooks.cpp
* Include text for exception code in report

system/SerialBuffer.h
* Move non-trivial code into .cpp module
* Add `getReadData()` and `skipRead()` methods for efficient block reading

esp_systemapi.h
* Remove uart prototypes - driver must be used instead
* Ensure wdt functions are available

uart.h, uart.cpp
* Add UART2 and provide generic notification callback handling
* Add UART_OPT_CALLBACK_RAW option
* Save some IRAM by moving uart_get_uart, uart_set_callback, uart_peek_last_char, uart_rx_available and moving uart_detach into flash
* Use inline functions in preference to macros
* Default debug port should be undefined, not UART0
* Use bit manipulation macros to simplify/clarify code
* Disable interrupts in `uart_tx_free()` and `uart_rx_available()` to ensure result is accurate
* Fix incorrect RX FIFO size reading, causes problems in FIFO overflow situation
* Add `uart_get_status()` to support reporting of errors and Rx BREAK condition
* Add mode parameter to uart_flush() so tx/rx can be flushed independently, e.g. if a receive overrun occurs.
* Add `uart_set_break()` function

gdbstub, exception handling and stack dumping

* appspecific/gdb/ code is always compiled into application, contains system exception handler and stack dumper
* gdb/ code is only compiled when ENABLE_GDB=1
* gdbstub revised for readability and checking
* uart handling code in separate module (gdbuart.cpp)
* Add build option to support both patched and unpatched GDB versions
* Extend GDBSTUB_CTRLC_BREAK to prevent accidental breaking
* Add gdb_enable() function
* Include options for stack dumping, with defaults to provide consistent behaviour (i.e. OFF in release builds, ON in debug builds)
* Add debugging code to stub which can be optionally enabled to see realtime behaviour
* Implement register read/write (p & P) and binary write (X) commands
* Provide API for GDB system functions

LiveDebug sample

* Revise to highlight possible debugging issues with hardware timer, and illustrate other timer types which are 'debug safe'
* Add serial read callback to demonstrate UART2 function
* Implement simple interactive debug console using GDB syscall support
* Add error checking/reporting to serial receive callback

Basic_Serial sample
* Add error checking/reporting to serial receive callback
  • Loading branch information
mikee47 authored and slaff committed Apr 8, 2019
1 parent 5300c0b commit c618f85
Show file tree
Hide file tree
Showing 53 changed files with 6,152 additions and 784 deletions.
4 changes: 0 additions & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
path = docs/wiki
url = https://github.com/SmingHub/Sming.wiki.git
ignore = dirty
[submodule "Sming/third-party/esp-gdbstub"]
path = Sming/third-party/esp-gdbstub
url = https://github.com/espressif/esp-gdbstub.git
ignore = dirty
[submodule "Sming/third-party/pwm"]
path = Sming/third-party/pwm
url = https://github.com/StefanBruens/ESP8266_new_pwm.git
Expand Down
65 changes: 11 additions & 54 deletions Sming/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,6 @@
# MacOS / Linux
# SMING_HOME = /opt/esp-open-sdk

## COM port parameter is reqruied to flash firmware correctly.
## Windows:
# COM_PORT = COM3

# MacOS / Linux:
# COM_PORT = /dev/tty.usbserial

# Com port speed
COM_SPEED = 115200

# Git command
GIT ?= git

Expand Down Expand Up @@ -117,7 +107,7 @@ export COMPILE := gcc
export PATH := $(ESP_HOME)/xtensa-lx106-elf/bin:$(PATH)
XTENSA_TOOLS_ROOT := $(ESP_HOME)/xtensa-lx106-elf/bin

# select which tools to use as compiler, librarian and linker
# select which tools to use as assembler, compiler, librarian and linker
AS := $(XTENSA_TOOLS_ROOT)/xtensa-lx106-elf-gcc
CC := $(XTENSA_TOOLS_ROOT)/xtensa-lx106-elf-gcc
CXX := $(XTENSA_TOOLS_ROOT)/xtensa-lx106-elf-g++
Expand All @@ -126,16 +116,6 @@ LD := $(XTENSA_TOOLS_ROOT)/xtensa-lx106-elf-gcc
OBJCOPY := $(XTENSA_TOOLS_ROOT)/xtensa-lx106-elf-objcopy
OBJDUMP := $(XTENSA_TOOLS_ROOT)/xtensa-lx106-elf-objdump

## COM port parameters
# Default COM port speed (generic)
COM_SPEED ?= 115200

# Default COM port speed (used for flashing)
COM_SPEED_ESPTOOL ?= $(COM_SPEED)

# Default COM port speed (used in code)
COM_SPEED_SERIAL ?= $(COM_SPEED)

### Debug output parameters
# By default `debugf` does not print file name and line number. If you want this enabled set the directive below to 1
DEBUG_PRINT_FILENAME_AND_LINE ?= 0
Expand Down Expand Up @@ -220,15 +200,6 @@ ifneq (,$(findstring third-party/ESP8266_NONOS_SDK, $(SDK_BASE)))
SDK_INTERNAL = 1
endif

# => esp-gdbstub
ifeq ($(ENABLE_GDB), 1)
THIRD_PARTY_DATA += third-party/esp-gdbstub/Makefile
MODULES += third-party/esp-gdbstub
EXTRA_INCDIR += third-party/esp-gdbstub
else ifneq ($(SMING_RELEASE),1)
MODULES += gdb
endif

# => umm_malloc (custom heap allocation)
ifeq ($(ENABLE_CUSTOM_HEAP), 1)
THIRD_PARTY_DATA += third-party/umm_malloc/Makefile
Expand Down Expand Up @@ -284,16 +255,21 @@ MFORCE32 := $(shell $(CC) --help=target | grep mforce-l32)
CFLAGS_COMMON = -Wl,-EL -nostdlib -mlongcalls -mtext-section-literals -finline-functions -fdata-sections -ffunction-sections
# compiler flags using during compilation of source files. Add '-pg' for debugging
CFLAGS += -Wall -Wundef -Wpointer-arith -Wno-comment $(CFLAGS_COMMON) \
-D__ets__ -DICACHE_FLASH -DUSE_OPTIMIZE_PRINTF -DARDUINO=106 -DCOM_SPEED_SERIAL=$(COM_SPEED_SERIAL) -DENABLE_CMD_EXECUTOR=$(ENABLE_CMD_EXECUTOR) -DESP8266=1 -DSMING_INCLUDED=1
-D__ets__ -DICACHE_FLASH -DUSE_OPTIMIZE_PRINTF -DARDUINO=106 -DENABLE_CMD_EXECUTOR=$(ENABLE_CMD_EXECUTOR) -DESP8266=1 -DSMING_INCLUDED=1
ifneq ($(STRICT),1)
CFLAGS += -Werror -Wno-sign-compare -Wno-parentheses -Wno-unused-variable -Wno-unused-but-set-variable -Wno-strict-aliasing -Wno-return-type -Wno-maybe-uninitialized
endif

ifeq ($(ENABLE_GDB), 1)
CFLAGS += -ggdb -DENABLE_GDB=1
endif

ifeq ($(SMING_RELEASE),1)
# See: https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
# for full list of optimization options
CFLAGS += -Os -DSMING_RELEASE=1 -DLWIP_NOASSERT
else ifeq ($(ENABLE_GDB), 1)
CFLAGS += -Og -ggdb -DGDBSTUB_FREERTOS=0 -DENABLE_GDB=1 -DGDBSTUB_CTRLC_BREAK=0
CFLAGS += -Og
else
CFLAGS += -Os -g
endif
Expand All @@ -319,16 +295,8 @@ ifneq ($(STRICT),1)
CXXFLAGS += -Wno-reorder
endif

# linker flags used to generate the main object file
LDFLAGS = -nostdlib -u call_user_start -Wl,-static -Wl,--gc-sections -Wl,-wrap,system_restart_local

# linker script used for the above linkier step
LD_PATH = compiler/ld/
LD_SCRIPT = $(LD_PATH)eagle.app.v6.cpp.ld

# various paths from the SDK used in this project
SDK_LIBDIR = lib
SDK_LDDIR = ld
SDK_INCDIR = include include/json

# SSL support using axTLS
Expand Down Expand Up @@ -359,25 +327,18 @@ SDK_LIBDIR := $(addprefix $(SDK_BASE)/,$(SDK_LIBDIR))
SDK_INCDIR := $(addprefix -I$(SDK_BASE)/,$(SDK_INCDIR))

AS_SRC := $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.s))
ifeq ($(ENABLE_GDB), 1)
AS_SRC += $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.S))
endif
AS_SRC += $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.S))
C_SRC := $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.c))
CXX_SRC := $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.cpp))
AS_OBJ := $(patsubst %.s,$(BUILD_BASE)/%.o,$(AS_SRC))
ifeq ($(ENABLE_GDB), 1)
AS_OBJ += $(patsubst %.S,$(BUILD_BASE)/%.o,$(AS_SRC))
endif
AS_OBJ += $(patsubst %.S,$(BUILD_BASE)/%.o,$(AS_SRC))
C_OBJ := $(patsubst %.c,$(BUILD_BASE)/%.o,$(C_SRC))
CXX_OBJ := $(patsubst %.cpp,$(BUILD_BASE)/%.o,$(CXX_SRC))
OBJ := $(AS_OBJ) $(C_OBJ) $(CXX_OBJ)
LIBS := $(addprefix -l,$(LIBS))
APP_AR := $(addprefix $(BUILD_BASE)/,$(TARGET)_app.a)
TARGET_OUT := $(addprefix $(BUILD_BASE)/,$(TARGET).out)

#LD_SCRIPT := $(addprefix -T$(SDK_BASE)/$(SDK_LDDIR)/,$(LD_SCRIPT))
LD_SCRIPT := $(addprefix -T,$(LD_SCRIPT))

INCDIR := $(addprefix -I,$(SRC_DIR))
EXTRA_INCDIR := $(addprefix -I,$(EXTRA_INCDIR))
MODULE_INCDIR := $(addsuffix /include,$(INCDIR))
Expand All @@ -399,19 +360,15 @@ endif
vpath %.c $(SRC_DIR)
vpath %.cpp $(SRC_DIR)
vpath %.s $(SRC_DIR)
ifeq ($(ENABLE_GDB), 1)
vpath %.S $(SRC_DIR)
endif
vpath %.S $(SRC_DIR)

define compile-objects
$1/%.o: %.s
$(vecho) "AS $$<"
$(Q) $(AS) $(INCDIR) $(MODULE_INCDIR) $(EXTRA_INCDIR) $(SDK_INCDIR) $(CFLAGS) -c $$< -o $$@
ifeq ($(ENABLE_GDB), 1)
$1/%.o: %.S
$(vecho) "AS $$<"
$(Q) $(AS) $(INCDIR) $(MODULE_INCDIR) $(EXTRA_INCDIR) $(SDK_INCDIR) $(CFLAGS) -c $$< -o $$@
endif
$1/%.o: %.c $1/%.c.d
$(vecho) "CC $$<"
$(Q) $(CC) $(INCDIR) $(MODULE_INCDIR) $(EXTRA_INCDIR) $(SDK_INCDIR) $(CFLAGS) -c $$< -o $$@
Expand Down
34 changes: 28 additions & 6 deletions Sming/Makefile-project.mk
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ endif
# define your custom directories in the project's own Makefile before including this one
MODULES ?= app # default to app if not set by user
EXTRA_INCDIR ?= include # default to include if not set by user
MODULES += $(SMING_HOME)/appspecific/gdb

ENABLE_CUSTOM_LWIP ?= 1
LWIP_INCDIR = $(SMING_HOME)/system/esp-lwip/lwip/include
Expand Down Expand Up @@ -261,23 +262,32 @@ ifeq ($(ENABLE_WPS),1)
endif

# compiler flags using during compilation of source files
CFLAGS = -Wpointer-arith -Wundef -Werror -Wl,-EL -nostdlib -mlongcalls -mtext-section-literals -finline-functions -fdata-sections -ffunction-sections \
CFLAGS = -Wall -Wundef -Wpointer-arith -Wno-comment -Wl,-EL -nostdlib -mlongcalls -mtext-section-literals -finline-functions -fdata-sections -ffunction-sections \
-D__ets__ -DICACHE_FLASH -DARDUINO=106 -DCOM_SPEED_SERIAL=$(COM_SPEED_SERIAL) $(USER_CFLAGS) -DENABLE_CMD_EXECUTOR=$(ENABLE_CMD_EXECUTOR) -DSMING_INCLUDED=1
ifneq ($(STRICT),1)
CFLAGS += -Werror -Wno-sign-compare -Wno-parentheses -Wno-unused-variable -Wno-unused-but-set-variable -Wno-strict-aliasing -Wno-return-type -Wno-maybe-uninitialized
endif

# => SDK
ifneq (,$(findstring third-party/ESP8266_NONOS_SDK, $(SDK_BASE)))
CFLAGS += -DSDK_INTERNAL
endif

ifeq ($(ENABLE_GDB), 1)
CFLAGS += -ggdb -DENABLE_GDB=1
MODULES += $(SMING_HOME)/gdb
endif

ifeq ($(SMING_RELEASE),1)
# See: https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
# for full list of optimization options
CFLAGS += -Os -DSMING_RELEASE=1
else ifeq ($(ENABLE_GDB), 1)
CFLAGS += -Og -ggdb -DGDBSTUB_FREERTOS=0 -DENABLE_GDB=1 -DGDBSTUB_CTRLC_BREAK=0
MODULES += $(THIRD_PARTY_DIR)/esp-gdbstub
EXTRA_INCDIR += $(THIRD_PARTY_DIR)/esp-gdbstub
CFLAGS += -Og
else
CFLAGS += -Os -g
endif

ifeq ($(ENABLE_WPS),1)
CFLAGS += -DENABLE_WPS=1
endif
Expand Down Expand Up @@ -320,7 +330,8 @@ ifeq ($(DISABLE_SPIFFS), 1)
endif

# linker flags used to generate the main object file
LDFLAGS = -nostdlib -u call_user_start -u custom_crash_callback -Wl,-static -Wl,--gc-sections -Wl,-Map=$(FW_BASE)/firmware.map -Wl,-wrap,system_restart_local
LDFLAGS = -nostdlib -u call_user_start -u custom_crash_callback \
-Wl,-static -Wl,--gc-sections -Wl,-Map=$(FW_BASE)/firmware.map -Wl,-wrap,system_restart_local

# linker script used for the above linkier step
LD_PATH = $(SMING_HOME)/compiler/ld
Expand Down Expand Up @@ -377,7 +388,8 @@ SDK_LIBDIR = lib
SDK_LDDIR = ld
SDK_INCDIR = include

# select which tools to use as compiler, librarian and linker
# select which tools to use as assembler, compiler, librarian and linker
AS := $(XTENSA_TOOLS_ROOT)/xtensa-lx106-elf-gcc
CC := $(XTENSA_TOOLS_ROOT)/xtensa-lx106-elf-gcc
CXX := $(XTENSA_TOOLS_ROOT)/xtensa-lx106-elf-g++
AR := $(XTENSA_TOOLS_ROOT)/xtensa-lx106-elf-ar
Expand All @@ -396,7 +408,11 @@ CXX_SRC := $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.cpp))

C_OBJ := $(patsubst %.c,$(BUILD_BASE)/%.o,$(C_SRC))
CXX_OBJ := $(patsubst %.cpp,$(BUILD_BASE)/%.o,$(CXX_SRC))

AS_SRC := $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.s))
AS_OBJ := $(patsubst %.s,$(BUILD_BASE)/%.o,$(AS_SRC))
AS_SRC := $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.S))
AS_OBJ += $(patsubst %.S,$(BUILD_BASE)/%.o,$(AS_SRC))

OBJ := $(AS_OBJ) $(C_OBJ) $(CXX_OBJ)

Expand All @@ -422,6 +438,12 @@ endif


define compile-objects
${BUILD_BASE}/$1/%.o: $1/%.s
$(vecho) "AS $$<"
$(Q) $(AS) $(INCDIR) $(MODULE_INCDIR) $(EXTRA_INCDIR) $(SDK_INCDIR) $(CFLAGS) -c $$< -o $$@
${BUILD_BASE}/$1/%.o: $1/%.S
$(vecho) "AS $$<"
$(Q) $(AS) $(INCDIR) $(MODULE_INCDIR) $(EXTRA_INCDIR) $(SDK_INCDIR) $(CFLAGS) -c $$< -o $$@
${BUILD_BASE}/$1/%.o: $1/%.c ${BUILD_BASE}/$1/%.c.d
$(vecho) "CC $$<"
$(Q) $(CC) $(INCDIR) $(MODULE_INCDIR) $(EXTRA_INCDIR) $(SDK_INCDIR) $(CFLAGS) -c $$< -o $$@
Expand Down
Loading

0 comments on commit c618f85

Please sign in to comment.