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