Skip to content

Commit c649637

Browse files
committed
mutex debugging routines: LocksHeld() and AssertLockHeld()
1 parent 207cfbf commit c649637

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

src/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2215,6 +2215,8 @@ void PushGetBlocks(CNode* pnode, CBlockIndex* pindexBegin, uint256 hashEnd)
22152215

22162216
bool ProcessBlock(CValidationState &state, CNode* pfrom, CBlock* pblock, CDiskBlockPos *dbp)
22172217
{
2218+
AssertLockHeld("cs_main");
2219+
22182220
// Check for duplicate
22192221
uint256 hash = pblock->GetHash();
22202222
if (mapBlockIndex.count(hash))

src/sync.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ struct CLockLocation
4242
return mutexName+" "+sourceFile+":"+itostr(sourceLine);
4343
}
4444

45+
std::string MutexName() const { return mutexName; }
46+
4547
private:
4648
std::string mutexName;
4749
std::string sourceFile;
@@ -126,4 +128,20 @@ void LeaveCritical()
126128
pop_lock();
127129
}
128130

131+
std::string LocksHeld()
132+
{
133+
std::string result;
134+
BOOST_FOREACH(const PAIRTYPE(void*, CLockLocation)&i, *lockstack)
135+
result += i.second.ToString() + std::string("\n");
136+
return result;
137+
}
138+
139+
void AssertLockHeld(std::string strName)
140+
{
141+
BOOST_FOREACH(const PAIRTYPE(void*, CLockLocation)&i, *lockstack)
142+
if (i.second.MutexName() == strName) return;
143+
LogPrintf("Lock %s not held; locks held:\n%s", strName.c_str(), LocksHeld().c_str());
144+
assert(0);
145+
}
146+
129147
#endif /* DEBUG_LOCKORDER */

src/sync.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,12 @@ typedef AnnotatedMixin<boost::mutex> CWaitableCriticalSection;
8787
#ifdef DEBUG_LOCKORDER
8888
void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false);
8989
void LeaveCritical();
90+
std::string LocksHeld();
91+
void AssertLockHeld(std::string strName);
9092
#else
9193
void static inline EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false) {}
9294
void static inline LeaveCritical() {}
95+
void static inline AssertLockHeld(std::string) {}
9396
#endif
9497

9598
#ifdef DEBUG_LOCKCONTENTION

0 commit comments

Comments
 (0)