Optimize debug log

1. optimize wifi debug log
2. optimize lwip debug log
This commit is contained in:
liu zhifu
2020-04-28 15:57:18 +08:00
committed by maojianxin
parent 79db8df354
commit 2e5ea3a51b
14 changed files with 2413 additions and 189 deletions

View File

@@ -0,0 +1,13 @@
idf_build_get_property(target IDF_TARGET)
set(srcs "src/esp_debug.c")
set(include_dirs
lwip/port/esp32/include
)
idf_component_register(SRCS "${srcs}"
INCLUDE_DIRS include
PRIV_INCLUDE_DIRS
REQUIRES esp_event lwip
PRIV_REQUIRES "${target}")

View File

@@ -0,0 +1,228 @@
menu "ESP DEBUG"
config ESP32_WIFI_DEBUG_COUNTER
bool "Enable WiFi Debug Counter"
default n
help
Select this option to enable more debug counters
config ESP32_WIFI_DEBUG_PKT_FILTER_ENABLE
bool "Enable WiFi Packet Filter "
default n
help
Select this option to enable WiFi packet filter
menu "WiFi Packet Filter Type"
depends on ESP32_WIFI_DEBUG_PKT_FILTER_ENABLE
config ESP32_WIFI_DEBUG_PKT_FILTER_TCP
bool "Filter TCP Packets"
default n
help
Select this option to filter TCP packets.
config ESP32_WIFI_DEBUG_PKT_FILTER_UDP
bool "Filter UDP Packets"
default n
help
Select this option to filter UDP packets.
config ESP32_WIFI_DEBUG_PKT_FILTER_ARP
bool "Filter ARP Packets"
default n
help
Select this option to filter ARP packets.
config ESP32_WIFI_DEBUG_PKT_FILTER_DHCP
bool "Filter DHCP Packets"
default n
help
Select this option to filter DHCP packets.
config ESP32_WIFI_DEBUG_PKT_FILTER_DNS
bool "Filter DNS Packets"
default n
help
Select this option to filter DNS packets.
config ESP32_WIFI_DEBUG_PKT_FILTER_MDNS
bool "Filter MDNS Packets"
default n
help
Select this option to filter DNS packets.
endmenu
config ESP32_DEBUG_TASK_ENABLE
bool "Enable Debug Polling Task"
default n
help
Select this option to create debug task.
config ESP32_DEBUG_TASK_PRIORITY
int "Debug Task priority"
depends on ESP32_DEBUG_TASK_ENABLE
range 1 23
default 10
help
Set WiFi debug task priority
config ESP32_DEBUG_TASK_STACK_SIZE
int "Debug Task Stack Size"
depends on ESP32_DEBUG_TASK_ENABLE
default 3072
range 1024 65536
help
Configure WiFi debug task stack size.
config ESP32_DEBUG_TASK_POLLING_ENABLE
bool "Enable Debug Task Polling"
depends on ESP32_DEBUG_TASK_ENABLE
default n
help
Select this option to enable Debug Task Polling.
When the polling is enabled, the Debug Task query and dump the specified counters periodically.
config ESP32_DEBUG_TASK_POLLING_PERIOD
int "Debug Task Polling Period (seconds)"
depends on ESP32_DEBUG_TASK_POLLING_ENABLE
range 1 65535
default 30
help
Select this option to enable Debug Task Polling
menu "WiFi Polling Modules"
depends on ESP32_DEBUG_TASK_POLLING_ENABLE
config ESP32_DEBUG_TASK_POLLING_WIFI_HARDWARE_DIAG
bool "Polling WiFi hardware diag registers"
default n
help
Select this option to polling WiFi hardware diag registers
config ESP32_DEBUG_TASK_POLLING_WIFI_HARDWARE_COUNTERS
bool "Polling WiFi hardware counters"
default n
help
Select this option to polling WiFi hardware counters
config ESP32_DEBUG_TASK_POLLING_WIFI_HARDWARE_MISC
bool "Polling WiFi hardware misc registers"
default n
help
Select this option to polling WiFi hardware misc registers
config ESP32_DEBUG_TASK_POLLING_WIFI_BUFFER_COUNTERS
bool "Polling WiFi buffer counters"
default y
help
Select this option to polling WiFi buffer counters
config ESP32_DEBUG_TASK_POLLING_WIFI_INT_COUNTERS
bool "Polling WiFi interrupt counters"
default y
help
Select this option to polling WiFi interrupt counters
config ESP32_DEBUG_TASK_POLLING_WIFI_RX_COUNTERS
bool "Polling WiFi receiving packet counters"
default y
help
Select this option to polling WiFi receiving packet counters
config ESP32_DEBUG_TASK_POLLING_WIFI_TX_COUNTERS
bool "Polling WiFi transmitting packet counters"
default y
help
Select this option to polling WiFi transmitting packet counters
config ESP32_DEBUG_TASK_POLLING_WIFI_BUFFER_COUNTERS
bool "Polling WiFi buffer counters"
default y
help
Select this option to polling WiFi buffer counters
config ESP32_DEBUG_TASK_POLLING_WIFI_SLEEP_COUNTERS
bool "Polling WiFi sleep counters"
default y
help
Select this option to polling WiFi sleep counters
config ESP32_DEBUG_TASK_POLLING_WIFI_EVENT_COUNTERS
bool "Polling WiFi event counters"
default n
help
Select this option to polling WiFi key event counters
config ESP32_DEBUG_TASK_POLLING_WIFI_NVS_INFO
bool "Polling WiFi NVS info"
default n
help
Select this option to polling WiFi NVS info
config ESP32_DEBUG_TASK_POLLING_WIFI_STA_INFO
bool "Polling WiFi station internal info"
default n
help
Select this option to polling WiFi station internal info
config ESP32_DEBUG_TASK_POLLING_WIFI_AP_INFO
bool "Polling WiFi soft-AP internal info"
default n
help
Select this option to polling WiFi soft-AP internal info
config ESP32_DEBUG_TASK_POLLING_WIFI_TRC_INFO
bool "Polling WiFi rate control info"
default n
help
Select this option to polling WiFi rate control info
config ESP32_DEBUG_TASK_POLLING_WIFI_MEMORY_INFO
bool "Polling WiFi memory info"
default n
help
Select this option to polling WiFi memory info
endmenu
menu "LWIP Polling Modules"
depends on ESP32_DEBUG_TASK_POLLING_ENABLE
config ESP32_DEBUG_TASK_POLLING_LWIP_SOCKETS
bool "Polling LWIP sockets info"
default n
help
Select this option to polling WiFi memory info
config ESP32_DEBUG_TASK_POLLING_LWIP_PKT_COUNTER
bool "Polling LWIP packet counters"
default n
help
Select this option to polling LWIP packet counters
endmenu
menu "System Polling Modules"
depends on ESP32_DEBUG_TASK_POLLING_ENABLE
config ESP32_DEBUG_TASK_POLLING_TASK_INFO
bool "Polling task info"
default n
help
Select this option to polling LWIP packet counters
config ESP32_DEBUG_TASK_POLLING_MEMORY_INFO
bool "Polling memory info"
default n
help
Select this option to polling memory info
endmenu
endmenu

View File

View File

@@ -0,0 +1,5 @@
#
# Component Makefile
#
COMPONENT_SRCDIRS := src
COMPONENT_PRIV_INCLUDEDIRS := private_include

View File

@@ -0,0 +1,100 @@
// Copyright 2020-2021 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* All the APIs declared here are internal only APIs, it can only be used by
* espressif internal modules, such as SSC, LWIP, TCPIP adapter etc, espressif
* customers are not recommended to use them.
*
* If someone really want to use specified APIs declared in here, please contact
* espressif AE/developer to make sure you know the limitations or risk of
* the API, otherwise you may get unexpected behavior!!!
*
*/
#ifndef __ESP_DEBUG_H__
#define __ESP_DEBUG_H__
#include <stdint.h>
#include <stdbool.h>
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "sys/queue.h"
#include "esp_err.h"
#include "esp_event.h"
#include "esp_wifi.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
uint32_t diag;
} wifi_hardware_diag_t;
void esp_wifi_debug_get_hardare_diag(wifi_hardware_diag_t *reg);
void esp_wifi_debug_dump_hardware_diag(const wifi_hardware_diag_t *reg);
typedef struct {
} wifi_int_counter_t;
void esp_wifi_debug_get_int_counter(wifi_int_counter_t *cnt);
void esp_wifi_debug_dump_int_counter(const wifi_int_counter_t *cnt);
/* Polling type definition
* - bit0 is for debug task deleting
* - bit1~15 is for system info polling
* - bit16~31 is for LWIP info polling
* - bit32~63 is for WiFi info polling
*/
#define DEBUG_MODULE_TASK_DELETE 1
#define DEBUG_MODULE_SYS_MEM (1ULL<<1)
#define DEBUG_MODULE_SYS_TASK (1ULL<<2)
#define DEBUG_MODULE_LWIP_SOCKETS (1ULL<<16)
#define DEBUG_MODULE_LWIP_STATS (1ULL<<17)
#define DEBUG_MODULE_WIFI_HW_DIAG (1ULL<<32)
#define DEBUG_MODULE_WIFI_HW_COUNTERS (1ULL<<33)
#define DEBUG_MODULE_WIFI_HW_MISC (1ULL<<34)
#define DEBUG_MODULE_WIFI_INT_COUNTERS (1ULL<<35)
#define DEBUG_MODULE_WIFI_RX_COUNTERS (1ULL<<36)
#define DEBUG_MODULE_WIFI_TX_COUNTERS (1ULL<<37)
#define DEBUG_MODULE_WIFI_BUFFER_COUNTERS (1ULL<<38)
#define DEBUG_MODULE_WIFI_SLEEP_COUNTERS (1ULL<<39)
#define DEBUG_MODULE_WIFI_EVENT_COUNTERS (1ULL<<40)
#define DEBUG_MODULE_WIFI_NVS (1ULL<<41)
#define DEBUG_MODULE_WIFI_STA (1ULL<<42)
#define DEBUG_MODULE_WIFI_AP (1ULL<<43)
#define DEBUG_MODULE_WIFI_TRC (1ULL<<44)
#define DEBUG_MODULE_ALL (0xFFFFFFFFFFFFFFFE)
typedef uint64_t wifi_polling_request_t;
void esp_lwip_debug_dump_sockets(void);
void esp_lwip_debug_dump_stats(void);
void esp_lwip_dump(uint64_t modules);
esp_err_t esp_debug_init(void);
esp_err_t esp_debug_deinit(void);
esp_err_t esp_debug_poll(wifi_polling_request_t request);
#ifdef __cplusplus
}
#endif
#endif /* __WIFI_DEBUG_H__ */

View File

@@ -0,0 +1,359 @@
// Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <esp_event.h>
#include <esp_wifi.h>
#include "esp_log.h"
#include "esp_wifi_internal.h"
#include "esp_debug.h"
#include "wifi_debug.h"
#define TAG "wdbg"
static bool s_debug_init = false;
#if CONFIG_ESP32_DEBUG_TASK_ENABLE
#define WIFI_DEBUG_TASK_QUEUE_SIZE 5
static SemaphoreHandle_t s_debug_mutex = NULL;
static QueueHandle_t s_debug_task_queue = NULL;
static bool s_debug_task_init = false;
void debug_lock(void)
{
if (s_debug_mutex) {
xSemaphoreTake(s_debug_mutex, portMAX_DELAY);
} else {
s_debug_mutex = xSemaphoreCreateMutex();
if (!s_debug_mutex) {
ESP_LOGE(TAG, "fail to create mutex");
abort();
} else {
xSemaphoreTake(s_debug_mutex, portMAX_DELAY);
}
}
}
void debug_unlock(void)
{
if (s_debug_mutex) {
xSemaphoreGive(s_debug_mutex);
} else {
ESP_LOGE(TAG, "unlock null mutex");
abort();
}
}
static uint64_t debug_periodical_request(void)
{
uint64_t request = 0;
#if CONFIG_ESP32_DEBUG_TASK_POLLING_LWIP_SOCKETS
request |= DEBUG_MODULE_LWIP_SOCKETS;
#endif
#if CONFIG_ESP32_DEBUG_TASK_POLLING_LWIP_STATS
request |= DEBUG_MODULE_LWIP_STATISTICS;
#endif
#if CONFIG_ESP32_DEBUG_TASK_POLLING_WIFI_HARDWARE_DIAG
request |= DEBUG_MODULE_WIFI_HW_DIAG;
#endif
#if CONFIG_ESP32_DEBUG_TASK_POLLING_WIFI_HARDWARE_COUNTERS
request |= DEBUG_MODULE_WIFI_HW_COUNTERS;
#endif
#if CONFIG_ESP32_DEBUG_TASK_POLLING_WIFI_HARDWARE_MISC
request |= DEBUG_MODULE_WIFI_HW_MISC;
#endif
#if CONFIG_ESP32_DEBUG_TASK_POLLING_WIFI_INT_COUNTERS
request |= DEBUG_MODULE_WIFI_INT_COUNTERS;
#endif
#if CONFIG_ESP32_DEBUG_TASK_POLLING_WIFI_RX_COUNTERS
request |= DEBUG_MODULE_WIFI_RX_COUNTERS;
#endif
#if CONFIG_ESP32_DEBUG_TASK_POLLING_WIFI_TX_COUNTERS
request |= DEBUG_MODULE_WIFI_TX_COUNTERS;
#endif
#if CONFIG_ESP32_DEBUG_TASK_POLLING_WIFI_BUFFER_COUNTERS
request |= DEBUG_MODULE_WIFI_BUFFER_COUNTERS;
#endif
#if CONFIG_ESP32_DEBUG_TASK_POLLING_WIFI_SLEEP_COUNTERS
request |= DEBUG_MODULE_WIFI_SLEEP_COUNTERS;
#endif
#if CONFIG_ESP32_DEBUG_TASK_POLLING_WIFI_EVENT_COUNTERS
request |= DEBUG_MODULE_WIFI_EVENT_COUNTERS;
#endif
#if CONFIG_ESP32_DEBUG_TASK_POLLING_WIFI_NVS_INFO
request |= DEBUG_MODULE_WIFI_NVS;
#endif
#if CONFIG_ESP32_DEBUG_TASK_POLLING_WIFI_STA_INFO
request |= DEBUG_MODULE_WIFI_STA;
#endif
#if CONFIG_ESP32_DEBUG_TASK_POLLING_WIFI_AP_INFO
request |= DEBUG_MODULE_WIFI_AP;
#endif
#if CONFIG_ESP32_DEBUG_TASK_POLLING_WIFI_TRC_INFO
request |= DEBUG_MODULE_WIFI_TRC;
#endif
return request;
}
static uint64_t debug_modules_to_lwip_modules(uint64_t modules)
{
uint64_t lwip_modules = 0;
if (modules & DEBUG_MODULE_LWIP_SOCKETS) {
lwip_modules |= 1;//LWIP_MODULE_SOCKETS;
}
if (modules & DEBUG_MODULE_LWIP_STATS) {
lwip_modules |= 1<<1;//LWIP_MODULE_STATS;
}
return lwip_modules;
}
static uint64_t debug_modules_to_wifi_modules(uint64_t modules)
{
uint64_t wifi_modules = 0;
if (modules & DEBUG_MODULE_WIFI_HW_DIAG) {
wifi_modules |= WIFI_MODULE_HW_DIAG;
}
if (modules & DEBUG_MODULE_WIFI_HW_COUNTERS) {
wifi_modules |= WIFI_MODULE_HW_COUNTERS;
}
if (modules & DEBUG_MODULE_WIFI_INT_COUNTERS) {
wifi_modules |= WIFI_MODULE_INT_COUNTERS;
}
if (modules & DEBUG_MODULE_WIFI_RX_COUNTERS) {
wifi_modules |= WIFI_MODULE_RX_COUNTERS;
}
if (modules & DEBUG_MODULE_WIFI_TX_COUNTERS) {
wifi_modules |= WIFI_MODULE_TX_COUNTERS;
}
if (modules & DEBUG_MODULE_WIFI_BUFFER_COUNTERS) {
wifi_modules |= WIFI_MODULE_BUFFER_COUNTERS;
}
if (modules & DEBUG_MODULE_WIFI_SLEEP_COUNTERS) {
wifi_modules |= WIFI_MODULE_SLEEP;
}
if (modules & DEBUG_MODULE_WIFI_EVENT_COUNTERS) {
wifi_modules |= WIFI_MODULE_EVENT_COUNTERS;
}
if (modules & DEBUG_MODULE_WIFI_NVS) {
wifi_modules |= WIFI_MODULE_NVS;
}
if (modules & DEBUG_MODULE_WIFI_STA) {
wifi_modules |= WIFI_MODULE_STA;
}
if (modules & DEBUG_MODULE_WIFI_AP) {
wifi_modules |= WIFI_MODULE_AP;
}
if (modules & DEBUG_MODULE_WIFI_TRC) {
wifi_modules |= WIFI_MODULE_TRC;
}
return wifi_modules;
}
static void debug_process_request(wifi_polling_request_t request)
{
uint64_t modules = 0;
ESP_LOGI(TAG, "poll begin, type=%llx, ram=%d mini=%d", request, esp_get_free_heap_size(), esp_get_minimum_free_heap_size());
modules = debug_modules_to_lwip_modules(request);
esp_lwip_dump(modules);
modules = debug_modules_to_wifi_modules(request);
esp_wifi_dump(modules);
ESP_LOGI(TAG, "poll end, ram=%d mini=%d", esp_get_free_heap_size(), esp_get_minimum_free_heap_size());
}
static void debug_task(void *param)
{
wifi_polling_request_t request = 0;
uint32_t timeout = portMAX_DELAY;
#if CONFIG_ESP32_DEBUG_TASK_POLLING_ENABLE
timeout = (CONFIG_ESP32_DEBUG_TASK_POLLING_PERIOD * 1000) / portTICK_RATE_MS;
#endif
while (1) {
if (pdTRUE != xQueueReceive(s_debug_task_queue, &request, timeout)) {
request = debug_periodical_request();
}
debug_process_request(request);
if (request == DEBUG_MODULE_TASK_DELETE) {
break;
}
}
vQueueDelete(s_debug_task_queue);
s_debug_task_queue = NULL;
vTaskDelete(NULL);
}
esp_err_t debug_task_create(void)
{
int ret;
debug_lock();
if (s_debug_task_init == true) {
ESP_LOGE(TAG, "wifi init task already created");
debug_unlock();
return ESP_OK;
}
s_debug_task_queue = xQueueCreate(WIFI_DEBUG_TASK_QUEUE_SIZE, sizeof(wifi_polling_request_t));
if (!s_debug_task_queue) {
ESP_LOGE(TAG, "fail to create debug task queue");
debug_unlock();
return ESP_FAIL;
}
ret = xTaskCreate(debug_task, "wifi_debug", CONFIG_ESP32_DEBUG_TASK_STACK_SIZE, NULL, CONFIG_ESP32_DEBUG_TASK_PRIORITY, NULL);
if (pdTRUE != ret) {
ESP_LOGE(TAG, "fail to create debug task");
vQueueDelete(s_debug_task_queue);
s_debug_task_queue = NULL;
debug_unlock();
return ESP_FAIL;
}
s_debug_task_init = true;
debug_unlock();
ESP_LOGI(TAG, "wifi debug task created");
return ESP_OK;
}
static void debug_task_delete(void)
{
wifi_polling_request_t request = DEBUG_MODULE_TASK_DELETE;
debug_lock();
if (false == s_debug_task_init) {
ESP_LOGI(TAG, "wifi debug task already deleted");
debug_unlock();
return;
}
//post delete event to WiFi debug task with blocking
xQueueSend(s_debug_task_queue, &request, portMAX_DELAY);
/* Wait until debug task is deleted */
do {
vTaskDelay(1);
} while (s_debug_task_queue);
s_debug_task_init = false;
debug_unlock();
ESP_LOGI(TAG, "wifi debug task deleted");
return;
}
esp_err_t esp_debug_poll(wifi_polling_request_t request)
{
debug_lock();
if (false == s_debug_task_init) {
ESP_LOGE(TAG, "wifi debug task not created");
debug_unlock();
return ESP_FAIL;
}
//post event to WiFi debug task without any blocking
if (pdTRUE != xQueueSend(s_debug_task_queue, &request, 0) ) {
ESP_LOGW(TAG, "failed to post polling event=%llx", request);
}
debug_unlock();
return ESP_OK;
}
#else
esp_err_t esp_debug_poll(wifi_polling_request_t request)
{
ESP_LOGE(TAG, "poll disabled");
return ESP_OK;
}
#endif
esp_err_t esp_debug_init(void)
{
esp_err_t ret = ESP_OK;
if (true == s_debug_init) {
return ESP_OK;
}
// TODO Set Packets Filter
ret = esp_wifi_stats_init();
if (ESP_OK != ret) {
return ESP_FAIL;
}
#if CONFIG_ESP32_DEBUG_TASK_ENABLE
//Create polling task
ret = debug_task_create();
if (ESP_OK != ret) {
esp_wifi_stats_deinit();
return ESP_FAIL;
}
#endif
s_debug_init = true;
return ESP_OK;
}
esp_err_t esp_debug_deinit(void)
{
if (false == s_debug_init) {
return ESP_OK;
}
esp_wifi_stats_deinit();
#if CONFIG_ESP32_DEBUG_TASK_ENABLE
debug_task_delete();
#endif
s_debug_init = false;
return ESP_OK;
}