Просмотр исходного кода

component/bt : fix the bug of deinit/disable

1. fix the exception of deinit/disable
2. debug mem leak of deinit, fix some memleak, but still something wrong.
3. debug men leak of disable.
4. add bluedroid memory debug
Tian Hao 9 лет назад
Родитель
Сommit
7ae7a56457

+ 6 - 0
components/bt/Kconfig

@@ -14,6 +14,12 @@ config BTC_TASK_STACK_SIZE
 	help
 		This select btc task stack size
 
+config BLUEDROID_MEM_DEBUG
+	bool "Bluedroid memory debug"
+	default no
+	help
+		Bluedroid memory debug
+
 #config BT_BTLE
 #    bool "Enable BTLE"
 #    depends on BT_ENABLED

+ 1 - 1
components/bt/bluedroid/bta/dm/bta_dm_api.c

@@ -2168,8 +2168,8 @@ void BTA_VendorCleanup (void)
     BTM_BleGetVendorCapabilities(&cmn_ble_vsc_cb);
 
 #if (BLE_INCLUDED == TRUE && BLE_ANDROID_CONTROLLER_SCAN_FILTER == TRUE)
+    btm_ble_adv_filter_cleanup();   // when BLE_VND_INCLUDED is false, this function will be ignore, so move it out of "if"
     if (cmn_ble_vsc_cb.max_filter > 0) {
-        btm_ble_adv_filter_cleanup();
 #if BLE_PRIVACY_SPT == TRUE
         btm_ble_resolving_list_cleanup ();
 #endif

+ 2 - 0
components/bt/bluedroid/bta/sys/bta_sys_main.c

@@ -598,6 +598,8 @@ void bta_alarm_cb(void *data)
     TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;
 
     fixed_queue_enqueue(btu_bta_alarm_queue, p_tle);
+
+    btu_task_post(SIG_BTU_WORK);
 }
 
 void bta_sys_start_timer(TIMER_LIST_ENT *p_tle, UINT16 type, INT32 timeout_ms)

+ 5 - 16
components/bt/bluedroid/gki/gki_buffer.c

@@ -20,15 +20,6 @@
 #include "allocator.h"
 #include "gki_int.h"
 
-#define ALIGN_POOL(pl_size)  ( (((pl_size) + 3) / sizeof(UINT32)) * sizeof(UINT32))
-#define BUFFER_HDR_SIZE     (sizeof(BUFFER_HDR_T))                  /* Offset past header */
-#define BUFFER_PADDING_SIZE (sizeof(BUFFER_HDR_T) + sizeof(UINT32)) /* Header + Magic Number */
-#define MAGIC_NO            0xDDBADDBA
-
-#define BUF_STATUS_FREE     0
-#define BUF_STATUS_UNLINKED 1
-#define BUF_STATUS_QUEUED   2
-
 /*******************************************************************************
 **
 ** Function         gki_init_free_queue
@@ -173,7 +164,7 @@ void GKI_init_q (BUFFER_Q *p_q)
 
 /*******************************************************************************
 **
-** Function         GKI_getbuf
+** Function         GKI_getbuf_func
 **
 ** Description      Called by an application to get a free buffer which
 **                  is of size greater or equal to the requested size.
@@ -187,7 +178,7 @@ void GKI_init_q (BUFFER_Q *p_q)
 ** Returns          A pointer to the buffer, or NULL if none available
 **
 *******************************************************************************/
-void *GKI_getbuf (UINT16 size)
+void *GKI_getbuf_func(UINT16 size)
 {
     BUFFER_HDR_T *header = osi_malloc(size + BUFFER_HDR_SIZE);
     header->status  = BUF_STATUS_UNLINKED;
@@ -198,10 +189,9 @@ void *GKI_getbuf (UINT16 size)
     return header + 1;
 }
 
-
 /*******************************************************************************
 **
-** Function         GKI_getpoolbuf
+** Function         GKI_getpoolbuf_func
 **
 ** Description      Called by an application to get a free buffer from
 **                  a specific buffer pool.
@@ -214,9 +204,9 @@ void *GKI_getbuf (UINT16 size)
 ** Returns          A pointer to the buffer, or NULL if none available
 **
 *******************************************************************************/
