docs: add gtk-doc blobs
[platform/upstream/gstreamer.git] / gst / gsttracer.c
1 /* GStreamer
2  * Copyright (C) 2013 Stefan Sauer <ensonic@users.sf.net>
3  *
4  * gsttracer.h: tracing subsystem
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 /**
23  * SECTION:gsttracer
24  * @short_description: Tracing subsystem
25  *
26  * The tracing subsystem provides hooks in the core library and API for modules
27  * to attach to them.
28  *
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.
33  *
34  * The user can activate tracers by setting the environment variable GST_TRACE
35  * to a ';' separated list of tracers.
36  */
37
38 #include "gst_private.h"
39 #include "gstenumtypes.h"
40 #include "gstregistry.h"
41 #include "gsttracer.h"
42 #include "gsttracerfactory.h"
43 #include "gstutils.h"
44
45 #ifndef GST_DISABLE_GST_DEBUG
46
47 GST_DEBUG_CATEGORY_EXTERN (tracer_debug);
48 #define GST_CAT_DEFAULT tracer_debug
49
50 /* tracing plugins base class */
51
52 enum
53 {
54   PROP_0,
55   PROP_PARAMS,
56   PROP_MASK,
57   PROP_LAST
58 };
59
60 static GParamSpec *properties[PROP_LAST];
61
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);
66
67 struct _GstTracerPrivate
68 {
69   const gchar *params;
70   GstTracerHook mask;
71 };
72
73 #define gst_tracer_parent_class parent_class
74 G_DEFINE_ABSTRACT_TYPE (GstTracer, gst_tracer, GST_TYPE_OBJECT);
75
76 static void
77 gst_tracer_class_init (GstTracerClass * klass)
78 {
79   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
80
81   gobject_class->set_property = gst_tracer_set_property;
82   gobject_class->get_property = gst_tracer_get_property;
83
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);
87
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);
91
92   g_object_class_install_properties (gobject_class, PROP_LAST, properties);
93   g_type_class_add_private (klass, sizeof (GstTracerPrivate));
94 }
95
96 static void
97 gst_tracer_init (GstTracer * tracer)
98 {
99   tracer->priv = G_TYPE_INSTANCE_GET_PRIVATE (tracer, GST_TYPE_TRACER,
100       GstTracerPrivate);
101 }
102
103 static void
104 gst_tracer_set_property (GObject * object, guint prop_id,
105     const GValue * value, GParamSpec * pspec)
106 {
107   GstTracer *self = GST_TRACER_CAST (object);
108
109   switch (prop_id) {
110     case PROP_PARAMS:
111       self->priv->params = g_value_get_string (value);
112       break;
113     case PROP_MASK:
114       self->priv->mask = g_value_get_flags (value);
115       break;
116     default:
117       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
118       break;
119   }
120 }
121
122 static void
123 gst_tracer_get_property (GObject * object, guint prop_id,
124     GValue * value, GParamSpec * pspec)
125 {
126   GstTracer *self = GST_TRACER_CAST (object);
127
128   switch (prop_id) {
129     case PROP_PARAMS:
130       g_value_set_string (value, self->priv->params);
131       break;
132     case PROP_MASK:
133       g_value_set_flags (value, self->priv->mask);
134       break;
135     default:
136       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
137       break;
138   }
139 }
140
141 static void
142 gst_tracer_invoke (GstTracer * self, GstTracerHookId hid,
143     GstTracerMessageId mid, va_list var_args)
144 {
145   GstTracerClass *klass = GST_TRACER_GET_CLASS (self);
146
147   g_return_if_fail (klass->invoke);
148
149   klass->invoke (self, hid, mid, var_args);
150 }
151
152 /* tracing modules */
153
154 gboolean
155 gst_tracer_register (GstPlugin * plugin, const gchar * name, GType type)
156 {
157   GstPluginFeature *existing_feature;
158   GstRegistry *registry;
159   GstTracerFactory *factory;
160
161   g_return_val_if_fail (name != NULL, FALSE);
162   g_return_val_if_fail (g_type_is_a (type, GST_TYPE_TRACER), FALSE);
163
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.
168    */
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);
178     return TRUE;
179   }
180
181   factory = g_object_newv (GST_TYPE_TRACER_FACTORY, 0, NULL);
182   GST_DEBUG_OBJECT (factory, "new tracer factory for %s", name);
183
184   gst_plugin_feature_set_name (GST_PLUGIN_FEATURE_CAST (factory), name);
185   gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE_CAST (factory),
186       GST_RANK_NONE);
187
188   factory->type = type;
189   GST_DEBUG_OBJECT (factory, "tracer factory for %u:%s",
190       (guint) type, g_type_name (type));
191
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);
197   } else {
198     GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = "NULL";
199     GST_PLUGIN_FEATURE_CAST (factory)->plugin = NULL;
200   }
201   GST_PLUGIN_FEATURE_CAST (factory)->loaded = TRUE;
202
203   gst_registry_add_feature (gst_registry_get (),
204       GST_PLUGIN_FEATURE_CAST (factory));
205
206   return TRUE;
207 }
208
209 /* tracing helpers */
210
211 gboolean _priv_tracer_enabled = FALSE;
212 /* TODO(ensonic): use GPtrArray ? */
213 GList *_priv_tracers[GST_TRACER_HOOK_ID_LAST] = { NULL, };
214
215 /* Initialize the debugging system */
216 void
217 _priv_gst_tracer_init (void)
218 {
219   const gchar *env = g_getenv ("GST_TRACE");
220
221   if (env != NULL && *env != '\0') {
222     GstRegistry *registry = gst_registry_get ();
223     GstPluginFeature *feature;
224     GstTracerFactory *factory;
225     GstTracerHook mask;
226     GstTracer *tracer;
227     gchar **t = g_strsplit_set (env, ";", 0);
228     gint i = 0, j;
229     gchar *params;
230
231     GST_INFO ("enabling tracers: '%s'", env);
232
233     while (t[i]) {
234       // check t[i] for params
235       if ((params = strchr (t[i], '('))) {
236         gchar *end = strchr (&params[1], ')');
237         *params = '\0';
238         params++;
239         if (end)
240           *end = '\0';
241       } else {
242         params = NULL;
243       }
244
245       GST_INFO ("checking tracer: '%s'", t[i]);
246
247       if ((feature = gst_registry_lookup_feature (registry, t[i]))) {
248         factory = GST_TRACER_FACTORY (gst_plugin_feature_load (feature));
249         if (factory) {
250           GST_INFO_OBJECT (factory, "creating tracer: type-id=%u",
251               (guint) factory->type);
252
253           tracer = g_object_new (factory->type, "params", params, NULL);
254           g_object_get (tracer, "mask", &mask, NULL);
255
256           if (mask) {
257             /* add to lists according to mask */
258             j = 0;
259             while (mask && (j < GST_TRACER_HOOK_ID_LAST)) {
260               if (mask & 1) {
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);
264               }
265               mask >>= 1;
266               j++;
267             }
268
269             _priv_tracer_enabled = TRUE;
270           } else {
271             GST_WARNING_OBJECT (tracer,
272                 "tracer with zero mask won't have any effect");
273           }
274           gst_object_unref (tracer);
275         } else {
276           GST_WARNING_OBJECT (feature,
277               "loading plugin containing feature %s failed!", t[i]);
278         }
279       } else {
280         GST_WARNING ("no tracer named '%s'", t[i]);
281       }
282       i++;
283     }
284     g_strfreev (t);
285   }
286 }
287
288 void
289 _priv_gst_tracer_deinit (void)
290 {
291   gint i;
292   GList *node;
293
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);
298     }
299     g_list_free (_priv_tracers[i]);
300     _priv_tracers[i] = NULL;
301   }
302   _priv_tracer_enabled = FALSE;
303 }
304
305 void
306 gst_tracer_dispatch (GstTracerHookId hid, GstTracerMessageId mid, ...)
307 {
308   va_list var_args;
309   GList *node;
310
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);
314     va_end (var_args);
315   }
316 }
317
318 /* tracing module helpers */
319
320 void
321 gst_tracer_log_trace (GstStructure * s)
322 {
323   gchar *data;
324
325   // TODO(ensonic): use a GVariant?
326   data = gst_structure_to_string (s);
327   GST_TRACE ("%s", data);
328   g_free (data);
329   gst_structure_free (s);
330 }
331
332 #endif /* GST_DISABLE_GST_DEBUG */