blob: bff410ea1db0c7de8cadd1796ed9b8256a1b97ae [file] [log] [blame]
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "debugger.h"
18
Elliott Hughes3bb81562011-10-21 18:52:59 -070019#include <sys/uio.h>
20
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -070021#include "ScopedPrimitiveArray.h"
Elliott Hughes47fce012011-10-25 18:37:19 -070022#include "stack_indirect_reference_table.h"
Elliott Hughes475fc232011-10-25 15:00:35 -070023#include "thread_list.h"
24
Elliott Hughes872d4ec2011-10-21 17:07:15 -070025namespace art {
26
Elliott Hughes475fc232011-10-25 15:00:35 -070027class ObjectRegistry {
28 public:
29 ObjectRegistry() : lock_("ObjectRegistry lock") {
30 }
31
32 JDWP::ObjectId Add(Object* o) {
33 if (o == NULL) {
34 return 0;
35 }
36 JDWP::ObjectId id = static_cast<JDWP::ObjectId>(reinterpret_cast<uintptr_t>(o));
37 MutexLock mu(lock_);
38 map_[id] = o;
39 return id;
40 }
41
Elliott Hughes234ab152011-10-26 14:02:26 -070042 void Clear() {
43 MutexLock mu(lock_);
44 LOG(DEBUG) << "Debugger has detached; object registry had " << map_.size() << " entries";
45 map_.clear();
46 }
47
Elliott Hughes475fc232011-10-25 15:00:35 -070048 bool Contains(JDWP::ObjectId id) {
49 MutexLock mu(lock_);
50 return map_.find(id) != map_.end();
51 }
52
Elliott Hughesbfe487b2011-10-26 15:48:55 -070053 void VisitRoots(Heap::RootVisitor* visitor, void* arg) {
54 MutexLock mu(lock_);
55 typedef std::map<JDWP::ObjectId, Object*>::iterator It; // C++0x auto
56 for (It it = map_.begin(); it != map_.end(); ++it) {
57 visitor(it->second, arg);
58 }
59 }
60
Elliott Hughes475fc232011-10-25 15:00:35 -070061 private:
62 Mutex lock_;
63 std::map<JDWP::ObjectId, Object*> map_;
64};
65
Elliott Hughes4ffd3132011-10-24 12:06:42 -070066// JDWP is allowed unless the Zygote forbids it.
67static bool gJdwpAllowed = true;
68
Elliott Hughes3bb81562011-10-21 18:52:59 -070069// Was there a -Xrunjdwp or -agent argument on the command-line?
70static bool gJdwpConfigured = false;
71
72// Broken-down JDWP options. (Only valid if gJdwpConfigured is true.)
Elliott Hughes376a7a02011-10-24 18:35:55 -070073static JDWP::JdwpOptions gJdwpOptions;
Elliott Hughes3bb81562011-10-21 18:52:59 -070074
75// Runtime JDWP state.
76static JDWP::JdwpState* gJdwpState = NULL;
77static bool gDebuggerConnected; // debugger or DDMS is connected.
78static bool gDebuggerActive; // debugger is making requests.
79
Elliott Hughes47fce012011-10-25 18:37:19 -070080static bool gDdmThreadNotification = false;
81
Elliott Hughes767a1472011-10-26 18:49:02 -070082// DDMS GC-related settings.
83static Dbg::HpifWhen gDdmHpifWhen = Dbg::HPIF_WHEN_NEVER;
84static Dbg::HpsgWhen gDdmHpsgWhen = Dbg::HPSG_WHEN_NEVER;
85static Dbg::HpsgWhat gDdmHpsgWhat;
86static Dbg::HpsgWhen gDdmNhsgWhen = Dbg::HPSG_WHEN_NEVER;
87static Dbg::HpsgWhat gDdmNhsgWhat;
88
Elliott Hughes475fc232011-10-25 15:00:35 -070089static ObjectRegistry* gRegistry = NULL;
90
Elliott Hughes3bb81562011-10-21 18:52:59 -070091/*
92 * Handle one of the JDWP name/value pairs.
93 *
94 * JDWP options are:
95 * help: if specified, show help message and bail
96 * transport: may be dt_socket or dt_shmem
97 * address: for dt_socket, "host:port", or just "port" when listening
98 * server: if "y", wait for debugger to attach; if "n", attach to debugger
99 * timeout: how long to wait for debugger to connect / listen
100 *
101 * Useful with server=n (these aren't supported yet):
102 * onthrow=<exception-name>: connect to debugger when exception thrown
103 * onuncaught=y|n: connect to debugger when uncaught exception thrown
104 * launch=<command-line>: launch the debugger itself
105 *
106 * The "transport" option is required, as is "address" if server=n.
107 */
108static bool ParseJdwpOption(const std::string& name, const std::string& value) {
109 if (name == "transport") {
110 if (value == "dt_socket") {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700111 gJdwpOptions.transport = JDWP::kJdwpTransportSocket;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700112 } else if (value == "dt_android_adb") {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700113 gJdwpOptions.transport = JDWP::kJdwpTransportAndroidAdb;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700114 } else {
115 LOG(ERROR) << "JDWP transport not supported: " << value;
116 return false;
117 }
118 } else if (name == "server") {
119 if (value == "n") {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700120 gJdwpOptions.server = false;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700121 } else if (value == "y") {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700122 gJdwpOptions.server = true;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700123 } else {
124 LOG(ERROR) << "JDWP option 'server' must be 'y' or 'n'";
125 return false;
126 }
127 } else if (name == "suspend") {
128 if (value == "n") {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700129 gJdwpOptions.suspend = false;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700130 } else if (value == "y") {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700131 gJdwpOptions.suspend = true;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700132 } else {
133 LOG(ERROR) << "JDWP option 'suspend' must be 'y' or 'n'";
134 return false;
135 }
136 } else if (name == "address") {
137 /* this is either <port> or <host>:<port> */
138 std::string port_string;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700139 gJdwpOptions.host.clear();
Elliott Hughes3bb81562011-10-21 18:52:59 -0700140 std::string::size_type colon = value.find(':');
141 if (colon != std::string::npos) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700142 gJdwpOptions.host = value.substr(0, colon);
Elliott Hughes3bb81562011-10-21 18:52:59 -0700143 port_string = value.substr(colon + 1);
144 } else {
145 port_string = value;
146 }
147 if (port_string.empty()) {
148 LOG(ERROR) << "JDWP address missing port: " << value;
149 return false;
150 }
151 char* end;
152 long port = strtol(port_string.c_str(), &end, 10);
153 if (*end != '\0') {
154 LOG(ERROR) << "JDWP address has junk in port field: " << value;
155 return false;
156 }
Elliott Hughes376a7a02011-10-24 18:35:55 -0700157 gJdwpOptions.port = port;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700158 } else if (name == "launch" || name == "onthrow" || name == "oncaught" || name == "timeout") {
159 /* valid but unsupported */
160 LOG(INFO) << "Ignoring JDWP option '" << name << "'='" << value << "'";
161 } else {
162 LOG(INFO) << "Ignoring unrecognized JDWP option '" << name << "'='" << value << "'";
163 }
164
165 return true;
166}
167
168/*
169 * Parse the latter half of a -Xrunjdwp/-agentlib:jdwp= string, e.g.:
170 * "transport=dt_socket,address=8000,server=y,suspend=n"
171 */
172bool Dbg::ParseJdwpOptions(const std::string& options) {
Elliott Hughes47fce012011-10-25 18:37:19 -0700173 LOG(VERBOSE) << "ParseJdwpOptions: " << options;
174
Elliott Hughes3bb81562011-10-21 18:52:59 -0700175 std::vector<std::string> pairs;
176 Split(options, ',', pairs);
177
178 for (size_t i = 0; i < pairs.size(); ++i) {
179 std::string::size_type equals = pairs[i].find('=');
180 if (equals == std::string::npos) {
181 LOG(ERROR) << "Can't parse JDWP option '" << pairs[i] << "' in '" << options << "'";
182 return false;
183 }
184 ParseJdwpOption(pairs[i].substr(0, equals), pairs[i].substr(equals + 1));
185 }
186
Elliott Hughes376a7a02011-10-24 18:35:55 -0700187 if (gJdwpOptions.transport == JDWP::kJdwpTransportUnknown) {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700188 LOG(ERROR) << "Must specify JDWP transport: " << options;
189 }
Elliott Hughes376a7a02011-10-24 18:35:55 -0700190 if (!gJdwpOptions.server && (gJdwpOptions.host.empty() || gJdwpOptions.port == 0)) {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700191 LOG(ERROR) << "Must specify JDWP host and port when server=n: " << options;
192 return false;
193 }
194
195 gJdwpConfigured = true;
196 return true;
197}
198
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700199void Dbg::StartJdwp() {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700200 if (!gJdwpAllowed || !gJdwpConfigured) {
201 // No JDWP for you!
202 return;
203 }
204
Elliott Hughes475fc232011-10-25 15:00:35 -0700205 CHECK(gRegistry == NULL);
206 gRegistry = new ObjectRegistry;
207
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700208 // Init JDWP if the debugger is enabled. This may connect out to a
209 // debugger, passively listen for a debugger, or block waiting for a
210 // debugger.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700211 gJdwpState = JDWP::JdwpState::Create(&gJdwpOptions);
212 if (gJdwpState == NULL) {
213 LOG(WARNING) << "debugger thread failed to initialize";
Elliott Hughes475fc232011-10-25 15:00:35 -0700214 return;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700215 }
216
217 // If a debugger has already attached, send the "welcome" message.
218 // This may cause us to suspend all threads.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700219 if (gJdwpState->IsActive()) {
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700220 //ScopedThreadStateChange(Thread::Current(), Thread::kRunnable);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700221 if (!gJdwpState->PostVMStart()) {
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700222 LOG(WARNING) << "failed to post 'start' message to debugger";
223 }
224 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700225}
226
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700227void Dbg::StopJdwp() {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700228 delete gJdwpState;
Elliott Hughes475fc232011-10-25 15:00:35 -0700229 delete gRegistry;
230 gRegistry = NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700231}
232
Elliott Hughes767a1472011-10-26 18:49:02 -0700233void Dbg::GcDidFinish() {
234 if (gDdmHpifWhen != HPIF_WHEN_NEVER) {
235 LOG(DEBUG) << "Sending VM heap info to DDM";
236 DdmSendHeapInfo(gDdmHpifWhen, false);
237 }
238 if (gDdmHpsgWhen != HPSG_WHEN_NEVER) {
239 LOG(DEBUG) << "Dumping VM heap to DDM";
240 DdmSendHeapSegments(false, false);
241 }
242 if (gDdmNhsgWhen != HPSG_WHEN_NEVER) {
243 LOG(DEBUG) << "Dumping native heap to DDM";
244 DdmSendHeapSegments(false, true);
245 }
246}
247
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700248void Dbg::SetJdwpAllowed(bool allowed) {
249 gJdwpAllowed = allowed;
250}
251
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700252DebugInvokeReq* Dbg::GetInvokeReq() {
Elliott Hughes475fc232011-10-25 15:00:35 -0700253 return Thread::Current()->GetInvokeReq();
254}
255
256Thread* Dbg::GetDebugThread() {
257 return (gJdwpState != NULL) ? gJdwpState->GetDebugThread() : NULL;
258}
259
260void Dbg::ClearWaitForEventThread() {
261 gJdwpState->ClearWaitForEventThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700262}
263
264void Dbg::Connected() {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700265 CHECK(!gDebuggerConnected);
266 LOG(VERBOSE) << "JDWP has attached";
267 gDebuggerConnected = true;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700268}
269
270void Dbg::Active() {
271 UNIMPLEMENTED(FATAL);
272}
273
274void Dbg::Disconnected() {
Elliott Hughes234ab152011-10-26 14:02:26 -0700275 CHECK(gDebuggerConnected);
276
277 gDebuggerActive = false;
278
279 //dvmDisableAllSubMode(kSubModeDebuggerActive);
280
281 gRegistry->Clear();
282 gDebuggerConnected = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700283}
284
285bool Dbg::IsDebuggerConnected() {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700286 return gDebuggerActive;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700287}
288
289bool Dbg::IsDebuggingEnabled() {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700290 return gJdwpConfigured;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700291}
292
293int64_t Dbg::LastDebuggerActivity() {
294 UNIMPLEMENTED(WARNING);
295 return -1;
296}
297
298int Dbg::ThreadRunning() {
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700299 return static_cast<int>(Thread::Current()->SetState(Thread::kRunnable));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700300}
301
302int Dbg::ThreadWaiting() {
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700303 return static_cast<int>(Thread::Current()->SetState(Thread::kVmWait));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700304}
305
Elliott Hughes6ba581a2011-10-25 11:45:35 -0700306int Dbg::ThreadContinuing(int new_state) {
307 return static_cast<int>(Thread::Current()->SetState(static_cast<Thread::State>(new_state)));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700308}
309
310void Dbg::UndoDebuggerSuspensions() {
Elliott Hughes234ab152011-10-26 14:02:26 -0700311 Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700312}
313
314void Dbg::Exit(int status) {
315 UNIMPLEMENTED(FATAL);
316}
317
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700318void Dbg::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
319 if (gRegistry != NULL) {
320 gRegistry->VisitRoots(visitor, arg);
321 }
322}
323
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700324const char* Dbg::GetClassDescriptor(JDWP::RefTypeId id) {
325 UNIMPLEMENTED(FATAL);
326 return NULL;
327}
328
329JDWP::ObjectId Dbg::GetClassObject(JDWP::RefTypeId id) {
330 UNIMPLEMENTED(FATAL);
331 return 0;
332}
333
334JDWP::RefTypeId Dbg::GetSuperclass(JDWP::RefTypeId id) {
335 UNIMPLEMENTED(FATAL);
336 return 0;
337}
338
339JDWP::ObjectId Dbg::GetClassLoader(JDWP::RefTypeId id) {
340 UNIMPLEMENTED(FATAL);
341 return 0;
342}
343
344uint32_t Dbg::GetAccessFlags(JDWP::RefTypeId id) {
345 UNIMPLEMENTED(FATAL);
346 return 0;
347}
348
349bool Dbg::IsInterface(JDWP::RefTypeId id) {
350 UNIMPLEMENTED(FATAL);
351 return false;
352}
353
354void Dbg::GetClassList(uint32_t* pNumClasses, JDWP::RefTypeId** pClassRefBuf) {
355 UNIMPLEMENTED(FATAL);
356}
357
358void Dbg::GetVisibleClassList(JDWP::ObjectId classLoaderId, uint32_t* pNumClasses, JDWP::RefTypeId** pClassRefBuf) {
359 UNIMPLEMENTED(FATAL);
360}
361
362void Dbg::GetClassInfo(JDWP::RefTypeId classId, uint8_t* pTypeTag, uint32_t* pStatus, const char** pSignature) {
363 UNIMPLEMENTED(FATAL);
364}
365
366bool Dbg::FindLoadedClassBySignature(const char* classDescriptor, JDWP::RefTypeId* pRefTypeId) {
367 UNIMPLEMENTED(FATAL);
368 return false;
369}
370
371void Dbg::GetObjectType(JDWP::ObjectId objectId, uint8_t* pRefTypeTag, JDWP::RefTypeId* pRefTypeId) {
372 UNIMPLEMENTED(FATAL);
373}
374
375uint8_t Dbg::GetClassObjectType(JDWP::RefTypeId refTypeId) {
376 UNIMPLEMENTED(FATAL);
377 return 0;
378}
379
380const char* Dbg::GetSignature(JDWP::RefTypeId refTypeId) {
381 UNIMPLEMENTED(FATAL);
382 return NULL;
383}
384
385const char* Dbg::GetSourceFile(JDWP::RefTypeId refTypeId) {
386 UNIMPLEMENTED(FATAL);
387 return NULL;
388}
389
390const char* Dbg::GetObjectTypeName(JDWP::ObjectId objectId) {
391 UNIMPLEMENTED(FATAL);
392 return NULL;
393}
394
395uint8_t Dbg::GetObjectTag(JDWP::ObjectId objectId) {
396 UNIMPLEMENTED(FATAL);
397 return 0;
398}
399
400int Dbg::GetTagWidth(int tag) {
401 UNIMPLEMENTED(FATAL);
402 return 0;
403}
404
405int Dbg::GetArrayLength(JDWP::ObjectId arrayId) {
406 UNIMPLEMENTED(FATAL);
407 return 0;
408}
409
410uint8_t Dbg::GetArrayElementTag(JDWP::ObjectId arrayId) {
411 UNIMPLEMENTED(FATAL);
412 return 0;
413}
414
415bool Dbg::OutputArray(JDWP::ObjectId arrayId, int firstIndex, int count, JDWP::ExpandBuf* pReply) {
416 UNIMPLEMENTED(FATAL);
417 return false;
418}
419
420bool Dbg::SetArrayElements(JDWP::ObjectId arrayId, int firstIndex, int count, const uint8_t* buf) {
421 UNIMPLEMENTED(FATAL);
422 return false;
423}
424
425JDWP::ObjectId Dbg::CreateString(const char* str) {
426 UNIMPLEMENTED(FATAL);
427 return 0;
428}
429
430JDWP::ObjectId Dbg::CreateObject(JDWP::RefTypeId classId) {
431 UNIMPLEMENTED(FATAL);
432 return 0;
433}
434
435JDWP::ObjectId Dbg::CreateArrayObject(JDWP::RefTypeId arrayTypeId, uint32_t length) {
436 UNIMPLEMENTED(FATAL);
437 return 0;
438}
439
440bool Dbg::MatchType(JDWP::RefTypeId instClassId, JDWP::RefTypeId classId) {
441 UNIMPLEMENTED(FATAL);
442 return false;
443}
444
445const char* Dbg::GetMethodName(JDWP::RefTypeId refTypeId, JDWP::MethodId id) {
446 UNIMPLEMENTED(FATAL);
447 return NULL;
448}
449
450void Dbg::OutputAllFields(JDWP::RefTypeId refTypeId, bool withGeneric, JDWP::ExpandBuf* pReply) {
451 UNIMPLEMENTED(FATAL);
452}
453
454void Dbg::OutputAllMethods(JDWP::RefTypeId refTypeId, bool withGeneric, JDWP::ExpandBuf* pReply) {
455 UNIMPLEMENTED(FATAL);
456}
457
458void Dbg::OutputAllInterfaces(JDWP::RefTypeId refTypeId, JDWP::ExpandBuf* pReply) {
459 UNIMPLEMENTED(FATAL);
460}
461
462void Dbg::OutputLineTable(JDWP::RefTypeId refTypeId, JDWP::MethodId methodId, JDWP::ExpandBuf* pReply) {
463 UNIMPLEMENTED(FATAL);
464}
465
466void Dbg::OutputVariableTable(JDWP::RefTypeId refTypeId, JDWP::MethodId id, bool withGeneric, JDWP::ExpandBuf* pReply) {
467 UNIMPLEMENTED(FATAL);
468}
469
470uint8_t Dbg::GetFieldBasicTag(JDWP::ObjectId objId, JDWP::FieldId fieldId) {
471 UNIMPLEMENTED(FATAL);
472 return 0;
473}
474
475uint8_t Dbg::GetStaticFieldBasicTag(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId) {
476 UNIMPLEMENTED(FATAL);
477 return 0;
478}
479
480void Dbg::GetFieldValue(JDWP::ObjectId objectId, JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply) {
481 UNIMPLEMENTED(FATAL);
482}
483
484void Dbg::SetFieldValue(JDWP::ObjectId objectId, JDWP::FieldId fieldId, uint64_t value, int width) {
485 UNIMPLEMENTED(FATAL);
486}
487
488void Dbg::GetStaticFieldValue(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply) {
489 UNIMPLEMENTED(FATAL);
490}
491
492void Dbg::SetStaticFieldValue(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId, uint64_t rawValue, int width) {
493 UNIMPLEMENTED(FATAL);
494}
495
496char* Dbg::StringToUtf8(JDWP::ObjectId strId) {
497 UNIMPLEMENTED(FATAL);
498 return NULL;
499}
500
501char* Dbg::GetThreadName(JDWP::ObjectId threadId) {
502 UNIMPLEMENTED(FATAL);
503 return NULL;
504}
505
506JDWP::ObjectId Dbg::GetThreadGroup(JDWP::ObjectId threadId) {
507 UNIMPLEMENTED(FATAL);
508 return 0;
509}
510
511char* Dbg::GetThreadGroupName(JDWP::ObjectId threadGroupId) {
512 UNIMPLEMENTED(FATAL);
513 return NULL;
514}
515
516JDWP::ObjectId Dbg::GetThreadGroupParent(JDWP::ObjectId threadGroupId) {
517 UNIMPLEMENTED(FATAL);
518 return 0;
519}
520
521JDWP::ObjectId Dbg::GetSystemThreadGroupId() {
522 UNIMPLEMENTED(FATAL);
523 return 0;
524}
525
526JDWP::ObjectId Dbg::GetMainThreadGroupId() {
527 UNIMPLEMENTED(FATAL);
528 return 0;
529}
530
531bool Dbg::GetThreadStatus(JDWP::ObjectId threadId, uint32_t* threadStatus, uint32_t* suspendStatus) {
532 UNIMPLEMENTED(FATAL);
533 return false;
534}
535
536uint32_t Dbg::GetThreadSuspendCount(JDWP::ObjectId threadId) {
537 UNIMPLEMENTED(FATAL);
538 return 0;
539}
540
541bool Dbg::ThreadExists(JDWP::ObjectId threadId) {
542 UNIMPLEMENTED(FATAL);
543 return false;
544}
545
546bool Dbg::IsSuspended(JDWP::ObjectId threadId) {
547 UNIMPLEMENTED(FATAL);
548 return false;
549}
550
551//void Dbg::WaitForSuspend(JDWP::ObjectId threadId);
552
553void Dbg::GetThreadGroupThreads(JDWP::ObjectId threadGroupId, JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount) {
554 UNIMPLEMENTED(FATAL);
555}
556
557void Dbg::GetAllThreads(JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount) {
558 UNIMPLEMENTED(FATAL);
559}
560
561int Dbg::GetThreadFrameCount(JDWP::ObjectId threadId) {
562 UNIMPLEMENTED(FATAL);
563 return 0;
564}
565
566bool Dbg::GetThreadFrame(JDWP::ObjectId threadId, int num, JDWP::FrameId* pFrameId, JDWP::JdwpLocation* pLoc) {
567 UNIMPLEMENTED(FATAL);
568 return false;
569}
570
571JDWP::ObjectId Dbg::GetThreadSelfId() {
Elliott Hughes475fc232011-10-25 15:00:35 -0700572 return gRegistry->Add(Thread::Current()->GetPeer());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700573}
574
Elliott Hughes475fc232011-10-25 15:00:35 -0700575void Dbg::SuspendVM() {
576 Runtime::Current()->GetThreadList()->SuspendAll(true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700577}
578
579void Dbg::ResumeVM() {
Elliott Hughes475fc232011-10-25 15:00:35 -0700580 Runtime::Current()->GetThreadList()->ResumeAll(true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700581}
582
583void Dbg::SuspendThread(JDWP::ObjectId threadId) {
584 UNIMPLEMENTED(FATAL);
585}
586
587void Dbg::ResumeThread(JDWP::ObjectId threadId) {
588 UNIMPLEMENTED(FATAL);
589}
590
591void Dbg::SuspendSelf() {
Elliott Hughes475fc232011-10-25 15:00:35 -0700592 Runtime::Current()->GetThreadList()->SuspendSelfForDebugger();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700593}
594
595bool Dbg::GetThisObject(JDWP::ObjectId threadId, JDWP::FrameId frameId, JDWP::ObjectId* pThisId) {
596 UNIMPLEMENTED(FATAL);
597 return false;
598}
599
600void Dbg::GetLocalValue(JDWP::ObjectId threadId, JDWP::FrameId frameId, int slot, uint8_t tag, uint8_t* buf, int expectedLen) {
601 UNIMPLEMENTED(FATAL);
602}
603
604void Dbg::SetLocalValue(JDWP::ObjectId threadId, JDWP::FrameId frameId, int slot, uint8_t tag, uint64_t value, int width) {
605 UNIMPLEMENTED(FATAL);
606}
607
608void Dbg::PostLocationEvent(const Method* method, int pcOffset, Object* thisPtr, int eventFlags) {
609 UNIMPLEMENTED(FATAL);
610}
611
612void Dbg::PostException(void* throwFp, int throwRelPc, void* catchFp, int catchRelPc, Object* exception) {
613 UNIMPLEMENTED(FATAL);
614}
615
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700616void Dbg::PostClassPrepare(Class* c) {
617 UNIMPLEMENTED(FATAL);
618}
619
620bool Dbg::WatchLocation(const JDWP::JdwpLocation* pLoc) {
621 UNIMPLEMENTED(FATAL);
622 return false;
623}
624
625void Dbg::UnwatchLocation(const JDWP::JdwpLocation* pLoc) {
626 UNIMPLEMENTED(FATAL);
627}
628
629bool Dbg::ConfigureStep(JDWP::ObjectId threadId, JDWP::JdwpStepSize size, JDWP::JdwpStepDepth depth) {
630 UNIMPLEMENTED(FATAL);
631 return false;
632}
633
634void Dbg::UnconfigureStep(JDWP::ObjectId threadId) {
635 UNIMPLEMENTED(FATAL);
636}
637
638JDWP::JdwpError Dbg::InvokeMethod(JDWP::ObjectId threadId, JDWP::ObjectId objectId, JDWP::RefTypeId classId, JDWP::MethodId methodId, uint32_t numArgs, uint64_t* argArray, uint32_t options, uint8_t* pResultTag, uint64_t* pResultValue, JDWP::ObjectId* pExceptObj) {
639 UNIMPLEMENTED(FATAL);
640 return JDWP::ERR_NONE;
641}
642
643void Dbg::ExecuteMethod(DebugInvokeReq* pReq) {
644 UNIMPLEMENTED(FATAL);
645}
646
647void Dbg::RegisterObjectId(JDWP::ObjectId id) {
648 UNIMPLEMENTED(FATAL);
649}
650
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700651/*
652 * "buf" contains a full JDWP packet, possibly with multiple chunks. We
653 * need to process each, accumulate the replies, and ship the whole thing
654 * back.
655 *
656 * Returns "true" if we have a reply. The reply buffer is newly allocated,
657 * and includes the chunk type/length, followed by the data.
658 *
659 * TODO: we currently assume that the request and reply include a single
660 * chunk. If this becomes inconvenient we will need to adapt.
661 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700662bool Dbg::DdmHandlePacket(const uint8_t* buf, int dataLen, uint8_t** pReplyBuf, int* pReplyLen) {
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700663 CHECK_GE(dataLen, 0);
664
665 Thread* self = Thread::Current();
666 JNIEnv* env = self->GetJniEnv();
667
668 static jclass Chunk_class = env->FindClass("org/apache/harmony/dalvik/ddmc/Chunk");
669 static jclass DdmServer_class = env->FindClass("org/apache/harmony/dalvik/ddmc/DdmServer");
670 static jmethodID dispatch_mid = env->GetStaticMethodID(DdmServer_class, "dispatch",
671 "(I[BII)Lorg/apache/harmony/dalvik/ddmc/Chunk;");
672 static jfieldID data_fid = env->GetFieldID(Chunk_class, "data", "[B");
673 static jfieldID length_fid = env->GetFieldID(Chunk_class, "length", "I");
674 static jfieldID offset_fid = env->GetFieldID(Chunk_class, "offset", "I");
675 static jfieldID type_fid = env->GetFieldID(Chunk_class, "type", "I");
676
677 // Create a byte[] corresponding to 'buf'.
678 jbyteArray dataArray = env->NewByteArray(dataLen);
679 if (dataArray == NULL) {
680 LOG(WARNING) << "byte[] allocation failed: " << dataLen;
681 env->ExceptionClear();
682 return false;
683 }
684 env->SetByteArrayRegion(dataArray, 0, dataLen, reinterpret_cast<const jbyte*>(buf));
685
686 const int kChunkHdrLen = 8;
687
688 // Run through and find all chunks. [Currently just find the first.]
689 ScopedByteArrayRO contents(env, dataArray);
690 jint type = JDWP::get4BE(reinterpret_cast<const uint8_t*>(&contents[0]));
691 jint length = JDWP::get4BE(reinterpret_cast<const uint8_t*>(&contents[4]));
692 jint offset = kChunkHdrLen;
693 if (offset + length > dataLen) {
694 LOG(WARNING) << StringPrintf("bad chunk found (len=%u pktLen=%d)", length, dataLen);
695 return false;
696 }
697
698 // Call "private static Chunk dispatch(int type, byte[] data, int offset, int length)".
699 jobject chunk = env->CallStaticObjectMethod(DdmServer_class, dispatch_mid, type, dataArray, offset, length);
700 if (env->ExceptionCheck()) {
701 LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type);
702 env->ExceptionDescribe();
703 env->ExceptionClear();
704 return false;
705 }
706
707 if (chunk == NULL) {
708 return false;
709 }
710
711 /*
712 * Pull the pieces out of the chunk. We copy the results into a
713 * newly-allocated buffer that the caller can free. We don't want to
714 * continue using the Chunk object because nothing has a reference to it.
715 *
716 * We could avoid this by returning type/data/offset/length and having
717 * the caller be aware of the object lifetime issues, but that
718 * integrates the JDWP code more tightly into the VM, and doesn't work
719 * if we have responses for multiple chunks.
720 *
721 * So we're pretty much stuck with copying data around multiple times.
722 */
723 jbyteArray replyData = reinterpret_cast<jbyteArray>(env->GetObjectField(chunk, data_fid));
724 length = env->GetIntField(chunk, length_fid);
725 offset = env->GetIntField(chunk, offset_fid);
726 type = env->GetIntField(chunk, type_fid);
727
728 LOG(VERBOSE) << StringPrintf("DDM reply: type=0x%08x data=%p offset=%d length=%d", type, replyData, offset, length);
729 if (length == 0 || replyData == NULL) {
730 return false;
731 }
732
733 jsize replyLength = env->GetArrayLength(replyData);
734 if (offset + length > replyLength) {
735 LOG(WARNING) << StringPrintf("chunk off=%d len=%d exceeds reply array len %d", offset, length, replyLength);
736 return false;
737 }
738
739 uint8_t* reply = new uint8_t[length + kChunkHdrLen];
740 if (reply == NULL) {
741 LOG(WARNING) << "malloc failed: " << (length + kChunkHdrLen);
742 return false;
743 }
744 JDWP::set4BE(reply + 0, type);
745 JDWP::set4BE(reply + 4, length);
746 env->GetByteArrayRegion(replyData, offset, length, reinterpret_cast<jbyte*>(reply + kChunkHdrLen));
747
748 *pReplyBuf = reply;
749 *pReplyLen = length + kChunkHdrLen;
750
751 LOG(VERBOSE) << StringPrintf("dvmHandleDdm returning type=%.4s buf=%p len=%d", (char*) reply, reply, length);
752 return true;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700753}
754
Elliott Hughes47fce012011-10-25 18:37:19 -0700755void DdmBroadcast(bool connect) {
756 LOG(VERBOSE) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "...";
757
758 Thread* self = Thread::Current();
759 if (self->GetState() != Thread::kRunnable) {
760 LOG(ERROR) << "DDM broadcast in thread state " << self->GetState();
761 /* try anyway? */
762 }
763
764 JNIEnv* env = self->GetJniEnv();
765 static jclass DdmServer_class = env->FindClass("org/apache/harmony/dalvik/ddmc/DdmServer");
766 static jmethodID broadcast_mid = env->GetStaticMethodID(DdmServer_class, "broadcast", "(I)V");
767 jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/;
768 env->CallStaticVoidMethod(DdmServer_class, broadcast_mid, event);
769 if (env->ExceptionCheck()) {
770 LOG(ERROR) << "DdmServer.broadcast " << event << " failed";
771 env->ExceptionDescribe();
772 env->ExceptionClear();
773 }
774}
775
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700776void Dbg::DdmConnected() {
Elliott Hughes47fce012011-10-25 18:37:19 -0700777 DdmBroadcast(true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700778}
779
780void Dbg::DdmDisconnected() {
Elliott Hughes47fce012011-10-25 18:37:19 -0700781 DdmBroadcast(false);
782 gDdmThreadNotification = false;
783}
784
785/*
786 * Send a notification when a thread starts or stops.
787 *
788 * Because we broadcast the full set of threads when the notifications are
789 * first enabled, it's possible for "thread" to be actively executing.
790 */
791void DdmSendThreadNotification(Thread* t, bool started) {
792 if (!gDdmThreadNotification) {
793 return;
794 }
795
796 if (started) {
797 SirtRef<String> name(t->GetName());
798 size_t char_count = (name.get() != NULL) ? name->GetLength() : 0;
799
800 size_t byte_count = char_count*2 + sizeof(uint32_t)*2;
801 std::vector<uint8_t> buf(byte_count);
802 JDWP::set4BE(&buf[0], t->GetThinLockId());
803 JDWP::set4BE(&buf[4], char_count);
804 if (char_count > 0) {
805 // Copy the UTF-16 string, transforming to big-endian.
806 const jchar* src = name->GetCharArray()->GetData();
807 jchar* dst = reinterpret_cast<jchar*>(&buf[8]);
808 while (char_count--) {
809 JDWP::set2BE(reinterpret_cast<uint8_t*>(dst++), *src++);
810 }
811 }
812 Dbg::DdmSendChunk(CHUNK_TYPE("THCR"), buf.size(), &buf[0]);
813 } else {
814 uint8_t buf[4];
815 JDWP::set4BE(&buf[0], t->GetThinLockId());
816 Dbg::DdmSendChunk(CHUNK_TYPE("THDE"), 4, buf);
817 }
818}
819
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700820void DdmSendThreadStartCallback(Thread* t, void*) {
Elliott Hughes47fce012011-10-25 18:37:19 -0700821 DdmSendThreadNotification(t, true);
822}
823
824void Dbg::DdmSetThreadNotification(bool enable) {
825 // We lock the thread list to avoid sending duplicate events or missing
826 // a thread change. We should be okay holding this lock while sending
827 // the messages out. (We have to hold it while accessing a live thread.)
828 ThreadListLock lock;
829
830 gDdmThreadNotification = enable;
831 if (enable) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700832 Runtime::Current()->GetThreadList()->ForEach(DdmSendThreadStartCallback, NULL);
Elliott Hughes47fce012011-10-25 18:37:19 -0700833 }
834}
835
836void PostThreadStartOrStop(Thread* t, bool is_start) {
837 if (gDebuggerActive) {
838 JDWP::ObjectId id = gRegistry->Add(t->GetPeer());
839 gJdwpState->PostThreadChange(id, is_start);
840 }
841 DdmSendThreadNotification(t, is_start);
842}
843
844void Dbg::PostThreadStart(Thread* t) {
845 PostThreadStartOrStop(t, true);
846}
847
848void Dbg::PostThreadDeath(Thread* t) {
849 PostThreadStartOrStop(t, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700850}
851
Elliott Hughes3bb81562011-10-21 18:52:59 -0700852void Dbg::DdmSendChunk(int type, size_t byte_count, const uint8_t* buf) {
853 CHECK(buf != NULL);
854 iovec vec[1];
855 vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(buf));
856 vec[0].iov_len = byte_count;
857 Dbg::DdmSendChunkV(type, vec, 1);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700858}
859
860void Dbg::DdmSendChunkV(int type, const struct iovec* iov, int iovcnt) {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700861 if (gJdwpState == NULL) {
862 LOG(VERBOSE) << "Debugger thread not active, ignoring DDM send: " << type;
863 } else {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700864 gJdwpState->DdmSendChunkV(type, iov, iovcnt);
Elliott Hughes3bb81562011-10-21 18:52:59 -0700865 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700866}
867
Elliott Hughes767a1472011-10-26 18:49:02 -0700868int Dbg::DdmHandleHpifChunk(HpifWhen when) {
869 if (when == HPIF_WHEN_NOW) {
870 DdmSendHeapInfo(when, true);
871 return true;
872 }
873
874 if (when != HPIF_WHEN_NEVER && when != HPIF_WHEN_NEXT_GC && when != HPIF_WHEN_EVERY_GC) {
875 LOG(ERROR) << "invalid HpifWhen value: " << static_cast<int>(when);
876 return false;
877 }
878
879 gDdmHpifWhen = when;
880 return true;
881}
882
883bool Dbg::DdmHandleHpsgNhsgChunk(Dbg::HpsgWhen when, Dbg::HpsgWhat what, bool native) {
884 if (when != HPSG_WHEN_NEVER && when != HPSG_WHEN_EVERY_GC) {
885 LOG(ERROR) << "invalid HpsgWhen value: " << static_cast<int>(when);
886 return false;
887 }
888
889 if (what != HPSG_WHAT_MERGED_OBJECTS && what != HPSG_WHAT_DISTINCT_OBJECTS) {
890 LOG(ERROR) << "invalid HpsgWhat value: " << static_cast<int>(what);
891 return false;
892 }
893
894 if (native) {
895 gDdmNhsgWhen = when;
896 gDdmNhsgWhat = what;
897 } else {
898 gDdmHpsgWhen = when;
899 gDdmHpsgWhat = what;
900 }
901 return true;
902}
903
904void Dbg::DdmSendHeapInfo(HpifWhen reason, bool shouldLock) {
905 UNIMPLEMENTED(WARNING) << "reason=" << static_cast<int>(reason) << " shouldLock=" << shouldLock;
906}
907
908void Dbg::DdmSendHeapSegments(bool shouldLock, bool native) {
909 UNIMPLEMENTED(WARNING) << "shouldLock=" << shouldLock << " native=" << native;
910}
911
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700912} // namespace art