blob: 2d9924d788717f070e3f08dc1a802f371abb08f4 [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 Hughes475fc232011-10-25 15:00:35 -070082static ObjectRegistry* gRegistry = NULL;
83
Elliott Hughes3bb81562011-10-21 18:52:59 -070084/*
85 * Handle one of the JDWP name/value pairs.
86 *
87 * JDWP options are:
88 * help: if specified, show help message and bail
89 * transport: may be dt_socket or dt_shmem
90 * address: for dt_socket, "host:port", or just "port" when listening
91 * server: if "y", wait for debugger to attach; if "n", attach to debugger
92 * timeout: how long to wait for debugger to connect / listen
93 *
94 * Useful with server=n (these aren't supported yet):
95 * onthrow=<exception-name>: connect to debugger when exception thrown
96 * onuncaught=y|n: connect to debugger when uncaught exception thrown
97 * launch=<command-line>: launch the debugger itself
98 *
99 * The "transport" option is required, as is "address" if server=n.
100 */
101static bool ParseJdwpOption(const std::string& name, const std::string& value) {
102 if (name == "transport") {
103 if (value == "dt_socket") {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700104 gJdwpOptions.transport = JDWP::kJdwpTransportSocket;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700105 } else if (value == "dt_android_adb") {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700106 gJdwpOptions.transport = JDWP::kJdwpTransportAndroidAdb;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700107 } else {
108 LOG(ERROR) << "JDWP transport not supported: " << value;
109 return false;
110 }
111 } else if (name == "server") {
112 if (value == "n") {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700113 gJdwpOptions.server = false;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700114 } else if (value == "y") {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700115 gJdwpOptions.server = true;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700116 } else {
117 LOG(ERROR) << "JDWP option 'server' must be 'y' or 'n'";
118 return false;
119 }
120 } else if (name == "suspend") {
121 if (value == "n") {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700122 gJdwpOptions.suspend = false;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700123 } else if (value == "y") {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700124 gJdwpOptions.suspend = true;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700125 } else {
126 LOG(ERROR) << "JDWP option 'suspend' must be 'y' or 'n'";
127 return false;
128 }
129 } else if (name == "address") {
130 /* this is either <port> or <host>:<port> */
131 std::string port_string;
Elliott Hughes376a7a02011-10-24 18:35:55 -0700132 gJdwpOptions.host.clear();
Elliott Hughes3bb81562011-10-21 18:52:59 -0700133 std::string::size_type colon = value.find(':');
134 if (colon != std::string::npos) {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700135 gJdwpOptions.host = value.substr(0, colon);
Elliott Hughes3bb81562011-10-21 18:52:59 -0700136 port_string = value.substr(colon + 1);
137 } else {
138 port_string = value;
139 }
140 if (port_string.empty()) {
141 LOG(ERROR) << "JDWP address missing port: " << value;
142 return false;
143 }
144 char* end;
145 long port = strtol(port_string.c_str(), &end, 10);
146 if (*end != '\0') {
147 LOG(ERROR) << "JDWP address has junk in port field: " << value;
148 return false;
149 }
Elliott Hughes376a7a02011-10-24 18:35:55 -0700150 gJdwpOptions.port = port;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700151 } else if (name == "launch" || name == "onthrow" || name == "oncaught" || name == "timeout") {
152 /* valid but unsupported */
153 LOG(INFO) << "Ignoring JDWP option '" << name << "'='" << value << "'";
154 } else {
155 LOG(INFO) << "Ignoring unrecognized JDWP option '" << name << "'='" << value << "'";
156 }
157
158 return true;
159}
160
161/*
162 * Parse the latter half of a -Xrunjdwp/-agentlib:jdwp= string, e.g.:
163 * "transport=dt_socket,address=8000,server=y,suspend=n"
164 */
165bool Dbg::ParseJdwpOptions(const std::string& options) {
Elliott Hughes47fce012011-10-25 18:37:19 -0700166 LOG(VERBOSE) << "ParseJdwpOptions: " << options;
167
Elliott Hughes3bb81562011-10-21 18:52:59 -0700168 std::vector<std::string> pairs;
169 Split(options, ',', pairs);
170
171 for (size_t i = 0; i < pairs.size(); ++i) {
172 std::string::size_type equals = pairs[i].find('=');
173 if (equals == std::string::npos) {
174 LOG(ERROR) << "Can't parse JDWP option '" << pairs[i] << "' in '" << options << "'";
175 return false;
176 }
177 ParseJdwpOption(pairs[i].substr(0, equals), pairs[i].substr(equals + 1));
178 }
179
Elliott Hughes376a7a02011-10-24 18:35:55 -0700180 if (gJdwpOptions.transport == JDWP::kJdwpTransportUnknown) {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700181 LOG(ERROR) << "Must specify JDWP transport: " << options;
182 }
Elliott Hughes376a7a02011-10-24 18:35:55 -0700183 if (!gJdwpOptions.server && (gJdwpOptions.host.empty() || gJdwpOptions.port == 0)) {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700184 LOG(ERROR) << "Must specify JDWP host and port when server=n: " << options;
185 return false;
186 }
187
188 gJdwpConfigured = true;
189 return true;
190}
191
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700192void Dbg::StartJdwp() {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700193 if (!gJdwpAllowed || !gJdwpConfigured) {
194 // No JDWP for you!
195 return;
196 }
197
Elliott Hughes475fc232011-10-25 15:00:35 -0700198 CHECK(gRegistry == NULL);
199 gRegistry = new ObjectRegistry;
200
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700201 // Init JDWP if the debugger is enabled. This may connect out to a
202 // debugger, passively listen for a debugger, or block waiting for a
203 // debugger.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700204 gJdwpState = JDWP::JdwpState::Create(&gJdwpOptions);
205 if (gJdwpState == NULL) {
206 LOG(WARNING) << "debugger thread failed to initialize";
Elliott Hughes475fc232011-10-25 15:00:35 -0700207 return;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700208 }
209
210 // If a debugger has already attached, send the "welcome" message.
211 // This may cause us to suspend all threads.
Elliott Hughes376a7a02011-10-24 18:35:55 -0700212 if (gJdwpState->IsActive()) {
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700213 //ScopedThreadStateChange(Thread::Current(), Thread::kRunnable);
Elliott Hughes376a7a02011-10-24 18:35:55 -0700214 if (!gJdwpState->PostVMStart()) {
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700215 LOG(WARNING) << "failed to post 'start' message to debugger";
216 }
217 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700218}
219
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700220void Dbg::StopJdwp() {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700221 delete gJdwpState;
Elliott Hughes475fc232011-10-25 15:00:35 -0700222 delete gRegistry;
223 gRegistry = NULL;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700224}
225
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700226void Dbg::SetJdwpAllowed(bool allowed) {
227 gJdwpAllowed = allowed;
228}
229
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700230DebugInvokeReq* Dbg::GetInvokeReq() {
Elliott Hughes475fc232011-10-25 15:00:35 -0700231 return Thread::Current()->GetInvokeReq();
232}
233
234Thread* Dbg::GetDebugThread() {
235 return (gJdwpState != NULL) ? gJdwpState->GetDebugThread() : NULL;
236}
237
238void Dbg::ClearWaitForEventThread() {
239 gJdwpState->ClearWaitForEventThread();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700240}
241
242void Dbg::Connected() {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700243 CHECK(!gDebuggerConnected);
244 LOG(VERBOSE) << "JDWP has attached";
245 gDebuggerConnected = true;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700246}
247
248void Dbg::Active() {
249 UNIMPLEMENTED(FATAL);
250}
251
252void Dbg::Disconnected() {
Elliott Hughes234ab152011-10-26 14:02:26 -0700253 CHECK(gDebuggerConnected);
254
255 gDebuggerActive = false;
256
257 //dvmDisableAllSubMode(kSubModeDebuggerActive);
258
259 gRegistry->Clear();
260 gDebuggerConnected = false;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700261}
262
263bool Dbg::IsDebuggerConnected() {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700264 return gDebuggerActive;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700265}
266
267bool Dbg::IsDebuggingEnabled() {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700268 return gJdwpConfigured;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700269}
270
271int64_t Dbg::LastDebuggerActivity() {
272 UNIMPLEMENTED(WARNING);
273 return -1;
274}
275
276int Dbg::ThreadRunning() {
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700277 return static_cast<int>(Thread::Current()->SetState(Thread::kRunnable));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700278}
279
280int Dbg::ThreadWaiting() {
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700281 return static_cast<int>(Thread::Current()->SetState(Thread::kVmWait));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700282}
283
Elliott Hughes6ba581a2011-10-25 11:45:35 -0700284int Dbg::ThreadContinuing(int new_state) {
285 return static_cast<int>(Thread::Current()->SetState(static_cast<Thread::State>(new_state)));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700286}
287
288void Dbg::UndoDebuggerSuspensions() {
Elliott Hughes234ab152011-10-26 14:02:26 -0700289 Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700290}
291
292void Dbg::Exit(int status) {
293 UNIMPLEMENTED(FATAL);
294}
295
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700296void Dbg::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
297 if (gRegistry != NULL) {
298 gRegistry->VisitRoots(visitor, arg);
299 }
300}
301
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700302const char* Dbg::GetClassDescriptor(JDWP::RefTypeId id) {
303 UNIMPLEMENTED(FATAL);
304 return NULL;
305}
306
307JDWP::ObjectId Dbg::GetClassObject(JDWP::RefTypeId id) {
308 UNIMPLEMENTED(FATAL);
309 return 0;
310}
311
312JDWP::RefTypeId Dbg::GetSuperclass(JDWP::RefTypeId id) {
313 UNIMPLEMENTED(FATAL);
314 return 0;
315}
316
317JDWP::ObjectId Dbg::GetClassLoader(JDWP::RefTypeId id) {
318 UNIMPLEMENTED(FATAL);
319 return 0;
320}
321
322uint32_t Dbg::GetAccessFlags(JDWP::RefTypeId id) {
323 UNIMPLEMENTED(FATAL);
324 return 0;
325}
326
327bool Dbg::IsInterface(JDWP::RefTypeId id) {
328 UNIMPLEMENTED(FATAL);
329 return false;
330}
331
332void Dbg::GetClassList(uint32_t* pNumClasses, JDWP::RefTypeId** pClassRefBuf) {
333 UNIMPLEMENTED(FATAL);
334}
335
336void Dbg::GetVisibleClassList(JDWP::ObjectId classLoaderId, uint32_t* pNumClasses, JDWP::RefTypeId** pClassRefBuf) {
337 UNIMPLEMENTED(FATAL);
338}
339
340void Dbg::GetClassInfo(JDWP::RefTypeId classId, uint8_t* pTypeTag, uint32_t* pStatus, const char** pSignature) {
341 UNIMPLEMENTED(FATAL);
342}
343
344bool Dbg::FindLoadedClassBySignature(const char* classDescriptor, JDWP::RefTypeId* pRefTypeId) {
345 UNIMPLEMENTED(FATAL);
346 return false;
347}
348
349void Dbg::GetObjectType(JDWP::ObjectId objectId, uint8_t* pRefTypeTag, JDWP::RefTypeId* pRefTypeId) {
350 UNIMPLEMENTED(FATAL);
351}
352
353uint8_t Dbg::GetClassObjectType(JDWP::RefTypeId refTypeId) {
354 UNIMPLEMENTED(FATAL);
355 return 0;
356}
357
358const char* Dbg::GetSignature(JDWP::RefTypeId refTypeId) {
359 UNIMPLEMENTED(FATAL);
360 return NULL;
361}
362
363const char* Dbg::GetSourceFile(JDWP::RefTypeId refTypeId) {
364 UNIMPLEMENTED(FATAL);
365 return NULL;
366}
367
368const char* Dbg::GetObjectTypeName(JDWP::ObjectId objectId) {
369 UNIMPLEMENTED(FATAL);
370 return NULL;
371}
372
373uint8_t Dbg::GetObjectTag(JDWP::ObjectId objectId) {
374 UNIMPLEMENTED(FATAL);
375 return 0;
376}
377
378int Dbg::GetTagWidth(int tag) {
379 UNIMPLEMENTED(FATAL);
380 return 0;
381}
382
383int Dbg::GetArrayLength(JDWP::ObjectId arrayId) {
384 UNIMPLEMENTED(FATAL);
385 return 0;
386}
387
388uint8_t Dbg::GetArrayElementTag(JDWP::ObjectId arrayId) {
389 UNIMPLEMENTED(FATAL);
390 return 0;
391}
392
393bool Dbg::OutputArray(JDWP::ObjectId arrayId, int firstIndex, int count, JDWP::ExpandBuf* pReply) {
394 UNIMPLEMENTED(FATAL);
395 return false;
396}
397
398bool Dbg::SetArrayElements(JDWP::ObjectId arrayId, int firstIndex, int count, const uint8_t* buf) {
399 UNIMPLEMENTED(FATAL);
400 return false;
401}
402
403JDWP::ObjectId Dbg::CreateString(const char* str) {
404 UNIMPLEMENTED(FATAL);
405 return 0;
406}
407
408JDWP::ObjectId Dbg::CreateObject(JDWP::RefTypeId classId) {
409 UNIMPLEMENTED(FATAL);
410 return 0;
411}
412
413JDWP::ObjectId Dbg::CreateArrayObject(JDWP::RefTypeId arrayTypeId, uint32_t length) {
414 UNIMPLEMENTED(FATAL);
415 return 0;
416}
417
418bool Dbg::MatchType(JDWP::RefTypeId instClassId, JDWP::RefTypeId classId) {
419 UNIMPLEMENTED(FATAL);
420 return false;
421}
422
423const char* Dbg::GetMethodName(JDWP::RefTypeId refTypeId, JDWP::MethodId id) {
424 UNIMPLEMENTED(FATAL);
425 return NULL;
426}
427
428void Dbg::OutputAllFields(JDWP::RefTypeId refTypeId, bool withGeneric, JDWP::ExpandBuf* pReply) {
429 UNIMPLEMENTED(FATAL);
430}
431
432void Dbg::OutputAllMethods(JDWP::RefTypeId refTypeId, bool withGeneric, JDWP::ExpandBuf* pReply) {
433 UNIMPLEMENTED(FATAL);
434}
435
436void Dbg::OutputAllInterfaces(JDWP::RefTypeId refTypeId, JDWP::ExpandBuf* pReply) {
437 UNIMPLEMENTED(FATAL);
438}
439
440void Dbg::OutputLineTable(JDWP::RefTypeId refTypeId, JDWP::MethodId methodId, JDWP::ExpandBuf* pReply) {
441 UNIMPLEMENTED(FATAL);
442}
443
444void Dbg::OutputVariableTable(JDWP::RefTypeId refTypeId, JDWP::MethodId id, bool withGeneric, JDWP::ExpandBuf* pReply) {
445 UNIMPLEMENTED(FATAL);
446}
447
448uint8_t Dbg::GetFieldBasicTag(JDWP::ObjectId objId, JDWP::FieldId fieldId) {
449 UNIMPLEMENTED(FATAL);
450 return 0;
451}
452
453uint8_t Dbg::GetStaticFieldBasicTag(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId) {
454 UNIMPLEMENTED(FATAL);
455 return 0;
456}
457
458void Dbg::GetFieldValue(JDWP::ObjectId objectId, JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply) {
459 UNIMPLEMENTED(FATAL);
460}
461
462void Dbg::SetFieldValue(JDWP::ObjectId objectId, JDWP::FieldId fieldId, uint64_t value, int width) {
463 UNIMPLEMENTED(FATAL);
464}
465
466void Dbg::GetStaticFieldValue(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply) {
467 UNIMPLEMENTED(FATAL);
468}
469
470void Dbg::SetStaticFieldValue(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId, uint64_t rawValue, int width) {
471 UNIMPLEMENTED(FATAL);
472}
473
474char* Dbg::StringToUtf8(JDWP::ObjectId strId) {
475 UNIMPLEMENTED(FATAL);
476 return NULL;
477}
478
479char* Dbg::GetThreadName(JDWP::ObjectId threadId) {
480 UNIMPLEMENTED(FATAL);
481 return NULL;
482}
483
484JDWP::ObjectId Dbg::GetThreadGroup(JDWP::ObjectId threadId) {
485 UNIMPLEMENTED(FATAL);
486 return 0;
487}
488
489char* Dbg::GetThreadGroupName(JDWP::ObjectId threadGroupId) {
490 UNIMPLEMENTED(FATAL);
491 return NULL;
492}
493
494JDWP::ObjectId Dbg::GetThreadGroupParent(JDWP::ObjectId threadGroupId) {
495 UNIMPLEMENTED(FATAL);
496 return 0;
497}
498
499JDWP::ObjectId Dbg::GetSystemThreadGroupId() {
500 UNIMPLEMENTED(FATAL);
501 return 0;
502}
503
504JDWP::ObjectId Dbg::GetMainThreadGroupId() {
505 UNIMPLEMENTED(FATAL);
506 return 0;
507}
508
509bool Dbg::GetThreadStatus(JDWP::ObjectId threadId, uint32_t* threadStatus, uint32_t* suspendStatus) {
510 UNIMPLEMENTED(FATAL);
511 return false;
512}
513
514uint32_t Dbg::GetThreadSuspendCount(JDWP::ObjectId threadId) {
515 UNIMPLEMENTED(FATAL);
516 return 0;
517}
518
519bool Dbg::ThreadExists(JDWP::ObjectId threadId) {
520 UNIMPLEMENTED(FATAL);
521 return false;
522}
523
524bool Dbg::IsSuspended(JDWP::ObjectId threadId) {
525 UNIMPLEMENTED(FATAL);
526 return false;
527}
528
529//void Dbg::WaitForSuspend(JDWP::ObjectId threadId);
530
531void Dbg::GetThreadGroupThreads(JDWP::ObjectId threadGroupId, JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount) {
532 UNIMPLEMENTED(FATAL);
533}
534
535void Dbg::GetAllThreads(JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount) {
536 UNIMPLEMENTED(FATAL);
537}
538
539int Dbg::GetThreadFrameCount(JDWP::ObjectId threadId) {
540 UNIMPLEMENTED(FATAL);
541 return 0;
542}
543
544bool Dbg::GetThreadFrame(JDWP::ObjectId threadId, int num, JDWP::FrameId* pFrameId, JDWP::JdwpLocation* pLoc) {
545 UNIMPLEMENTED(FATAL);
546 return false;
547}
548
549JDWP::ObjectId Dbg::GetThreadSelfId() {
Elliott Hughes475fc232011-10-25 15:00:35 -0700550 return gRegistry->Add(Thread::Current()->GetPeer());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700551}
552
Elliott Hughes475fc232011-10-25 15:00:35 -0700553void Dbg::SuspendVM() {
554 Runtime::Current()->GetThreadList()->SuspendAll(true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700555}
556
557void Dbg::ResumeVM() {
Elliott Hughes475fc232011-10-25 15:00:35 -0700558 Runtime::Current()->GetThreadList()->ResumeAll(true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700559}
560
561void Dbg::SuspendThread(JDWP::ObjectId threadId) {
562 UNIMPLEMENTED(FATAL);
563}
564
565void Dbg::ResumeThread(JDWP::ObjectId threadId) {
566 UNIMPLEMENTED(FATAL);
567}
568
569void Dbg::SuspendSelf() {
Elliott Hughes475fc232011-10-25 15:00:35 -0700570 Runtime::Current()->GetThreadList()->SuspendSelfForDebugger();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700571}
572
573bool Dbg::GetThisObject(JDWP::ObjectId threadId, JDWP::FrameId frameId, JDWP::ObjectId* pThisId) {
574 UNIMPLEMENTED(FATAL);
575 return false;
576}
577
578void Dbg::GetLocalValue(JDWP::ObjectId threadId, JDWP::FrameId frameId, int slot, uint8_t tag, uint8_t* buf, int expectedLen) {
579 UNIMPLEMENTED(FATAL);
580}
581
582void Dbg::SetLocalValue(JDWP::ObjectId threadId, JDWP::FrameId frameId, int slot, uint8_t tag, uint64_t value, int width) {
583 UNIMPLEMENTED(FATAL);
584}
585
586void Dbg::PostLocationEvent(const Method* method, int pcOffset, Object* thisPtr, int eventFlags) {
587 UNIMPLEMENTED(FATAL);
588}
589
590void Dbg::PostException(void* throwFp, int throwRelPc, void* catchFp, int catchRelPc, Object* exception) {
591 UNIMPLEMENTED(FATAL);
592}
593
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700594void Dbg::PostClassPrepare(Class* c) {
595 UNIMPLEMENTED(FATAL);
596}
597
598bool Dbg::WatchLocation(const JDWP::JdwpLocation* pLoc) {
599 UNIMPLEMENTED(FATAL);
600 return false;
601}
602
603void Dbg::UnwatchLocation(const JDWP::JdwpLocation* pLoc) {
604 UNIMPLEMENTED(FATAL);
605}
606
607bool Dbg::ConfigureStep(JDWP::ObjectId threadId, JDWP::JdwpStepSize size, JDWP::JdwpStepDepth depth) {
608 UNIMPLEMENTED(FATAL);
609 return false;
610}
611
612void Dbg::UnconfigureStep(JDWP::ObjectId threadId) {
613 UNIMPLEMENTED(FATAL);
614}
615
616JDWP::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) {
617 UNIMPLEMENTED(FATAL);
618 return JDWP::ERR_NONE;
619}
620
621void Dbg::ExecuteMethod(DebugInvokeReq* pReq) {
622 UNIMPLEMENTED(FATAL);
623}
624
625void Dbg::RegisterObjectId(JDWP::ObjectId id) {
626 UNIMPLEMENTED(FATAL);
627}
628
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700629/*
630 * "buf" contains a full JDWP packet, possibly with multiple chunks. We
631 * need to process each, accumulate the replies, and ship the whole thing
632 * back.
633 *
634 * Returns "true" if we have a reply. The reply buffer is newly allocated,
635 * and includes the chunk type/length, followed by the data.
636 *
637 * TODO: we currently assume that the request and reply include a single
638 * chunk. If this becomes inconvenient we will need to adapt.
639 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700640bool Dbg::DdmHandlePacket(const uint8_t* buf, int dataLen, uint8_t** pReplyBuf, int* pReplyLen) {
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700641 CHECK_GE(dataLen, 0);
642
643 Thread* self = Thread::Current();
644 JNIEnv* env = self->GetJniEnv();
645
646 static jclass Chunk_class = env->FindClass("org/apache/harmony/dalvik/ddmc/Chunk");
647 static jclass DdmServer_class = env->FindClass("org/apache/harmony/dalvik/ddmc/DdmServer");
648 static jmethodID dispatch_mid = env->GetStaticMethodID(DdmServer_class, "dispatch",
649 "(I[BII)Lorg/apache/harmony/dalvik/ddmc/Chunk;");
650 static jfieldID data_fid = env->GetFieldID(Chunk_class, "data", "[B");
651 static jfieldID length_fid = env->GetFieldID(Chunk_class, "length", "I");
652 static jfieldID offset_fid = env->GetFieldID(Chunk_class, "offset", "I");
653 static jfieldID type_fid = env->GetFieldID(Chunk_class, "type", "I");
654
655 // Create a byte[] corresponding to 'buf'.
656 jbyteArray dataArray = env->NewByteArray(dataLen);
657 if (dataArray == NULL) {
658 LOG(WARNING) << "byte[] allocation failed: " << dataLen;
659 env->ExceptionClear();
660 return false;
661 }
662 env->SetByteArrayRegion(dataArray, 0, dataLen, reinterpret_cast<const jbyte*>(buf));
663
664 const int kChunkHdrLen = 8;
665
666 // Run through and find all chunks. [Currently just find the first.]
667 ScopedByteArrayRO contents(env, dataArray);
668 jint type = JDWP::get4BE(reinterpret_cast<const uint8_t*>(&contents[0]));
669 jint length = JDWP::get4BE(reinterpret_cast<const uint8_t*>(&contents[4]));
670 jint offset = kChunkHdrLen;
671 if (offset + length > dataLen) {
672 LOG(WARNING) << StringPrintf("bad chunk found (len=%u pktLen=%d)", length, dataLen);
673 return false;
674 }
675
676 // Call "private static Chunk dispatch(int type, byte[] data, int offset, int length)".
677 jobject chunk = env->CallStaticObjectMethod(DdmServer_class, dispatch_mid, type, dataArray, offset, length);
678 if (env->ExceptionCheck()) {
679 LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type);
680 env->ExceptionDescribe();
681 env->ExceptionClear();
682 return false;
683 }
684
685 if (chunk == NULL) {
686 return false;
687 }
688
689 /*
690 * Pull the pieces out of the chunk. We copy the results into a
691 * newly-allocated buffer that the caller can free. We don't want to
692 * continue using the Chunk object because nothing has a reference to it.
693 *
694 * We could avoid this by returning type/data/offset/length and having
695 * the caller be aware of the object lifetime issues, but that
696 * integrates the JDWP code more tightly into the VM, and doesn't work
697 * if we have responses for multiple chunks.
698 *
699 * So we're pretty much stuck with copying data around multiple times.
700 */
701 jbyteArray replyData = reinterpret_cast<jbyteArray>(env->GetObjectField(chunk, data_fid));
702 length = env->GetIntField(chunk, length_fid);
703 offset = env->GetIntField(chunk, offset_fid);
704 type = env->GetIntField(chunk, type_fid);
705
706 LOG(VERBOSE) << StringPrintf("DDM reply: type=0x%08x data=%p offset=%d length=%d", type, replyData, offset, length);
707 if (length == 0 || replyData == NULL) {
708 return false;
709 }
710
711 jsize replyLength = env->GetArrayLength(replyData);
712 if (offset + length > replyLength) {
713 LOG(WARNING) << StringPrintf("chunk off=%d len=%d exceeds reply array len %d", offset, length, replyLength);
714 return false;
715 }
716
717 uint8_t* reply = new uint8_t[length + kChunkHdrLen];
718 if (reply == NULL) {
719 LOG(WARNING) << "malloc failed: " << (length + kChunkHdrLen);
720 return false;
721 }
722 JDWP::set4BE(reply + 0, type);
723 JDWP::set4BE(reply + 4, length);
724 env->GetByteArrayRegion(replyData, offset, length, reinterpret_cast<jbyte*>(reply + kChunkHdrLen));
725
726 *pReplyBuf = reply;
727 *pReplyLen = length + kChunkHdrLen;
728
729 LOG(VERBOSE) << StringPrintf("dvmHandleDdm returning type=%.4s buf=%p len=%d", (char*) reply, reply, length);
730 return true;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700731}
732
Elliott Hughes47fce012011-10-25 18:37:19 -0700733void DdmBroadcast(bool connect) {
734 LOG(VERBOSE) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "...";
735
736 Thread* self = Thread::Current();
737 if (self->GetState() != Thread::kRunnable) {
738 LOG(ERROR) << "DDM broadcast in thread state " << self->GetState();
739 /* try anyway? */
740 }
741
742 JNIEnv* env = self->GetJniEnv();
743 static jclass DdmServer_class = env->FindClass("org/apache/harmony/dalvik/ddmc/DdmServer");
744 static jmethodID broadcast_mid = env->GetStaticMethodID(DdmServer_class, "broadcast", "(I)V");
745 jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/;
746 env->CallStaticVoidMethod(DdmServer_class, broadcast_mid, event);
747 if (env->ExceptionCheck()) {
748 LOG(ERROR) << "DdmServer.broadcast " << event << " failed";
749 env->ExceptionDescribe();
750 env->ExceptionClear();
751 }
752}
753
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700754void Dbg::DdmConnected() {
Elliott Hughes47fce012011-10-25 18:37:19 -0700755 DdmBroadcast(true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700756}
757
758void Dbg::DdmDisconnected() {
Elliott Hughes47fce012011-10-25 18:37:19 -0700759 DdmBroadcast(false);
760 gDdmThreadNotification = false;
761}
762
763/*
764 * Send a notification when a thread starts or stops.
765 *
766 * Because we broadcast the full set of threads when the notifications are
767 * first enabled, it's possible for "thread" to be actively executing.
768 */
769void DdmSendThreadNotification(Thread* t, bool started) {
770 if (!gDdmThreadNotification) {
771 return;
772 }
773
774 if (started) {
775 SirtRef<String> name(t->GetName());
776 size_t char_count = (name.get() != NULL) ? name->GetLength() : 0;
777
778 size_t byte_count = char_count*2 + sizeof(uint32_t)*2;
779 std::vector<uint8_t> buf(byte_count);
780 JDWP::set4BE(&buf[0], t->GetThinLockId());
781 JDWP::set4BE(&buf[4], char_count);
782 if (char_count > 0) {
783 // Copy the UTF-16 string, transforming to big-endian.
784 const jchar* src = name->GetCharArray()->GetData();
785 jchar* dst = reinterpret_cast<jchar*>(&buf[8]);
786 while (char_count--) {
787 JDWP::set2BE(reinterpret_cast<uint8_t*>(dst++), *src++);
788 }
789 }
790 Dbg::DdmSendChunk(CHUNK_TYPE("THCR"), buf.size(), &buf[0]);
791 } else {
792 uint8_t buf[4];
793 JDWP::set4BE(&buf[0], t->GetThinLockId());
794 Dbg::DdmSendChunk(CHUNK_TYPE("THDE"), 4, buf);
795 }
796}
797
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700798void DdmSendThreadStartCallback(Thread* t, void*) {
Elliott Hughes47fce012011-10-25 18:37:19 -0700799 DdmSendThreadNotification(t, true);
800}
801
802void Dbg::DdmSetThreadNotification(bool enable) {
803 // We lock the thread list to avoid sending duplicate events or missing
804 // a thread change. We should be okay holding this lock while sending
805 // the messages out. (We have to hold it while accessing a live thread.)
806 ThreadListLock lock;
807
808 gDdmThreadNotification = enable;
809 if (enable) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700810 Runtime::Current()->GetThreadList()->ForEach(DdmSendThreadStartCallback, NULL);
Elliott Hughes47fce012011-10-25 18:37:19 -0700811 }
812}
813
814void PostThreadStartOrStop(Thread* t, bool is_start) {
815 if (gDebuggerActive) {
816 JDWP::ObjectId id = gRegistry->Add(t->GetPeer());
817 gJdwpState->PostThreadChange(id, is_start);
818 }
819 DdmSendThreadNotification(t, is_start);
820}
821
822void Dbg::PostThreadStart(Thread* t) {
823 PostThreadStartOrStop(t, true);
824}
825
826void Dbg::PostThreadDeath(Thread* t) {
827 PostThreadStartOrStop(t, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700828}
829
Elliott Hughes3bb81562011-10-21 18:52:59 -0700830void Dbg::DdmSendChunk(int type, size_t byte_count, const uint8_t* buf) {
831 CHECK(buf != NULL);
832 iovec vec[1];
833 vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(buf));
834 vec[0].iov_len = byte_count;
835 Dbg::DdmSendChunkV(type, vec, 1);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700836}
837
838void Dbg::DdmSendChunkV(int type, const struct iovec* iov, int iovcnt) {
Elliott Hughes3bb81562011-10-21 18:52:59 -0700839 if (gJdwpState == NULL) {
840 LOG(VERBOSE) << "Debugger thread not active, ignoring DDM send: " << type;
841 } else {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700842 gJdwpState->DdmSendChunkV(type, iov, iovcnt);
Elliott Hughes3bb81562011-10-21 18:52:59 -0700843 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700844}
845
846} // namespace art