summaryrefslogtreecommitdiffstats
path: root/desktop/source/app/crashreport.cxx
blob: 0458edf9793a11257a097f84c44a46509767a8c3 (plain)
1
2
3
4
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <desktop/crashreport.hxx>
#include <rtl/bootstrap.hxx>
#include <osl/file.hxx>
#include <comphelper/processfactory.hxx>
#include <ucbhelper/proxydecider.hxx>
#include <unotools/bootstrap.hxx>
#include <o3tl/char16_t2wchar_t.hxx>
#include <desktop/minidump.hxx>
#include <rtl/ustrbuf.hxx>

#include <config_version.h>
#include <config_folders.h>

#include <string>
#include <regex>


#if HAVE_FEATURE_BREAKPAD

#include <fstream>
#if defined( UNX ) && !defined MACOSX && !defined IOS && !defined ANDROID
#include <client/linux/handler/exception_handler.h>
#elif defined _WIN32
#if defined __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmicrosoft-enum-value"
#endif
#include <client/windows/handler/exception_handler.h>
#if defined __clang__
#pragma clang diagnostic pop
#endif
#include <locale>
#include <codecvt>
#endif

osl::Mutex CrashReporter::maMutex;
osl::Mutex CrashReporter::maActiveSfxObjectNameMutex;
osl::Mutex CrashReporter::maUnoLogCmdMutex;
std::unique_ptr<google_breakpad::ExceptionHandler> CrashReporter::mpExceptionHandler;
bool CrashReporter::mbInit = false;
CrashReporter::vmaKeyValues CrashReporter::maKeyValues;
CrashReporter::vmaloggedUnoCommands CrashReporter::maloggedUnoCommands;
OUString CrashReporter::msActiveSfxObjectName;


#if defined( UNX ) && !defined MACOSX && !defined IOS && !defined ANDROID
static bool dumpCallback(const google_breakpad::MinidumpDescriptor& descriptor, void* /*context*/, bool succeeded)
{
    CrashReporter::addKeyValue("Active-SfxObject",CrashReporter::getActiveSfxObjectName(),CrashReporter::AddItem);
    CrashReporter::addKeyValue("Last-4-Uno-Commands",CrashReporter::getLoggedUnoCommands(),CrashReporter::AddItem);
    CrashReporter::addKeyValue("DumpFile", OStringToOUString(descriptor.path(), RTL_TEXTENCODING_UTF8), CrashReporter::Write);
    SAL_WARN("desktop", "minidump generated: " << descriptor.path());

    return succeeded;
}
#elif defined _WIN32
static bool dumpCallback(const wchar_t* path, const wchar_t* id,
    void* /*context*/, EXCEPTION_POINTERS* /*exinfo*/,
    MDRawAssertionInfo* /*assertion*/,
    bool succeeded)
{
    // TODO: moggi: can we avoid this conversion
#ifdef _MSC_VER
#pragma warning (disable: 4996)
#endif
    std::wstring_convert<std::codecvt_utf8<wchar_t>> conv1;
    std::string aPath = conv1.to_bytes(std::wstring(path)) + conv1.to_bytes(std::wstring(id)) + ".dmp";
    CrashReporter::addKeyValue("Active-SfxObject",CrashReporter::getActiveSfxObjectName(),CrashReporter::AddItem);
    CrashReporter::addKeyValue("Last-4-Uno-Commands",CrashReporter::getLoggedUnoCommands(),CrashReporter::AddItem);
    CrashReporter::addKeyValue("DumpFile", OStringToOUString(aPath.c_str(), RTL_TEXTENCODING_UTF8), CrashReporter::AddItem);
    CrashReporter::addKeyValue("GDIHandles", OUString::number(::GetGuiResources(::GetCurrentProcess(), GR_GDIOBJECTS)), CrashReporter::Write);
    SAL_WARN("desktop", "minidump generated: " << aPath);
    return succeeded;
}
#endif


void CrashReporter::writeToFile(std::ios_base::openmode Openmode)
{
#if defined _WIN32
    const std::string iniPath = getIniFileName();
    std::wstring iniPathW;
    const int nChars = MultiByteToWideChar(CP_UTF8, 0, iniPath.c_str(), -1, nullptr, 0);
    auto buf = std::make_unique<wchar_t[]>(nChars);
    if (MultiByteToWideChar(CP_UTF8, 0, iniPath.c_str(), -1, buf.get(), nChars) != 0)
        iniPathW = buf.get();

    std::ofstream ini_file
        = iniPathW.empty() ? std::ofstream(iniPath, Openmode) : std::ofstream(iniPathW, Openmode);
#else
    std::ofstream ini_file(getIniFileName(), Openmode);
#endif

    for (auto& keyValue : maKeyValues)
    {
        ini_file << OUStringToOString(keyValue.first, RTL_TEXTENCODING_UTF8) << "=";
        ini_file << OUStringToOString(keyValue.second, RTL_TEXTENCODING_UTF8) << "\n";
    }

    maKeyValues.clear();
    ini_file.close();
}

