+
+/**
+ * gst_info_strdup_vprintf:
+ * @format: a printf style format string
+ * @args: the va_list of printf arguments for @format
+ *
+ * Allocates, fills and returns a null terminated string from the printf style
+ * @format string and @args.
+ *
+ * See gst_info_vasprintf() for when this function is required.
+ *
+ * Free with g_free().
+ *
+ * Returns: (nullable): a newly allocated null terminated string or %NULL on any error
+ *
+ * Since: 1.8
+ */
+gchar *
+gst_info_strdup_vprintf (const gchar * format, va_list args)
+{
+ gchar *ret;
+
+ if (gst_info_vasprintf (&ret, format, args) < 0)
+ ret = NULL;
+
+ return ret;
+}
+
+/**
+ * gst_info_strdup_printf:
+ * @format: a printf style format string
+ * @...: the printf arguments for @format
+ *
+ * Allocates, fills and returns a 0-terminated string from the printf style
+ * @format string and corresponding arguments.
+ *
+ * See gst_info_vasprintf() for when this function is required.
+ *
+ * Free with g_free().
+ *
+ * Returns: (nullable): a newly allocated null terminated string or %NULL on any error
+ *
+ * Since: 1.8
+ */
+gchar *
+gst_info_strdup_printf (const gchar * format, ...)
+{
+ gchar *ret;
+ va_list args;
+
+ va_start (args, format);
+ ret = gst_info_strdup_vprintf (format, args);
+ va_end (args);
+
+ return ret;
+}
+
+/**
+ * gst_print:
+ * @format: a printf style format string
+ * @...: the printf arguments for @format
+ *
+ * Outputs a formatted message via the GLib print handler. The default print
+ * handler simply outputs the message to stdout.
+ *
+ * This function will not append a new-line character at the end, unlike
+ * gst_println() which will.
+ *
+ * All strings must be in ASCII or UTF-8 encoding.
+ *
+ * This function differs from g_print() in that it supports all the additional
+ * printf specifiers that are supported by GStreamer's debug logging system,
+ * such as #GST_PTR_FORMAT and #GST_SEGMENT_FORMAT.
+ *
+ * This function is primarily for printing debug output.
+ *
+ * Since: 1.12
+ */
+void
+gst_print (const gchar * format, ...)
+{
+ va_list args;
+ gchar *str;
+
+ va_start (args, format);
+ str = gst_info_strdup_vprintf (format, args);
+ va_end (args);
+
+ g_print ("%s", str);
+ g_free (str);
+}
+
+/**
+ * gst_println:
+ * @format: a printf style format string
+ * @...: the printf arguments for @format
+ *
+ * Outputs a formatted message via the GLib print handler. The default print
+ * handler simply outputs the message to stdout.
+ *
+ * This function will append a new-line character at the end, unlike
+ * gst_print() which will not.
+ *
+ * All strings must be in ASCII or UTF-8 encoding.
+ *
+ * This function differs from g_print() in that it supports all the additional
+ * printf specifiers that are supported by GStreamer's debug logging system,
+ * such as #GST_PTR_FORMAT and #GST_SEGMENT_FORMAT.
+ *
+ * This function is primarily for printing debug output.
+ *
+ * Since: 1.12
+ */
+void
+gst_println (const gchar * format, ...)
+{
+ va_list args;
+ gchar *str;
+
+ va_start (args, format);
+ str = gst_info_strdup_vprintf (format, args);
+ va_end (args);
+
+ g_print ("%s\n", str);
+ g_free (str);
+}
+
+/**
+ * gst_printerr:
+ * @format: a printf style format string
+ * @...: the printf arguments for @format
+ *
+ * Outputs a formatted message via the GLib error message handler. The default
+ * handler simply outputs the message to stderr.
+ *
+ * This function will not append a new-line character at the end, unlike
+ * gst_printerrln() which will.
+ *
+ * All strings must be in ASCII or UTF-8 encoding.
+ *
+ * This function differs from g_printerr() in that it supports the additional
+ * printf specifiers that are supported by GStreamer's debug logging system,
+ * such as #GST_PTR_FORMAT and #GST_SEGMENT_FORMAT.
+ *
+ * This function is primarily for printing debug output.
+ *
+ * Since: 1.12
+ */
+void
+gst_printerr (const gchar * format, ...)
+{
+ va_list args;
+ gchar *str;
+
+ va_start (args, format);
+ str = gst_info_strdup_vprintf (format, args);
+ va_end (args);
+
+ g_printerr ("%s", str);
+ g_free (str);
+}
+
+/**
+ * gst_printerrln:
+ * @format: a printf style format string
+ * @...: the printf arguments for @format
+ *
+ * Outputs a formatted message via the GLib error message handler. The default
+ * handler simply outputs the message to stderr.
+ *
+ * This function will append a new-line character at the end, unlike
+ * gst_printerr() which will not.
+ *
+ * All strings must be in ASCII or UTF-8 encoding.
+ *
+ * This function differs from g_printerr() in that it supports the additional
+ * printf specifiers that are supported by GStreamer's debug logging system,
+ * such as #GST_PTR_FORMAT and #GST_SEGMENT_FORMAT.
+ *
+ * This function is primarily for printing debug output.
+ *
+ * Since: 1.12
+ */
+void
+gst_printerrln (const gchar * format, ...)
+{
+ va_list args;
+ gchar *str;
+
+ va_start (args, format);
+ str = gst_info_strdup_vprintf (format, args);
+ va_end (args);
+
+ g_printerr ("%s\n", str);
+ g_free (str);
+}
+
+#ifdef HAVE_UNWIND
+#ifdef HAVE_DW
+static gboolean
+append_debug_info (GString * trace, Dwfl * dwfl, const void *ip)
+{
+ Dwfl_Line *line;
+ Dwarf_Addr addr;
+ Dwfl_Module *module;
+ const gchar *function_name;
+
+ if (dwfl_linux_proc_report (dwfl, getpid ()) != 0)
+ return FALSE;
+
+ if (dwfl_report_end (dwfl, NULL, NULL))
+ return FALSE;
+
+ addr = (uintptr_t) ip;
+ module = dwfl_addrmodule (dwfl, addr);
+ function_name = dwfl_module_addrname (module, addr);
+
+ g_string_append_printf (trace, "%s (", function_name ? function_name : "??");
+
+ line = dwfl_getsrc (dwfl, addr);
+ if (line != NULL) {
+ gint nline;
+ Dwarf_Addr addr;
+ const gchar *filename = dwfl_lineinfo (line, &addr,
+ &nline, NULL, NULL, NULL);
+
+ g_string_append_printf (trace, "%s:%d", strrchr (filename,
+ G_DIR_SEPARATOR) + 1, nline);
+ } else {
+ const gchar *eflfile = NULL;
+
+ dwfl_module_info (module, NULL, NULL, NULL, NULL, NULL, &eflfile, NULL);
+ g_string_append_printf (trace, "%s:%p", eflfile ? eflfile : "??", ip);
+ }
+
+ return TRUE;
+}
+#endif /* HAVE_DW */
+
+static gchar *
+generate_unwind_trace (GstStackTraceFlags flags)
+{
+ gint unret;
+ unw_context_t uc;
+ unw_cursor_t cursor;
+ gboolean use_libunwind = TRUE;
+ GString *trace = g_string_new (NULL);
+
+#ifdef HAVE_DW
+ Dwfl *dwfl = NULL;
+ Dwfl_Callbacks callbacks = {
+ .find_elf = dwfl_linux_proc_find_elf,
+ .find_debuginfo = dwfl_standard_find_debuginfo,
+ };
+
+ if ((flags & GST_STACK_TRACE_SHOW_FULL))
+ dwfl = dwfl_begin (&callbacks);
+#endif /* HAVE_DW */
+
+ unret = unw_getcontext (&uc);
+ if (unret) {
+ GST_DEBUG ("Could not get libunwind context (%d)", unret);
+
+ goto done;
+ }
+ unret = unw_init_local (&cursor, &uc);
+ if (unret) {
+ GST_DEBUG ("Could not init libunwind context (%d)", unret);
+
+ goto done;
+ }
+
+ while (unw_step (&cursor) > 0) {
+#ifdef HAVE_DW
+ if (dwfl) {
+ unw_word_t ip;
+
+ unret = unw_get_reg (&cursor, UNW_REG_IP, &ip);
+ if (unret) {
+ GST_DEBUG ("libunwind could read frame info (%d)", unret);
+
+ goto done;
+ }
+
+ if (append_debug_info (trace, dwfl, (void *) (ip - 4))) {
+ use_libunwind = FALSE;
+ g_string_append (trace, ")\n");
+ }
+ }
+#endif /* HAVE_DW */
+
+ if (use_libunwind) {
+ char name[32];
+
+ unw_word_t offset = 0;
+ unw_get_proc_name (&cursor, name, sizeof (name), &offset);
+ g_string_append_printf (trace, "%s (0x%" G_GSIZE_FORMAT ")\n", name,
+ (gsize) offset);
+ }
+ }
+
+done:
+#ifdef HAVE_DW
+ if (dwfl)
+ dwfl_end (dwfl);
+#endif
+
+ return g_string_free (trace, FALSE);
+}
+
+#endif /* HAVE_UNWIND */
+
+#ifdef HAVE_BACKTRACE
+static gchar *
+generate_backtrace_trace (void)
+{
+ int j, nptrs;
+ void *buffer[BT_BUF_SIZE];
+ char **strings;
+ GString *trace;
+
+ trace = g_string_new (NULL);
+ nptrs = backtrace (buffer, BT_BUF_SIZE);
+
+ strings = backtrace_symbols (buffer, nptrs);
+
+ if (!strings)
+ return NULL;
+
+ for (j = 0; j < nptrs; j++)
+ g_string_append_printf (trace, "%s\n", strings[j]);
+
+ free (strings);
+
+ return g_string_free (trace, FALSE);
+}
+#else
+#define generate_backtrace_trace() NULL
+#endif /* HAVE_BACKTRACE */
+
+#ifdef HAVE_DBGHELP
+static void
+dbghelp_initialize_symbols (HANDLE process)
+{
+ static gsize initialization_value = 0;
+
+ if (g_once_init_enter (&initialization_value)) {
+ GST_INFO ("Initializing Windows symbol handler");
+ SymSetOptions (SYMOPT_LOAD_LINES);
+ SymInitialize (process, NULL, TRUE);
+ GST_INFO ("Initialized Windows symbol handler");
+
+ g_once_init_leave (&initialization_value, 1);
+ }
+}
+
+static gchar *
+generate_dbghelp_trace (void)
+{
+ HANDLE process = GetCurrentProcess ();
+ HANDLE thread = GetCurrentThread ();
+ IMAGEHLP_MODULE64 module_info;
+ DWORD machine;
+ CONTEXT context;
+ STACKFRAME64 frame = { 0 };
+ PVOID save_context;
+ GString *trace = g_string_new (NULL);
+
+ dbghelp_initialize_symbols (process);
+
+ memset (&context, 0, sizeof (CONTEXT));
+ context.ContextFlags = CONTEXT_FULL;
+
+ RtlCaptureContext (&context);
+
+ frame.AddrPC.Mode = AddrModeFlat;
+ frame.AddrStack.Mode = AddrModeFlat;
+ frame.AddrFrame.Mode = AddrModeFlat;
+
+#if (defined _M_IX86)
+ machine = IMAGE_FILE_MACHINE_I386;
+ frame.AddrFrame.Offset = context.Ebp;
+ frame.AddrPC.Offset = context.Eip;
+ frame.AddrStack.Offset = context.Esp;
+#elif (defined _M_X64)
+ machine = IMAGE_FILE_MACHINE_AMD64;
+ frame.AddrFrame.Offset = context.Rbp;
+ frame.AddrPC.Offset = context.Rip;
+ frame.AddrStack.Offset = context.Rsp;
+#else
+ goto done;
+#endif
+
+ module_info.SizeOfStruct = sizeof (module_info);
+ save_context = (machine == IMAGE_FILE_MACHINE_I386) ? NULL : &context;
+
+ while (TRUE) {
+ char buffer[sizeof (SYMBOL_INFO) + MAX_SYM_NAME * sizeof (TCHAR)];
+ PSYMBOL_INFO symbol = (PSYMBOL_INFO) buffer;
+ IMAGEHLP_LINE64 line;
+ DWORD displacement = 0;
+
+ symbol->SizeOfStruct = sizeof (SYMBOL_INFO);
+ symbol->MaxNameLen = MAX_SYM_NAME;
+
+ line.SizeOfStruct = sizeof (line);
+
+ if (!StackWalk64 (machine, process, thread, &frame, save_context, 0,
+ SymFunctionTableAccess64, SymGetModuleBase64, 0))
+ break;
+
+ if (SymFromAddr (process, frame.AddrPC.Offset, 0, symbol))
+ g_string_append_printf (trace, "%s ", symbol->Name);
+ else
+ g_string_append (trace, "?? ");
+
+ if (SymGetLineFromAddr64 (process, frame.AddrPC.Offset, &displacement,
+ &line))
+ g_string_append_printf (trace, "(%s:%u)", line.FileName, line.LineNumber);
+ else if (SymGetModuleInfo64 (process, frame.AddrPC.Offset, &module_info))
+ g_string_append_printf (trace, "(%s)", module_info.ImageName);
+ else
+ g_string_append_printf (trace, "(%s)", "??");
+
+ g_string_append (trace, "\n");
+ }
+
+done:
+ return g_string_free (trace, FALSE);
+}
+#endif /* HAVE_DBGHELP */
+
+/**
+ * gst_debug_get_stack_trace:
+ * @flags: A set of #GstStackTraceFlags to determine how the stack
+ * trace should look like. Pass 0 to retrieve a minimal backtrace.
+ *
+ * Returns: (nullable): a stack trace, if libunwind or glibc backtrace are
+ * present, else %NULL.
+ *
+ * Since: 1.12
+ */
+gchar *
+gst_debug_get_stack_trace (GstStackTraceFlags flags)
+{
+ gchar *trace = NULL;
+#ifdef HAVE_BACKTRACE
+ gboolean have_backtrace = TRUE;
+#else
+ gboolean have_backtrace = FALSE;
+#endif
+
+#ifdef HAVE_UNWIND
+ if ((flags & GST_STACK_TRACE_SHOW_FULL) || !have_backtrace)
+ trace = generate_unwind_trace (flags);
+#endif /* HAVE_UNWIND */
+
+#ifdef HAVE_DBGHELP
+ trace = generate_dbghelp_trace ();
+#endif
+
+ if (trace)
+ return trace;
+ else if (have_backtrace)
+ return generate_backtrace_trace ();
+
+ return NULL;
+}
+
+/**
+ * gst_debug_print_stack_trace:
+ *
+ * If libunwind, glibc backtrace or DbgHelp are present
+ * a stack trace is printed.
+ */