~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ikvm/debugger/JdwpHandler.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (C) 2009 Volker Berlin (vberlin@inetsoftware.de)
 
3
 
 
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.
 
7
 
 
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:
 
11
 
 
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.
 
19
 
 
20
  Jeroen Frijters
 
21
  jeroen@frijters.net
 
22
  
 
23
*/
 
24
 
 
25
using System;
 
26
using System.Collections.Generic;
 
27
using System.Text;
 
28
using ikvm.debugger.requests;
 
29
using ikvm.debugger.win;
 
30
 
 
31
namespace ikvm.debugger
 
32
{
 
33
    /// <summary>
 
34
    /// Implementation of the JDWP Protocol. The documentation is at:
 
35
    /// http://java.sun.com/javase/6/docs/platform/jpda/jdwp/jdwp-protocol.html
 
36
    /// </summary>
 
37
    class JdwpHandler
 
38
    {
 
39
 
 
40
        private readonly JdwpConnection conn;
 
41
 
 
42
        // TODO Create a real implementation
 
43
        private readonly TargetVM target;
 
44
 
 
45
        internal JdwpHandler(JdwpConnection conn, TargetVM target)
 
46
        {
 
47
            this.conn = conn;
 
48
            this.target = target;
 
49
        }
 
50
 
 
51
        internal void Run()
 
52
        {
 
53
            while (true)
 
54
            {
 
55
                Packet packet = conn.ReadPacket();
 
56
                Console.Error.WriteLine("Packet:"+packet.CommandSet + " " + packet.Command);
 
57
                switch (packet.CommandSet)
 
58
                {
 
59
                    case CommandSet.VirtualMachine:
 
60
                        CommandSetVirtualMachine(packet);
 
61
                        break;
 
62
                    case CommandSet.ReferenceType:
 
63
                        CommandSetReferenceType(packet);
 
64
                        break;
 
65
                    case CommandSet.EventRequest:
 
66
                        CommandSetEventRequest(packet);
 
67
                        break;
 
68
                    default:
 
69
                        NotImplementedPacket(packet);
 
70
                        break;
 
71
                }
 
72
                conn.SendPacket(packet);
 
73
            }
 
74
        }
 
75
 
 
76
        /// <summary>
 
77
        /// http://java.sun.com/javase/6/docs/platform/jpda/jdwp/jdwp-protocol.html#JDWP_VirtualMachine
 
78
        /// </summary>
 
79
        /// <param name="packet"></param>
 
80
        private void CommandSetVirtualMachine(Packet packet)
 
81
        {
 
82
            switch (packet.Command)
 
83
            {
 
84
                case VirtualMachine.Version:
 
85
                    packet.WriteString("IKVM Debugger");
 
86
                    packet.WriteInt(1);
 
87
                    packet.WriteInt(6);
 
88
                    packet.WriteString("1.6.0");
 
89
                    packet.WriteString("IKVM.NET");
 
90
                    break;
 
91
                case VirtualMachine.ClassesBySignature:
 
92
                    String jniClassName = packet.ReadString();
 
93
                    IList<TargetType> types = target.FindTypes(jniClassName);
 
94
                    packet.WriteInt(types.Count); // count
 
95
 
 
96
                    foreach (TargetType type in types)
 
97
                    {
 
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);
 
102
                    }
 
103
 
 
104
                    break;
 
105
                case VirtualMachine.AllThreads:
 
106
                    int[] ids = target.GetThreadIDs();
 
107
                    packet.WriteInt(ids.Length);
 
108
                    for (int i = 0; i < ids.Length; i++)
 
109
                    {
 
110
                        packet.WriteObjectID(ids[i]);
 
111
                    }
 
112
                    break;
 
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
 
120
                    break;
 
121
                case VirtualMachine.Suspend:
 
122
                    target.Suspend();
 
123
                    break;
 
124
                case VirtualMachine.Resume:
 
125
                    target.Resume();
 
126
                    break;
 
127
                case VirtualMachine.Exit:
 
128
                    target.Exit(packet.ReadInt());
 
129
                    break;
 
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?   
 
138
                    break;
 
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
 
172
                    break;
 
173
                default:
 
174
                    NotImplementedPacket(packet); // include a SendPacket
 
175
                    break;
 
176
            }
 
177
        }
 
178
 
 
179
        /// <summary>
 
180
        /// http://java.sun.com/javase/6/docs/platform/jpda/jdwp/jdwp-protocol.html#JDWP_ReferenceType
 
181
        /// </summary>
 
182
        /// <param name="packet"></param>
 
183
        private void CommandSetReferenceType(Packet packet)
 
184
        {
 
185
            switch (packet.Command)
 
186
            {
 
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());
 
192
                    break;
 
193
                case ReferenceType.ClassLoader:
 
194
                    int classLoaderID = packet.ReadObjectID();
 
195
                    packet.WriteObjectID(0); //TODO 0 - System Classloader, we can use module ID instead
 
196
                    break;
 
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)
 
204
                    {
 
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);
 
211
                    }
 
212
                    break;
 
213
                default:
 
214
                    NotImplementedPacket(packet);
 
215
                    break;
 
216
            }
 
217
        }
 
218
 
 
219
        /// <summary>
 
220
        /// http://java.sun.com/javase/6/docs/platform/jpda/jdwp/jdwp-protocol.html#JDWP_EventRequest
 
221
        /// </summary>
 
222
        /// <param name="packet"></param>
 
223
        private void CommandSetEventRequest(Packet packet)
 
224
        {
 
225
            switch (packet.Command)
 
226
            {
 
227
                case EventRequest.CmdSet:
 
228
                    EventRequest eventRequest = EventRequest.create(packet);
 
229
                    Console.Error.WriteLine(eventRequest);
 
230
                    if (eventRequest == null)
 
231
                    {
 
232
                        NotImplementedPacket(packet);
 
233
                    }
 
234
                    else
 
235
                    {
 
236
                        target.AddEventRequest(eventRequest);
 
237
                        packet.WriteInt(eventRequest.RequestId);
 
238
                    }
 
239
                    break;
 
240
                default:
 
241
                    NotImplementedPacket(packet);
 
242
                    break;
 
243
            }
 
244
        }
 
245
 
 
246
        private void NotImplementedPacket(Packet packet)
 
247
        {
 
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;
 
252
        }
 
253
    }
 
254
}