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