blob: 77c307c01d262a99aed84b59a5439629115a4b36 [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/*
18 * Dalvik-specific side of debugger support. (The JDWP code is intended to
19 * be relatively generic.)
20 */
21#ifndef ART_DEBUGGER_H_
22#define ART_DEBUGGER_H_
23
24#include <pthread.h>
25
Elliott Hughes3bb81562011-10-21 18:52:59 -070026#include <string>
27
Elliott Hughes872d4ec2011-10-21 17:07:15 -070028#include "jdwp/jdwp.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070029#include "object.h"
Elliott Hughes872d4ec2011-10-21 17:07:15 -070030
31namespace art {
32
Elliott Hughes545a0642011-11-08 19:10:03 -080033struct AllocRecord;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070034struct Thread;
35
36/*
37 * Invoke-during-breakpoint support.
38 */
39struct DebugInvokeReq {
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -080040 DebugInvokeReq()
41 : invoke_needed_(false),
42 lock_("a DebugInvokeReq lock"),
43 cond_("a DebugInvokeReq condition variable") {
Elliott Hughes475fc232011-10-25 15:00:35 -070044 }
45
Elliott Hughes872d4ec2011-10-21 17:07:15 -070046 /* boolean; only set when we're in the tail end of an event handler */
47 bool ready;
48
49 /* boolean; set if the JDWP thread wants this thread to do work */
Elliott Hughesd07986f2011-12-06 18:27:45 -080050 bool invoke_needed_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070051
52 /* request */
Elliott Hughesd07986f2011-12-06 18:27:45 -080053 Object* receiver_; /* not used for ClassType.InvokeMethod */
54 Object* thread_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070055 Class* class_;
Elliott Hughesd07986f2011-12-06 18:27:45 -080056 Method* method_;
Elliott Hughes45651fd2012-02-21 15:48:20 -080057 uint32_t arg_count_;
58 uint64_t* arg_values_; /* will be NULL if arg_count_ == 0 */
Elliott Hughesd07986f2011-12-06 18:27:45 -080059 uint32_t options_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070060
61 /* result */
Elliott Hughes475fc232011-10-25 15:00:35 -070062 JDWP::JdwpError error;
Elliott Hughesd07986f2011-12-06 18:27:45 -080063 JDWP::JdwpTag result_tag;
Elliott Hughes475fc232011-10-25 15:00:35 -070064 JValue result_value;
65 JDWP::ObjectId exception;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070066
67 /* condition variable to wait on while the method executes */
68 Mutex lock_;
69 ConditionVariable cond_;
70};
71
72class Dbg {
Elliott Hughesff17f1f2012-01-24 18:12:29 -080073 public:
Elliott Hughes3bb81562011-10-21 18:52:59 -070074 static bool ParseJdwpOptions(const std::string& options);
Elliott Hughes4ffd3132011-10-24 12:06:42 -070075 static void SetJdwpAllowed(bool allowed);
76
Elliott Hughesd1cc8362011-10-24 16:58:50 -070077 static void StartJdwp();
78 static void StopJdwp();
79
Elliott Hughes767a1472011-10-26 18:49:02 -070080 // Invoked by the GC in case we need to keep DDMS informed.
81 static void GcDidFinish();
82
Elliott Hughes872d4ec2011-10-21 17:07:15 -070083 // Return the DebugInvokeReq for the current thread.
84 static DebugInvokeReq* GetInvokeReq();
85
Elliott Hughes475fc232011-10-25 15:00:35 -070086 static Thread* GetDebugThread();
87 static void ClearWaitForEventThread();
88
Elliott Hughes872d4ec2011-10-21 17:07:15 -070089 /*
90 * Enable/disable breakpoints and step modes. Used to provide a heads-up
91 * when the debugger attaches.
92 */
93 static void Connected();
Elliott Hughesa2155262011-11-16 16:26:58 -080094 static void GoActive();
Elliott Hughes872d4ec2011-10-21 17:07:15 -070095 static void Disconnected();
Elliott Hughes86964332012-02-15 19:37:42 -080096 static void Disposed();
Elliott Hughes872d4ec2011-10-21 17:07:15 -070097
98 /*
99 * Returns "true" if a debugger is connected. Returns "false" if it's
100 * just DDM.
101 */
102 static bool IsDebuggerConnected();
103
104 static bool IsDebuggingEnabled();
105
Elliott Hughes86964332012-02-15 19:37:42 -0800106 static bool IsDisposed();
107
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700108 /*
109 * Time, in milliseconds, since the last debugger activity. Does not
110 * include DDMS activity. Returns -1 if there has been no activity.
111 * Returns 0 if we're in the middle of handling a debugger request.
112 */
113 static int64_t LastDebuggerActivity();
114
115 /*
116 * Block/allow GC depending on what we're doing. These return the old
117 * status, which can be fed to ThreadContinuing() to restore the previous
118 * mode.
119 */
120 static int ThreadRunning();
121 static int ThreadWaiting();
122 static int ThreadContinuing(int status);
123
124 static void UndoDebuggerSuspensions();
125
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700126 static void Exit(int status);
127
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700128 static void VisitRoots(Heap::RootVisitor* visitor, void* arg);
129
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700130 /*
131 * Class, Object, Array
132 */
Elliott Hughesc308a5d2012-02-16 17:12:06 -0800133 static std::string GetClassName(JDWP::RefTypeId id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800134 static JDWP::JdwpError GetClassObject(JDWP::RefTypeId id, JDWP::ObjectId& classObjectId);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800135 static JDWP::JdwpError GetSuperclass(JDWP::RefTypeId id, JDWP::RefTypeId& superclassId);
Elliott Hughes436e3722012-02-17 20:01:47 -0800136 static JDWP::JdwpError GetClassLoader(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply);
137 static JDWP::JdwpError GetModifiers(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply);
138 static JDWP::JdwpError GetReflectedType(JDWP::RefTypeId classId, JDWP::ExpandBuf* pReply);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800139 static void GetClassList(std::vector<JDWP::RefTypeId>& classes);
Elliott Hughes436e3722012-02-17 20:01:47 -0800140 static JDWP::JdwpError GetClassInfo(JDWP::RefTypeId classId, JDWP::JdwpTypeTag* pTypeTag, uint32_t* pStatus, std::string* pDescriptor);
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800141 static void FindLoadedClassBySignature(const char* descriptor, std::vector<JDWP::RefTypeId>& ids);
Elliott Hughes2435a572012-02-17 16:07:41 -0800142 static JDWP::JdwpError GetReferenceType(JDWP::ObjectId objectId, JDWP::ExpandBuf* pReply);
Elliott Hughes1fe7afb2012-02-13 17:23:03 -0800143 static JDWP::JdwpError GetSignature(JDWP::RefTypeId refTypeId, std::string& signature);
Elliott Hughes436e3722012-02-17 20:01:47 -0800144 static JDWP::JdwpError GetSourceFile(JDWP::RefTypeId refTypeId, std::string& source_file);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700145 static uint8_t GetObjectTag(JDWP::ObjectId objectId);
Elliott Hughesaed4be92011-12-02 16:16:23 -0800146 static size_t GetTagWidth(JDWP::JdwpTag tag);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700147
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800148 static JDWP::JdwpError GetArrayLength(JDWP::ObjectId arrayId, int& length);
149 static JDWP::JdwpError OutputArray(JDWP::ObjectId arrayId, int firstIndex, int count, JDWP::ExpandBuf* pReply);
150 static JDWP::JdwpError SetArrayElements(JDWP::ObjectId arrayId, int firstIndex, int count, const uint8_t* buf);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700151
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -0800152 static JDWP::ObjectId CreateString(const std::string& str);
Elliott Hughes436e3722012-02-17 20:01:47 -0800153 static JDWP::JdwpError CreateObject(JDWP::RefTypeId classId, JDWP::ObjectId& new_object);
154 static JDWP::JdwpError CreateArrayObject(JDWP::RefTypeId arrayTypeId, uint32_t length, JDWP::ObjectId& new_array);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700155
156 static bool MatchType(JDWP::RefTypeId instClassId, JDWP::RefTypeId classId);
157
158 /*
159 * Method and Field
160 */
Elliott Hughes03181a82011-11-17 17:22:21 -0800161 static std::string GetMethodName(JDWP::RefTypeId refTypeId, JDWP::MethodId id);
Elliott Hughes436e3722012-02-17 20:01:47 -0800162 static JDWP::JdwpError OutputDeclaredFields(JDWP::RefTypeId refTypeId, bool withGeneric, JDWP::ExpandBuf* pReply);
163 static JDWP::JdwpError OutputDeclaredMethods(JDWP::RefTypeId refTypeId, bool withGeneric, JDWP::ExpandBuf* pReply);
164 static JDWP::JdwpError OutputDeclaredInterfaces(JDWP::RefTypeId refTypeId, JDWP::ExpandBuf* pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700165 static void OutputLineTable(JDWP::RefTypeId refTypeId, JDWP::MethodId methodId, JDWP::ExpandBuf* pReply);
166 static void OutputVariableTable(JDWP::RefTypeId refTypeId, JDWP::MethodId id, bool withGeneric, JDWP::ExpandBuf* pReply);
167
Elliott Hughesaed4be92011-12-02 16:16:23 -0800168 static JDWP::JdwpTag GetFieldBasicTag(JDWP::FieldId fieldId);
169 static JDWP::JdwpTag GetStaticFieldBasicTag(JDWP::FieldId fieldId);
Elliott Hughes3f4d58f2012-02-18 20:05:37 -0800170 static JDWP::JdwpError GetFieldValue(JDWP::ObjectId objectId, JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800171 static JDWP::JdwpError SetFieldValue(JDWP::ObjectId objectId, JDWP::FieldId fieldId, uint64_t value, int width);
Elliott Hughes0cf74332012-02-23 23:14:00 -0800172 static JDWP::JdwpError GetStaticFieldValue(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply);
Elliott Hughes3d1ca6d2012-02-13 15:43:19 -0800173 static JDWP::JdwpError SetStaticFieldValue(JDWP::FieldId fieldId, uint64_t value, int width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700174
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800175 static std::string StringToUtf8(JDWP::ObjectId strId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700176
177 /*
178 * Thread, ThreadGroup, Frame
179 */
Elliott Hughesa2e54f62011-11-17 13:01:30 -0800180 static bool GetThreadName(JDWP::ObjectId threadId, std::string& name);
Elliott Hughes2435a572012-02-17 16:07:41 -0800181 static JDWP::JdwpError GetThreadGroup(JDWP::ObjectId threadId, JDWP::ExpandBuf* pReply);
Elliott Hughes499c5132011-11-17 14:55:11 -0800182 static std::string GetThreadGroupName(JDWP::ObjectId threadGroupId);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700183 static JDWP::ObjectId GetThreadGroupParent(JDWP::ObjectId threadGroupId);
184 static JDWP::ObjectId GetSystemThreadGroupId();
185 static JDWP::ObjectId GetMainThreadGroupId();
186
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800187 static bool GetThreadStatus(JDWP::ObjectId threadId, JDWP::JdwpThreadStatus* pThreadStatus, JDWP::JdwpSuspendStatus* pSuspendStatus);
Elliott Hughes2435a572012-02-17 16:07:41 -0800188 static JDWP::JdwpError GetThreadSuspendCount(JDWP::ObjectId threadId, JDWP::ExpandBuf* pReply);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700189 static bool ThreadExists(JDWP::ObjectId threadId);
190 static bool IsSuspended(JDWP::ObjectId threadId);
191 //static void WaitForSuspend(JDWP::ObjectId threadId);
192 static void GetThreadGroupThreads(JDWP::ObjectId threadGroupId, JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount);
193 static void GetAllThreads(JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount);
194 static int GetThreadFrameCount(JDWP::ObjectId threadId);
Elliott Hughes530fa002012-03-12 11:44:49 -0700195 static void GetThreadFrame(JDWP::ObjectId threadId, int num, JDWP::FrameId* pFrameId, JDWP::JdwpLocation* pLoc);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700196
197 static JDWP::ObjectId GetThreadSelfId();
Elliott Hughes475fc232011-10-25 15:00:35 -0700198 static void SuspendVM();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700199 static void ResumeVM();
200 static void SuspendThread(JDWP::ObjectId threadId);
201 static void ResumeThread(JDWP::ObjectId threadId);
202 static void SuspendSelf();
203
Elliott Hughes91bf6cd2012-02-14 17:27:48 -0800204 static void GetThisObject(JDWP::FrameId frameId, JDWP::ObjectId* pThisId);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800205 static void GetLocalValue(JDWP::ObjectId threadId, JDWP::FrameId frameId, int slot, JDWP::JdwpTag tag, uint8_t* buf, size_t expectedLen);
206 static void SetLocalValue(JDWP::ObjectId threadId, JDWP::FrameId frameId, int slot, JDWP::JdwpTag tag, uint64_t value, size_t width);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700207
208 /*
209 * Debugger notification
210 */
211 enum {
Elliott Hughes86964332012-02-15 19:37:42 -0800212 kBreakpoint = 0x01,
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700213 kSingleStep = 0x02,
214 kMethodEntry = 0x04,
215 kMethodExit = 0x08,
216 };
217 static void PostLocationEvent(const Method* method, int pcOffset, Object* thisPtr, int eventFlags);
Elliott Hughesd07986f2011-12-06 18:27:45 -0800218 static void PostException(Method** sp, Method* throwMethod, uintptr_t throwNativePc, Method* catchMethod, uintptr_t catchNativePc, Object* exception);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700219 static void PostThreadStart(Thread* t);
220 static void PostThreadDeath(Thread* t);
221 static void PostClassPrepare(Class* c);
222
Elliott Hughes91bf6cd2012-02-14 17:27:48 -0800223 static void UpdateDebugger(int32_t dex_pc, Thread* self, Method** sp);
224
Elliott Hughes86964332012-02-15 19:37:42 -0800225 static void WatchLocation(const JDWP::JdwpLocation* pLoc);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700226 static void UnwatchLocation(const JDWP::JdwpLocation* pLoc);
Elliott Hughes2435a572012-02-17 16:07:41 -0800227 static JDWP::JdwpError ConfigureStep(JDWP::ObjectId threadId, JDWP::JdwpStepSize size, JDWP::JdwpStepDepth depth);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700228 static void UnconfigureStep(JDWP::ObjectId threadId);
229
Elliott Hughes45651fd2012-02-21 15:48:20 -0800230 static JDWP::JdwpError InvokeMethod(JDWP::ObjectId threadId, JDWP::ObjectId objectId, JDWP::RefTypeId classId, JDWP::MethodId methodId, uint32_t arg_count, uint64_t* arg_values, JDWP::JdwpTag* arg_types, uint32_t options, JDWP::JdwpTag* pResultTag, uint64_t* pResultValue, JDWP::ObjectId* pExceptObj);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700231 static void ExecuteMethod(DebugInvokeReq* pReq);
232
233 /* perform "late registration" of an object ID */
234 static void RegisterObjectId(JDWP::ObjectId id);
235
236 /*
237 * DDM support.
238 */
Elliott Hughes82188472011-11-07 18:11:48 -0800239 static void DdmSendThreadNotification(Thread* t, uint32_t type);
Elliott Hughes47fce012011-10-25 18:37:19 -0700240 static void DdmSetThreadNotification(bool enable);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700241 static bool DdmHandlePacket(const uint8_t* buf, int dataLen, uint8_t** pReplyBuf, int* pReplyLen);
242 static void DdmConnected();
243 static void DdmDisconnected();
Elliott Hughes21f32d72011-11-09 17:44:13 -0800244 static void DdmSendChunk(uint32_t type, const std::vector<uint8_t>& bytes);
Elliott Hughes82188472011-11-07 18:11:48 -0800245 static void DdmSendChunk(uint32_t type, size_t len, const uint8_t* buf);
Elliott Hughescccd84f2011-12-05 16:51:54 -0800246 static void DdmSendChunkV(uint32_t type, const struct iovec* iov, int iov_count);
Elliott Hughes767a1472011-10-26 18:49:02 -0700247
Elliott Hughes545a0642011-11-08 19:10:03 -0800248 /*
249 * Recent allocation tracking support.
250 */
251 static void RecordAllocation(Class* type, size_t byte_count);
252 static void SetAllocTrackingEnabled(bool enabled);
253 static inline bool IsAllocTrackingEnabled() { return recent_allocation_records_ != NULL; }
254 static jbyteArray GetRecentAllocations();
255 static void DumpRecentAllocations();
256
Elliott Hughes767a1472011-10-26 18:49:02 -0700257 enum HpifWhen {
258 HPIF_WHEN_NEVER = 0,
259 HPIF_WHEN_NOW = 1,
260 HPIF_WHEN_NEXT_GC = 2,
261 HPIF_WHEN_EVERY_GC = 3
262 };
263 static int DdmHandleHpifChunk(HpifWhen when);
264
265 enum HpsgWhen {
266 HPSG_WHEN_NEVER = 0,
267 HPSG_WHEN_EVERY_GC = 1,
268 };
269 enum HpsgWhat {
270 HPSG_WHAT_MERGED_OBJECTS = 0,
271 HPSG_WHAT_DISTINCT_OBJECTS = 1,
272 };
273 static bool DdmHandleHpsgNhsgChunk(HpsgWhen when, HpsgWhat what, bool native);
274
Elliott Hughes7162ad92011-10-27 14:08:42 -0700275 static void DdmSendHeapInfo(HpifWhen reason);
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700276 static void DdmSendHeapSegments(bool native);
Elliott Hughes545a0642011-11-08 19:10:03 -0800277
278 private:
Elliott Hughesa2155262011-11-16 16:26:58 -0800279 static void DdmBroadcast(bool);
280 static void GetThreadGroupThreadsImpl(Object*, JDWP::ObjectId**, uint32_t*);
281 static void PostThreadStartOrStop(Thread*, uint32_t);
282
Elliott Hughes545a0642011-11-08 19:10:03 -0800283 static AllocRecord* recent_allocation_records_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700284};
285
286#define CHUNK_TYPE(_name) \
Elliott Hughes82188472011-11-07 18:11:48 -0800287 static_cast<uint32_t>((_name)[0] << 24 | (_name)[1] << 16 | (_name)[2] << 8 | (_name)[3])
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700288
289} // namespace art
290
291#endif // ART_DEBUGGER_H_