Skip to content
This repository was archived by the owner on Feb 29, 2024. It is now read-only.

Commit c8bd2c3

Browse files
vladlfVlad Lifliand
authored andcommitted
Don't allow breakpoints in Java debugger worker thread.
Debugging the debugger may cause deadlock. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=111432504
1 parent 5d0bd3d commit c8bd2c3

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

src/agent/jvmti_agent.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,12 @@ void JvmtiAgent::JvmtiOnBreakpoint(
247247
jthread thread,
248248
jmethodID method,
249249
jlocation location) {
250+
// Ignore breakpoint events from debugger worker threads. Debugging
251+
// the debugger may cause deadlock.
252+
if (JvmtiAgentThread::IsInAgentThread()) {
253+
return;
254+
}
255+
250256
std::shared_ptr<Debugger> debugger = debugger_;
251257

252258
if (debugger != nullptr) {

src/agent/jvmti_agent_thread.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
namespace devtools {
2222
namespace cdbg {
2323

24+
// Thread local variable indicating whether the current thread is JVMTI
25+
// agent thread created by "JvmtiAgentThread".
26+
static __thread bool g_is_agent_thread = false;
27+
28+
2429
JvmtiAgentThread::JvmtiAgentThread() {
2530
}
2631

@@ -85,6 +90,9 @@ bool JvmtiAgentThread::StartAgentThread(
8590
[] (jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
8691
set_thread_jni(jni);
8792

93+
DCHECK(!g_is_agent_thread);
94+
g_is_agent_thread = true;
95+
8896
auto* agent_arg = reinterpret_cast<
8997
std::pair<string, std::function<bool()>>*>(arg);
9098

@@ -94,6 +102,8 @@ bool JvmtiAgentThread::StartAgentThread(
94102

95103
LOG(INFO) << "Agent thread exited: " << agent_arg->first;
96104

105+
g_is_agent_thread = false;
106+
97107
delete agent_arg;
98108
},
99109
agent_arg,
@@ -111,6 +121,10 @@ bool JvmtiAgentThread::StartAgentThread(
111121
}
112122

113123

124+
bool JvmtiAgentThread::IsInAgentThread() {
125+
return g_is_agent_thread;
126+
}
127+
114128
} // namespace cdbg
115129
} // namespace devtools
116130

src/agent/jvmti_agent_thread.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ class JvmtiAgentThread : public AgentThread {
4040

4141
void Sleep(int ms) override;
4242

43+
// Returns true if the current thread is JVMTI agent thread created by
44+
// this class.
45+
static bool IsInAgentThread();
46+
4347
private:
4448
// Creates the thread objects and starts the agent thread.
4549
bool StartAgentThread(

0 commit comments

Comments
 (0)