gstinfo.c 31.1 KB
Newer Older
1
2
3
/* GStreamer
 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
 *                    2000 Wim Taymans <wtay@chello.be>
4
 *                    2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
5
 *
6
 * gstinfo.c: debugging functions
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

24
25
26
27
28
29
#include "gst_private.h"

#include "gstinfo.h"

#ifndef GST_DISABLE_GST_DEBUG

30
#ifdef HAVE_DLFCN_H
31
#include <dlfcn.h>
32
#endif
33
34
35
#ifdef HAVE_PRINTF_EXTENSION
#include <printf.h>
#endif
36
#include <unistd.h>
37
#include <string.h> /* G_VA_COPY */
38
39
#include "gstinfo.h"
#include "gstlog.h"
40
#include "gst_private.h"
41
42
#include "gstelement.h"
#include "gstpad.h"
43
#include "gstscheduler.h"
44
45
46
#include "gst_private.h"

GST_DEBUG_CATEGORY_STATIC(GST_CAT_DEBUG);
47

48
#if 0
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#if defined __sgi__
#include <rld_interface.h>
typedef struct DL_INFO {
  const char * dli_fname;
  void       * dli_fbase;
  const char * dli_sname;
  void       * dli_saddr;
  int          dli_version;
  int          dli_reserved1;
  long         dli_reserved[4];
} Dl_info;
#define _RLD_DLADDR             14
int dladdr(void *address, Dl_info *dl);

int dladdr(void *address, Dl_info *dl)
{
  void *v;
  v = _rld_new_interface(_RLD_DLADDR,address,dl);
  return (int)v;
}
69
#endif /* __sgi__ */
70
#endif
71

72
73
extern gchar *_gst_progname;

74
75
76
77
static void	gst_debug_reset_threshold	(gpointer category,
						 gpointer unused);
static void	gst_debug_reset_all_thresholds	(void);

78
79
80
81
82
83
84
#ifdef HAVE_PRINTF_EXTENSION
static int _gst_info_printf_extension (FILE *stream, const struct printf_info *info,
    const void *const *args);
static int _gst_info_printf_extension_arginfo (const struct printf_info *info, size_t n,
    int *argtypes);
#endif

85
86
87
88
89
90
struct _GstDebugMessage {
  gchar *		message;
  const gchar *		format;
  va_list		arguments;
};

91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* list of all name/level pairs from --gst-debug and GST_DEBUG */
static GStaticMutex __level_name_mutex = G_STATIC_MUTEX_INIT;
static GSList *__level_name = NULL;
typedef struct {
  GPatternSpec *	pat;
  GstDebugLevel		level;
} LevelNameEntry;

/* list of all categories */
static GStaticMutex __cat_mutex = G_STATIC_MUTEX_INIT;
static GSList *__categories = NULL;

/* all registered debug handlers */
typedef struct {
  GstLogFunction	func;
  gpointer		user_data;
} LogFuncEntry;
static GStaticMutex __log_func_mutex = G_STATIC_MUTEX_INIT;
static GSList *__log_functions = NULL;

static GstAtomicInt __default_level;
static GstAtomicInt __use_color;
Benjamin Otte's avatar
Benjamin Otte committed
113
gboolean __gst_debug_enabled = TRUE;
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141


GstDebugCategory *GST_CAT_DEFAULT = NULL;

GstDebugCategory *GST_CAT_GST_INIT = NULL;
GstDebugCategory *GST_CAT_COTHREADS = NULL;
GstDebugCategory *GST_CAT_COTHREAD_SWITCH = NULL;
GstDebugCategory *GST_CAT_AUTOPLUG = NULL;
GstDebugCategory *GST_CAT_AUTOPLUG_ATTEMPT = NULL;
GstDebugCategory *GST_CAT_PARENTAGE = NULL;
GstDebugCategory *GST_CAT_STATES = NULL;
GstDebugCategory *GST_CAT_PLANNING = NULL;
GstDebugCategory *GST_CAT_SCHEDULING = NULL;
GstDebugCategory *GST_CAT_DATAFLOW = NULL;
GstDebugCategory *GST_CAT_BUFFER = NULL;
GstDebugCategory *GST_CAT_CAPS = NULL;
GstDebugCategory *GST_CAT_CLOCK = NULL;
GstDebugCategory *GST_CAT_ELEMENT_PADS = NULL;
GstDebugCategory *GST_CAT_PADS = NULL;
GstDebugCategory *GST_CAT_PIPELINE = NULL;
GstDebugCategory *GST_CAT_PLUGIN_LOADING = NULL;
GstDebugCategory *GST_CAT_PLUGIN_INFO = NULL;
GstDebugCategory *GST_CAT_PROPERTIES = NULL;
GstDebugCategory *GST_CAT_THREAD = NULL;
GstDebugCategory *GST_CAT_TYPES = NULL;
GstDebugCategory *GST_CAT_XML = NULL;
GstDebugCategory *GST_CAT_NEGOTIATION = NULL;
GstDebugCategory *GST_CAT_REFCOUNTING = NULL;
142
GstDebugCategory *GST_CAT_ERROR_SYSTEM = NULL;
143
144
145
GstDebugCategory *GST_CAT_EVENT = NULL;
GstDebugCategory *GST_CAT_PARAMS = NULL;
GstDebugCategory *GST_CAT_CALL_TRACE = NULL;
146
GstDebugCategory *GST_CAT_SEEK = NULL;
147

