lib/util: Move log_stack_trace() to common code
authorAndrew Bartlett <abartlet@samba.org>
Tue, 10 Apr 2018 04:35:07 +0000 (16:35 +1200)
committerJeremy Allison <jra@samba.org>
Tue, 10 Apr 2018 23:06:39 +0000 (01:06 +0200)
Signed-off-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
lib/util/fault.c
lib/util/fault.h
lib/util/wscript_configure
source3/include/local.h
source3/include/proto.h
source3/lib/util.c
source3/wscript

index fef82a3fcf7be47f2e7b076e1eddac1f3675794b..e539aff9e7b4762092b01358628550abf66e0f70 100644 (file)
@@ -3,6 +3,7 @@
    Critical Fault handling
    Copyright (C) Andrew Tridgell 1992-1998
    Copyright (C) Tim Prouty 2009
+   Copyright (C) James Peach 2006
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -170,3 +171,106 @@ _PUBLIC_ void smb_panic(const char *why)
        }
        smb_panic_default(why);
 }
+
+
+
+/*******************************************************************
+ Print a backtrace of the stack to the debug log. This function
+ DELIBERATELY LEAKS MEMORY. The expectation is that you should
+ exit shortly after calling it.
+********************************************************************/
+
+/* Buffer size to use when printing backtraces */
+#define BACKTRACE_STACK_SIZE 64
+
+
+#ifdef HAVE_LIBUNWIND_H
+#include <libunwind.h>
+#endif
+
+#ifdef HAVE_EXECINFO_H
+#include <execinfo.h>
+#endif
+
+void log_stack_trace(void)
+{
+#ifdef HAVE_LIBUNWIND
+       /* Try to use libunwind before any other technique since on ia64
+        * libunwind correctly walks the stack in more circumstances than
+        * backtrace.
+        */
+       unw_cursor_t cursor;
+       unw_context_t uc;
+       unsigned i = 0;
+
+       char procname[256];
+       unw_word_t ip, sp, off;
+
+       procname[sizeof(procname) - 1] = '\0';
+
+       if (unw_getcontext(&uc) != 0) {
+               goto libunwind_failed;
+       }
+
+       if (unw_init_local(&cursor, &uc) != 0) {
+               goto libunwind_failed;
+       }
+
+       DEBUG(0, ("BACKTRACE:\n"));
+
+       do {
+           ip = sp = 0;
+           unw_get_reg(&cursor, UNW_REG_IP, &ip);
+           unw_get_reg(&cursor, UNW_REG_SP, &sp);
+
+           switch (unw_get_proc_name(&cursor,
+                       procname, sizeof(procname) - 1, &off) ) {
+           case 0:
+                   /* Name found. */
+           case -UNW_ENOMEM:
+                   /* Name truncated. */
+                   DEBUGADD(0, (" #%u %s + %#llx [ip=%#llx] [sp=%#llx]\n",
+                           i, procname, (long long)off,
+                           (long long)ip, (long long) sp));
+                   break;
+           default:
+           /* case -UNW_ENOINFO: */
+           /* case -UNW_EUNSPEC: */
+                   /* No symbol name found. */
+                   DEBUGADD(0, (" #%u %s [ip=%#llx] [sp=%#llx]\n",
+                           i, "<unknown symbol>",
+                           (long long)ip, (long long) sp));
+           }
+           ++i;
+       } while (unw_step(&cursor) > 0);
+
+       return;
+
+libunwind_failed:
+       DEBUG(0, ("unable to produce a stack trace with libunwind\n"));
+
+#elif HAVE_BACKTRACE_SYMBOLS
+       void *backtrace_stack[BACKTRACE_STACK_SIZE];
+       size_t backtrace_size;
+       char **backtrace_strings;
+
+       /* get the backtrace (stack frames) */
+       backtrace_size = backtrace(backtrace_stack,BACKTRACE_STACK_SIZE);
+       backtrace_strings = backtrace_symbols(backtrace_stack, backtrace_size);
+
+       DEBUG(0, ("BACKTRACE: %lu stack frames:\n",
+                 (unsigned long)backtrace_size));
+
+       if (backtrace_strings) {
+               int i;
+
+               for (i = 0; i < backtrace_size; i++)
+                       DEBUGADD(0, (" #%u %s\n", i, backtrace_strings[i]));
+
+               /* Leak the backtrace_strings, rather than risk what free() might do */
+       }
+
+#else
+       DEBUG(0, ("unable to produce a stack trace on this platform\n"));
+#endif
+}
index 0ac6cb9ceb5a428fe130a339ac0c9e9f7f27b298..dfa339b7650980a1ee9c30651e3cd32457f179fa 100644 (file)
@@ -53,5 +53,6 @@ void fault_setup(void);
 void fault_setup_disable(void);
 _NORETURN_ void smb_panic(const char *reason);
 
+void log_stack_trace(void);
 
 #endif /* _SAMBA_FAULT_H_ */
index 8e5a59c84803d8518eac3e25a92bb3219a3d62eb..2a8dbef699b1ebde90f1b475fd2eb2ffb2002313 100644 (file)
@@ -6,6 +6,7 @@ if Options.options.disable_fault_handling:
 
 # backtrace could be in libexecinfo or in libc
 conf.CHECK_FUNCS_IN('backtrace backtrace_symbols', 'execinfo', checklibc=True, headers='execinfo.h')
+conf.CHECK_HEADERS('execinfo.h libunwind.h')
 
 conf.CHECK_STRUCTURE_MEMBER('struct statvfs', 'f_frsize', define='HAVE_FRSIZE', headers='sys/statvfs.h')
 
