+ 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");