forked from GoogleCloudPlatform/cloud-debug-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjvm_breakpoints_manager.h
More file actions
167 lines (131 loc) · 5.6 KB
/
Copy pathjvm_breakpoints_manager.h
File metadata and controls
167 lines (131 loc) · 5.6 KB
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
/**
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DEVTOOLS_CDBG_DEBUGLETS_JAVA_JVM_BREAKPOINTS_MANAGER_H_
#define DEVTOOLS_CDBG_DEBUGLETS_JAVA_JVM_BREAKPOINTS_MANAGER_H_
#include <functional>
#include <map>
#include <memory>
#include <set>
#include <vector>
#include "leaky_bucket.h"
#include "breakpoints_manager.h"
#include "canary_control.h"
#include "class_indexer.h"
#include "common.h"
#include "mutex.h"
namespace devtools {
namespace cdbg {
struct JvmEvaluators;
class FormatQueue;
// Manages list of active breakpoints and processes breakpoint hit events.
// This class is thread safe.
class JvmBreakpointsManager : public BreakpointsManager {
public:
// "evaluators" and "format_queue" not owned by this class and must outlive
// this class.
JvmBreakpointsManager(
std::function<std::shared_ptr<Breakpoint>(
BreakpointsManager*,
std::unique_ptr<BreakpointModel>)> breakpoint_factory,
JvmEvaluators* evaluators,
FormatQueue* format_queue,
CanaryControl* canary_control);
~JvmBreakpointsManager() override;
void Cleanup() override;
void SetActiveBreakpointsList(
std::vector<std::unique_ptr<BreakpointModel>> breakpoints) override;
void JvmtiOnCompiledMethodUnload(jmethodID method) override;
void JvmtiOnBreakpoint(
jthread thread,
jmethodID method,
jlocation location) override;
bool SetJvmtiBreakpoint(
jmethodID method,
jlocation location,
std::shared_ptr<Breakpoint> breakpoint) override;
void ClearJvmtiBreakpoint(
jmethodID method,
jlocation location,
std::shared_ptr<Breakpoint> breakpoint) override;
// "breakpoint_id" is not reference because it is a string that might get
// deleted when breakpoint gets completed.
void CompleteBreakpoint(std::string breakpoint_id) override;
LeakyBucket* GetGlobalConditionCostLimiter() override {
return global_condition_cost_limiter_.get();
}
LeakyBucket* GetGlobalDynamicLogLimiter() override {
return global_dynamic_log_limiter_.get();
}
LeakyBucket* GetGlobalDynamicLogBytesLimiter() override {
return global_dynamic_log_bytes_limiter_.get();
}
private:
// Copies current list of active breakpoints (under "mu_data_" lock) into
// a temporary list. The motivation to copy is to avoid lock while iterating
// through "active_breakpoints_". All calls to "Breakpoint" have to be made
// without lock. Otherwise we will get deadlock since "Breakpoint" may cause
// other JVMTI callbacks to happen.
std::vector<std::shared_ptr<Breakpoint>> GetActiveBreakpoints();
// Callback invoked when JVM initialized (aka prepared) a Java class.
void OnClassPrepared(const std::string& type_name,
const std::string& class_signature);
private:
// Functor to create new instances of "Breakpoint".
const std::function<std::shared_ptr<Breakpoint>(
BreakpointsManager*,
std::unique_ptr<BreakpointModel>)> breakpoint_factory_;
// Bundles all the evaluation classes together. Not owned by this class.
JvmEvaluators* const evaluators_;
// Breakpoint hit results that wait to be reported to the hub. Not owned by
// this class.
FormatQueue* const format_queue_;
// Optional manager of canary breakpoints.
CanaryControl* const canary_control_;
// Registration of a callbacks when a class has been loaded.
ClassIndexer::OnClassPreparedEvent::Cookie on_class_prepared_cookie_;
// Locks access to all breakpoint related data structures.
absl::Mutex mu_data_;
// Serializes calls to "SetActiveBreakpointsList" so that two simultaneous
// calls don't intermingle.
absl::Mutex mu_set_active_breakpoints_list_;
// List of currently active breakpoints (keyed by breakpoint ID).
std::map<std::string, std::shared_ptr<Breakpoint>> active_breakpoints_;
// List of recently completed breakpoint IDs that were removed from
// "active_breakpoints_" and should not go back even if listed
// in "SetActiveBreakpointsList" in case of a race condition between
// reporting a breakpoint hit and receiving list of active breakpoints
// from the server. Entries are removed from the set when hub stops listing
// the breakpoint as active.
std::set<std::string> completed_breakpoints_;
// Reverse map of breakpoints. "method_map_" allows quick lookup of
// breakpoint given code location (useful on breakpoint hit) and lookup
// of all breakpoints in a certain method (to support method unload).
std::map<
jmethodID,
std::vector<
std::pair<jlocation,
std::shared_ptr<Breakpoint>>>> method_map_;
// Global limit of the cost of condition checks.
const std::unique_ptr<LeakyBucket> global_condition_cost_limiter_;
// Global limit on total number of dynamic logs.
const std::unique_ptr<LeakyBucket> global_dynamic_log_limiter_;
// Global limit on total number of dynamic log bytes.
const std::unique_ptr<LeakyBucket> global_dynamic_log_bytes_limiter_;
DISALLOW_COPY_AND_ASSIGN(JvmBreakpointsManager);
};
} // namespace cdbg
} // namespace devtools
#endif // DEVTOOLS_CDBG_DEBUGLETS_JAVA_JVM_BREAKPOINTS_MANAGER_H_