index 7f97d4ece9c7628084a76610a461a384c609de18..c2be1ff3b7fd57c4df9e4e6771876cf42c401337 100644 (file)
 /* Number in seconds for winbindd to wait for the mutex. Make this 2 * smbd wait time. */
 #define WINBIND_SERVER_MUTEX_WAIT_TIME (( ((NUM_CLI_AUTH_CONNECT_RETRIES) * ((CLI_AUTH_TIMEOUT)/1000)) + 5)*2)
 
-/* Buffer size to use when printing backtraces */
-#define BACKTRACE_STACK_SIZE 64
-
 /* size of listen() backlog in smbd */
 #define SMBD_LISTEN_BACKLOG 50
 
index 4c51e4d30599535c0ade5ad97547a7f27189e965..fea4ba51be5a0c4846275e424478c68dc598098d 100644 (file)
@@ -356,7 +356,6 @@ char *gidtoname(gid_t gid);
 uid_t nametouid(const char *name);
 gid_t nametogid(const char *name);
 void smb_panic_s3(const char *why);
-void log_stack_trace(void);
 const char *readdirname(DIR *p);
 bool is_in_path(const char *name, name_compare_entry *namelist, bool case_sensitive);
 void set_namearray(name_compare_entry **ppname_array, const char *namelist);
index 98c47b3df9f8e9a59932d76c045f23f715556fa4..5f786f95d3e2eccd113a39eec585d195713c5747 100644 (file)
@@ -838,103 +838,6 @@ void smb_panic_s3(const char *why)
        dump_core();
 }
 
-/*******************************************************************
- Print a backtrace of the stack to the debug log. This function
- DELIBERATELY LEAKS MEMORY. The expectation is that you should
- exit shortly after calling it.
-********************************************************************/
-
-#ifdef HAVE_LIBUNWIND_H
-#include <libunwind.h>
-#endif
-
-#ifdef HAVE_EXECINFO_H
-#include <execinfo.h>
-#endif
-
-void log_stack_trace(void)
-{
-#ifdef HAVE_LIBUNWIND
-       /* Try to use libunwind before any other technique since on ia64
-        * libunwind correctly walks the stack in more circumstances than
-        * backtrace.
-        */ 
-       unw_cursor_t cursor;
-       unw_context_t uc;
-       unsigned i = 0;
-
-       char procname[256];
-       unw_word_t ip, sp, off;
-
-       procname[sizeof(procname) - 1] = '\0';
-
-       if (unw_getcontext(&uc) != 0) {
-               goto libunwind_failed;
-       }
-
-       if (unw_init_local(&cursor, &uc) != 0) {
-               goto libunwind_failed;
-       }
-
-       DEBUG(0, ("BACKTRACE:\n"));
-
-       do {
-           ip = sp = 0;
-           unw_get_reg(&cursor, UNW_REG_IP, &ip);
-           unw_get_reg(&cursor, UNW_REG_SP, &sp);
-
-           switch (unw_get_proc_name(&cursor,
-                       procname, sizeof(procname) - 1, &off) ) {
-           case 0:
-                   /* Name found. */
-           case -UNW_ENOMEM:
-                   /* Name truncated. */
-                   DEBUGADD(0, (" #%u %s + %#llx [ip=%#llx] [sp=%#llx]\n",
-                           i, procname, (long long)off,
-                           (long long)ip, (long long) sp));
-                   break;
-           default:
-           /* case -UNW_ENOINFO: */
-           /* case -UNW_EUNSPEC: */
-                   /* No symbol name found. */
-                   DEBUGADD(0, (" #%u %s [ip=%#llx] [sp=%#llx]\n",
-                           i, "<unknown symbol>",
-                           (long long)ip, (long long) sp));
-           }
-           ++i;
-       } while (unw_step(&cursor) > 0);
-
-       return;
-
-libunwind_failed:
-       DEBUG(0, ("unable to produce a stack trace with libunwind\n"));
-
-#elif HAVE_BACKTRACE_SYMBOLS
-       void *backtrace_stack[BACKTRACE_STACK_SIZE];
-       size_t backtrace_size;
-       char **backtrace_strings;
-
-       /* get the backtrace (stack frames) */
-       backtrace_size = backtrace(backtrace_stack,BACKTRACE_STACK_SIZE);
-       backtrace_strings = backtrace_symbols(backtrace_stack, backtrace_size);
-
-       DEBUG(0, ("BACKTRACE: %lu stack frames:\n", 
-                 (unsigned long)backtrace_size));
-
-       if (backtrace_strings) {
-               int i;
-
-               for (i = 0; i < backtrace_size; i++)
-                       DEBUGADD(0, (" #%u %s\n", i, backtrace_strings[i]));
-
-               /* Leak the backtrace_strings, rather than risk what free() might do */
-       }
-
-#else
-       DEBUG(0, ("unable to produce a stack trace on this platform\n"));
-#endif
-}
-
 /*******************************************************************
   A readdir wrapper which just returns the file name.
  ********************************************************************/
index 8d658b2f53e20e7fba26afedd761511bf7fc66f1..ab64e80214ead976af2a5be702b2c0b4add8b400 100644 (file)
@@ -96,7 +96,7 @@ def configure(conf):
     # We crash without vfs_default
     required_static_modules.extend(TO_LIST('vfs_default'))
 
-    conf.CHECK_HEADERS('execinfo.h libunwind.h netdb.h')
+    conf.CHECK_HEADERS('netdb.h')
     conf.CHECK_HEADERS('linux/falloc.h linux/ioctl.h')
 
     conf.CHECK_FUNCS('getcwd fchown chmod fchmod mknod')