tracer: add a GFlag for the tracer scope
[platform/upstream/gstreamer.git] / gst / gsttracerutils.c
1 /* GStreamer
2  * Copyright (C) 2013 Stefan Sauer <ensonic@users.sf.net>
3  *
4  * gsttracerutils.c: 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:gsttracerutils
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  * The user can activate tracers by setting the environment variable GST_TRACE
30  * to a ';' separated list of tracers.
31  */
32
33 #include "gst_private.h"
34 #include "gsttracer.h"
35 #include "gsttracerfactory.h"
36 #include "gsttracerutils.h"
37
38 #ifndef GST_DISABLE_GST_TRACER_HOOKS
39
40 /* tracer quarks */
41
42 /* These strings must match order and number declared in the GstTracerQuarkId
43  * enum in gsttracerutils.h! */
44 static const gchar *_quark_strings[] = {
45   "pad-push-pre", "pad-push-post", "pad-push-list-pre", "pad-push-list-post",
46   "pad-pull-range-pre", "pad-pull-range-post", "pad-push-event-pre",
47   "pad-push-event-post", "pad-query-pre", "pad-query-post",
48   "element-post-message-pre",
49   "element-post-message-post", "element-query-pre", "element-query-post",
50   "element-new", "element-add-pad", "element-remove-pad",
51   "bin-add-pre", "bin-add-post", "bin-remove-pre", "bin-remove-post",
52   "pad-link-pre", "pad-link-post", "pad-unlink-pre", "pad-unlink-post",
53   "element-change-state-pre", "element-change-state-post"
54 };
55
56 GQuark _priv_gst_tracer_quark_table[GST_TRACER_QUARK_MAX];
57
58 /* tracing helpers */
59
60 gboolean _priv_tracer_enabled = FALSE;
61 GHashTable *_priv_tracers = NULL;
62
63 /* Initialize the tracing system */
64 void
65 _priv_gst_tracing_init (void)
66 {
67   const gchar *env = g_getenv ("GST_TRACER_PLUGINS");
68
69   if (env != NULL && *env != '\0') {
70     GstRegistry *registry = gst_registry_get ();
71     GstPluginFeature *feature;
72     GstTracerFactory *factory;
73     gchar **t = g_strsplit_set (env, ";", 0);
74     gint i = 0;
75     gchar *params;
76
77     GST_INFO ("enabling tracers: '%s'", env);
78
79     if (G_N_ELEMENTS (_quark_strings) != GST_TRACER_QUARK_MAX)
80       g_warning ("the quark table is not consistent! %d != %d",
81           (gint) G_N_ELEMENTS (_quark_strings), GST_TRACER_QUARK_MAX);
82
83     for (i = 0; i < GST_TRACER_QUARK_MAX; i++) {
84       _priv_gst_tracer_quark_table[i] =
85           g_quark_from_static_string (_quark_strings[i]);
86     }
87
88     _priv_tracers = g_hash_table_new (NULL, NULL);
89
90     i = 0;
91     while (t[i]) {
92       // check t[i] for params
93       if ((params = strchr (t[i], '('))) {
94         gchar *end = strchr (&params[1], ')');
95         *params = '\0';
96         params++;
97         if (end)
98           *end = '\0';
99       } else {
100         params = NULL;
101       }
102
103       GST_INFO ("checking tracer: '%s'", t[i]);
104
105       if ((feature = gst_registry_lookup_feature (registry, t[i]))) {
106         factory = GST_TRACER_FACTORY (gst_plugin_feature_load (feature));
107         if (factory) {
108           GST_INFO_OBJECT (factory, "creating tracer: type-id=%u",
109               (guint) factory->type);
110
111           /* tracers register them self to the hooks */
112           gst_object_unref (g_object_new (factory->type, "params", params,
113                   NULL));
114         } else {
115           GST_WARNING_OBJECT (feature,
116               "loading plugin containing feature %s failed!", t[i]);
117         }
118       } else {
119         GST_WARNING ("no tracer named '%s'", t[i]);
120       }
121       i++;
122     }
123     g_strfreev (t);
124   }
125 }
126
127 void
128 _priv_gst_tracing_deinit (void)
129 {
130   GList *h_list, *h_node, *t_node;
131   GstTracerHook *hook;
132
133   _priv_tracer_enabled = FALSE;
134   if (!_priv_tracers)
135     return;
136
137   /* shutdown tracers for final reports */
138   h_list = g_hash_table_get_values (_priv_tracers);
139   for (h_node = h_list; h_node; h_node = g_list_next (h_node)) {
140     for (t_node = h_node->data; t_node; t_node = g_list_next (t_node)) {
141       hook = (GstTracerHook *) t_node->data;
142       gst_object_unref (hook->tracer);
143       g_slice_free (GstTracerHook, hook);
144     }
145     g_list_free (h_node->data);
146   }
147   g_list_free (h_list);
148   g_hash_table_destroy (_priv_tracers);
149   _priv_tracers = NULL;
150 }
151
152 static void
153 gst_tracing_register_hook_id (GstTracer * tracer, GQuark detail, GCallback func)
154 {
155   gpointer key = GINT_TO_POINTER (detail);
156   GList *list = g_hash_table_lookup (_priv_tracers, key);
157   GstTracerHook *hook = g_slice_new0 (GstTracerHook);
158   hook->tracer = gst_object_ref (tracer);
159   hook->func = func;
160
161   list = g_list_prepend (list, hook);
162   g_hash_table_replace (_priv_tracers, key, list);
163   GST_DEBUG ("registering tracer for '%s', list.len=%d",
164       (detail ? g_quark_to_string (detail) : "*"), g_list_length (list));
165   _priv_tracer_enabled = TRUE;
166 }
167
168 /**
169  * gst_tracing_register_hook:
170  * @tracer: the tracer
171  * @detail: the detailed hook
172  * @func: (scope async): the callback
173  *
174  * Register @func to be called when the trace hook @detail is getting invoked.
175  * Use %NULL for @detail to register to all hooks.
176  */
177 void
178 gst_tracing_register_hook (GstTracer * tracer, const gchar * detail,
179     GCallback func)
180 {
181   gst_tracing_register_hook_id (tracer, g_quark_try_string (detail), func);
182 }
183
184 #endif /* GST_DISABLE_GST_TRACER_HOOKS */