2015-04-09 20:25:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-06-14 22:01:02 +04:00
|
|
|
/* 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/. */
|
|
|
|
|
2013-09-12 18:47:37 +04:00
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
|
|
|
|
2013-06-14 22:01:02 +04:00
|
|
|
#include "IOInterposer.h"
|
2013-09-12 18:47:37 +04:00
|
|
|
|
2014-04-20 00:28:02 +04:00
|
|
|
#include "IOInterposerPrivate.h"
|
|
|
|
#include "MainThreadIOLogger.h"
|
2014-03-15 10:10:39 +04:00
|
|
|
#include "mozilla/Atomics.h"
|
2013-09-12 18:47:37 +04:00
|
|
|
#include "mozilla/Mutex.h"
|
2014-04-09 08:57:52 +04:00
|
|
|
#include "mozilla/RefPtr.h"
|
2013-09-12 18:47:37 +04:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2014-03-15 02:35:01 +04:00
|
|
|
#include "mozilla/ThreadLocal.h"
|
2015-07-31 22:11:48 +03:00
|
|
|
#include "nscore.h" // for NS_FREE_PERMANENT_DATA
|
2014-03-15 02:35:01 +04:00
|
|
|
#if !defined(XP_WIN)
|
|
|
|
#include "NSPRInterposer.h"
|
|
|
|
#endif // !defined(XP_WIN)
|
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
#include "PoisonIOInterposer.h"
|
2013-06-14 22:01:02 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
2013-09-12 18:47:37 +04:00
|
|
|
namespace {
|
|
|
|
|
2014-04-09 08:57:52 +04:00
|
|
|
/** Find if a vector contains a specific element */
|
|
|
|
template<class T>
|
2014-08-13 22:45:37 +04:00
|
|
|
bool
|
|
|
|
VectorContains(const std::vector<T>& aVector, const T& aElement)
|
2014-04-09 08:57:52 +04:00
|
|
|
{
|
2014-08-13 22:45:37 +04:00
|
|
|
return std::find(aVector.begin(), aVector.end(), aElement) != aVector.end();
|
2014-04-09 08:57:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Remove element from a vector */
|
|
|
|
template<class T>
|
2014-08-13 22:45:37 +04:00
|
|
|
void
|
|
|
|
VectorRemove(std::vector<T>& aVector, const T& aElement)
|
2014-04-09 08:57:52 +04:00
|
|
|
{
|
2014-08-13 22:45:37 +04:00
|
|
|
typename std::vector<T>::iterator newEnd =
|
|
|
|
std::remove(aVector.begin(), aVector.end(), aElement);
|
|
|
|
aVector.erase(newEnd, aVector.end());
|
2014-04-09 08:57:52 +04:00
|
|
|
}
|
|
|
|
|
2013-09-12 18:47:37 +04:00
|
|
|
/** Lists of Observers */
|
2014-04-29 21:32:03 +04:00
|
|
|
struct ObserverLists
|
2014-04-09 08:57:52 +04:00
|
|
|
{
|
2014-04-29 21:32:03 +04:00
|
|
|
private:
|
|
|
|
~ObserverLists() {}
|
|
|
|
|
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ObserverLists)
|
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
ObserverLists() {}
|
2013-09-12 18:47:37 +04:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
ObserverLists(ObserverLists const& aOther)
|
2014-04-09 08:57:52 +04:00
|
|
|
: mCreateObservers(aOther.mCreateObservers)
|
|
|
|
, mReadObservers(aOther.mReadObservers)
|
|
|
|
, mWriteObservers(aOther.mWriteObservers)
|
|
|
|
, mFSyncObservers(aOther.mFSyncObservers)
|
|
|
|
, mStatObservers(aOther.mStatObservers)
|
|
|
|
, mCloseObservers(aOther.mCloseObservers)
|
2014-04-20 00:28:02 +04:00
|
|
|
, mStageObservers(aOther.mStageObservers)
|
2013-09-12 18:47:37 +04:00
|
|
|
{
|
|
|
|
}
|
2014-04-20 00:28:02 +04:00
|
|
|
// Lists of observers for I/O events.
|
2013-09-12 18:47:37 +04:00
|
|
|
// These are implemented as vectors since they are allowed to survive gecko,
|
|
|
|
// without reporting leaks. This is necessary for the IOInterposer to be used
|
|
|
|
// for late-write checks.
|
2013-10-01 23:53:19 +04:00
|
|
|
std::vector<IOInterposeObserver*> mCreateObservers;
|
2013-09-12 18:47:37 +04:00
|
|
|
std::vector<IOInterposeObserver*> mReadObservers;
|
|
|
|
std::vector<IOInterposeObserver*> mWriteObservers;
|
|
|
|
std::vector<IOInterposeObserver*> mFSyncObservers;
|
2013-10-01 23:53:19 +04:00
|
|
|
std::vector<IOInterposeObserver*> mStatObservers;
|
|
|
|
std::vector<IOInterposeObserver*> mCloseObservers;
|
2014-04-20 00:28:02 +04:00
|
|
|
std::vector<IOInterposeObserver*> mStageObservers;
|
2013-09-12 18:47:37 +04:00
|
|
|
};
|
2013-06-14 22:01:02 +04:00
|
|
|
|
2014-04-09 08:57:52 +04:00
|
|
|
class PerThreadData
|
2013-06-14 22:01:02 +04:00
|
|
|
{
|
2014-04-09 08:57:52 +04:00
|
|
|
public:
|
2014-08-05 17:36:59 +04:00
|
|
|
explicit PerThreadData(bool aIsMainThread = false)
|
2014-04-09 08:57:52 +04:00
|
|
|
: mIsMainThread(aIsMainThread)
|
|
|
|
, mIsHandlingObservation(false)
|
|
|
|
, mCurrentGeneration(0)
|
|
|
|
{
|
2014-04-29 21:29:43 +04:00
|
|
|
MOZ_COUNT_CTOR(PerThreadData);
|
|
|
|
}
|
|
|
|
|
|
|
|
~PerThreadData()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(PerThreadData);
|
2014-04-09 08:57:52 +04:00
|
|
|
}
|
2013-06-14 22:01:02 +04:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
void CallObservers(IOInterposeObserver::Observation& aObservation)
|
2014-04-09 08:57:52 +04:00
|
|
|
{
|
|
|
|
// Prevent recursive reporting.
|
|
|
|
if (mIsHandlingObservation) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mIsHandlingObservation = true;
|
|
|
|
// Decide which list of observers to inform
|
|
|
|
std::vector<IOInterposeObserver*>* observers = nullptr;
|
|
|
|
switch (aObservation.ObservedOperation()) {
|
|
|
|
case IOInterposeObserver::OpCreateOrOpen:
|
2014-08-13 22:45:37 +04:00
|
|
|
observers = &mObserverLists->mCreateObservers;
|
2014-04-09 08:57:52 +04:00
|
|
|
break;
|
|
|
|
case IOInterposeObserver::OpRead:
|
2014-08-13 22:45:37 +04:00
|
|
|
observers = &mObserverLists->mReadObservers;
|
2014-04-09 08:57:52 +04:00
|
|
|
break;
|
|
|
|
case IOInterposeObserver::OpWrite:
|
2014-08-13 22:45:37 +04:00
|
|
|
observers = &mObserverLists->mWriteObservers;
|
2014-04-09 08:57:52 +04:00
|
|
|
break;
|
|
|
|
case IOInterposeObserver::OpFSync:
|
2014-08-13 22:45:37 +04:00
|
|
|
observers = &mObserverLists->mFSyncObservers;
|
2014-04-09 08:57:52 +04:00
|
|
|
break;
|
|
|
|
case IOInterposeObserver::OpStat:
|
2014-08-13 22:45:37 +04:00
|
|
|
observers = &mObserverLists->mStatObservers;
|
2014-04-09 08:57:52 +04:00
|
|
|
break;
|
|
|
|
case IOInterposeObserver::OpClose:
|
2014-08-13 22:45:37 +04:00
|
|
|
observers = &mObserverLists->mCloseObservers;
|
2014-04-09 08:57:52 +04:00
|
|
|
break;
|
2014-04-20 00:28:02 +04:00
|
|
|
case IOInterposeObserver::OpNextStage:
|
2014-08-13 22:45:37 +04:00
|
|
|
observers = &mObserverLists->mStageObservers;
|
2014-04-20 00:28:02 +04:00
|
|
|
break;
|
2014-08-13 22:45:37 +04:00
|
|
|
default: {
|
|
|
|
// Invalid IO operation, see documentation comment for
|
|
|
|
// IOInterposer::Report()
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
// Just ignore it in non-debug builds.
|
|
|
|
return;
|
|
|
|
}
|
2014-04-09 08:57:52 +04:00
|
|
|
}
|
|
|
|
MOZ_ASSERT(observers);
|
|
|
|
|
|
|
|
// Inform observers
|
2014-08-13 22:45:37 +04:00
|
|
|
for (auto i = observers->begin(), e = observers->end(); i != e; ++i) {
|
2014-04-09 08:57:52 +04:00
|
|
|
(*i)->Observe(aObservation);
|
|
|
|
}
|
|
|
|
mIsHandlingObservation = false;
|
|
|
|
}
|
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
inline uint32_t GetCurrentGeneration() const { return mCurrentGeneration; }
|
2014-04-09 08:57:52 +04:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
inline bool IsMainThread() const { return mIsMainThread; }
|
2014-04-09 08:57:52 +04:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
inline void SetObserverLists(uint32_t aNewGeneration,
|
|
|
|
RefPtr<ObserverLists>& aNewLists)
|
2014-04-09 08:57:52 +04:00
|
|
|
{
|
|
|
|
mCurrentGeneration = aNewGeneration;
|
|
|
|
mObserverLists = aNewLists;
|
|
|
|
}
|
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
inline void ClearObserverLists()
|
2014-04-29 21:29:43 +04:00
|
|
|
{
|
|
|
|
if (mObserverLists) {
|
|
|
|
mCurrentGeneration = 0;
|
|
|
|
mObserverLists = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-09 08:57:52 +04:00
|
|
|
private:
|
|
|
|
bool mIsMainThread;
|
|
|
|
bool mIsHandlingObservation;
|
|
|
|
uint32_t mCurrentGeneration;
|
|
|
|
RefPtr<ObserverLists> mObserverLists;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MasterList
|
2013-06-14 22:01:02 +04:00
|
|
|
{
|
2014-04-09 08:57:52 +04:00
|
|
|
public:
|
|
|
|
MasterList()
|
2014-04-20 00:28:02 +04:00
|
|
|
: mObservedOperations(IOInterposeObserver::OpNone)
|
2014-04-09 08:57:52 +04:00
|
|
|
, mIsEnabled(true)
|
|
|
|
{
|
2014-04-29 21:29:43 +04:00
|
|
|
MOZ_COUNT_CTOR(MasterList);
|
2014-04-09 08:57:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
~MasterList()
|
|
|
|
{
|
2014-04-29 21:29:43 +04:00
|
|
|
MOZ_COUNT_DTOR(MasterList);
|
2014-04-09 08:57:52 +04:00
|
|
|
}
|
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
inline void Disable() { mIsEnabled = false; }
|
2014-04-09 08:57:52 +04:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
void Register(IOInterposeObserver::Operation aOp,
|
|
|
|
IOInterposeObserver* aObserver)
|
2014-04-09 08:57:52 +04:00
|
|
|
{
|
2014-04-20 00:28:02 +04:00
|
|
|
IOInterposer::AutoLock lock(mLock);
|
2014-04-09 08:57:52 +04:00
|
|
|
|
|
|
|
ObserverLists* newLists = nullptr;
|
|
|
|
if (mObserverLists) {
|
|
|
|
newLists = new ObserverLists(*mObserverLists);
|
|
|
|
} else {
|
|
|
|
newLists = new ObserverLists();
|
|
|
|
}
|
|
|
|
// You can register to observe multiple types of observations
|
|
|
|
// but you'll never be registered twice for the same observations.
|
|
|
|
if (aOp & IOInterposeObserver::OpCreateOrOpen &&
|
|
|
|
!VectorContains(newLists->mCreateObservers, aObserver)) {
|
|
|
|
newLists->mCreateObservers.push_back(aObserver);
|
|
|
|
}
|
|
|
|
if (aOp & IOInterposeObserver::OpRead &&
|
|
|
|
!VectorContains(newLists->mReadObservers, aObserver)) {
|
|
|
|
newLists->mReadObservers.push_back(aObserver);
|
|
|
|
}
|
|
|
|
if (aOp & IOInterposeObserver::OpWrite &&
|
|
|
|
!VectorContains(newLists->mWriteObservers, aObserver)) {
|
|
|
|
newLists->mWriteObservers.push_back(aObserver);
|
|
|
|
}
|
|
|
|
if (aOp & IOInterposeObserver::OpFSync &&
|
|
|
|
!VectorContains(newLists->mFSyncObservers, aObserver)) {
|
|
|
|
newLists->mFSyncObservers.push_back(aObserver);
|
|
|
|
}
|
|
|
|
if (aOp & IOInterposeObserver::OpStat &&
|
|
|
|
!VectorContains(newLists->mStatObservers, aObserver)) {
|
|
|
|
newLists->mStatObservers.push_back(aObserver);
|
|
|
|
}
|
|
|
|
if (aOp & IOInterposeObserver::OpClose &&
|
|
|
|
!VectorContains(newLists->mCloseObservers, aObserver)) {
|
|
|
|
newLists->mCloseObservers.push_back(aObserver);
|
|
|
|
}
|
2014-04-20 00:28:02 +04:00
|
|
|
if (aOp & IOInterposeObserver::OpNextStage &&
|
|
|
|
!VectorContains(newLists->mStageObservers, aObserver)) {
|
|
|
|
newLists->mStageObservers.push_back(aObserver);
|
|
|
|
}
|
2014-04-09 08:57:52 +04:00
|
|
|
mObserverLists = newLists;
|
2014-08-13 22:45:37 +04:00
|
|
|
mObservedOperations =
|
|
|
|
(IOInterposeObserver::Operation)(mObservedOperations | aOp);
|
2014-04-09 08:57:52 +04:00
|
|
|
|
|
|
|
mCurrentGeneration++;
|
|
|
|
}
|
2013-06-14 22:01:02 +04:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
void Unregister(IOInterposeObserver::Operation aOp,
|
|
|
|
IOInterposeObserver* aObserver)
|
2014-04-09 08:57:52 +04:00
|
|
|
{
|
2014-04-20 00:28:02 +04:00
|
|
|
IOInterposer::AutoLock lock(mLock);
|
2014-04-09 08:57:52 +04:00
|
|
|
|
|
|
|
ObserverLists* newLists = nullptr;
|
|
|
|
if (mObserverLists) {
|
|
|
|
newLists = new ObserverLists(*mObserverLists);
|
|
|
|
} else {
|
|
|
|
newLists = new ObserverLists();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aOp & IOInterposeObserver::OpCreateOrOpen) {
|
|
|
|
VectorRemove(newLists->mCreateObservers, aObserver);
|
|
|
|
if (newLists->mCreateObservers.empty()) {
|
2014-08-13 22:45:37 +04:00
|
|
|
mObservedOperations =
|
|
|
|
(IOInterposeObserver::Operation)(mObservedOperations &
|
|
|
|
~IOInterposeObserver::OpCreateOrOpen);
|
2014-04-09 08:57:52 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (aOp & IOInterposeObserver::OpRead) {
|
|
|
|
VectorRemove(newLists->mReadObservers, aObserver);
|
|
|
|
if (newLists->mReadObservers.empty()) {
|
2014-08-13 22:45:37 +04:00
|
|
|
mObservedOperations =
|
|
|
|
(IOInterposeObserver::Operation)(mObservedOperations &
|
|
|
|
~IOInterposeObserver::OpRead);
|
2014-04-09 08:57:52 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (aOp & IOInterposeObserver::OpWrite) {
|
|
|
|
VectorRemove(newLists->mWriteObservers, aObserver);
|
|
|
|
if (newLists->mWriteObservers.empty()) {
|
2014-08-13 22:45:37 +04:00
|
|
|
mObservedOperations =
|
|
|
|
(IOInterposeObserver::Operation)(mObservedOperations &
|
|
|
|
~IOInterposeObserver::OpWrite);
|
2014-04-09 08:57:52 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (aOp & IOInterposeObserver::OpFSync) {
|
|
|
|
VectorRemove(newLists->mFSyncObservers, aObserver);
|
|
|
|
if (newLists->mFSyncObservers.empty()) {
|
2014-08-13 22:45:37 +04:00
|
|
|
mObservedOperations =
|
|
|
|
(IOInterposeObserver::Operation)(mObservedOperations &
|
|
|
|
~IOInterposeObserver::OpFSync);
|
2014-04-09 08:57:52 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (aOp & IOInterposeObserver::OpStat) {
|
|
|
|
VectorRemove(newLists->mStatObservers, aObserver);
|
|
|
|
if (newLists->mStatObservers.empty()) {
|
2014-08-13 22:45:37 +04:00
|
|
|
mObservedOperations =
|
|
|
|
(IOInterposeObserver::Operation)(mObservedOperations &
|
|
|
|
~IOInterposeObserver::OpStat);
|
2014-04-09 08:57:52 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (aOp & IOInterposeObserver::OpClose) {
|
|
|
|
VectorRemove(newLists->mCloseObservers, aObserver);
|
|
|
|
if (newLists->mCloseObservers.empty()) {
|
2014-08-13 22:45:37 +04:00
|
|
|
mObservedOperations =
|
|
|
|
(IOInterposeObserver::Operation)(mObservedOperations &
|
|
|
|
~IOInterposeObserver::OpClose);
|
2014-04-09 08:57:52 +04:00
|
|
|
}
|
|
|
|
}
|
2014-04-20 00:28:02 +04:00
|
|
|
if (aOp & IOInterposeObserver::OpNextStage) {
|
|
|
|
VectorRemove(newLists->mStageObservers, aObserver);
|
|
|
|
if (newLists->mStageObservers.empty()) {
|
2014-08-13 22:45:37 +04:00
|
|
|
mObservedOperations =
|
|
|
|
(IOInterposeObserver::Operation)(mObservedOperations &
|
|
|
|
~IOInterposeObserver::OpNextStage);
|
2014-04-20 00:28:02 +04:00
|
|
|
}
|
|
|
|
}
|
2014-04-09 08:57:52 +04:00
|
|
|
mObserverLists = newLists;
|
|
|
|
mCurrentGeneration++;
|
|
|
|
}
|
2014-04-29 21:29:43 +04:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
void Update(PerThreadData& aPtd)
|
2014-04-09 08:57:52 +04:00
|
|
|
{
|
|
|
|
if (mCurrentGeneration == aPtd.GetCurrentGeneration()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// If the generation counts don't match then we need to update the current
|
|
|
|
// thread's observer list with the new master list.
|
2014-04-20 00:28:02 +04:00
|
|
|
IOInterposer::AutoLock lock(mLock);
|
2014-04-09 08:57:52 +04:00
|
|
|
aPtd.SetObserverLists(mCurrentGeneration, mObserverLists);
|
|
|
|
}
|
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
inline bool IsObservedOperation(IOInterposeObserver::Operation aOp)
|
2014-04-09 08:57:52 +04:00
|
|
|
{
|
|
|
|
// The quick reader may observe that no locks are being employed here,
|
|
|
|
// hence the result of the operations is truly undefined. However, most
|
|
|
|
// computers will usually return either true or false, which is good enough.
|
|
|
|
// It is not a problem if we occasionally report more or less IO than is
|
|
|
|
// actually occurring.
|
|
|
|
return mIsEnabled && !!(mObservedOperations & aOp);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<ObserverLists> mObserverLists;
|
|
|
|
// Note, we cannot use mozilla::Mutex here as the ObserverLists may be leaked
|
|
|
|
// (We want to monitor IO during shutdown). Furthermore, as we may have to
|
|
|
|
// unregister observers during shutdown an OffTheBooksMutex is not an option
|
|
|
|
// either, as its base calls into sDeadlockDetector which may be nullptr
|
|
|
|
// during shutdown.
|
2014-04-20 00:28:02 +04:00
|
|
|
IOInterposer::Mutex mLock;
|
2014-04-09 08:57:52 +04:00
|
|
|
// Flags tracking which operations are being observed
|
|
|
|
IOInterposeObserver::Operation mObservedOperations;
|
|
|
|
// Used for quickly disabling everything by IOInterposer::Disable()
|
|
|
|
Atomic<bool> mIsEnabled;
|
|
|
|
// Used to inform threads that the master observer list has changed
|
|
|
|
Atomic<uint32_t> mCurrentGeneration;
|
|
|
|
};
|
|
|
|
|
2014-04-20 00:28:02 +04:00
|
|
|
// Special observation used by IOInterposer::EnteringNextStage()
|
|
|
|
class NextStageObservation : public IOInterposeObserver::Observation
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NextStageObservation()
|
|
|
|
: IOInterposeObserver::Observation(IOInterposeObserver::OpNextStage,
|
|
|
|
"IOInterposer", false)
|
|
|
|
{
|
|
|
|
mStart = TimeStamp::Now();
|
2014-08-19 02:42:14 +04:00
|
|
|
mEnd = mStart;
|
2014-04-20 00:28:02 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-09 08:57:52 +04:00
|
|
|
// List of observers registered
|
|
|
|
static StaticAutoPtr<MasterList> sMasterList;
|
|
|
|
static ThreadLocal<PerThreadData*> sThreadLocalData;
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2013-09-12 18:47:37 +04:00
|
|
|
|
2014-02-01 07:14:03 +04:00
|
|
|
IOInterposeObserver::Observation::Observation(Operation aOperation,
|
|
|
|
const char* aReference,
|
|
|
|
bool aShouldReport)
|
|
|
|
: mOperation(aOperation)
|
|
|
|
, mReference(aReference)
|
|
|
|
, mShouldReport(IOInterposer::IsObservedOperation(aOperation) &&
|
|
|
|
aShouldReport)
|
|
|
|
{
|
|
|
|
if (mShouldReport) {
|
|
|
|
mStart = TimeStamp::Now();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IOInterposeObserver::Observation::Observation(Operation aOperation,
|
|
|
|
const TimeStamp& aStart,
|
|
|
|
const TimeStamp& aEnd,
|
|
|
|
const char* aReference)
|
|
|
|
: mOperation(aOperation)
|
|
|
|
, mStart(aStart)
|
|
|
|
, mEnd(aEnd)
|
|
|
|
, mReference(aReference)
|
|
|
|
, mShouldReport(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-20 00:28:02 +04:00
|
|
|
const char*
|
|
|
|
IOInterposeObserver::Observation::ObservedOperationString() const
|
|
|
|
{
|
2014-08-13 22:45:37 +04:00
|
|
|
switch (mOperation) {
|
2014-04-20 00:28:02 +04:00
|
|
|
case OpCreateOrOpen:
|
|
|
|
return "create/open";
|
|
|
|
case OpRead:
|
|
|
|
return "read";
|
|
|
|
case OpWrite:
|
|
|
|
return "write";
|
|
|
|
case OpFSync:
|
|
|
|
return "fsync";
|
|
|
|
case OpStat:
|
|
|
|
return "stat";
|
|
|
|
case OpClose:
|
|
|
|
return "close";
|
|
|
|
case OpNextStage:
|
|
|
|
return "NextStage";
|
|
|
|
default:
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-01 07:14:03 +04:00
|
|
|
void
|
|
|
|
IOInterposeObserver::Observation::Report()
|
|
|
|
{
|
|
|
|
if (mShouldReport) {
|
|
|
|
mEnd = TimeStamp::Now();
|
|
|
|
IOInterposer::Report(*this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-20 00:28:02 +04:00
|
|
|
bool
|
2014-04-09 08:57:52 +04:00
|
|
|
IOInterposer::Init()
|
2013-06-14 22:01:02 +04:00
|
|
|
{
|
2013-09-12 18:47:37 +04:00
|
|
|
// Don't initialize twice...
|
2014-04-09 08:57:52 +04:00
|
|
|
if (sMasterList) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!sThreadLocalData.init()) {
|
|
|
|
return false;
|
2013-06-14 22:01:02 +04:00
|
|
|
}
|
2014-04-09 08:57:52 +04:00
|
|
|
bool isMainThread = true;
|
|
|
|
RegisterCurrentThread(isMainThread);
|
|
|
|
sMasterList = new MasterList();
|
|
|
|
|
2014-04-20 00:28:02 +04:00
|
|
|
MainThreadIOLogger::Init();
|
|
|
|
|
2014-03-15 02:35:01 +04:00
|
|
|
// Now we initialize the various interposers depending on platform
|
|
|
|
InitPoisonIOInterposer();
|
|
|
|
// We don't hook NSPR on Windows because PoisonIOInterposer captures a
|
|
|
|
// superset of the former's events.
|
|
|
|
#if !defined(XP_WIN)
|
|
|
|
InitNSPRIOInterposing();
|
|
|
|
#endif
|
2014-04-09 08:57:52 +04:00
|
|
|
return true;
|
2014-03-15 02:35:01 +04:00
|
|
|
}
|
|
|
|
|
2014-04-20 00:28:02 +04:00
|
|
|
bool
|
2014-03-15 02:35:01 +04:00
|
|
|
IOInterposeObserver::IsMainThread()
|
|
|
|
{
|
2014-04-09 08:57:52 +04:00
|
|
|
if (!sThreadLocalData.initialized()) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-13 22:45:37 +04:00
|
|
|
PerThreadData* ptd = sThreadLocalData.get();
|
2014-04-09 08:57:52 +04:00
|
|
|
if (!ptd) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return ptd->IsMainThread();
|
2013-06-14 22:01:02 +04:00
|
|
|
}
|
|
|
|
|
2014-04-20 00:28:02 +04:00
|
|
|
void
|
2014-04-09 08:57:52 +04:00
|
|
|
IOInterposer::Clear()
|
2013-06-14 22:01:02 +04:00
|
|
|
{
|
2015-07-31 22:11:48 +03:00
|
|
|
/* Clear() is a no-op on release builds so that we may continue to trap I/O
|
|
|
|
until process termination. In leak-checking builds, we need to shut down
|
|
|
|
IOInterposer so that all references are properly released. */
|
|
|
|
#ifdef NS_FREE_PERMANENT_DATA
|
2014-04-29 21:29:43 +04:00
|
|
|
UnregisterCurrentThread();
|
2014-04-09 08:57:52 +04:00
|
|
|
sMasterList = nullptr;
|
2014-04-29 21:29:43 +04:00
|
|
|
#endif
|
2013-06-14 22:01:02 +04:00
|
|
|
}
|
|
|
|
|
2014-04-20 00:28:02 +04:00
|
|
|
void
|
2014-03-15 10:10:39 +04:00
|
|
|
IOInterposer::Disable()
|
|
|
|
{
|
2014-04-09 08:57:52 +04:00
|
|
|
if (!sMasterList) {
|
2014-03-15 10:10:39 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-04-09 08:57:52 +04:00
|
|
|
sMasterList->Disable();
|
2014-03-15 10:10:39 +04:00
|
|
|
}
|
|
|
|
|
2014-04-20 00:28:02 +04:00
|
|
|
void
|
2014-04-09 08:57:52 +04:00
|
|
|
IOInterposer::Report(IOInterposeObserver::Observation& aObservation)
|
2013-06-14 22:01:02 +04:00
|
|
|
{
|
2014-04-09 08:57:52 +04:00
|
|
|
PerThreadData* ptd = sThreadLocalData.get();
|
|
|
|
if (!ptd) {
|
|
|
|
// In this case the current thread is not registered with IOInterposer.
|
|
|
|
// Alternatively we could take the slow path and just lock everything if
|
|
|
|
// we're not registered. That could potentially perform poorly, though.
|
2013-09-12 18:47:37 +04:00
|
|
|
return;
|
2013-06-14 22:01:02 +04:00
|
|
|
}
|
2014-04-29 21:29:43 +04:00
|
|
|
|
|
|
|
if (!sMasterList) {
|
|
|
|
// If there is no longer a master list then we should clear the local one.
|
|
|
|
ptd->ClearObserverLists();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-09 08:57:52 +04:00
|
|
|
sMasterList->Update(*ptd);
|
2013-06-14 22:01:02 +04:00
|
|
|
|
2014-04-09 08:57:52 +04:00
|
|
|
// Don't try to report if there's nobody listening.
|
|
|
|
if (!IOInterposer::IsObservedOperation(aObservation.ObservedOperation())) {
|
|
|
|
return;
|
2013-09-12 18:47:37 +04:00
|
|
|
}
|
|
|
|
|
2014-04-09 08:57:52 +04:00
|
|
|
ptd->CallObservers(aObservation);
|
2013-09-12 18:47:37 +04:00
|
|
|
}
|
|
|
|
|
2014-04-20 00:28:02 +04:00
|
|
|
bool
|
2014-03-15 10:10:39 +04:00
|
|
|
IOInterposer::IsObservedOperation(IOInterposeObserver::Operation aOp)
|
|
|
|
{
|
2014-04-09 08:57:52 +04:00
|
|
|
return sMasterList && sMasterList->IsObservedOperation(aOp);
|
2014-03-15 10:10:39 +04:00
|
|
|
}
|
|
|
|
|
2014-04-20 00:28:02 +04:00
|
|
|
void
|
2014-04-09 08:57:52 +04:00
|
|
|
IOInterposer::Register(IOInterposeObserver::Operation aOp,
|
|
|
|
IOInterposeObserver* aObserver)
|
2013-09-12 18:47:37 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aObserver);
|
2014-04-09 08:57:52 +04:00
|
|
|
if (!sMasterList || !aObserver) {
|
2013-09-12 18:47:37 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-09 08:57:52 +04:00
|
|
|
sMasterList->Register(aOp, aObserver);
|
2013-06-14 22:01:02 +04:00
|
|
|
}
|
|
|
|
|
2014-04-20 00:28:02 +04:00
|
|
|
void
|
2014-04-09 08:57:52 +04:00
|
|
|
IOInterposer::Unregister(IOInterposeObserver::Operation aOp,
|
|
|
|
IOInterposeObserver* aObserver)
|
2013-09-12 18:47:37 +04:00
|
|
|
{
|
2014-04-09 08:57:52 +04:00
|
|
|
if (!sMasterList) {
|
2013-09-12 18:47:37 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-09 08:57:52 +04:00
|
|
|
sMasterList->Unregister(aOp, aObserver);
|
|
|
|
}
|
2013-09-12 18:47:37 +04:00
|
|
|
|
2014-04-20 00:28:02 +04:00
|
|
|
void
|
2014-04-09 08:57:52 +04:00
|
|
|
IOInterposer::RegisterCurrentThread(bool aIsMainThread)
|
|
|
|
{
|
|
|
|
if (!sThreadLocalData.initialized()) {
|
|
|
|
return;
|
2014-01-21 03:02:51 +04:00
|
|
|
}
|
2014-04-09 08:57:52 +04:00
|
|
|
MOZ_ASSERT(!sThreadLocalData.get());
|
|
|
|
PerThreadData* curThreadData = new PerThreadData(aIsMainThread);
|
|
|
|
sThreadLocalData.set(curThreadData);
|
2013-09-12 18:47:37 +04:00
|
|
|
}
|
2014-03-15 02:35:01 +04:00
|
|
|
|
2014-04-20 00:28:02 +04:00
|
|
|
void
|
2014-04-09 08:57:52 +04:00
|
|
|
IOInterposer::UnregisterCurrentThread()
|
2014-03-15 02:35:01 +04:00
|
|
|
{
|
2014-04-09 08:57:52 +04:00
|
|
|
if (!sThreadLocalData.initialized()) {
|
2014-03-15 02:35:01 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-04-09 08:57:52 +04:00
|
|
|
PerThreadData* curThreadData = sThreadLocalData.get();
|
|
|
|
MOZ_ASSERT(curThreadData);
|
|
|
|
sThreadLocalData.set(nullptr);
|
|
|
|
delete curThreadData;
|
2014-03-15 02:35:01 +04:00
|
|
|
}
|
|
|
|
|
2014-04-20 00:28:02 +04:00
|
|
|
void
|
|
|
|
IOInterposer::EnteringNextStage()
|
|
|
|
{
|
|
|
|
if (!sMasterList) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NextStageObservation observation;
|
|
|
|
Report(observation);
|
|
|
|
}
|
|
|
|
|