148
/**
149
150
151
152
 * _gst_debug_init:
 * 
 * Initializes the debugging system.
 * Normally you don't want to call this, because gst_init does it for you.
153
 */
154
155
156
157
158
void _gst_debug_init (void)
{
  gst_atomic_int_init (&__default_level, GST_LEVEL_DEFAULT);
  gst_atomic_int_init (&__use_color, 1);

159
160
161
162
163
#ifdef HAVE_PRINTF_EXTENSION
  register_printf_function (GST_PTR_FORMAT[0], _gst_info_printf_extension,
      _gst_info_printf_extension_arginfo);
#endif

164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
  /* do NOT use a single debug function before this line has been run */
  GST_CAT_DEFAULT	= _gst_debug_category_new ("default", 
				GST_DEBUG_UNDERLINE,
				NULL);
  GST_CAT_DEBUG		= _gst_debug_category_new ("GST_DEBUG",
				GST_DEBUG_BOLD | GST_DEBUG_FG_YELLOW,
				"debugging subsystem");

  gst_debug_add_log_function (gst_debug_log_default, NULL);

  /* FIXME: add descriptions here */
  GST_CAT_GST_INIT	= _gst_debug_category_new ("GST_INIT",
				GST_DEBUG_BOLD | GST_DEBUG_FG_RED,
				NULL);
  GST_CAT_COTHREADS	= _gst_debug_category_new ("GST_COTHREADS",
				GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN,
				NULL);
  GST_CAT_COTHREAD_SWITCH = _gst_debug_category_new ("GST_COTHREAD_SWITCH",
				GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_GREEN,
				NULL);
  GST_CAT_AUTOPLUG	= _gst_debug_category_new ("GST_AUTOPLUG",
				GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE,
				NULL);
  GST_CAT_AUTOPLUG_ATTEMPT = _gst_debug_category_new ("GST_AUTOPLUG_ATTEMPT",
				GST_DEBUG_BOLD | GST_DEBUG_FG_CYAN | GST_DEBUG_BG_BLUE,
				NULL);
  GST_CAT_PARENTAGE	= _gst_debug_category_new ("GST_PARENTAGE",
				GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED,
				NULL);
  GST_CAT_STATES	= _gst_debug_category_new ("GST_STATES",
				GST_DEBUG_BOLD | GST_DEBUG_FG_RED,
				NULL);
  GST_CAT_PLANNING	= _gst_debug_category_new ("GST_PLANNING",
				GST_DEBUG_BOLD | GST_DEBUG_FG_MAGENTA,
				NULL);
  GST_CAT_SCHEDULING	= _gst_debug_category_new ("GST_SCHEDULING",
				GST_DEBUG_BOLD | GST_DEBUG_FG_MAGENTA,
				NULL);
  GST_CAT_DATAFLOW	= _gst_debug_category_new ("GST_DATAFLOW",
				GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN,
				NULL);
  GST_CAT_BUFFER	= _gst_debug_category_new ("GST_BUFFER",
				GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN,
				NULL);
  GST_CAT_CAPS		= _gst_debug_category_new ("GST_CAPS",
				GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE,
				NULL);
  GST_CAT_CLOCK		= _gst_debug_category_new ("GST_CLOCK",
				GST_DEBUG_BOLD | GST_DEBUG_FG_YELLOW,
				NULL);
  GST_CAT_ELEMENT_PADS	= _gst_debug_category_new ("GST_ELEMENT_PADS",
				GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED,
				NULL);
  GST_CAT_PADS		= _gst_debug_category_new ("GST_PADS",
				GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED,
				NULL);
  GST_CAT_PIPELINE	= _gst_debug_category_new ("GST_PIPELINE",
				GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED,
				NULL);
  GST_CAT_PLUGIN_LOADING = _gst_debug_category_new ("GST_PLUGIN_LOADING",
				GST_DEBUG_BOLD | GST_DEBUG_FG_CYAN,
				NULL);
  GST_CAT_PLUGIN_INFO	= _gst_debug_category_new ("GST_PLUGIN_INFO",
				GST_DEBUG_BOLD | GST_DEBUG_FG_CYAN,
				NULL);
  GST_CAT_PROPERTIES	= _gst_debug_category_new ("GST_PROPERTIES",
				GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_BLUE,
				NULL);
  GST_CAT_THREAD	= _gst_debug_category_new ("GST_THREAD",
				GST_DEBUG_BOLD | GST_DEBUG_FG_RED,
				NULL);
  GST_CAT_TYPES		= _gst_debug_category_new ("GST_TYPES",
				GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED,
				NULL);
  GST_CAT_XML		= _gst_debug_category_new ("GST_XML",
				GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED,
				NULL);
  GST_CAT_NEGOTIATION	= _gst_debug_category_new ("GST_NEGOTIATION",
				GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE,
				NULL);
  GST_CAT_REFCOUNTING	= _gst_debug_category_new ("GST_REFCOUNTING",
				GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE | GST_DEBUG_BG_GREEN,
				NULL);
247
248
249
250
  GST_CAT_ERROR_SYSTEM  = _gst_debug_category_new ("GST_ERROR_SYSTEM",
                                GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_WHITE,
                                NULL);

251
252
253
254
255
256
257
258
259
  GST_CAT_EVENT		= _gst_debug_category_new ("GST_EVENT",
				GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED,
				NULL);
  GST_CAT_PARAMS	= _gst_debug_category_new ("GST_PARAMS",
				GST_DEBUG_BOLD | GST_DEBUG_FG_BLACK | GST_DEBUG_BG_YELLOW,
				NULL);
  GST_CAT_CALL_TRACE	= _gst_debug_category_new ("GST_CALL_TRACE",
				GST_DEBUG_BOLD,
				NULL);
260
261
262
  GST_CAT_SEEK          = _gst_debug_category_new ("GST_SEEK",
				0,
				"plugins reacting to seek events");
263
264
}