void CrashReporter::addKeyValue(const OUString& rKey, const OUString& rValue, tAddKeyHandling AddKeyHandling)
{
    osl::MutexGuard aGuard(maMutex);

    if (IsDumpEnable())
    {
        if (!rKey.isEmpty())
            maKeyValues.push_back(mpair(rKey, rValue));

        if (AddKeyHandling != AddItem)
        {
            if (mbInit)
                writeToFile(std::ios_base::app);
            else if (AddKeyHandling == Create)
                writeCommonInfo();
        }
    }
}

void CrashReporter::writeCommonInfo()
{
    writeSystemInfo();

    ucbhelper::InternetProxyDecider proxy_decider(::comphelper::getProcessComponentContext());

    static const OUStringLiteral protocol = u"https";
    static const OUStringLiteral url = u"crashreport.libreoffice.org";
    const sal_Int32 port = 443;

    const ucbhelper::InternetProxyServer proxy_server = proxy_decider.getProxy(protocol, url, port);

    // save the new Keys
    vmaKeyValues atlast = maKeyValues;
    // clear the keys, the following Keys should be at the begin
    maKeyValues.clear();

    // limit the amount of code that needs to be executed before the crash reporting
    addKeyValue("ProductName", "LibreOffice", AddItem);
    addKeyValue("Version", LIBO_VERSION_DOTTED, AddItem);
    addKeyValue("BuildID", utl::Bootstrap::getBuildIdData(""), AddItem);
    addKeyValue("URL", protocol + "://" + url + "/submit/", AddItem);

    if (!proxy_server.aName.isEmpty())
    {
        addKeyValue("Proxy", proxy_server.aName + ":" + OUString::number(proxy_server.nPort), AddItem);
    }

    // write the new keys at the end
    maKeyValues.insert(maKeyValues.end(), atlast.begin(), atlast.end());

    mbInit = true;

    writeToFile(std::ios_base::trunc);

    updateMinidumpLocation();
}

void CrashReporter::setActiveSfxObjectName(const OUString& rActiveSfxObjectName)
{
    osl::MutexGuard aGuard(maActiveSfxObjectNameMutex);
    msActiveSfxObjectName = rActiveSfxObjectName;
}

OUString CrashReporter::getActiveSfxObjectName()
{
    osl::MutexGuard aGuard(maActiveSfxObjectNameMutex);
    return msActiveSfxObjectName;
}

void CrashReporter::logUnoCommand(const OUString& rUnoCommand)
{
    osl::MutexGuard aGuard(maUnoLogCmdMutex);

    if( maloggedUnoCommands.size() == 4 )
        maloggedUnoCommands.pop_front();

    maloggedUnoCommands.push_back(rUnoCommand);
}

OUString CrashReporter::getLoggedUnoCommands()
{
    osl::MutexGuard aGuard(maUnoLogCmdMutex);

    OUString aCommandSeperator="";
    OUStringBuffer aUnoCommandBuffer;

    for( auto& unocommand: maloggedUnoCommands)
    {
        aUnoCommandBuffer.append(aCommandSeperator);
        aUnoCommandBuffer.append(unocommand);
        aCommandSeperator=",";
    }
    return aUnoCommandBuffer.makeStringAndClear();
}

namespace {

OUString getCrashDirectory()
{
    OUString aCrashURL;
    rtl::Bootstrap::get("CrashDirectory", aCrashURL);
    // Need to convert to URL in case of user-defined path
    osl::FileBase::getFileURLFromSystemPath(aCrashURL, aCrashURL);

    if (aCrashURL.isEmpty()) { // Fall back to user profile
        aCrashURL = "${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE("bootstrap") ":UserInstallation}/crash/";
        rtl::Bootstrap::expandMacros(aCrashURL);
    }

    if (!aCrashURL.endsWith("/"))
        aCrashURL += "/";

    osl::Directory::create(aCrashURL);
    OUString aCrashPath;
    osl::FileBase::getSystemPathFromFileURL(aCrashURL, aCrashPath);
    return aCrashPath;
}

}

