blob: 07c445e692435b37b186bed749f9eb9b4494e780 [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 Hughes475fc232011-10-25 15:00:35 -070022#include "thread_list.h"
23
Elliott Hughes872d4ec2011-10-21 17:07:15 -070024namespace art {
25
Elliott Hughes475fc232011-10-25 15:00:35 -070026class ObjectRegistry {
27 public:
28 ObjectRegistry() : lock_("ObjectRegistry lock") {
29 }
30
31 JDWP::ObjectId Add(Object* o) {
32 if (o == NULL) {
33 return 0;
34 }
35 JDWP::ObjectId id = static_cast<JDWP::ObjectId>(reinterpret_cast<uintptr_t>(o));
36 MutexLock mu(lock_);
37 map_[id] = o;
38 return id;
39 }
40
41 bool Contains(JDWP::ObjectId id) {
42 MutexLock mu(lock_);
43 return map_.find(id) != map_.end();
44 }
45
46 private:
47 Mutex lock_;
48 std::map<JDWP::ObjectId, Object*> map_;
49};
50
Elliott Hughes4ffd3132011-10-24 12:06:42 -070051// JDWP is allowed unless the Zygote forbids it.
52static bool gJdwpAllowed = true;
53
Elliott Hughes3bb81562011-10-21 18:52:59 -070054// Was there a -Xrunjdwp or -agent argument on the command-line?
55static bool gJdwpConfigured = false;
56
57// Broken-down JDWP options. (Only valid if gJdwpConfigured is true.)
Elliott Hughes376a7a02011-10-24 18:35:55 -070058static JDWP::JdwpOptions gJdwpOptions;
Elliott Hughes3bb81562011-10-21 18:52:59 -070059
60// Runtime JDWP state.
61static JDWP::JdwpState* gJdwpState = NULL;
62static bool gDebuggerConnected; // debugger or DDMS is connected.
63static bool gDebuggerActive; // debugger is making requests.
64
Elliott Hughes475fc232011-10-25 15:00:35 -070065static ObjectRegistry* gRegistry = NULL;
66
Elliott Hughes3bb81562011-10-21 18:52:59 -070067/*
68 * Handle one of the JDWP name/value pairs.
69 *
70 * JDWP options are:
71 * help: if specified, show help message and bail
72 * transport: may be dt_socket or dt_shmem
73 * address: for dt_socket, "host:port", or just "port" when listening
74 * server: if "y", wait for debugger to attach; if "n", attach to debugger
75 * timeout: how long to wait for debugger to connect / listen
76 *
77 * Useful with server=n (these aren't supported yet):
78 * onthrow=<exception-name>: connect to debugger when exception thrown
79 * onuncaught=y|n: connect to debugger when uncaught exception thrown
80 * launch=<command-line>: launch the debugger itself
81 *
82 * The "transport" option is required, as is "address" if server=n.
83 */
84static bool ParseJdwpOption(const std::string& name, const std::string& value) {
85 if (name == "transport") {
86 if (value == "dt_socket") {
Elliott Hughes376a7a02011-10-24 18:35:55 -070087 gJdwpOptions.transport = JDWP::kJdwpTransportSocket;
Elliott Hughes3bb81562011-10-21 18:52:59 -070088 } else if (value == "dt_android_adb") {
Elliott Hughes376a7a02011-10-24 18:35:55 -070089 gJdwpOptions.transport = JDWP::kJdwpTransportAndroidAdb;
Elliott Hughes3bb81562011-10-21 18:52:59 -070090 } else {
91 LOG(ERROR) << "JDWP transport not supported: " << value;
92 return false;
93 }
94 } else if (name == "server") {
95 if (value == "n") {
Elliott Hughes376a7a02011-10-24 18:35:55 -070096 gJdwpOptions.server = false;
Elliott Hughes3bb81562011-10-21 18:52:59 -070097 } else if (value == "y") {
Elliott Hughes376a7a02011-10-24 18:35:55 -070098 gJdwpOptions.server = true;
Elliott Hughes3bb81562011-10-21 18:52:59 -070099 } else {
100 LOG(ERROR) << "JDWP option 'server' must be 'y' or 'n'";
101 return false;
102 }
103 } else if (name == "suspend") {
104 if (value == "n") {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700105 gJdwpOptions.suspend = false;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700106 } else if (value == "y") {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700107 gJdwpOptions.suspend = true;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700108 } else {
109 LOG(ERROR) << "JDWP option 'suspend' must be 'y' or 'n'";
110 return false;
111 }
112 } else if (name == "address") {
113 /* this is either <port> or <host>:<port> */
114 std::string port_string;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700115 gJdwpOptions.host.clear();
Elliott Hughes3bb81562011-10-21 18:52:59 -0700116 std::string::size_type colon = value.find(':');
117 if (colon != std::string::npos) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700118 gJdwpOptions.host = value.substr(0, colon);
Elliott Hughes3bb81562011-10-21 18:52:59 -0700119 port_string = value.substr(colon + 1);
120 } else {
121 port_string = value;
122 }
123 if (port_string.empty()) {
124 LOG(ERROR) << "JDWP address missing port: " << value;
125 return false;
126 }
127 char* end;
128 long port = strtol(port_string.c_str(), &end, 10);
129 if (*end != '\0') {
130 LOG(ERROR) << "JDWP address has junk in port field: " << value;
131 return false;
132 }
Elliott Hughes376a7a02011-10-24 18:35:55 -0700133 gJdwpOptions.port = port;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700134 } else if (name == "launch" || name == "onthrow" || name == "oncaught" || name == "timeout") {
135 /* valid but unsupported */
136 LOG(INFO) << "Ignoring JDWP option '" << name << "'='" << value << "'";
137 } else {
138 LOG(INFO) << "Ignoring unrecognized JDWP option '" << name << "'='" << value << "'";
139 }
140
141 return true;
142}
143
144/*
145 * Parse the latter half of a -Xrunjdwp/-agentlib:jdwp= string, e.g.:
146 * "transport=dt_socket,address=8000,server=y,suspend=n"
147 */
148bool Dbg::ParseJdwpOptions(const std::string& options) {
149 std::vector<std::string> pairs;
150 Split(options, ',', pairs);
151
152 for (size_t i = 0; i < pairs.size(); ++i) {
153 std::string::size_type equals = pairs[i].find('=');
154 if (equals == std::string::npos) {
155 LOG(ERROR) << "Can't parse JDWP option '" << pairs[i] << "' in '" << options << "'";
156 return false;
157 }
158 ParseJdwpOption(pairs[i].substr(0, equals), pairs[i].substr(equals + 1));
159 }
160
Elliott Hughes376a7a02011-10-24 18:35:55 -0700161 if (gJdwpOptions.transport == JDWP::kJdwpTransportUnknown) {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700162 LOG(ERROR) << "Must specify JDWP transport: " << options;
163 }
Elliott Hughes376a7a02011-10-24 18:35:55 -0700164 if (!gJdwpOptions.server && (gJdwpOptions.host.empty() || gJdwpOptions.port == 0)) {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700165 LOG(ERROR) << "Must specify JDWP host and port when server=n: " << options;
166 return false;
167 }
168
169 gJdwpConfigured = true;
170 return true;
171}
172
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700173void Dbg::StartJdwp() {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700174 if (!gJdwpAllowed || !gJdwpConfigured) {
175 // No JDWP for you!
176 return;
177 }
178
Elliott Hughes475fc232011-10-25 15:00:35 -0700179 CHECK(gRegistry == NULL);
180 gRegistry = new ObjectRegistry;
181
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700182 // Init JDWP if the debugger is enabled. This may connect out to a
183 // debugger, passively listen for a debugger, or block waiting for a
184 // debugger.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700185 gJdwpState = JDWP::JdwpState::Create(&gJdwpOptions);
186 if (gJdwpState == NULL) {
187 LOG(WARNING) << "debugger thread failed to initialize";
Elliott Hughes475fc232011-10-25 15:00:35 -0700188 return;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700189 }
190
191 // If a debugger has already attached, send the "welcome" message.
192 // This may cause us to suspend all threads.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700193 if (gJdwpState->IsActive()) {
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700194 //ScopedThreadStateChange(Thread::Current(), Thread::kRunnable);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700195 if (!gJdwpState->PostVMStart()) {
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700196 LOG(WARNING) << "failed to post 'start' message to debugger";
197 }
198 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700199}
200
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700201void Dbg::StopJdwp() {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700202 delete gJdwpState;
Elliott Hughes475fc232011-10-25 15:00:35 -0700203 delete gRegistry;
204 gRegistry = NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700205}
206
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700207void Dbg::SetJdwpAllowed(bool allowed) {
208 gJdwpAllowed = allowed;
209}
210
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700211DebugInvokeReq* Dbg::GetInvokeReq() {
Elliott Hughes475fc232011-10-25 15:00:35 -0700212 return Thread::Current()->GetInvokeReq();
213}
214
215Thread* Dbg::GetDebugThread() {
216 return (gJdwpState != NULL) ? gJdwpState->GetDebugThread() : NULL;
217}
218
219void Dbg::ClearWaitForEventThread() {
220 gJdwpState->ClearWaitForEventThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700221}
222
223void Dbg::Connected() {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700224 CHECK(!gDebuggerConnected);
225 LOG(VERBOSE) << "JDWP has attached";
226 gDebuggerConnected = true;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700227}
228
229void Dbg::Active() {
230 UNIMPLEMENTED(FATAL);
231}
232
233void Dbg::Disconnected() {
234 UNIMPLEMENTED(FATAL);
235}
236
237bool Dbg::IsDebuggerConnected() {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700238 return gDebuggerActive;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700239}
240
241bool Dbg::IsDebuggingEnabled() {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700242 return gJdwpConfigured;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700243}
244
245int64_t Dbg::LastDebuggerActivity() {
246 UNIMPLEMENTED(WARNING);
247 return -1;
248}
249
250int Dbg::ThreadRunning() {
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700251 return static_cast<int>(Thread::Current()->SetState(Thread::kRunnable));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700252}
253
254int Dbg::ThreadWaiting() {
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700255 return static_cast<int>(Thread::Current()->SetState(Thread::kVmWait));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700256}
257
Elliott Hughes6ba581a2011-10-25 11:45:35 -0700258int Dbg::ThreadContinuing(int new_state) {
259 return static_cast<int>(Thread::Current()->SetState(static_cast<Thread::State>(new_state)));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700260}
261
262void Dbg::UndoDebuggerSuspensions() {
263 UNIMPLEMENTED(FATAL);
264}
265
266void Dbg::Exit(int status) {
267 UNIMPLEMENTED(FATAL);
268}
269
270const char* Dbg::GetClassDescriptor(JDWP::RefTypeId id) {
271 UNIMPLEMENTED(FATAL);
272 return NULL;
273}
274
275JDWP::ObjectId Dbg::GetClassObject(JDWP::RefTypeId id) {
276 UNIMPLEMENTED(FATAL);
277 return 0;
278}
279
280JDWP::RefTypeId Dbg::GetSuperclass(JDWP::RefTypeId id) {
281 UNIMPLEMENTED(FATAL);
282 return 0;
283}
284
285JDWP::ObjectId Dbg::GetClassLoader(JDWP::RefTypeId id) {
286 UNIMPLEMENTED(FATAL);
287 return 0;
288}
289
290uint32_t Dbg::GetAccessFlags(JDWP::RefTypeId id) {
291 UNIMPLEMENTED(FATAL);
292 return 0;
293}
294
295bool Dbg::IsInterface(JDWP::RefTypeId id) {
296 UNIMPLEMENTED(FATAL);
297 return false;
298}
299
300void Dbg::GetClassList(uint32_t* pNumClasses, JDWP::RefTypeId** pClassRefBuf) {
301 UNIMPLEMENTED(FATAL);
302}
303
304void Dbg::GetVisibleClassList(JDWP::ObjectId classLoaderId, uint32_t* pNumClasses, JDWP::RefTypeId** pClassRefBuf) {
305 UNIMPLEMENTED(FATAL);
306}
307
308void Dbg::GetClassInfo(JDWP::RefTypeId classId, uint8_t* pTypeTag, uint32_t* pStatus, const char** pSignature) {
309 UNIMPLEMENTED(FATAL);
310}
311
312bool Dbg::FindLoadedClassBySignature(const char* classDescriptor, JDWP::RefTypeId* pRefTypeId) {
313 UNIMPLEMENTED(FATAL);
314 return false;
315}
316
317void Dbg::GetObjectType(JDWP::ObjectId objectId, uint8_t* pRefTypeTag, JDWP::RefTypeId* pRefTypeId) {
318 UNIMPLEMENTED(FATAL);
319}
320
321uint8_t Dbg::GetClassObjectType(JDWP::RefTypeId refTypeId) {
322 UNIMPLEMENTED(FATAL);
323 return 0;
324}
325
326const char* Dbg::GetSignature(JDWP::RefTypeId refTypeId) {
327 UNIMPLEMENTED(FATAL);
328 return NULL;
329}
330
331const char* Dbg::GetSourceFile(JDWP::RefTypeId refTypeId) {
332 UNIMPLEMENTED(FATAL);
333 return NULL;
334}
335
336const char* Dbg::GetObjectTypeName(JDWP::ObjectId objectId) {
337 UNIMPLEMENTED(FATAL);
338 return NULL;
339}
340
341uint8_t Dbg::GetObjectTag(JDWP::ObjectId objectId) {
342 UNIMPLEMENTED(FATAL);
343 return 0;
344}
345
346int Dbg::GetTagWidth(int tag) {
347 UNIMPLEMENTED(FATAL);
348 return 0;
349}
350
351int Dbg::GetArrayLength(JDWP::ObjectId arrayId) {
352 UNIMPLEMENTED(FATAL);
353 return 0;
354}
355
356uint8_t Dbg::GetArrayElementTag(JDWP::ObjectId arrayId) {
357 UNIMPLEMENTED(FATAL);
358 return 0;
359}
360
361bool Dbg::OutputArray(JDWP::ObjectId arrayId, int firstIndex, int count, JDWP::ExpandBuf* pReply) {
362 UNIMPLEMENTED(FATAL);
363 return false;
364}
365
366bool Dbg::SetArrayElements(JDWP::ObjectId arrayId, int firstIndex, int count, const uint8_t* buf) {
367 UNIMPLEMENTED(FATAL);
368 return false;
369}
370
371JDWP::ObjectId Dbg::CreateString(const char* str) {
372 UNIMPLEMENTED(FATAL);
373 return 0;
374}
375
376JDWP::ObjectId Dbg::CreateObject(JDWP::RefTypeId classId) {
377 UNIMPLEMENTED(FATAL);
378 return 0;
379}
380
381JDWP::ObjectId Dbg::CreateArrayObject(JDWP::RefTypeId arrayTypeId, uint32_t length) {
382 UNIMPLEMENTED(FATAL);
383 return 0;
384}
385
386bool Dbg::MatchType(JDWP::RefTypeId instClassId, JDWP::RefTypeId classId) {
387 UNIMPLEMENTED(FATAL);
388 return false;
389}
390
391const char* Dbg::GetMethodName(JDWP::RefTypeId refTypeId, JDWP::MethodId id) {
392 UNIMPLEMENTED(FATAL);
393 return NULL;
394}
395
396void Dbg::OutputAllFields(JDWP::RefTypeId refTypeId, bool withGeneric, JDWP::ExpandBuf* pReply) {
397 UNIMPLEMENTED(FATAL);
398}
399
400void Dbg::OutputAllMethods(JDWP::RefTypeId refTypeId, bool withGeneric, JDWP::ExpandBuf* pReply) {
401 UNIMPLEMENTED(FATAL);
402}
403
404void Dbg::OutputAllInterfaces(JDWP::RefTypeId refTypeId, JDWP::ExpandBuf* pReply) {
405 UNIMPLEMENTED(FATAL);
406}
407
408void Dbg::OutputLineTable(JDWP::RefTypeId refTypeId, JDWP::MethodId methodId, JDWP::ExpandBuf* pReply) {
409 UNIMPLEMENTED(FATAL);
410}
411
412void Dbg::OutputVariableTable(JDWP::RefTypeId refTypeId, JDWP::MethodId id, bool withGeneric, JDWP::ExpandBuf* pReply) {
413 UNIMPLEMENTED(FATAL);
414}
415
416uint8_t Dbg::GetFieldBasicTag(JDWP::ObjectId objId, JDWP::FieldId fieldId) {
417 UNIMPLEMENTED(FATAL);
418 return 0;
419}
420
421uint8_t Dbg::GetStaticFieldBasicTag(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId) {
422 UNIMPLEMENTED(FATAL);
423 return 0;
424}
425
426void Dbg::GetFieldValue(JDWP::ObjectId objectId, JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply) {
427 UNIMPLEMENTED(FATAL);
428}
429
430void Dbg::SetFieldValue(JDWP::ObjectId objectId, JDWP::FieldId fieldId, uint64_t value, int width) {
431 UNIMPLEMENTED(FATAL);
432}
433
434void Dbg::GetStaticFieldValue(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply) {
435 UNIMPLEMENTED(FATAL);
436}
437
438void Dbg::SetStaticFieldValue(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId, uint64_t rawValue, int width) {
439 UNIMPLEMENTED(FATAL);
440}
441
442char* Dbg::StringToUtf8(JDWP::ObjectId strId) {
443 UNIMPLEMENTED(FATAL);
444 return NULL;
445}
446
447char* Dbg::GetThreadName(JDWP::ObjectId threadId) {
448 UNIMPLEMENTED(FATAL);
449 return NULL;
450}
451
452JDWP::ObjectId Dbg::GetThreadGroup(JDWP::ObjectId threadId) {
453 UNIMPLEMENTED(FATAL);
454 return 0;
455}
456
457char* Dbg::GetThreadGroupName(JDWP::ObjectId threadGroupId) {
458 UNIMPLEMENTED(FATAL);
459 return NULL;
460}
461
462JDWP::ObjectId Dbg::GetThreadGroupParent(JDWP::ObjectId threadGroupId) {
463 UNIMPLEMENTED(FATAL);
464 return 0;
465}
466
467JDWP::ObjectId Dbg::GetSystemThreadGroupId() {
468 UNIMPLEMENTED(FATAL);
469 return 0;
470}
471
472JDWP::ObjectId Dbg::GetMainThreadGroupId() {
473 UNIMPLEMENTED(FATAL);
474 return 0;
475}
476
477bool Dbg::GetThreadStatus(JDWP::ObjectId threadId, uint32_t* threadStatus, uint32_t* suspendStatus) {
478 UNIMPLEMENTED(FATAL);
479 return false;
480}
481
482uint32_t Dbg::GetThreadSuspendCount(JDWP::ObjectId threadId) {
483 UNIMPLEMENTED(FATAL);
484 return 0;
485}
486
487bool Dbg::ThreadExists(JDWP::ObjectId threadId) {
488 UNIMPLEMENTED(FATAL);
489 return false;
490}
491
492bool Dbg::IsSuspended(JDWP::ObjectId threadId) {
493 UNIMPLEMENTED(FATAL);
494 return false;
495}
496
497//void Dbg::WaitForSuspend(JDWP::ObjectId threadId);
498
499void Dbg::GetThreadGroupThreads(JDWP::ObjectId threadGroupId, JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount) {
500 UNIMPLEMENTED(FATAL);
501}
502
503void Dbg::GetAllThreads(JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount) {
504 UNIMPLEMENTED(FATAL);
505}
506
507int Dbg::GetThreadFrameCount(JDWP::ObjectId threadId) {
508 UNIMPLEMENTED(FATAL);
509 return 0;
510}
511
512bool Dbg::GetThreadFrame(JDWP::ObjectId threadId, int num, JDWP::FrameId* pFrameId, JDWP::JdwpLocation* pLoc) {
513 UNIMPLEMENTED(FATAL);
514 return false;
515}
516
517JDWP::ObjectId Dbg::GetThreadSelfId() {
Elliott Hughes475fc232011-10-25 15:00:35 -0700518 return gRegistry->Add(Thread::Current()->GetPeer());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700519}
520
Elliott Hughes475fc232011-10-25 15:00:35 -0700521void Dbg::SuspendVM() {
522 Runtime::Current()->GetThreadList()->SuspendAll(true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700523}
524
525void Dbg::ResumeVM() {
Elliott Hughes475fc232011-10-25 15:00:35 -0700526 Runtime::Current()->GetThreadList()->ResumeAll(true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700527}
528
529void Dbg::SuspendThread(JDWP::ObjectId threadId) {
530 UNIMPLEMENTED(FATAL);
531}
532
533void Dbg::ResumeThread(JDWP::ObjectId threadId) {
534 UNIMPLEMENTED(FATAL);
535}
536
537void Dbg::SuspendSelf() {
Elliott Hughes475fc232011-10-25 15:00:35 -0700538 Runtime::Current()->GetThreadList()->SuspendSelfForDebugger();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700539}
540
541bool Dbg::GetThisObject(JDWP::ObjectId threadId, JDWP::FrameId frameId, JDWP::ObjectId* pThisId) {
542 UNIMPLEMENTED(FATAL);
543 return false;
544}
545
546void Dbg::GetLocalValue(JDWP::ObjectId threadId, JDWP::FrameId frameId, int slot, uint8_t tag, uint8_t* buf, int expectedLen) {
547 UNIMPLEMENTED(FATAL);
548}
549
550void Dbg::SetLocalValue(JDWP::ObjectId threadId, JDWP::FrameId frameId, int slot, uint8_t tag, uint64_t value, int width) {
551 UNIMPLEMENTED(FATAL);
552}
553
554void Dbg::PostLocationEvent(const Method* method, int pcOffset, Object* thisPtr, int eventFlags) {
555 UNIMPLEMENTED(FATAL);
556}
557
558void Dbg::PostException(void* throwFp, int throwRelPc, void* catchFp, int catchRelPc, Object* exception) {
559 UNIMPLEMENTED(FATAL);
560}
561
562void Dbg::PostThreadStart(Thread* t) {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700563 if (!gDebuggerConnected) {
564 return;
565 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700566 UNIMPLEMENTED(WARNING);
567}
568
569void Dbg::PostThreadDeath(Thread* t) {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700570 if (!gDebuggerConnected) {
571 return;
572 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700573 UNIMPLEMENTED(WARNING);
574}
575
576void Dbg::PostClassPrepare(Class* c) {
577 UNIMPLEMENTED(FATAL);
578}
579
580bool Dbg::WatchLocation(const JDWP::JdwpLocation* pLoc) {
581 UNIMPLEMENTED(FATAL);
582 return false;
583}
584
585void Dbg::UnwatchLocation(const JDWP::JdwpLocation* pLoc) {
586 UNIMPLEMENTED(FATAL);
587}
588
589bool Dbg::ConfigureStep(JDWP::ObjectId threadId, JDWP::JdwpStepSize size, JDWP::JdwpStepDepth depth) {
590 UNIMPLEMENTED(FATAL);
591 return false;
592}
593
594void Dbg::UnconfigureStep(JDWP::ObjectId threadId) {
595 UNIMPLEMENTED(FATAL);
596}
597
598JDWP::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) {
599 UNIMPLEMENTED(FATAL);
600 return JDWP::ERR_NONE;
601}
602
603void Dbg::ExecuteMethod(DebugInvokeReq* pReq) {
604 UNIMPLEMENTED(FATAL);
605}
606
607void Dbg::RegisterObjectId(JDWP::ObjectId id) {
608 UNIMPLEMENTED(FATAL);
609}
610
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700611/*
612 * "buf" contains a full JDWP packet, possibly with multiple chunks. We
613 * need to process each, accumulate the replies, and ship the whole thing
614 * back.
615 *
616 * Returns "true" if we have a reply. The reply buffer is newly allocated,
617 * and includes the chunk type/length, followed by the data.
618 *
619 * TODO: we currently assume that the request and reply include a single
620 * chunk. If this becomes inconvenient we will need to adapt.
621 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700622bool Dbg::DdmHandlePacket(const uint8_t* buf, int dataLen, uint8_t** pReplyBuf, int* pReplyLen) {
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700623 CHECK_GE(dataLen, 0);
624
625 Thread* self = Thread::Current();
626 JNIEnv* env = self->GetJniEnv();
627
628 static jclass Chunk_class = env->FindClass("org/apache/harmony/dalvik/ddmc/Chunk");
629 static jclass DdmServer_class = env->FindClass("org/apache/harmony/dalvik/ddmc/DdmServer");
630 static jmethodID dispatch_mid = env->GetStaticMethodID(DdmServer_class, "dispatch",
631 "(I[BII)Lorg/apache/harmony/dalvik/ddmc/Chunk;");
632 static jfieldID data_fid = env->GetFieldID(Chunk_class, "data", "[B");
633 static jfieldID length_fid = env->GetFieldID(Chunk_class, "length", "I");
634 static jfieldID offset_fid = env->GetFieldID(Chunk_class, "offset", "I");
635 static jfieldID type_fid = env->GetFieldID(Chunk_class, "type", "I");
636
637 // Create a byte[] corresponding to 'buf'.
638 jbyteArray dataArray = env->NewByteArray(dataLen);
639 if (dataArray == NULL) {
640 LOG(WARNING) << "byte[] allocation failed: " << dataLen;
641 env->ExceptionClear();
642 return false;
643 }
644 env->SetByteArrayRegion(dataArray, 0, dataLen, reinterpret_cast<const jbyte*>(buf));
645
646 const int kChunkHdrLen = 8;
647
648 // Run through and find all chunks. [Currently just find the first.]
649 ScopedByteArrayRO contents(env, dataArray);
650 jint type = JDWP::get4BE(reinterpret_cast<const uint8_t*>(&contents[0]));
651 jint length = JDWP::get4BE(reinterpret_cast<const uint8_t*>(&contents[4]));
652 jint offset = kChunkHdrLen;
653 if (offset + length > dataLen) {
654 LOG(WARNING) << StringPrintf("bad chunk found (len=%u pktLen=%d)", length, dataLen);
655 return false;
656 }
657
658 // Call "private static Chunk dispatch(int type, byte[] data, int offset, int length)".
659 jobject chunk = env->CallStaticObjectMethod(DdmServer_class, dispatch_mid, type, dataArray, offset, length);
660 if (env->ExceptionCheck()) {
661 LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type);
662 env->ExceptionDescribe();
663 env->ExceptionClear();
664 return false;
665 }
666
667 if (chunk == NULL) {
668 return false;
669 }
670
671 /*
672 * Pull the pieces out of the chunk. We copy the results into a
673 * newly-allocated buffer that the caller can free. We don't want to
674 * continue using the Chunk object because nothing has a reference to it.
675 *
676 * We could avoid this by returning type/data/offset/length and having
677 * the caller be aware of the object lifetime issues, but that
678 * integrates the JDWP code more tightly into the VM, and doesn't work
679 * if we have responses for multiple chunks.
680 *
681 * So we're pretty much stuck with copying data around multiple times.
682 */
683 jbyteArray replyData = reinterpret_cast<jbyteArray>(env->GetObjectField(chunk, data_fid));
684 length = env->GetIntField(chunk, length_fid);
685 offset = env->GetIntField(chunk, offset_fid);
686 type = env->GetIntField(chunk, type_fid);
687
688 LOG(VERBOSE) << StringPrintf("DDM reply: type=0x%08x data=%p offset=%d length=%d", type, replyData, offset, length);
689 if (length == 0 || replyData == NULL) {
690 return false;
691 }
692
693 jsize replyLength = env->GetArrayLength(replyData);
694 if (offset + length > replyLength) {
695 LOG(WARNING) << StringPrintf("chunk off=%d len=%d exceeds reply array len %d", offset, length, replyLength);
696 return false;
697 }
698
699 uint8_t* reply = new uint8_t[length + kChunkHdrLen];
700 if (reply == NULL) {
701 LOG(WARNING) << "malloc failed: " << (length + kChunkHdrLen);
702 return false;
703 }
704 JDWP::set4BE(reply + 0, type);
705 JDWP::set4BE(reply + 4, length);
706 env->GetByteArrayRegion(replyData, offset, length, reinterpret_cast<jbyte*>(reply + kChunkHdrLen));
707
708 *pReplyBuf = reply;
709 *pReplyLen = length + kChunkHdrLen;
710
711 LOG(VERBOSE) << StringPrintf("dvmHandleDdm returning type=%.4s buf=%p len=%d", (char*) reply, reply, length);
712 return true;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700713}
714
715void Dbg::DdmConnected() {
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700716 LOG(VERBOSE) << "Broadcasting DDM connect...";
717 //broadcast(CONNECTED);
718 UNIMPLEMENTED(WARNING);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700719}
720
721void Dbg::DdmDisconnected() {
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700722 LOG(VERBOSE) << "Broadcasting DDM disconnect...";
723 //broadcast(DISCONNECTED);
724 //gDvm.ddmThreadNotification = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700725}
726
Elliott Hughes3bb81562011-10-21 18:52:59 -0700727void Dbg::DdmSendChunk(int type, size_t byte_count, const uint8_t* buf) {
728 CHECK(buf != NULL);
729 iovec vec[1];
730 vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(buf));
731 vec[0].iov_len = byte_count;
732 Dbg::DdmSendChunkV(type, vec, 1);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700733}
734
735void Dbg::DdmSendChunkV(int type, const struct iovec* iov, int iovcnt) {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700736 if (gJdwpState == NULL) {
737 LOG(VERBOSE) << "Debugger thread not active, ignoring DDM send: " << type;
738 } else {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700739 gJdwpState->DdmSendChunkV(type, iov, iovcnt);
Elliott Hughes3bb81562011-10-21 18:52:59 -0700740 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700741}
742
743} // namespace art