2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
4 * 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
5 * Copyright (C) 2008-2009 Tim-Philipp Müller <tim centricular net>
7 * gstinfo.c: debugging functions
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
27 * @short_description: Debugging and logging facilities
28 * @see_also: #gst-running for command line parameters
29 * and environment variables that affect the debugging output.
31 * GStreamer's debugging subsystem is an easy way to get information about what
32 * the application is doing. It is not meant for programming errors. Use GLib
33 * methods (g_warning and friends) for that.
35 * The debugging subsystem works only after GStreamer has been initialized
36 * - for example by calling gst_init().
38 * The debugging subsystem is used to log informational messages while the
39 * application runs. Each messages has some properties attached to it. Among
40 * these properties are the debugging category, the severity (called "level"
41 * here) and an optional #GObject it belongs to. Each of these messages is sent
42 * to all registered debugging handlers, which then handle the messages.
43 * GStreamer attaches a default handler on startup, which outputs requested
46 * Messages are output by using shortcut macros like #GST_DEBUG,
47 * #GST_CAT_ERROR_OBJECT or similar. These all expand to calling gst_debug_log()
48 * with the right parameters.
49 * The only thing a developer will probably want to do is define his own
50 * categories. This is easily done with 3 lines. At the top of your code,
52 * the variables and set the default category.
53 * |[<!-- language="C" -->
54 * GST_DEBUG_CATEGORY_STATIC (my_category); // define category (statically)
55 * #define GST_CAT_DEFAULT my_category // set as default
57 * After that you only need to initialize the category.
58 * |[<!-- language="C" -->
59 * GST_DEBUG_CATEGORY_INIT (my_category, "my category",
60 * 0, "This is my very own");
62 * Initialization must be done before the category is used first.
64 * in their plugin_init function, libraries and applications should do that
65 * during their initialization.
67 * The whole debugging subsystem can be disabled at build time with passing the
68 * --disable-gst-debug switch to configure. If this is done, every function,
69 * macro and even structs described in this file evaluate to default values or
71 * So don't take addresses of these functions or use other tricks.
72 * If you must do that for some reason, there is still an option.
74 * subsystem was compiled out, #GST_DISABLE_GST_DEBUG is defined in
76 * so you can check that before doing your trick.
77 * Disabling the debugging subsystem will give you a slight (read: unnoticeable)
78 * speed increase and will reduce the size of your compiled code. The GStreamer
79 * library itself becomes around 10% smaller.
81 * Please note that there are naming conventions for the names of debugging
82 * categories. These are explained at GST_DEBUG_CATEGORY_INIT().
86 #include "gst_private.h"
89 #undef gst_debug_remove_log_function
90 #undef gst_debug_add_log_function
92 #ifndef GST_DISABLE_GST_DEBUG
96 #include <stdio.h> /* fprintf */
97 #include <glib/gstdio.h>
100 # include <unistd.h> /* getpid on UNIX */
102 #ifdef HAVE_PROCESS_H
103 # include <process.h> /* getpid on win32 */
105 #include <string.h> /* G_VA_COPY */
107 # define WIN32_LEAN_AND_MEAN /* prevents from including too many things */
108 # include <windows.h> /* GetStdHandle, windows console */
111 #include "gst_private.h"
112 #include "gstutils.h"
113 #include "gstquark.h"
114 #include "gstsegment.h"
115 #include "gstvalue.h"
116 #include "gstcapsfeatures.h"
118 #ifdef HAVE_VALGRIND_VALGRIND_H
119 # include <valgrind/valgrind.h>
121 #include <glib/gprintf.h> /* g_sprintf */
123 /* our own printf implementation with custom extensions to %p for caps etc. */
124 #include "printf/printf.h"
125 #include "printf/printf-extension.h"
127 static char *gst_info_printf_pointer_extension_func (const char *format,
129 #else /* GST_DISABLE_GST_DEBUG */
131 #include <glib/gprintf.h>
132 #endif /* !GST_DISABLE_GST_DEBUG */
135 /* No need for remote debugging so turn on the 'local only' optimizations in
137 #define UNW_LOCAL_ONLY
139 #include <libunwind.h>
148 #include <elfutils/libdwfl.h>
150 #endif /* HAVE_UNWIND */
152 #ifdef HAVE_BACKTRACE
153 #include <execinfo.h>
154 #define BT_BUF_SIZE 100
155 #endif /* HAVE_BACKTRACE */
157 extern gboolean gst_is_initialized (void);
159 /* we want these symbols exported even if debug is disabled, to maintain
160 * ABI compatibility. Unless GST_REMOVE_DISABLED is defined. */
161 #if !defined(GST_DISABLE_GST_DEBUG) || !defined(GST_REMOVE_DISABLED)
163 /* disabled by default, as soon as some threshold is set > NONE,
164 * it becomes enabled. */
165 gboolean _gst_debug_enabled = FALSE;
166 GstDebugLevel _gst_debug_min = GST_LEVEL_NONE;
168 GstDebugCategory *GST_CAT_DEFAULT = NULL;
170 GstDebugCategory *GST_CAT_GST_INIT = NULL;
171 GstDebugCategory *GST_CAT_MEMORY = NULL;
172 GstDebugCategory *GST_CAT_PARENTAGE = NULL;
173 GstDebugCategory *GST_CAT_STATES = NULL;
174 GstDebugCategory *GST_CAT_SCHEDULING = NULL;
176 GstDebugCategory *GST_CAT_BUFFER = NULL;
177 GstDebugCategory *GST_CAT_BUFFER_LIST = NULL;
178 GstDebugCategory *GST_CAT_BUS = NULL;
179 GstDebugCategory *GST_CAT_CAPS = NULL;
180 GstDebugCategory *GST_CAT_CLOCK = NULL;
181 GstDebugCategory *GST_CAT_ELEMENT_PADS = NULL;
182 GstDebugCategory *GST_CAT_PADS = NULL;
183 GstDebugCategory *GST_CAT_PERFORMANCE = NULL;
184 GstDebugCategory *GST_CAT_PIPELINE = NULL;
185 GstDebugCategory *GST_CAT_PLUGIN_LOADING = NULL;
186 GstDebugCategory *GST_CAT_PLUGIN_INFO = NULL;
187 GstDebugCategory *GST_CAT_PROPERTIES = NULL;
188 GstDebugCategory *GST_CAT_NEGOTIATION = NULL;
189 GstDebugCategory *GST_CAT_REFCOUNTING = NULL;
190 GstDebugCategory *GST_CAT_ERROR_SYSTEM = NULL;
191 GstDebugCategory *GST_CAT_EVENT = NULL;
192 GstDebugCategory *GST_CAT_MESSAGE = NULL;
193 GstDebugCategory *GST_CAT_PARAMS = NULL;
194 GstDebugCategory *GST_CAT_CALL_TRACE = NULL;
195 GstDebugCategory *GST_CAT_SIGNAL = NULL;
196 GstDebugCategory *GST_CAT_PROBE = NULL;
197 GstDebugCategory *GST_CAT_REGISTRY = NULL;
198 GstDebugCategory *GST_CAT_QOS = NULL;
199 GstDebugCategory *_priv_GST_CAT_POLL = NULL;
200 GstDebugCategory *GST_CAT_META = NULL;
201 GstDebugCategory *GST_CAT_LOCKING = NULL;
202 GstDebugCategory *GST_CAT_CONTEXT = NULL;
203 GstDebugCategory *_priv_GST_CAT_PROTECTION = NULL;
206 #endif /* !defined(GST_DISABLE_GST_DEBUG) || !defined(GST_REMOVE_DISABLED) */
208 #ifndef GST_DISABLE_GST_DEBUG
210 /* underscore is to prevent conflict with GST_CAT_DEBUG define */
211 GST_DEBUG_CATEGORY_STATIC (_GST_CAT_DEBUG);
215 #include <rld_interface.h>
216 typedef struct DL_INFO
218 const char *dli_fname;
220 const char *dli_sname;
224 long dli_reserved[4];
228 #define _RLD_DLADDR 14
229 int dladdr (void *address, Dl_info * dl);
232 dladdr (void *address, Dl_info * dl)
236 v = _rld_new_interface (_RLD_DLADDR, address, dl);
242 static void gst_debug_reset_threshold (gpointer category, gpointer unused);
243 static void gst_debug_reset_all_thresholds (void);
245 struct _GstDebugMessage
252 /* list of all name/level pairs from --gst-debug and GST_DEBUG */
253 static GMutex __level_name_mutex;
254 static GSList *__level_name = NULL;
262 /* list of all categories */
263 static GMutex __cat_mutex;
264 static GSList *__categories = NULL;
266 static GstDebugCategory *_gst_debug_get_category_locked (const gchar * name);
269 /* all registered debug handlers */
274 GDestroyNotify notify;
277 static GMutex __log_func_mutex;
278 static GSList *__log_functions = NULL;
280 /* whether to add the default log function in gst_init() */
281 static gboolean add_default_log_func = TRUE;
283 #define PRETTY_TAGS_DEFAULT TRUE
284 static gboolean pretty_tags = PRETTY_TAGS_DEFAULT;
286 static volatile gint G_GNUC_MAY_ALIAS __default_level = GST_LEVEL_DEFAULT;
287 static volatile gint G_GNUC_MAY_ALIAS __use_color = GST_DEBUG_COLOR_MODE_ON;
289 /* FIXME: export this? */
291 _priv_gst_in_valgrind (void)
299 in_valgrind = GST_VG_UNCHECKED;
301 if (in_valgrind == GST_VG_UNCHECKED) {
302 #ifdef HAVE_VALGRIND_VALGRIND_H
303 if (RUNNING_ON_VALGRIND) {
304 GST_CAT_INFO (GST_CAT_GST_INIT, "we're running inside valgrind");
305 in_valgrind = GST_VG_INSIDE;
307 GST_CAT_LOG (GST_CAT_GST_INIT, "not doing extra valgrind stuff");
308 in_valgrind = GST_VG_NO_VALGRIND;
311 in_valgrind = GST_VG_NO_VALGRIND;
313 g_assert (in_valgrind == GST_VG_NO_VALGRIND ||
314 in_valgrind == GST_VG_INSIDE);
316 return (in_valgrind == GST_VG_INSIDE);
320 _replace_pattern_in_gst_debug_file_name (gchar * name, const char * token, guint val)
323 if ((token_start = strstr (name, token))) {
324 gsize token_len = strlen (token);
325 gchar * name_prefix = name;
326 gchar * name_suffix = token_start + token_len;
327 token_start[0] = '\0';
328 name = g_strdup_printf ("%s%u%s", name_prefix, val, name_suffix);
329 g_free (name_prefix);
335 _priv_gst_debug_file_name (const gchar * env)
339 name = g_strdup (env);
340 name = _replace_pattern_in_gst_debug_file_name (name, "%p", getpid ());
341 name = _replace_pattern_in_gst_debug_file_name (name, "%r", g_random_int ());
346 /* Initialize the debugging system */
348 _priv_gst_debug_init (void)
353 if (add_default_log_func) {
354 env = g_getenv ("GST_DEBUG_FILE");
355 if (env != NULL && *env != '\0') {
356 if (strcmp (env, "-") == 0) {
359 gchar *name = _priv_gst_debug_file_name (env);
360 log_file = g_fopen (name, "w");
362 if (log_file == NULL) {
363 g_printerr ("Could not open log file '%s' for writing: %s\n", env,
372 gst_debug_add_log_function (gst_debug_log_default, log_file, NULL);
375 __gst_printf_pointer_extension_set_func
376 (gst_info_printf_pointer_extension_func);
378 /* do NOT use a single debug function before this line has been run */
379 GST_CAT_DEFAULT = _gst_debug_category_new ("default",
380 GST_DEBUG_UNDERLINE, NULL);
381 _GST_CAT_DEBUG = _gst_debug_category_new ("GST_DEBUG",
382 GST_DEBUG_BOLD | GST_DEBUG_FG_YELLOW, "debugging subsystem");
384 /* FIXME: add descriptions here */
385 GST_CAT_GST_INIT = _gst_debug_category_new ("GST_INIT",
386 GST_DEBUG_BOLD | GST_DEBUG_FG_RED, NULL);
387 GST_CAT_MEMORY = _gst_debug_category_new ("GST_MEMORY",
388 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, "memory");
389 GST_CAT_PARENTAGE = _gst_debug_category_new ("GST_PARENTAGE",
390 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
391 GST_CAT_STATES = _gst_debug_category_new ("GST_STATES",
392 GST_DEBUG_BOLD | GST_DEBUG_FG_RED, NULL);
393 GST_CAT_SCHEDULING = _gst_debug_category_new ("GST_SCHEDULING",
394 GST_DEBUG_BOLD | GST_DEBUG_FG_MAGENTA, NULL);
395 GST_CAT_BUFFER = _gst_debug_category_new ("GST_BUFFER",
396 GST_DEBUG_BOLD | GST_DEBUG_BG_GREEN, NULL);
397 GST_CAT_BUFFER_LIST = _gst_debug_category_new ("GST_BUFFER_LIST",
398 GST_DEBUG_BOLD | GST_DEBUG_BG_GREEN, NULL);
399 GST_CAT_BUS = _gst_debug_category_new ("GST_BUS", GST_DEBUG_BG_YELLOW, NULL);
400 GST_CAT_CAPS = _gst_debug_category_new ("GST_CAPS",
401 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
402 GST_CAT_CLOCK = _gst_debug_category_new ("GST_CLOCK",
403 GST_DEBUG_BOLD | GST_DEBUG_FG_YELLOW, NULL);
404 GST_CAT_ELEMENT_PADS = _gst_debug_category_new ("GST_ELEMENT_PADS",
405 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
406 GST_CAT_PADS = _gst_debug_category_new ("GST_PADS",
407 GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_RED, NULL);
408 GST_CAT_PERFORMANCE = _gst_debug_category_new ("GST_PERFORMANCE",
409 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
410 GST_CAT_PIPELINE = _gst_debug_category_new ("GST_PIPELINE",
411 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
412 GST_CAT_PLUGIN_LOADING = _gst_debug_category_new ("GST_PLUGIN_LOADING",
413 GST_DEBUG_BOLD | GST_DEBUG_FG_CYAN, NULL);
414 GST_CAT_PLUGIN_INFO = _gst_debug_category_new ("GST_PLUGIN_INFO",
415 GST_DEBUG_BOLD | GST_DEBUG_FG_CYAN, NULL);
416 GST_CAT_PROPERTIES = _gst_debug_category_new ("GST_PROPERTIES",
417 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_BLUE, NULL);
418 GST_CAT_NEGOTIATION = _gst_debug_category_new ("GST_NEGOTIATION",
419 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
420 GST_CAT_REFCOUNTING = _gst_debug_category_new ("GST_REFCOUNTING",
421 GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_BLUE, NULL);
422 GST_CAT_ERROR_SYSTEM = _gst_debug_category_new ("GST_ERROR_SYSTEM",
423 GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_WHITE, NULL);
425 GST_CAT_EVENT = _gst_debug_category_new ("GST_EVENT",
426 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
427 GST_CAT_MESSAGE = _gst_debug_category_new ("GST_MESSAGE",
428 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
429 GST_CAT_PARAMS = _gst_debug_category_new ("GST_PARAMS",
430 GST_DEBUG_BOLD | GST_DEBUG_FG_BLACK | GST_DEBUG_BG_YELLOW, NULL);
431 GST_CAT_CALL_TRACE = _gst_debug_category_new ("GST_CALL_TRACE",
432 GST_DEBUG_BOLD, NULL);
433 GST_CAT_SIGNAL = _gst_debug_category_new ("GST_SIGNAL",
434 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
435 GST_CAT_PROBE = _gst_debug_category_new ("GST_PROBE",
436 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "pad probes");
437 GST_CAT_REGISTRY = _gst_debug_category_new ("GST_REGISTRY", 0, "registry");
438 GST_CAT_QOS = _gst_debug_category_new ("GST_QOS", 0, "QoS");
439 _priv_GST_CAT_POLL = _gst_debug_category_new ("GST_POLL", 0, "poll");
440 GST_CAT_META = _gst_debug_category_new ("GST_META", 0, "meta");
441 GST_CAT_LOCKING = _gst_debug_category_new ("GST_LOCKING", 0, "locking");
442 GST_CAT_CONTEXT = _gst_debug_category_new ("GST_CONTEXT", 0, NULL);
443 _priv_GST_CAT_PROTECTION =
444 _gst_debug_category_new ("GST_PROTECTION", 0, "protection");
446 /* print out the valgrind message if we're in valgrind */
447 _priv_gst_in_valgrind ();
449 env = g_getenv ("GST_DEBUG_OPTIONS");
451 if (strstr (env, "full_tags") || strstr (env, "full-tags"))
453 else if (strstr (env, "pretty_tags") || strstr (env, "pretty-tags"))
457 if (g_getenv ("GST_DEBUG_NO_COLOR") != NULL)
458 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_OFF);
459 env = g_getenv ("GST_DEBUG_COLOR_MODE");
461 gst_debug_set_color_mode_from_string (env);
463 env = g_getenv ("GST_DEBUG");
465 gst_debug_set_threshold_from_string (env, FALSE);
469 /* we can't do this further above, because we initialize the GST_CAT_DEFAULT struct */
470 #define GST_CAT_DEFAULT _GST_CAT_DEBUG
474 * @category: category to log
475 * @level: level of the message is in
476 * @file: the file that emitted the message, usually the __FILE__ identifier
477 * @function: the function that emitted the message
478 * @line: the line from that the message was emitted, usually __LINE__
479 * @object: (transfer none) (allow-none): the object this message relates to,
481 * @format: a printf style format string
482 * @...: optional arguments for the format
484 * Logs the given message using the currently registered debugging handlers.
487 gst_debug_log (GstDebugCategory * category, GstDebugLevel level,
488 const gchar * file, const gchar * function, gint line,
489 GObject * object, const gchar * format, ...)
493 va_start (var_args, format);
494 gst_debug_log_valist (category, level, file, function, line, object, format,
499 /* based on g_basename(), which we can't use because it was deprecated */
500 static inline const gchar *
501 gst_path_basename (const gchar * file_name)
503 register const gchar *base;
505 base = strrchr (file_name, G_DIR_SEPARATOR);
508 const gchar *q = strrchr (file_name, '/');
509 if (base == NULL || (q != NULL && q > base))
516 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
517 return file_name + 2;
523 * gst_debug_log_valist:
524 * @category: category to log
525 * @level: level of the message is in
526 * @file: the file that emitted the message, usually the __FILE__ identifier
527 * @function: the function that emitted the message
528 * @line: the line from that the message was emitted, usually __LINE__
529 * @object: (transfer none) (allow-none): the object this message relates to,
531 * @format: a printf style format string
532 * @args: optional arguments for the format
534 * Logs the given message using the currently registered debugging handlers.
537 gst_debug_log_valist (GstDebugCategory * category, GstDebugLevel level,
538 const gchar * file, const gchar * function, gint line,
539 GObject * object, const gchar * format, va_list args)
541 GstDebugMessage message;
545 g_return_if_fail (category != NULL);
547 if (level > gst_debug_category_get_threshold (category))
550 g_return_if_fail (file != NULL);
551 g_return_if_fail (function != NULL);
552 g_return_if_fail (format != NULL);
554 message.message = NULL;
555 message.format = format;
556 G_VA_COPY (message.arguments, args);
558 handler = __log_functions;
560 entry = handler->data;
561 handler = g_slist_next (handler);
562 entry->func (category, level, file, function, line, object, &message,
565 g_free (message.message);
566 va_end (message.arguments);
570 * gst_debug_message_get:
571 * @message: a debug message
573 * Gets the string representation of a #GstDebugMessage. This function is used
574 * in debug handlers to extract the message.
576 * Returns: the string representation of a #GstDebugMessage.
579 gst_debug_message_get (GstDebugMessage * message)
581 if (message->message == NULL) {
584 len = __gst_vasprintf (&message->message, message->format,
588 message->message = NULL;
590 return message->message;
593 #define MAX_BUFFER_DUMP_STRING_LEN 100
595 /* structure_to_pretty_string:
596 * @str: a serialized #GstStructure
598 * If the serialized structure contains large buffers such as images the hex
599 * representation of those buffers will be shortened so that the string remains
602 * Returns: the filtered string
605 prettify_structure_string (gchar * str)
607 gchar *pos = str, *end;
609 while ((pos = strstr (pos, "(buffer)"))) {
612 pos += strlen ("(buffer)");
613 for (end = pos; *end != '\0' && *end != ';' && *end != ' '; ++end)
615 if (count > MAX_BUFFER_DUMP_STRING_LEN) {
616 memcpy (pos + MAX_BUFFER_DUMP_STRING_LEN - 6, "..", 2);
617 memcpy (pos + MAX_BUFFER_DUMP_STRING_LEN - 4, pos + count - 4, 4);
618 memmove (pos + MAX_BUFFER_DUMP_STRING_LEN, pos + count,
619 strlen (pos + count) + 1);
620 pos += MAX_BUFFER_DUMP_STRING_LEN;
627 static inline gchar *
628 gst_info_structure_to_string (const GstStructure * s)
631 gchar *str = gst_structure_to_string (s);
632 if (G_UNLIKELY (pretty_tags && s->name == GST_QUARK (TAGLIST)))
633 return prettify_structure_string (str);
640 static inline gchar *
641 gst_info_describe_buffer (GstBuffer * buffer)
643 const gchar *offset_str = "none";
644 const gchar *offset_end_str = "none";
645 gchar offset_buf[32], offset_end_buf[32];
647 if (GST_BUFFER_OFFSET_IS_VALID (buffer)) {
648 g_snprintf (offset_buf, sizeof (offset_buf), "%" G_GUINT64_FORMAT,
649 GST_BUFFER_OFFSET (buffer));
650 offset_str = offset_buf;
652 if (GST_BUFFER_OFFSET_END_IS_VALID (buffer)) {
653 g_snprintf (offset_end_buf, sizeof (offset_end_buf), "%" G_GUINT64_FORMAT,
654 GST_BUFFER_OFFSET_END (buffer));
655 offset_end_str = offset_end_buf;
658 return g_strdup_printf ("buffer: %p, pts %" GST_TIME_FORMAT ", dts %"
659 GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT
660 ", offset %s, offset_end %s, flags 0x%x", buffer,
661 GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
662 GST_TIME_ARGS (GST_BUFFER_DTS (buffer)),
663 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
664 gst_buffer_get_size (buffer), offset_str, offset_end_str,
665 GST_BUFFER_FLAGS (buffer));
668 static inline gchar *
669 gst_info_describe_buffer_list (GstBufferList * list)
671 GstClockTime pts = GST_CLOCK_TIME_NONE;
672 GstClockTime dts = GST_CLOCK_TIME_NONE;
673 gsize total_size = 0;
676 n = gst_buffer_list_length (list);
677 for (i = 0; i < n; ++i) {
678 GstBuffer *buf = gst_buffer_list_get (list, i);
681 pts = GST_BUFFER_PTS (buf);
682 dts = GST_BUFFER_DTS (buf);
685 total_size += gst_buffer_get_size (buf);
688 return g_strdup_printf ("bufferlist: %p, %u buffers, pts %" GST_TIME_FORMAT
689 ", dts %" GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT, list, n,
690 GST_TIME_ARGS (pts), GST_TIME_ARGS (dts), total_size);
693 static inline gchar *
694 gst_info_describe_event (GstEvent * event)
698 s = gst_info_structure_to_string (gst_event_get_structure (event));
699 ret = g_strdup_printf ("%s event: %p, time %" GST_TIME_FORMAT
700 ", seq-num %d, %s", GST_EVENT_TYPE_NAME (event), event,
701 GST_TIME_ARGS (GST_EVENT_TIMESTAMP (event)), GST_EVENT_SEQNUM (event),
707 static inline gchar *
708 gst_info_describe_message (GstMessage * message)
712 s = gst_info_structure_to_string (gst_message_get_structure (message));
713 ret = g_strdup_printf ("%s message: %p, time %" GST_TIME_FORMAT
714 ", seq-num %d, element '%s', %s", GST_MESSAGE_TYPE_NAME (message),
715 message, GST_TIME_ARGS (GST_MESSAGE_TIMESTAMP (message)),
716 GST_MESSAGE_SEQNUM (message),
717 ((message->src) ? GST_ELEMENT_NAME (message->src) : "(NULL)"),
723 static inline gchar *
724 gst_info_describe_query (GstQuery * query)
728 s = gst_info_structure_to_string (gst_query_get_structure (query));
729 ret = g_strdup_printf ("%s query: %p, %s", GST_QUERY_TYPE_NAME (query),
730 query, (s ? s : "(NULL)"));
735 static inline gchar *
736 gst_info_describe_stream (GstStream * stream)
738 gchar *ret, *caps_str = NULL, *tags_str = NULL;
742 caps = gst_stream_get_caps (stream);
744 caps_str = gst_caps_to_string (caps);
745 gst_caps_unref (caps);
748 tags = gst_stream_get_tags (stream);
750 tags_str = gst_tag_list_to_string (tags);
751 gst_tag_list_unref (tags);
755 g_strdup_printf ("stream %s %p, ID %s, flags 0x%x, caps [%s], tags [%s]",
756 gst_stream_type_get_name (gst_stream_get_stream_type (stream)), stream,
757 gst_stream_get_stream_id (stream), gst_stream_get_stream_flags (stream),
758 caps_str ? caps_str : "", tags_str ? tags_str : "");
766 static inline gchar *
767 gst_info_describe_stream_collection (GstStreamCollection * collection)
770 GString *streams_str;
773 streams_str = g_string_new ("<");
774 for (i = 0; i < gst_stream_collection_get_size (collection); i++) {
775 GstStream *stream = gst_stream_collection_get_stream (collection, i);
778 s = gst_info_describe_stream (stream);
779 g_string_append_printf (streams_str, " %s,", s);
782 g_string_append (streams_str, " >");
784 ret = g_strdup_printf ("collection %p (%d streams) %s", collection,
785 gst_stream_collection_get_size (collection), streams_str->str);
787 g_string_free (streams_str, TRUE);
792 gst_debug_print_object (gpointer ptr)
794 GObject *object = (GObject *) ptr;
797 /* This is a cute trick to detect unmapped memory, but is unportable,
798 * slow, screws around with madvise, and not actually that useful. */
802 ret = madvise ((void *) ((unsigned long) ptr & (~0xfff)), 4096, 0);
803 if (ret == -1 && errno == ENOMEM) {
804 buffer = g_strdup_printf ("%p (unmapped memory)", ptr);
809 /* nicely printed object */
810 if (object == NULL) {
811 return g_strdup ("(NULL)");
813 if (GST_IS_CAPS (ptr)) {
814 return gst_caps_to_string ((const GstCaps *) ptr);
816 if (GST_IS_STRUCTURE (ptr)) {
817 return gst_info_structure_to_string ((const GstStructure *) ptr);
819 if (*(GType *) ptr == GST_TYPE_CAPS_FEATURES) {
820 return gst_caps_features_to_string ((const GstCapsFeatures *) ptr);
822 if (GST_IS_TAG_LIST (ptr)) {
823 gchar *str = gst_tag_list_to_string ((GstTagList *) ptr);
824 if (G_UNLIKELY (pretty_tags))
825 return prettify_structure_string (str);
829 if (*(GType *) ptr == GST_TYPE_DATE_TIME) {
830 return __gst_date_time_serialize ((GstDateTime *) ptr, TRUE);
832 if (GST_IS_BUFFER (ptr)) {
833 return gst_info_describe_buffer (GST_BUFFER_CAST (ptr));
835 if (GST_IS_BUFFER_LIST (ptr)) {
836 return gst_info_describe_buffer_list (GST_BUFFER_LIST_CAST (ptr));
839 if (*(guint32 *) ptr == 0xffffffff) {
840 return g_strdup_printf ("<poisoned@%p>", ptr);
843 if (GST_IS_MESSAGE (object)) {
844 return gst_info_describe_message (GST_MESSAGE_CAST (object));
846 if (GST_IS_QUERY (object)) {
847 return gst_info_describe_query (GST_QUERY_CAST (object));
849 if (GST_IS_EVENT (object)) {
850 return gst_info_describe_event (GST_EVENT_CAST (object));
852 if (GST_IS_CONTEXT (object)) {
853 GstContext *context = GST_CONTEXT_CAST (object);
856 const GstStructure *structure;
858 type = gst_context_get_context_type (context);
859 structure = gst_context_get_structure (context);
861 s = gst_info_structure_to_string (structure);
863 ret = g_strdup_printf ("context '%s'='%s'", type, s);
867 if (GST_IS_STREAM (object)) {
868 return gst_info_describe_stream (GST_STREAM_CAST (object));
870 if (GST_IS_STREAM_COLLECTION (object)) {
872 gst_info_describe_stream_collection (GST_STREAM_COLLECTION_CAST
875 if (GST_IS_PAD (object) && GST_OBJECT_NAME (object)) {
876 return g_strdup_printf ("<%s:%s>", GST_DEBUG_PAD_NAME (object));
878 if (GST_IS_OBJECT (object) && GST_OBJECT_NAME (object)) {
879 return g_strdup_printf ("<%s>", GST_OBJECT_NAME (object));
881 if (G_IS_OBJECT (object)) {
882 return g_strdup_printf ("<%s@%p>", G_OBJECT_TYPE_NAME (object), object);
885 return g_strdup_printf ("%p", ptr);
889 gst_debug_print_segment (gpointer ptr)
891 GstSegment *segment = (GstSegment *) ptr;
893 /* nicely printed segment */
894 if (segment == NULL) {
895 return g_strdup ("(NULL)");
898 switch (segment->format) {
899 case GST_FORMAT_UNDEFINED:{
900 return g_strdup_printf ("UNDEFINED segment");
902 case GST_FORMAT_TIME:{
903 return g_strdup_printf ("time segment start=%" GST_TIME_FORMAT
904 ", offset=%" GST_TIME_FORMAT ", stop=%" GST_TIME_FORMAT
905 ", rate=%f, applied_rate=%f" ", flags=0x%02x, time=%" GST_TIME_FORMAT
906 ", base=%" GST_TIME_FORMAT ", position %" GST_TIME_FORMAT
907 ", duration %" GST_TIME_FORMAT, GST_TIME_ARGS (segment->start),
908 GST_TIME_ARGS (segment->offset), GST_TIME_ARGS (segment->stop),
909 segment->rate, segment->applied_rate, (guint) segment->flags,
910 GST_TIME_ARGS (segment->time), GST_TIME_ARGS (segment->base),
911 GST_TIME_ARGS (segment->position), GST_TIME_ARGS (segment->duration));
914 const gchar *format_name;
916 format_name = gst_format_get_name (segment->format);
917 if (G_UNLIKELY (format_name == NULL))
918 format_name = "(UNKNOWN FORMAT)";
919 return g_strdup_printf ("%s segment start=%" G_GINT64_FORMAT
920 ", offset=%" G_GINT64_FORMAT ", stop=%" G_GINT64_FORMAT
921 ", rate=%f, applied_rate=%f" ", flags=0x%02x, time=%" G_GINT64_FORMAT
922 ", base=%" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT
923 ", duration %" G_GINT64_FORMAT, format_name, segment->start,
924 segment->offset, segment->stop, segment->rate, segment->applied_rate,
925 (guint) segment->flags, segment->time, segment->base,
926 segment->position, segment->duration);
932 gst_info_printf_pointer_extension_func (const char *format, void *ptr)
936 if (format[0] == 'p' && format[1] == '\a') {
938 case 'A': /* GST_PTR_FORMAT */
939 s = gst_debug_print_object (ptr);
941 case 'B': /* GST_SEGMENT_FORMAT */
942 s = gst_debug_print_segment (ptr);
944 case 'a': /* GST_WRAPPED_PTR_FORMAT */
945 s = priv_gst_string_take_and_wrap (gst_debug_print_object (ptr));
948 /* must have been compiled against a newer version with an extension
949 * we don't known about yet - just ignore and fallback to %p below */
954 s = g_strdup_printf ("%p", ptr);
960 * gst_debug_construct_term_color:
961 * @colorinfo: the color info
963 * Constructs a string that can be used for getting the desired color in color
965 * You need to free the string after use.
967 * Returns: (transfer full) (type gchar*): a string containing the color
971 gst_debug_construct_term_color (guint colorinfo)
975 color = g_string_new ("\033[00");
977 if (colorinfo & GST_DEBUG_BOLD) {
978 g_string_append_len (color, ";01", 3);
980 if (colorinfo & GST_DEBUG_UNDERLINE) {
981 g_string_append_len (color, ";04", 3);
983 if (colorinfo & GST_DEBUG_FG_MASK) {
984 g_string_append_printf (color, ";3%1d", colorinfo & GST_DEBUG_FG_MASK);
986 if (colorinfo & GST_DEBUG_BG_MASK) {
987 g_string_append_printf (color, ";4%1d",
988 (colorinfo & GST_DEBUG_BG_MASK) >> 4);
990 g_string_append_c (color, 'm');
992 return g_string_free (color, FALSE);
996 * gst_debug_construct_win_color:
997 * @colorinfo: the color info
999 * Constructs an integer that can be used for getting the desired color in
1000 * windows' terminals (cmd.exe). As there is no mean to underline, we simply
1001 * ignore this attribute.
1003 * This function returns 0 on non-windows machines.
1005 * Returns: an integer containing the color definition
1008 gst_debug_construct_win_color (guint colorinfo)
1012 static const guchar ansi_to_win_fg[8] = {
1014 FOREGROUND_RED, /* red */
1015 FOREGROUND_GREEN, /* green */
1016 FOREGROUND_RED | FOREGROUND_GREEN, /* yellow */
1017 FOREGROUND_BLUE, /* blue */
1018 FOREGROUND_RED | FOREGROUND_BLUE, /* magenta */
1019 FOREGROUND_GREEN | FOREGROUND_BLUE, /* cyan */
1020 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE /* white */
1022 static const guchar ansi_to_win_bg[8] = {
1026 BACKGROUND_RED | BACKGROUND_GREEN,
1028 BACKGROUND_RED | BACKGROUND_BLUE,
1029 BACKGROUND_GREEN | FOREGROUND_BLUE,
1030 BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE
1033 /* we draw black as white, as cmd.exe can only have black bg */
1034 if ((colorinfo & (GST_DEBUG_FG_MASK | GST_DEBUG_BG_MASK)) == 0) {
1035 color = ansi_to_win_fg[7];
1037 if (colorinfo & GST_DEBUG_UNDERLINE) {
1038 color |= BACKGROUND_INTENSITY;
1040 if (colorinfo & GST_DEBUG_BOLD) {
1041 color |= FOREGROUND_INTENSITY;
1043 if (colorinfo & GST_DEBUG_FG_MASK) {
1044 color |= ansi_to_win_fg[colorinfo & GST_DEBUG_FG_MASK];
1046 if (colorinfo & GST_DEBUG_BG_MASK) {
1047 color |= ansi_to_win_bg[(colorinfo & GST_DEBUG_BG_MASK) >> 4];
1053 /* width of %p varies depending on actual value of pointer, which can make
1054 * output unevenly aligned if multiple threads are involved, hence the %14p
1055 * (should really be %18p, but %14p seems a good compromise between too many
1056 * white spaces and likely unalignment on my system) */
1057 #if defined (GLIB_SIZEOF_VOID_P) && GLIB_SIZEOF_VOID_P == 8
1058 #define PTR_FMT "%14p"
1060 #define PTR_FMT "%10p"
1062 #define PID_FMT "%5d"
1063 #define CAT_FMT "%20s %s:%d:%s:%s"
1066 static const guchar levelcolormap_w32[GST_LEVEL_COUNT] = {
1067 /* GST_LEVEL_NONE */
1068 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
1069 /* GST_LEVEL_ERROR */
1070 FOREGROUND_RED | FOREGROUND_INTENSITY,
1071 /* GST_LEVEL_WARNING */
1072 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY,
1073 /* GST_LEVEL_INFO */
1074 FOREGROUND_GREEN | FOREGROUND_INTENSITY,
1075 /* GST_LEVEL_DEBUG */
1076 FOREGROUND_GREEN | FOREGROUND_BLUE,
1078 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
1079 /* GST_LEVEL_FIXME */
1080 FOREGROUND_RED | FOREGROUND_GREEN,
1081 /* GST_LEVEL_TRACE */
1082 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
1083 /* placeholder for log level 8 */
1085 /* GST_LEVEL_MEMDUMP */
1086 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
1089 static const guchar available_colors[] = {
1090 FOREGROUND_RED, FOREGROUND_GREEN, FOREGROUND_RED | FOREGROUND_GREEN,
1091 FOREGROUND_BLUE, FOREGROUND_RED | FOREGROUND_BLUE,
1092 FOREGROUND_GREEN | FOREGROUND_BLUE,
1094 #endif /* G_OS_WIN32 */
1095 static const gchar *levelcolormap[GST_LEVEL_COUNT] = {
1096 "\033[37m", /* GST_LEVEL_NONE */
1097 "\033[31;01m", /* GST_LEVEL_ERROR */
1098 "\033[33;01m", /* GST_LEVEL_WARNING */
1099 "\033[32;01m", /* GST_LEVEL_INFO */
1100 "\033[36m", /* GST_LEVEL_DEBUG */
1101 "\033[37m", /* GST_LEVEL_LOG */
1102 "\033[33;01m", /* GST_LEVEL_FIXME */
1103 "\033[37m", /* GST_LEVEL_TRACE */
1104 "\033[37m", /* placeholder for log level 8 */
1105 "\033[37m" /* GST_LEVEL_MEMDUMP */
1109 * gst_debug_log_default:
1110 * @category: category to log
1111 * @level: level of the message
1112 * @file: the file that emitted the message, usually the __FILE__ identifier
1113 * @function: the function that emitted the message
1114 * @line: the line from that the message was emitted, usually __LINE__
1115 * @message: the actual message
1116 * @object: (transfer none) (allow-none): the object this message relates to,
1118 * @user_data: the FILE* to log to
1120 * The default logging handler used by GStreamer. Logging functions get called
1121 * whenever a macro like GST_DEBUG or similar is used. By default this function
1122 * is setup to output the message and additional info to stderr (or the log file
1123 * specified via the GST_DEBUG_FILE environment variable) as received via
1126 * You can add other handlers by using gst_debug_add_log_function().
1127 * And you can remove this handler by calling
1128 * gst_debug_remove_log_function(gst_debug_log_default);
1131 gst_debug_log_default (GstDebugCategory * category, GstDebugLevel level,
1132 const gchar * file, const gchar * function, gint line,
1133 GObject * object, GstDebugMessage * message, gpointer user_data)
1136 GstClockTime elapsed;
1138 GstDebugColorMode color_mode;
1139 FILE *log_file = user_data ? user_data : stderr;
1142 /* __FILE__ might be a file name or an absolute path or a
1143 * relative path, irrespective of the exact compiler used,
1144 * in which case we want to shorten it to the filename for
1147 if (c == '.' || c == '/' || c == '\\' || (c != '\0' && file[1] == ':')) {
1148 file = gst_path_basename (file);
1152 color_mode = gst_debug_get_color_mode ();
1155 obj = gst_debug_print_object (object);
1160 elapsed = GST_CLOCK_DIFF (_priv_gst_start_time, gst_util_get_timestamp ());
1162 if (color_mode != GST_DEBUG_COLOR_MODE_OFF) {
1164 /* We take a lock to keep colors and content together.
1165 * Maybe there is a better way but for now this will do the right
1167 static GMutex win_print_mutex;
1168 g_mutex_lock (&win_print_mutex);
1169 if (color_mode == GST_DEBUG_COLOR_MODE_UNIX) {
1171 /* colors, non-windows */
1172 gchar *color = NULL;
1175 const gchar *levelcolor;
1177 color = gst_debug_construct_term_color (gst_debug_category_get_color
1180 g_sprintf (pidcolor, "\033[3%1dm", pid % 6 + 31);
1181 levelcolor = levelcolormap[level];
1183 #define PRINT_FMT " %s"PID_FMT"%s "PTR_FMT" %s%s%s %s"CAT_FMT"%s %s\n"
1184 fprintf (log_file, "%" GST_TIME_FORMAT PRINT_FMT, GST_TIME_ARGS (elapsed),
1185 pidcolor, pid, clear, g_thread_self (), levelcolor,
1186 gst_debug_level_get_name (level), clear, color,
1187 gst_debug_category_get_name (category), file, line, function, obj,
1188 clear, gst_debug_message_get (message));
1194 /* colors, windows. */
1195 const gint clear = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1196 #define SET_COLOR(c) G_STMT_START { \
1197 if (log_file == stderr) \
1198 SetConsoleTextAttribute (GetStdHandle (STD_ERROR_HANDLE), (c)); \
1201 fprintf (log_file, "%" GST_TIME_FORMAT " ", GST_TIME_ARGS (elapsed));
1204 SET_COLOR (available_colors[pid % G_N_ELEMENTS (available_colors)]);
1205 fprintf (log_file, PID_FMT, pid);
1209 fprintf (log_file, " " PTR_FMT " ", g_thread_self ());
1212 SET_COLOR (levelcolormap_w32[level]);
1213 fprintf (log_file, "%s ", gst_debug_level_get_name (level));
1216 SET_COLOR (gst_debug_construct_win_color (gst_debug_category_get_color
1218 fprintf (log_file, CAT_FMT, gst_debug_category_get_name (category),
1219 file, line, function, obj);
1223 fprintf (log_file, " %s\n", gst_debug_message_get (message));
1226 g_mutex_unlock (&win_print_mutex);
1229 /* no color, all platforms */
1230 #define PRINT_FMT " "PID_FMT" "PTR_FMT" %s "CAT_FMT" %s\n"
1231 fprintf (log_file, "%" GST_TIME_FORMAT PRINT_FMT, GST_TIME_ARGS (elapsed),
1232 pid, g_thread_self (), gst_debug_level_get_name (level),
1233 gst_debug_category_get_name (category), file, line, function, obj,
1234 gst_debug_message_get (message));
1244 * gst_debug_level_get_name:
1245 * @level: the level to get the name for
1247 * Get the string representation of a debugging level
1252 gst_debug_level_get_name (GstDebugLevel level)
1255 case GST_LEVEL_NONE:
1257 case GST_LEVEL_ERROR:
1259 case GST_LEVEL_WARNING:
1261 case GST_LEVEL_INFO:
1263 case GST_LEVEL_DEBUG:
1267 case GST_LEVEL_FIXME:
1269 case GST_LEVEL_TRACE:
1271 case GST_LEVEL_MEMDUMP:
1274 g_warning ("invalid level specified for gst_debug_level_get_name");
1280 * gst_debug_add_log_function:
1281 * @func: the function to use
1282 * @user_data: user data
1283 * @notify: called when @user_data is not used anymore
1285 * Adds the logging function to the list of logging functions.
1286 * Be sure to use #G_GNUC_NO_INSTRUMENT on that function, it is needed.
1289 gst_debug_add_log_function (GstLogFunction func, gpointer user_data,
1290 GDestroyNotify notify)
1292 LogFuncEntry *entry;
1296 func = gst_debug_log_default;
1298 entry = g_slice_new (LogFuncEntry);
1300 entry->user_data = user_data;
1301 entry->notify = notify;
1302 /* FIXME: we leak the old list here - other threads might access it right now
1303 * in gst_debug_logv. Another solution is to lock the mutex in gst_debug_logv,
1304 * but that is waaay costly.
1305 * It'd probably be clever to use some kind of RCU here, but I don't know
1306 * anything about that.
1308 g_mutex_lock (&__log_func_mutex);
1309 list = g_slist_copy (__log_functions);
1310 __log_functions = g_slist_prepend (list, entry);
1311 g_mutex_unlock (&__log_func_mutex);
1313 if (gst_is_initialized ())
1314 GST_DEBUG ("prepended log function %p (user data %p) to log functions",
1319 gst_debug_compare_log_function_by_func (gconstpointer entry, gconstpointer func)
1321 gpointer entryfunc = (gpointer) (((LogFuncEntry *) entry)->func);
1323 return (entryfunc < func) ? -1 : (entryfunc > func) ? 1 : 0;
1327 gst_debug_compare_log_function_by_data (gconstpointer entry, gconstpointer data)
1329 gpointer entrydata = ((LogFuncEntry *) entry)->user_data;
1331 return (entrydata < data) ? -1 : (entrydata > data) ? 1 : 0;
1335 gst_debug_remove_with_compare_func (GCompareFunc func, gpointer data)
1338 GSList *new, *cleanup = NULL;
1341 g_mutex_lock (&__log_func_mutex);
1342 new = __log_functions;
1344 while ((found = g_slist_find_custom (new, data, func))) {
1345 if (new == __log_functions) {
1346 /* make a copy when we have the first hit, so that we modify the copy and
1347 * make that the new list later */
1348 new = g_slist_copy (new);
1351 cleanup = g_slist_prepend (cleanup, found->data);
1352 new = g_slist_delete_link (new, found);
1355 /* FIXME: We leak the old list here. See _add_log_function for why. */
1356 __log_functions = new;
1357 g_mutex_unlock (&__log_func_mutex);
1360 LogFuncEntry *entry = cleanup->data;
1363 entry->notify (entry->user_data);
1365 g_slice_free (LogFuncEntry, entry);
1366 cleanup = g_slist_delete_link (cleanup, cleanup);
1372 * gst_debug_remove_log_function:
1373 * @func: (scope call) (allow-none): the log function to remove, or %NULL to
1374 * remove the default log function
1376 * Removes all registered instances of the given logging functions.
1378 * Returns: How many instances of the function were removed
1381 gst_debug_remove_log_function (GstLogFunction func)
1386 func = gst_debug_log_default;
1389 gst_debug_remove_with_compare_func
1390 (gst_debug_compare_log_function_by_func, (gpointer) func);
1392 if (gst_is_initialized ()) {
1393 GST_DEBUG ("removed log function %p %d times from log function list", func,
1396 /* If the default log function is removed before gst_init() was called,
1397 * set a flag so we don't add it in gst_init() later */
1398 if (func == gst_debug_log_default) {
1399 add_default_log_func = FALSE;
1408 * gst_debug_remove_log_function_by_data:
1409 * @data: user data of the log function to remove
1411 * Removes all registered instances of log functions with the given user data.
1413 * Returns: How many instances of the function were removed
1416 gst_debug_remove_log_function_by_data (gpointer data)
1421 gst_debug_remove_with_compare_func
1422 (gst_debug_compare_log_function_by_data, data);
1424 if (gst_is_initialized ())
1426 ("removed %d log functions with user data %p from log function list",
1433 * gst_debug_set_colored:
1434 * @colored: Whether to use colored output or not
1436 * Sets or unsets the use of coloured debugging output.
1437 * Same as gst_debug_set_color_mode () with the argument being
1438 * being GST_DEBUG_COLOR_MODE_ON or GST_DEBUG_COLOR_MODE_OFF.
1440 * This function may be called before gst_init().
1443 gst_debug_set_colored (gboolean colored)
1445 GstDebugColorMode new_mode;
1446 new_mode = colored ? GST_DEBUG_COLOR_MODE_ON : GST_DEBUG_COLOR_MODE_OFF;
1447 g_atomic_int_set (&__use_color, (gint) new_mode);
1451 * gst_debug_set_color_mode:
1452 * @mode: The coloring mode for debug output. See @GstDebugColorMode.
1454 * Changes the coloring mode for debug output.
1456 * This function may be called before gst_init().
1461 gst_debug_set_color_mode (GstDebugColorMode mode)
1463 g_atomic_int_set (&__use_color, mode);
1467 * gst_debug_set_color_mode_from_string:
1468 * @mode: The coloring mode for debug output. One of the following:
1469 * "on", "auto", "off", "disable", "unix".
1471 * Changes the coloring mode for debug output.
1473 * This function may be called before gst_init().
1478 gst_debug_set_color_mode_from_string (const gchar * mode)
1480 if ((strcmp (mode, "on") == 0) || (strcmp (mode, "auto") == 0))
1481 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_ON);
1482 else if ((strcmp (mode, "off") == 0) || (strcmp (mode, "disable") == 0))
1483 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_OFF);
1484 else if (strcmp (mode, "unix") == 0)
1485 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_UNIX);
1489 * gst_debug_is_colored:
1491 * Checks if the debugging output should be colored.
1493 * Returns: %TRUE, if the debug output should be colored.
1496 gst_debug_is_colored (void)
1498 GstDebugColorMode mode = g_atomic_int_get (&__use_color);
1499 return (mode == GST_DEBUG_COLOR_MODE_UNIX || mode == GST_DEBUG_COLOR_MODE_ON);
1503 * gst_debug_get_color_mode:
1505 * Changes the coloring mode for debug output.
1507 * Returns: see @GstDebugColorMode for possible values.
1512 gst_debug_get_color_mode (void)
1514 return g_atomic_int_get (&__use_color);
1518 * gst_debug_set_active:
1519 * @active: Whether to use debugging output or not
1521 * If activated, debugging messages are sent to the debugging
1523 * It makes sense to deactivate it for speed issues.
1524 * <note><para>This function is not threadsafe. It makes sense to only call it
1525 * during initialization.</para></note>
1528 gst_debug_set_active (gboolean active)
1530 _gst_debug_enabled = active;
1532 _gst_debug_min = GST_LEVEL_COUNT;
1534 _gst_debug_min = GST_LEVEL_NONE;
1538 * gst_debug_is_active:
1540 * Checks if debugging output is activated.
1542 * Returns: %TRUE, if debugging is activated
1545 gst_debug_is_active (void)
1547 return _gst_debug_enabled;
1551 * gst_debug_set_default_threshold:
1552 * @level: level to set
1554 * Sets the default threshold to the given level and updates all categories to
1555 * use this threshold.
1557 * This function may be called before gst_init().
1560 gst_debug_set_default_threshold (GstDebugLevel level)
1562 g_atomic_int_set (&__default_level, level);
1563 gst_debug_reset_all_thresholds ();
1567 * gst_debug_get_default_threshold:
1569 * Returns the default threshold that is used for new categories.
1571 * Returns: the default threshold level
1574 gst_debug_get_default_threshold (void)
1576 return (GstDebugLevel) g_atomic_int_get (&__default_level);
1580 gst_debug_reset_threshold (gpointer category, gpointer unused)
1582 GstDebugCategory *cat = (GstDebugCategory *) category;
1585 g_mutex_lock (&__level_name_mutex);
1586 walk = __level_name;
1588 LevelNameEntry *entry = walk->data;
1590 walk = g_slist_next (walk);
1591 if (g_pattern_match_string (entry->pat, cat->name)) {
1592 if (gst_is_initialized ())
1593 GST_LOG ("category %s matches pattern %p - gets set to level %d",
1594 cat->name, entry->pat, entry->level);
1595 gst_debug_category_set_threshold (cat, entry->level);
1599 gst_debug_category_set_threshold (cat, gst_debug_get_default_threshold ());
1602 g_mutex_unlock (&__level_name_mutex);
1606 gst_debug_reset_all_thresholds (void)
1608 g_mutex_lock (&__cat_mutex);
1609 g_slist_foreach (__categories, gst_debug_reset_threshold, NULL);
1610 g_mutex_unlock (&__cat_mutex);
1614 for_each_threshold_by_entry (gpointer data, gpointer user_data)
1616 GstDebugCategory *cat = (GstDebugCategory *) data;
1617 LevelNameEntry *entry = (LevelNameEntry *) user_data;
1619 if (g_pattern_match_string (entry->pat, cat->name)) {
1620 if (gst_is_initialized ())
1621 GST_LOG ("category %s matches pattern %p - gets set to level %d",
1622 cat->name, entry->pat, entry->level);
1623 gst_debug_category_set_threshold (cat, entry->level);
1628 * gst_debug_set_threshold_for_name:
1629 * @name: name of the categories to set
1630 * @level: level to set them to
1632 * Sets all categories which match the given glob style pattern to the given
1636 gst_debug_set_threshold_for_name (const gchar * name, GstDebugLevel level)
1639 LevelNameEntry *entry;
1641 g_return_if_fail (name != NULL);
1643 pat = g_pattern_spec_new (name);
1644 entry = g_slice_new (LevelNameEntry);
1646 entry->level = level;
1647 g_mutex_lock (&__level_name_mutex);
1648 __level_name = g_slist_prepend (__level_name, entry);
1649 g_mutex_unlock (&__level_name_mutex);
1650 g_mutex_lock (&__cat_mutex);
1651 g_slist_foreach (__categories, for_each_threshold_by_entry, entry);
1652 g_mutex_unlock (&__cat_mutex);
1656 * gst_debug_unset_threshold_for_name:
1657 * @name: name of the categories to set
1659 * Resets all categories with the given name back to the default level.
1662 gst_debug_unset_threshold_for_name (const gchar * name)
1667 g_return_if_fail (name != NULL);
1669 pat = g_pattern_spec_new (name);
1670 g_mutex_lock (&__level_name_mutex);
1671 walk = __level_name;
1672 /* improve this if you want, it's mighty slow */
1674 LevelNameEntry *entry = walk->data;
1676 if (g_pattern_spec_equal (entry->pat, pat)) {
1677 __level_name = g_slist_remove_link (__level_name, walk);
1678 g_pattern_spec_free (entry->pat);
1679 g_slice_free (LevelNameEntry, entry);
1680 g_slist_free_1 (walk);
1681 walk = __level_name;
1683 walk = g_slist_next (walk);
1686 g_mutex_unlock (&__level_name_mutex);
1687 g_pattern_spec_free (pat);
1688 gst_debug_reset_all_thresholds ();
1692 _gst_debug_category_new (const gchar * name, guint color,
1693 const gchar * description)
1695 GstDebugCategory *cat, *catfound;
1697 g_return_val_if_fail (name != NULL, NULL);
1699 cat = g_slice_new (GstDebugCategory);
1700 cat->name = g_strdup (name);
1702 if (description != NULL) {
1703 cat->description = g_strdup (description);
1705 cat->description = g_strdup ("no description");
1707 g_atomic_int_set (&cat->threshold, 0);
1708 gst_debug_reset_threshold (cat, NULL);
1710 /* add to category list */
1711 g_mutex_lock (&__cat_mutex);
1712 catfound = _gst_debug_get_category_locked (name);
1714 g_free ((gpointer) cat->name);
1715 g_free ((gpointer) cat->description);
1716 g_slice_free (GstDebugCategory, cat);
1719 __categories = g_slist_prepend (__categories, cat);
1721 g_mutex_unlock (&__cat_mutex);
1727 * gst_debug_category_free:
1728 * @category: #GstDebugCategory to free.
1730 * Removes and frees the category and all associated resources.
1733 gst_debug_category_free (GstDebugCategory * category)
1735 if (category == NULL)
1738 /* remove from category list */
1739 g_mutex_lock (&__cat_mutex);
1740 __categories = g_slist_remove (__categories, category);
1741 g_mutex_unlock (&__cat_mutex);
1743 g_free ((gpointer) category->name);
1744 g_free ((gpointer) category->description);
1745 g_slice_free (GstDebugCategory, category);
1749 * gst_debug_category_set_threshold:
1750 * @category: a #GstDebugCategory to set threshold of.
1751 * @level: the #GstDebugLevel threshold to set.
1753 * Sets the threshold of the category to the given level. Debug information will
1754 * only be output if the threshold is lower or equal to the level of the
1755 * debugging message.
1757 * Do not use this function in production code, because other functions may
1758 * change the threshold of categories as side effect. It is however a nice
1759 * function to use when debugging (even from gdb).
1763 gst_debug_category_set_threshold (GstDebugCategory * category,
1764 GstDebugLevel level)
1766 g_return_if_fail (category != NULL);
1768 if (level > _gst_debug_min) {
1769 _gst_debug_enabled = TRUE;
1770 _gst_debug_min = level;
1773 g_atomic_int_set (&category->threshold, level);
1777 * gst_debug_category_reset_threshold:
1778 * @category: a #GstDebugCategory to reset threshold of.
1780 * Resets the threshold of the category to the default level. Debug information
1781 * will only be output if the threshold is lower or equal to the level of the
1782 * debugging message.
1783 * Use this function to set the threshold back to where it was after using
1784 * gst_debug_category_set_threshold().
1787 gst_debug_category_reset_threshold (GstDebugCategory * category)
1789 gst_debug_reset_threshold (category, NULL);
1793 * gst_debug_category_get_threshold:
1794 * @category: a #GstDebugCategory to get threshold of.
1796 * Returns the threshold of a #GstDebugCategory.
1798 * Returns: the #GstDebugLevel that is used as threshold.
1801 gst_debug_category_get_threshold (GstDebugCategory * category)
1803 return (GstDebugLevel) g_atomic_int_get (&category->threshold);
1807 * gst_debug_category_get_name:
1808 * @category: a #GstDebugCategory to get name of.
1810 * Returns the name of a debug category.
1812 * Returns: the name of the category.
1815 gst_debug_category_get_name (GstDebugCategory * category)
1817 return category->name;
1821 * gst_debug_category_get_color:
1822 * @category: a #GstDebugCategory to get the color of.
1824 * Returns the color of a debug category used when printing output in this
1827 * Returns: the color of the category.
1830 gst_debug_category_get_color (GstDebugCategory * category)
1832 return category->color;
1836 * gst_debug_category_get_description:
1837 * @category: a #GstDebugCategory to get the description of.
1839 * Returns the description of a debug category.
1841 * Returns: the description of the category.
1844 gst_debug_category_get_description (GstDebugCategory * category)
1846 return category->description;
1850 * gst_debug_get_all_categories:
1852 * Returns a snapshot of a all categories that are currently in use . This list
1853 * may change anytime.
1854 * The caller has to free the list after use.
1856 * Returns: (transfer container) (element-type Gst.DebugCategory): the list of
1860 gst_debug_get_all_categories (void)
1864 g_mutex_lock (&__cat_mutex);
1865 ret = g_slist_copy (__categories);
1866 g_mutex_unlock (&__cat_mutex);
1871 static GstDebugCategory *
1872 _gst_debug_get_category_locked (const gchar * name)
1874 GstDebugCategory *ret = NULL;
1877 for (node = __categories; node; node = g_slist_next (node)) {
1878 ret = (GstDebugCategory *) node->data;
1879 if (!strcmp (name, ret->name)) {
1887 _gst_debug_get_category (const gchar * name)
1889 GstDebugCategory *ret;
1891 g_mutex_lock (&__cat_mutex);
1892 ret = _gst_debug_get_category_locked (name);
1893 g_mutex_unlock (&__cat_mutex);
1899 parse_debug_category (gchar * str, const gchar ** category)
1904 /* works in place */
1907 if (str[0] != '\0') {
1916 parse_debug_level (gchar * str, GstDebugLevel * level)
1921 /* works in place */
1924 if (g_ascii_isdigit (str[0])) {
1927 l = strtoul (str, &endptr, 10);
1928 if (endptr > str && endptr[0] == 0) {
1929 *level = (GstDebugLevel) l;
1933 } else if (strcmp (str, "ERROR") == 0) {
1934 *level = GST_LEVEL_ERROR;
1935 } else if (strncmp (str, "WARN", 4) == 0) {
1936 *level = GST_LEVEL_WARNING;
1937 } else if (strcmp (str, "FIXME") == 0) {
1938 *level = GST_LEVEL_FIXME;
1939 } else if (strcmp (str, "INFO") == 0) {
1940 *level = GST_LEVEL_INFO;
1941 } else if (strcmp (str, "DEBUG") == 0) {
1942 *level = GST_LEVEL_DEBUG;
1943 } else if (strcmp (str, "LOG") == 0) {
1944 *level = GST_LEVEL_LOG;
1945 } else if (strcmp (str, "TRACE") == 0) {
1946 *level = GST_LEVEL_TRACE;
1947 } else if (strcmp (str, "MEMDUMP") == 0) {
1948 *level = GST_LEVEL_MEMDUMP;
1956 * gst_debug_set_threshold_from_string:
1957 * @list: comma-separated list of "category:level" pairs to be used
1958 * as debug logging levels
1959 * @reset: %TRUE to clear all previously-set debug levels before setting
1961 * %FALSE if adding the threshold described by @list to the one already set.
1963 * Sets the debug logging wanted in the same form as with the GST_DEBUG
1964 * environment variable. You can use wildcards such as '*', but note that
1965 * the order matters when you use wild cards, e.g. "foosrc:6,*src:3,*:2" sets
1966 * everything to log level 2.
1971 gst_debug_set_threshold_from_string (const gchar * list, gboolean reset)
1979 gst_debug_set_default_threshold (0);
1981 split = g_strsplit (list, ",", 0);
1983 for (walk = split; *walk; walk++) {
1984 if (strchr (*walk, ':')) {
1985 gchar **values = g_strsplit (*walk, ":", 2);
1987 if (values[0] && values[1]) {
1988 GstDebugLevel level;
1989 const gchar *category;
1991 if (parse_debug_category (values[0], &category)
1992 && parse_debug_level (values[1], &level))
1993 gst_debug_set_threshold_for_name (category, level);
1996 g_strfreev (values);
1998 GstDebugLevel level;
2000 if (parse_debug_level (*walk, &level))
2001 gst_debug_set_default_threshold (level);
2008 /*** FUNCTION POINTERS ********************************************************/
2010 static GHashTable *__gst_function_pointers; /* NULL */
2011 static GMutex __dbg_functions_mutex;
2013 /* This function MUST NOT return NULL */
2015 _gst_debug_nameof_funcptr (GstDebugFuncPtr func)
2023 if (G_UNLIKELY (func == NULL))
2026 g_mutex_lock (&__dbg_functions_mutex);
2027 if (G_LIKELY (__gst_function_pointers)) {
2028 ptrname = g_hash_table_lookup (__gst_function_pointers, (gpointer) func);
2029 g_mutex_unlock (&__dbg_functions_mutex);
2030 if (G_LIKELY (ptrname))
2033 g_mutex_unlock (&__dbg_functions_mutex);
2035 /* we need to create an entry in the hash table for this one so we don't leak
2038 if (dladdr ((gpointer) func, &dl_info) && dl_info.dli_sname) {
2039 gchar *name = g_strdup (dl_info.dli_sname);
2041 _gst_debug_register_funcptr (func, name);
2046 gchar *name = g_strdup_printf ("%p", (gpointer) func);
2048 _gst_debug_register_funcptr (func, name);
2054 _gst_debug_register_funcptr (GstDebugFuncPtr func, const gchar * ptrname)
2056 gpointer ptr = (gpointer) func;
2058 g_mutex_lock (&__dbg_functions_mutex);
2060 if (!__gst_function_pointers)
2061 __gst_function_pointers = g_hash_table_new (g_direct_hash, g_direct_equal);
2062 if (!g_hash_table_lookup (__gst_function_pointers, ptr))
2063 g_hash_table_insert (__gst_function_pointers, ptr, (gpointer) ptrname);
2065 g_mutex_unlock (&__dbg_functions_mutex);
2069 gst_info_dump_mem_line (gchar * linebuf, gsize linebuf_size,
2070 const guint8 * mem, gsize mem_offset, gsize mem_size)
2072 gchar hexstr[50], ascstr[18], digitstr[4];
2084 while (i < mem_size) {
2085 ascstr[i] = (g_ascii_isprint (mem[i])) ? mem[i] : '.';
2086 g_snprintf (digitstr, sizeof (digitstr), "%02x ", mem[i]);
2087 g_strlcat (hexstr, digitstr, sizeof (hexstr));
2093 g_snprintf (linebuf, linebuf_size, "%08x: %-48.48s %-16.16s",
2094 (guint) mem_offset, hexstr, ascstr);
2098 _gst_debug_dump_mem (GstDebugCategory * cat, const gchar * file,
2099 const gchar * func, gint line, GObject * obj, const gchar * msg,
2100 const guint8 * data, guint length)
2104 gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "--------"
2105 "-------------------------------------------------------------------");
2107 if (msg != NULL && *msg != '\0') {
2108 gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "%s", msg);
2111 while (off < length) {
2114 /* gst_info_dump_mem_line will process 16 bytes at most */
2115 gst_info_dump_mem_line (buf, sizeof (buf), data, off, length - off);
2116 gst_debug_log (cat, GST_LEVEL_MEMDUMP, file, func, line, obj, "%s", buf);
2120 gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "--------"
2121 "-------------------------------------------------------------------");
2124 #else /* !GST_DISABLE_GST_DEBUG */
2125 #ifndef GST_REMOVE_DISABLED
2128 _gst_debug_category_new (const gchar * name, guint color,
2129 const gchar * description)
2135 _gst_debug_register_funcptr (GstDebugFuncPtr func, const gchar * ptrname)
2139 /* This function MUST NOT return NULL */
2141 _gst_debug_nameof_funcptr (GstDebugFuncPtr func)
2147 gst_debug_log (GstDebugCategory * category, GstDebugLevel level,
2148 const gchar * file, const gchar * function, gint line,
2149 GObject * object, const gchar * format, ...)
2154 gst_debug_log_valist (GstDebugCategory * category, GstDebugLevel level,
2155 const gchar * file, const gchar * function, gint line,
2156 GObject * object, const gchar * format, va_list args)
2161 gst_debug_message_get (GstDebugMessage * message)
2167 gst_debug_log_default (GstDebugCategory * category, GstDebugLevel level,
2168 const gchar * file, const gchar * function, gint line,
2169 GObject * object, GstDebugMessage * message, gpointer unused)
2174 gst_debug_level_get_name (GstDebugLevel level)
2180 gst_debug_add_log_function (GstLogFunction func, gpointer user_data,
2181 GDestroyNotify notify)
2186 gst_debug_remove_log_function (GstLogFunction func)
2192 gst_debug_remove_log_function_by_data (gpointer data)
2198 gst_debug_set_active (gboolean active)
2203 gst_debug_is_active (void)
2209 gst_debug_set_colored (gboolean colored)
2214 gst_debug_set_color_mode (GstDebugColorMode mode)
2219 gst_debug_set_color_mode_from_string (const gchar * str)
2224 gst_debug_is_colored (void)
2230 gst_debug_get_color_mode (void)
2232 return GST_DEBUG_COLOR_MODE_OFF;
2236 gst_debug_set_threshold_from_string (const gchar * list, gboolean reset)
2241 gst_debug_set_default_threshold (GstDebugLevel level)
2246 gst_debug_get_default_threshold (void)
2248 return GST_LEVEL_NONE;
2252 gst_debug_set_threshold_for_name (const gchar * name, GstDebugLevel level)
2257 gst_debug_unset_threshold_for_name (const gchar * name)
2262 gst_debug_category_free (GstDebugCategory * category)
2267 gst_debug_category_set_threshold (GstDebugCategory * category,
2268 GstDebugLevel level)
2273 gst_debug_category_reset_threshold (GstDebugCategory * category)
2278 gst_debug_category_get_threshold (GstDebugCategory * category)
2280 return GST_LEVEL_NONE;
2284 gst_debug_category_get_name (GstDebugCategory * category)
2290 gst_debug_category_get_color (GstDebugCategory * category)
2296 gst_debug_category_get_description (GstDebugCategory * category)
2302 gst_debug_get_all_categories (void)
2308 _gst_debug_get_category (const gchar * name)
2314 gst_debug_construct_term_color (guint colorinfo)
2316 return g_strdup ("00");
2320 gst_debug_construct_win_color (guint colorinfo)
2326 _priv_gst_in_valgrind (void)
2332 _gst_debug_dump_mem (GstDebugCategory * cat, const gchar * file,
2333 const gchar * func, gint line, GObject * obj, const gchar * msg,
2334 const guint8 * data, guint length)
2337 #endif /* GST_REMOVE_DISABLED */
2338 #endif /* GST_DISABLE_GST_DEBUG */
2340 /* Need this for _gst_element_error_printf even if GST_REMOVE_DISABLED is set:
2341 * fallback function that cleans up the format string and replaces all pointer
2342 * extension formats with plain %p. */
2343 #ifdef GST_DISABLE_GST_DEBUG
2345 __gst_info_fallback_vasprintf (char **result, char const *format, va_list args)
2347 gchar *clean_format, *c;
2353 clean_format = g_strdup (format);
2355 while ((c = strstr (c, "%p\a"))) {
2356 if (c[3] < 'A' || c[3] > 'Z') {
2360 len = strlen (c + 4);
2361 memmove (c + 2, c + 4, len + 1);
2364 while ((c = strstr (clean_format, "%P"))) /* old GST_PTR_FORMAT */
2366 while ((c = strstr (clean_format, "%Q"))) /* old GST_SEGMENT_FORMAT */
2369 len = g_vasprintf (result, clean_format, args);
2371 g_free (clean_format);
2373 if (*result == NULL)
2381 * gst_info_vasprintf:
2382 * @result: (out): the resulting string
2383 * @format: a printf style format string
2384 * @args: the va_list of printf arguments for @format
2386 * Allocates and fills a string large enough (including the terminating null
2387 * byte) to hold the specified printf style @format and @args.
2389 * This function deals with the GStreamer specific printf specifiers
2390 * #GST_PTR_FORMAT and #GST_SEGMENT_FORMAT. If you do not have these specifiers
2391 * in your @format string, you do not need to use this function and can use
2392 * alternatives such as g_vasprintf().
2394 * Free @result with g_free().
2396 * Returns: the length of the string allocated into @result or -1 on any error
2401 gst_info_vasprintf (gchar ** result, const gchar * format, va_list args)
2403 /* This will fallback to __gst_info_fallback_vasprintf() via a #define in
2404 * gst_private.h if the debug system is disabled which will remove the gst
2405 * specific printf format specifiers */
2406 return __gst_vasprintf (result, format, args);
2410 * gst_info_strdup_vprintf:
2411 * @format: a printf style format string
2412 * @args: the va_list of printf arguments for @format
2414 * Allocates, fills and returns a null terminated string from the printf style
2415 * @format string and @args.
2417 * See gst_info_vasprintf() for when this function is required.
2419 * Free with g_free().
2421 * Returns: a newly allocated null terminated string or %NULL on any error
2426 gst_info_strdup_vprintf (const gchar * format, va_list args)
2430 if (gst_info_vasprintf (&ret, format, args) < 0)
2437 * gst_info_strdup_printf:
2438 * @format: a printf style format string
2439 * @...: the printf arguments for @format
2441 * Allocates, fills and returns a null terminated string from the printf style
2442 * @format string and corresponding arguments.
2444 * See gst_info_vasprintf() for when this function is required.
2446 * Free with g_free().
2448 * Returns: a newly allocated null terminated string or %NULL on any error
2453 gst_info_strdup_printf (const gchar * format, ...)
2458 va_start (args, format);
2459 ret = gst_info_strdup_vprintf (format, args);
2468 append_debug_info (GString * trace, const void *ip)
2473 Dwfl_Module *module;
2474 const gchar *function_name;
2475 gchar *debuginfo_path = NULL;
2476 Dwfl_Callbacks callbacks = {
2477 .find_elf = dwfl_linux_proc_find_elf,
2478 .find_debuginfo = dwfl_standard_find_debuginfo,
2479 .debuginfo_path = &debuginfo_path,
2482 dwfl = dwfl_begin (&callbacks);
2486 if (dwfl_linux_proc_report (dwfl, getpid ()) != 0)
2489 if (dwfl_report_end (dwfl, NULL, NULL))
2492 addr = (uintptr_t) ip;
2493 module = dwfl_addrmodule (dwfl, addr);
2494 function_name = dwfl_module_addrname (module, addr);
2496 g_string_append_printf (trace, "%s (", function_name ? function_name : "??");
2498 line = dwfl_getsrc (dwfl, addr);
2502 const gchar *filename = dwfl_lineinfo (line, &addr,
2503 &nline, NULL, NULL, NULL);
2505 g_string_append_printf (trace, "%s:%d", strrchr (filename,
2506 G_DIR_SEPARATOR) + 1, nline);
2508 const gchar *eflfile = NULL;
2510 dwfl_module_info (module, NULL, NULL, NULL, NULL, NULL, &eflfile, NULL);
2511 g_string_append_printf (trace, "%s:%p", eflfile ? eflfile : "??", ip);
2516 #endif /* HAVE_DW */
2519 generate_unwind_trace (void)
2522 unw_cursor_t cursor;
2523 gboolean use_libunwind = TRUE;
2524 GString *trace = g_string_new (NULL);
2526 unw_getcontext (&uc);
2527 unw_init_local (&cursor, &uc);
2529 while (unw_step (&cursor) > 0) {
2533 unw_get_reg (&cursor, UNW_REG_IP, &ip);
2534 if (append_debug_info (trace, (void *) (ip - 4))) {
2535 use_libunwind = FALSE;
2536 g_string_append (trace, ")\n");
2538 #endif /* HAVE_DW */
2540 if (use_libunwind) {
2543 unw_word_t offset = 0;
2544 unw_get_proc_name (&cursor, name, sizeof (name), &offset);
2545 g_string_append_printf (trace, "%s (0x%" G_GSIZE_FORMAT ")\n", name,
2550 return g_string_free (trace, FALSE);
2553 #endif /* HAVE_UNWIND */
2555 #ifdef HAVE_BACKTRACE
2557 generate_backtrace_trace (void)
2560 void *buffer[BT_BUF_SIZE];
2564 trace = g_string_new (NULL);
2565 nptrs = backtrace (buffer, BT_BUF_SIZE);
2567 strings = backtrace_symbols (buffer, nptrs);
2572 for (j = 0; j < nptrs; j++)
2573 g_string_append_printf (trace, "%s\n", strings[j]);
2575 return g_string_free (trace, FALSE);
2577 #endif /* HAVE_BACKTRACE */
2580 * gst_debug_get_stack_trace:
2582 * If libunwind or glibc backtrace are present, a stack trace
2586 gst_debug_get_stack_trace (void)
2588 gchar *trace = NULL;
2591 trace = generate_unwind_trace ();
2594 #endif /* HAVE_UNWIND */
2596 #ifdef HAVE_BACKTRACE
2597 trace = generate_backtrace_trace ();
2598 #endif /* HAVE_BACKTRACE */
2604 * gst_debug_print_stack_trace:
2606 * If libunwind or glibc backtrace are present
2607 * a stack trace is printed.
2610 gst_debug_print_stack_trace (void)
2612 gchar *trace = gst_debug_get_stack_trace ();
2615 g_print ("%s\n", trace);