debugger.h revision 7934ac288acfb2552bb0b06ec1f61e5820d924a4
1/* 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/* 18 * Dalvik-specific side of debugger support. (The JDWP code is intended to 19 * be relatively generic.) 20 */ 21#ifndef ART_RUNTIME_DEBUGGER_H_ 22#define ART_RUNTIME_DEBUGGER_H_ 23 24#include <pthread.h> 25 26#include <string> 27 28#include "jdwp/jdwp.h" 29#include "jni.h" 30#include "jvalue.h" 31#include "root_visitor.h" 32 33namespace art { 34namespace mirror { 35class AbstractMethod; 36class Class; 37class Object; 38class Throwable; 39} // namespace mirror 40struct AllocRecord; 41class Thread; 42class ThrowLocation; 43 44/* 45 * Invoke-during-breakpoint support. 46 */ 47struct DebugInvokeReq { 48 DebugInvokeReq() 49 : ready(false), invoke_needed_(false), 50 receiver_(NULL), thread_(NULL), class_(NULL), method_(NULL), 51 arg_count_(0), arg_values_(NULL), options_(0), error(JDWP::ERR_NONE), 52 result_tag(JDWP::JT_VOID), exception(0), 53 lock_("a DebugInvokeReq lock", kBreakpointInvokeLock), 54 cond_("a DebugInvokeReq condition variable", lock_) { 55 } 56 57 /* boolean; only set when we're in the tail end of an event handler */ 58 bool ready; 59 60 /* boolean; set if the JDWP thread wants this thread to do work */ 61 bool invoke_needed_; 62 63 /* request */ 64 mirror::Object* receiver_; /* not used for ClassType.InvokeMethod */ 65 mirror::Object* thread_; 66 mirror::Class* class_; 67 mirror::AbstractMethod* method_; 68 uint32_t arg_count_; 69 uint64_t* arg_values_; /* will be NULL if arg_count_ == 0 */ 70 uint32_t options_; 71 72 /* result */ 73 JDWP::JdwpError error; 74 JDWP::JdwpTag result_tag; 75 JValue result_value; 76 JDWP::ObjectId exception; 77 78 /* condition variable to wait on while the method executes */ 79 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 80 ConditionVariable cond_ GUARDED_BY(lock_); 81}; 82 83class Dbg { 84 public: 85 static bool ParseJdwpOptions(const std::string& options); 86 static void SetJdwpAllowed(bool allowed); 87 88 static void StartJdwp(); 89 static void StopJdwp(); 90 91 // Invoked by the GC in case we need to keep DDMS informed. 92 static void GcDidFinish() LOCKS_EXCLUDED(Locks::mutator_lock_); 93 94 // Return the DebugInvokeReq for the current thread. 95 static DebugInvokeReq* GetInvokeReq(); 96 97 static Thread* GetDebugThread(); 98 static void ClearWaitForEventThread(); 99 100 /* 101 * Enable/disable breakpoints and step modes. Used to provide a heads-up 102 * when the debugger attaches. 103 */ 104 static void Connected(); 105 static void GoActive() LOCKS_EXCLUDED(Locks::breakpoint_lock_, Locks::mutator_lock_); 106 static void Disconnected() LOCKS_EXCLUDED(Locks::mutator_lock_); 107 static void Disposed(); 108 109 // Returns true if we're actually debugging with a real debugger, false if it's 110 // just DDMS (or nothing at all). 111 static bool IsDebuggerActive(); 112 113 // Returns true if we had -Xrunjdwp or -agentlib:jdwp= on the command line. 114 static bool IsJdwpConfigured(); 115 116 static bool IsDisposed(); 117 118 /* 119 * Time, in milliseconds, since the last debugger activity. Does not 120 * include DDMS activity. Returns -1 if there has been no activity. 121 * Returns 0 if we're in the middle of handling a debugger request. 122 */ 123 static int64_t LastDebuggerActivity(); 124 125 static void UndoDebuggerSuspensions(); 126 127 /* 128 * Class, Object, Array 129 */ 130 static std::string GetClassName(JDWP::RefTypeId id) 131 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 132 static JDWP::JdwpError GetClassObject(JDWP::RefTypeId id, JDWP::ObjectId& class_object_id) 133 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 134 static JDWP::JdwpError GetSuperclass(JDWP::RefTypeId id, JDWP::RefTypeId& superclass_id) 135 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 136 static JDWP::JdwpError GetClassLoader(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) 137 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 138 static JDWP::JdwpError GetModifiers(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) 139 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 140 static JDWP::JdwpError GetReflectedType(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) 141 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 142 static void GetClassList(std::vector<JDWP::RefTypeId>& classes) 143 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 144 static JDWP::JdwpError GetClassInfo(JDWP::RefTypeId class_id, JDWP::JdwpTypeTag* pTypeTag, 145 uint32_t* pStatus, std::string* pDescriptor) 146 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 147 static void FindLoadedClassBySignature(const char* descriptor, std::vector<JDWP::RefTypeId>& ids) 148 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 149 static JDWP::JdwpError GetReferenceType(JDWP::ObjectId object_id, JDWP::ExpandBuf* pReply) 150 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 151 static JDWP::JdwpError GetSignature(JDWP::RefTypeId ref_type_id, std::string& signature) 152 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 153 static JDWP::JdwpError GetSourceFile(JDWP::RefTypeId ref_type_id, std::string& source_file) 154 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 155 static JDWP::JdwpError GetObjectTag(JDWP::ObjectId object_id, uint8_t& tag) 156 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 157 static size_t GetTagWidth(JDWP::JdwpTag tag); 158 159 static JDWP::JdwpError GetArrayLength(JDWP::ObjectId array_id, int& length) 160 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 161 static JDWP::JdwpError OutputArray(JDWP::ObjectId array_id, int offset, int count, 162 JDWP::ExpandBuf* pReply) 163 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 164 static JDWP::JdwpError SetArrayElements(JDWP::ObjectId array_id, int offset, int count, 165 JDWP::Request& request) 166 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 167 168 static JDWP::ObjectId CreateString(const std::string& str) 169 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 170 static JDWP::JdwpError CreateObject(JDWP::RefTypeId class_id, JDWP::ObjectId& new_object) 171 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 172 static JDWP::JdwpError CreateArrayObject(JDWP::RefTypeId array_class_id, uint32_t length, 173 JDWP::ObjectId& new_array) 174 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 175 176 static bool MatchType(JDWP::RefTypeId instance_class_id, JDWP::RefTypeId class_id) 177 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 178 179 // 180 // Monitors. 181 // 182 static JDWP::JdwpError GetMonitorInfo(JDWP::ObjectId object_id, JDWP::ExpandBuf* reply) 183 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 184 static JDWP::JdwpError GetOwnedMonitors(JDWP::ObjectId thread_id, 185 std::vector<JDWP::ObjectId>& monitors, 186 std::vector<uint32_t>& stack_depths) 187 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 188 static JDWP::JdwpError GetContendedMonitor(JDWP::ObjectId thread_id, JDWP::ObjectId& contended_monitor) 189 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 190 191 // 192 // Heap. 193 // 194 static JDWP::JdwpError GetInstanceCounts(const std::vector<JDWP::RefTypeId>& class_ids, 195 std::vector<uint64_t>& counts) 196 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 197 static JDWP::JdwpError GetInstances(JDWP::RefTypeId class_id, int32_t max_count, 198 std::vector<JDWP::ObjectId>& instances) 199 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 200 static JDWP::JdwpError GetReferringObjects(JDWP::ObjectId object_id, int32_t max_count, 201 std::vector<JDWP::ObjectId>& referring_objects) 202 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 203 static JDWP::JdwpError DisableCollection(JDWP::ObjectId object_id) 204 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 205 static JDWP::JdwpError EnableCollection(JDWP::ObjectId object_id) 206 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 207 static JDWP::JdwpError IsCollected(JDWP::ObjectId object_id, bool& is_collected) 208 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 209 static void DisposeObject(JDWP::ObjectId object_id, uint32_t reference_count) 210 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 211 212 // 213 // Methods and fields. 214 // 215 static std::string GetMethodName(JDWP::MethodId method_id) 216 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 217 static JDWP::JdwpError OutputDeclaredFields(JDWP::RefTypeId ref_type_id, bool with_generic, 218 JDWP::ExpandBuf* pReply) 219 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 220 static JDWP::JdwpError OutputDeclaredMethods(JDWP::RefTypeId ref_type_id, bool with_generic, 221 JDWP::ExpandBuf* pReply) 222 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 223 static JDWP::JdwpError OutputDeclaredInterfaces(JDWP::RefTypeId ref_type_id, 224 JDWP::ExpandBuf* pReply) 225 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 226 static void OutputLineTable(JDWP::RefTypeId ref_type_id, JDWP::MethodId method_id, 227 JDWP::ExpandBuf* pReply) 228 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 229 static void OutputVariableTable(JDWP::RefTypeId ref_type_id, JDWP::MethodId id, bool with_generic, 230 JDWP::ExpandBuf* pReply) 231 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 232 static JDWP::JdwpError GetBytecodes(JDWP::RefTypeId class_id, JDWP::MethodId method_id, 233 std::vector<uint8_t>& bytecodes) 234 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 235 236 static std::string GetFieldName(JDWP::FieldId field_id) 237 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 238 static JDWP::JdwpTag GetFieldBasicTag(JDWP::FieldId field_id) 239 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 240 static JDWP::JdwpTag GetStaticFieldBasicTag(JDWP::FieldId field_id) 241 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 242 static JDWP::JdwpError GetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id, 243 JDWP::ExpandBuf* pReply) 244 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 245 static JDWP::JdwpError SetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id, 246 uint64_t value, int width) 247 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 248 static JDWP::JdwpError GetStaticFieldValue(JDWP::RefTypeId ref_type_id, JDWP::FieldId field_id, 249 JDWP::ExpandBuf* pReply) 250 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 251 static JDWP::JdwpError SetStaticFieldValue(JDWP::FieldId field_id, uint64_t value, int width) 252 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 253 254 static std::string StringToUtf8(JDWP::ObjectId string_id) 255 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 256 257 /* 258 * Thread, ThreadGroup, Frame 259 */ 260 static JDWP::JdwpError GetThreadName(JDWP::ObjectId thread_id, std::string& name) 261 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) 262 LOCKS_EXCLUDED(Locks::thread_list_lock_); 263 static JDWP::JdwpError GetThreadGroup(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply); 264 static std::string GetThreadGroupName(JDWP::ObjectId thread_group_id); 265 static JDWP::ObjectId GetThreadGroupParent(JDWP::ObjectId thread_group_id) 266 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 267 static JDWP::ObjectId GetSystemThreadGroupId() 268 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 269 static JDWP::ObjectId GetMainThreadGroupId(); 270 271 static JDWP::JdwpError GetThreadStatus(JDWP::ObjectId thread_id, JDWP::JdwpThreadStatus* pThreadStatus, JDWP::JdwpSuspendStatus* pSuspendStatus); 272 static JDWP::JdwpError GetThreadDebugSuspendCount(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply); 273 // static void WaitForSuspend(JDWP::ObjectId thread_id); 274 275 // Fills 'thread_ids' with the threads in the given thread group. If thread_group_id == 0, 276 // returns all threads. 277 static void GetThreads(JDWP::ObjectId thread_group_id, std::vector<JDWP::ObjectId>& thread_ids) 278 LOCKS_EXCLUDED(Locks::thread_list_lock_) 279 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 280 static void GetChildThreadGroups(JDWP::ObjectId thread_group_id, std::vector<JDWP::ObjectId>& child_thread_group_ids); 281 282 static JDWP::JdwpError GetThreadFrameCount(JDWP::ObjectId thread_id, size_t& result); 283 static JDWP::JdwpError GetThreadFrames(JDWP::ObjectId thread_id, size_t start_frame, 284 size_t frame_count, JDWP::ExpandBuf* buf) 285 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 286 287 static JDWP::ObjectId GetThreadSelfId() 288 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 289 static void SuspendVM() 290 LOCKS_EXCLUDED(Locks::thread_list_lock_, 291 Locks::thread_suspend_count_lock_); 292 static void ResumeVM(); 293 static JDWP::JdwpError SuspendThread(JDWP::ObjectId thread_id, bool request_suspension = true) 294 LOCKS_EXCLUDED(Locks::mutator_lock_, 295 Locks::thread_list_lock_, 296 Locks::thread_suspend_count_lock_); 297 298 static void ResumeThread(JDWP::ObjectId thread_id) 299 LOCKS_EXCLUDED(Locks::thread_list_lock_, 300 Locks::thread_suspend_count_lock_) 301 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 302 static void SuspendSelf(); 303 304 static JDWP::JdwpError GetThisObject(JDWP::ObjectId thread_id, JDWP::FrameId frame_id, 305 JDWP::ObjectId* result) 306 LOCKS_EXCLUDED(Locks::thread_list_lock_, 307 Locks::thread_suspend_count_lock_) 308 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 309 static void GetLocalValue(JDWP::ObjectId thread_id, JDWP::FrameId frame_id, int slot, 310 JDWP::JdwpTag tag, uint8_t* buf, size_t expectedLen) 311 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 312 static void SetLocalValue(JDWP::ObjectId thread_id, JDWP::FrameId frame_id, int slot, 313 JDWP::JdwpTag tag, uint64_t value, size_t width) 314 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 315 316 static JDWP::JdwpError Interrupt(JDWP::ObjectId thread_id); 317 318 /* 319 * Debugger notification 320 */ 321 enum { 322 kBreakpoint = 0x01, 323 kSingleStep = 0x02, 324 kMethodEntry = 0x04, 325 kMethodExit = 0x08, 326 }; 327 static void PostLocationEvent(const mirror::AbstractMethod* method, int pcOffset, 328 mirror::Object* thisPtr, int eventFlags) 329 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 330 static void PostException(Thread* thread, const ThrowLocation& throw_location, 331 mirror::AbstractMethod* catch_method, 332 uint32_t catch_dex_pc, mirror::Throwable* exception) 333 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 334 static void PostThreadStart(Thread* t) 335 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 336 static void PostThreadDeath(Thread* t) 337 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 338 static void PostClassPrepare(mirror::Class* c) 339 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 340 341 static void UpdateDebugger(Thread* thread, mirror::Object* this_object, 342 const mirror::AbstractMethod* method, uint32_t new_dex_pc) 343 LOCKS_EXCLUDED(Locks::breakpoint_lock_) 344 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 345 346 static void WatchLocation(const JDWP::JdwpLocation* pLoc) 347 LOCKS_EXCLUDED(Locks::breakpoint_lock_) 348 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 349 static void UnwatchLocation(const JDWP::JdwpLocation* pLoc) 350 LOCKS_EXCLUDED(Locks::breakpoint_lock_) 351 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 352 static JDWP::JdwpError ConfigureStep(JDWP::ObjectId thread_id, JDWP::JdwpStepSize size, 353 JDWP::JdwpStepDepth depth) 354 LOCKS_EXCLUDED(Locks::breakpoint_lock_) 355 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 356 static void UnconfigureStep(JDWP::ObjectId thread_id) LOCKS_EXCLUDED(Locks::breakpoint_lock_); 357 358 static JDWP::JdwpError InvokeMethod(JDWP::ObjectId thread_id, JDWP::ObjectId object_id, 359 JDWP::RefTypeId class_id, JDWP::MethodId method_id, 360 uint32_t arg_count, uint64_t* arg_values, 361 JDWP::JdwpTag* arg_types, uint32_t options, 362 JDWP::JdwpTag* pResultTag, uint64_t* pResultValue, 363 JDWP::ObjectId* pExceptObj) 364 LOCKS_EXCLUDED(Locks::thread_list_lock_, 365 Locks::thread_suspend_count_lock_) 366 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 367 static void ExecuteMethod(DebugInvokeReq* pReq); 368 369 /* 370 * DDM support. 371 */ 372 static void DdmSendThreadNotification(Thread* t, uint32_t type) 373 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 374 static void DdmSetThreadNotification(bool enable); 375 static bool DdmHandlePacket(JDWP::Request& request, uint8_t** pReplyBuf, int* pReplyLen); 376 static void DdmConnected() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 377 static void DdmDisconnected() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 378 static void DdmSendChunk(uint32_t type, const std::vector<uint8_t>& bytes) 379 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 380 static void DdmSendChunk(uint32_t type, size_t len, const uint8_t* buf) 381 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 382 static void DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) 383 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 384 385 /* 386 * Recent allocation tracking support. 387 */ 388 static void RecordAllocation(mirror::Class* type, size_t byte_count) 389 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 390 static void SetAllocTrackingEnabled(bool enabled); 391 static inline bool IsAllocTrackingEnabled() { return recent_allocation_records_ != NULL; } 392 static jbyteArray GetRecentAllocations() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 393 static void DumpRecentAllocations(); 394 395 enum HpifWhen { 396 HPIF_WHEN_NEVER = 0, 397 HPIF_WHEN_NOW = 1, 398 HPIF_WHEN_NEXT_GC = 2, 399 HPIF_WHEN_EVERY_GC = 3 400 }; 401 static int DdmHandleHpifChunk(HpifWhen when) 402 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 403 404 enum HpsgWhen { 405 HPSG_WHEN_NEVER = 0, 406 HPSG_WHEN_EVERY_GC = 1, 407 }; 408 enum HpsgWhat { 409 HPSG_WHAT_MERGED_OBJECTS = 0, 410 HPSG_WHAT_DISTINCT_OBJECTS = 1, 411 }; 412 static bool DdmHandleHpsgNhsgChunk(HpsgWhen when, HpsgWhat what, bool native); 413 414 static void DdmSendHeapInfo(HpifWhen reason) 415 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 416 static void DdmSendHeapSegments(bool native) 417 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 418 419 private: 420 static void DdmBroadcast(bool connect) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 421 static void PostThreadStartOrStop(Thread*, uint32_t) 422 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 423 424 static AllocRecord* recent_allocation_records_; 425}; 426 427#define CHUNK_TYPE(_name) \ 428 static_cast<uint32_t>((_name)[0] << 24 | (_name)[1] << 16 | (_name)[2] << 8 | (_name)[3]) 429 430} // namespace art 431 432#endif // ART_RUNTIME_DEBUGGER_H_ 433