265
266
/* we can't do this further above, because we initialize the GST_CAT_DEFAULT struct */
#define GST_CAT_DEFAULT GST_CAT_DEBUG
267

268
269
270
271
272
273
274
275
276
277
278
279
/**
 * gst_debug_log:
 * @category: category to log
 * @level: level of the message is in
 * @file: the file that emitted the message, usually the __FILE__ identifier
 * @function: the function that emitted the message
 * @line: the line from that the message was emitted, usually __LINE__
 * @object: the object this message relates to or NULL if none
 * @format: a printf style format string
 * @...: optional arguments for the format
 * 
 * Logs the given message using the currently registered debugging handlers.
280
 */
281
282
void gst_debug_log (GstDebugCategory *category, GstDebugLevel level,
		    const gchar *file, const gchar *function, gint line,
283
		    GObject *object, const gchar *format, ...)
284
285
286
287
{
  va_list var_args;
  
  va_start (var_args, format);
Benjamin Otte's avatar
Benjamin Otte committed
288
  gst_debug_log_valist (category, level, file, function, line, object, format, var_args);
289
290
  va_end (var_args);
}
291
/**
Benjamin Otte's avatar
Benjamin Otte committed
292
 * gst_debug_log_valist:
293
294
295
296
297
298
299
300
301
302
 * @category: category to log
 * @level: level of the message is in
 * @file: the file that emitted the message, usually the __FILE__ identifier
 * @function: the function that emitted the message
 * @line: the line from that the message was emitted, usually __LINE__
 * @object: the object this message relates to or NULL if none
 * @format: a printf style format string
 * @args: optional arguments for the format
 * 
 * Logs the given message using the currently registered debugging handlers.
303
 */
Benjamin Otte's avatar
Benjamin Otte committed
304
305
void gst_debug_log_valist (GstDebugCategory *category, GstDebugLevel level,
			   const gchar *file, const gchar *function, gint line,
306
			   GObject *object, const gchar *format, va_list args)
307
{
308
  GstDebugMessage message;
309
310
311
312
313
314
315
316
  LogFuncEntry *entry;
  GSList *handler;

  g_return_if_fail (category != NULL);
  g_return_if_fail (file != NULL);
  g_return_if_fail (function != NULL);
  g_return_if_fail (format != NULL);

317
318
  message.message = NULL;
  message.format = format;
319
  G_VA_COPY (message.arguments, args);
320
  
321
322
323
324
  handler = __log_functions;
  while (handler) {
    entry = handler->data;
    handler = g_slist_next (handler);
325
326
327
    entry->func (category, level, file, function, line, object, &message, entry->user_data);
  }
  g_free (message.message);
328
  va_end (message.arguments);
329
330
331
332
333
334
335
336
337
338
339
340
341
}
/**
 * gst_debug_message_get:
 * @message: a debug message
 *
 * Gets the string representation of a GstDebugMessage. This function is used
 * in debug handlers to extract the message.
 */
const gchar *
gst_debug_message_get (GstDebugMessage *message)
{
  if (message->message == NULL) {
    message->message = g_strdup_vprintf (message->format, message->arguments);
342
  }
343
  return message->message;
344
}
345

346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392

static gchar *
gst_debug_print_object (gpointer ptr)
{
  GObject *object = (GObject *)ptr;

#ifdef unused
  /* This is a cute trick to detect unmapped memory, but is unportable,
   * slow, screws around with madvise, and not actually that useful. */
  {
    int ret;

    ret = madvise ((void *)((unsigned long)ptr & (~0xfff)), 4096, 0);
    if (ret == -1 && errno == ENOMEM) {
      buffer = g_strdup_printf ("%p (unmapped memory)", ptr);
    }
  }
#endif

  /* nicely printed object */
  if (object == NULL) {
    return g_strdup ("NULL");
  }
  if (*(GType *)ptr == GST_TYPE_CAPS) {
    return gst_caps_to_string ((GstCaps *)ptr);
  }
  if (*(GType *)ptr == GST_TYPE_STRUCTURE) {
    return gst_structure_to_string ((GstStructure *)ptr);
  }
#ifdef USE_POISONING
  if (*(int *)ptr == 0xffffffff) {
    return g_strdup_printf ("<poisoned@%p>", ptr);
  }
#endif
  if (GST_IS_PAD (object) && GST_OBJECT_NAME (object)) {
    return g_strdup_printf ("<%s:%s>", GST_DEBUG_PAD_NAME (object));
  }
  if (GST_IS_OBJECT (object) && GST_OBJECT_NAME (object)) {
    return g_strdup_printf ("<%s>", GST_OBJECT_NAME (object));
  }
  if (G_IS_OBJECT (object)) {
    return g_strdup_printf ("<%s@%p>", G_OBJECT_TYPE_NAME(object), object);  
  }

  return g_strdup_printf ("%p", ptr);
}

