winvlc.c 16.6 KB
Newer Older
1
/*****************************************************************************
2
 * winvlc.c: the Windows VLC media player
3
 *****************************************************************************
4
 * Copyright (C) 1998-2011 the VideoLAN team
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 * Authors: Vincent Seguin <seguin@via.ecp.fr>
 *          Samuel Hocevar <sam@zoy.org>
 *          Gildas Bazin <gbazin@videolan.org>
 *          Derk-Jan Hartman <hartman at videolan dot org>
 *          Lots of other people, see the libvlc AUTHORS file
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
31
#ifndef UNICODE
32
#define UNICODE
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
33 34
#endif

35 36
#include <vlc/vlc.h>
#include <windows.h>
37
#include <shellapi.h>
38

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
39 40 41 42 43 44 45 46 47 48
#ifndef _WIN32_IE
#  define  _WIN32_IE 0x501
#endif
#include <fcntl.h>
#include <io.h>
#include <shlobj.h>
#include <wininet.h>
#define PSAPI_VERSION 1
#include <psapi.h>
#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 56
    size_t len;
    len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
57

58 59 60 61
    char *out = (char *)malloc (len);
    if (out)
        WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
    return out;
62 63
}

64 65 66 67 68 69 70
#if (_WIN32_WINNT < _WIN32_WINNT_WIN8)
static BOOL SetDefaultDllDirectories_(DWORD flags)
{
    HMODULE h = GetModuleHandle(TEXT("kernel32.dll"));
    if (h == NULL)
        return FALSE;

71
    BOOL (WINAPI * SetDefaultDllDirectoriesReal)(DWORD);
72 73 74 75 76 77 78 79 80

    SetDefaultDllDirectoriesReal = GetProcAddress(h,
                                                  "SetDefaultDllDirectories");
    if (SetDefaultDllDirectoriesReal == NULL)
        return FALSE;

    return SetDefaultDllDirectoriesReal(flags);
}
# define SetDefaultDllDirectories SetDefaultDllDirectories_
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

#endif

static void PrioritizeSystem32(void)
{
#ifndef HAVE_PROCESS_MITIGATION_IMAGE_LOAD_POLICY
    typedef struct _PROCESS_MITIGATION_IMAGE_LOAD_POLICY {
      union {
        DWORD  Flags;
        struct {
          DWORD NoRemoteImages  :1;
          DWORD NoLowMandatoryLabelImages  :1;
          DWORD PreferSystem32Images  :1;
          DWORD ReservedFlags  :29;
        };
      };
    } PROCESS_MITIGATION_IMAGE_LOAD_POLICY;
98
#endif
99 100 101 102 103 104 105 106 107 108 109 110 111 112
#if _WIN32_WINNT < _WIN32_WINNT_WIN8
    BOOL WINAPI (*SetProcessMitigationPolicy)(PROCESS_MITIGATION_POLICY, PVOID, SIZE_T);
    HINSTANCE h_Kernel32 = GetModuleHandle(TEXT("kernel32.dll"));
    if ( !h_Kernel32 )
        return;
    SetProcessMitigationPolicy = (BOOL (WINAPI *)(PROCESS_MITIGATION_POLICY, PVOID, SIZE_T))
                                   GetProcAddress(h_Kernel32, "SetProcessMitigationPolicy");
    if (SetProcessMitigationPolicy == NULL)
        return;
#endif
    PROCESS_MITIGATION_IMAGE_LOAD_POLICY m = { .Flags = 0 };
    m.PreferSystem32Images = 1;
    SetProcessMitigationPolicy( 10 /* ProcessImageLoadPolicy */, &m, sizeof( m ) );
}
113

114 115 116
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    LPSTR lpCmdLine,
                    int nCmdShow )
