blob: ca6643f8fd3e9ebd49c572b3294d86ba0638dfdc [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
42 bool Contains(JDWP::ObjectId id) {
43 MutexLock mu(lock_);
44 return map_.find(id) != map_.end();
45 }
46
47 private:
48 Mutex lock_;
49 std::map<JDWP::ObjectId, Object*> map_;
50};
51
Elliott Hughes4ffd3132011-10-24 12:06:42 -070052// JDWP is allowed unless the Zygote forbids it.
53static bool gJdwpAllowed = true;
54
Elliott Hughes3bb81562011-10-21 18:52:59 -070055// Was there a -Xrunjdwp or -agent argument on the command-line?
56static bool gJdwpConfigured = false;
57
58// Broken-down JDWP options. (Only valid if gJdwpConfigured is true.)
Elliott Hughes376a7a02011-10-24 18:35:55 -070059static JDWP::JdwpOptions gJdwpOptions;
Elliott Hughes3bb81562011-10-21 18:52:59 -070060
61// Runtime JDWP state.
62static JDWP::JdwpState* gJdwpState = NULL;
63static bool gDebuggerConnected; // debugger or DDMS is connected.
64static bool gDebuggerActive; // debugger is making requests.
65
Elliott Hughes47fce012011-10-25 18:37:19 -070066static bool gDdmThreadNotification = false;
67
Elliott Hughes475fc232011-10-25 15:00:35 -070068static ObjectRegistry* gRegistry = NULL;
69
Elliott Hughes3bb81562011-10-21 18:52:59 -070070/*
71 * Handle one of the JDWP name/value pairs.
72 *
73 * JDWP options are:
74 * help: if specified, show help message and bail
75 * transport: may be dt_socket or dt_shmem
76 * address: for dt_socket, "host:port", or just "port" when listening
77 * server: if "y", wait for debugger to attach; if "n", attach to debugger
78 * timeout: how long to wait for debugger to connect / listen
79 *
80 * Useful with server=n (these aren't supported yet):
81 * onthrow=<exception-name>: connect to debugger when exception thrown
82 * onuncaught=y|n: connect to debugger when uncaught exception thrown
83 * launch=<command-line>: launch the debugger itself
84 *
85 * The "transport" option is required, as is "address" if server=n.
86 */
87static bool ParseJdwpOption(const std::string& name, const std::string& value) {
88 if (name == "transport") {
89 if (value == "dt_socket") {
Elliott Hughes376a7a02011-10-24 18:35:55 -070090 gJdwpOptions.transport = JDWP::kJdwpTransportSocket;
Elliott Hughes3bb81562011-10-21 18:52:59 -070091 } else if (value == "dt_android_adb") {
Elliott Hughes376a7a02011-10-24 18:35:55 -070092 gJdwpOptions.transport = JDWP::kJdwpTransportAndroidAdb;
Elliott Hughes3bb81562011-10-21 18:52:59 -070093 } else {
94 LOG(ERROR) << "JDWP transport not supported: " << value;
95 return false;
96 }
97 } else if (name == "server") {
98 if (value == "n") {
Elliott Hughes376a7a02011-10-24 18:35:55 -070099 gJdwpOptions.server = false;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700100 } else if (value == "y") {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700101 gJdwpOptions.server = true;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700102 } else {
103 LOG(ERROR) << "JDWP option 'server' must be 'y' or 'n'";
104 return false;
105 }
106 } else if (name == "suspend") {
107 if (value == "n") {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700108 gJdwpOptions.suspend = false;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700109 } else if (value == "y") {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700110 gJdwpOptions.suspend = true;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700111 } else {
112 LOG(ERROR) << "JDWP option 'suspend' must be 'y' or 'n'";
113 return false;
114 }
115 } else if (name == "address") {
116 /* this is either <port> or <host>:<port> */
117 std::string port_string;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700118 gJdwpOptions.host.clear();
Elliott Hughes3bb81562011-10-21 18:52:59 -0700119 std::string::size_type colon = value.find(':');
120 if (colon != std::string::npos) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700121 gJdwpOptions.host = value.substr(0, colon);
Elliott Hughes3bb81562011-10-21 18:52:59 -0700122 port_string = value.substr(colon + 1);
123 } else {
124 port_string = value;
125 }
126 if (port_string.empty()) {
127 LOG(ERROR) << "JDWP address missing port: " << value;
128 return false;
129 }
130 char* end;
131 long port = strtol(port_string.c_str(), &end, 10);
132 if (*end != '\0') {
133 LOG(ERROR) << "JDWP address has junk in port field: " << value;
134 return false;
135 }
Elliott Hughes376a7a02011-10-24 18:35:55 -0700136 gJdwpOptions.port = port;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700137 } else if (name == "launch" || name == "onthrow" || name == "oncaught" || name == "timeout") {
138 /* valid but unsupported */
139 LOG(INFO) << "Ignoring JDWP option '" << name << "'='" << value << "'";
140 } else {
141 LOG(INFO) << "Ignoring unrecognized JDWP option '" << name << "'='" << value << "'";
142 }
143
144 return true;
145}
146
147/*
148 * Parse the latter half of a -Xrunjdwp/-agentlib:jdwp= string, e.g.:
149 * "transport=dt_socket,address=8000,server=y,suspend=n"
150 */
151bool Dbg::ParseJdwpOptions(const std::string& options) {
Elliott Hughes47fce012011-10-25 18:37:19 -0700152 LOG(VERBOSE) << "ParseJdwpOptions: " << options;
153
Elliott Hughes3bb81562011-10-21 18:52:59 -0700154 std::vector<std::string> pairs;
155 Split(options, ',', pairs);
156
157 for (size_t i = 0; i < pairs.size(); ++i) {
158 std::string::size_type equals = pairs[i].find('=');
159 if (equals == std::string::npos) {
160 LOG(ERROR) << "Can't parse JDWP option '" << pairs[i] << "' in '" << options << "'";
161 return false;
162 }
163 ParseJdwpOption(pairs[i].substr(0, equals), pairs[i].substr(equals + 1));
164 }
165
Elliott Hughes376a7a02011-10-24 18:35:55 -0700166 if (gJdwpOptions.transport == JDWP::kJdwpTransportUnknown) {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700167 LOG(ERROR) << "Must specify JDWP transport: " << options;
168 }
Elliott Hughes376a7a02011-10-24 18:35:55 -0700169 if (!gJdwpOptions.server && (gJdwpOptions.host.empty() || gJdwpOptions.port == 0)) {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700170 LOG(ERROR) << "Must specify JDWP host and port when server=n: " << options;
171 return false;
172 }
173
174 gJdwpConfigured = true;
175 return true;
176}
177
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700178void Dbg::StartJdwp() {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700179 if (!gJdwpAllowed || !gJdwpConfigured) {
180 // No JDWP for you!
181 return;
182 }
183
Elliott Hughes475fc232011-10-25 15:00:35 -0700184 CHECK(gRegistry == NULL);
185 gRegistry = new ObjectRegistry;
186
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700187 // Init JDWP if the debugger is enabled. This may connect out to a
188 // debugger, passively listen for a debugger, or block waiting for a
189 // debugger.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700190 gJdwpState = JDWP::JdwpState::Create(&gJdwpOptions);
191 if (gJdwpState == NULL) {
192 LOG(WARNING) << "debugger thread failed to initialize";
Elliott Hughes475fc232011-10-25 15:00:35 -0700193 return;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700194 }
195
196 // If a debugger has already attached, send the "welcome" message.
197 // This may cause us to suspend all threads.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700198 if (gJdwpState->IsActive()) {
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700199 //ScopedThreadStateChange(Thread::Current(), Thread::kRunnable);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700200 if (!gJdwpState->PostVMStart()) {
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700201 LOG(WARNING) << "failed to post 'start' message to debugger";
202 }
203 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700204}
205
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700206void Dbg::StopJdwp() {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700207 delete gJdwpState;
Elliott Hughes475fc232011-10-25 15:00:35 -0700208 delete gRegistry;
209 gRegistry = NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700210}
211
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700212void Dbg::SetJdwpAllowed(bool allowed) {
213 gJdwpAllowed = allowed;
214}
215
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700216DebugInvokeReq* Dbg::GetInvokeReq() {
Elliott Hughes475fc232011-10-25 15:00:35 -0700217 return Thread::Current()->GetInvokeReq();
218}
219
220Thread* Dbg::GetDebugThread() {
221 return (gJdwpState != NULL) ? gJdwpState->GetDebugThread() : NULL;
222}
223
224void Dbg::ClearWaitForEventThread() {
225 gJdwpState->ClearWaitForEventThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700226}
227
228void Dbg::Connected() {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700229 CHECK(!gDebuggerConnected);
230 LOG(VERBOSE) << "JDWP has attached";
231 gDebuggerConnected = true;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700232}
233
234void Dbg::Active() {
235 UNIMPLEMENTED(FATAL);
236}
237
238void Dbg::Disconnected() {
239 UNIMPLEMENTED(FATAL);
240}
241
242bool Dbg::IsDebuggerConnected() {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700243 return gDebuggerActive;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700244}
245
246bool Dbg::IsDebuggingEnabled() {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700247 return gJdwpConfigured;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700248}
249
250int64_t Dbg::LastDebuggerActivity() {
251 UNIMPLEMENTED(WARNING);
252 return -1;
253}
254
255int Dbg::ThreadRunning() {
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700256 return static_cast<int>(Thread::Current()->SetState(Thread::kRunnable));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700257}
258
259int Dbg::ThreadWaiting() {
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700260 return static_cast<int>(Thread::Current()->SetState(Thread::kVmWait));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700261}
262
Elliott Hughes6ba581a2011-10-25 11:45:35 -0700263int Dbg::ThreadContinuing(int new_state) {
264 return static_cast<int>(Thread::Current()->SetState(static_cast<Thread::State>(new_state)));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700265}
266
267void Dbg::UndoDebuggerSuspensions() {
268 UNIMPLEMENTED(FATAL);
269}
270
271void Dbg::Exit(int status) {
272 UNIMPLEMENTED(FATAL);
273}
274
275const char* Dbg::GetClassDescriptor(JDWP::RefTypeId id) {
276 UNIMPLEMENTED(FATAL);
277 return NULL;
278}
279
280JDWP::ObjectId Dbg::GetClassObject(JDWP::RefTypeId id) {
281 UNIMPLEMENTED(FATAL);
282 return 0;
283}
284
285JDWP::RefTypeId Dbg::GetSuperclass(JDWP::RefTypeId id) {
286 UNIMPLEMENTED(FATAL);
287 return 0;
288}
289
290JDWP::ObjectId Dbg::GetClassLoader(JDWP::RefTypeId id) {
291 UNIMPLEMENTED(FATAL);
292 return 0;
293}
294
295uint32_t Dbg::GetAccessFlags(JDWP::RefTypeId id) {
296 UNIMPLEMENTED(FATAL);
297 return 0;
298}
299
300bool Dbg::IsInterface(JDWP::RefTypeId id) {
301 UNIMPLEMENTED(FATAL);
302 return false;
303}
304
305void Dbg::GetClassList(uint32_t* pNumClasses, JDWP::RefTypeId** pClassRefBuf) {
306 UNIMPLEMENTED(FATAL);
307}
308
309void Dbg::GetVisibleClassList(JDWP::ObjectId classLoaderId, uint32_t* pNumClasses, JDWP::RefTypeId** pClassRefBuf) {
310 UNIMPLEMENTED(FATAL);
311}
312
313void Dbg::GetClassInfo(JDWP::RefTypeId classId, uint8_t* pTypeTag, uint32_t* pStatus, const char** pSignature) {
314 UNIMPLEMENTED(FATAL);
315}
316
317bool Dbg::FindLoadedClassBySignature(const char* classDescriptor, JDWP::RefTypeId* pRefTypeId) {
318 UNIMPLEMENTED(FATAL);
319 return false;
320}
321
322void Dbg::GetObjectType(JDWP::ObjectId objectId, uint8_t* pRefTypeTag, JDWP::RefTypeId* pRefTypeId) {
323 UNIMPLEMENTED(FATAL);
324}
325
326uint8_t Dbg::GetClassObjectType(JDWP::RefTypeId refTypeId) {
327 UNIMPLEMENTED(FATAL);
328 return 0;
329}
330
331const char* Dbg::GetSignature(JDWP::RefTypeId refTypeId) {
332 UNIMPLEMENTED(FATAL);
333 return NULL;
334}
335
336const char* Dbg::GetSourceFile(JDWP::RefTypeId refTypeId) {
337 UNIMPLEMENTED(FATAL);
338 return NULL;
339}
340
341const char* Dbg::GetObjectTypeName(JDWP::ObjectId objectId) {
342 UNIMPLEMENTED(FATAL);
343 return NULL;
344}
345
346uint8_t Dbg::GetObjectTag(JDWP::ObjectId objectId) {
347 UNIMPLEMENTED(FATAL);
348 return 0;
349}
350
351int Dbg::GetTagWidth(int tag) {
352 UNIMPLEMENTED(FATAL);
353 return 0;
354}
355
356int Dbg::GetArrayLength(JDWP::ObjectId arrayId) {
357 UNIMPLEMENTED(FATAL);
358 return 0;
359}
360
361uint8_t Dbg::GetArrayElementTag(JDWP::ObjectId arrayId) {
362 UNIMPLEMENTED(FATAL);
363 return 0;
364}
365
366bool Dbg::OutputArray(JDWP::ObjectId arrayId, int firstIndex, int count, JDWP::ExpandBuf* pReply) {
367 UNIMPLEMENTED(FATAL);
368 return false;
369}
370
371bool Dbg::SetArrayElements(JDWP::ObjectId arrayId, int firstIndex, int count, const uint8_t* buf) {
372 UNIMPLEMENTED(FATAL);
373 return false;
374}
375
376JDWP::ObjectId Dbg::CreateString(const char* str) {
377 UNIMPLEMENTED(FATAL);
378 return 0;
379}
380
381JDWP::ObjectId Dbg::CreateObject(JDWP::RefTypeId classId) {
382 UNIMPLEMENTED(FATAL);
383 return 0;
384}
385
386JDWP::ObjectId Dbg::CreateArrayObject(JDWP::RefTypeId arrayTypeId, uint32_t length) {
387 UNIMPLEMENTED(FATAL);
388 return 0;
389}
390
391bool Dbg::MatchType(JDWP::RefTypeId instClassId, JDWP::RefTypeId classId) {
392 UNIMPLEMENTED(FATAL);
393 return false;
394}
395
396const char* Dbg::GetMethodName(JDWP::RefTypeId refTypeId, JDWP::MethodId id) {
397 UNIMPLEMENTED(FATAL);
398 return NULL;
399}
400
401void Dbg::OutputAllFields(JDWP::RefTypeId refTypeId, bool withGeneric, JDWP::ExpandBuf* pReply) {
402 UNIMPLEMENTED(FATAL);
403}
404
405void Dbg::OutputAllMethods(JDWP::RefTypeId refTypeId, bool withGeneric, JDWP::ExpandBuf* pReply) {
406 UNIMPLEMENTED(FATAL);
407}
408
409void Dbg::OutputAllInterfaces(JDWP::RefTypeId refTypeId, JDWP::ExpandBuf* pReply) {
410 UNIMPLEMENTED(FATAL);
411}
412
413void Dbg::OutputLineTable(JDWP::RefTypeId refTypeId, JDWP::MethodId methodId, JDWP::ExpandBuf* pReply) {
414 UNIMPLEMENTED(FATAL);
415}
416
417void Dbg::OutputVariableTable(JDWP::RefTypeId refTypeId, JDWP::MethodId id, bool withGeneric, JDWP::ExpandBuf* pReply) {
418 UNIMPLEMENTED(FATAL);
419}
420
421uint8_t Dbg::GetFieldBasicTag(JDWP::ObjectId objId, JDWP::FieldId fieldId) {
422 UNIMPLEMENTED(FATAL);
423 return 0;
424}
425
426uint8_t Dbg::GetStaticFieldBasicTag(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId) {
427 UNIMPLEMENTED(FATAL);
428 return 0;
429}
430
431void Dbg::GetFieldValue(JDWP::ObjectId objectId, JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply) {
432 UNIMPLEMENTED(FATAL);
433}
434
435void Dbg::SetFieldValue(JDWP::ObjectId objectId, JDWP::FieldId fieldId, uint64_t value, int width) {
436 UNIMPLEMENTED(FATAL);
437}
438
439void Dbg::GetStaticFieldValue(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply) {
440 UNIMPLEMENTED(FATAL);
441}
442
443void Dbg::SetStaticFieldValue(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId, uint64_t rawValue, int width) {
444 UNIMPLEMENTED(FATAL);
445}
446
447char* Dbg::StringToUtf8(JDWP::ObjectId strId) {
448 UNIMPLEMENTED(FATAL);
449 return NULL;
450}
451
452char* Dbg::GetThreadName(JDWP::ObjectId threadId) {
453 UNIMPLEMENTED(FATAL);
454 return NULL;
455}
456
457JDWP::ObjectId Dbg::GetThreadGroup(JDWP::ObjectId threadId) {
458 UNIMPLEMENTED(FATAL);
459 return 0;
460}
461
462char* Dbg::GetThreadGroupName(JDWP::ObjectId threadGroupId) {
463 UNIMPLEMENTED(FATAL);
464 return NULL;
465}
466
467JDWP::ObjectId Dbg::GetThreadGroupParent(JDWP::ObjectId threadGroupId) {
468 UNIMPLEMENTED(FATAL);
469 return 0;
470}
471
472JDWP::ObjectId Dbg::GetSystemThreadGroupId() {
473 UNIMPLEMENTED(FATAL);
474 return 0;
475}
476
477JDWP::ObjectId Dbg::GetMainThreadGroupId() {
478 UNIMPLEMENTED(FATAL);
479 return 0;
480}
481
482bool Dbg::GetThreadStatus(JDWP::ObjectId threadId, uint32_t* threadStatus, uint32_t* suspendStatus) {
483 UNIMPLEMENTED(FATAL);
484 return false;
485}
486
487uint32_t Dbg::GetThreadSuspendCount(JDWP::ObjectId threadId) {
488 UNIMPLEMENTED(FATAL);
489 return 0;
490}
491
492bool Dbg::ThreadExists(JDWP::ObjectId threadId) {
493 UNIMPLEMENTED(FATAL);
494 return false;
495}
496
497bool Dbg::IsSuspended(JDWP::ObjectId threadId) {
498 UNIMPLEMENTED(FATAL);
499 return false;
500}
501
502//void Dbg::WaitForSuspend(JDWP::ObjectId threadId);
503
504void Dbg::GetThreadGroupThreads(JDWP::ObjectId threadGroupId, JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount) {
505 UNIMPLEMENTED(FATAL);
506}
507
508void Dbg::GetAllThreads(JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount) {
509 UNIMPLEMENTED(FATAL);
510}
511
512int Dbg::GetThreadFrameCount(JDWP::ObjectId threadId) {
513 UNIMPLEMENTED(FATAL);
514 return 0;
515}
516
517bool Dbg::GetThreadFrame(JDWP::ObjectId threadId, int num, JDWP::FrameId* pFrameId, JDWP::JdwpLocation* pLoc) {
518 UNIMPLEMENTED(FATAL);
519 return false;
520}
521
522JDWP::ObjectId Dbg::GetThreadSelfId() {
Elliott Hughes475fc232011-10-25 15:00:35 -0700523 return gRegistry->Add(Thread::Current()->GetPeer());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700524}
525
Elliott Hughes475fc232011-10-25 15:00:35 -0700526void Dbg::SuspendVM() {
527 Runtime::Current()->GetThreadList()->SuspendAll(true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700528}
529
530void Dbg::ResumeVM() {
Elliott Hughes475fc232011-10-25 15:00:35 -0700531 Runtime::Current()->GetThreadList()->ResumeAll(true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700532}
533
534void Dbg::SuspendThread(JDWP::ObjectId threadId) {
535 UNIMPLEMENTED(FATAL);
536}
537
538void Dbg::ResumeThread(JDWP::ObjectId threadId) {
539 UNIMPLEMENTED(FATAL);
540}
541
542void Dbg::SuspendSelf() {
Elliott Hughes475fc232011-10-25 15:00:35 -0700543 Runtime::Current()->GetThreadList()->SuspendSelfForDebugger();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700544}
545
546bool Dbg::GetThisObject(JDWP::ObjectId threadId, JDWP::FrameId frameId, JDWP::ObjectId* pThisId) {
547 UNIMPLEMENTED(FATAL);
548 return false;
549}
550
551void Dbg::GetLocalValue(JDWP::ObjectId threadId, JDWP::FrameId frameId, int slot, uint8_t tag, uint8_t* buf, int expectedLen) {
552 UNIMPLEMENTED(FATAL);
553}
554
555void Dbg::SetLocalValue(JDWP::ObjectId threadId, JDWP::FrameId frameId, int slot, uint8_t tag, uint64_t value, int width) {
556 UNIMPLEMENTED(FATAL);
557}
558
559void Dbg::PostLocationEvent(const Method* method, int pcOffset, Object* thisPtr, int eventFlags) {
560 UNIMPLEMENTED(FATAL);
561}
562
563void Dbg::PostException(void* throwFp, int throwRelPc, void* catchFp, int catchRelPc, Object* exception) {
564 UNIMPLEMENTED(FATAL);
565}
566
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700567void Dbg::PostClassPrepare(Class* c) {
568 UNIMPLEMENTED(FATAL);
569}
570
571bool Dbg::WatchLocation(const JDWP::JdwpLocation* pLoc) {
572 UNIMPLEMENTED(FATAL);
573 return false;
574}
575
576void Dbg::UnwatchLocation(const JDWP::JdwpLocation* pLoc) {
577 UNIMPLEMENTED(FATAL);
578}
579
580bool Dbg::ConfigureStep(JDWP::ObjectId threadId, JDWP::JdwpStepSize size, JDWP::JdwpStepDepth depth) {
581 UNIMPLEMENTED(FATAL);
582 return false;
583}
584
585void Dbg::UnconfigureStep(JDWP::ObjectId threadId) {
586 UNIMPLEMENTED(FATAL);
587}
588
589JDWP::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) {
590 UNIMPLEMENTED(FATAL);
591 return JDWP::ERR_NONE;
592}
593
594void Dbg::ExecuteMethod(DebugInvokeReq* pReq) {
595 UNIMPLEMENTED(FATAL);
596}
597
598void Dbg::RegisterObjectId(JDWP::ObjectId id) {
599 UNIMPLEMENTED(FATAL);
600}
601
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700602/*
603 * "buf" contains a full JDWP packet, possibly with multiple chunks. We
604 * need to process each, accumulate the replies, and ship the whole thing
605 * back.
606 *
607 * Returns "true" if we have a reply. The reply buffer is newly allocated,
608 * and includes the chunk type/length, followed by the data.
609 *
610 * TODO: we currently assume that the request and reply include a single
611 * chunk. If this becomes inconvenient we will need to adapt.
612 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700613bool Dbg::DdmHandlePacket(const uint8_t* buf, int dataLen, uint8_t** pReplyBuf, int* pReplyLen) {
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700614 CHECK_GE(dataLen, 0);
615
616 Thread* self = Thread::Current();
617 JNIEnv* env = self->GetJniEnv();
618
619 static jclass Chunk_class = env->FindClass("org/apache/harmony/dalvik/ddmc/Chunk");
620 static jclass DdmServer_class = env->FindClass("org/apache/harmony/dalvik/ddmc/DdmServer");
621 static jmethodID dispatch_mid = env->GetStaticMethodID(DdmServer_class, "dispatch",
622 "(I[BII)Lorg/apache/harmony/dalvik/ddmc/Chunk;");
623 static jfieldID data_fid = env->GetFieldID(Chunk_class, "data", "[B");
624 static jfieldID length_fid = env->GetFieldID(Chunk_class, "length", "I");
625 static jfieldID offset_fid = env->GetFieldID(Chunk_class, "offset", "I");
626 static jfieldID type_fid = env->GetFieldID(Chunk_class, "type", "I");
627
628 // Create a byte[] corresponding to 'buf'.
629 jbyteArray dataArray = env->NewByteArray(dataLen);
630 if (dataArray == NULL) {
631 LOG(WARNING) << "byte[] allocation failed: " << dataLen;
632 env->ExceptionClear();
633 return false;
634 }
635 env->SetByteArrayRegion(dataArray, 0, dataLen, reinterpret_cast<const jbyte*>(buf));
636
637 const int kChunkHdrLen = 8;
638
639 // Run through and find all chunks. [Currently just find the first.]
640 ScopedByteArrayRO contents(env, dataArray);
641 jint type = JDWP::get4BE(reinterpret_cast<const uint8_t*>(&contents[0]));
642 jint length = JDWP::get4BE(reinterpret_cast<const uint8_t*>(&contents[4]));
643 jint offset = kChunkHdrLen;
644 if (offset + length > dataLen) {
645 LOG(WARNING) << StringPrintf("bad chunk found (len=%u pktLen=%d)", length, dataLen);
646 return false;
647 }
648
649 // Call "private static Chunk dispatch(int type, byte[] data, int offset, int length)".
650 jobject chunk = env->CallStaticObjectMethod(DdmServer_class, dispatch_mid, type, dataArray, offset, length);
651 if (env->ExceptionCheck()) {
652 LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type);
653 env->ExceptionDescribe();
654 env->ExceptionClear();
655 return false;
656 }
657
658 if (chunk == NULL) {
659 return false;
660 }
661
662 /*
663 * Pull the pieces out of the chunk. We copy the results into a
664 * newly-allocated buffer that the caller can free. We don't want to
665 * continue using the Chunk object because nothing has a reference to it.
666 *
667 * We could avoid this by returning type/data/offset/length and having
668 * the caller be aware of the object lifetime issues, but that
669 * integrates the JDWP code more tightly into the VM, and doesn't work
670 * if we have responses for multiple chunks.
671 *
672 * So we're pretty much stuck with copying data around multiple times.
673 */
674 jbyteArray replyData = reinterpret_cast<jbyteArray>(env->GetObjectField(chunk, data_fid));
675 length = env->GetIntField(chunk, length_fid);
676 offset = env->GetIntField(chunk, offset_fid);
677 type = env->GetIntField(chunk, type_fid);
678
679 LOG(VERBOSE) << StringPrintf("DDM reply: type=0x%08x data=%p offset=%d length=%d", type, replyData, offset, length);
680 if (length == 0 || replyData == NULL) {
681 return false;
682 }
683
684 jsize replyLength = env->GetArrayLength(replyData);
685 if (offset + length > replyLength) {
686 LOG(WARNING) << StringPrintf("chunk off=%d len=%d exceeds reply array len %d", offset, length, replyLength);
687 return false;
688 }
689
690 uint8_t* reply = new uint8_t[length + kChunkHdrLen];
691 if (reply == NULL) {
692 LOG(WARNING) << "malloc failed: " << (length + kChunkHdrLen);
693 return false;
694 }
695 JDWP::set4BE(reply + 0, type);
696 JDWP::set4BE(reply + 4, length);
697 env->GetByteArrayRegion(replyData, offset, length, reinterpret_cast<jbyte*>(reply + kChunkHdrLen));
698
699 *pReplyBuf = reply;
700 *pReplyLen = length + kChunkHdrLen;
701
702 LOG(VERBOSE) << StringPrintf("dvmHandleDdm returning type=%.4s buf=%p len=%d", (char*) reply, reply, length);
703 return true;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700704}
705
Elliott Hughes47fce012011-10-25 18:37:19 -0700706void DdmBroadcast(bool connect) {
707 LOG(VERBOSE) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "...";
708
709 Thread* self = Thread::Current();
710 if (self->GetState() != Thread::kRunnable) {
711 LOG(ERROR) << "DDM broadcast in thread state " << self->GetState();
712 /* try anyway? */
713 }
714
715 JNIEnv* env = self->GetJniEnv();
716 static jclass DdmServer_class = env->FindClass("org/apache/harmony/dalvik/ddmc/DdmServer");
717 static jmethodID broadcast_mid = env->GetStaticMethodID(DdmServer_class, "broadcast", "(I)V");
718 jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/;
719 env->CallStaticVoidMethod(DdmServer_class, broadcast_mid, event);
720 if (env->ExceptionCheck()) {
721 LOG(ERROR) << "DdmServer.broadcast " << event << " failed";
722 env->ExceptionDescribe();
723 env->ExceptionClear();
724 }
725}
726
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700727void Dbg::DdmConnected() {
Elliott Hughes47fce012011-10-25 18:37:19 -0700728 DdmBroadcast(true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700729}
730
731void Dbg::DdmDisconnected() {
Elliott Hughes47fce012011-10-25 18:37:19 -0700732 DdmBroadcast(false);
733 gDdmThreadNotification = false;
734}
735
736/*
737 * Send a notification when a thread starts or stops.
738 *
739 * Because we broadcast the full set of threads when the notifications are
740 * first enabled, it's possible for "thread" to be actively executing.
741 */
742void DdmSendThreadNotification(Thread* t, bool started) {
743 if (!gDdmThreadNotification) {
744 return;
745 }
746
747 if (started) {
748 SirtRef<String> name(t->GetName());
749 size_t char_count = (name.get() != NULL) ? name->GetLength() : 0;
750
751 size_t byte_count = char_count*2 + sizeof(uint32_t)*2;
752 std::vector<uint8_t> buf(byte_count);
753 JDWP::set4BE(&buf[0], t->GetThinLockId());
754 JDWP::set4BE(&buf[4], char_count);
755 if (char_count > 0) {
756 // Copy the UTF-16 string, transforming to big-endian.
757 const jchar* src = name->GetCharArray()->GetData();
758 jchar* dst = reinterpret_cast<jchar*>(&buf[8]);
759 while (char_count--) {
760 JDWP::set2BE(reinterpret_cast<uint8_t*>(dst++), *src++);
761 }
762 }
763 Dbg::DdmSendChunk(CHUNK_TYPE("THCR"), buf.size(), &buf[0]);
764 } else {
765 uint8_t buf[4];
766 JDWP::set4BE(&buf[0], t->GetThinLockId());
767 Dbg::DdmSendChunk(CHUNK_TYPE("THDE"), 4, buf);
768 }
769}
770
771void DdmSendThreadStartCallback(Thread* t) {
772 DdmSendThreadNotification(t, true);
773}
774
775void Dbg::DdmSetThreadNotification(bool enable) {
776 // We lock the thread list to avoid sending duplicate events or missing
777 // a thread change. We should be okay holding this lock while sending
778 // the messages out. (We have to hold it while accessing a live thread.)
779 ThreadListLock lock;
780
781 gDdmThreadNotification = enable;
782 if (enable) {
783 Runtime::Current()->GetThreadList()->ForEach(DdmSendThreadStartCallback);
784 }
785}
786
787void PostThreadStartOrStop(Thread* t, bool is_start) {
788 if (gDebuggerActive) {
789 JDWP::ObjectId id = gRegistry->Add(t->GetPeer());
790 gJdwpState->PostThreadChange(id, is_start);
791 }
792 DdmSendThreadNotification(t, is_start);
793}
794
795void Dbg::PostThreadStart(Thread* t) {
796 PostThreadStartOrStop(t, true);
797}
798
799void Dbg::PostThreadDeath(Thread* t) {
800 PostThreadStartOrStop(t, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700801}
802
Elliott Hughes3bb81562011-10-21 18:52:59 -0700803void Dbg::DdmSendChunk(int type, size_t byte_count, const uint8_t* buf) {
804 CHECK(buf != NULL);
805 iovec vec[1];
806 vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(buf));
807 vec[0].iov_len = byte_count;
808 Dbg::DdmSendChunkV(type, vec, 1);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700809}
810
811void Dbg::DdmSendChunkV(int type, const struct iovec* iov, int iovcnt) {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700812 if (gJdwpState == NULL) {
813 LOG(VERBOSE) << "Debugger thread not active, ignoring DDM send: " << type;
814 } else {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700815 gJdwpState->DdmSendChunkV(type, iov, iovcnt);
Elliott Hughes3bb81562011-10-21 18:52:59 -0700816 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700817}
818
819} // namespace art