393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/**
 * gst_debug_construct_term_color:
 * @colorinfo: the color info
 * 
 * Constructs a string that can be used for getting the desired color in color
 * terminals.
 * You need to free the string after use.
 * 
 * Returns: a string containing the color definition
 */
gchar *
gst_debug_construct_term_color (guint colorinfo)
{
  GString *color;
  gchar *ret;
408

409
  color = g_string_new ("\033[00");
410

411
412
413
414
415
416
417
418
419
420
421
422
423
  if (colorinfo & GST_DEBUG_BOLD) {
    g_string_append (color, ";01");
  }
  if (colorinfo & GST_DEBUG_UNDERLINE) {
    g_string_append (color, ";04");
  }
  if (colorinfo & GST_DEBUG_FG_MASK) {
    g_string_append_printf (color, ";3%1d", colorinfo & GST_DEBUG_FG_MASK);
  }
  if (colorinfo & GST_DEBUG_BG_MASK) {
    g_string_append_printf (color, ";4%1d", (colorinfo & GST_DEBUG_BG_MASK) >> 4);
  }
  g_string_append (color, "m");
424

425
426
427
  ret = color->str;
  g_string_free (color, FALSE);
  return ret;
428
429
}
/**
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
 * gst_debug_log_default:
 * @category: category to log
 * @level: level of the message
 * @file: the file that emitted the message, usually the __FILE__ identifier
 * @function: the function that emitted the message
 * @line: the line from that the message was emitted, usually __LINE__
 * @message: the actual message
 * @object: the object this message relates to or NULL if none
 * @unused: an unused variable, reserved for some user_data.
 * 
 * The default logging handler used by GStreamer. Logging functions get called
 * whenever a macro like GST_DEBUG or similar is used. This function outputs the
 * message and additional info using the glib error handler.
 * You can add other handlers by using #gst_debug_add_log_function. 
 * And you can remove this handler by calling
 * gst_debug_remove_log_function (gst_debug_log_default);
446
447
 */
void
448
449
gst_debug_log_default (GstDebugCategory *category, GstDebugLevel level,
		       const gchar *file, const gchar *function, gint line,
450
		       GObject *object, GstDebugMessage *message, gpointer unused)
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
{
  gchar *color;
  gchar *clear;
  gchar *obj;
  gchar *pidcolor;
  gint pid;
  
  if (level > gst_debug_category_get_threshold (category))
    return;
  
  pid = getpid();

  /* color info */
  if (gst_debug_is_colored ()) {
    color = gst_debug_construct_term_color (gst_debug_category_get_color (category));
    clear = "\033[00m";
    pidcolor = g_strdup_printf ("\033[3%1dm", pid % 6 + 31);
  } else {
    color = g_strdup ("");
    clear = "";
    pidcolor = g_strdup ("");
  }
473

474
475
476
  obj = gst_debug_print_object (object);

  g_printerr ("%s %s%15s%s(%s%5d%s) %s%s(%d):%s: %s%s %s\n", 
477
478
479
480
  	      gst_debug_level_get_name (level),
              color, gst_debug_category_get_name (category), clear,
              pidcolor, pid, clear,
  	      color, file, line, function, obj, clear,
481
	      gst_debug_message_get (message));
482

483
484
485
486
  g_free (color);
  g_free (pidcolor);
  g_free (obj);
}
487
/**
488
489
490
491
492
493
 * gst_debug_level_get_name:
 * @level: the level to get the name for
 * 
 * Get the string trepresentation of a debugging level
 * 
 * Returns: the name
494
 */
495
496
497
498
499
500
501
502
503
504
505
506
507
508
const gchar *
gst_debug_level_get_name (GstDebugLevel level)
{
  switch (level) {
    case GST_LEVEL_NONE:	return "";
    case GST_LEVEL_ERROR:	return "ERROR";
    case GST_LEVEL_WARNING:	return "WARN ";
    case GST_LEVEL_INFO:	return "INFO ";
    case GST_LEVEL_DEBUG:	return "DEBUG";
    case GST_LEVEL_LOG:		return "LOG  ";
    default:
      g_warning ("invalid level specified for gst_debug_level_get_name");
      return "";
  }
509
510
}
/**
511
512
513
514
515
516
 * gst_debug_add_log_function:
 * @func: the function to use
 * @data: user data
 * 
 * Adds the logging function to the list of logging functions.
 * Be sure to use G_GNUC_NO_INSTRUMENT on that function, it is needed.
517
518
 */