void CrashReporter::updateMinidumpLocation()
{
#if defined( UNX ) && !defined MACOSX && !defined IOS && !defined ANDROID
    OUString aURL = getCrashDirectory();
    OString aOStringUrl = OUStringToOString(aURL, RTL_TEXTENCODING_UTF8);
    google_breakpad::MinidumpDescriptor descriptor(aOStringUrl.getStr());
    mpExceptionHandler->set_minidump_descriptor(descriptor);
#elif defined _WIN32
    OUString aURL = getCrashDirectory();
    mpExceptionHandler->set_dump_path(o3tl::toW(aURL.getStr()));
#endif
}

bool CrashReporter::crashReportInfoExists()
{
    static const bool InfoExist = crashreport::readConfig(CrashReporter::getIniFileName(), nullptr);
    return InfoExist;
}

bool CrashReporter::readSendConfig(std::string& response)
{
    return crashreport::readConfig(CrashReporter::getIniFileName(), &response);
}

void CrashReporter::installExceptionHandler()
{
    if (!IsDumpEnable())
        return;
#if defined( UNX ) && !defined MACOSX && !defined IOS && !defined ANDROID
    google_breakpad::MinidumpDescriptor descriptor("/tmp");
    mpExceptionHandler = std::make_unique<google_breakpad::ExceptionHandler>(descriptor, nullptr, dumpCallback, nullptr, true, -1);
#elif defined _WIN32
    mpExceptionHandler = std::make_unique<google_breakpad::ExceptionHandler>(L".", nullptr, dumpCallback, nullptr, google_breakpad::ExceptionHandler::HANDLER_ALL);
#endif
}

void CrashReporter::removeExceptionHandler()
{
    mpExceptionHandler.reset();
}



bool CrashReporter::IsDumpEnable()
{
    auto const env = std::getenv("CRASH_DUMP_ENABLE");
    if (env != nullptr && env[0] != '\0') {
        return true;
    }
    // read configuration item 'CrashDumpEnable' -> bool on/off
    OUString sToken;
    if (rtl::Bootstrap::get("CrashDumpEnable", sToken))
    {
        return sToken.toBoolean();
    }
    return true; // default, always on
}


std::string CrashReporter::getIniFileName()
{
    OUString url = getCrashDirectory() + "dump.ini";
    OString aUrl = OUStringToOString(url, RTL_TEXTENCODING_UTF8);
    std::string aRet(aUrl.getStr());
    return aRet;
}

