Implement get of command handlers.
[SquirrelJME.git] / modules / debug-jdwp / src / main / java / cc / squirreljme / jdwp / host / JDWPHostCommandSetEventRequest.java
blob4b13dc54b82bfefa1c3d76b648b215cd3d99f80c
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc.squirreljme.jdwp.host;
12 import cc.squirreljme.jdwp.JDWPCommand;
13 import cc.squirreljme.jdwp.JDWPCommandSetEventRequest;
14 import cc.squirreljme.jdwp.JDWPErrorType;
15 import cc.squirreljme.jdwp.JDWPEventKind;
16 import cc.squirreljme.jdwp.JDWPEventModifierKind;
17 import cc.squirreljme.jdwp.JDWPException;
18 import cc.squirreljme.jdwp.JDWPHostController;
19 import cc.squirreljme.jdwp.JDWPHostEventRequest;
20 import cc.squirreljme.jdwp.JDWPHostLocation;
21 import cc.squirreljme.jdwp.JDWPPacket;
22 import cc.squirreljme.jdwp.JDWPSuspendPolicy;
23 import cc.squirreljme.jdwp.host.event.JDWPHostCallStackStepping;
24 import cc.squirreljme.jdwp.JDWPClassPatternMatcher;
25 import cc.squirreljme.jdwp.host.event.JDWPHostExceptionOnly;
26 import cc.squirreljme.jdwp.host.event.JDWPHostFieldOnly;
27 import cc.squirreljme.jdwp.host.event.JDWPHostEventFilter;
28 import cc.squirreljme.jdwp.JDWPStepDepth;
29 import cc.squirreljme.jdwp.JDWPStepSize;
31 /**
32 * Event request command set.
34 * @since 2021/03/12
36 public enum JDWPHostCommandSetEventRequest
37 implements JDWPCommandHandler
39 /** Set event requests. */
40 SET(JDWPCommandSetEventRequest.SET)
42 /**
43 * {@inheritDoc}
44 * @since 2021/03/12
46 @Override
47 public JDWPPacket execute(JDWPHostController __controller,
48 JDWPPacket __packet)
49 throws JDWPException
51 // Which kind of event? If not supported, it is not valid
52 JDWPEventKind eventKind = JDWPEventKind.of(__packet.readByte());
53 if (eventKind == null)
54 return __controller.reply(__packet.id(),
55 JDWPErrorType.INVALID_EVENT_TYPE);
57 // How does this suspend?
58 JDWPSuspendPolicy suspendPolicy =
59 JDWPSuspendPolicy.of(__packet.readByte());
61 // Is there at least one filter?
62 boolean hasFilter = false;
64 // Modifier properties
65 int occurrenceLimit = -1;
66 Object thread = null;
67 Object type = null;
68 JDWPClassPatternMatcher includeClass = null;
69 JDWPClassPatternMatcher excludeClass = null;
70 JDWPHostFieldOnly fieldOnly = null;
71 JDWPHostLocation location = null;
72 Object thisInstance = null;
73 boolean thisInstanceSet = false;
74 JDWPHostExceptionOnly exception = null;
75 JDWPHostCallStackStepping callStackStepping = null;
77 // Modifier kinds
78 int numModifiers = __packet.readInt();
79 for (int i = 0; i < numModifiers; i++)
81 // Check if the kind if supported or known about
82 JDWPEventModifierKind modKind = JDWPEventModifierKind.of(__packet.readByte());
83 if (modKind == null)
84 return __controller.reply(__packet.id(),
85 JDWPErrorType.NOT_IMPLEMENTED);
87 // If this is not a valid modifier, ignore this
88 if (!eventKind.isValidModifier(modKind))
89 throw JDWPErrorType.ILLEGAL_ARGUMENT.toss(
90 null, modKind.ordinal(), null);
92 // Everything except occurrences has a filter!
93 if (modKind != JDWPEventModifierKind.LIMIT_OCCURRENCES)
94 hasFilter = true;
96 // Depends on the kind
97 switch (modKind)
99 case LIMIT_OCCURRENCES:
100 occurrenceLimit = __packet.readInt();
101 break;
103 case THREAD_ONLY:
104 thread = __controller.readThread(__packet);
106 if (thread != null)
107 __controller.getState().items.put(thread);
108 break;
110 case CLASS_ONLY:
111 type = __controller.readType(__packet, false);
113 if (type != null)
114 __controller.getState().items.put(type);
115 break;
117 case CLASS_MATCH_PATTERN:
118 includeClass = new JDWPClassPatternMatcher(
119 __packet.readString());
120 break;
122 case CLASS_EXCLUDE_PATTERN:
123 excludeClass = new JDWPClassPatternMatcher(
124 __packet.readString());
125 break;
127 // A specific location in a class
128 case LOCATION_ONLY:
129 location = __controller.readLocation(__packet);
130 break;
132 case EXCEPTION_ONLY:
133 exception = new JDWPHostExceptionOnly(
134 __controller.readType(__packet, true),
135 __packet.readBoolean(),
136 __packet.readBoolean());
137 break;
139 case FIELD_ONLY:
141 // Make sure this is a valid field
142 Object atType = __controller.readType(__packet,
143 false);
144 int fieldDx = __packet.readId();
145 if (!__controller.viewType().isValidField(atType,
146 fieldDx))
147 JDWPErrorType.INVALID_FIELD_ID.toss(atType,
148 fieldDx, null);
150 fieldOnly = new JDWPHostFieldOnly(atType, fieldDx);
152 break;
154 case CALL_STACK_STEPPING:
155 callStackStepping = new JDWPHostCallStackStepping(
156 __controller.readThread(__packet),
157 JDWPStepSize.of(__packet.readInt()),
158 JDWPStepDepth.of(__packet.readInt()));
159 break;
161 case THIS_INSTANCE_ONLY:
162 thisInstance = __controller.readObject(__packet, true);
163 thisInstanceSet = true;
164 break;
166 // Report not-implemented
167 default:
168 return __controller.reply(__packet.id(),
169 JDWPErrorType.NOT_IMPLEMENTED);
173 // Initialize the event filter with all the modifier parameters
174 JDWPHostEventFilter
175 eventFilter = (hasFilter ? new JDWPHostEventFilter(thread,
176 type, includeClass, excludeClass, fieldOnly, location,
177 thisInstanceSet, thisInstance, exception,
178 callStackStepping) : null);
180 // Register the event request
181 JDWPHostEventRequest request = new JDWPHostEventRequest(
182 __controller.getCommLink().nextId(), eventKind, suspendPolicy,
183 occurrenceLimit, eventFilter);
184 __controller.getEventManager().addEventRequest(request);
186 // Perform injection for the event so whatever we are using for
187 // the call can trip events
188 __controller.tripRequest(request);
190 // Respond with the ID of this event
191 JDWPPacket rv = __controller.reply(
192 __packet.id(), JDWPErrorType.NO_ERROR);
193 rv.writeInt(request.id);
195 return rv;
199 /** Clear event. */
200 CLEAR(JDWPCommandSetEventRequest.CLEAR)
203 * {@inheritDoc}
204 * @since 2021/04/18
206 @Override
207 public JDWPPacket execute(JDWPHostController __controller,
208 JDWPPacket __packet)
209 throws JDWPException
211 // Which kind of event? If not supported, it is not valid
212 JDWPEventKind eventKind = JDWPEventKind.of(__packet.readByte());
213 if (eventKind == null)
214 return __controller.reply(__packet.id(),
215 JDWPErrorType.INVALID_EVENT_TYPE);
217 // Delete the event, if it is known... the kind is ignored since
218 // we always use unique IDs regardless of type
219 __controller.getEventManager().delete(__packet.readInt());
221 // Always successful, even if the ID is not valid
222 return null;
226 /** Clear all breakpoints. */
227 CLEAR_ALL_BREAKPOINTS(JDWPCommandSetEventRequest.CLEAR_ALL_BREAKPOINTS)
230 * {@inheritDoc}
231 * @since 2021/04/30
233 @Override
234 public JDWPPacket execute(JDWPHostController __controller,
235 JDWPPacket __packet)
236 throws JDWPException
238 __controller.getEventManager().clear(JDWPEventKind.BREAKPOINT);
240 return null;
244 /* End. */
247 /** The base command. */
248 public final JDWPCommand command;
250 /** The ID of the packet. */
251 public final int id;
254 * Returns the ID used.
256 * @param __id The ID used.
257 * @since 2021/03/12
259 JDWPHostCommandSetEventRequest(JDWPCommand __id)
261 this.command = __id;
262 this.id = __id.debuggerId();
266 * {@inheritDoc}
267 * @since 2024/01/23
269 @Override
270 public final JDWPCommand command()
272 return this.command;
276 * {@inheritDoc}
277 * @since 2021/03/12
279 @Override
280 public final int debuggerId()
282 return this.id;