117
{
118
    int argc;
119

120 121 122 123 124 125 126
    /* VLC does not change the thread locale, so gettext/libintil will use the
     * user default locale as reference. */
    /* gettext versions 0.18-0.18.1 will use the Windows Vista locale name
     * if the GETTEXT_MUI environment variable is set. If not set or if running
     * on Windows 2000/XP/2003 an hard-coded language ID list is used. This
     * putenv() call may become redundant with later versions of gettext. */
    putenv("GETTEXT_MUI=1");
127 128
#ifdef TOP_BUILDDIR
    putenv("VLC_PLUGIN_PATH=Z:"TOP_BUILDDIR"/modules");
129
    putenv("VLC_DATA_PATH=Z:"TOP_SRCDIR"/share");
130 131
#endif

132
    SetErrorMode(SEM_FAILCRITICALERRORS);
133
    HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
134

135
    /* SetProcessDEPPolicy, SetDllDirectory, & Co. */
136 137
    HINSTANCE h_Kernel32 = GetModuleHandle(TEXT("kernel32.dll"));
    if (h_Kernel32 != NULL)
138
    {
139
        /* Enable DEP */
140
# define PROCESS_DEP_ENABLE 1
141
        BOOL (WINAPI * mySetProcessDEPPolicy)( DWORD dwFlags);
142
        mySetProcessDEPPolicy = (BOOL (WINAPI *)(DWORD))
143 144 145
                            GetProcAddress(h_Kernel32, "SetProcessDEPPolicy");
        if(mySetProcessDEPPolicy)
            mySetProcessDEPPolicy(PROCESS_DEP_ENABLE);
146 147

        /* Do NOT load any library from cwd. */
148
        BOOL (WINAPI * mySetDllDirectoryA)(const char* lpPathName);
149
        mySetDllDirectoryA = (BOOL (WINAPI *)(const char*))
150
                            GetProcAddress(h_Kernel32, "SetDllDirectoryA");
151 152
        if(mySetDllDirectoryA)
            mySetDllDirectoryA("");
153 154
    }

155 156 157 158
    /***
     * The LoadLibrary* calls from the modules and the 3rd party code
     * will search in SYSTEM32 only
     * */
159
    SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_SYSTEM32);
160 161 162 163
    /***
     * Load DLLs from system32 before any other folder (when possible)
     */
    PrioritizeSystem32();
164

165
    /* Args */
166 167 168
    wchar_t **wargv = CommandLineToArgvW (GetCommandLine (), &argc);
    if (wargv == NULL)
        return 1;
169

170
    char *argv[argc + 3];
171 172
    BOOL crash_handling = TRUE;
    int j = 0;
173
    char *lang = NULL;
174

175
    argv[j++] = FromWide( L"--media-library" );
176 177
    argv[j++] = FromWide( L"--no-ignore-config" );
    for (int i = 1; i < argc; i++)
178 179 180 181
    {
        if(!wcscmp(wargv[i], L"--no-crashdump"))
        {
            crash_handling = FALSE;
Rafaël Carré's avatar
Rafaël Carré committed
182
            continue; /* don't give argument to libvlc */
183
        }
184 185 186 187 188 189
        if (!wcsncmp(wargv[i], L"--language", 10) )
        {
            if (i < argc - 1 && wcsncmp( wargv[i + 1], L"--", 2 ))
                lang = FromWide (wargv[++i]);
            continue;
        }
Rafaël Carré's avatar
Rafaël Carré committed
190 191

        argv[j++] = FromWide (wargv[i]);
192 193 194
    }

    argc = j;
195 196
    argv[argc] = NULL;
    LocalFree (wargv);
197 198 199

    if(crash_handling)
    {
200 201 202 203
        static wchar_t path[MAX_PATH];
        if( S_OK != SHGetFolderPathW( NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE,
                    NULL, SHGFP_TYPE_CURRENT, path ) )
            fprintf( stderr, "Can't open the vlc conf PATH\n" );
204
        _snwprintf( path+wcslen( path ), MAX_PATH,  L"%s", L"\\vlc\\crashdump" );
205 206
        crashdump_path = &path[0];

207 208 209 210
        check_crashdump();
        SetUnhandledExceptionFilter(vlc_exception_filter);
    }

211
    _setmode( _fileno( stdin ), _O_BINARY ); /* Needed for pipes */