// Write system-specific information such as the CPU name and features.
// This may allow us to get some statistics for decisions (such as when
// deciding whether SSE2 can be made a hard-requirement for Windows).
// Breakpad provides this information poorly or not at all.
#if defined( UNX ) && !defined MACOSX && !defined IOS && !defined ANDROID
void CrashReporter::writeSystemInfo()
{
    // Get 'model name' and 'flags' from /proc/cpuinfo.
    if( std::ifstream cpuinfo( "/proc/cpuinfo" ); cpuinfo )
    {
        bool haveModel = false;
        bool haveFlags = false;
        std::regex modelRegex( "^model name[ \t]*:[ \t]*(.*)$" );
        std::regex flagsRegex( "^flags[ \t]*:[ \t]*(.*)$" );
        for( std::string line; std::getline( cpuinfo, line ); )
        {
            std::smatch match;
            if( !haveModel && std::regex_match( line, match, modelRegex ) && match.size() == 2)
            {
                addKeyValue("CPUModelName", OUString::fromUtf8( match[ 1 ].str()), AddItem);
                haveModel = true;
            }
            if( !haveFlags && std::regex_match( line, match, flagsRegex ) && match.size() == 2)
            {
                addKeyValue("CPUFlags", OUString::fromUtf8( match[ 1 ].str()), AddItem);
                haveFlags = true;
            }
            if( haveModel && haveFlags )
                break;
        }
    }
    // Get 'MemTotal' from /proc/meminfo.
    if( std::ifstream meminfo( "/proc/meminfo" ); meminfo )
    {
        std::regex memTotalRegex( "^MemTotal[ \t]*:[ \t]*(.*)$" );
        for( std::string line; std::getline( meminfo, line ); )
        {
            std::smatch match;
            if( std::regex_match( line, match, memTotalRegex ) && match.size() == 2)
            {
                addKeyValue("MemoryTotal", OUString::fromUtf8( match[ 1 ].str()), AddItem);
                break;
            }
        }
    }
}
#elif defined _WIN32
void CrashReporter::writeSystemInfo()
{
#if !defined(_ARM64_)
    // Get CPU model name and flags.
    // See https://docs.microsoft.com/en-us/cpp/intrinsics/cpuid-cpuidex
    // and https://en.wikipedia.org/wiki/CPUID .
    int cpui[ 4 ];
    __cpuid( cpui, 0x80000000 ); // Get the highest extended ID.
    unsigned int exIds = cpui[ 0 ];
    if( exIds >= 0x80000004 )
    {
        int brand[ 16 ];
        __cpuidex( brand, 0x80000002, 0 );
        __cpuidex( brand + 4, 0x80000003, 0 );
        __cpuidex( brand + 8, 0x80000004, 0 );
        brand[ 12 ] = 0;;
        addKeyValue( "CPUModelName", OUString::fromUtf8( reinterpret_cast< const char* >( brand )),
            AddItem );
    }
    __cpuid( cpui, 0 ); // Get the highest ID.
    int ids = cpui[ 0 ];
    unsigned int ecx1 = 0, edx1 = 0, ebx7 = 0, ecx7 = 0, ecx81 = 0, edx81 = 0;
    if( ids >= 0x1 )
    {
        __cpuidex( cpui, 0x1, 0 );
        ecx1 = cpui[ 2 ];
        edx1 = cpui[ 3 ];
    }
    if( ids >= 0x7 )
    {
        __cpuidex( cpui, 0x7, 0 );
        ebx7 = cpui[ 1 ];
        ecx7 = cpui[ 2 ];
    }
    if( exIds >= 0x80000001 )
    {
        __cpuidex( cpui, 0x80000001, 0 );
        ecx81 = cpui[ 2 ];
        edx81 = cpui[ 3 ];
    }
    struct FlagItem
    {
        unsigned int* reg;
        int bit;
        const char* name;
    };
    const FlagItem flagItems[] =
    {
        { &ecx1, 0, "sse3" },
        { &ecx1, 1, "pclmulqdq" },
        { &ecx1, 3, "monitor" },
        { &ecx1, 9, "ssse3" },
        { &ecx1, 12, "fma" },
        { &ecx1, 13, "cpmxch16b" },
        { &ecx1, 19, "sse41" },
        { &ecx1, 20, "sse42" },
        { &ecx1, 22, "movbe" },
        { &ecx1, 23, "popcnt" },
        { &ecx1, 25, "aes" },
        { &ecx1, 26, "xsave" },
        { &ecx1, 27, "osxsave" },
        { &ecx1, 28, "avx" },
        { &ecx1, 29, "f16c" },
        { &ecx1, 30, "rdrand" },
        { &edx1, 5, "msr" },
        { &edx1, 8, "cx8" },
        { &edx1, 11, "sep" },
        { &edx1, 15, "cmov" },
        { &edx1, 19, "clfsh" },
        { &edx1, 23, "mmx" },
        { &edx1, 24, "fxsr" },
        { &edx1, 25, "sse" },
        { &edx1, 26, "sse2" },
        { &edx1, 28, "ht" },
        { &ebx7, 0, "fsgsbase" },
        { &ebx7, 3, "bmi1" },
        { &ebx7, 4, "hle" },
        { &ebx7, 5, "avx2" },
        { &ebx7, 8, "bmi2" },
        { &ebx7, 9, "erms" },
        { &ebx7, 10, "invpcid" },
        { &ebx7, 11, "rtm" },
        { &ebx7, 16, "avx512f" },
        { &ebx7, 18, "rdseed" },
        { &ebx7, 19, "adx" },
        { &ebx7, 26, "avx512pf" },
        { &ebx7, 27, "avx512er" },
        { &ebx7, 28, "avx512cd" },
        { &ebx7, 29, "sha" },
        { &ecx7, 0, "prefetchwt1" },
        { &ecx81, 0, "lahf" },
        { &ecx81, 5, "abm" },
        { &ecx81, 6, "sse4a" },
        { &ecx81, 11, "xop" },
        { &ecx81, 21, "tbm" },
        { &edx81, 11, "syscall" },
        { &edx81, 22, "mmxext" },
        { &edx81, 27, "rdtscp" },
        { &edx81, 30, "3dnowext" },
        { &edx81, 31, "3dnow" }
    };
    OUStringBuffer flags;
    for( const FlagItem& item : flagItems )
    {
        if( *item.reg & ( 1U << item.bit ))
        {
            if( !flags.isEmpty())
                flags.append( " " );
            flags.appendAscii( item.name );
        }
    }
    if( !flags.isEmpty())
        addKeyValue( "CPUFlags", flags.makeStringAndClear(), AddItem );
#endif
    // Get total memory.
    MEMORYSTATUSEX memoryStatus;
    memoryStatus.dwLength = sizeof( memoryStatus );
    if( GlobalMemoryStatusEx( &memoryStatus ))
    {
        addKeyValue( "MemoryTotal", OUString::number( int( memoryStatus.ullTotalPhys / 1024 ))
            + " kB", AddItem );
    }
}
#else
void CrashReporter::writeSystemInfo()
{
}
#endif

#endif //HAVE_FEATURE_BREAKPAD

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */