miniobject: fix debug
[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 (deprecated)
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  * SECTION:gsttrace
25  * @short_description: Tracing functionality
26  *
27  * Traces allows to track object allocation. They provide a instance counter per
28  * #GType. The counter is incremented for each object allocated and decremented
29  * it when it's freed.
30  *
31  * <example>
32  * <title>Tracing object instances</title>
33  *   <programlisting>
34  *     // trace un-freed object instances
35  *     gst_alloc_trace_set_flags_all (GST_ALLOC_TRACE_LIVE);
36  *     if (!gst_alloc_trace_available ()) {
37  *       g_warning ("Trace not available (recompile with trace enabled).");
38  *     }
39  *     gst_alloc_trace_print_live ();
40  *     // do something here
41  *     gst_alloc_trace_print_live ();
42  *   </programlisting>
43  * </example>
44  *
45  * Last reviewed on 2005-11-21 (0.9.5)
46  */
47
48 #ifdef HAVE_CONFIG_H
49 #include "config.h"
50 #endif
51 #include <stdio.h>
52 #ifdef HAVE_UNISTD_H
53 #include <unistd.h>
54 #endif
55 #include <sys/stat.h>
56 #include <fcntl.h>
57 #include <string.h>
58
59 #if defined (_MSC_VER) && _MSC_VER >= 1400
60 # include <io.h>
61 #endif
62
63 #include "gst_private.h"
64 #include "gstinfo.h"
65
66 #include "gsttrace.h"
67
68 GStaticMutex _gst_trace_mutex = G_STATIC_MUTEX_INIT;
69
70 static
71 #ifdef __inline__
72   __inline__
73 #endif
74     void
75 read_tsc (gint64 * dst)
76 {
77 #if defined(HAVE_RDTSC) && defined(__GNUC__)
78   guint64 tsc;
79   __asm__ __volatile__ ("rdtsc":"=A" (tsc));
80
81   *dst = tsc;
82 #else
83   *dst = 0;
84 #endif
85 }
86
87 /**
88  * gst_trace_read_tsc:
89  * @dst: (out) pointer to hold the result.
90  *
91  * Read a platform independent timer value that can be used in
92  * benchmarks.
93  */
94 void
95 gst_trace_read_tsc (gint64 * dst)
96 {
97   read_tsc (dst);
98 }
99
100 static GstTrace *_gst_trace_default = NULL;
101 gint _gst_trace_on = 1;
102
103 /**
104  * gst_trace_new:
105  * @filename: a filename
106  * @size: the max size of the file
107  *
108  * Create a ringbuffer of @size in the file with @filename to
109  * store trace results in.
110  *
111  * Free-function: gst_trace_destroy
112  *
113  * Returns: (transfer full): a new #GstTrace.
114  */
115 GstTrace *
116 gst_trace_new (const gchar * filename, gint size)
117 {
118   GstTrace *trace = g_slice_new (GstTrace);
119
120   g_return_val_if_fail (trace != NULL, NULL);
121   trace->filename = g_strdup (filename);
122   GST_DEBUG ("opening '%s'", trace->filename);
123 #ifndef S_IWUSR
124 #define S_IWUSR S_IWRITE
125 #endif
126 #ifndef S_IRUSR
127 #define S_IRUSR S_IREAD
128 #endif
129   trace->fd =
130       open (trace->filename, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
131   perror ("opening trace file");
132   g_return_val_if_fail (trace->fd > 0, NULL);
133   trace->buf = g_malloc (size * sizeof (GstTraceEntry));
134   g_return_val_if_fail (trace->buf != NULL, NULL);
135   trace->bufsize = size;
136   trace->bufoffset = 0;
137
138   return trace;
139 }
140
141 /**
142  * gst_trace_destroy:
143  * @trace: (in) (transfer full): the #GstTrace to destroy
144  *
145  * Flush an close the previously allocated @trace.
146  */
147 void
148 gst_trace_destroy (GstTrace * trace)
149 {
150   g_return_if_fail (trace != NULL);
151   g_return_if_fail (trace->buf != NULL);
152
153   if (gst_trace_get_remaining (trace) > 0)
154     gst_trace_flush (trace);
155   close (trace->fd);
156   g_free (trace->buf);
157   g_slice_free (GstTrace, trace);
158 }
159
160 /**
161  * gst_trace_flush:
162  * @trace: the #GstTrace to flush.
163  *
164  * Flush any pending trace entries in @trace to the trace file.
165  * @trace can be NULL in which case the default #GstTrace will be
166  * flushed.
167  */
168 void
169 gst_trace_flush (GstTrace * trace)
170 {
171   if (!trace) {
172     trace = _gst_trace_default;
173     if (!trace)
174       return;
175   }
176
177   g_return_if_fail (write (trace->fd, trace->buf,
178           trace->bufoffset * sizeof (GstTraceEntry)) != -1);
179   trace->bufoffset = 0;
180 }
181
182 /**
183  * gst_trace_text_flush:
184  * @trace: the #GstTrace to flush.
185  *
186  * Flush any pending trace entries in @trace to the trace file,
187  * formatted as a text line with timestamp and sequence numbers.
188  * @trace can be NULL in which case the default #GstTrace will be
189  * flushed.
190  */
191 void
192 gst_trace_text_flush (GstTrace * trace)
193 {
194   int i;
195
196 #define STRSIZE (20 + 1 + 10 + 1 + 10 + 1 + 112 + 1 + 1)
197   char str[STRSIZE];
198
199   if (!trace) {
200     trace = _gst_trace_default;
201     if (!trace)
202       return;
203   }
204
205   for (i = 0; i < trace->bufoffset; i++) {
206     g_snprintf (str, STRSIZE, "%20" G_GINT64_FORMAT " %10d %10d %s\n",
207         trace->buf[i].timestamp,
208         trace->buf[i].sequence, trace->buf[i].data, trace->buf[i].message);
209     g_return_if_fail (write (trace->fd, str, strlen (str)) != -1);
210   }
211   trace->bufoffset = 0;
212 #undef STRSIZE
213 }
214
215 /**
216  * gst_trace_set_default:
217  * @trace: the #GstTrace to set as the default.
218  *
219  * Set the default #GstTrace to @trace.
220  */
221 void
222 gst_trace_set_default (GstTrace * trace)
223 {
224   g_return_if_fail (trace != NULL);
225   _gst_trace_default = trace;
226 }
227
228 void
229 _gst_trace_add_entry (GstTrace * trace, guint32 seq, guint32 data, gchar * msg)
230 {
231   GstTraceEntry *entry;
232
233   if (!trace) {
234     trace = _gst_trace_default;
235     if (!trace)
236       return;
237   }
238
239   entry = trace->buf + trace->bufoffset;
240   read_tsc (&(entry->timestamp));
241   entry->sequence = seq;
242   entry->data = data;
243   strncpy (entry->message, msg, 112);
244   entry->message[111] = '\0';
245   trace->bufoffset++;
246
247   gst_trace_flush (trace);
248 }
249
250
251 /* global flags */
252 static GstAllocTraceFlags _gst_trace_flags = 0;
253
254 /* list of registered tracers */
255 static GList *_gst_alloc_tracers = NULL;
256
257 /**
258  * gst_alloc_trace_available:
259  *
260  * Check if alloc tracing was compiled into the core
261  *
262  * Returns: TRUE if the core was compiled with alloc
263  * tracing enabled.
264  */
265 gboolean
266 gst_alloc_trace_available (void)
267 {
268 #ifdef GST_DISABLE_ALLOC_TRACE
269   return FALSE;
270 #else
271   return TRUE;
272 #endif
273 }
274
275 /**
276  * _gst_alloc_trace_register:
277  * @name: the name of the new alloc trace object.
278  *
279  * Register an get a handle to a GstAllocTrace object that
280  * can be used to trace memory allocations.
281  *
282  * Returns: A handle to a GstAllocTrace.
283  */
284 GstAllocTrace *
285 _gst_alloc_trace_register (const gchar * name)
286 {
287   GstAllocTrace *trace;
288
289   g_return_val_if_fail (name, NULL);
290
291   trace = g_slice_new (GstAllocTrace);
292   trace->name = g_strdup (name);
293   trace->live = 0;
294   trace->mem_live = NULL;
295   trace->flags = _gst_trace_flags;
296
297   _gst_alloc_tracers = g_list_prepend (_gst_alloc_tracers, trace);
298
299   return trace;
300 }
301
302 /**
303  * gst_alloc_trace_list:
304  *
305  * Get a list of all registered alloc trace objects.
306  *
307  * Returns: a GList of GstAllocTrace objects.
308  */
309 const GList *
310 gst_alloc_trace_list (void)
311 {
312   return _gst_alloc_tracers;
313 }
314
315 /**
316  * gst_alloc_trace_live_all:
317  *
318  * Get the total number of live registered alloc trace objects.
319  *
320  * Returns: the total number of live registered alloc trace objects.
321  */
322 int
323 gst_alloc_trace_live_all (void)
324 {
325   GList *walk = _gst_alloc_tracers;
326   int num = 0;
327
328   while (walk) {
329     GstAllocTrace *trace = (GstAllocTrace *) walk->data;
330
331     num += trace->live;
332
333     walk = g_list_next (walk);
334   }
335
336   return num;
337 }
338
339 static gint
340 compare_func (GstAllocTrace * a, GstAllocTrace * b)
341 {
342   return strcmp (a->name, b->name);
343 }
344
345 static GList *
346 gst_alloc_trace_list_sorted (void)
347 {
348   GList *ret;
349
350   ret = g_list_sort (g_list_copy (_gst_alloc_tracers),
351       (GCompareFunc) compare_func);
352
353   return ret;
354 }
355
356 /**
357  * gst_alloc_trace_print_all:
358  *
359  * Print the status of all registered alloc trace objects.
360  */
361 void
362 gst_alloc_trace_print_all (void)
363 {
364   GList *orig, *walk;
365
366   orig = walk = gst_alloc_trace_list_sorted ();
367
368   while (walk) {
369     GstAllocTrace *trace = (GstAllocTrace *) walk->data;
370
371     gst_alloc_trace_print (trace);
372
373     walk = g_list_next (walk);
374   }
375
376   g_list_free (orig);
377 }
378
379 /**
380  * gst_alloc_trace_print_live:
381  *
382  * Print the status of all registered alloc trace objects, ignoring those
383  * without live objects.
384  */
385 void
386 gst_alloc_trace_print_live (void)
387 {
388   GList *orig, *walk;
389
390   orig = walk = gst_alloc_trace_list_sorted ();
391
392   while (walk) {
393     GstAllocTrace *trace = (GstAllocTrace *) walk->data;
394
395     if (trace->live)
396       gst_alloc_trace_print (trace);
397
398     walk = g_list_next (walk);
399   }
400
401   g_list_free (orig);
402 }
403
404 /**
405  * gst_alloc_trace_set_flags_all:
406  * @flags: the options to enable
407  *
408  * Enable the specified options on all registered alloc trace
409  * objects.
410  */
411 void
412 gst_alloc_trace_set_flags_all (GstAllocTraceFlags flags)
413 {
414   GList *walk = _gst_alloc_tracers;
415
416   while (walk) {
417     GstAllocTrace *trace = (GstAllocTrace *) walk->data;
418
419     GST_DEBUG ("setting flags %d on %p", (gint) flags, trace);
420     gst_alloc_trace_set_flags (trace, flags);
421
422     walk = g_list_next (walk);
423   }
424   _gst_trace_flags = flags;
425 }
426
427 /**
428  * gst_alloc_trace_get:
429  * @name: the name of the alloc trace object
430  *
431  * Get the named alloc trace object.
432  *
433  * Returns: a GstAllocTrace with the given name or NULL when
434  * no alloc tracer was registered with that name.
435  */
436 GstAllocTrace *
437 gst_alloc_trace_get (const gchar * name)
438 {
439   GList *walk = _gst_alloc_tracers;
440
441   g_return_val_if_fail (name, NULL);
442
443   while (walk) {
444     GstAllocTrace *trace = (GstAllocTrace *) walk->data;
445
446     if (!strcmp (trace->name, name))
447       return trace;
448
449     walk = g_list_next (walk);
450   }
451   return NULL;
452 }
453
454 /**
455  * gst_alloc_trace_print:
456  * @trace: the GstAllocTrace to print
457  *
458  * Print the status of the given GstAllocTrace.
459  */
460 void
461 gst_alloc_trace_print (const GstAllocTrace * trace)
462 {
463   GSList *mem_live;
464
465   g_return_if_fail (trace != NULL);
466
467   if (trace->flags & GST_ALLOC_TRACE_LIVE) {
468     g_print ("%-22.22s : %d\n", trace->name, trace->live);
469   } else {
470     g_print ("%-22.22s : (no live count)\n", trace->name);
471   }
472
473   if (trace->flags & GST_ALLOC_TRACE_MEM_LIVE) {
474     mem_live = trace->mem_live;
475
476     while (mem_live) {
477       gpointer data = mem_live->data;
478
479       if (G_IS_OBJECT (data)) {
480         g_print ("%-22.22s : %p\n", g_type_name (G_OBJECT_TYPE (data)), data);
481       } else {
482         g_print ("%-22.22s : %p\n", "", data);
483       }
484       mem_live = mem_live->next;
485     }
486   }
487 }
488
489 /**
490  * gst_alloc_trace_set_flags:
491  * @trace: the GstAllocTrace
492  * @flags: flags to set
493  *
494  * Enable the given features on the given GstAllocTrace object.
495  */
496 void
497 gst_alloc_trace_set_flags (GstAllocTrace * trace, GstAllocTraceFlags flags)
498 {
499   g_return_if_fail (trace != NULL);
500
501   trace->flags = flags;
502 }