parts of the patch submitted in bug #113913
[platform/upstream/gstreamer.git] / gst / gsttrace.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gsttrace.c: Tracing functions (depracated)
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <string.h>
30
31 #include "gst_private.h"
32
33 #include "gstlog.h"
34 #include "gsttrace.h"
35
36 static 
37 #ifdef __inline__
38 __inline__
39 #endif
40 void
41 read_tsc (gint64 * dst)
42 {
43 #ifdef HAVE_RDTSC
44   guint64 tsc;
45   __asm__ __volatile__ ("rdtsc":"=A" (tsc));
46
47   *dst = tsc;
48 #else
49   *dst = 0;
50 #endif
51 }
52
53 void
54 gst_trace_read_tsc (gint64 * dst)
55 {
56   read_tsc (dst);
57 }
58
59 GstTrace *_gst_trace_default = NULL;
60 gint _gst_trace_on = 1;
61
62 GstTrace *
63 gst_trace_new (gchar * filename, gint size)
64 {
65   GstTrace *trace = g_malloc (sizeof (GstTrace));
66
67   g_return_val_if_fail (trace != NULL, NULL);
68   trace->filename = g_strdup (filename);
69   g_print ("opening '%s'\n", trace->filename);
70   trace->fd = open (trace->filename, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
71   perror ("opening trace file");
72   g_return_val_if_fail (trace->fd > 0, NULL);
73   trace->buf = g_malloc (size * sizeof (GstTraceEntry));
74   g_return_val_if_fail (trace->buf != NULL, NULL);
75   trace->bufsize = size;
76   trace->bufoffset = 0;
77
78   return trace;
79 }
80
81 void
82 gst_trace_destroy (GstTrace * trace)
83 {
84   g_return_if_fail (trace != NULL);
85   g_return_if_fail (trace->buf != NULL);
86
87   if (gst_trace_get_remaining (trace) > 0)
88     gst_trace_flush (trace);
89   close (trace->fd);
90   g_free (trace->buf);
91   g_free (trace);
92 }
93
94 void
95 gst_trace_flush (GstTrace * trace)
96 {
97   if (!trace) {
98     trace = _gst_trace_default;
99     if (!trace)
100       return;
101   }
102
103   write (trace->fd, trace->buf, trace->bufoffset * sizeof (GstTraceEntry));
104   trace->bufoffset = 0;
105 }
106
107 void
108 gst_trace_text_flush (GstTrace * trace)
109 {
110   int i;
111 #define STRSIZE (20 + 1 + 10 + 1 + 10 + 1 + 112 + 1 + 1)
112   char str[STRSIZE];
113
114   if (!trace) {
115     trace = _gst_trace_default;
116     if (!trace)
117       return;
118   }
119
120   for (i = 0; i < trace->bufoffset; i++) {
121     snprintf (str, STRSIZE, "%20" G_GINT64_FORMAT " %10d %10d %s\n",
122               trace->buf[i].timestamp,
123               trace->buf[i].sequence, trace->buf[i].data, trace->buf[i].message);
124     write (trace->fd, str, strlen (str));
125   }
126   trace->bufoffset = 0;
127 #undef STRSIZE
128 }
129
130 void
131 gst_trace_set_default (GstTrace * trace)
132 {
133   g_return_if_fail (trace != NULL);
134   _gst_trace_default = trace;
135 }
136
137 void
138 _gst_trace_add_entry (GstTrace * trace, guint32 seq, guint32 data, gchar * msg)
139 {
140   GstTraceEntry *entry;
141
142   if (!trace) {
143     trace = _gst_trace_default;
144     if (!trace)
145       return;
146   }
147
148   entry = trace->buf + trace->bufoffset;
149   read_tsc (&(entry->timestamp));
150   entry->sequence = seq;
151   entry->data = data;
152   strncpy (entry->message, msg, 112);
153   trace->bufoffset++;
154
155   gst_trace_flush (trace);
156 }
157
158
159 /* global flags */
160 static GstAllocTraceFlags _gst_trace_flags = 0;
161 /* list of registered tracers */
162 static GList *_gst_alloc_tracers = NULL;
163
164 /**
165  * gst_alloc_trace_available:
166  *
167  * Check if alloc tracing was commiled into the core
168  *
169  * Returns: TRUE if the core was compiled with alloc
170  * tracing enabled.
171  */
172 gboolean
173 gst_alloc_trace_available (void)
174 {
175 #ifdef GST_DISABLE_ALLOC_TRACE
176   return FALSE;
177 #else
178   return TRUE;
179 #endif
180 }
181
182 /**
183  * _gst_alloc_trace_register:
184  * @name: the name of the new alloc trace object.
185  *
186  * Register an get a handle to a GstAllocTrace object that
187  * can be used to trace memory allocations.
188  *
189  * Returns: A handle to a GstAllocTrace.
190  */
191 GstAllocTrace*
192 _gst_alloc_trace_register (const gchar *name)
193 {
194   GstAllocTrace *trace;
195
196   g_return_val_if_fail (name, NULL);
197
198   trace = g_new0 (GstAllocTrace, 1);
199   trace->name = g_strdup (name);
200   trace->live = 0;
201   trace->mem_live = NULL;
202   trace->flags = _gst_trace_flags;
203
204   _gst_alloc_tracers = g_list_prepend (_gst_alloc_tracers, trace);
205
206   return trace;
207 }
208
209 /**
210  * gst_alloc_trace_list:
211  *
212  * Get a list of all registered alloc trace objects.
213  *
214  * Returns: a GList of GstAllocTrace objects.
215  */
216 const GList*
217 gst_alloc_trace_list (void)
218 {
219   return _gst_alloc_tracers;
220 }
221
222 /**
223  * gst_alloc_trace_live_all:
224  *
225  * Returns the total number of live registered alloc trace objects.
226  */
227 int
228 gst_alloc_trace_live_all (void)
229 {
230   GList *walk = _gst_alloc_tracers;
231   int num = 0;
232   
233   while (walk) {
234     GstAllocTrace *trace = (GstAllocTrace *) walk->data;
235
236     num += trace->live;
237
238     walk = g_list_next (walk);
239   }
240
241   return num;
242 }
243
244 /**
245  * gst_alloc_trace_print_all:
246  *
247  * Print the status of all registered alloc trace objectes.
248  */
249 void
250 gst_alloc_trace_print_all (void)
251 {
252   GList *walk = _gst_alloc_tracers;
253   
254   while (walk) {
255     GstAllocTrace *trace = (GstAllocTrace *) walk->data;
256
257     gst_alloc_trace_print (trace);
258
259     walk = g_list_next (walk);
260   }
261 }
262
263 /**
264  * gst_alloc_trace_set_flags_all:
265  * @flags: the options to enable
266  *
267  * Enable the specified options on all registered alloc trace
268  * objects.
269  */
270 void
271 gst_alloc_trace_set_flags_all (GstAllocTraceFlags flags)
272 {
273   GList *walk = _gst_alloc_tracers;
274
275   while (walk) {
276     GstAllocTrace *trace = (GstAllocTrace *) walk->data;
277
278     g_print ("set flags on %p\n", trace);
279     gst_alloc_trace_set_flags (trace, flags);
280
281     walk = g_list_next (walk);
282   }
283   _gst_trace_flags = flags;
284 }
285
286 /**
287  * gst_alloc_trace_get:
288  * @name: the name of the alloc trace object
289  *
290  * Get the named alloc trace object.
291  *
292  * Returns: a GstAllocTrace with the given name or NULL when
293  * no alloc tracer was registered with that name.
294  */
295 GstAllocTrace*
296 gst_alloc_trace_get (const gchar *name)
297 {
298   GList *walk = _gst_alloc_tracers;
299
300   g_return_val_if_fail (name, NULL);
301
302   while (walk) {
303     GstAllocTrace *trace = (GstAllocTrace *) walk->data;
304
305     if (!strcmp (trace->name, name))
306       return trace;
307
308     walk = g_list_next (walk);
309   }
310   return NULL;
311 }
312
313 /**
314  * gst_alloc_trace_print:
315  * @trace: the GstAllocTrace to print
316  *
317  * Print the status of the given GstAllocTrace.
318  */
319 void
320 gst_alloc_trace_print (const GstAllocTrace *trace)
321 {
322   GSList *mem_live;
323
324   g_return_if_fail (trace != NULL);
325
326   g_print ("%s (%p): flags %d", trace->name, trace, trace->flags);
327
328   if (trace->flags & GST_ALLOC_TRACE_LIVE) {
329     g_print (", live %d", trace->live);
330   }
331   if (trace->flags & GST_ALLOC_TRACE_MEM_LIVE) {
332     mem_live = trace->mem_live;
333
334     if (!mem_live) {
335       g_print (", no live memory");
336     }
337     else {
338       g_print (", dumping live memory: ");
339
340       while (mem_live) {
341         g_print ("%p ", mem_live->data);
342         mem_live = g_slist_next (mem_live);
343       }
344       g_print ("\ntotal %d", g_slist_length (trace->mem_live));
345     }
346   }
347   g_print ("\n");
348 }
349
350 /**
351  * gst_alloc_trace_set_flags:
352  * @trace: the GstAllocTrace 
353  * @flags: flags to set 
354  *
355  * Enable the given features on the given GstAllocTrace object.
356  */
357 void
358 gst_alloc_trace_set_flags (GstAllocTrace *trace, GstAllocTraceFlags flags)
359 {
360   g_return_if_fail (trace != NULL);
361
362   trace->flags = flags;
363 }