void
519
520
521
gst_debug_add_log_function (GstLogFunction func, gpointer data)
{
  LogFuncEntry *entry;
Benjamin Otte's avatar
Benjamin Otte committed
522
  GSList *list;
523

524
  g_return_if_fail (func != NULL);
525

526
527
528
  entry = g_new (LogFuncEntry, 1);
  entry->func = func;
  entry->user_data = data;
Benjamin Otte's avatar
Benjamin Otte committed
529
530
531
532
533
534
  /* FIXME: we leak the old list here - other threads might access it right now
   * in gst_debug_logv. Another solution is to lock the mutex in gst_debug_logv,
   * but that is waaay costly.
   * It'd probably be clever to use some kind of RCU here, but I don't know 
   * anything about that.
   */
535
  g_static_mutex_lock (&__log_func_mutex);
Benjamin Otte's avatar
Benjamin Otte committed
536
537
  list = g_slist_copy (__log_functions);
  __log_functions = g_slist_prepend (list, entry);
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
  g_static_mutex_unlock (&__log_func_mutex);

  GST_DEBUG ("prepended log function %p (user data %p) to log functions",
             func, data);
}
static gint
gst_debug_compare_log_function_by_func (gconstpointer entry, gconstpointer func)
{
  gpointer entryfunc = ((LogFuncEntry *) entry)->func;
  
  return (entryfunc < func) ? -1 : (entryfunc > func) ? 1 : 0;
}
static gint
gst_debug_compare_log_function_by_data (gconstpointer entry, gconstpointer data)
{
  gpointer entrydata = ((LogFuncEntry *) entry)->user_data;
  
  return (entrydata < data) ? -1 : (entrydata > data) ? 1 : 0;
}
Benjamin Otte's avatar
Benjamin Otte committed
557
558
559
560
561
562
563
564
static guint
gst_debug_remove_with_compare_func (GCompareFunc func, gpointer data)
{
  GSList *found;
  GSList *new;
  guint removals = 0;
  g_static_mutex_lock (&__log_func_mutex);
  new = __log_functions;
565
566
  while ((found = g_slist_find_custom (new, data, func))) {
    if (new == __log_functions) {
Benjamin Otte's avatar
Benjamin Otte committed
567
      new = g_slist_copy (new);
568
      continue;
Benjamin Otte's avatar
Benjamin Otte committed
569
    }
570
    g_free (found->data);
Benjamin Otte's avatar
Benjamin Otte committed
571
    new = g_slist_delete_link (new, found);
Benjamin Otte's avatar
Benjamin Otte committed
572
573
574
575
576
577
578
579
    removals++;
  }
  /* FIXME: We leak the old list here. See _add_log_function for why. */
  __log_functions = new;
  g_static_mutex_unlock (&__log_func_mutex);

  return removals;
}
Erik Walthinsen's avatar
Erik Walthinsen committed
580
/**
581
582
583
584
585
586
 * gst_debug_remove_log_function:
 * @func: the log function to remove
 * 
 * Removes all registrered instances of the given logging functions.
 * 
 * Returns: How many instances of the function were removed
Erik Walthinsen's avatar
Erik Walthinsen committed
587
 */
588
589
590
guint
gst_debug_remove_log_function (GstLogFunction func)
{
Benjamin Otte's avatar
Benjamin Otte committed
591
592
  guint removals;
  
593
  g_return_val_if_fail (func != NULL, 0);
594

Benjamin Otte's avatar
Benjamin Otte committed
595
  removals = gst_debug_remove_with_compare_func (gst_debug_compare_log_function_by_func, func);
596
597
  GST_DEBUG ("removed log function %p %d times from log function list",
             func, removals);
598

599
  return removals;
600
}
601
602
603
604
605
606
607
608
609
610
611
/**
 * gst_debug_remove_log_function_by_data:
 * @data: user data of the log function to remove
 * 
 * Removes all registrered instances of log functions with the given user data.
 * 
 * Returns: How many instances of the function were removed
 */
guint
gst_debug_remove_log_function_by_data (gpointer data)
{
Benjamin Otte's avatar
Benjamin Otte committed
612
  guint removals;
613

Benjamin Otte's avatar
Benjamin Otte committed
614
  removals = gst_debug_remove_with_compare_func (gst_debug_compare_log_function_by_data, data);
615
616
  GST_DEBUG ("removed %d log functions with user data %p from log function list",
             removals, data);
617

618
619
  return removals;
}
Erik Walthinsen's avatar
Erik Walthinsen committed
620
/**
621
622
623
624
 * gst_debug_set_colored:
 * @colored: Whether to use colored output or not
 * 
 * Sets or unsets the use of coloured debugging output.
Erik Walthinsen's avatar
Erik Walthinsen committed
625
 */
626
void
627
628
629
gst_debug_set_colored (gboolean colored)
{
  gst_atomic_int_set (&__use_color, colored ? 1 : 0);
630
}
Erik Walthinsen's avatar
Erik Walthinsen committed
631
/**
632
633
634
635
636
 * gst_debug_is_colored:
 * 
 * Checks if the debugging output should be colored.
 * 
 * Returns: TRUE, if the debug output should be colored.
Erik Walthinsen's avatar
Erik Walthinsen committed
637
 */
