]> git.sesse.net Git - vlc/blob - src/misc/objects.c
Remove dead old object thread code
[vlc] / src / misc / objects.c
1 /*****************************************************************************
2  * objects.c: vlc_object_t handling
3  *****************************************************************************
4  * Copyright (C) 2004-2008 the VideoLAN team
5  * Copyright (C) 2006-2010 RĂ©mi Denis-Courmont
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /**
25  * \file
26  * This file contains the functions to handle the vlc_object_t type
27  *
28  * Unless otherwise stated, functions in this file are not cancellation point.
29  * All functions in this file are safe w.r.t. deferred cancellation.
30  */
31
32
33 /*****************************************************************************
34  * Preamble
35  *****************************************************************************/
36 #ifdef HAVE_CONFIG_H
37 # include "config.h"
38 #endif
39
40 #include <vlc_common.h>
41
42 #include "../libvlc.h"
43 #include <vlc_aout.h>
44 #include "audio_output/aout_internal.h"
45
46 #include "vlc_interface.h"
47 #include "vlc_codec.h"
48
49 #include "variables.h"
50
51 #ifdef HAVE_SEARCH_H
52 # include <search.h>
53 #endif
54
55 #ifndef WIN32
56 # include <vlc_fs.h>
57 # include <unistd.h>
58 #else
59 # include <io.h>
60 # include <winsock2.h>
61 # include <ws2tcpip.h>
62 # undef  read
63 # define read( a, b, c )  recv (a, b, c, 0)
64 # undef  write
65 # define write( a, b, c ) send (a, b, c, 0)
66 # undef  close
67 # define close( a )       closesocket (a)
68 #endif
69
70 #include <limits.h>
71 #include <assert.h>
72
73 #if defined (HAVE_SYS_EVENTFD_H)
74 # include <sys/eventfd.h>
75 # ifndef EFD_CLOEXEC
76 #  define EFD_CLOEXEC 0
77 #  warning EFD_CLOEXEC missing. Consider updating libc.
78 # endif
79 #endif
80
81
82 /*****************************************************************************
83  * Local prototypes
84  *****************************************************************************/
85 static int  DumpCommand( vlc_object_t *, char const *,
86                          vlc_value_t, vlc_value_t, void * );
87
88 static vlc_object_t * FindParentName( vlc_object_t *, const char * );
89 static vlc_object_t * FindChildName ( vlc_object_internals_t *, const char * );
90 static void PrintObject( vlc_object_internals_t *, const char * );
91 static void DumpStructure( vlc_object_internals_t *, unsigned, char * );
92
93 static vlc_list_t   * NewList       ( int );
94
95 static void vlc_object_destroy( vlc_object_t *p_this );
96
97 /*****************************************************************************
98  * Local structure lock
99  *****************************************************************************/
100 static void libvlc_lock (libvlc_int_t *p_libvlc)
101 {
102     vlc_mutex_lock (&(libvlc_priv (p_libvlc)->structure_lock));
103 }
104
105 static void libvlc_unlock (libvlc_int_t *p_libvlc)
106 {
107     vlc_mutex_unlock (&(libvlc_priv (p_libvlc)->structure_lock));
108 }
109
110 #undef vlc_custom_create
111 void *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
112                          int i_type, const char *psz_type )
113 {
114     vlc_object_t *p_new;
115     vlc_object_internals_t *p_priv;
116
117     /* NOTE:
118      * VLC objects are laid out as follow:
119      * - first the LibVLC-private per-object data,
120      * - then VLC_COMMON members from vlc_object_t,
121      * - finally, the type-specific data (if any).
122      *
123      * This function initializes the LibVLC and common data,
124      * and zeroes the rest.
125      */
126     p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
127     if( p_priv == NULL )
128         return NULL;
129
130     assert (i_size >= sizeof (vlc_object_t));
131     p_new = (vlc_object_t *)(p_priv + 1);
132
133     p_priv->i_object_type = i_type;
134     p_new->psz_object_type = psz_type;
135     p_priv->psz_name = NULL;
136
137     p_new->b_die = false;
138     p_new->b_force = false;
139
140     p_new->psz_header = NULL;
141
142     if (p_this)
143         p_new->i_flags = p_this->i_flags
144             & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
145
146     p_priv->var_root = NULL;
147
148     if( p_this == NULL )
149     {
150         libvlc_int_t *self = (libvlc_int_t*)p_new;
151         p_new->p_libvlc = self;
152         vlc_mutex_init (&(libvlc_priv (self)->structure_lock));
153         p_this = p_new;
154     }
155     else
156         p_new->p_libvlc = p_this->p_libvlc;
157
158     vlc_spin_init( &p_priv->ref_spin );
159     p_priv->i_refcount = 1;
160     p_priv->pf_destructor = NULL;
161     p_new->p_parent = NULL;
162     p_priv->first = NULL;
163
164     /* Initialize mutexes and condvars */
165     vlc_mutex_init( &p_priv->var_lock );
166     vlc_cond_init( &p_priv->var_wait );
167     p_priv->pipes[0] = p_priv->pipes[1] = -1;
168
169     if (p_new == VLC_OBJECT(p_new->p_libvlc))
170     {   /* TODO: should be in src/libvlc.c */
171         int canc = vlc_savecancel ();
172         var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
173         var_AddCallback( p_new, "tree", DumpCommand, NULL );
174         var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
175         var_AddCallback( p_new, "vars", DumpCommand, NULL );
176         vlc_restorecancel (canc);
177     }
178
179     return p_new;
180 }
181
182 #undef vlc_object_create
183 /**
184  * Allocates and initializes a vlc object.
185  *
186  * @param i_size object byte size
187  *
188  * @return the new object, or NULL on error.
189  */
190 void *vlc_object_create( vlc_object_t *p_this, size_t i_size )
191 {
192     return vlc_custom_create( p_this, i_size, VLC_OBJECT_GENERIC, "generic" );
193 }
194
195 #undef vlc_object_set_destructor
196 /**
197  ****************************************************************************
198  * Set the destructor of a vlc object
199  *
200  * This function sets the destructor of the vlc object. It will be called
201  * when the object is destroyed when the its refcount reaches 0.
202  * (It is called by the internal function vlc_object_destroy())
203  *****************************************************************************/
204 void vlc_object_set_destructor( vlc_object_t *p_this,
205                                 vlc_destructor_t pf_destructor )
206 {
207     vlc_object_internals_t *p_priv = vlc_internals(p_this );
208
209     vlc_spin_lock( &p_priv->ref_spin );
210     p_priv->pf_destructor = pf_destructor;
211     vlc_spin_unlock( &p_priv->ref_spin );
212 }
213
214 static vlc_mutex_t name_lock = VLC_STATIC_MUTEX;
215
216 #undef vlc_object_set_name
217 int vlc_object_set_name(vlc_object_t *obj, const char *name)
218 {
219     vlc_object_internals_t *priv = vlc_internals(obj);
220     char *newname = name ? strdup (name) : NULL;
221     char *oldname;
222
223     vlc_mutex_lock (&name_lock);
224     oldname = priv->psz_name;
225     priv->psz_name = newname;
226     vlc_mutex_unlock (&name_lock);
227
228     free (oldname);
229     return (priv->psz_name || !name) ? VLC_SUCCESS : VLC_ENOMEM;
230 }
231
232 #undef vlc_object_get_name
233 char *vlc_object_get_name(const vlc_object_t *obj)
234 {
235     vlc_object_internals_t *priv = vlc_internals(obj);
236     char *name;
237
238     vlc_mutex_lock (&name_lock);
239     name = priv->psz_name ? strdup (priv->psz_name) : NULL;
240     vlc_mutex_unlock (&name_lock);
241
242     return name;
243 }
244
245 /**
246  ****************************************************************************
247  * Destroy a vlc object (Internal)
248  *
249  * This function destroys an object that has been previously allocated with
250  * vlc_object_create. The object's refcount must be zero and it must not be
251  * attached to other objects in any way.
252  *
253  * This function must be called with cancellation disabled (currently).
254  *****************************************************************************/
255 static void vlc_object_destroy( vlc_object_t *p_this )
256 {
257     vlc_object_internals_t *p_priv = vlc_internals( p_this );
258
259     /* Send a kill to the object's thread if applicable */
260     vlc_object_kill( p_this );
261
262     /* Call the custom "subclass" destructor */
263     if( p_priv->pf_destructor )
264         p_priv->pf_destructor( p_this );
265
266     /* Destroy the associated variables. */
267     var_DestroyAll( p_this );
268
269     vlc_cond_destroy( &p_priv->var_wait );
270     vlc_mutex_destroy( &p_priv->var_lock );
271
272     free( p_this->psz_header );
273
274     free( p_priv->psz_name );
275
276     vlc_spin_destroy( &p_priv->ref_spin );
277     if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
278         close( p_priv->pipes[1] );
279     if( p_priv->pipes[0] != -1 )
280         close( p_priv->pipes[0] );
281     if( VLC_OBJECT(p_this->p_libvlc) == p_this )
282         vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
283
284     free( p_priv );
285 }
286
287
288 #ifdef WIN32
289 /**
290  * select()-able pipes emulated using Winsock
291  */
292 # define vlc_pipe selectable_pipe
293 static int selectable_pipe (int fd[2])
294 {
295     SOCKADDR_IN addr;
296     int addrlen = sizeof (addr);
297
298     SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
299            c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
300     if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
301         goto error;
302
303     memset (&addr, 0, sizeof (addr));
304     addr.sin_family = AF_INET;
305     addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
306     if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
307      || getsockname (l, (PSOCKADDR)&addr, &addrlen)
308      || listen (l, 1)
309      || connect (c, (PSOCKADDR)&addr, addrlen))
310         goto error;
311
312     a = accept (l, NULL, NULL);
313     if (a == INVALID_SOCKET)
314         goto error;
315
316     closesocket (l);
317     //shutdown (a, 0);
318     //shutdown (c, 1);
319     fd[0] = c;
320     fd[1] = a;
321     return 0;
322
323 error:
324     if (l != INVALID_SOCKET)
325         closesocket (l);
326     if (c != INVALID_SOCKET)
327         closesocket (c);
328     return -1;
329 }
330 #endif /* WIN32 */
331
332 static vlc_mutex_t pipe_lock = VLC_STATIC_MUTEX;
333
334 /**
335  * Returns the readable end of a pipe that becomes readable once termination
336  * of the object is requested (vlc_object_kill()).
337  * This can be used to wake-up out of a select() or poll() event loop, such
338  * typically when doing network I/O.
339  *
340  * Note that the pipe will remain the same for the lifetime of the object.
341  * DO NOT read the pipe nor close it yourself. Ever.
342  *
343  * @param obj object that would be "killed"
344  * @return a readable pipe descriptor, or -1 on error.
345  */
346 int vlc_object_waitpipe( vlc_object_t *obj )
347 {
348     vlc_object_internals_t *internals = vlc_internals( obj );
349
350     vlc_mutex_lock (&pipe_lock);
351     if (internals->pipes[0] == -1)
352     {
353         /* This can only ever happen if someone killed us without locking: */
354         assert (internals->pipes[1] == -1);
355
356         /* pipe() is not a cancellation point, but write() is and eventfd() is
357          * unspecified (not in POSIX). */
358         int canc = vlc_savecancel ();
359 #if defined (HAVE_SYS_EVENTFD_H)
360         internals->pipes[0] = internals->pipes[1] = eventfd (0, EFD_CLOEXEC);
361         if (internals->pipes[0] == -1)
362 #endif
363         {
364             if (vlc_pipe (internals->pipes))
365                 internals->pipes[0] = internals->pipes[1] = -1;
366         }
367
368         if (internals->pipes[0] != -1 && obj->b_die)
369         {   /* Race condition: vlc_object_kill() already invoked! */
370             msg_Dbg (obj, "waitpipe: object already dying");
371             write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
372         }
373         vlc_restorecancel (canc);
374     }
375     vlc_mutex_unlock (&pipe_lock);
376     return internals->pipes[0];
377 }
378
379 #undef vlc_object_kill
380 /**
381  * Requests termination of an object, cancels the object thread, and make the
382  * object wait pipe (if it exists) readable. Not a cancellation point.
383  */
384 void vlc_object_kill( vlc_object_t *p_this )
385 {
386     vlc_object_internals_t *priv = vlc_internals( p_this );
387     int fd = -1;
388
389     vlc_mutex_lock( &pipe_lock );
390     if( !p_this->b_die )
391     {
392         fd = priv->pipes[1];
393         p_this->b_die = true;
394     }
395
396     /* This also serves as a memory barrier toward vlc_object_alive(): */
397     vlc_mutex_unlock( &pipe_lock );
398
399     if (fd != -1)
400     {
401         int canc = vlc_savecancel ();
402
403         /* write _after_ setting b_die, so vlc_object_alive() returns false */
404         write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
405         msg_Dbg (p_this, "waitpipe: object killed");
406         vlc_restorecancel (canc);
407     }
408 }
409
410 static int objnamecmp(const vlc_object_t *obj, const char *name)
411 {
412     char *objname = vlc_object_get_name(obj);
413     if (objname == NULL)
414         return INT_MIN;
415
416     int ret = strcmp (objname, name);
417     free (objname);
418     return ret;
419 }
420
421 #undef vlc_object_find_name
422 /**
423  * Finds a named object and increment its reference count.
424  * Beware that objects found in this manner can be "owned" by another thread,
425  * be of _any_ type, and be attached to any module (if any). With such an
426  * object reference, you can set or get object variables, emit log messages,
427  * and read write-once object parameters (psz_object_type, etc).
428  * You CANNOT cast the object to a more specific object type, and you
429  * definitely cannot invoke object type-specific callbacks with this.
430  *
431  * @param p_this object to search from
432  * @param psz_name name of the object to search for
433  * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
434  *
435  * @return a matching object (must be released by the caller),
436  * or NULL on error.
437  */
438 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
439                                     const char *psz_name, int i_mode )
440 {
441     vlc_object_t *p_found;
442
443     /* Reading psz_object_name from a separate inhibits thread-safety.
444      * Use a libvlc address variable instead for that sort of things! */
445     msg_Warn( p_this, "%s(%s) is not safe!", __func__, psz_name );
446     /* If have the requested name ourselves, don't look further */
447     if( !objnamecmp(p_this, psz_name) )
448     {
449         vlc_object_hold( p_this );
450         return p_this;
451     }
452
453     /* Otherwise, recursively look for the object */
454     if (i_mode == FIND_ANYWHERE)
455         return vlc_object_find_name (VLC_OBJECT(p_this->p_libvlc), psz_name,
456                                      FIND_CHILD);
457
458     libvlc_lock (p_this->p_libvlc);
459     vlc_mutex_lock (&name_lock);
460     switch (i_mode)
461     {
462         case FIND_PARENT:
463             p_found = FindParentName (p_this, psz_name);
464             break;
465         case FIND_CHILD:
466             p_found = FindChildName (vlc_internals (p_this), psz_name);
467             break;
468         default:
469             assert (0);
470     }
471     vlc_mutex_unlock (&name_lock);
472     libvlc_unlock (p_this->p_libvlc);
473     return p_found;
474 }
475
476 #undef vlc_object_hold
477 /**
478  * Increment an object reference counter.
479  */
480 void * vlc_object_hold( vlc_object_t *p_this )
481 {
482     vlc_object_internals_t *internals = vlc_internals( p_this );
483
484     vlc_spin_lock( &internals->ref_spin );
485     /* Avoid obvious freed object uses */
486     assert( internals->i_refcount > 0 );
487     /* Increment the counter */
488     internals->i_refcount++;
489     vlc_spin_unlock( &internals->ref_spin );
490     return p_this;
491 }
492
493 #undef vlc_object_release
494 /*****************************************************************************
495  * Decrement an object refcount
496  * And destroy the object if its refcount reach zero.
497  *****************************************************************************/
498 void vlc_object_release( vlc_object_t *p_this )
499 {
500     vlc_object_internals_t *internals = vlc_internals( p_this );
501     vlc_object_t *parent = NULL;
502     bool b_should_destroy;
503
504     vlc_spin_lock( &internals->ref_spin );
505     assert( internals->i_refcount > 0 );
506
507     if( internals->i_refcount > 1 )
508     {
509         /* Fast path */
510         /* There are still other references to the object */
511         internals->i_refcount--;
512         vlc_spin_unlock( &internals->ref_spin );
513         return;
514     }
515     vlc_spin_unlock( &internals->ref_spin );
516
517     /* Slow path */
518     /* Remember that we cannot hold the spin while waiting on the mutex */
519     libvlc_lock (p_this->p_libvlc);
520     /* Take the spin again. Note that another thread may have held the
521      * object in the (very short) mean time. */
522     vlc_spin_lock( &internals->ref_spin );
523     b_should_destroy = --internals->i_refcount == 0;
524     vlc_spin_unlock( &internals->ref_spin );
525
526     if( b_should_destroy )
527     {
528         /* Detach from parent to protect against FIND_CHILDREN */
529         parent = p_this->p_parent;
530         if (likely(parent))
531         {
532            /* Unlink */
533            if (internals->prev != NULL)
534                internals->prev->next = internals->next;
535            else
536                vlc_internals(parent)->first = internals->next;
537            if (internals->next != NULL)
538                internals->next->prev = internals->prev;
539         }
540
541         /* We have no children */
542         assert (internals->first == NULL);
543     }
544     libvlc_unlock (p_this->p_libvlc);
545
546     if( b_should_destroy )
547     {
548         int canc;
549
550         canc = vlc_savecancel ();
551         vlc_object_destroy( p_this );
552         vlc_restorecancel (canc);
553         if (parent)
554             vlc_object_release (parent);
555     }
556 }
557
558 #undef vlc_object_attach
559 /**
560  ****************************************************************************
561  * attach object to a parent object
562  *****************************************************************************
563  * This function sets p_this as a child of p_parent, and p_parent as a parent
564  * of p_this.
565  *****************************************************************************/
566 void vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
567 {
568     if( !p_this ) return;
569
570     vlc_object_internals_t *pap = vlc_internals (p_parent);
571     vlc_object_internals_t *priv = vlc_internals (p_this);
572
573     priv->prev = NULL;
574     vlc_object_hold (p_parent);
575     libvlc_lock (p_this->p_libvlc);
576
577     /* Attach the parent to its child */
578     assert (p_this->p_parent == NULL);
579     p_this->p_parent = p_parent;
580
581     /* Attach the child to its parent */
582     priv->next = pap->first;
583     if (priv->next != NULL)
584         priv->next->prev = priv;
585     pap->first = priv;
586     libvlc_unlock (p_this->p_libvlc);
587 }
588
589
590 #undef vlc_list_children
591 /**
592  * Gets the list of children of an objects, and increment their reference
593  * count.
594  * @return a list (possibly empty) or NULL in case of error.
595  */
596 vlc_list_t *vlc_list_children( vlc_object_t *obj )
597 {
598     vlc_list_t *l;
599     vlc_object_internals_t *priv;
600     unsigned count = 0;
601
602     libvlc_lock (obj->p_libvlc);
603     for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
604          count++;
605     l = NewList (count);
606     if (likely(l != NULL))
607     {
608         unsigned i = 0;
609
610         for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
611             l->p_values[i++].p_object = vlc_object_hold (vlc_externals (priv));
612     }
613     libvlc_unlock (obj->p_libvlc);
614     return l;
615 }
616
617 static void DumpVariable (const void *data, const VISIT which, const int depth)
618 {
619     if (which != postorder && which != leaf)
620         return;
621     (void) depth;
622
623     const variable_t *p_var = *(const variable_t **)data;
624     const char *psz_type = "unknown";
625
626     switch( p_var->i_type & VLC_VAR_TYPE )
627     {
628 #define MYCASE( type, nice )    \
629         case VLC_VAR_ ## type:  \
630             psz_type = nice;    \
631             break;
632         MYCASE( VOID, "void" );
633         MYCASE( BOOL, "bool" );
634         MYCASE( INTEGER, "integer" );
635         MYCASE( HOTKEY, "hotkey" );
636         MYCASE( STRING, "string" );
637         MYCASE( VARIABLE, "variable" );
638         MYCASE( FLOAT, "float" );
639         MYCASE( TIME, "time" );
640         MYCASE( COORDS, "coords" );
641         MYCASE( ADDRESS, "address" );
642         MYCASE( MUTEX, "mutex" );
643 #undef MYCASE
644     }
645     printf( " *-o \"%s\" (%s", p_var->psz_name, psz_type );
646     if( p_var->psz_text )
647         printf( ", %s", p_var->psz_text );
648     fputc( ')', stdout );
649     if( p_var->i_type & VLC_VAR_HASCHOICE )
650         fputs( ", has choices", stdout );
651     if( p_var->i_type & VLC_VAR_ISCOMMAND )
652         fputs( ", command", stdout );
653     if( p_var->i_entries )
654         printf( ", %d callbacks", p_var->i_entries );
655     switch( p_var->i_type & VLC_VAR_CLASS )
656     {
657         case VLC_VAR_VOID:
658         case VLC_VAR_MUTEX:
659             break;
660         case VLC_VAR_BOOL:
661             printf( ": %s", p_var->val.b_bool ? "true" : "false" );
662             break;
663         case VLC_VAR_INTEGER:
664             printf( ": %"PRId64, p_var->val.i_int );
665             break;
666         case VLC_VAR_STRING:
667             printf( ": \"%s\"", p_var->val.psz_string );
668             break;
669         case VLC_VAR_FLOAT:
670             printf( ": %f", p_var->val.f_float );
671             break;
672         case VLC_VAR_TIME:
673             printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
674             break;
675         case VLC_VAR_COORDS:
676             printf( ": %"PRId32"x%"PRId32,
677                     p_var->val.coords.x, p_var->val.coords.y );
678             break;
679         case VLC_VAR_ADDRESS:
680             printf( ": %p", p_var->val.p_address );
681             break;
682     }
683     fputc( '\n', stdout );
684 }
685
686 /*****************************************************************************
687  * DumpCommand: print the current vlc structure
688  *****************************************************************************
689  * This function prints either an ASCII tree showing the connections between
690  * vlc objects, and additional information such as their refcount, thread ID,
691  * etc. (command "tree"), or the same data as a simple list (command "list").
692  *****************************************************************************/
693 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
694                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
695 {
696     (void)oldval; (void)p_data;
697     vlc_object_t *p_object = NULL;
698
699     if( *newval.psz_string )
700     {
701         /* try using the object's name to find it */
702         p_object = vlc_object_find_name( p_this, newval.psz_string,
703                                          FIND_ANYWHERE );
704         if( !p_object )
705         {
706             return VLC_ENOOBJ;
707         }
708     }
709
710     libvlc_lock (p_this->p_libvlc);
711     if( *psz_cmd == 't' )
712     {
713         char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
714
715         if( !p_object )
716             p_object = VLC_OBJECT(p_this->p_libvlc);
717
718         psz_foo[0] = '|';
719         DumpStructure( vlc_internals(p_object), 0, psz_foo );
720     }
721     else if( *psz_cmd == 'v' )
722     {
723         if( !p_object )
724             p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
725
726         PrintObject( vlc_internals(p_object), "" );
727         vlc_mutex_lock( &vlc_internals( p_object )->var_lock );
728         if( vlc_internals( p_object )->var_root == NULL )
729             puts( " `-o No variables" );
730         else
731             twalk( vlc_internals( p_object )->var_root, DumpVariable );
732         vlc_mutex_unlock( &vlc_internals( p_object )->var_lock );
733     }
734     libvlc_unlock (p_this->p_libvlc);
735
736     if( *newval.psz_string )
737     {
738         vlc_object_release( p_object );
739     }
740     return VLC_SUCCESS;
741 }
742
743 /*****************************************************************************
744  * vlc_list_release: free a list previously allocated by vlc_list_find
745  *****************************************************************************
746  * This function decreases the refcount of all objects in the list and
747  * frees the list.
748  *****************************************************************************/
749 void vlc_list_release( vlc_list_t *p_list )
750 {
751     int i_index;
752
753     for( i_index = 0; i_index < p_list->i_count; i_index++ )
754     {
755         vlc_object_release( p_list->p_values[i_index].p_object );
756     }
757
758     free( p_list->p_values );
759     free( p_list );
760 }
761
762 /* Following functions are local */
763
764 static vlc_object_t *FindParentName (vlc_object_t *p_this, const char *name)
765 {
766     for (vlc_object_t *parent = p_this->p_parent;
767          parent != NULL;
768          parent = parent->p_parent)
769     {
770         const char *objname = vlc_internals (parent)->psz_name;
771         if (objname && !strcmp (objname, name))
772             return vlc_object_hold (parent);
773     }
774     return NULL;
775 }
776
777 static vlc_object_t *FindChildName (vlc_object_internals_t *priv,
778                                     const char *name)
779 {
780     for (priv = priv->first; priv != NULL; priv = priv->next)
781     {
782         if (priv->psz_name && !strcmp (priv->psz_name, name))
783             return vlc_object_hold (vlc_externals (priv));
784
785         vlc_object_t *found = FindChildName (priv, name);
786         if (found != NULL)
787             return found;
788     }
789     return NULL;
790 }
791
792 static void PrintObject( vlc_object_internals_t *priv,
793                          const char *psz_prefix )
794 {
795     char psz_refcount[20], psz_name[50], psz_parent[20];
796
797     int canc = vlc_savecancel ();
798     memset( &psz_name, 0, sizeof(psz_name) );
799
800     vlc_mutex_lock (&name_lock);
801     if (priv->psz_name != NULL)
802     {
803         snprintf( psz_name, 49, " \"%s\"", priv->psz_name );
804         if( psz_name[48] )
805             psz_name[48] = '\"';
806     }
807     vlc_mutex_unlock (&name_lock);
808
809     psz_refcount[0] = '\0';
810     if( priv->i_refcount > 0 )
811         snprintf( psz_refcount, 19, ", %u refs", priv->i_refcount );
812
813     psz_parent[0] = '\0';
814     /* FIXME: need structure lock!!! */
815     if( vlc_externals(priv)->p_parent )
816         snprintf( psz_parent, 19, ", parent %p",
817                   vlc_externals(priv)->p_parent );
818
819     printf( " %so %p %s%s%s%s\n", psz_prefix,
820             vlc_externals(priv), vlc_externals(priv)->psz_object_type,
821             psz_name, psz_refcount, psz_parent );
822     vlc_restorecancel (canc);
823 }
824
825 static void DumpStructure (vlc_object_internals_t *priv, unsigned i_level,
826                            char *psz_foo)
827 {
828     char i_back = psz_foo[i_level];
829     psz_foo[i_level] = '\0';
830
831     PrintObject (priv, psz_foo);
832
833     psz_foo[i_level] = i_back;
834
835     if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
836     {
837         msg_Warn( vlc_externals(priv), "structure tree is too deep" );
838         return;
839     }
840
841     for (priv = priv->first; priv != NULL; priv = priv->next)
842     {
843         if( i_level )
844         {
845             psz_foo[i_level-1] = ' ';
846
847             if( psz_foo[i_level-2] == '`' )
848             {
849                 psz_foo[i_level-2] = ' ';
850             }
851         }
852
853         psz_foo[i_level] = priv->next ? '|' : '`';
854         psz_foo[i_level+1] = '-';
855         psz_foo[i_level+2] = '\0';
856
857         DumpStructure (priv, i_level + 2, psz_foo);
858     }
859 }
860
861 static vlc_list_t * NewList( int i_count )
862 {
863     vlc_list_t * p_list = malloc( sizeof( vlc_list_t ) );
864     if( p_list == NULL )
865         return NULL;
866
867     p_list->i_count = i_count;
868
869     if( i_count == 0 )
870     {
871         p_list->p_values = NULL;
872         return p_list;
873     }
874
875     p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
876     if( p_list->p_values == NULL )
877     {
878         p_list->i_count = 0;
879         return p_list;
880     }
881
882     return p_list;
883 }