fix doc build fix autogen
[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   const int 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 }
128
129 void
130 gst_trace_set_default (GstTrace * trace)
131 {
132   g_return_if_fail (trace != NULL);
133   _gst_trace_default = trace;
134 }
135
136 void
137 _gst_trace_add_entry (GstTrace * trace, guint32 seq, guint32 data, gchar * msg)
138 {
139   GstTraceEntry *entry;
140
141   if (!trace) {
142     trace = _gst_trace_default;
143     if (!trace)
144       return;
145   }
146
147   entry = trace->buf + trace->bufoffset;
148   read_tsc (&(entry->timestamp));
149   entry->sequence = seq;
150   entry->data = data;
151   strncpy (entry->message, msg, 112);
152   trace->bufoffset++;
153
154   gst_trace_flush (trace);
155 }
156
157
158 /* global flags */
159 static GstAllocTraceFlags _gst_trace_flags = 0;
160 /* list of registered tracers */
161 static GList *_gst_alloc_tracers = NULL;
162
163 /**
164  * gst_alloc_trace_available:
165  *
166  * Check if alloc tracing was commiled into the core
167  *
168  * Returns: TRUE if the core was compiled with alloc
169  * tracing enabled.
170  */
171 gboolean
172 gst_alloc_trace_available (void)
173 {
174 #ifdef GST_DISABLE_ALLOC_TRACE
175   return FALSE;
176 #else
177   return TRUE;
178 #endif
179 }
180
181 /**
182  * _gst_alloc_trace_register:
183  * @name: the name of the new alloc trace object.
184  *
185  * Register an get a handle to a GstAllocTrace object that
186  * can be used to trace memory allocations.
187  *
188  * Returns: A handle to a GstAllocTrace.
189  */
190 GstAllocTrace*
191 _gst_alloc_trace_register (const gchar *name)
192 {
193   GstAllocTrace *trace;
194
195   g_return_val_if_fail (name, NULL);
196
197   trace = g_new0 (GstAllocTrace, 1);
198   trace->name = g_strdup (name);
199   trace->live = 0;
200   trace->mem_live = NULL;
201   trace->flags = _gst_trace_flags;
202
203   _gst_alloc_tracers = g_list_prepend (_gst_alloc_tracers, trace);
204
205   return trace;
206 }
207
208 /**
209  * gst_alloc_trace_list:
210  *
211  * Get a list of all registered alloc trace objects.
212  *
213  * Returns: a GList of GstAllocTrace objects.
214  */
215 const GList*
216 gst_alloc_trace_list (void)
217 {
218   return _gst_alloc_tracers;
219 }
220
221 /**
222  * gst_alloc_trace_live_all:
223  *
224  * Returns the total number of live registered alloc trace objects.
225  */
226 int
227 gst_alloc_trace_live_all (void)
228 {
229   GList *walk = _gst_alloc_tracers;
230   int num = 0;
231   
232   while (walk) {
233     GstAllocTrace *trace = (GstAllocTrace *) walk->data;
234
235     num += trace->live;
236
237     walk = g_list_next (walk);
238   }
239
240   return num;
241 }
242
243 /**
244  * gst_alloc_trace_print_all:
245  *
246  * Print the status of all registered alloc trace objectes.
247  */
248 void
249 gst_alloc_trace_print_all (void)
250 {
251   GList *walk = _gst_alloc_tracers;
252   
253   while (walk) {
254     GstAllocTrace *trace = (GstAllocTrace *) walk->data;
255
256     gst_alloc_trace_print (trace);
257
258     walk = g_list_next (walk);
259   }
260 }
261
262 /**
263  * gst_alloc_trace_set_flags_all:
264  * @flags: the options to enable
265  *
266  * Enable the specified options on all registered alloc trace
267  * objects.
268  */
269 void
270 gst_alloc_trace_set_flags_all (GstAllocTraceFlags flags)
271 {
272   GList *walk = _gst_alloc_tracers;
273
274   while (walk) {
275     GstAllocTrace *trace = (GstAllocTrace *) walk->data;
276
277     g_print ("set flags on %p\n", trace);
278     gst_alloc_trace_set_flags (trace, flags);
279
280     walk = g_list_next (walk);
281   }
282   _gst_trace_flags = flags;
283 }
284
285 /**
286  * gst_alloc_trace_get:
287  * @name: the name of the alloc trace object
288  *
289  * Get the named alloc trace object.
290  *
291  * Returns: a GstAllocTrace with the given name or NULL when
292  * no alloc tracer was registered with that name.
293  */
294 GstAllocTrace*
295 gst_alloc_trace_get (const gchar *name)
296 {
297   GList *walk = _gst_alloc_tracers;
298
299   g_return_val_if_fail (name, NULL);
300
301   while (walk) {
302     GstAllocTrace *trace = (GstAllocTrace *) walk->data;
303
304     if (!strcmp (trace->name, name))
305       return trace;
306
307     walk = g_list_next (walk);
308   }
309   return NULL;
310 }
311
312 /**
313  * gst_alloc_trace_print:
314  * @trace: the GstAllocTrace to print
315  *
316  * Print the status of the given GstAllocTrace.
317  */
318 void
319 gst_alloc_trace_print (const GstAllocTrace *trace)
320 {
321   GSList *mem_live;
322
323   g_return_if_fail (trace != NULL);
324
325   g_print ("%s (%p): flags %d", trace->name, trace, trace->flags);
326
327   if (trace->flags & GST_ALLOC_TRACE_LIVE) {
328     g_print (", live %d", trace->live);
329   }
330   if (trace->flags & GST_ALLOC_TRACE_MEM_LIVE) {
331     mem_live = trace->mem_live;
332
333     if (!mem_live) {
334       g_print (", no live memory");
335     }
336     else {
337       g_print (", dumping live memory: ");
338
339       while (mem_live) {
340         g_print ("%p ", mem_live->data);
341         mem_live = g_slist_next (mem_live);
342       }
343       g_print ("\ntotal %d", g_slist_length (trace->mem_live));
344     }
345   }
346   g_print ("\n");
347 }
348
349 /**
350  * gst_alloc_trace_set_flags:
351  * @trace: the GstAllocTrace 
352  * @flags: flags to set 
353  *
354  * Enable the given features on the given GstAllocTrace object.
355  */
356 void
357 gst_alloc_trace_set_flags (GstAllocTrace *trace, GstAllocTraceFlags flags)
358 {
359   g_return_if_fail (trace != NULL);
360
361   trace->flags = flags;
362 }