638
639
640
641
gboolean
gst_debug_is_colored (void)
{
  return gst_atomic_int_read (&__use_color) == 0 ? FALSE : TRUE;
642
}
Erik Walthinsen's avatar
Erik Walthinsen committed
643
/**
644
645
646
647
648
649
 * gst_debug_set_active:
 * @active: Whether to use debugging output or not
 * 
 * If activated, debugging messages are sent to the debugging
 * handlers.
 * It makes sense to deactivate it for speed issues.
Benjamin Otte's avatar
Benjamin Otte committed
650
651
 * <note><para>This function is not threadsafe. It makes sense to only call it
 * during initialization.</para></note>
Erik Walthinsen's avatar
Erik Walthinsen committed
652
 */
653
void
654
655
gst_debug_set_active (gboolean active)
{
Benjamin Otte's avatar
Benjamin Otte committed
656
  __gst_debug_enabled = active;
657
}
Erik Walthinsen's avatar
Erik Walthinsen committed
658
/**
659
660
661
662
663
664
665
666
667
 * gst_debug_is_active:
 * 
 * Checks if debugging output is activated.
 * 
 * Returns: TRUE, if debugging is activated
 */
gboolean
gst_debug_is_active (void)
{
Benjamin Otte's avatar
Benjamin Otte committed
668
  return __gst_debug_enabled;
669
670
671
672
673
674
675
}
/**
 * gst_debug_set_default_threshold:
 * @level: level to set
 * 
 * Sets the default threshold to the given level and updates all categories to
 * use this threshold.
Erik Walthinsen's avatar
Erik Walthinsen committed
676
 */
677
void
678
679
680
681
gst_debug_set_default_threshold (GstDebugLevel level)
{
  gst_atomic_int_set (&__default_level, level);
  gst_debug_reset_all_thresholds ();
682
}
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
/**
 * gst_debug_get_default_threshold:
 * 
 * Returns the default threshold that is used for new categories.
 * 
 * Returns: the default threshold level
 */
GstDebugLevel
gst_debug_get_default_threshold (void)
{	
  return (GstDebugLevel) gst_atomic_int_read (&__default_level);
}
static void
gst_debug_reset_threshold (gpointer category, gpointer unused)
{
  GstDebugCategory *cat = (GstDebugCategory *) category;
  GSList *walk;
  
  g_static_mutex_lock (&__level_name_mutex);
  walk = __level_name;
  while (walk) {
    LevelNameEntry *entry = walk->data;
    walk = g_slist_next (walk);
    if (g_pattern_match_string (entry->pat, cat->name)) {
      GST_LOG ("category %s matches pattern %p - gets set to level %d",
               cat->name, entry->pat, entry->level);
      gst_debug_category_set_threshold (cat, entry->level);
      goto exit;
    }
  }
  gst_debug_category_set_threshold (cat, gst_debug_get_default_threshold ());
714

715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
exit:
  g_static_mutex_unlock (&__level_name_mutex);
}
static void
gst_debug_reset_all_thresholds (void)
{
  g_static_mutex_lock (&__cat_mutex);
  g_slist_foreach (__categories, gst_debug_reset_threshold, NULL);
  g_static_mutex_unlock (&__cat_mutex);
}
static void
for_each_threshold_by_entry (gpointer data, gpointer user_data)
{
  GstDebugCategory *cat = (GstDebugCategory *) data;
  LevelNameEntry *entry = (LevelNameEntry *) user_data;
730

731
732
733
734
735
736
  if (g_pattern_match_string (entry->pat, cat->name)) {
    GST_LOG ("category %s matches pattern %p - gets set to level %d",
             cat->name, entry->pat, entry->level);
    gst_debug_category_set_threshold (cat, entry->level);
  }
}
Erik Walthinsen's avatar
Erik Walthinsen committed
737
/**
738
739
740
741
742
743
 * gst_debug_set_threshold_for_name:
 * @name: name of the categories to set
 * @level: level to set them to
 * 
 * Sets all categories which match the gven glob style pattern to the given 
 * level.
Erik Walthinsen's avatar
Erik Walthinsen committed
744
 */
745
void
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
gst_debug_set_threshold_for_name (const gchar *name, GstDebugLevel level)
{
  GPatternSpec *pat;
  LevelNameEntry *entry;
	
  g_return_if_fail (name != NULL);

  pat = g_pattern_spec_new (name);
  entry = g_new (LevelNameEntry, 1);
  entry->pat = pat;
  entry->level = level;
  g_static_mutex_lock (&__level_name_mutex);
  __level_name = g_slist_prepend (__level_name, entry);
  g_static_mutex_unlock (&__level_name_mutex);
  g_static_mutex_lock (&__cat_mutex);
  g_slist_foreach (__categories, for_each_threshold_by_entry, entry);
  g_static_mutex_unlock (&__cat_mutex);
}
/**
 * gst_debug_unset_threshold_for_name:
 * @name: name of the categories to set
 * 
 * Resets all categories with the given name back to the default level.
 */
