2022-01-06 13:44:17 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* mm/mm_heap/mm_memdump.c
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/mm/mm.h>
|
|
|
|
|
|
|
|
#include "mm_heap/mm.h"
|
|
|
|
|
2022-01-20 10:17:29 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if UINTPTR_MAX <= UINT32_MAX
|
|
|
|
# define MM_PTR_FMT_WIDTH 11
|
|
|
|
#elif UINTPTR_MAX <= UINT64_MAX
|
|
|
|
# define MM_PTR_FMT_WIDTH 19
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-01-06 13:44:17 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void memdump_handler(FAR struct mm_allocnode_s *node, FAR void *arg)
|
|
|
|
{
|
2022-11-20 08:00:44 +01:00
|
|
|
pid_t pid = *(FAR pid_t *)arg;
|
2022-12-15 12:55:16 +01:00
|
|
|
size_t nodesize = SIZEOF_MM_NODE(node);
|
2022-01-06 13:44:17 +01:00
|
|
|
|
2022-12-15 12:55:16 +01:00
|
|
|
if ((node->size & MM_ALLOC_BIT) != 0)
|
2022-01-06 13:44:17 +01:00
|
|
|
{
|
2022-12-15 12:55:16 +01:00
|
|
|
DEBUGASSERT(nodesize >= SIZEOF_MM_ALLOCNODE);
|
2022-07-04 09:41:06 +02:00
|
|
|
#if CONFIG_MM_BACKTRACE < 0
|
2023-04-17 17:01:23 +02:00
|
|
|
if (pid == MM_BACKTRACE_ALLOC_PID)
|
2022-01-20 10:17:29 +01:00
|
|
|
#else
|
2023-04-17 17:01:23 +02:00
|
|
|
if (pid == MM_BACKTRACE_ALLOC_PID || node->pid == pid)
|
2022-01-20 10:17:29 +01:00
|
|
|
#endif
|
2022-01-06 13:44:17 +01:00
|
|
|
{
|
2022-07-04 09:41:06 +02:00
|
|
|
#if CONFIG_MM_BACKTRACE < 0
|
2022-01-20 10:17:29 +01:00
|
|
|
syslog(LOG_INFO, "%12zu%*p\n",
|
2022-12-15 12:55:16 +01:00
|
|
|
nodesize, MM_PTR_FMT_WIDTH,
|
2022-01-20 10:17:29 +01:00
|
|
|
((FAR char *)node + SIZEOF_MM_ALLOCNODE));
|
|
|
|
#else
|
2022-07-04 09:41:06 +02:00
|
|
|
# if CONFIG_MM_BACKTRACE > 0
|
2022-01-20 10:17:29 +01:00
|
|
|
int i;
|
2022-02-09 08:27:22 +01:00
|
|
|
FAR const char *format = " %0*p";
|
2022-07-04 09:41:06 +02:00
|
|
|
# endif
|
|
|
|
char buf[CONFIG_MM_BACKTRACE * MM_PTR_FMT_WIDTH + 1];
|
2022-01-20 10:17:29 +01:00
|
|
|
|
2022-02-09 08:27:22 +01:00
|
|
|
buf[0] = '\0';
|
2022-07-04 09:41:06 +02:00
|
|
|
# if CONFIG_MM_BACKTRACE > 0
|
|
|
|
for (i = 0; i < CONFIG_MM_BACKTRACE && node->backtrace[i]; i++)
|
2022-01-20 10:17:29 +01:00
|
|
|
{
|
2022-02-09 08:27:22 +01:00
|
|
|
sprintf(buf + i * MM_PTR_FMT_WIDTH, format,
|
|
|
|
MM_PTR_FMT_WIDTH - 1, node->backtrace[i]);
|
2022-01-20 10:17:29 +01:00
|
|
|
}
|
2022-07-04 09:41:06 +02:00
|
|
|
# endif
|
2022-01-20 10:17:29 +01:00
|
|
|
|
|
|
|
syslog(LOG_INFO, "%6d%12zu%*p%s\n",
|
2022-12-15 12:55:16 +01:00
|
|
|
(int)node->pid, nodesize, MM_PTR_FMT_WIDTH,
|
2022-01-20 10:17:29 +01:00
|
|
|
((FAR char *)node + SIZEOF_MM_ALLOCNODE), buf);
|
|
|
|
#endif
|
2022-01-06 13:44:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FAR struct mm_freenode_s *fnode = (FAR void *)node;
|
|
|
|
|
2022-12-30 09:30:55 +01:00
|
|
|
DEBUGASSERT(nodesize >= MM_MIN_CHUNK);
|
2022-01-06 13:44:17 +01:00
|
|
|
DEBUGASSERT(fnode->blink->flink == fnode);
|
2022-12-15 12:55:16 +01:00
|
|
|
DEBUGASSERT(SIZEOF_MM_NODE(fnode->blink) <= nodesize);
|
2022-01-06 13:44:17 +01:00
|
|
|
DEBUGASSERT(fnode->flink == NULL ||
|
|
|
|
fnode->flink->blink == fnode);
|
|
|
|
DEBUGASSERT(fnode->flink == NULL ||
|
2022-12-15 12:55:16 +01:00
|
|
|
SIZEOF_MM_NODE(fnode->flink) == 0 ||
|
|
|
|
SIZEOF_MM_NODE(fnode->flink) >= nodesize);
|
2022-01-06 13:44:17 +01:00
|
|
|
|
2023-04-17 17:01:23 +02:00
|
|
|
if (pid <= MM_BACKTRACE_FREE_PID)
|
2022-01-06 13:44:17 +01:00
|
|
|
{
|
2022-01-20 10:17:29 +01:00
|
|
|
syslog(LOG_INFO, "%12zu%*p\n",
|
2022-12-15 12:55:16 +01:00
|
|
|
nodesize, MM_PTR_FMT_WIDTH,
|
2022-01-20 10:17:29 +01:00
|
|
|
((FAR char *)node + SIZEOF_MM_ALLOCNODE));
|
2022-01-06 13:44:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mm_memdump
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* mm_memdump returns a memory info about specified pid of task/thread.
|
2022-01-20 10:17:29 +01:00
|
|
|
* if pid equals -1, this function will dump all allocated node and output
|
|
|
|
* backtrace for every allocated node for this heap, if pid equals -2, this
|
|
|
|
* function will dump all free node for this heap, and if pid is greater
|
|
|
|
* than or equal to 0, will dump pid allocated node and output backtrace.
|
2022-01-06 13:44:17 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void mm_memdump(FAR struct mm_heap_s *heap, pid_t pid)
|
|
|
|
{
|
2022-11-20 08:00:44 +01:00
|
|
|
struct mallinfo_task info;
|
2022-01-06 13:44:17 +01:00
|
|
|
|
2022-01-20 10:17:29 +01:00
|
|
|
if (pid >= -1)
|
2022-01-06 13:44:17 +01:00
|
|
|
{
|
|
|
|
syslog(LOG_INFO, "Dump all used memory node info:\n");
|
2022-07-04 09:41:06 +02:00
|
|
|
#if CONFIG_MM_BACKTRACE < 0
|
2022-01-20 10:17:29 +01:00
|
|
|
syslog(LOG_INFO, "%12s%*s\n", "Size", MM_PTR_FMT_WIDTH, "Address");
|
|
|
|
#else
|
|
|
|
syslog(LOG_INFO, "%6s%12s%*s %s\n", "PID", "Size", MM_PTR_FMT_WIDTH,
|
|
|
|
"Address", "Backtrace");
|
|
|
|
#endif
|
2022-01-06 13:44:17 +01:00
|
|
|
}
|
2022-01-20 10:17:29 +01:00
|
|
|
else
|
2022-01-06 13:44:17 +01:00
|
|
|
{
|
|
|
|
syslog(LOG_INFO, "Dump all free memory node info:\n");
|
2022-01-20 10:17:29 +01:00
|
|
|
syslog(LOG_INFO, "%12s%*s\n", "Size", MM_PTR_FMT_WIDTH, "Address");
|
2022-01-06 13:44:17 +01:00
|
|
|
}
|
|
|
|
|
2022-11-06 16:27:44 +01:00
|
|
|
#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
|
2022-11-26 16:44:38 +01:00
|
|
|
mempool_multiple_memdump(heap->mm_mpool, pid);
|
2022-11-06 16:27:44 +01:00
|
|
|
#endif
|
2022-11-20 08:00:44 +01:00
|
|
|
mm_foreach(heap, memdump_handler, &pid);
|
2022-01-06 13:44:17 +01:00
|
|
|
|
2022-11-20 08:00:44 +01:00
|
|
|
info.pid = pid;
|
|
|
|
mm_mallinfo_task(heap, &info);
|
2022-01-06 13:44:17 +01:00
|
|
|
syslog(LOG_INFO, "%12s%12s\n", "Total Blks", "Total Size");
|
2022-11-20 08:00:44 +01:00
|
|
|
syslog(LOG_INFO, "%12d%12d\n", info.aordblks, info.uordblks);
|
2022-01-06 13:44:17 +01:00
|
|
|
}
|