X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=gst%2Fgstinfo.c;h=fa3adc02619ea9b5d8883e2021c659d6e0bc96f2;hb=32b7108d54447f79a0a70ff9504ced877c069706;hp=ffff06d6d3a2cfbc211fe5d299ff5a07958b158f;hpb=154eefecc93b69c6af8040ceef5f004e595a827f;p=platform%2Fupstream%2Fgstreamer.git diff --git a/gst/gstinfo.c b/gst/gstinfo.c index ffff06d..fa3adc0 100644 --- a/gst/gstinfo.c +++ b/gst/gstinfo.c @@ -24,8 +24,9 @@ /** * SECTION:gstinfo + * @title: GstInfo * @short_description: Debugging and logging facilities - * @see_also: #gstreamer-gstconfig, #gstreamer-Gst for command line parameters + * @see_also: #gst-running for command line parameters * and environment variables that affect the debugging output. * * GStreamer's debugging subsystem is an easy way to get information about what @@ -50,12 +51,12 @@ * categories. This is easily done with 3 lines. At the top of your code, * declare * the variables and set the default category. - * |[ + * |[ * GST_DEBUG_CATEGORY_STATIC (my_category); // define category (statically) * #define GST_CAT_DEFAULT my_category // set as default * ]| * After that you only need to initialize the category. - * |[ + * |[ * GST_DEBUG_CATEGORY_INIT (my_category, "my category", * 0, "This is my very own"); * ]| @@ -90,7 +91,6 @@ #undef gst_debug_add_log_function #ifndef GST_DISABLE_GST_DEBUG - #ifdef HAVE_DLFCN_H # include #endif @@ -127,9 +127,51 @@ static char *gst_info_printf_pointer_extension_func (const char *format, void *ptr); +#else /* GST_DISABLE_GST_DEBUG */ +#include #endif /* !GST_DISABLE_GST_DEBUG */ +#ifdef HAVE_UNWIND +/* No need for remote debugging so turn on the 'local only' optimizations in + * libunwind */ +#define UNW_LOCAL_ONLY + +#include +#include +#include +#include +#include +#include +#include + +#ifdef HAVE_DW +#include +#endif /* HAVE_DW */ +#endif /* HAVE_UNWIND */ + +#ifdef HAVE_BACKTRACE +#include +#define BT_BUF_SIZE 100 +#endif /* HAVE_BACKTRACE */ + +#ifdef HAVE_DBGHELP +#include +#include +#include +#endif /* HAVE_DBGHELP */ + +#ifdef G_OS_WIN32 +/* We take a lock in order to + * 1) keep colors and content together for a single line + * 2) serialise gst_print*() and gst_printerr*() with each other and the debug + * log to keep the debug log colouring from interfering with those and + * to prevent broken output on the windows terminal. + * Maybe there is a better way but for now this will do the right + * thing. */ +G_LOCK_DEFINE_STATIC (win_print_mutex); +#endif + extern gboolean gst_is_initialized (void); /* we want these symbols exported even if debug is disabled, to maintain @@ -176,6 +218,7 @@ GstDebugCategory *_priv_GST_CAT_POLL = NULL; GstDebugCategory *GST_CAT_META = NULL; GstDebugCategory *GST_CAT_LOCKING = NULL; GstDebugCategory *GST_CAT_CONTEXT = NULL; +GstDebugCategory *_priv_GST_CAT_PROTECTION = NULL; #endif /* !defined(GST_DISABLE_GST_DEBUG) || !defined(GST_REMOVE_DISABLED) */ @@ -185,12 +228,6 @@ GstDebugCategory *GST_CAT_CONTEXT = NULL; /* underscore is to prevent conflict with GST_CAT_DEBUG define */ GST_DEBUG_CATEGORY_STATIC (_GST_CAT_DEBUG); -/* time of initialization, so we get useful debugging output times - * FIXME: we use this in gstdebugutils.c, what about a function + macro to - * get the running time: GST_DEBUG_RUNNING_TIME - */ -GstClockTime _priv_gst_info_start_time; - #if 0 #if defined __sgi__ #include @@ -258,6 +295,9 @@ LogFuncEntry; static GMutex __log_func_mutex; static GSList *__log_functions = NULL; +/* whether to add the default log function in gst_init() */ +static gboolean add_default_log_func = TRUE; + #define PRETTY_TAGS_DEFAULT TRUE static gboolean pretty_tags = PRETTY_TAGS_DEFAULT; @@ -294,6 +334,34 @@ _priv_gst_in_valgrind (void) return (in_valgrind == GST_VG_INSIDE); } +static gchar * +_replace_pattern_in_gst_debug_file_name (gchar * name, const char *token, + guint val) +{ + gchar *token_start; + if ((token_start = strstr (name, token))) { + gsize token_len = strlen (token); + gchar *name_prefix = name; + gchar *name_suffix = token_start + token_len; + token_start[0] = '\0'; + name = g_strdup_printf ("%s%u%s", name_prefix, val, name_suffix); + g_free (name_prefix); + } + return name; +} + +static gchar * +_priv_gst_debug_file_name (const gchar * env) +{ + gchar *name; + + name = g_strdup (env); + name = _replace_pattern_in_gst_debug_file_name (name, "%p", getpid ()); + name = _replace_pattern_in_gst_debug_file_name (name, "%r", g_random_int ()); + + return name; +} + /* Initialize the debugging system */ void _priv_gst_debug_init (void) @@ -301,24 +369,27 @@ _priv_gst_debug_init (void) const gchar *env; FILE *log_file; - env = g_getenv ("GST_DEBUG_FILE"); - if (env != NULL && *env != '\0') { - if (strcmp (env, "-") == 0) { - log_file = stdout; - } else { - log_file = g_fopen (env, "w"); - if (log_file == NULL) { - g_printerr ("Could not open log file '%s' for writing: %s\n", env, - g_strerror (errno)); - log_file = stderr; + if (add_default_log_func) { + env = g_getenv ("GST_DEBUG_FILE"); + if (env != NULL && *env != '\0') { + if (strcmp (env, "-") == 0) { + log_file = stdout; + } else { + gchar *name = _priv_gst_debug_file_name (env); + log_file = g_fopen (name, "w"); + g_free (name); + if (log_file == NULL) { + g_printerr ("Could not open log file '%s' for writing: %s\n", env, + g_strerror (errno)); + log_file = stderr; + } } + } else { + log_file = stderr; } - } else { - log_file = stderr; - } - /* get time we started for debugging messages */ - _priv_gst_info_start_time = gst_util_get_timestamp (); + gst_debug_add_log_function (gst_debug_log_default, log_file, NULL); + } __gst_printf_pointer_extension_set_func (gst_info_printf_pointer_extension_func); @@ -329,8 +400,6 @@ _priv_gst_debug_init (void) _GST_CAT_DEBUG = _gst_debug_category_new ("GST_DEBUG", GST_DEBUG_BOLD | GST_DEBUG_FG_YELLOW, "debugging subsystem"); - gst_debug_add_log_function (gst_debug_log_default, log_file, NULL); - /* FIXME: add descriptions here */ GST_CAT_GST_INIT = _gst_debug_category_new ("GST_INIT", GST_DEBUG_BOLD | GST_DEBUG_FG_RED, NULL); @@ -354,7 +423,7 @@ _priv_gst_debug_init (void) GST_CAT_ELEMENT_PADS = _gst_debug_category_new ("GST_ELEMENT_PADS", GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL); GST_CAT_PADS = _gst_debug_category_new ("GST_PADS", - GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_RED, NULL); + GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_BLUE, NULL); GST_CAT_PERFORMANCE = _gst_debug_category_new ("GST_PERFORMANCE", GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL); GST_CAT_PIPELINE = _gst_debug_category_new ("GST_PIPELINE", @@ -390,6 +459,8 @@ _priv_gst_debug_init (void) GST_CAT_META = _gst_debug_category_new ("GST_META", 0, "meta"); GST_CAT_LOCKING = _gst_debug_category_new ("GST_LOCKING", 0, "locking"); GST_CAT_CONTEXT = _gst_debug_category_new ("GST_CONTEXT", 0, NULL); + _priv_GST_CAT_PROTECTION = + _gst_debug_category_new ("GST_PROTECTION", 0, "protection"); /* print out the valgrind message if we're in valgrind */ _priv_gst_in_valgrind (); @@ -409,9 +480,8 @@ _priv_gst_debug_init (void) gst_debug_set_color_mode_from_string (env); env = g_getenv ("GST_DEBUG"); - if (env) { + if (env) gst_debug_set_threshold_from_string (env, FALSE); - } } /* we can't do this further above, because we initialize the GST_CAT_DEFAULT struct */ @@ -444,7 +514,6 @@ gst_debug_log (GstDebugCategory * category, GstDebugLevel level, va_end (var_args); } -#ifdef G_OS_WIN32 /* based on g_basename(), which we can't use because it was deprecated */ static inline const gchar * gst_path_basename (const gchar * file_name) @@ -467,7 +536,6 @@ gst_path_basename (const gchar * file_name) return file_name; } -#endif /** * gst_debug_log_valist: @@ -493,17 +561,14 @@ gst_debug_log_valist (GstDebugCategory * category, GstDebugLevel level, GSList *handler; g_return_if_fail (category != NULL); + + if (level > gst_debug_category_get_threshold (category)) + return; + g_return_if_fail (file != NULL); g_return_if_fail (function != NULL); g_return_if_fail (format != NULL); - /* The predefined macro __FILE__ is always the exact path given to the - * compiler with MSVC, which may or may not be the basename. We work - * around it at runtime to improve the readability. */ -#ifdef G_OS_WIN32 - file = gst_path_basename (file); -#endif - message.message = NULL; message.format = format; G_VA_COPY (message.arguments, args); @@ -526,7 +591,7 @@ gst_debug_log_valist (GstDebugCategory * category, GstDebugLevel level, * Gets the string representation of a #GstDebugMessage. This function is used * in debug handlers to extract the message. * - * Returns: the string representation of a #GstDebugMessage. + * Returns: (nullable): the string representation of a #GstDebugMessage. */ const gchar * gst_debug_message_get (GstDebugMessage * message) @@ -581,7 +646,7 @@ static inline gchar * gst_info_structure_to_string (const GstStructure * s) { if (G_LIKELY (s)) { - gchar *str = gst_structure_to_string (s);; + gchar *str = gst_structure_to_string (s); if (G_UNLIKELY (pretty_tags && s->name == GST_QUARK (TAGLIST))) return prettify_structure_string (str); else @@ -593,14 +658,54 @@ gst_info_structure_to_string (const GstStructure * s) static inline gchar * gst_info_describe_buffer (GstBuffer * buffer) { + const gchar *offset_str = "none"; + const gchar *offset_end_str = "none"; + gchar offset_buf[32], offset_end_buf[32]; + + if (GST_BUFFER_OFFSET_IS_VALID (buffer)) { + g_snprintf (offset_buf, sizeof (offset_buf), "%" G_GUINT64_FORMAT, + GST_BUFFER_OFFSET (buffer)); + offset_str = offset_buf; + } + if (GST_BUFFER_OFFSET_END_IS_VALID (buffer)) { + g_snprintf (offset_end_buf, sizeof (offset_end_buf), "%" G_GUINT64_FORMAT, + GST_BUFFER_OFFSET_END (buffer)); + offset_end_str = offset_end_buf; + } + return g_strdup_printf ("buffer: %p, pts %" GST_TIME_FORMAT ", dts %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT - ", offset %" G_GUINT64_FORMAT ", offset_end %" G_GUINT64_FORMAT - ", flags 0x%x", buffer, GST_TIME_ARGS (GST_BUFFER_PTS (buffer)), + ", offset %s, offset_end %s, flags 0x%x", buffer, + GST_TIME_ARGS (GST_BUFFER_PTS (buffer)), GST_TIME_ARGS (GST_BUFFER_DTS (buffer)), GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)), - gst_buffer_get_size (buffer), GST_BUFFER_OFFSET (buffer), - GST_BUFFER_OFFSET_END (buffer), GST_BUFFER_FLAGS (buffer)); + gst_buffer_get_size (buffer), offset_str, offset_end_str, + GST_BUFFER_FLAGS (buffer)); +} + +static inline gchar * +gst_info_describe_buffer_list (GstBufferList * list) +{ + GstClockTime pts = GST_CLOCK_TIME_NONE; + GstClockTime dts = GST_CLOCK_TIME_NONE; + gsize total_size = 0; + guint n, i; + + n = gst_buffer_list_length (list); + for (i = 0; i < n; ++i) { + GstBuffer *buf = gst_buffer_list_get (list, i); + + if (i == 0) { + pts = GST_BUFFER_PTS (buf); + dts = GST_BUFFER_DTS (buf); + } + + total_size += gst_buffer_get_size (buf); + } + + return g_strdup_printf ("bufferlist: %p, %u buffers, pts %" GST_TIME_FORMAT + ", dts %" GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT, list, n, + GST_TIME_ARGS (pts), GST_TIME_ARGS (dts), total_size); } static inline gchar * @@ -645,6 +750,62 @@ gst_info_describe_query (GstQuery * query) return ret; } +static inline gchar * +gst_info_describe_stream (GstStream * stream) +{ + gchar *ret, *caps_str = NULL, *tags_str = NULL; + GstCaps *caps; + GstTagList *tags; + + caps = gst_stream_get_caps (stream); + if (caps) { + caps_str = gst_caps_to_string (caps); + gst_caps_unref (caps); + } + + tags = gst_stream_get_tags (stream); + if (tags) { + tags_str = gst_tag_list_to_string (tags); + gst_tag_list_unref (tags); + } + + ret = + g_strdup_printf ("stream %s %p, ID %s, flags 0x%x, caps [%s], tags [%s]", + gst_stream_type_get_name (gst_stream_get_stream_type (stream)), stream, + gst_stream_get_stream_id (stream), gst_stream_get_stream_flags (stream), + caps_str ? caps_str : "", tags_str ? tags_str : ""); + + g_free (caps_str); + g_free (tags_str); + + return ret; +} + +static inline gchar * +gst_info_describe_stream_collection (GstStreamCollection * collection) +{ + gchar *ret; + GString *streams_str; + guint i; + + streams_str = g_string_new ("<"); + for (i = 0; i < gst_stream_collection_get_size (collection); i++) { + GstStream *stream = gst_stream_collection_get_stream (collection, i); + gchar *s; + + s = gst_info_describe_stream (stream); + g_string_append_printf (streams_str, " %s,", s); + g_free (s); + } + g_string_append (streams_str, " >"); + + ret = g_strdup_printf ("collection %p (%d streams) %s", collection, + gst_stream_collection_get_size (collection), streams_str->str); + + g_string_free (streams_str, TRUE); + return ret; +} + static gchar * gst_debug_print_object (gpointer ptr) { @@ -689,6 +850,9 @@ gst_debug_print_object (gpointer ptr) if (GST_IS_BUFFER (ptr)) { return gst_info_describe_buffer (GST_BUFFER_CAST (ptr)); } + if (GST_IS_BUFFER_LIST (ptr)) { + return gst_info_describe_buffer_list (GST_BUFFER_LIST_CAST (ptr)); + } #ifdef USE_POISONING if (*(guint32 *) ptr == 0xffffffff) { return g_strdup_printf ("", ptr); @@ -718,6 +882,14 @@ gst_debug_print_object (gpointer ptr) g_free (s); return ret; } + if (GST_IS_STREAM (object)) { + return gst_info_describe_stream (GST_STREAM_CAST (object)); + } + if (GST_IS_STREAM_COLLECTION (object)) { + return + gst_info_describe_stream_collection (GST_STREAM_COLLECTION_CAST + (object)); + } if (GST_IS_PAD (object) && GST_OBJECT_NAME (object)) { return g_strdup_printf ("<%s:%s>", GST_DEBUG_PAD_NAME (object)); } @@ -787,6 +959,9 @@ gst_info_printf_pointer_extension_func (const char *format, void *ptr) case 'B': /* GST_SEGMENT_FORMAT */ s = gst_debug_print_segment (ptr); break; + case 'a': /* GST_WRAPPED_PTR_FORMAT */ + s = priv_gst_string_take_and_wrap (gst_debug_print_object (ptr)); + break; default: /* must have been compiled against a newer version with an extension * we don't known about yet - just ignore and fallback to %p below */ @@ -979,10 +1154,27 @@ gst_debug_log_default (GstDebugCategory * category, GstDebugLevel level, GstClockTime elapsed; gchar *obj = NULL; GstDebugColorMode color_mode; + const gchar *message_str; FILE *log_file = user_data ? user_data : stderr; - - if (level > gst_debug_category_get_threshold (category)) - return; + gchar c; + + /* Get message string first because printing it might call into our custom + * printf format extension mechanism which in turn might log something, e.g. + * from inside gst_structure_to_string() when something can't be serialised. + * This means we either need to do this outside of any critical section or + * use a recursive lock instead. As we always need the message string in all + * code paths, we might just as well get it here first thing and outside of + * the win_print_mutex critical section. */ + message_str = gst_debug_message_get (message); + + /* __FILE__ might be a file name or an absolute path or a + * relative path, irrespective of the exact compiler used, + * in which case we want to shorten it to the filename for + * readability. */ + c = file[0]; + if (c == '.' || c == '/' || c == '\\' || (c != '\0' && file[1] == ':')) { + file = gst_path_basename (file); + } pid = getpid (); color_mode = gst_debug_get_color_mode (); @@ -990,19 +1182,14 @@ gst_debug_log_default (GstDebugCategory * category, GstDebugLevel level, if (object) { obj = gst_debug_print_object (object); } else { - obj = g_strdup (""); + obj = (gchar *) ""; } - elapsed = GST_CLOCK_DIFF (_priv_gst_info_start_time, - gst_util_get_timestamp ()); + elapsed = GST_CLOCK_DIFF (_priv_gst_start_time, gst_util_get_timestamp ()); if (color_mode != GST_DEBUG_COLOR_MODE_OFF) { #ifdef G_OS_WIN32 - /* We take a lock to keep colors and content together. - * Maybe there is a better way but for now this will do the right - * thing. */ - static GMutex win_print_mutex; - g_mutex_lock (&win_print_mutex); + G_LOCK (win_print_mutex); if (color_mode == GST_DEBUG_COLOR_MODE_UNIX) { #endif /* colors, non-windows */ @@ -1022,7 +1209,7 @@ gst_debug_log_default (GstDebugCategory * category, GstDebugLevel level, pidcolor, pid, clear, g_thread_self (), levelcolor, gst_debug_level_get_name (level), clear, color, gst_debug_category_get_name (category), file, line, function, obj, - clear, gst_debug_message_get (message)); + clear, message_str); fflush (log_file); #undef PRINT_FMT g_free (color); @@ -1057,10 +1244,10 @@ gst_debug_log_default (GstDebugCategory * category, GstDebugLevel level, fflush (log_file); /* message */ SET_COLOR (clear); - fprintf (log_file, " %s\n", gst_debug_message_get (message)); + fprintf (log_file, " %s\n", message_str); fflush (log_file); } - g_mutex_unlock (&win_print_mutex); + G_UNLOCK (win_print_mutex); #endif } else { /* no color, all platforms */ @@ -1068,12 +1255,13 @@ gst_debug_log_default (GstDebugCategory * category, GstDebugLevel level, fprintf (log_file, "%" GST_TIME_FORMAT PRINT_FMT, GST_TIME_ARGS (elapsed), pid, g_thread_self (), gst_debug_level_get_name (level), gst_debug_category_get_name (category), file, line, function, obj, - gst_debug_message_get (message)); + message_str); fflush (log_file); #undef PRINT_FMT } - g_free (obj); + if (object != NULL) + g_free (obj); } /** @@ -1206,7 +1394,8 @@ gst_debug_remove_with_compare_func (GCompareFunc func, gpointer data) /** * gst_debug_remove_log_function: - * @func: (scope call): the log function to remove + * @func: (scope call) (allow-none): the log function to remove, or %NULL to + * remove the default log function * * Removes all registered instances of the given logging functions. * @@ -1223,9 +1412,18 @@ gst_debug_remove_log_function (GstLogFunction func) removals = gst_debug_remove_with_compare_func (gst_debug_compare_log_function_by_func, (gpointer) func); - if (gst_is_initialized ()) + + if (gst_is_initialized ()) { GST_DEBUG ("removed log function %p %d times from log function list", func, removals); + } else { + /* If the default log function is removed before gst_init() was called, + * set a flag so we don't add it in gst_init() later */ + if (func == gst_debug_log_default) { + add_default_log_func = FALSE; + ++removals; + } + } return removals; } @@ -1347,8 +1545,8 @@ gst_debug_get_color_mode (void) * If activated, debugging messages are sent to the debugging * handlers. * It makes sense to deactivate it for speed issues. - * This function is not threadsafe. It makes sense to only call it - * during initialization. + * > This function is not threadsafe. It makes sense to only call it + * during initialization. */ void gst_debug_set_active (gboolean active) @@ -1402,6 +1600,20 @@ gst_debug_get_default_threshold (void) return (GstDebugLevel) g_atomic_int_get (&__default_level); } +static gboolean +gst_debug_apply_entry (GstDebugCategory * cat, LevelNameEntry * entry) +{ + if (!g_pattern_match_string (entry->pat, cat->name)) + return FALSE; + + if (gst_is_initialized ()) + GST_LOG ("category %s matches pattern %p - gets set to level %d", + cat->name, entry->pat, entry->level); + + gst_debug_category_set_threshold (cat, entry->level); + return TRUE; +} + static void gst_debug_reset_threshold (gpointer category, gpointer unused) { @@ -1409,23 +1621,16 @@ gst_debug_reset_threshold (gpointer category, gpointer unused) GSList *walk; g_mutex_lock (&__level_name_mutex); - walk = __level_name; - while (walk) { - LevelNameEntry *entry = walk->data; - walk = g_slist_next (walk); - if (g_pattern_match_string (entry->pat, cat->name)) { - if (gst_is_initialized ()) - GST_LOG ("category %s matches pattern %p - gets set to level %d", - cat->name, entry->pat, entry->level); - gst_debug_category_set_threshold (cat, entry->level); - goto exit; - } + for (walk = __level_name; walk != NULL; walk = walk->next) { + if (gst_debug_apply_entry (cat, walk->data)) + break; } - gst_debug_category_set_threshold (cat, gst_debug_get_default_threshold ()); -exit: g_mutex_unlock (&__level_name_mutex); + + if (walk == NULL) + gst_debug_category_set_threshold (cat, gst_debug_get_default_threshold ()); } static void @@ -1442,12 +1647,7 @@ for_each_threshold_by_entry (gpointer data, gpointer user_data) GstDebugCategory *cat = (GstDebugCategory *) data; LevelNameEntry *entry = (LevelNameEntry *) user_data; - if (g_pattern_match_string (entry->pat, cat->name)) { - if (gst_is_initialized ()) - GST_LOG ("category %s matches pattern %p - gets set to level %d", - cat->name, entry->pat, entry->level); - gst_debug_category_set_threshold (cat, entry->level); - } + gst_debug_apply_entry (cat, entry); } /** @@ -1505,6 +1705,8 @@ gst_debug_unset_threshold_for_name (const gchar * name) g_slice_free (LevelNameEntry, entry); g_slist_free_1 (walk); walk = __level_name; + } else { + walk = g_slist_next (walk); } } g_mutex_unlock (&__level_name_mutex); @@ -1577,11 +1779,9 @@ gst_debug_category_free (GstDebugCategory * category) * Sets the threshold of the category to the given level. Debug information will * only be output if the threshold is lower or equal to the level of the * debugging message. - * - * Do not use this function in production code, because other functions may - * change the threshold of categories as side effect. It is however a nice - * function to use when debugging (even from gdb). - * + * > Do not use this function in production code, because other functions may + * > change the threshold of categories as side effect. It is however a nice + * > function to use when debugging (even from gdb). */ void gst_debug_category_set_threshold (GstDebugCategory * category, @@ -1800,7 +2000,7 @@ gst_debug_set_threshold_from_string (const gchar * list, gboolean reset) g_assert (list); if (reset) - gst_debug_set_default_threshold (0); + gst_debug_set_default_threshold (GST_LEVEL_DEFAULT); split = g_strsplit (list, ",", 0); @@ -1813,8 +2013,15 @@ gst_debug_set_threshold_from_string (const gchar * list, gboolean reset) const gchar *category; if (parse_debug_category (values[0], &category) - && parse_debug_level (values[1], &level)) + && parse_debug_level (values[1], &level)) { gst_debug_set_threshold_for_name (category, level); + + /* bump min-level anyway to allow the category to be registered in the + * future still */ + if (level > _gst_debug_min) { + _gst_debug_min = level; + } + } } g_strfreev (values); @@ -1860,7 +2067,7 @@ _gst_debug_nameof_funcptr (GstDebugFuncPtr func) * the name */ #ifdef HAVE_DLADDR if (dladdr ((gpointer) func, &dl_info) && dl_info.dli_sname) { - gchar *name = g_strdup (dl_info.dli_sname); + const gchar *name = g_intern_string (dl_info.dli_sname); _gst_debug_register_funcptr (func, name); return name; @@ -1868,9 +2075,12 @@ _gst_debug_nameof_funcptr (GstDebugFuncPtr func) #endif { gchar *name = g_strdup_printf ("%p", (gpointer) func); + const gchar *iname = g_intern_string (name); - _gst_debug_register_funcptr (func, name); - return name; + g_free (name); + + _gst_debug_register_funcptr (func, iname); + return iname; } } @@ -1883,8 +2093,22 @@ _gst_debug_register_funcptr (GstDebugFuncPtr func, const gchar * ptrname) if (!__gst_function_pointers) __gst_function_pointers = g_hash_table_new (g_direct_hash, g_direct_equal); - if (!g_hash_table_lookup (__gst_function_pointers, ptr)) + if (!g_hash_table_lookup (__gst_function_pointers, ptr)) { g_hash_table_insert (__gst_function_pointers, ptr, (gpointer) ptrname); + } + + g_mutex_unlock (&__dbg_functions_mutex); +} + +void +_priv_gst_debug_cleanup (void) +{ + g_mutex_lock (&__dbg_functions_mutex); + + if (__gst_function_pointers) { + g_hash_table_unref (__gst_function_pointers); + __gst_function_pointers = NULL; + } g_mutex_unlock (&__dbg_functions_mutex); } @@ -1968,6 +2192,11 @@ _gst_debug_nameof_funcptr (GstDebugFuncPtr func) } void +_priv_gst_debug_cleanup (void) +{ +} + +void gst_debug_log (GstDebugCategory * category, GstDebugLevel level, const gchar * file, const gchar * function, gint line, GObject * object, const gchar * format, ...) @@ -2165,7 +2394,6 @@ _gst_debug_dump_mem (GstDebugCategory * cat, const gchar * file, * fallback function that cleans up the format string and replaces all pointer * extension formats with plain %p. */ #ifdef GST_DISABLE_GST_DEBUG -#include int __gst_info_fallback_vasprintf (char **result, char const *format, va_list args) { @@ -2202,70 +2430,847 @@ __gst_info_fallback_vasprintf (char **result, char const *format, va_list args) } #endif -#ifdef GST_ENABLE_FUNC_INSTRUMENTATION -/* FIXME make this thread specific */ -static GSList *stack_trace = NULL; +/** + * gst_info_vasprintf: + * @result: (out): the resulting string + * @format: a printf style format string + * @args: the va_list of printf arguments for @format + * + * Allocates and fills a string large enough (including the terminating null + * byte) to hold the specified printf style @format and @args. + * + * This function deals with the GStreamer specific printf specifiers + * #GST_PTR_FORMAT and #GST_SEGMENT_FORMAT. If you do not have these specifiers + * in your @format string, you do not need to use this function and can use + * alternatives such as g_vasprintf(). + * + * Free @result with g_free(). + * + * Returns: the length of the string allocated into @result or -1 on any error + * + * Since: 1.8 + */ +gint +gst_info_vasprintf (gchar ** result, const gchar * format, va_list args) +{ + /* This will fallback to __gst_info_fallback_vasprintf() via a #define in + * gst_private.h if the debug system is disabled which will remove the gst + * specific printf format specifiers */ + return __gst_vasprintf (result, format, args); +} + +/** + * 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 -__cyg_profile_func_enter (void *this_fn, void *call_site) - G_GNUC_NO_INSTRUMENT; - void __cyg_profile_func_enter (void *this_fn, void *call_site) +gst_print (const gchar * format, ...) { - gchar *name = _gst_debug_nameof_funcptr (this_fn); - gchar *site = _gst_debug_nameof_funcptr (call_site); + va_list args; + gchar *str; + + va_start (args, format); + str = gst_info_strdup_vprintf (format, args); + va_end (args); - GST_CAT_DEBUG (GST_CAT_CALL_TRACE, "entering function %s from %s", name, - site); - stack_trace = - g_slist_prepend (stack_trace, g_strdup_printf ("%8p in %s from %p (%s)", - this_fn, name, call_site, site)); +#ifdef G_OS_WIN32 + G_LOCK (win_print_mutex); +#endif - g_free (name); - g_free (site); + g_print ("%s", str); + +#ifdef G_OS_WIN32 + G_UNLOCK (win_print_mutex); +#endif + 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 -__cyg_profile_func_exit (void *this_fn, void *call_site) - G_GNUC_NO_INSTRUMENT; - void __cyg_profile_func_exit (void *this_fn, void *call_site) +gst_println (const gchar * format, ...) { - gchar *name = _gst_debug_nameof_funcptr (this_fn); + va_list args; + gchar *str; - GST_CAT_DEBUG (GST_CAT_CALL_TRACE, "leaving function %s", name); - g_free (stack_trace->data); - stack_trace = g_slist_delete_link (stack_trace, stack_trace); + va_start (args, format); + str = gst_info_strdup_vprintf (format, args); + va_end (args); - g_free (name); +#ifdef G_OS_WIN32 + G_LOCK (win_print_mutex); +#endif + + g_print ("%s\n", str); + +#ifdef G_OS_WIN32 + G_UNLOCK (win_print_mutex); +#endif + g_free (str); } /** - * gst_debug_print_stack_trace: + * 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. * - * If GST_ENABLE_FUNC_INSTRUMENTATION is defined a stacktrace is available for - * gstreamer code, which can be printed with this function. + * This function is primarily for printing debug output. + * + * Since: 1.12 */ void -gst_debug_print_stack_trace (void) +gst_printerr (const gchar * format, ...) { - GSList *walk = stack_trace; - gint count = 0; + va_list args; + gchar *str; - if (walk) - walk = g_slist_next (walk); + va_start (args, format); + str = gst_info_strdup_vprintf (format, args); + va_end (args); - while (walk) { - gchar *name = (gchar *) walk->data; +#ifdef G_OS_WIN32 + G_LOCK (win_print_mutex); +#endif + + g_printerr ("%s", str); + +#ifdef G_OS_WIN32 + G_UNLOCK (win_print_mutex); +#endif + 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); + +#ifdef G_OS_WIN32 + G_LOCK (win_print_mutex); +#endif + + g_printerr ("%s\n", str); + +#ifdef G_OS_WIN32 + G_UNLOCK (win_print_mutex); +#endif + 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); - g_print ("#%-2d %s\n", count++, name); + strings = backtrace_symbols (buffer, nptrs); - walk = g_slist_next (walk); + 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. + */ void gst_debug_print_stack_trace (void) { - /* nothing because it's compiled out */ + gchar *trace = gst_debug_get_stack_trace (GST_STACK_TRACE_SHOW_FULL); + + if (trace) { +#ifdef G_OS_WIN32 + G_LOCK (win_print_mutex); +#endif + + g_print ("%s\n", trace); + +#ifdef G_OS_WIN32 + G_UNLOCK (win_print_mutex); +#endif + } + + g_free (trace); +} + +#ifndef GST_DISABLE_GST_DEBUG +typedef struct +{ + guint max_size_per_thread; + guint thread_timeout; + GQueue threads; + GHashTable *thread_index; +} GstRingBufferLogger; + +typedef struct +{ + GList *link; + gint64 last_use; + GThread *thread; + + GQueue log; + gsize log_size; +} GstRingBufferLog; + +G_LOCK_DEFINE_STATIC (ring_buffer_logger); +static GstRingBufferLogger *ring_buffer_logger = NULL; + +static void +gst_ring_buffer_logger_log (GstDebugCategory * category, + GstDebugLevel level, + const gchar * file, + const gchar * function, + gint line, GObject * object, GstDebugMessage * message, gpointer user_data) +{ + GstRingBufferLogger *logger = user_data; + gint pid; + GThread *thread; + GstClockTime elapsed; + gchar *obj = NULL; + gchar c; + gchar *output; + gsize output_len; + GstRingBufferLog *log; + gint64 now = g_get_monotonic_time (); + const gchar *message_str = gst_debug_message_get (message); + + /* __FILE__ might be a file name or an absolute path or a + * relative path, irrespective of the exact compiler used, + * in which case we want to shorten it to the filename for + * readability. */ + c = file[0]; + if (c == '.' || c == '/' || c == '\\' || (c != '\0' && file[1] == ':')) { + file = gst_path_basename (file); + } + + if (object) { + obj = gst_debug_print_object (object); + } else { + obj = (gchar *) ""; + } + + elapsed = GST_CLOCK_DIFF (_priv_gst_start_time, gst_util_get_timestamp ()); + pid = getpid (); + thread = g_thread_self (); + + /* no color, all platforms */ +#define PRINT_FMT " "PID_FMT" "PTR_FMT" %s "CAT_FMT" %s\n" + output = + g_strdup_printf ("%" GST_TIME_FORMAT PRINT_FMT, GST_TIME_ARGS (elapsed), + pid, thread, gst_debug_level_get_name (level), + gst_debug_category_get_name (category), file, line, function, obj, + message_str); +#undef PRINT_FMT + + output_len = strlen (output); + + if (object != NULL) + g_free (obj); + + G_LOCK (ring_buffer_logger); + + if (logger->thread_timeout > 0) { + gchar *buf; + + /* Remove all threads that saw no output since thread_timeout seconds. + * By construction these are all at the tail of the queue, and the queue + * is ordered by last use, so we just need to look at the tail. + */ + while (logger->threads.tail) { + log = logger->threads.tail->data; + if (log->last_use + logger->thread_timeout * G_USEC_PER_SEC >= now) + break; + + g_hash_table_remove (logger->thread_index, log->thread); + while ((buf = g_queue_pop_head (&log->log))) + g_free (buf); + g_free (log); + g_queue_pop_tail (&logger->threads); + } + } + + /* Get logger for this thread, and put it back at the + * head of the threads queue */ + log = g_hash_table_lookup (logger->thread_index, thread); + if (!log) { + log = g_new0 (GstRingBufferLog, 1); + g_queue_init (&log->log); + log->log_size = 0; + g_queue_push_head (&logger->threads, log); + log->link = logger->threads.head; + log->thread = thread; + g_hash_table_insert (logger->thread_index, thread, log); + } else { + g_queue_unlink (&logger->threads, log->link); + g_queue_push_head_link (&logger->threads, log->link); + } + log->last_use = now; + + if (output_len < logger->max_size_per_thread) { + gchar *buf; + + /* While using a GQueue here is not the most efficient thing to do, we + * have to allocate a string for every output anyway and could just store + * that instead of copying it to an actual ringbuffer. + * Better than GQueue would be GstQueueArray, but that one is in + * libgstbase and we can't use it here. That one allocation will not make + * much of a difference anymore, considering the number of allocations + * needed to get to this point... + */ + while (log->log_size + output_len > logger->max_size_per_thread) { + buf = g_queue_pop_head (&log->log); + log->log_size -= strlen (buf); + g_free (buf); + } + g_queue_push_tail (&log->log, output); + log->log_size += output_len; + } else { + gchar *buf; + + /* Can't really write anything as the line is bigger than the maximum + * allowed log size already, so just remove everything */ + + while ((buf = g_queue_pop_head (&log->log))) + g_free (buf); + g_free (output); + log->log_size = 0; + } + + G_UNLOCK (ring_buffer_logger); +} + +/** + * gst_debug_ring_buffer_logger_get_logs: + * + * Fetches the current logs per thread from the ring buffer logger. See + * gst_debug_add_ring_buffer_logger() for details. + * + * Returns: (transfer full) (array zero-terminated): NULL-terminated array of + * strings with the debug output per thread + * + * Since: 1.14 + */ +gchar ** +gst_debug_ring_buffer_logger_get_logs (void) +{ + gchar **logs, **tmp; + GList *l; + + g_return_val_if_fail (ring_buffer_logger != NULL, NULL); + + G_LOCK (ring_buffer_logger); + + tmp = logs = g_new0 (gchar *, ring_buffer_logger->threads.length + 1); + for (l = ring_buffer_logger->threads.head; l; l = l->next) { + GstRingBufferLog *log = l->data; + GList *l; + gchar *p; + gsize len; + + *tmp = p = g_new0 (gchar, log->log_size + 1); + + for (l = log->log.head; l; l = l->next) { + len = strlen (l->data); + memcpy (p, l->data, len); + p += len; + } + + tmp++; + } + + G_UNLOCK (ring_buffer_logger); + + return logs; +} + +static void +gst_ring_buffer_logger_free (GstRingBufferLogger * logger) +{ + G_LOCK (ring_buffer_logger); + if (ring_buffer_logger == logger) { + GstRingBufferLog *log; + + while ((log = g_queue_pop_head (&logger->threads))) { + gchar *buf; + while ((buf = g_queue_pop_head (&log->log))) + g_free (buf); + g_free (log); + } + + g_hash_table_unref (logger->thread_index); + + g_free (logger); + ring_buffer_logger = NULL; + } + G_UNLOCK (ring_buffer_logger); +} + +/** + * gst_debug_add_ring_buffer_logger: + * @max_size_per_thread: Maximum size of log per thread in bytes + * @thread_timeout: Timeout for threads in seconds + * + * Adds a memory ringbuffer based debug logger that stores up to + * @max_size_per_thread bytes of logs per thread and times out threads after + * @thread_timeout seconds of inactivity. + * + * Logs can be fetched with gst_debug_ring_buffer_logger_get_logs() and the + * logger can be removed again with gst_debug_remove_ring_buffer_logger(). + * Only one logger at a time is possible. + * + * Since: 1.14 + */ +void +gst_debug_add_ring_buffer_logger (guint max_size_per_thread, + guint thread_timeout) +{ + GstRingBufferLogger *logger; + + G_LOCK (ring_buffer_logger); + + if (ring_buffer_logger) { + g_warn_if_reached (); + G_UNLOCK (ring_buffer_logger); + return; + } + + logger = ring_buffer_logger = g_new0 (GstRingBufferLogger, 1); + + logger->max_size_per_thread = max_size_per_thread; + logger->thread_timeout = thread_timeout; + logger->thread_index = g_hash_table_new (g_direct_hash, g_direct_equal); + g_queue_init (&logger->threads); + + gst_debug_add_log_function (gst_ring_buffer_logger_log, logger, + (GDestroyNotify) gst_ring_buffer_logger_free); + G_UNLOCK (ring_buffer_logger); +} + +/** + * gst_debug_remove_ring_buffer_logger: + * + * Removes any previously added ring buffer logger with + * gst_debug_add_ring_buffer_logger(). + * + * Since: 1.14 + */ +void +gst_debug_remove_ring_buffer_logger (void) +{ + gst_debug_remove_log_function (gst_ring_buffer_logger_log); +} + +#else /* GST_DISABLE_GST_DEBUG */ +#ifndef GST_REMOVE_DISABLED + +gchar ** +gst_debug_ring_buffer_logger_get_logs (void) +{ + return NULL; +} + +void +gst_debug_add_ring_buffer_logger (guint max_size_per_thread, + guint thread_timeout) +{ } -#endif /* GST_ENABLE_FUNC_INSTRUMENTATION */ +void +gst_debug_remove_ring_buffer_logger (void) +{ +} + +#endif /* GST_REMOVE_DISABLED */ +#endif /* GST_DISABLE_GST_DEBUG */