void
gst_debug_unset_threshold_for_name (const gchar *name)
{
  GSList *walk;
  GPatternSpec *pat;
	
  g_return_if_fail (name != NULL);
777

778
779
780
781
782
783
784
785
786
787
788
789
  pat = g_pattern_spec_new (name);
  g_static_mutex_lock (&__level_name_mutex);
  walk = __level_name;
  /* improve this if you want, it's mighty slow */
  while (walk) {
    LevelNameEntry *entry = walk->data;
    if (g_pattern_spec_equal (entry->pat, pat)) {
      __level_name = g_slist_remove_link (__level_name, walk);
      g_pattern_spec_free (entry->pat);
      g_free (entry);
      g_slist_free_1 (walk);
      walk = __level_name;
790
791
    }
  }
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
  g_static_mutex_unlock (&__level_name_mutex);
  g_pattern_spec_free (pat);
  gst_debug_reset_all_thresholds ();
}
GstDebugCategory *
_gst_debug_category_new	(gchar *name, guint color, gchar *description)
{
  GstDebugCategory *cat;
  
  g_return_val_if_fail (name != NULL, NULL);

  cat = g_new (GstDebugCategory, 1);
  cat->name = g_strdup (name);
  cat->color = color;
  if (description != NULL) {
    cat->description = g_strdup (description);
  } else {
    cat->description = g_strdup ("no description");
  }
  cat->threshold = g_new (GstAtomicInt, 1);
  gst_atomic_int_init (cat->threshold, 0);
  gst_debug_reset_threshold (cat, NULL);
814

815
816
817
818
  /* add to category list */
  g_static_mutex_lock (&__cat_mutex);
  __categories = g_slist_prepend (__categories, cat);
  g_static_mutex_unlock (&__cat_mutex);
819

820
821
822
823
  return cat;
}
/**
 * gst_debug_category_free:
824
825
826
 * @category: #GstDebugCategory to free.
 *
 * Removes and frees the category and all associated resources.
827
828
829
830
831
832
833
834
835
836
837
 */
void
gst_debug_category_free (GstDebugCategory *category)
{
  if (category == NULL) return;

  /* remove from category list */
  g_static_mutex_lock (&__cat_mutex);
  __categories = g_slist_remove (__categories, category);
  g_static_mutex_unlock (&__cat_mutex);

838
839
  g_free ((gpointer) category->name);
  g_free ((gpointer) category->description);
840
841
842
843
  gst_atomic_int_destroy (category->threshold);
  g_free (category->threshold);
  g_free (category);
}
844

845
846
/**
 * gst_debug_category_set_threshold:
847
848
849
 * @category: a #GstDebugCategory to set threshold of.
 * @level: the #GstDebugLevel threshold to set.
 *
850
 * Sets the threshold of the category to the given level. Debug information will
851
 * only be output if the threshold is lower or equal to the level of the
852
853
 * debugging message.
 * <note><para>
854
855
 * Do not use this function in production code, because other functions may
 * change the threshold of categories as side effect. It is however a nice
856
857
858
859
860
861
862
 * function to use when debugging (even from gdb).
 * </para></note>
 */
void
gst_debug_category_set_threshold (GstDebugCategory *category, GstDebugLevel level)
{
  g_return_if_fail (category != NULL);
863

864
865
  gst_atomic_int_set (category->threshold, level);
}
866

867
868
/**
 * gst_debug_category_reset_threshold:
869
870
871
872
 * @category: a #GstDebugCategory to reset threshold of.
 *
 * Resets the threshold of the category to the default level. Debug information
 * will only be output if the threshold is lower or equal to the level of the
873
 * debugging message.
874
 * Use this function to set the threshold back to where it was after using
875
876
877
878
879
880
881
 * gst_debug_category_set_threshold().
 */
void
gst_debug_category_reset_threshold (GstDebugCategory *category)
{
  gst_debug_reset_threshold (category, NULL);
}
882

883
884
/**
 * gst_debug_category_get_threshold:
885
886
 * @category: a #GstDebugCategory to get threshold of.
 *
887
 * Returns the threshold of a #GstCategory.
888
889
 *
 * Returns: the #GstDebugLevel that is used as threshold.
890
891
892
893
894
895
 */
GstDebugLevel
gst_debug_category_get_threshold (GstDebugCategory *category)
{
  return gst_atomic_int_read (category->threshold);
}
896

897
898
/**
 * gst_debug_category_get_name:
899
900
901
902
903
 * @category: a #GstDebugCategory to get name of.
 *
 * Returns the name of a debug category.
 *
 * Returns: the name of the category.
904
905
906
907
908
909
 */
const gchar *
gst_debug_category_get_name (GstDebugCategory *category)
{
  return category->name;
}
910

911
912
/**
 * gst_debug_category_get_color:
913
914
915
916
917
918
 * @category: a #GstDebugCategory to get the color of.
 *
 * Returns the color of a debug category used when printing output in this
 * category.
 *
 * Returns: the color of the category.
919
920
921
922
923
924
 */
guint
gst_debug_category_get_color (GstDebugCategory *category)
{
  return category->color;
}
925

926
927
/**
 * gst_debug_category_get_description:
928
929
930
931
932
 * @category: a #GstDebugCategory to get the description of.
 *
 * Returns the description of a debug category.
 *
 * Returns: the description of the category.
933
934
935
936
937
938
939
940
941
942
943
944
 */