-void *GKI_getpoolbuf (UINT8 pool_id)
+void *GKI_getpoolbuf_func(UINT8 pool_id)
 {
-    return GKI_getbuf(gki_cb.com.pool_size[pool_id]);
+    return GKI_getbuf_func(gki_cb.com.pool_size[pool_id]);
 }
 
 /*******************************************************************************
@@ -235,7 +225,6 @@ void GKI_freebuf (void *p_buf)
     osi_free((BUFFER_HDR_T *)p_buf - 1);
 }
 
-
 /*******************************************************************************
 **
 ** Function         GKI_get_buf_size

+ 36 - 3
components/bt/bluedroid/gki/include/gki.h

@@ -21,8 +21,18 @@
 
 #include "bt_target.h"
 #include "bt_types.h"
+#include "gki_common.h"
+#include "gki_int.h"
+#include "allocator.h"
 
-//static const char GKI_MODULE[] = "gki_module";
+#define ALIGN_POOL(pl_size)  ( (((pl_size) + 3) / sizeof(UINT32)) * sizeof(UINT32))
+#define BUFFER_HDR_SIZE     (sizeof(BUFFER_HDR_T))                  /* Offset past header */
+#define BUFFER_PADDING_SIZE (sizeof(BUFFER_HDR_T) + sizeof(UINT32)) /* Header + Magic Number */
+#define MAGIC_NO            0xDDBADDBA
+
+#define BUF_STATUS_FREE     0
+#define BUF_STATUS_UNLINKED 1
+#define BUF_STATUS_QUEUED   2
 
 /* Timer list entry callback type
 */
