]> git.sesse.net Git - vlc/blob - bin/winvlc.c
win64: report crashes
[vlc] / bin / winvlc.c
1 /*****************************************************************************
2  * winvlc.c: the Windows VLC media player
3  *****************************************************************************
4  * Copyright (C) 1998-2011 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 <windows.h>
34
35 #if !defined(UNDER_CE)
36 # ifndef _WIN32_IE
37 #   define  _WIN32_IE 0x501
38 # endif
39 # include <shlobj.h>
40 # include <wininet.h>
41 # define HeapEnableTerminationOnCorruption (HEAP_INFORMATION_CLASS)1
42 static void check_crashdump(void);
43 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo);
44 #endif
45
46 #ifndef UNDER_CE
47 static char *FromWide (const wchar_t *wide)
48 {
49     size_t len;
50     len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
51
52     char *out = (char *)malloc (len);
53     if (out)
54         WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
55     return out;
56 }
57 #else
58 static int parse_cmdline (char *line, char ***argvp)
59 {
60     char **argv = malloc (sizeof (char *));
61     int argc = 0;
62
63     while (*line != '\0')
64     {
65         char quote = 0;
66
67         /* Skips white spaces */
68         while (strchr ("\t ", *line))
69             line++;
70         if (!*line)
71             break;
72
73         /* Starts a new parameter */
74         argv = realloc (argv, (argc + 2) * sizeof (char *));
75         if (*line == '"')
76         {
77             quote = '"';
78             line++;
79         }
80         argv[argc++] = line;
81
82     more:
83             while (*line && !strchr ("\t ", *line))
84             line++;
85
86     if (line > argv[argc - 1] && line[-1] == quote)
87         /* End of quoted parameter */
88         line[-1] = 0;
89     else
90         if (*line && quote)
91     {
92         /* Space within a quote */
93         line++;
94         goto more;
95     }
96     else
97         /* End of unquoted parameter */
98         if (*line)
99             *line++ = 0;
100     }
101     argv[argc] = NULL;
102     *argvp = argv;
103     return argc;
104 }
105 #endif
106
107 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
108 #ifndef UNDER_CE
109                     LPSTR lpCmdLine,
110 #else
111                     LPWSTR lpCmdLine,
112 #endif
113                     int nCmdShow )
114 {
115     int argc;
116
117 #ifndef UNDER_CE
118 #ifdef TOP_BUILDDIR
119     putenv("VLC_PLUGIN_PATH=Z:"TOP_BUILDDIR"/modules");
120 #endif
121
122     HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
123
124     /* SetProcessDEPPolicy */
125     HINSTANCE h_Kernel32 = LoadLibraryW(L"kernel32.dll");
126     if(h_Kernel32)
127     {
128         BOOL (WINAPI * mySetProcessDEPPolicy)( DWORD dwFlags);
129         BOOL (WINAPI * mySetDllDirectoryA)(const char* lpPathName);
130 # define PROCESS_DEP_ENABLE 1
131
132         mySetProcessDEPPolicy = (BOOL WINAPI (*)(DWORD))
133                             GetProcAddress(h_Kernel32, "SetProcessDEPPolicy");
134         if(mySetProcessDEPPolicy)
135             mySetProcessDEPPolicy(PROCESS_DEP_ENABLE);
136
137         /* Do NOT load any library from cwd. */
138         mySetDllDirectoryA = (BOOL WINAPI (*)(const char*)) GetProcAddress(h_Kernel32, "SetDllDirectoryA");
139         if(mySetDllDirectoryA)
140             mySetDllDirectoryA("");
141
142         FreeLibrary(h_Kernel32);
143     }
144
145     /* Args */
146     wchar_t **wargv = CommandLineToArgvW (GetCommandLine (), &argc);
147     if (wargv == NULL)
148         return 1;
149
150     char *argv[argc + 3];
151     BOOL crash_handling = TRUE;
152     int j = 0;
153
154     argv[j++] = FromWide( L"--media-library" );
155     argv[j++] = FromWide( L"--no-ignore-config" );
156 #ifdef TOP_SRCDIR
157     argv[j++] = FromWide (L"--data-path=Z:"TOP_SRCDIR"/share");
158 #endif
159     for (int i = 1; i < argc; i++)
160     {
161         if(!wcscmp(wargv[i], L"--no-crashdump"))
162         {
163             crash_handling = FALSE;
164             continue; /* don't give argument to libvlc */
165         }
166
167         argv[j++] = FromWide (wargv[i]);
168     }
169
170     argc = j;
171     argv[argc] = NULL;
172     LocalFree (wargv);
173
174     if(crash_handling)
175     {
176         check_crashdump();
177         SetUnhandledExceptionFilter(vlc_exception_filter);
178     }
179
180 #else /* UNDER_CE */
181     char **argv, psz_cmdline[wcslen(lpCmdLine) * 4];
182
183     WideCharToMultiByte( CP_UTF8, 0, lpCmdLine, -1,
184                          psz_cmdline, sizeof (psz_cmdline), NULL, NULL );
185
186     argc = parse_cmdline (psz_cmdline, &argv);
187 #endif
188
189     /* Initialize libvlc */
190     libvlc_instance_t *vlc;
191     vlc = libvlc_new (argc, (const char **)argv);
192     if (vlc != NULL)
193     {
194         libvlc_add_intf (vlc, "globalhotkeys,none");
195         libvlc_add_intf (vlc, NULL);
196         libvlc_playlist_play (vlc, -1, 0, NULL);
197         libvlc_wait (vlc);
198         libvlc_release (vlc);
199     }
200
201     for (int i = 0; i < argc; i++)
202         free (argv[i]);
203
204     (void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
205     return 0;
206 }
207
208 #if !defined( UNDER_CE )
209 /* Crashdumps handling */
210 static void get_crashdump_path(wchar_t * wdir)
211 {
212     if( S_OK != SHGetFolderPathW( NULL,
213                         CSIDL_APPDATA | CSIDL_FLAG_CREATE,
214                         NULL, SHGFP_TYPE_CURRENT, wdir ) )
215         fprintf( stderr, "Can't open the vlc conf PATH\n" );
216
217     swprintf( wdir+wcslen( wdir ), L"%s", L"\\vlc\\crashdump" );
218 }
219
220 static void check_crashdump()
221 {
222     wchar_t * wdir = (wchar_t *)malloc(sizeof(wchar_t)*MAX_PATH);
223     get_crashdump_path(wdir);
224
225     FILE * fd = _wfopen ( wdir, L"r, ccs=UTF-8" );
226     if( fd )
227     {
228         fclose( fd );
229         int answer = MessageBox( NULL, L"VLC media player just crashed." \
230         " Do you want to send a bug report to the developers team?",
231         L"VLC crash reporting", MB_YESNO);
232
233         if(answer == IDYES)
234         {
235             HINTERNET Hint = InternetOpen(L"VLC Crash Reporter", INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL,0);
236             if(Hint)
237             {
238                 HINTERNET ftp = InternetConnect(Hint, L"crash.videolan.org", INTERNET_DEFAULT_FTP_PORT,
239                                                 NULL, NULL, INTERNET_SERVICE_FTP, 0, 0);
240                 if(ftp)
241                 {
242                     SYSTEMTIME now;
243                     GetSystemTime(&now);
244                     wchar_t remote_file[MAX_PATH];
245                     swprintf( remote_file, L"/crashs/%04d%02d%02d%02d%02d%02d",now.wYear,
246                             now.wMonth, now.wDay, now.wHour, now.wMinute, now.wSecond  );
247
248                     if( FtpPutFile( ftp, wdir, remote_file, FTP_TRANSFER_TYPE_BINARY, 0) )
249                         MessageBox( NULL, L"Report sent correctly. Thanks a lot for the help.",
250                                     L"Report sent", MB_OK);
251                     else
252                         MessageBox( NULL, L"There was an error while transferring to the FTP server. "\
253                                     "Thanks a lot for the help anyway.",
254                                     L"Report sending failed", MB_OK);
255                     InternetCloseHandle(ftp);
256                 }
257                 else
258                 {
259                     MessageBox( NULL, L"There was an error while connecting to the FTP server. "\
260                                     "Thanks a lot for the help anyway.",
261                                     L"Report sending failed", MB_OK);
262                     fprintf(stderr,"Can't connect to FTP server 0x%08lu\n",
263                             (unsigned long)GetLastError());
264                 }
265                 InternetCloseHandle(Hint);
266             }
267             else
268             {
269                   MessageBox( NULL, L"There was an error while connecting to Internet. "\
270                                     "Thanks a lot for the help anyway.",
271                                     L"Report sending failed", MB_OK);
272             }
273         }
274
275         _wremove(wdir);
276     }
277     free((void *)wdir);
278 }
279
280 /*****************************************************************************
281  * vlc_exception_filter: handles unhandled exceptions, like segfaults
282  *****************************************************************************/
283 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
284 {
285     if(IsDebuggerPresent())
286     {
287         //If a debugger is present, pass the exception to the debugger with EXCEPTION_CONTINUE_SEARCH
288         return EXCEPTION_CONTINUE_SEARCH;
289     }
290     else
291     {
292         fprintf( stderr, "unhandled vlc exception\n" );
293
294         wchar_t * wdir = (wchar_t *)malloc(sizeof(wchar_t)*MAX_PATH);
295         get_crashdump_path(wdir);
296         FILE * fd = _wfopen ( wdir, L"w, ccs=UTF-8" );
297         free((void *)wdir);
298
299         if( !fd )
300         {
301             fprintf( stderr, "\nerror while opening file" );
302             exit( 1 );
303         }
304
305         OSVERSIONINFO osvi;
306         ZeroMemory( &osvi, sizeof(OSVERSIONINFO) );
307         osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
308         GetVersionEx( &osvi );
309
310         fwprintf( fd, L"[version]\nOS=%d.%d.%d.%d.%s\nVLC=" VERSION_MESSAGE, osvi.dwMajorVersion,
311                                                                osvi.dwMinorVersion,
312                                                                osvi.dwBuildNumber,
313                                                                osvi.dwPlatformId,
314                                                                osvi.szCSDVersion);
315
316         const CONTEXT *const pContext = (const CONTEXT *)lpExceptionInfo->ContextRecord;
317         const EXCEPTION_RECORD *const pException = (const EXCEPTION_RECORD *)lpExceptionInfo->ExceptionRecord;
318         /*No nested exceptions for now*/
319         fwprintf( fd, L"\n\n[exceptions]\n%08x at %px",pException->ExceptionCode,
320                                                 pException->ExceptionAddress );
321
322         for( unsigned int i = 0; i < pException->NumberParameters; i++ )
323             fwprintf( fd, L" | %p", pException->ExceptionInformation[i] );
324
325 #ifdef WIN64
326         fwprintf( fd, L"\n\n[context]\nRDI:%px\nRSI:%px\n" \
327                     "RBX:%px\nRDX:%px\nRCX:%px\nRAX:%px\n" \
328                     "RBP:%px\nRIP:%px\nRSP:%px\nR8:%px\n" \
329                     "R9:%px\nR10:%px\nR11:%px\nR12:%px\n" \
330                     "R13:%px\nR14:%px\nR15:%px\n",
331                         pContext->Rdi,pContext->Rsi,pContext->Rbx,
332                         pContext->Rdx,pContext->Rcx,pContext->Rax,
333                         pContext->Rbp,pContext->Rip,pContext->Rsp,
334                         pContext->R8,pContext->R9,pContext->R10,
335                         pContext->R11,pContext->R12,pContext->R13,
336                         pContext->R14,pContext->R15 );
337 #else
338         fwprintf( fd, L"\n\n[context]\nEDI:%px\nESI:%px\n" \
339                     "EBX:%px\nEDX:%px\nECX:%px\nEAX:%px\n" \
340                     "EBP:%px\nEIP:%px\nESP:%px\n",
341                         pContext->Edi,pContext->Esi,pContext->Ebx,
342                         pContext->Edx,pContext->Ecx,pContext->Eax,
343                         pContext->Ebp,pContext->Eip,pContext->Esp );
344 #endif
345
346         fwprintf( fd, L"\n[stacktrace]\n#EIP|base|module\n" );
347
348 #ifdef WIN64
349         LPCVOID caller = (LPCVOID)pContext->Rip;
350         LPVOID *pBase  = (LPVOID*)pContext->Rbp;
351 #else
352         LPVOID *pBase  = (LPVOID*)pContext->Ebp;
353         LPCVOID caller = (LPCVOID)pContext->Eip;
354 #endif
355         for( unsigned frame = 0; frame <= 100; frame++ )
356         {
357             MEMORY_BASIC_INFORMATION mbi;
358             wchar_t module[ 256 ];
359             VirtualQuery( caller, &mbi, sizeof( mbi ) ) ;
360             GetModuleFileName( mbi.AllocationBase, module, 256 );
361             fwprintf( fd, L"%p|%s\n", caller, module );
362
363             /*The last BP points to NULL!*/
364             caller = *(pBase + 1);
365             if( !caller )
366                 break;
367             pBase = *pBase;
368         }
369
370         fclose( fd );
371         fflush( stderr );
372         exit( 1 );
373     }
374 }
375 #endif