[gimp] app: Add get_backtrace() function to base-utils.[ch]



commit 98463cf081f3d59dbbff1f2803df2f213e7940d1
Author: Martin Nordholts <martinn src gnome org>
Date:   Thu Feb 25 18:02:04 2010 +0100

    app: Add get_backtrace() function to base-utils.[ch]

 app/base/base-utils.c |   45 +++++++++++++++++++++++++++++++++++++++++++++
 app/base/base-utils.h |    1 +
 2 files changed, 46 insertions(+), 0 deletions(-)
---
diff --git a/app/base/base-utils.c b/app/base/base-utils.c
index 4704081..f323bb5 100644
--- a/app/base/base-utils.c
+++ b/app/base/base-utils.c
@@ -29,10 +29,18 @@
 #include <process.h>
 #endif
 
+#ifdef G_OS_UNIX
+/* For get_backtrace() */
+#include <stdlib.h>
+#include <string.h>
+#include <execinfo.h>
+#endif
+
 #include "base-utils.h"
 
 
 #define NUM_PROCESSORS_DEFAULT 1
+#define MAX_FUNC               100
 
 
 /*  public functions  */
@@ -93,3 +101,40 @@ get_physical_memory_size (void)
 
   return 0;
 }
+
+/**
+ * get_backtrace:
+ *
+ * Returns: The current stack trace. Free with g_free(). Mainly meant
+ * for debugging, for example storing the allocation stack traces for
+ * objects to hunt down leaks.
+ **/
+char *
+get_backtrace (void)
+{
+#ifdef G_OS_UNIX
+  void     *functions[MAX_FUNC];
+  char    **function_names;
+  int       n_functions;
+  int       i;
+  GString  *result;
+
+  /* Get symbols */
+  n_functions    = backtrace (functions, MAX_FUNC);
+  function_names = backtrace_symbols (functions, n_functions);
+
+  /* Construct stack trace */
+  result = g_string_new ("");
+  for (i = 0; i < n_functions; i++)
+    g_string_append_printf (result, "%s\n", function_names[i]);
+
+  /* We must not free the function names themselves, we only need to
+   * free the array that points to them
+   */
+  free (function_names);
+
+  return g_string_free (result, FALSE/*free_segment*/);
+#else
+  return g_strdup ("backtrace() only available with GNU libc\n");
+#endif
+}
diff --git a/app/base/base-utils.h b/app/base/base-utils.h
index 9e8ae1d..7888904 100644
--- a/app/base/base-utils.h
+++ b/app/base/base-utils.h
@@ -22,6 +22,7 @@
 gint     get_pid                  (void);
 gint     get_number_of_processors (void);
 guint64  get_physical_memory_size (void);
+char   * get_backtrace            (void);
 
 
 #endif /* __BASE_H__ */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]