const gchar *
gst_debug_category_get_description (GstDebugCategory *category)
{
  return category->description;
}
/**
 * gst_debug_get_all_categories:
 *
 * Returns a snapshot of a all categories that are currently in use . This list
 * may change anytime.
 * The caller has to free the list after use.
945
 * <emphasis>This function is not threadsafe, so only use it while only the
946
 * main thread is running.</emphasis>
947
 *
948
949
950
951
952
953
 * Returns: the list of categories
 */
GSList *
gst_debug_get_all_categories (void)
{
  GSList *ret;
954

955
956
957
  g_static_mutex_lock (&__cat_mutex);
  ret = g_slist_copy (__categories);
  g_static_mutex_unlock (&__cat_mutex);
958

959
  return ret; 
960
}
961

962
/*** FUNCTION POINTERS ********************************************************/
963
964

GHashTable *__gst_function_pointers = NULL;
965
const gchar *_gst_debug_nameof_funcptr (void *ptr) G_GNUC_NO_INSTRUMENT;
966

967
/* This function MUST NOT return NULL */
968
const gchar *
969
970
971
_gst_debug_nameof_funcptr (void *ptr)
{
  gchar *ptrname;
972
#ifdef HAVE_DLADDR
973
  Dl_info dlinfo;
974
975
#endif

976
  if (__gst_function_pointers && (ptrname = g_hash_table_lookup(__gst_function_pointers,ptr))) {
977
978
979
980
    return ptrname;
  }
  /* we need to create an entry in the hash table for this one so we don't leak
   * the name */
981
982
#ifdef HAVE_DLADDR
  if (dladdr(ptr,&dlinfo) && dlinfo.dli_sname) {
983
984
985
    gchar *name = g_strdup (dlinfo.dli_sname);
    _gst_debug_register_funcptr (ptr, name);
    return name;
986
987
988
  } else
#endif
  {
989
990
991
    gchar *name = g_strdup_printf ("%p", ptr);
    _gst_debug_register_funcptr (ptr, name);
    return name;
992
993
  }
}
994

995
996
997
998
999
1000
1001
1002
1003
1004
void *
_gst_debug_register_funcptr (void *ptr, gchar *ptrname)
{
  if (!__gst_function_pointers)
    __gst_function_pointers = g_hash_table_new (g_direct_hash, g_direct_equal);
  if (!g_hash_table_lookup (__gst_function_pointers, ptr))
    g_hash_table_insert (__gst_function_pointers, ptr, ptrname);

  return ptr;
}
1005

1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
#ifdef HAVE_PRINTF_EXTENSION
static int
_gst_info_printf_extension (FILE *stream, const struct printf_info *info,
    const void *const *args)
{
  char *buffer;
  int len;
  void *ptr;

  buffer = NULL;
  ptr = *(void **)args[0];

  buffer = gst_debug_print_object (ptr);
  len = fprintf (stream, "%*s", (info->left ? -info->width : info->width),
      buffer);

  free (buffer);
  return len;
}

static int
_gst_info_printf_extension_arginfo (const struct printf_info *info, size_t n,
    int *argtypes)
{
  if (n > 0)
    argtypes[0] = PA_POINTER;
  return 1;
}
#endif /* HAVE_PRINTF_EXTENSION */

1036
#endif /* GST_DISABLE_GST_DEBUG */
1037
1038

#ifdef GST_ENABLE_FUNC_INSTRUMENTATION
1039
1040
1041
/* FIXME make this thread specific */
static GSList *stack_trace = NULL;

1042
void __cyg_profile_func_enter(void *this_fn,void *call_site) G_GNUC_NO_INSTRUMENT;
1043
1044
1045
1046
1047
void __cyg_profile_func_enter(void *this_fn,void *call_site) 
{
  gchar *name = _gst_debug_nameof_funcptr (this_fn);
  gchar *site = _gst_debug_nameof_funcptr (call_site);
	
1048
  GST_CAT_DEBUG(GST_CAT_CALL_TRACE, "entering function %s from %s", name, site);
1049
1050
1051
1052
  stack_trace = g_slist_prepend (stack_trace, g_strdup_printf ("%8p in %s from %p (%s)", this_fn, name, call_site, site));

  g_free (name);
  g_free (site);
1053
1054
}

1055
void __cyg_profile_func_exit(void *this_fn,void *call_site) G_GNUC_NO_INSTRUMENT;
1056
1057
1058
1059
void __cyg_profile_func_exit(void *this_fn,void *call_site) 
{
  gchar *name = _gst_debug_nameof_funcptr (this_fn);

1060
  GST_CAT_DEBUG(GST_CAT_CALL_TRACE, "leaving function %s", name);
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
  g_free (stack_trace->data);
  stack_trace = g_slist_delete_link (stack_trace, stack_trace);

  g_free (name);
}

void 
gst_debug_print_stack_trace (void)
{
  GSList *walk = stack_trace;
  gint count = 0;

  if (walk)
    walk = g_slist_next (walk);

  while (walk) {
    gchar *name = (gchar *) walk->data;

    g_print ("#%-2d %s\n", count++, name);

    walk = g_slist_next (walk);
  }
}
#else
void 
gst_debug_print_stack_trace (void)
{
1088
  /* nothing because it's compiled out */
1089
1090
1091
}

#endif /* GST_ENABLE_FUNC_INTSTRUMENTATION */