]> git.sesse.net Git - vlc/blob - bin/winvlc.c
Win32: stop stacktrace on bad pointer
[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 #ifndef UNICODE
32 #define UNICODE
33 #endif
34
35 #include <vlc/vlc.h>
36 #include <windows.h>
37 #include <shellapi.h>
38
39 #ifndef _WIN32_IE
40 #  define  _WIN32_IE 0x501
41 #endif
42 #include <fcntl.h>
43 #include <io.h>
44 #include <shlobj.h>
45 #include <wininet.h>
46 #define PSAPI_VERSION 1
47 #include <psapi.h>
48 #define HeapEnableTerminationOnCorruption (HEAP_INFORMATION_CLASS)1
49 static void check_crashdump(void);
50 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo);
51 static const wchar_t *crashdump_path;
52
53 static char *FromWide (const wchar_t *wide)
54 {
55     size_t len;
56     len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
57
58     char *out = (char *)malloc (len);
59     if (out)
60         WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
61     return out;
62 }
63
64 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
65                     LPSTR lpCmdLine,
66                     int nCmdShow )
67 {
68     int argc;
69
70     /* VLC does not change the thread locale, so gettext/libintil will use the
71      * user default locale as reference. */
72     /* gettext versions 0.18-0.18.1 will use the Windows Vista locale name
73      * if the GETTEXT_MUI environment variable is set. If not set or if running
74      * on Windows 2000/XP/2003 an hard-coded language ID list is used. This
75      * putenv() call may become redundant with later versions of gettext. */
76     putenv("GETTEXT_MUI=1");
77 #ifdef TOP_BUILDDIR
78     putenv("VLC_PLUGIN_PATH=Z:"TOP_BUILDDIR"/modules");
79     putenv("VLC_DATA_PATH=Z:"TOP_SRCDIR"/share");
80 #endif
81
82     SetErrorMode(SEM_FAILCRITICALERRORS);
83     HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
84
85     /* SetProcessDEPPolicy */
86     HINSTANCE h_Kernel32 = LoadLibraryW(L"kernel32.dll");
87     if(h_Kernel32)
88     {
89         BOOL (WINAPI * mySetProcessDEPPolicy)( DWORD dwFlags);
90         BOOL (WINAPI * mySetDllDirectoryA)(const char* lpPathName);
91 # define PROCESS_DEP_ENABLE 1
92
93         mySetProcessDEPPolicy = (BOOL WINAPI (*)(DWORD))
94                             GetProcAddress(h_Kernel32, "SetProcessDEPPolicy");
95         if(mySetProcessDEPPolicy)
96             mySetProcessDEPPolicy(PROCESS_DEP_ENABLE);
97
98         /* Do NOT load any library from cwd. */
99         mySetDllDirectoryA = (BOOL WINAPI (*)(const char*))
100                             GetProcAddress(h_Kernel32, "SetDllDirectoryA");
101         if(mySetDllDirectoryA)
102             mySetDllDirectoryA("");
103
104         FreeLibrary(h_Kernel32);
105     }
106
107     /* Args */
108     wchar_t **wargv = CommandLineToArgvW (GetCommandLine (), &argc);
109     if (wargv == NULL)
110         return 1;
111
112     char *argv[argc + 4];
113     BOOL crash_handling = TRUE;
114     int j = 0;
115     char *lang = NULL;
116
117     argv[j++] = FromWide( L"--media-library" );
118     argv[j++] = FromWide( L"--stats" );
119     argv[j++] = FromWide( L"--no-ignore-config" );
120     for (int i = 1; i < argc; i++)
121     {
122         if(!wcscmp(wargv[i], L"--no-crashdump"))
123         {
124             crash_handling = FALSE;
125             continue; /* don't give argument to libvlc */
126         }
127         if (!wcsncmp(wargv[i], L"--language", 10) )
128         {
129             if (i < argc - 1 && wcsncmp( wargv[i + 1], L"--", 2 ))
130                 lang = FromWide (wargv[++i]);
131             continue;
132         }
133
134         argv[j++] = FromWide (wargv[i]);
135     }
136
137     argc = j;
138     argv[argc] = NULL;
139     LocalFree (wargv);
140
141     if(crash_handling)
142     {
143         static wchar_t path[MAX_PATH];
144         if( S_OK != SHGetFolderPathW( NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE,
145                     NULL, SHGFP_TYPE_CURRENT, path ) )
146             fprintf( stderr, "Can't open the vlc conf PATH\n" );
147         _snwprintf( path+wcslen( path ), MAX_PATH,  L"%s", L"\\vlc\\crashdump" );
148         crashdump_path = &path[0];
149
150         check_crashdump();
151         SetUnhandledExceptionFilter(vlc_exception_filter);
152     }
153
154     _setmode( STDIN_FILENO, _O_BINARY ); /* Needed for pipes */
155
156     /* */
157     if (!lang)
158     {
159         HKEY h_key;
160         if( RegOpenKeyEx( HKEY_CURRENT_USER, TEXT("Software\\VideoLAN\\VLC\\"), 0, KEY_READ, &h_key )
161                 == ERROR_SUCCESS )
162         {
163             TCHAR szData[256];
164             DWORD len = 256;
165             if( RegQueryValueEx( h_key, TEXT("Lang"), NULL, NULL, (LPBYTE) &szData, &len ) == ERROR_SUCCESS )
166                 lang = FromWide( szData );
167         }
168     }
169
170     if (lang && strncmp( lang, "auto", 4 ) )
171     {
172         char tmp[11];
173         snprintf(tmp, 11, "LANG=%s", lang);
174         putenv(tmp);
175     }
176     free(lang);
177
178     /* Initialize libvlc */
179     libvlc_instance_t *vlc;
180     vlc = libvlc_new (argc, (const char **)argv);
181     if (vlc != NULL)
182     {
183         libvlc_set_app_id (vlc, "org.VideoLAN.VLC", PACKAGE_VERSION,
184                            PACKAGE_NAME);
185         libvlc_set_user_agent (vlc, "VLC media player", "VLC/"PACKAGE_VERSION);
186         libvlc_add_intf (vlc, "hotkeys,none");
187         libvlc_add_intf (vlc, "globalhotkeys,none");
188         libvlc_add_intf (vlc, NULL);
189         libvlc_playlist_play (vlc, -1, 0, NULL);
190         libvlc_wait (vlc);
191         libvlc_release (vlc);
192     }
193     else
194         MessageBox (NULL, TEXT("VLC media player could not start.\n"
195                     "Either the command line options were invalid or no plugins were found.\n"),
196                     TEXT("VLC media player"),
197                     MB_OK|MB_ICONERROR);
198
199
200     for (int i = 0; i < argc; i++)
201         free (argv[i]);
202
203     (void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
204     return 0;
205 }
206
207 /* Crashdumps handling */
208 static void check_crashdump(void)
209 {
210     wchar_t mv_crashdump_path[MAX_PATH];
211     wcscpy (mv_crashdump_path, crashdump_path);
212     wcscat (mv_crashdump_path, L".mv");
213
214     if (_wrename (crashdump_path, mv_crashdump_path))
215         return;
216
217     FILE * fd = _wfopen ( mv_crashdump_path, L"r, ccs=UTF-8" );
218     if( !fd )
219         return;
220     fclose( fd );
221
222     int answer = MessageBox( NULL, L"Ooops: VLC media player just crashed.\n" \
223     "Would you like to send a bug report to the developers team?",
224     L"VLC crash reporting", MB_YESNO);
225
226     if(answer == IDYES)
227     {
228         HINTERNET Hint = InternetOpen(L"VLC Crash Reporter",
229                 INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL,0);
230         if(Hint)
231         {
232             HINTERNET ftp = InternetConnect(Hint, L"crash.videolan.org",
233                         INTERNET_DEFAULT_FTP_PORT, NULL, NULL,
234                         INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
235             if(ftp)
236             {
237                 SYSTEMTIME now;
238                 GetSystemTime(&now);
239                 wchar_t remote_file[MAX_PATH];
240                 _snwprintf(remote_file, MAX_PATH,
241                         L"/crashes-win32/%04d%02d%02d%02d%02d%02d",
242                         now.wYear, now.wMonth, now.wDay, now.wHour,
243                         now.wMinute, now.wSecond );
244
245                 if( FtpPutFile( ftp, mv_crashdump_path, remote_file,
246                             FTP_TRANSFER_TYPE_BINARY, 0) )
247                     MessageBox( NULL, L"Report sent correctly. Thanks a lot " \
248                                 "for the help.", L"Report sent", MB_OK);
249                 else
250                     MessageBox( NULL, L"There was an error while "\
251                                 "transferring the data to the FTP server.\n"\
252                                 "Thanks a lot for the help.",
253                                 L"Report sending failed", MB_OK);
254                 InternetCloseHandle(ftp);
255             }
256             else
257             {
258                 MessageBox( NULL, L"There was an error while connecting to " \
259                                 "the FTP server. "\
260                                 "Thanks a lot for the help.",
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 the Internet.\n"\
270                                 "Thanks a lot for the help anyway.",
271                                 L"Report sending failed", MB_OK);
272         }
273     }
274
275     _wremove(mv_crashdump_path);
276 }
277
278 /*****************************************************************************
279  * vlc_exception_filter: handles unhandled exceptions, like segfaults
280  *****************************************************************************/
281 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
282 {
283     if(IsDebuggerPresent())
284     {
285         //If a debugger is present, pass the exception to the debugger
286         //with EXCEPTION_CONTINUE_SEARCH
287         return EXCEPTION_CONTINUE_SEARCH;
288     }
289     else
290     {
291         fprintf( stderr, "unhandled vlc exception\n" );
292
293         FILE * fd = _wfopen ( crashdump_path, L"w, ccs=UTF-8" );
294
295         if( !fd )
296         {
297             fprintf( stderr, "\nerror while opening file" );
298             exit( 1 );
299         }
300
301         OSVERSIONINFO osvi;
302         ZeroMemory( &osvi, sizeof(OSVERSIONINFO) );
303         osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
304         GetVersionEx( &osvi );
305
306         fwprintf( fd, L"[version]\nOS=%d.%d.%d.%d.%ls\nVLC=" VERSION_MESSAGE,
307                 osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.dwBuildNumber,
308                 osvi.dwPlatformId, osvi.szCSDVersion);
309
310         const CONTEXT *const pContext = (const CONTEXT *)
311             lpExceptionInfo->ContextRecord;
312         const EXCEPTION_RECORD *const pException = (const EXCEPTION_RECORD *)
313             lpExceptionInfo->ExceptionRecord;
314         /* No nested exceptions for now */
315         fwprintf( fd, L"\n\n[exceptions]\n%08x at %px", 
316                 pException->ExceptionCode, pException->ExceptionAddress );
317
318         for( unsigned int i = 0; i < pException->NumberParameters; i++ )
319             fwprintf( fd, L" | %p", pException->ExceptionInformation[i] );
320
321 #ifdef _WIN64
322         fwprintf( fd, L"\n\n[context]\nRDI:%px\nRSI:%px\n" \
323                     "RBX:%px\nRDX:%px\nRCX:%px\nRAX:%px\n" \
324                     "RBP:%px\nRIP:%px\nRSP:%px\nR8:%px\n" \
325                     "R9:%px\nR10:%px\nR11:%px\nR12:%px\n" \
326                     "R13:%px\nR14:%px\nR15:%px\n",
327                         pContext->Rdi,pContext->Rsi,pContext->Rbx,
328                         pContext->Rdx,pContext->Rcx,pContext->Rax,
329                         pContext->Rbp,pContext->Rip,pContext->Rsp,
330                         pContext->R8,pContext->R9,pContext->R10,
331                         pContext->R11,pContext->R12,pContext->R13,
332                         pContext->R14,pContext->R15 );
333 #else
334         fwprintf( fd, L"\n\n[context]\nEDI:%px\nESI:%px\n" \
335                     "EBX:%px\nEDX:%px\nECX:%px\nEAX:%px\n" \
336                     "EBP:%px\nEIP:%px\nESP:%px\n",
337                         pContext->Edi,pContext->Esi,pContext->Ebx,
338                         pContext->Edx,pContext->Ecx,pContext->Eax,
339                         pContext->Ebp,pContext->Eip,pContext->Esp );
340 #endif
341
342         fwprintf( fd, L"\n[stacktrace]\n#EIP|base|module\n" );
343
344 #ifdef _WIN64
345         LPCVOID caller = (LPCVOID)pContext->Rip;
346         LPVOID *pBase  = (LPVOID*)pContext->Rbp;
347 #else
348         LPVOID *pBase  = (LPVOID*)pContext->Ebp;
349         LPCVOID caller = (LPCVOID)pContext->Eip;
350 #endif
351         for( unsigned frame = 0; frame <= 100; frame++ )
352         {
353             MEMORY_BASIC_INFORMATION mbi;
354             wchar_t module[ 256 ];
355             VirtualQuery( caller, &mbi, sizeof( mbi ) ) ;
356             GetModuleFileName( mbi.AllocationBase, module, 256 );
357             fwprintf( fd, L"%p|%ls\n", caller, module );
358
359             if( IsBadReadPtr( pBase, 2 * sizeof( void* ) ) )
360                 break;
361
362             /*The last BP points to NULL!*/
363             caller = *(pBase + 1);
364             if( !caller )
365                 break;
366             pBase = *pBase;
367             if( !pBase )
368                 break;
369         }
370
371         HANDLE hpid = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
372                                         FALSE, GetCurrentProcessId());
373         if (hpid) {
374             HMODULE mods[1024];
375             DWORD size;
376             if (EnumProcessModules(hpid, mods, sizeof(mods), &size)) {
377                 fwprintf( fd, L"\n\n[modules]\n" );
378                 for (unsigned int i = 0; i < size / sizeof(HMODULE); i++) {
379                     wchar_t module[ 256 ];
380                     GetModuleFileName(mods[i], module, 256);
381                     fwprintf( fd, L"%p|%ls\n", mods[i], module);
382                 }
383             }
384             CloseHandle(hpid);
385         }
386
387         fclose( fd );
388         fflush( stderr );
389         exit( 1 );
390     }
391 }