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