2 * Copyright (C) 2013 Stefan Sauer <ensonic@users.sf.net>
4 * gsttracer.h: tracing subsystem
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
24 * @short_description: Tracing subsystem
26 * The tracing subsystem provides hooks in the core library and API for modules
29 * Tracing modules will subclass #GstTracer and register through
30 * gst_tracer_register(). Modules can attach to various hook-types - see
31 * #GstTracerHook. When invoked they receive hook specific contextual data,
32 * which they must not modify.
34 * The user can activate tracers by setting the environment variable GST_TRACE
35 * to a ';' separated list of tracers.
38 #include "gst_private.h"
39 #include "gstenumtypes.h"
40 #include "gstregistry.h"
41 #include "gsttracer.h"
42 #include "gsttracerfactory.h"
45 #ifndef GST_DISABLE_GST_DEBUG
47 GST_DEBUG_CATEGORY_EXTERN (tracer_debug);
48 #define GST_CAT_DEFAULT tracer_debug
50 /* tracing plugins base class */
60 static GParamSpec *properties[PROP_LAST];
62 static void gst_tracer_set_property (GObject * object, guint prop_id,
63 const GValue * value, GParamSpec * pspec);
64 static void gst_tracer_get_property (GObject * object, guint prop_id,
65 GValue * value, GParamSpec * pspec);
67 struct _GstTracerPrivate
73 #define gst_tracer_parent_class parent_class
74 G_DEFINE_ABSTRACT_TYPE (GstTracer, gst_tracer, GST_TYPE_OBJECT);
77 gst_tracer_class_init (GstTracerClass * klass)
79 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
81 gobject_class->set_property = gst_tracer_set_property;
82 gobject_class->get_property = gst_tracer_get_property;
84 properties[PROP_PARAMS] =
85 g_param_spec_string ("params", "Params", "Extra configuration parameters",
86 NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
88 properties[PROP_MASK] =
89 g_param_spec_flags ("mask", "Mask", "Event mask", GST_TYPE_TRACER_HOOK,
90 GST_TRACER_HOOK_NONE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
92 g_object_class_install_properties (gobject_class, PROP_LAST, properties);
93 g_type_class_add_private (klass, sizeof (GstTracerPrivate));
97 gst_tracer_init (GstTracer * tracer)
99 tracer->priv = G_TYPE_INSTANCE_GET_PRIVATE (tracer, GST_TYPE_TRACER,
104 gst_tracer_set_property (GObject * object, guint prop_id,
105 const GValue * value, GParamSpec * pspec)
107 GstTracer *self = GST_TRACER_CAST (object);
111 self->priv->params = g_value_get_string (value);
114 self->priv->mask = g_value_get_flags (value);
117 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
123 gst_tracer_get_property (GObject * object, guint prop_id,
124 GValue * value, GParamSpec * pspec)
126 GstTracer *self = GST_TRACER_CAST (object);
130 g_value_set_string (value, self->priv->params);
133 g_value_set_flags (value, self->priv->mask);
136 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
142 gst_tracer_invoke (GstTracer * self, GstTracerHookId hid,
143 GstTracerMessageId mid, va_list var_args)
145 GstTracerClass *klass = GST_TRACER_GET_CLASS (self);
147 g_return_if_fail (klass->invoke);
149 klass->invoke (self, hid, mid, var_args);
152 /* tracing modules */
155 gst_tracer_register (GstPlugin * plugin, const gchar * name, GType type)
157 GstPluginFeature *existing_feature;
158 GstRegistry *registry;
159 GstTracerFactory *factory;
161 g_return_val_if_fail (name != NULL, FALSE);
162 g_return_val_if_fail (g_type_is_a (type, GST_TYPE_TRACER), FALSE);
164 registry = gst_registry_get ();
165 /* check if feature already exists, if it exists there is no need to update it
166 * when the registry is getting updated, outdated plugins and all their
167 * features are removed and readded.
169 existing_feature = gst_registry_lookup_feature (registry, name);
170 if (existing_feature) {
171 GST_DEBUG_OBJECT (registry, "update existing feature %p (%s)",
172 existing_feature, name);
173 factory = GST_TRACER_FACTORY_CAST (existing_feature);
174 factory->type = type;
175 existing_feature->loaded = TRUE;
176 //g_type_set_qdata (type, __gst_elementclass_factory, factory);
177 gst_object_unref (existing_feature);
181 factory = g_object_newv (GST_TYPE_TRACER_FACTORY, 0, NULL);
182 GST_DEBUG_OBJECT (factory, "new tracer factory for %s", name);
184 gst_plugin_feature_set_name (GST_PLUGIN_FEATURE_CAST (factory), name);
185 gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE_CAST (factory),
188 factory->type = type;
189 GST_DEBUG_OBJECT (factory, "tracer factory for %u:%s",
190 (guint) type, g_type_name (type));
192 if (plugin && plugin->desc.name) {
193 GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = plugin->desc.name; /* interned string */
194 GST_PLUGIN_FEATURE_CAST (factory)->plugin = plugin;
195 g_object_add_weak_pointer ((GObject *) plugin,
196 (gpointer *) & GST_PLUGIN_FEATURE_CAST (factory)->plugin);
198 GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = "NULL";
199 GST_PLUGIN_FEATURE_CAST (factory)->plugin = NULL;
201 GST_PLUGIN_FEATURE_CAST (factory)->loaded = TRUE;
203 gst_registry_add_feature (gst_registry_get (),
204 GST_PLUGIN_FEATURE_CAST (factory));
209 /* tracing helpers */
211 gboolean _priv_tracer_enabled = FALSE;
212 /* TODO(ensonic): use GPtrArray ? */
213 GList *_priv_tracers[GST_TRACER_HOOK_ID_LAST] = { NULL, };
215 /* Initialize the debugging system */
217 _priv_gst_tracer_init (void)
219 const gchar *env = g_getenv ("GST_TRACE");
221 if (env != NULL && *env != '\0') {
222 GstRegistry *registry = gst_registry_get ();
223 GstPluginFeature *feature;
224 GstTracerFactory *factory;
227 gchar **t = g_strsplit_set (env, ";", 0);
231 GST_INFO ("enabling tracers: '%s'", env);
234 // check t[i] for params
235 if ((params = strchr (t[i], '('))) {
236 gchar *end = strchr (¶ms[1], ')');
245 GST_INFO ("checking tracer: '%s'", t[i]);
247 if ((feature = gst_registry_lookup_feature (registry, t[i]))) {
248 factory = GST_TRACER_FACTORY (gst_plugin_feature_load (feature));
250 GST_INFO_OBJECT (factory, "creating tracer: type-id=%u",
251 (guint) factory->type);
253 tracer = g_object_new (factory->type, "params", params, NULL);
254 g_object_get (tracer, "mask", &mask, NULL);
257 /* add to lists according to mask */
259 while (mask && (j < GST_TRACER_HOOK_ID_LAST)) {
261 _priv_tracers[j] = g_list_prepend (_priv_tracers[j],
262 gst_object_ref (tracer));
263 GST_WARNING_OBJECT (tracer, "added tracer to hook %d", j);
269 _priv_tracer_enabled = TRUE;
271 GST_WARNING_OBJECT (tracer,
272 "tracer with zero mask won't have any effect");
274 gst_object_unref (tracer);
276 GST_WARNING_OBJECT (feature,
277 "loading plugin containing feature %s failed!", t[i]);
280 GST_WARNING ("no tracer named '%s'", t[i]);
289 _priv_gst_tracer_deinit (void)
294 /* shutdown tracers for final reports */
295 for (i = 0; i < GST_TRACER_HOOK_ID_LAST; i++) {
296 for (node = _priv_tracers[i]; node; node = g_list_next (node)) {
297 gst_object_unref (node->data);
299 g_list_free (_priv_tracers[i]);
300 _priv_tracers[i] = NULL;
302 _priv_tracer_enabled = FALSE;
306 gst_tracer_dispatch (GstTracerHookId hid, GstTracerMessageId mid, ...)
311 for (node = _priv_tracers[hid]; node; node = g_list_next (node)) {
312 va_start (var_args, mid);
313 gst_tracer_invoke (node->data, hid, mid, var_args);
318 /* tracing module helpers */
321 gst_tracer_log_trace (GstStructure * s)
325 // TODO(ensonic): use a GVariant?
326 data = gst_structure_to_string (s);
327 GST_TRACE ("%s", data);
329 gst_structure_free (s);
332 #endif /* GST_DISABLE_GST_DEBUG */