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

Commit 10ef556

Browse files
jasonborgcopybara-github
authored andcommitted
Pass the blocklist source debuggee labels up to the Java code
PiperOrigin-RevId: 371746388 Change-Id: I70181d08cbd46d21268793c3bd5c0de28bb14047
1 parent b76bd59 commit 10ef556

File tree

9 files changed

+60
-25
lines changed

9 files changed

+60
-25
lines changed

src/agent/bridge.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <memory>
2121

2222
#include "common.h"
23+
#include "debuggee_labels.h"
2324
#include "model.h"
2425

2526
namespace devtools {
@@ -52,7 +53,8 @@ class Bridge {
5253
// Registers the debuggee with the controller. Returns true if the request
5354
// was successful. When registration succeeds "is_enabled" will usually be
5455
// set to true (unless the Hub remotely disables the debuglet).
55-
virtual bool RegisterDebuggee(bool* is_enabled) = 0;
56+
virtual bool RegisterDebuggee(bool* is_enabled,
57+
const DebuggeeLabels& debugee_labels) = 0;
5658

5759
// Queries for the list of currently active breakpoints. Returns false if the
5860
// network call failed.

src/agent/debuggee_labels.cc

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,31 @@ constexpr char DebuggeeLabels::kBlocklistSourceDeprecatedFile[];
88
constexpr char DebuggeeLabels::kBlocklistSourceFile[];
99
constexpr char DebuggeeLabels::kBlocklistSourceNone[];
1010

11-
void DebuggeeLabels::Set(const std::string& /* name */,
12-
const std::string& /* value */) {
13-
// TODO: Implement in follow on CL.
11+
void DebuggeeLabels::Set(const std::string& name, const std::string& value) {
12+
labels_[name] = value;
1413
}
1514

15+
JniLocalRef DebuggeeLabels::Get() const {
16+
ExceptionOr<JniLocalRef> jni_labels = jniproxy::HashMap()->NewObject();
17+
18+
if (jni_labels.HasException()) {
19+
// This means an error occurred trying to allocate the HashMap in the JVM.
20+
return JniLocalRef();
21+
}
22+
23+
for (const auto& label : labels_) {
24+
auto rc = jniproxy::HashMap()->put(jni_labels.GetData().get(),
25+
JniToJavaString(label.first).get(),
26+
JniToJavaString(label.second).get());
27+
28+
if (rc.HasException()) {
29+
return JniLocalRef();
30+
}
31+
}
32+
33+
return jni_labels.Release(ExceptionAction::IGNORE);
34+
}
35+
36+
1637
} // namespace cdbg
1738
} // namespace devtools

src/agent/debuggee_labels.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ class DebuggeeLabels {
3131
static constexpr char kBlocklistSourceNone[] = "none";
3232

3333
void Set(const std::string& name, const std::string& value);
34+
35+
JniLocalRef Get() const;
36+
37+
private:
38+
std::map<std::string, std::string> labels_;
3439
};
3540

3641
} // namespace cdbg

src/agent/jni_bridge.cc

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "jni_proxy_classloader.h"
2020
#include "jni_proxy_hubclient.h"
2121
#include "jni_proxy_hubclient_listactivebreakpointsresult.h"
22-
#include "jni_proxy_ju_hashmap.h"
2322

