blob: 0aa2c83bee48a72806b9cf6c6a4e610ca2c05371 [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 Hughes475fc232011-10-25 15:00:35 -070040 DebugInvokeReq() : lock_("a DebugInvokeReq lock"), cond_("a DebugInvokeReq condition variable") {
41 }
42
Elliott Hughes872d4ec2011-10-21 17:07:15 -070043 /* boolean; only set when we're in the tail end of an event handler */
44 bool ready;
45
46 /* boolean; set if the JDWP thread wants this thread to do work */
Elliott Hughes475fc232011-10-25 15:00:35 -070047 bool invoke_needed;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070048
49 /* request */
50 Object* obj; /* not used for ClassType.InvokeMethod */
51 Object* thread;
52 Class* class_;
53 Method* method;
Elliott Hughes475fc232011-10-25 15:00:35 -070054 uint32_t num_args;
55 uint64_t* arg_array; /* will be NULL if numArgs==0 */
Elliott Hughes872d4ec2011-10-21 17:07:15 -070056 uint32_t options;
57
58 /* result */
Elliott Hughes475fc232011-10-25 15:00:35 -070059 JDWP::JdwpError error;
60 uint8_t result_tag;
61 JValue result_value;
62 JDWP::ObjectId exception;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070063
64 /* condition variable to wait on while the method executes */
65 Mutex lock_;
66 ConditionVariable cond_;
67};
68
69class Dbg {
70public:
Elliott Hughes3bb81562011-10-21 18:52:59 -070071 static bool ParseJdwpOptions(const std::string& options);
Elliott Hughes4ffd3132011-10-24 12:06:42 -070072 static void SetJdwpAllowed(bool allowed);
73
Elliott Hughesd1cc8362011-10-24 16:58:50 -070074 static void StartJdwp();
75 static void StopJdwp();
76
Elliott Hughes767a1472011-10-26 18:49:02 -070077 // Invoked by the GC in case we need to keep DDMS informed.
78 static void GcDidFinish();
79
Elliott Hughes872d4ec2011-10-21 17:07:15 -070080 // Return the DebugInvokeReq for the current thread.
81 static DebugInvokeReq* GetInvokeReq();
82
Elliott Hughes475fc232011-10-25 15:00:35 -070083 static Thread* GetDebugThread();
84 static void ClearWaitForEventThread();
85
Elliott Hughes872d4ec2011-10-21 17:07:15 -070086 /*
87 * Enable/disable breakpoints and step modes. Used to provide a heads-up
88 * when the debugger attaches.
89 */
90 static void Connected();
91 static void Active();
92 static void Disconnected();
93
94 /*
95 * Returns "true" if a debugger is connected. Returns "false" if it's
96 * just DDM.
97 */
98 static bool IsDebuggerConnected();
99
100 static bool IsDebuggingEnabled();
101
102 /*
103 * Time, in milliseconds, since the last debugger activity. Does not
104 * include DDMS activity. Returns -1 if there has been no activity.
105 * Returns 0 if we're in the middle of handling a debugger request.
106 */
107 static int64_t LastDebuggerActivity();
108
109 /*
110 * Block/allow GC depending on what we're doing. These return the old
111 * status, which can be fed to ThreadContinuing() to restore the previous
112 * mode.
113 */
114 static int ThreadRunning();
115 static int ThreadWaiting();
116 static int ThreadContinuing(int status);
117
118 static void UndoDebuggerSuspensions();
119
120 // The debugger wants the VM to exit.
121 static void Exit(int status);
122
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700123 static void VisitRoots(Heap::RootVisitor* visitor, void* arg);
124
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700125 /*
126 * Class, Object, Array
127 */
128 static const char* GetClassDescriptor(JDWP::RefTypeId id);
129 static JDWP::ObjectId GetClassObject(JDWP::RefTypeId id);
130 static JDWP::RefTypeId GetSuperclass(JDWP::RefTypeId id);
131 static JDWP::ObjectId GetClassLoader(JDWP::RefTypeId id);
132 static uint32_t GetAccessFlags(JDWP::RefTypeId id);
133 static bool IsInterface(JDWP::RefTypeId id);
134 static void GetClassList(uint32_t* pNumClasses, JDWP::RefTypeId** pClassRefBuf);
135 static void GetVisibleClassList(JDWP::ObjectId classLoaderId, uint32_t* pNumClasses, JDWP::RefTypeId** pClassRefBuf);
136 static void GetClassInfo(JDWP::RefTypeId classId, uint8_t* pTypeTag, uint32_t* pStatus, const char** pSignature);
137 static bool FindLoadedClassBySignature(const char* classDescriptor, JDWP::RefTypeId* pRefTypeId);
138 static void GetObjectType(JDWP::ObjectId objectId, uint8_t* pRefTypeTag, JDWP::RefTypeId* pRefTypeId);
139 static uint8_t GetClassObjectType(JDWP::RefTypeId refTypeId);
140 static const char* GetSignature(JDWP::RefTypeId refTypeId);
141 static const char* GetSourceFile(JDWP::RefTypeId refTypeId);
142 static const char* GetObjectTypeName(JDWP::ObjectId objectId);
143 static uint8_t GetObjectTag(JDWP::ObjectId objectId);
144 static int GetTagWidth(int tag);
145
146 static int GetArrayLength(JDWP::ObjectId arrayId);
147 static uint8_t GetArrayElementTag(JDWP::ObjectId arrayId);
148 static bool OutputArray(JDWP::ObjectId arrayId, int firstIndex, int count, JDWP::ExpandBuf* pReply);
149 static bool SetArrayElements(JDWP::ObjectId arrayId, int firstIndex, int count, const uint8_t* buf);
150
151 static JDWP::ObjectId CreateString(const char* str);
152 static JDWP::ObjectId CreateObject(JDWP::RefTypeId classId);
153 static JDWP::ObjectId CreateArrayObject(JDWP::RefTypeId arrayTypeId, uint32_t length);
154
155 static bool MatchType(JDWP::RefTypeId instClassId, JDWP::RefTypeId classId);
156
157 /*
158 * Method and Field
159 */
160 static const char* GetMethodName(JDWP::RefTypeId refTypeId, JDWP::MethodId id);
161 static void OutputAllFields(JDWP::RefTypeId refTypeId, bool withGeneric, JDWP::ExpandBuf* pReply);
162 static void OutputAllMethods(JDWP::RefTypeId refTypeId, bool withGeneric, JDWP::ExpandBuf* pReply);
163 static void OutputAllInterfaces(JDWP::RefTypeId refTypeId, JDWP::ExpandBuf* pReply);
164 static void OutputLineTable(JDWP::RefTypeId refTypeId, JDWP::MethodId methodId, JDWP::ExpandBuf* pReply);
165 static void OutputVariableTable(JDWP::RefTypeId refTypeId, JDWP::MethodId id, bool withGeneric, JDWP::ExpandBuf* pReply);
166
167 static uint8_t GetFieldBasicTag(JDWP::ObjectId objId, JDWP::FieldId fieldId);
168 static uint8_t GetStaticFieldBasicTag(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId);
169 static void GetFieldValue(JDWP::ObjectId objectId, JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply);
170 static void SetFieldValue(JDWP::ObjectId objectId, JDWP::FieldId fieldId, uint64_t value, int width);
171 static void GetStaticFieldValue(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId, JDWP::ExpandBuf* pReply);
172 static void SetStaticFieldValue(JDWP::RefTypeId refTypeId, JDWP::FieldId fieldId, uint64_t rawValue, int width);
173
174 static char* StringToUtf8(JDWP::ObjectId strId);
175
176 /*
177 * Thread, ThreadGroup, Frame
178 */
179 static char* GetThreadName(JDWP::ObjectId threadId);
180 static JDWP::ObjectId GetThreadGroup(JDWP::ObjectId threadId);
181 static char* GetThreadGroupName(JDWP::ObjectId threadGroupId);
182 static JDWP::ObjectId GetThreadGroupParent(JDWP::ObjectId threadGroupId);
183 static JDWP::ObjectId GetSystemThreadGroupId();
184 static JDWP::ObjectId GetMainThreadGroupId();
185
186 static bool GetThreadStatus(JDWP::ObjectId threadId, uint32_t* threadStatus, uint32_t* suspendStatus);
187 static uint32_t GetThreadSuspendCount(JDWP::ObjectId threadId);
188 static bool ThreadExists(JDWP::ObjectId threadId);
189 static bool IsSuspended(JDWP::ObjectId threadId);
190 //static void WaitForSuspend(JDWP::ObjectId threadId);
191 static void GetThreadGroupThreads(JDWP::ObjectId threadGroupId, JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount);
192 static void GetAllThreads(JDWP::ObjectId** ppThreadIds, uint32_t* pThreadCount);
193 static int GetThreadFrameCount(JDWP::ObjectId threadId);
194 static bool GetThreadFrame(JDWP::ObjectId threadId, int num, JDWP::FrameId* pFrameId, JDWP::JdwpLocation* pLoc);
195
196 static JDWP::ObjectId GetThreadSelfId();
Elliott Hughes475fc232011-10-25 15:00:35 -0700197 static void SuspendVM();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700198 static void ResumeVM();
199 static void SuspendThread(JDWP::ObjectId threadId);
200 static void ResumeThread(JDWP::ObjectId threadId);
201 static void SuspendSelf();
202
203 static bool GetThisObject(JDWP::ObjectId threadId, JDWP::FrameId frameId, JDWP::ObjectId* pThisId);
204 static void GetLocalValue(JDWP::ObjectId threadId, JDWP::FrameId frameId, int slot, uint8_t tag, uint8_t* buf, int expectedLen);
205 static void SetLocalValue(JDWP::ObjectId threadId, JDWP::FrameId frameId, int slot, uint8_t tag, uint64_t value, int width);
206
207 /*
208 * Debugger notification
209 */
210 enum {
211 kBreakPoint = 0x01,
212 kSingleStep = 0x02,
213 kMethodEntry = 0x04,
214 kMethodExit = 0x08,
215 };
216 static void PostLocationEvent(const Method* method, int pcOffset, Object* thisPtr, int eventFlags);
217 static void PostException(void* throwFp, int throwRelPc, void* catchFp, int catchRelPc, Object* exception);
218 static void PostThreadStart(Thread* t);
219 static void PostThreadDeath(Thread* t);
220 static void PostClassPrepare(Class* c);
221
222 static bool WatchLocation(const JDWP::JdwpLocation* pLoc);
223 static void UnwatchLocation(const JDWP::JdwpLocation* pLoc);
224 static bool ConfigureStep(JDWP::ObjectId threadId, JDWP::JdwpStepSize size, JDWP::JdwpStepDepth depth);
225 static void UnconfigureStep(JDWP::ObjectId threadId);
226
227 static JDWP::JdwpError 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);
228 static void ExecuteMethod(DebugInvokeReq* pReq);
229
230 /* perform "late registration" of an object ID */
231 static void RegisterObjectId(JDWP::ObjectId id);
232
233 /*
234 * DDM support.
235 */
Elliott Hughes82188472011-11-07 18:11:48 -0800236 static void DdmSendThreadNotification(Thread* t, uint32_t type);
Elliott Hughes47fce012011-10-25 18:37:19 -0700237 static void DdmSetThreadNotification(bool enable);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700238 static bool DdmHandlePacket(const uint8_t* buf, int dataLen, uint8_t** pReplyBuf, int* pReplyLen);
239 static void DdmConnected();
240 static void DdmDisconnected();
Elliott Hughes82188472011-11-07 18:11:48 -0800241 static void DdmSendChunk(uint32_t type, size_t len, const uint8_t* buf);
242 static void DdmSendChunkV(uint32_t type, const struct iovec* iov, int iovcnt);
Elliott Hughes767a1472011-10-26 18:49:02 -0700243
Elliott Hughes545a0642011-11-08 19:10:03 -0800244 /*
245 * Recent allocation tracking support.
246 */
247 static void RecordAllocation(Class* type, size_t byte_count);
248 static void SetAllocTrackingEnabled(bool enabled);
249 static inline bool IsAllocTrackingEnabled() { return recent_allocation_records_ != NULL; }
250 static jbyteArray GetRecentAllocations();
251 static void DumpRecentAllocations();
252
Elliott Hughes767a1472011-10-26 18:49:02 -0700253 enum HpifWhen {
254 HPIF_WHEN_NEVER = 0,
255 HPIF_WHEN_NOW = 1,
256 HPIF_WHEN_NEXT_GC = 2,
257 HPIF_WHEN_EVERY_GC = 3
258 };
259 static int DdmHandleHpifChunk(HpifWhen when);
260
261 enum HpsgWhen {
262 HPSG_WHEN_NEVER = 0,
263 HPSG_WHEN_EVERY_GC = 1,
264 };
265 enum HpsgWhat {
266 HPSG_WHAT_MERGED_OBJECTS = 0,
267 HPSG_WHAT_DISTINCT_OBJECTS = 1,
268 };
269 static bool DdmHandleHpsgNhsgChunk(HpsgWhen when, HpsgWhat what, bool native);
270
Elliott Hughes7162ad92011-10-27 14:08:42 -0700271 static void DdmSendHeapInfo(HpifWhen reason);
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700272 static void DdmSendHeapSegments(bool native);
Elliott Hughes545a0642011-11-08 19:10:03 -0800273
274 private:
275 static AllocRecord* recent_allocation_records_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700276};
277
278#define CHUNK_TYPE(_name) \
Elliott Hughes82188472011-11-07 18:11:48 -0800279 static_cast<uint32_t>((_name)[0] << 24 | (_name)[1] << 16 | (_name)[2] << 8 | (_name)[3])
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700280
281} // namespace art
282
283#endif // ART_DEBUGGER_H_