212

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    /* */
    if (!lang)
    {
        HKEY h_key;
        if( RegOpenKeyEx( HKEY_CURRENT_USER, TEXT("Software\\VideoLAN\\VLC\\"), 0, KEY_READ, &h_key )
                == ERROR_SUCCESS )
        {
            TCHAR szData[256];
            DWORD len = 256;
            if( RegQueryValueEx( h_key, TEXT("Lang"), NULL, NULL, (LPBYTE) &szData, &len ) == ERROR_SUCCESS )
                lang = FromWide( szData );
        }
    }

    if (lang && strncmp( lang, "auto", 4 ) )
    {
229 230 231
        char tmp[11];
        snprintf(tmp, 11, "LANG=%s", lang);
        putenv(tmp);
232 233 234
    }
    free(lang);

235
    /* Initialize libvlc */
236
    libvlc_instance_t *vlc;
237
    vlc = libvlc_new (argc, (const char **)argv);
238 239
    if (vlc != NULL)
    {
240 241
        libvlc_set_app_id (vlc, "org.VideoLAN.VLC", PACKAGE_VERSION,
                           PACKAGE_NAME);
242
        libvlc_set_user_agent (vlc, "VLC media player", "VLC/"PACKAGE_VERSION);
243
        libvlc_add_intf (vlc, "hotkeys,none");
244 245
        libvlc_add_intf (vlc, "globalhotkeys,none");
        libvlc_add_intf (vlc, NULL);
246
        libvlc_playlist_play (vlc, -1, 0, NULL);
247 248 249
        libvlc_wait (vlc);
        libvlc_release (vlc);
    }
250 251 252 253 254 255
    else
        MessageBox (NULL, TEXT("VLC media player could not start.\n"
                    "Either the command line options were invalid or no plugins were found.\n"),
                    TEXT("VLC media player"),
                    MB_OK|MB_ICONERROR);

256

257 258 259 260
    for (int i = 0; i < argc; i++)
        free (argv[i]);

    (void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
261
    return 0;
262
}
263

