summaryrefslogtreecommitdiff
path: root/vcl/source/app/watchdog.cxx
blob: bd8b28db89e6a47ec807901cedfd12731f6e580c (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
/* -*- 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 <watchdog.hxx>

#include <config_features.h>

#include <osl/conditn.hxx>
#include <rtl/ref.hxx>
#include <rtl/string.hxx>
#include <sal/log.hxx>
#include <comphelper/debuggerinfo.hxx>
#include <opengl/zone.hxx>
#include <skia/zone.hxx>

#include <stdlib.h>

#if defined HAVE_VALGRIND_HEADERS
#include <valgrind/memcheck.h>
#endif

namespace
{
static volatile bool gbWatchdogFiring = false;
static osl::Condition* gpWatchdogExit = nullptr;
static rtl::Reference<WatchdogThread> gxWatchdog;

template <typename Zone> struct WatchdogHelper
{
    static inline sal_uInt64 nLastEnters = 0;
    static inline int nUnchanged = 0; // how many unchanged nEnters
    static inline bool bFired = false;
    static inline bool bAbortFired = false;
    static void setLastEnters() { nLastEnters = Zone::enterCount(); }
    static void check()
    {
        if (Zone::isInZone())
        {
            const CrashWatchdogTimingsValues& aTimingValues = Zone::getCrashWatchdogTimingsValues();

            if (nLastEnters == Zone::enterCount())
                nUnchanged++;
            else
                nUnchanged = 0;
            Zone::checkDebug(nUnchanged, aTimingValues);

            // Not making progress
            if (nUnchanged >= aTimingValues.mnDisableEntries)
            {
                if (!bFired)
                {
                    gbWatchdogFiring = true;
                    SAL_WARN("vcl.watchdog",
                             OStringLiteral("Watchdog triggered: hard disable ") + Zone::name());
                    Zone::hardDisable();
                    gbWatchdogFiring = false;
                }
                bFired = true;

                // we can hang using VCL in the abort handling -> be impatient
                if (bAbortFired)
                {
                    SAL_WARN("vcl.watchdog",
                             OStringLiteral("Watchdog gave up: hard exiting ") + Zone::name());
                    _Exit(1);
                }
            }

            // Not making even more progress
            if (nUnchanged >= aTimingValues.mnAbortAfter)
            {
                if (!bAbortFired)
                {
                    SAL_WARN("vcl.watchdog",
                             OStringLiteral("Watchdog gave up: aborting ") + Zone::name());
                    gbWatchdogFiring = true;
                    std::abort();
                }
                // coverity[dead_error_line] - we might have caught SIGABRT and failed to exit yet
                bAbortFired = true;
            }
        }
        else
        {
            nUnchanged = 0;
        }
    }
};

} // namespace

WatchdogThread::WatchdogThread()
    : salhelper::Thread("Crash Watchdog")
{
}

void WatchdogThread::execute()
{
    TimeValue aQuarterSecond(0, 1000 * 1000 * 1000 * 0.25);
    do
    {
#if HAVE_FEATURE_OPENGL
        WatchdogHelper<OpenGLZone>::setLastEnters();
#endif
#if HAVE_FEATURE_SKIA
        WatchdogHelper<SkiaZone>::setLastEnters();
#endif

        gpWatchdogExit->wait(&aQuarterSecond);

#if HAVE_FEATURE_OPENGL
        WatchdogHelper<OpenGLZone>::check();
#endif
#if HAVE_FEATURE_SKIA
        WatchdogHelper<SkiaZone>::check();
#endif

    } while (!gpWatchdogExit->check());
}

void WatchdogThread::start()
{
    if (gxWatchdog != nullptr)
        return; // already running
    if (getenv("SAL_DISABLE_WATCHDOG"))
        return;
#if defined HAVE_VALGRIND_HEADERS
    if (RUNNING_ON_VALGRIND)
        return;
#endif
#if defined DBG_UTIL
    if (comphelper::isDebuggerAttached())
        return;
#endif
    gpWatchdogExit = new osl::Condition();
    gxWatchdog.set(new WatchdogThread());
    gxWatchdog->launch();
}

void WatchdogThread::stop()
{
    if (gbWatchdogFiring)
        return; // in watchdog thread

    if (gpWatchdogExit)
        gpWatchdogExit->set();

    if (gxWatchdog.is())
    {
        gxWatchdog->join();
        gxWatchdog.clear();
    }

    delete gpWatchdogExit;
    gpWatchdogExit = nullptr;
}

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