@@ -62,14 +72,37 @@ typedef struct {
 
 /* To get and release buffers, change owner and get size
 */
-void    GKI_freebuf (void *);
-void   *GKI_getbuf (UINT16);
+void   *GKI_getbuf_func(UINT16);
+void   *GKI_getpoolbuf_func(UINT8);
+void    GKI_freebuf(void *);
 UINT16  GKI_get_buf_size (void *);
 void   *GKI_getpoolbuf (UINT8);
 UINT16  GKI_poolcount (UINT8);
 UINT16  GKI_poolfreecount (UINT8);
 UINT16  GKI_poolutilization (UINT8);
 
+#ifdef CONFIG_BLUEDROID_MEM_DEBUG
+
+#define GKI_getbuf(_size)                                            \
+({                                                                  \
+    BUFFER_HDR_T *header = osi_malloc((_size) + BUFFER_HDR_SIZE);      \
+    header->status  = BUF_STATUS_UNLINKED;                          \
+    header->p_next  = NULL;                                         \
+    header->Type    = 0;                                            \
+    header->size = (_size);                                          \
+    (header + 1);                                                   \
+})
+
+#define GKI_getpoolbuf(_pool_id)                                     \
+({                                                                  \
+    GKI_getbuf(gki_cb.com.pool_size[(_pool_id)]);                    \
+})
+           
+#else
+#define GKI_getbuf          GKI_getbuf_func
+#define GKI_getpoolbuf      GKI_getpoolbuf_func
+
+#endif /* CONFIG_BLUEDROID_MEM_DEBUG */
 
 /* User buffer queue management
 */

+ 1 - 1
components/bt/bluedroid/hci/hci_layer.c

@@ -198,7 +198,7 @@ static void hci_layer_deinit_env(void)
     command_waiting_response_t *cmd_wait_q;
 
     if (hci_host_env.command_queue) {
-        fixed_queue_free(hci_host_env.command_queue, osi_free);
+        fixed_queue_free(hci_host_env.command_queue, allocator_calloc.free);
     }
     if (hci_host_env.packet_queue) {
         fixed_queue_free(hci_host_env.packet_queue, buffer_allocator->free);

+ 11 - 2
components/bt/bluedroid/main/bte_main.c

@@ -37,6 +37,7 @@
 #include "hash_functions.h"
 #include "controller.h"
 #include "hci_layer.h"
+#include "bta_api.h"
 
 //#include "bluedroid_test.h"
 /*
@@ -104,6 +105,7 @@ fixed_queue_t *btu_hci_msg_queue;
 
 bluedroid_init_done_cb_t bluedroid_init_done_cb;
 
+extern void osi_mem_dbg_init(void);
 /******************************************************************************
 **
 ** Function         bte_main_boot_entry
@@ -115,6 +117,10 @@ bluedroid_init_done_cb_t bluedroid_init_done_cb;
 ******************************************************************************/
 int bte_main_boot_entry(bluedroid_init_done_cb_t cb)
 {
+#ifdef CONFIG_BLUEDROID_MEM_DEBUG
+    osi_mem_dbg_init();
+#endif
+
     if (gki_init()) {
         LOG_ERROR("%s: Init GKI Module Failure.\n", __func__);
         return -1;
@@ -163,6 +169,7 @@ void bte_main_shutdown(void)
 {
     //data_dispatcher_register_default(hci_layer_get_interface()->event_dispatcher, NULL);
     hci->set_data_queue(NULL);
+    fixed_queue_unregister_dequeue(btu_hci_msg_queue);
     fixed_queue_free(btu_hci_msg_queue, NULL);
 
     btu_hci_msg_queue = NULL;
@@ -174,9 +181,11 @@ void bte_main_shutdown(void)
         module_clean_up(get_module(GKI_MODULE));
     */
 
-    gki_clean_up();
-
+#if (BLE_INCLUDED == TRUE)
+    BTA_VendorCleanup();
+#endif
     bte_main_disable();
+    gki_clean_up();
 }
 
 /******************************************************************************

+ 1 - 1
components/bt/bluedroid/osi/alarm.c

@@ -62,7 +62,7 @@ static void alarm_cb_handler(TimerHandle_t xTimer)
     struct alarm_t *alarm;
 
     if (!xTimer) {
-        LOG_DEBUG("TimerName: NULL\n");
+        LOG_ERROR("TimerName: NULL\n");
         return;
     }
 

+ 98 - 7
components/bt/bluedroid/osi/allocator.c

@@ -24,6 +24,97 @@
 extern void *pvPortZalloc(size_t size);
 extern void vPortFree(void *pv);
 
+#ifdef CONFIG_BLUEDROID_MEM_DEBUG
+
+#define OSI_MEM_DBG_INFO_MAX    1024
+typedef struct {
+    void *p;
+    int size;
+    const char *func;
+    int line;
+} osi_mem_dbg_info_t;
+
+static uint32_t mem_dbg_count = 0;
+static uint32_t mem_dbg_count2 = 0;
+static osi_mem_dbg_info_t mem_dbg_info[OSI_MEM_DBG_INFO_MAX];
+
+void osi_mem_dbg_init(void)
+{
+    int i;
+
+    for (i = 0; i < OSI_MEM_DBG_INFO_MAX; i++) {
+        mem_dbg_info[i].p = NULL;
+        mem_dbg_info[i].size = 0;
+        mem_dbg_info[i].func = NULL;
+        mem_dbg_info[i].line = 0;
+    }
+    mem_dbg_count = 0;
+    mem_dbg_count2 = 0;
+}
+
+void osi_mem_dbg_record(void *p, int size, const char *func, int line)
+{
+    int i;
+
+    if (!p || size == 0) {
+        LOG_ERROR("%s invalid !!\n", __func__);
+        return;
+    }
+
+    for (i = 0; i < OSI_MEM_DBG_INFO_MAX; i++) {
+        if (mem_dbg_info[i].p == NULL) {
+            mem_dbg_info[i].p = p;
+            mem_dbg_info[i].size = size;
+            mem_dbg_info[i].func = func;
+            mem_dbg_info[i].line = line;
+            mem_dbg_count++;
+            break;
+        }
+    }
+
+    if (i >= OSI_MEM_DBG_INFO_MAX) {
+        LOG_ERROR("%s full !!\n", __func__);
+    }
+}
+
+void osi_mem_dbg_clean(void *p)
+{
+    int i;
+
+    if (!p) {
+        LOG_ERROR("%s invalid\n", __func__);
+        return;
+    }
+
+    for (i = 0; i < OSI_MEM_DBG_INFO_MAX; i++) {
+        if (mem_dbg_info[i].p == p) {
+            mem_dbg_info[i].p = NULL;
+            mem_dbg_info[i].size = 0;
+            mem_dbg_info[i].func = NULL;
+            mem_dbg_info[i].line = 0;
+            mem_dbg_count--;
+            break;
+        }
+    }
+
+    if (i >= OSI_MEM_DBG_INFO_MAX) {
+        LOG_ERROR("%s full !!\n", __func__);
+    }
+}
+
+void osi_mem_dbg_show(void)
+{
+    int i;
+
+    for (i = 0; i < OSI_MEM_DBG_INFO_MAX; i++) {
+        if (mem_dbg_info[i].p || mem_dbg_info[i].size != 0 ) {
+            LOG_ERROR("--> p %p, s %d, f %s, l %d\n", mem_dbg_info[i].p, mem_dbg_info[i].size, mem_dbg_info[i].func, mem_dbg_info[i].line);
+        }
+    }
+    LOG_ERROR("--> count %d\n", mem_dbg_count);
+}
+#endif
+
 char *osi_strdup(const char *str)
 {
     size_t size = strlen(str) + 1;  // + 1 for the null terminator
@@ -37,27 +128,27 @@ char *osi_strdup(const char *str)
     return new_string;
 }
 
-void *osi_malloc(size_t size)
+void *osi_malloc_func(size_t size)
 {
     return calloc(1, size);
 }
 
-void *osi_calloc(size_t size)
+void *osi_calloc_func(size_t size)
 {
     return calloc(1, size);
 }
 
-void osi_free(void *ptr)
+void osi_free_func(void *ptr)
 {
     free(ptr);
 }
 
 const allocator_t allocator_malloc = {
-    osi_malloc,
-    osi_free
+    osi_malloc_func,
+    osi_free_func
 };
 
 const allocator_t allocator_calloc = {
-    osi_calloc,
-    osi_free
+    osi_calloc_func,
+    osi_free_func
 };

+ 42 - 3
components/bt/bluedroid/osi/include/allocator.h

@@ -36,8 +36,47 @@ extern const allocator_t allocator_calloc;
 
 char *osi_strdup(const char *str);
 
-void *osi_malloc(size_t size);
-void *osi_calloc(size_t size);
-void osi_free(void *ptr);
+void *osi_malloc_func(size_t size);
+void *osi_calloc_func(size_t size);
+void osi_free_func(void *ptr);
+
+#ifdef CONFIG_BLUEDROID_MEM_DEBUG
+
+void osi_mem_dbg_init(void);
+void osi_mem_dbg_record(void *p, int size, const char *func, int line);
+void osi_mem_dbg_clean(void *p);
+void osi_mem_dbg_show(void);
+
+#define osi_malloc(size)                                \
+({                                                      \
+    void *p;                                            \
+                                                        \
+    p = osi_malloc_func(size);                          \
+    osi_mem_dbg_record(p, size, __func__, __LINE__);    \
+    (void *)p;                                           \
+})
+
+#define osi_calloc(size)                                \
+({                                                      \
+    void *p;                                            \
+                                                        \
+    p = osi_calloc_func(size);                          \
+    osi_mem_dbg_record(p, size, __func__, __LINE__);    \
+    (void *)p;                                           \
+})
+
+#define osi_free(ptr)                                   \
+({                                                      \
+    osi_mem_dbg_clean(ptr);                             \
+    osi_free_func((ptr));                               \
+})
+
+#else
+
+#define osi_malloc                  osi_malloc_func
+#define osi_calloc                  osi_calloc_func
+#define osi_free                    osi_free_func
+
+#endif /* CONFIG_BLUEDROID_MEM_DEBUG */
 
 #endif /* _ALLOCATOR_H_ */

+ 1 - 1
components/bt/bluedroid/osi/include/thread.h

@@ -51,7 +51,7 @@ enum {
 #define HCI_H4_TASK_STACK_SIZE		1024
 #define HCI_H4_TASK_PRIO			(configMAX_PRIORITIES - 3)
 #define HCI_H4_TASK_NAME			"hciH4T"
-#define HCI_H4_QUEUE_NUM			30
+#define HCI_H4_QUEUE_NUM			40
 
 #define BTU_TASK_STACK_SIZE			4096
 #define BTU_TASK_PRIO				(configMAX_PRIORITIES - 1)

+ 1 - 1
components/bt/bluedroid/stack/btm/btm_ble_bgconn.c

@@ -64,7 +64,7 @@ static void background_connections_lazy_init()
 {
     if (!background_connections) {
         background_connections = hash_map_new(background_connection_buckets,
-                                              hash_function_bdaddr, NULL, osi_free, bdaddr_equality_fn);
+                                              hash_function_bdaddr, NULL, allocator_calloc.free, bdaddr_equality_fn);
         assert(background_connections);
     }
 }

+ 3 - 3
components/bt/bluedroid/stack/btu/btu_init.c

@@ -217,9 +217,9 @@ error_exit:;
 void BTU_ShutDown(void)
 {
     btu_task_shut_down();
-    /*
-      fixed_queue_free(btu_bta_msg_queue, NULL);
-    */
+
+    fixed_queue_free(btu_bta_msg_queue, NULL);
+
     hash_map_free(btu_general_alarm_hash_map);
     pthread_mutex_destroy(&btu_general_alarm_lock);
     fixed_queue_free(btu_general_alarm_queue, NULL);

+ 0 - 1
components/bt/bluedroid/stack/btu/btu_task.c

@@ -378,7 +378,6 @@ void btu_task_start_up(void)
 
 void btu_task_shut_down(void)
 {
-    fixed_queue_unregister_dequeue(btu_hci_msg_queue);
     fixed_queue_unregister_dequeue(btu_general_alarm_queue);
     fixed_queue_unregister_dequeue(btu_oneshot_alarm_queue);
     fixed_queue_unregister_dequeue(btu_l2cap_alarm_queue);