You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by da...@apache.org on 2021/04/19 11:45:14 UTC

[incubator-nuttx] 02/05: libc/execinfo: add dump_stack support

This is an automated email from the ASF dual-hosted git repository.

davids5 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit 137eb594cb30b255e873974acccbde12a063ee27
Author: chao.an <an...@xiaomi.com>
AuthorDate: Wed Mar 31 11:28:07 2021 +0800

    libc/execinfo: add dump_stack support
    
    Signed-off-by: chao.an <an...@xiaomi.com>
---
 include/execinfo.h                                 |  8 +++-
 libs/libc/debug/Make.defs                          |  2 +-
 .../execinfo.h => libs/libc/debug/lib_dumpstack.c  | 52 ++++++++++++++++++----
 3 files changed, 51 insertions(+), 11 deletions(-)

diff --git a/include/execinfo.h b/include/execinfo.h
index bd47b7a..5fa5b35 100644
--- a/include/execinfo.h
+++ b/include/execinfo.h
@@ -25,10 +25,16 @@
  * Public Function Prototypes
  ****************************************************************************/
 
+#if defined(CONFIG_EABI_UNWINDER)
+
 /* Store up to SIZE return address of the current program state in
  * ARRAY and return the exact number of values stored.
  */
 
-extern int backtrace(FAR void **buffer, int size);
+extern int  backtrace(FAR void **buffer, int size);
+extern void dump_stack(void);
+#else
+# define dump_stack()
+#endif
 
 #endif /* __INCLUDE_EXECINFO_H */
diff --git a/libs/libc/debug/Make.defs b/libs/libc/debug/Make.defs
index 5d42e13..8b64610 100644
--- a/libs/libc/debug/Make.defs
+++ b/libs/libc/debug/Make.defs
@@ -20,7 +20,7 @@
 
 ifeq ($(CONFIG_EABI_UNWINDER),y)
 
-CSRCS += lib_backtrace.c
+CSRCS += lib_backtrace.c lib_dumpstack.c
 
 endif
 
diff --git a/include/execinfo.h b/libs/libc/debug/lib_dumpstack.c
similarity index 52%
copy from include/execinfo.h
copy to libs/libc/debug/lib_dumpstack.c
index bd47b7a..3984168 100644
--- a/include/execinfo.h
+++ b/libs/libc/debug/lib_dumpstack.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * include/execinfo.h
+ * libs/libc/debug/lib_dumpstack.c
  *
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
@@ -18,17 +18,51 @@
  *
  ****************************************************************************/
 
-#ifndef __INCLUDE_EXECINFO_H
-#define __INCLUDE_EXECINFO_H
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/types.h>
+
+#include <stdio.h>
+#include <syslog.h>
+#include <execinfo.h>
+
+#define DUMP_FORMAT "%*p"
+#define DUMP_WIDTH  (int)(2 * sizeof(FAR void *) + 3)
+
+#define DUMP_DEPTH  16
+#define DUMP_NITEM  8
+#define DUMP_LINESIZE (DUMP_NITEM * DUMP_WIDTH)
 
 /****************************************************************************
- * Public Function Prototypes
+ * Public Functions
  ****************************************************************************/
 
-/* Store up to SIZE return address of the current program state in
- * ARRAY and return the exact number of values stored.
- */
+void dump_stack(void)
+{
+  FAR void *address[DUMP_DEPTH];
+  char line[DUMP_LINESIZE];
+  int ret = 0;
+  int size;
+  int i;
 
-extern int backtrace(FAR void **buffer, int size);
+  size = backtrace(address, DUMP_DEPTH);
+  if (size <= 0)
+    {
+      return;
+    }
 
-#endif /* __INCLUDE_EXECINFO_H */
+  for (i = 0; i < size; i++)
+    {
+      ret += snprintf(line + ret, sizeof(line) - ret,
+                      DUMP_FORMAT, DUMP_WIDTH, address[i]);
+      if (i == size - 1 || ret % DUMP_LINESIZE == 0)
+        {
+          syslog(LOG_INFO, "[BackTrace]: %s\n", line);
+          ret = 0;
+        }
+    }
+}