You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ar...@apache.org on 2021/12/29 04:09:43 UTC

[incubator-nuttx] branch master updated: libc: Add backtrace_symbols[_fd] functions

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 20bbdd0  libc: Add backtrace_symbols[_fd] functions
20bbdd0 is described below

commit 20bbdd099780bd30558eb120453c720ff6a4f852
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Fri Dec 24 23:00:37 2021 +0800

    libc: Add backtrace_symbols[_fd] functions
    
    specified here:
    https://linux.die.net/man/3/backtrace_symbols
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 include/execinfo.h                                 |  3 +
 libs/libc/misc/Make.defs                           |  2 +-
 .../execinfo.h => libs/libc/misc/lib_execinfo.c    | 81 ++++++++++++++--------
 3 files changed, 56 insertions(+), 30 deletions(-)

diff --git a/include/execinfo.h b/include/execinfo.h
index 0f7df07..6380eaf 100644
--- a/include/execinfo.h
+++ b/include/execinfo.h
@@ -59,6 +59,9 @@ extern "C"
 #define EXTERN extern
 #endif
 
+FAR char **backtrace_symbols(FAR void *const *buffer, int size);
+void backtrace_symbols_fd(FAR void *const *buffer, int size, int fd);
+
 #undef EXTERN
 #if defined(__cplusplus)
 }
diff --git a/libs/libc/misc/Make.defs b/libs/libc/misc/Make.defs
index 74aea90..5710b7f 100644
--- a/libs/libc/misc/Make.defs
+++ b/libs/libc/misc/Make.defs
@@ -36,7 +36,7 @@ endif
 
 CSRCS += lib_dumpbuffer.c lib_dumpvbuffer.c lib_fnmatch.c lib_debug.c
 CSRCS += lib_crc64.c lib_crc32.c lib_crc16.c lib_crc8.c lib_crc8ccitt.c
-CSRCS += lib_crc8table.c lib_glob.c
+CSRCS += lib_crc8table.c lib_glob.c lib_execinfo.c
 
 # Keyboard driver encoder/decoder
 
diff --git a/include/execinfo.h b/libs/libc/misc/lib_execinfo.c
similarity index 58%
copy from include/execinfo.h
copy to libs/libc/misc/lib_execinfo.c
index 0f7df07..bee7f56 100644
--- a/include/execinfo.h
+++ b/libs/libc/misc/lib_execinfo.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * include/execinfo.h
+ * libs/libc/misc/lib_execinfo.c
  *
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
@@ -18,50 +18,73 @@
  *
  ****************************************************************************/
 
-#ifndef __INCLUDE_EXECINFO_H
-#define __INCLUDE_EXECINFO_H
-
 /****************************************************************************
  * Included Files
  ****************************************************************************/
 
-#include <sys/types.h>
-#include <sched.h>
+#include <execinfo.h>
+#include <stdio.h>
+
+#include "libc.h"
 
 /****************************************************************************
- * Pre-processor Definitions
+ * Private Functions
  ****************************************************************************/
 
-#if defined(CONFIG_SCHED_BACKTRACE)
+static FAR char **backtrace_malloc(FAR void *const *buffer, int size)
+{
+  size_t length = 0;
 
-/* Store up to SIZE return address of the current back trace in
- * ARRAY and return the exact number of values stored.
- */
+  if (size <= 0)
+    {
+      return NULL;
+    }
 
-#define backtrace(buffer, size) sched_backtrace(gettid(), buffer, size)
-#define dump_stack()            sched_dumpstack(gettid())
+  while (size-- > 0)
+    {
+      int ret = sprintf(NULL, "%pS", *buffer++);
+      if (ret < 0)
+        {
+          return NULL;
+        }
 
-#else
-# define backtrace(buffer, size) 0
-# define dump_stack()
-#endif
+      length += sizeof(FAR char *) + ret + 1;
+    }
+
+  return lib_malloc(length);
+}
 
 /****************************************************************************
- * Public Function Prototypes
+ * Public Functions
  ****************************************************************************/
 
-#undef EXTERN
-#if defined(__cplusplus)
-#define EXTERN extern "C"
-extern "C"
+FAR char **backtrace_symbols(FAR void *const *buffer, int size)
 {
-#else
-#define EXTERN extern
-#endif
+  FAR char **syms;
+  FAR char *buf;
+  int i;
 
-#undef EXTERN
-#if defined(__cplusplus)
+  syms = backtrace_malloc(buffer, size);
+  if (syms != NULL)
+    {
+      buf = syms[size];
+      for (i = 0; i < size; i++)
+        {
+          syms[i] = buf;
+          buf += sprintf(buf, "%pS", buffer[i]);
+          buf += 1;
+        }
+    }
+
+  return syms;
 }
-#endif
 
-#endif /* __INCLUDE_EXECINFO_H */
+void backtrace_symbols_fd(FAR void *const *buffer, int size, int fd)
+{
+  int i;
+
+  for (i = 0; i < size; i++)
+    {
+      dprintf(fd, "%pS\n", buffer[i]);
+    }
+}