264
/* Crashdumps handling */
265
static void check_crashdump(void)
266
{
267 268 269 270 271 272 273 274
    wchar_t mv_crashdump_path[MAX_PATH];
    wcscpy (mv_crashdump_path, crashdump_path);
    wcscat (mv_crashdump_path, L".mv");

    if (_wrename (crashdump_path, mv_crashdump_path))
        return;

    FILE * fd = _wfopen ( mv_crashdump_path, L"r, ccs=UTF-8" );
275 276 277
    if( !fd )
        return;
    fclose( fd );
278

279 280
    int answer = MessageBox( NULL, L"Ooops: VLC media player just crashed.\n" \
    "Would you like to send a bug report to the developers team?",
281
    L"VLC crash reporting", MB_YESNO);
282

283
    if(answer == IDYES)
284
    {
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
        HMODULE hWininet = LoadLibrary(TEXT("wininet.dll"));
        if (hWininet == NULL)
        {
            fprintf(stderr, "There was an error loading the network"
                    " 0x%08lx\n", (unsigned long)GetLastError());
            goto done;
        }

        HINTERNET (WINAPI *InternetOpenW_)(LPCWSTR ,DWORD dwAccessType,LPCWSTR lpszProxy,LPCWSTR lpszProxyBypass,DWORD dwFlags);
        HINTERNET (WINAPI *InternetConnectW_)(HINTERNET hInternet,LPCWSTR lpszServerName,INTERNET_PORT nServerPort,LPCWSTR lpszUserName,LPCWSTR lpszPassword,DWORD dwService,DWORD dwFlags,DWORD_PTR dwContext);
        BOOL (WINAPI *InternetCloseHandle_)(HINTERNET hInternet);
        BOOL (WINAPI *FtpPutFileW_)(HINTERNET hConnect,LPCWSTR lpszLocalFile,LPCWSTR lpszNewRemoteFile,DWORD dwFlags,DWORD_PTR dwContext);
        InternetOpenW_       = (void*)GetProcAddress(hWininet, "InternetOpenW");
        InternetConnectW_    = (void*)GetProcAddress(hWininet, "InternetConnectW");
        InternetCloseHandle_ = (void*)GetProcAddress(hWininet, "InternetCloseHandle");
        FtpPutFileW_         = (void*)GetProcAddress(hWininet, "FtpPutFileW");
        if (!InternetOpenW_ || !InternetConnectW_ || !InternetCloseHandle_ || !FtpPutFileW_)
        {
            fprintf(stderr, "There was an error loading the network API entries"
                    " 0x%08lx\n", (unsigned long)GetLastError());
            goto done;
        }

        HINTERNET Hint = InternetOpenW_(L"VLC Crash Reporter",
309
                INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL,0);
310
        if(Hint)
311
        {
312
            HINTERNET ftp = InternetConnectW_(Hint, L"crash.videolan.org",
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
313
                        INTERNET_DEFAULT_FTP_PORT, NULL, NULL,
314
                        INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
315
            if(ftp)
316
            {
317 318 319
                SYSTEMTIME now;
                GetSystemTime(&now);
                wchar_t remote_file[MAX_PATH];
320
                _snwprintf(remote_file, MAX_PATH,
321 322 323 324
                        L"/crashes-win32/%04d%02d%02d%02d%02d%02d",
                        now.wYear, now.wMonth, now.wDay, now.wHour,
                        now.wMinute, now.wSecond );

325
                if( FtpPutFileW_( ftp, mv_crashdump_path, remote_file,
326
                            FTP_TRANSFER_TYPE_BINARY, 0) )
327
                    fprintf(stderr, "Report sent correctly to FTP.\n");
328
                else
329
                    fprintf(stderr,"Couldn't send report to FTP server\n");
330

331
                InternetCloseHandle_(ftp);
332
            }
333 334
            else
            {
335
                fprintf(stderr, "Can't connect to FTP server 0x%08lx\n",
336
                        (unsigned long)GetLastError());
337
            }
338
            InternetCloseHandle_(Hint);
339 340 341
        }
        else
        {
342 343
              fprintf(stderr, "There was an error while connecting to the "
                      "Internet  0x%08lx\n", (unsigned long)GetLastError());
344
        }
345 346 347
done:
        if (hWininet != NULL)
            FreeLibrary(hWininet);
348 349
        MessageBox( NULL, L"Thanks a lot for helping improving VLC!",
                    L"VLC crash report" , MB_OK);
350
    }
351

352
    _wremove(mv_crashdump_path);
353 354
}

355 356 357 358 359
/*****************************************************************************
 * vlc_exception_filter: handles unhandled exceptions, like segfaults
 *****************************************************************************/
LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
{
360
    if(IsDebuggerPresent())
361
    {
362 363
        //If a debugger is present, pass the exception to the debugger
        //with EXCEPTION_CONTINUE_SEARCH
364
        return EXCEPTION_CONTINUE_SEARCH;
365
    }
366
    else
367
    {
368
        fprintf( stderr, "unhandled vlc exception\n" );
369

370
        FILE * fd = _wfopen ( crashdump_path, L"w, ccs=UTF-8" );
371 372 373 374 375 376

        if( !fd )
        {
            fprintf( stderr, "\nerror while opening file" );
            exit( 1 );
        }
377

378 379 380 381 382
        OSVERSIONINFO osvi;
        ZeroMemory( &osvi, sizeof(OSVERSIONINFO) );
        osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
        GetVersionEx( &osvi );

383
        fwprintf( fd, L"[version]\nOS=%d.%d.%d.%d.%ls\nVLC=" VERSION_MESSAGE,
384 385 386 387 388 389 390 391
                osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.dwBuildNumber,
                osvi.dwPlatformId, osvi.szCSDVersion);

        const CONTEXT *const pContext = (const CONTEXT *)
            lpExceptionInfo->ContextRecord;
        const EXCEPTION_RECORD *const pException = (const EXCEPTION_RECORD *)
            lpExceptionInfo->ExceptionRecord;
        /* No nested exceptions for now */
392
        fwprintf( fd, L"\n\n[exceptions]\n%08x at %px",
393
                pException->ExceptionCode, pException->ExceptionAddress );
394

Rafaël Carré's avatar
Rafaël Carré committed
395 396 397
        for( unsigned int i = 0; i < pException->NumberParameters; i++ )
            fwprintf( fd, L" | %p", pException->ExceptionInformation[i] );

398
#ifdef _WIN64
Rafaël Carré's avatar
Rafaël Carré committed
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
        fwprintf( fd, L"\n\n[context]\nRDI:%px\nRSI:%px\n" \
                    "RBX:%px\nRDX:%px\nRCX:%px\nRAX:%px\n" \
                    "RBP:%px\nRIP:%px\nRSP:%px\nR8:%px\n" \
                    "R9:%px\nR10:%px\nR11:%px\nR12:%px\n" \
                    "R13:%px\nR14:%px\nR15:%px\n",
                        pContext->Rdi,pContext->Rsi,pContext->Rbx,
                        pContext->Rdx,pContext->Rcx,pContext->Rax,
                        pContext->Rbp,pContext->Rip,pContext->Rsp,
                        pContext->R8,pContext->R9,pContext->R10,
                        pContext->R11,pContext->R12,pContext->R13,
                        pContext->R14,pContext->R15 );
#else
        fwprintf( fd, L"\n\n[context]\nEDI:%px\nESI:%px\n" \
                    "EBX:%px\nEDX:%px\nECX:%px\nEAX:%px\n" \
                    "EBP:%px\nEIP:%px\nESP:%px\n",
414 415 416
                        pContext->Edi,pContext->Esi,pContext->Ebx,
                        pContext->Edx,pContext->Ecx,pContext->Eax,
                        pContext->Ebp,pContext->Eip,pContext->Esp );
Rafaël Carré's avatar
Rafaël Carré committed
417
#endif
418

419
        fwprintf( fd, L"\n[stacktrace]\n#EIP|base|module\n" );
420

421
#ifdef _WIN64
Rafaël Carré's avatar
Rafaël Carré committed
422 423 424 425 426 427 428
        LPCVOID caller = (LPCVOID)pContext->Rip;
        LPVOID *pBase  = (LPVOID*)pContext->Rbp;
#else
        LPVOID *pBase  = (LPVOID*)pContext->Ebp;
        LPCVOID caller = (LPCVOID)pContext->Eip;
#endif
        for( unsigned frame = 0; frame <= 100; frame++ )
429
        {
Rafaël Carré's avatar
Rafaël Carré committed
430 431 432 433
            MEMORY_BASIC_INFORMATION mbi;
            wchar_t module[ 256 ];
            VirtualQuery( caller, &mbi, sizeof( mbi ) ) ;
            GetModuleFileName( mbi.AllocationBase, module, 256 );
434
            fwprintf( fd, L"%p|%ls\n", caller, module );
Rafaël Carré's avatar
Rafaël Carré committed
435

436 437 438
            if( IsBadReadPtr( pBase, 2 * sizeof( void* ) ) )
                break;

Rafaël Carré's avatar
Rafaël Carré committed
439 440 441 442 443
            /*The last BP points to NULL!*/
            caller = *(pBase + 1);
            if( !caller )
                break;
            pBase = *pBase;
444 445
            if( !pBase )
                break;
Rafaël Carré's avatar
Rafaël Carré committed
446
        }
447

448 449 450 451 452 453 454 455 456 457
        HANDLE hpid = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
                                        FALSE, GetCurrentProcessId());
        if (hpid) {
            HMODULE mods[1024];
            DWORD size;
            if (EnumProcessModules(hpid, mods, sizeof(mods), &size)) {
                fwprintf( fd, L"\n\n[modules]\n" );
                for (unsigned int i = 0; i < size / sizeof(HMODULE); i++) {
                    wchar_t module[ 256 ];
                    GetModuleFileName(mods[i], module, 256);
458
                    fwprintf( fd, L"%p|%ls\n", mods[i], module);
459 460 461 462 463
                }
            }
            CloseHandle(hpid);
        }

464 465 466 467
        fclose( fd );
        fflush( stderr );
        exit( 1 );
    }
468
}