2423
namespace devtools {
2524
namespace cdbg {
@@ -67,19 +66,19 @@ void JniBridge::EnqueueBreakpointUpdate(
6766
}
6867
}
6968

70-
71-
bool JniBridge::RegisterDebuggee(bool* is_enabled) {
69+
bool JniBridge::RegisterDebuggee(bool* is_enabled,
70+
const DebuggeeLabels& debuggee_labels) {
7271
*is_enabled = false;
7372

74-
// TODO Follow on CL will be getting actual labels wired in.
75-
ExceptionOr<JniLocalRef> labels = jniproxy::HashMap()->NewObject();
76-
if (labels.HasException()) {
77-
// This means an error occurred trying to allocate the HashMap in the JVM.
78-
return false; // No registration occurred
73+
JniLocalRef java_labels = debuggee_labels.Get();
74+
75+
if (!java_labels) {
76+
LOG(ERROR) << "Failed to create the Debuggee labels Java map.";
77+
return false;
7978
}
8079

8180
auto rc = jniproxy::HubClient()->registerDebuggee(jni_hub_.get(),
82-
labels.GetData().get());
81+
java_labels.get());
8382

8483
if (rc.HasException()) {
8584
// The Java code logs important errors.
@@ -91,7 +90,6 @@ bool JniBridge::RegisterDebuggee(bool* is_enabled) {
9190
return true;
9291
}
9392

94-
9593
Bridge::HangingGetResult JniBridge::ListActiveBreakpoints(
9694
std::vector<std::unique_ptr<BreakpointModel>>* breakpoints) {
9795
breakpoints->clear();

src/agent/jni_bridge.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "nullable.h"
2424
#include "bridge.h"
2525
#include "common.h"
26+
#include "debuggee_labels.h"
2627
#include "jni_utils.h"
2728
#include "mutex.h"
2829
#include "transmit_queue.h"
@@ -54,7 +55,8 @@ class JniBridge : public Bridge {
5455

5556
void Shutdown() override;
5657

57-
bool RegisterDebuggee(bool* is_enabled) override;
58+
bool RegisterDebuggee(bool* is_enabled,
59+
const DebuggeeLabels& debuggee_labels) override;
5860

5961
HangingGetResult ListActiveBreakpoints(
6062
std::vector<std::unique_ptr<BreakpointModel>>* breakpoints) override;

src/agent/jvmti_agent.cc

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ void JvmtiAgent::EnableJvmtiDebuggerNotifications(jvmtiEventMode mode) {
332332
}
333333

334334

335-
bool JvmtiAgent::OnWorkerReady() {
335+
bool JvmtiAgent::OnWorkerReady(DebuggeeLabels* debuggee_labels) {
336336
// Connect to Java internals implementation.
337337
{
338338
if (!internals_->LoadInternals()) {
@@ -381,13 +381,9 @@ bool JvmtiAgent::OnWorkerReady() {
381381
return false;
382382
}
383383

384-
// TODO The labels will be wired up and passed out in a follow on
385-
// CL.
386-
DebuggeeLabels debuggee_labels;
387-
388384
// Load data visibility configuration.
389385
data_visibility_policy_ =
390-
data_visibility_policy_fn_(internals_, &debuggee_labels);
386+
data_visibility_policy_fn_(internals_, debuggee_labels);
391387

392388
return true;
393389
}

src/agent/jvmti_agent.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class JvmtiAgent : public Worker::Provider {
121121
// Implementation of Worker::Provider interface
122122
//
123123

124-
bool OnWorkerReady() override;
124+
bool OnWorkerReady(DebuggeeLabels* debuggee_labels) override;
125125

126126
void OnIdle() override;
127127

src/agent/worker.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ void Worker::MainThreadProc() {
121121
// application startup time.
122122

123123
// Deferred initialization of the agent.
124-
if (!provider_->OnWorkerReady()) {
124+
if (!provider_->OnWorkerReady(&debuggee_labels_)) {
125125
LOG(ERROR) << "Agent initialization failed: "
126126
"debugger thread can't continue.";
127127
return; // Signal to stop the main debugger thread.
@@ -215,7 +215,7 @@ void Worker::TransmissionThreadProc() {
215215

216216
void Worker::RegisterDebuggee() {
217217
bool new_is_enabled = false;
218-
is_registered_ = bridge_->RegisterDebuggee(&new_is_enabled);
218+
is_registered_ = bridge_->RegisterDebuggee(&new_is_enabled, debuggee_labels_);
219219

220220
if (is_registered_) {
221221
provider_->EnableDebugger(new_is_enabled);

src/agent/worker.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "auto_reset_event.h"
2525
#include "canary_control.h"
2626
#include "common.h"
27+
#include "debuggee_labels.h"
2728
#include "format_queue.h"
2829
#include "model.h"
2930
#include "stopwatch.h"
@@ -50,7 +51,7 @@ class Worker {
5051
// callbacks, actions done from this function don't impact the start up
5152
// time of the application. If this function returns false, "Worker" will
5253
// stop and the debugger will not be functioning.
53-
virtual bool OnWorkerReady() = 0;
54+
virtual bool OnWorkerReady(DebuggeeLabels* debuggee_labels) = 0;
5455

5556
// Called periodically by the worker thread to give opportunity to the
5657
// agent to perform routine tasks. Examples: flushing logs, garbage
@@ -148,6 +149,16 @@ class Worker {
148149
// Result of last call to "RegisterDebuggee".
149150
bool is_registered_ { false };
150151

152+
// Debuggee labels gathered from the native code to be included in the set of
153+
// labels for the Debuggee in the RegisterDebuggee call.
154+
//
155+
// NOTE: Once the label are gathered before the first call to
156+
// RegisterDebuggee, we must be sure not to update it again, the same set of
157+
// labels should be used in every subsequent call since the labels are used in
158+
// the debuggee ID generation, so we don't want to cause duplicate IDs for the
159+
// same agent.
160+
DebuggeeLabels debuggee_labels_;
161+
151162
DISALLOW_COPY_AND_ASSIGN(Worker);
152163
};
153164

0 commit comments

Comments
 (0)