]> git.sesse.net Git - vlc/blob - bin/winvlc.c
Fixed major crash on Snow Leopard pointed by Pierre
[vlc] / bin / winvlc.c
1 /*****************************************************************************
2  * winvlc.c: the Windows VLC player
3  *****************************************************************************
4  * Copyright (C) 1998-2008 the VideoLAN team
5  *
6  * Authors: Vincent Seguin <seguin@via.ecp.fr>
7  *          Samuel Hocevar <sam@zoy.org>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *          Derk-Jan Hartman <hartman at videolan dot org>
10  *          Lots of other people, see the libvlc AUTHORS file
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #define UNICODE
32 #include <vlc/vlc.h>
33 #include <string.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <windows.h>
37
38 #if !defined(UNDER_CE)
39 # ifndef _WIN32_IE
40 #   define  _WIN32_IE 0x501
41 # endif
42 #   include <shlobj.h>
43 #   include <tlhelp32.h>
44 #   include <wininet.h>
45 # ifndef _WIN64
46 static void check_crashdump(void);
47 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo);
48 # endif
49 #endif
50
51 #ifndef UNDER_CE
52 static char *FromWide (const wchar_t *wide)
53 {
54     size_t len;
55     len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
56
57     char *out = (char *)malloc (len);
58     if (out)
59         WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
60     return out;
61 }
62 #else
63 static int parse_cmdline (char *line, char ***argvp)
64 {
65     char **argv = malloc (sizeof (char *));
66     int argc = 0;
67
68     while (*line != '\0')
69     {
70         char quote = 0;
71
72         /* Skips white spaces */
73         while (strchr ("\t ", *line))
74             line++;
75         if (!*line)
76             break;
77
78         /* Starts a new parameter */
79         argv = realloc (argv, (argc + 2) * sizeof (char *));
80         if (*line == '"')
81         {
82             quote = '"';
83             line++;
84         }
85         argv[argc++] = line;
86
87     more:
88             while (*line && !strchr ("\t ", *line))
89             line++;
90
91     if (line > argv[argc - 1] && line[-1] == quote)
92         /* End of quoted parameter */
93         line[-1] = 0;
94     else
95         if (*line && quote)
96     {
97         /* Space within a quote */
98         line++;
99         goto more;
100     }
101     else
102         /* End of unquoted parameter */
103         if (*line)
104             *line++ = 0;
105     }
106     argv[argc] = NULL;
107     *argvp = argv;
108     return argc;
109 }
110 #endif
111
112 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
113 #ifndef UNDER_CE
114                     LPSTR lpCmdLine,
115 #else
116                     LPWSTR lpCmdLine,
117 #endif
118                     int nCmdShow )
119 {
120     int argc, ret;
121 #ifndef UNDER_CE
122     wchar_t **wargv = CommandLineToArgvW (GetCommandLine (), &argc);
123     if (wargv == NULL)
124         return 1;
125
126     char *argv[argc + 1];
127     BOOL crash_handling = TRUE;
128     int j = 0;
129     for (int i = 0; i < argc; i++)
130     {
131         if(!wcscmp(wargv[i], L"--no-crashdump"))
132         {
133             crash_handling = FALSE;
134         }
135         else
136         {
137             argv[j] = FromWide (wargv[i]);
138             j++;
139         }
140     }
141
142     argc = j;
143     argv[argc] = NULL;
144     LocalFree (wargv);
145
146 # ifndef _WIN64
147     if(crash_handling)
148     {
149         check_crashdump();
150         SetUnhandledExceptionFilter(vlc_exception_filter);
151     }
152 # endif /* WIN64 */
153
154 #else
155     char **argv, psz_cmdline[wcslen(lpCmdLine) * 4];
156
157     WideCharToMultiByte( CP_UTF8, 0, lpCmdLine, -1,
158                          psz_cmdline, sizeof (psz_cmdline), NULL, NULL );
159
160     argc = parse_cmdline (psz_cmdline, &argv);
161 #endif
162
163     libvlc_exception_t ex, dummy;
164     libvlc_exception_init (&ex);
165     libvlc_exception_init (&dummy);
166
167     /* Initialize libvlc */
168     libvlc_instance_t *vlc;
169     vlc = libvlc_new (argc - 1, (const char **)argv + 1, &ex);
170     if (vlc != NULL)
171     {
172         libvlc_add_intf (vlc, "globalhotkeys,none", &ex);
173         libvlc_add_intf (vlc, NULL, &ex);
174         libvlc_playlist_play (vlc, -1, 0, NULL, &dummy);
175         libvlc_wait (vlc);
176         libvlc_release (vlc);
177     }
178
179     ret = libvlc_exception_raised (&ex);
180     libvlc_exception_clear (&ex);
181     libvlc_exception_clear (&dummy);
182
183     for (int i = 0; i < argc; i++)
184         free (argv[i]);
185
186     (void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
187     return ret;
188 }
189
190 #if !defined( UNDER_CE ) && !defined( _WIN64 )
191
192 static void get_crashdump_path(wchar_t * wdir)
193 {
194     if( S_OK != SHGetFolderPathW( NULL,
195                         CSIDL_APPDATA | CSIDL_FLAG_CREATE,
196                         NULL, SHGFP_TYPE_CURRENT, wdir ) )
197         fprintf( stderr, "Can't open the vlc conf PATH\n" );
198
199     swprintf( wdir+wcslen( wdir ), L"%s", L"\\vlc\\crashdump" );
200 }
201
202 static void check_crashdump()
203 {
204     wchar_t * wdir = (wchar_t *)malloc(sizeof(wchar_t)*MAX_PATH);
205     get_crashdump_path(wdir);
206
207     FILE * fd = _wfopen ( wdir, L"r, ccs=UTF-8" );
208     if( fd )
209     {
210         fclose( fd );
211         int answer = MessageBox( NULL, L"VLC media player just crashed." \
212         " Do you want to send a bug report to the developers team?",
213         L"VLC crash reporting", MB_YESNO);
214
215         if(answer == IDYES)
216         {
217             HINTERNET Hint = InternetOpen(L"VLC Crash Reporter", INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL,0);
218             if(Hint)
219             {
220                 HINTERNET ftp = InternetConnect(Hint, L"crash.videolan.org", INTERNET_DEFAULT_FTP_PORT,
221                                                 NULL, NULL, INTERNET_SERVICE_FTP, 0, 0);
222                 if(ftp)
223                 {
224                     SYSTEMTIME now;
225                     GetSystemTime(&now);
226                     wchar_t remote_file[MAX_PATH];
227                     swprintf( remote_file, L"/crashs/%04d%02d%02d%02d%02d%02d",now.wYear,
228                             now.wMonth, now.wDay, now.wHour, now.wMinute, now.wSecond  );
229
230                     FtpPutFile( ftp, wdir, remote_file, FTP_TRANSFER_TYPE_BINARY, 0);
231                     InternetCloseHandle(ftp);
232                 }
233                 else
234                     fprintf(stderr,"Can't connect to FTP server%d\n",GetLastError());
235                 InternetCloseHandle(Hint);
236             }
237         }
238
239         _wremove(wdir);
240     }
241     free((void *)wdir);
242 }
243
244 /*****************************************************************************
245  * vlc_exception_filter: handles unhandled exceptions, like segfaults
246  *****************************************************************************/
247 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
248 {
249     if(IsDebuggerPresent())
250     {
251         //If a debugger is present, pass the exception to the debugger with EXCEPTION_CONTINUE_SEARCH
252         return EXCEPTION_CONTINUE_SEARCH;
253     }
254     else
255     {
256         fprintf( stderr, "unhandled vlc exception\n" );
257
258         wchar_t * wdir = (wchar_t *)malloc(sizeof(wchar_t)*MAX_PATH);
259         get_crashdump_path(wdir);
260         FILE * fd = _wfopen ( wdir, L"w, ccs=UTF-8" );
261         free((void *)wdir);
262
263         if( !fd )
264         {
265             fprintf( stderr, "\nerror while opening file" );
266             exit( 1 );
267         }
268
269         OSVERSIONINFO osvi;
270         ZeroMemory( &osvi, sizeof(OSVERSIONINFO) );
271         osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
272         GetVersionEx( &osvi );
273
274         fwprintf( fd, L"[version]\nOS=%d.%d.%d.%d.%s\nVLC=" VERSION_MESSAGE, osvi.dwMajorVersion,
275                                                                osvi.dwMinorVersion,
276                                                                osvi.dwBuildNumber,
277                                                                osvi.dwPlatformId,
278                                                                osvi.szCSDVersion);
279
280         const CONTEXT *const pContext = (const CONTEXT *)lpExceptionInfo->ContextRecord;
281         const EXCEPTION_RECORD *const pException = (const EXCEPTION_RECORD *)lpExceptionInfo->ExceptionRecord;
282         /*No nested exceptions for now*/
283         fwprintf( fd, L"\n\n[exceptions]\n%08x at %08x",pException->ExceptionCode,
284                                                 pException->ExceptionAddress );
285         if( pException->NumberParameters > 0 )
286         {
287             unsigned int i;
288             for( i = 0; i < pException->NumberParameters; i++ )
289                 fprintf( fd, " | %08x", pException->ExceptionInformation[i] );
290         }
291
292         fwprintf( fd, L"\n\n[context]\nEDI:%08x\nESI:%08x\n" \
293                     "EBX:%08x\nEDX:%08x\nECX:%08x\nEAX:%08x\n" \
294                     "EBP:%08x\nEIP:%08x\nESP:%08x\n",
295                         pContext->Edi,pContext->Esi,pContext->Ebx,
296                         pContext->Edx,pContext->Ecx,pContext->Eax,
297                         pContext->Ebp,pContext->Eip,pContext->Esp );
298
299         fwprintf( fd, L"\n[stacktrace]\n#EIP|base|module\n" );
300
301         wchar_t module[ 256 ];
302         MEMORY_BASIC_INFORMATION mbi ;
303         VirtualQuery( (DWORD *)pContext->Eip, &mbi, sizeof( mbi ) ) ;
304         HINSTANCE hInstance = mbi.AllocationBase;
305         GetModuleFileName( hInstance, module, 256 ) ;
306         fwprintf( fd, L"%08x|%s\n", pContext->Eip, module );
307
308         DWORD pEbp = pContext->Ebp;
309         DWORD caller = *((DWORD*)pEbp + 1);
310
311         do
312         {
313             VirtualQuery( (DWORD *)caller, &mbi, sizeof( mbi ) ) ;
314             HINSTANCE hInstance = mbi.AllocationBase;
315             GetModuleFileName( hInstance, module, 256 ) ;
316             fwprintf( fd, L"%08x|%s\n", caller, module );
317             pEbp = *(DWORD*)pEbp ;
318             caller = *((DWORD*)pEbp + 1) ;
319             /*The last EBP points to NULL!*/
320         }while(caller);
321
322         fclose( fd );
323         fflush( stderr );
324         exit( 1 );
325     }
326 }
327 #endif