2
Copyright (C) 2009 Volker Berlin (vberlin@inetsoftware.de)
4
This software is provided 'as-is', without any express or implied
5
warranty. In no event will the authors be held liable for any damages
6
arising from the use of this software.
8
Permission is granted to anyone to use this software for any purpose,
9
including commercial applications, and to alter it and redistribute it
10
freely, subject to the following restrictions:
12
1. The origin of this software must not be misrepresented; you must not
13
claim that you wrote the original software. If you use this software
14
in a product, an acknowledgment in the product documentation would be
15
appreciated but is not required.
16
2. Altered source versions must be plainly marked as such, and must not be
17
misrepresented as being the original software.
18
3. This notice may not be removed or altered from any source distribution.
26
using System.Collections.Generic;
28
using ikvm.debugger.requests;
29
using ikvm.debugger.win;
31
namespace ikvm.debugger
34
/// Implementation of the JDWP Protocol. The documentation is at:
35
/// http://java.sun.com/javase/6/docs/platform/jpda/jdwp/jdwp-protocol.html
40
private readonly JdwpConnection conn;
42
// TODO Create a real implementation
43
private readonly TargetVM target;
45
internal JdwpHandler(JdwpConnection conn, TargetVM target)
55
Packet packet = conn.ReadPacket();
56
Console.Error.WriteLine("Packet:"+packet.CommandSet + " " + packet.Command);
57
switch (packet.CommandSet)
59
case CommandSet.VirtualMachine:
60
CommandSetVirtualMachine(packet);
62
case CommandSet.ReferenceType:
63
CommandSetReferenceType(packet);
65
case CommandSet.EventRequest:
66
CommandSetEventRequest(packet);
69
NotImplementedPacket(packet);
72
conn.SendPacket(packet);
77
/// http://java.sun.com/javase/6/docs/platform/jpda/jdwp/jdwp-protocol.html#JDWP_VirtualMachine
79
/// <param name="packet"></param>
80
private void CommandSetVirtualMachine(Packet packet)
82
switch (packet.Command)
84
case VirtualMachine.Version:
85
packet.WriteString("IKVM Debugger");
88
packet.WriteString("1.6.0");
89
packet.WriteString("IKVM.NET");
91
case VirtualMachine.ClassesBySignature:
92
String jniClassName = packet.ReadString();
93
IList<TargetType> types = target.FindTypes(jniClassName);
94
packet.WriteInt(types.Count); // count
96
foreach (TargetType type in types)
98
Console.Error.WriteLine("FindTypes:" + jniClassName + ":" + type.TypeId);
99
packet.WriteByte(TypeTag.CLASS); //TODO can also a interface
100
packet.WriteObjectID(type.TypeId);
101
packet.WriteInt(ClassStatus.INITIALIZED);
105
case VirtualMachine.AllThreads:
106
int[] ids = target.GetThreadIDs();
107
packet.WriteInt(ids.Length);
108
for (int i = 0; i < ids.Length; i++)
110
packet.WriteObjectID(ids[i]);
113
case VirtualMachine.IDSizes:
114
int size = 4; //we use a size of 4, a value of 8 is also possible
115
packet.WriteInt(size); // fieldID size in bytes
116
packet.WriteInt(size); // methodID size in bytes
117
packet.WriteInt(size); // objectID size in bytes
118
packet.WriteInt(size); // referenceTypeID size in bytes
119
packet.WriteInt(size); // frameID size in bytes
121
case VirtualMachine.Suspend:
124
case VirtualMachine.Resume:
127
case VirtualMachine.Exit:
128
target.Exit(packet.ReadInt());
130
case VirtualMachine.Capabilities:
131
packet.WriteBool(false); // Can the VM watch field modification, and therefore can it send the Modification Watchpoint Event?
132
packet.WriteBool(false); // Can the VM watch field access, and therefore can it send the Access Watchpoint Event?
133
packet.WriteBool(false); // Can the VM get the bytecodes of a given method?
134
packet.WriteBool(false); // Can the VM determine whether a field or method is synthetic? (that is, can the VM determine if the method or the field was invented by the compiler?)
135
packet.WriteBool(false); // Can the VM get the owned monitors infornation for a thread?
136
packet.WriteBool(false); // Can the VM get the current contended monitor of a thread?
137
packet.WriteBool(false); // Can the VM get the monitor information for a given object?
139
case VirtualMachine.CapabilitiesNew:
140
packet.WriteBool(false); // Can the VM watch field modification, and therefore can it send the Modification Watchpoint Event?
141
packet.WriteBool(false); // Can the VM watch field access, and therefore can it send the Access Watchpoint Event?
142
packet.WriteBool(false); // Can the VM get the bytecodes of a given method?
143
packet.WriteBool(false); // Can the VM determine whether a field or method is synthetic? (that is, can the VM determine if the method or the field was invented by the compiler?)
144
packet.WriteBool(false); // Can the VM get the owned monitors infornation for a thread?
145
packet.WriteBool(false); // Can the VM get the current contended monitor of a thread?
146
packet.WriteBool(false); // Can the VM get the monitor information for a given object?
147
packet.WriteBool(false); // Can the VM redefine classes?
148
packet.WriteBool(false); // Can the VM add methods when redefining classes?
149
packet.WriteBool(false); // Can the VM redefine classesin arbitrary ways?
150
packet.WriteBool(false); // Can the VM pop stack frames?
151
packet.WriteBool(false); // Can the VM filter events by specific object?
152
packet.WriteBool(false); // Can the VM get the source debug extension?
153
packet.WriteBool(false); // Can the VM request VM death events?
154
packet.WriteBool(false); // Can the VM set a default stratum?
155
packet.WriteBool(false); // Can the VM return instances, counts of instances of classes and referring objects?
156
packet.WriteBool(false); // Can the VM request monitor events?
157
packet.WriteBool(false); // Can the VM get monitors with frame depth info?
158
packet.WriteBool(false); // Can the VM filter class prepare events by source name?
159
packet.WriteBool(false); // Can the VM return the constant pool information?
160
packet.WriteBool(false); // Can the VM force early return from a method?
161
packet.WriteBool(false); // reserved22
162
packet.WriteBool(false); // reserved23
163
packet.WriteBool(false); // reserved24
164
packet.WriteBool(false); // reserved25
165
packet.WriteBool(false); // reserved26
166
packet.WriteBool(false); // reserved27
167
packet.WriteBool(false); // reserved28
168
packet.WriteBool(false); // reserved29
169
packet.WriteBool(false); // reserved30
170
packet.WriteBool(false); // reserved31
171
packet.WriteBool(false); // reserved32
174
NotImplementedPacket(packet); // include a SendPacket
180
/// http://java.sun.com/javase/6/docs/platform/jpda/jdwp/jdwp-protocol.html#JDWP_ReferenceType
182
/// <param name="packet"></param>
183
private void CommandSetReferenceType(Packet packet)
185
switch (packet.Command)
187
case ReferenceType.Signature:
188
int typeID = packet.ReadObjectID();
189
TargetType type = target.FindType(typeID);
190
Console.Error.WriteLine(typeID + ":" + type.GetJniSignature());
191
packet.WriteString(type.GetJniSignature());
193
case ReferenceType.ClassLoader:
194
int classLoaderID = packet.ReadObjectID();
195
packet.WriteObjectID(0); //TODO 0 - System Classloader, we can use module ID instead
197
case ReferenceType.MethodsWithGeneric:
198
typeID = packet.ReadObjectID();
199
Console.Error.WriteLine(typeID);
200
type = target.FindType(typeID);
201
IList<TargetMethod> methods = type.GetMethods();
202
packet.WriteInt(methods.Count);
203
foreach (TargetMethod method in methods)
205
Console.Error.WriteLine(method.MethodId + ":" + method.Name + ":" + method.JniSignature + ":" + method.GenericSignature+":"+method.AccessFlags);
206
packet.WriteObjectID(method.MethodId);
207
packet.WriteString(method.Name);
208
packet.WriteString(method.JniSignature);
209
packet.WriteString(method.GenericSignature);
210
packet.WriteInt(method.AccessFlags);
214
NotImplementedPacket(packet);
220
/// http://java.sun.com/javase/6/docs/platform/jpda/jdwp/jdwp-protocol.html#JDWP_EventRequest
222
/// <param name="packet"></param>
223
private void CommandSetEventRequest(Packet packet)
225
switch (packet.Command)
227
case EventRequest.CmdSet:
228
EventRequest eventRequest = EventRequest.create(packet);
229
Console.Error.WriteLine(eventRequest);
230
if (eventRequest == null)
232
NotImplementedPacket(packet);
236
target.AddEventRequest(eventRequest);
237
packet.WriteInt(eventRequest.RequestId);
241
NotImplementedPacket(packet);
246
private void NotImplementedPacket(Packet packet)
248
Console.Error.WriteLine("================================");
249
Console.Error.WriteLine("Not Implemented Packet:" + packet.CommandSet + "-" + packet.Command);
250
Console.Error.WriteLine("================================");
251
packet.Error = Error.NOT_IMPLEMENTED;