1 11 package org.eclipse.jdi.internal.request; 12 13 14 import java.io.ByteArrayOutputStream ; 15 import java.io.DataInputStream ; 16 import java.io.DataOutputStream ; 17 import java.io.IOException ; 18 import java.util.ArrayList ; 19 import java.util.HashMap ; 20 import java.util.Map ; 21 22 import org.eclipse.jdi.internal.FieldImpl; 23 import org.eclipse.jdi.internal.LocationImpl; 24 import org.eclipse.jdi.internal.MirrorImpl; 25 import org.eclipse.jdi.internal.ObjectReferenceImpl; 26 import org.eclipse.jdi.internal.ReferenceTypeImpl; 27 import org.eclipse.jdi.internal.ThreadReferenceImpl; 28 import org.eclipse.jdi.internal.VirtualMachineImpl; 29 import org.eclipse.jdi.internal.event.EventImpl; 30 import org.eclipse.jdi.internal.jdwp.JdwpCommandPacket; 31 import org.eclipse.jdi.internal.jdwp.JdwpReplyPacket; 32 33 import com.sun.jdi.InternalException; 34 import com.sun.jdi.ObjectCollectedException; 35 import com.sun.jdi.ObjectReference; 36 import com.sun.jdi.ReferenceType; 37 import com.sun.jdi.ThreadReference; 38 import com.sun.jdi.VMMismatchException; 39 import com.sun.jdi.request.EventRequest; 40 import com.sun.jdi.request.InvalidRequestStateException; 41 import com.sun.jdi.request.StepRequest; 42 43 44 50 public abstract class EventRequestImpl extends MirrorImpl implements EventRequest { 51 52 public static final byte STEP_SIZE_MIN_JDWP = 0; 53 public static final byte STEP_SIZE_LINE_JDWP = 1; 54 public static final byte STEP_DEPTH_INTO_JDWP = 0; 55 public static final byte STEP_DEPTH_OVER_JDWP = 1; 56 public static final byte STEP_DEPTH_OUT_JDWP = 2; 57 public static final byte STEP_DEPTH_REENTER_JDWP_HCR = 3; 59 60 public static final byte SUSPENDPOL_NONE_JDWP = 0; 61 public static final byte SUSPENDPOL_EVENT_THREAD_JDWP = 1; 62 public static final byte SUSPENDPOL_ALL_JDWP = 2; 63 64 65 public static final byte MODIF_KIND_COUNT = 1; 66 public static final byte MODIF_KIND_CONDITIONAL = 2; 67 public static final byte MODIF_KIND_THREADONLY = 3; 68 public static final byte MODIF_KIND_CLASSONLY = 4; 69 public static final byte MODIF_KIND_CLASSMATCH = 5; 70 public static final byte MODIF_KIND_CLASSEXCLUDE = 6; 71 public static final byte MODIF_KIND_LOCATIONONLY = 7; 72 public static final byte MODIF_KIND_EXCEPTIONONLY = 8; 73 public static final byte MODIF_KIND_FIELDONLY = 9; 74 public static final byte MODIF_KIND_STEP = 10; 75 public static final byte MODIF_KIND_INSTANCE = 11; 76 public static final byte MODIF_KIND_SOURCE_NAME_FILTER = 12; 77 78 79 private static HashMap fStepSizeMap = null; 80 private static HashMap fStepDepthMap = null; 81 private static HashMap fSuspendPolicyMap = null; 82 private static HashMap fModifierKindMap = null; 83 84 85 private boolean fGeneratedInside = false; 86 87 88 private HashMap fPropertyMap; 89 90 91 protected RequestID fRequestID = null; 92 93 private byte fSuspendPolicy = SUSPEND_ALL; 95 98 99 protected ArrayList fCountFilters; 100 101 102 protected ArrayList fThreadFilters = null; 103 104 105 protected ArrayList fClassFilters = null; 106 107 108 protected ArrayList fClassFilterRefs = null; 109 110 111 protected ArrayList fClassExclusionFilters = null; 112 113 114 protected ArrayList fLocationFilters = null; 115 116 117 protected ArrayList fExceptionFilters = null; 118 119 120 protected ArrayList fFieldFilters = null; 121 122 123 protected ArrayList fThreadStepFilters = null; 124 125 126 protected ArrayList fInstanceFilters = null; 127 131 protected ArrayList fSourceNameFilters = null; 132 133 136 protected EventRequestImpl(String description, VirtualMachineImpl vmImpl) { 137 super(description, vmImpl); 138 } 139 140 143 public String toString() { 144 return super.toString() + (fRequestID == null ? RequestMessages.EventRequestImpl___not_enabled__1 : RequestMessages.EventRequestImpl____2 + fRequestID); } 146 147 150 public Object getProperty(Object key) { 151 if (fPropertyMap == null) { 152 return null; 153 } 154 155 return fPropertyMap.get(key); 156 } 157 158 161 public void putProperty(Object key, Object value) { 162 if (fPropertyMap == null) 163 fPropertyMap = new HashMap (); 164 165 if (value == null) 166 fPropertyMap.remove(key); 167 else 168 fPropertyMap.put(key, value); 169 } 170 171 174 public void setGeneratedInside() { 175 fGeneratedInside = true; 176 } 177 178 181 public final boolean isGeneratedInside() { 182 return fGeneratedInside; 183 } 184 185 188 public synchronized void disable() { 189 if (!isEnabled()) 190 return; 191 192 initJdwpRequest(); 193 try { 194 ByteArrayOutputStream outBytes = new ByteArrayOutputStream (); 195 DataOutputStream outData = new DataOutputStream (outBytes); 196 writeByte(eventKind(), "event kind", EventImpl.eventKindMap(), outData); fRequestID.write(this, outData); 198 199 JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.ER_CLEAR, outBytes); 200 switch (replyPacket.errorCode()) { 201 case JdwpReplyPacket.NOT_FOUND: 202 throw new InvalidRequestStateException(); 203 } 204 defaultReplyErrorHandler(replyPacket.errorCode()); 205 206 virtualMachineImpl().eventRequestManagerImpl().removeRequestIDMapping(this); 207 fRequestID = null; 208 } catch (IOException e) { 209 defaultIOExceptionHandler(e); 210 } finally { 211 handledJdwpRequest(); 212 } 213 } 214 215 218 public synchronized void enable() { 219 if (isEnabled()) 220 return; 221 222 initJdwpRequest(); 223 try { 224 ByteArrayOutputStream outBytes = new ByteArrayOutputStream (); 225 DataOutputStream outData = new DataOutputStream (outBytes); 226 writeByte(eventKind(), "event kind", EventImpl.eventKindMap(), outData); writeByte(suspendPolicyJDWP(), "suspend policy", EventRequestImpl.suspendPolicyMap(), outData); writeInt(modifierCount(), "modifiers", outData); writeModifiers(outData); 230 231 JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.ER_SET, outBytes); 232 defaultReplyErrorHandler(replyPacket.errorCode()); 233 DataInputStream replyData = replyPacket.dataInStream(); 234 fRequestID = RequestID.read(this, replyData); 235 virtualMachineImpl().eventRequestManagerImpl().addRequestIDMapping(this); 236 } catch (IOException e) { 237 defaultIOExceptionHandler(e); 238 } finally { 239 handledJdwpRequest(); 240 } 241 } 242 243 246 public static void clearAllBreakpoints(MirrorImpl mirror) { 247 mirror.initJdwpRequest(); 248 try { 249 JdwpReplyPacket replyPacket = mirror.requestVM(JdwpCommandPacket.ER_CLEAR_ALL_BREAKPOINTS); 250 mirror.defaultReplyErrorHandler(replyPacket.errorCode()); 251 } finally { 252 mirror.handledJdwpRequest(); 253 } 254 } 255 256 259 public synchronized final boolean isEnabled() { 260 return fRequestID != null; 261 } 262 263 266 public void setEnabled(boolean enable) { 267 if (enable) 268 enable(); 269 else 270 disable(); 271 } 272 273 276 public void checkDisabled() throws InvalidRequestStateException { 277 if (isEnabled()) 278 throw new InvalidRequestStateException(); 279 } 280 281 284 public void setSuspendPolicy(int suspendPolicy) { 285 fSuspendPolicy = (byte)suspendPolicy; 286 if (isEnabled()) { 287 disable(); 288 enable(); 289 } 290 } 291 292 295 public int suspendPolicy() { 296 return fSuspendPolicy; 297 } 298 299 302 public final RequestID requestID() { 303 return fRequestID; 304 } 305 306 309 public void addCountFilter(int count) throws InvalidRequestStateException { 310 checkDisabled(); 311 if (fCountFilters == null) 312 fCountFilters = new ArrayList (); 313 314 fCountFilters.add(new Integer (count)); 315 } 316 317 320 public void addThreadFilter(ThreadReference threadFilter) throws ObjectCollectedException, VMMismatchException, InvalidRequestStateException { 321 checkVM(threadFilter); 322 checkDisabled(); 323 if (threadFilter.isCollected()) 324 throw new ObjectCollectedException(); 325 if (fThreadFilters == null) 326 fThreadFilters = new ArrayList (); 327 328 fThreadFilters.add(threadFilter); 329 } 330 331 334 public void addClassFilter(ReferenceType filter) throws VMMismatchException, InvalidRequestStateException { 335 checkVM(filter); 336 checkDisabled(); 337 if (fClassFilterRefs == null) 338 fClassFilterRefs = new ArrayList (); 339 340 fClassFilterRefs.add(filter); 341 } 342 343 346 public void addClassFilter(String filter) throws InvalidRequestStateException { 347 checkDisabled(); 348 if (fClassFilters == null) 349 fClassFilters = new ArrayList (); 350 351 fClassFilters.add(filter); 352 } 353 354 357 public void addClassExclusionFilter(String filter) throws InvalidRequestStateException { 358 checkDisabled(); 359 if (fClassExclusionFilters == null) 360 fClassExclusionFilters = new ArrayList (); 361 362 fClassExclusionFilters.add(filter); 363 } 364 365 368 public void addLocationFilter(LocationImpl location) throws VMMismatchException { 369 checkDisabled(); 370 checkVM(location); 372 if (fLocationFilters == null) 373 fLocationFilters = new ArrayList (); 374 375 fLocationFilters.add(location); 376 } 377 378 381 public void addExceptionFilter(ReferenceTypeImpl refType, boolean notifyCaught, boolean notifyUncaught) throws VMMismatchException { 382 checkDisabled(); 383 if (refType != null) 385 checkVM(refType); 386 387 if (fExceptionFilters == null) 388 fExceptionFilters = new ArrayList (); 389 390 ExceptionFilter filter = new ExceptionFilter(); 391 filter.fException = refType; 392 filter.fNotifyCaught = notifyCaught; 393 filter.fNotifyUncaught = notifyUncaught; 394 fExceptionFilters.add(filter); 395 } 396 397 400 public void addFieldFilter(FieldImpl field) throws VMMismatchException { 401 checkDisabled(); 402 checkVM(field); 404 if (fFieldFilters == null) 405 fFieldFilters = new ArrayList (); 406 407 fFieldFilters.add(field); 408 } 409 410 413 public void addStepFilter(ThreadReferenceImpl thread, int size, int depth) throws VMMismatchException { 414 checkDisabled(); 415 checkVM(thread); 417 418 if (fThreadStepFilters == null) 419 fThreadStepFilters = new ArrayList (); 420 421 ThreadStepFilter filter = new ThreadStepFilter(); 422 filter.fThread = thread; 423 filter.fThreadStepSize = size; 424 filter.fThreadStepDepth = depth; 425 fThreadStepFilters.add(filter); 426 } 427 428 432 public void addInstanceFilter(ObjectReference instance) { 433 checkDisabled(); 434 checkVM(instance); 435 if (fInstanceFilters == null) { 436 fInstanceFilters = new ArrayList (); 437 } 438 fInstanceFilters.add(instance); 439 } 440 441 448 public void addSourceNameFilter(String pattern) { 449 checkDisabled(); 450 if(fSourceNameFilters == null) { 451 fSourceNameFilters = new ArrayList (); 452 } 453 fSourceNameFilters.add(pattern); 454 } 455 456 459 460 463 public byte suspendPolicyJDWP() { 464 switch (fSuspendPolicy) { 465 case SUSPEND_NONE: 466 return SUSPENDPOL_NONE_JDWP; 467 case SUSPEND_EVENT_THREAD: 468 return SUSPENDPOL_EVENT_THREAD_JDWP; 469 case SUSPEND_ALL: 470 return SUSPENDPOL_ALL_JDWP; 471 default: 472 throw new InternalException(RequestMessages.EventRequestImpl_Invalid_suspend_policy_encountered___3 + fSuspendPolicy); 473 } 474 } 475 476 479 public int threadStepSizeJDWP(int threadStepSize) { 480 switch (threadStepSize) { 481 case StepRequest.STEP_MIN: 482 return STEP_SIZE_MIN_JDWP; 483 case StepRequest.STEP_LINE: 484 return STEP_SIZE_LINE_JDWP; 485 default: 486 throw new InternalException(RequestMessages.EventRequestImpl_Invalid_step_size_encountered___4 + threadStepSize); 487 } 488 } 489 490 493 public int threadStepDepthJDWP(int threadStepDepth) { 494 switch (threadStepDepth) { 495 case StepRequest.STEP_INTO: 496 return STEP_DEPTH_INTO_JDWP; 497 case StepRequest.STEP_OVER: 498 return STEP_DEPTH_OVER_JDWP; 499 case StepRequest.STEP_OUT: 500 return STEP_DEPTH_OUT_JDWP; 501 default: 502 throw new InternalException(RequestMessages.EventRequestImpl_Invalid_step_depth_encountered___5 + threadStepDepth); 503 } 504 } 505 506 509 protected abstract byte eventKind(); 510 511 514 protected int modifierCount() { 515 int count = 0; 516 517 if (fCountFilters != null) 518 count += fCountFilters.size(); 519 if (fThreadFilters != null) 520 count += fThreadFilters.size(); 521 if (fClassFilterRefs != null) 522 count += fClassFilterRefs.size(); 523 if (fClassFilters != null) 524 count += fClassFilters.size(); 525 if (fClassExclusionFilters != null) 526 count += fClassExclusionFilters.size(); 527 if (fLocationFilters != null) 528 count += fLocationFilters.size(); 529 if (fExceptionFilters != null) 530 count += fExceptionFilters.size(); 531 if (fFieldFilters != null) 532 count += fFieldFilters.size(); 533 if (fThreadStepFilters != null) 534 count += fThreadStepFilters.size(); 535 if (fInstanceFilters != null) 536 count += fInstanceFilters.size(); 537 if(fSourceNameFilters != null) { 538 if (supportsSourceNameFilters()) { 539 count += fSourceNameFilters.size(); 540 } 541 } 542 return count; 543 } 544 545 548 protected void writeModifiers(DataOutputStream outData) throws IOException { 549 if (fThreadStepFilters != null) { 552 for (int i = 0; i < fThreadStepFilters.size(); i++) { 553 ThreadStepFilter filter = (ThreadStepFilter)fThreadStepFilters.get(i); 554 writeByte(MODIF_KIND_STEP, "modifier", modifierKindMap(), outData); filter.fThread.write(this, outData); 556 writeInt(threadStepSizeJDWP(filter.fThreadStepSize), "step size", outData); writeInt(threadStepDepthJDWP(filter.fThreadStepDepth), "step depth", outData); } 559 } 560 if (fFieldFilters != null) { 561 for (int i = 0; i < fFieldFilters.size(); i++) { 562 writeByte(MODIF_KIND_FIELDONLY, "modifier", modifierKindMap(), outData); ((FieldImpl)fFieldFilters.get(i)).writeWithReferenceType(this, outData); 564 } 565 } 566 if (fExceptionFilters != null) { 567 for (int i = 0; i < fExceptionFilters.size(); i++) { 568 ExceptionFilter filter = (ExceptionFilter)fExceptionFilters.get(i); 569 writeByte(MODIF_KIND_EXCEPTIONONLY, "modifier", modifierKindMap(), outData); if (filter.fException != null) 571 filter.fException.write(this, outData); 572 else 573 ReferenceTypeImpl.writeNull(this, outData); 574 575 writeBoolean(filter.fNotifyCaught, "notify caught", outData); writeBoolean(filter.fNotifyUncaught, "notify uncaught", outData); } 578 } 579 if (fLocationFilters != null) { 580 for (int i = 0; i < fLocationFilters.size(); i++) { 581 writeByte(MODIF_KIND_LOCATIONONLY, "modifier", modifierKindMap(), outData); ((LocationImpl)fLocationFilters.get(i)).write(this, outData); 583 } 584 } 585 if (fClassExclusionFilters != null) { 586 for (int i = 0; i < fClassExclusionFilters.size(); i++) { 587 writeByte(MODIF_KIND_CLASSEXCLUDE, "modifier", modifierKindMap(), outData); writeString((String )fClassExclusionFilters.get(i), "class excl. filter", outData); } 590 } 591 if (fClassFilters != null) { 592 for (int i = 0; i < fClassFilters.size(); i++) { 593 writeByte(MODIF_KIND_CLASSMATCH, "modifier", modifierKindMap(), outData); writeString((String )fClassFilters.get(i), "class filter", outData); } 596 } 597 if (fClassFilterRefs != null) { 598 for (int i = 0; i < fClassFilterRefs.size(); i++) { 599 writeByte(MODIF_KIND_CLASSONLY, "modifier", modifierKindMap(), outData); ((ReferenceTypeImpl)fClassFilterRefs.get(i)).write(this, outData); 601 } 602 } 603 if (fThreadFilters != null) { 604 for (int i = 0; i < fThreadFilters.size(); i++) { 605 writeByte(MODIF_KIND_THREADONLY, "modifier", modifierKindMap(), outData); ((ThreadReferenceImpl)fThreadFilters.get(i)).write(this, outData); 607 } 608 } 609 if (fCountFilters != null) { 610 for (int i = 0; i < fCountFilters.size(); i++) { 611 writeByte(MODIF_KIND_COUNT, "modifier", modifierKindMap(), outData); writeInt(((Integer )fCountFilters.get(i)).intValue(), "count filter", outData); } 614 } 615 if (fInstanceFilters != null) { 616 for (int i = 0; i < fInstanceFilters.size(); i++) { 617 writeByte(MODIF_KIND_INSTANCE, "modifier", modifierKindMap(), outData); ((ObjectReferenceImpl)fInstanceFilters.get(i)).write(this, outData); 619 } 620 } 621 if(fSourceNameFilters != null) { 622 if (supportsSourceNameFilters()) { 623 for (int i = 0; i < fSourceNameFilters.size(); i++) { 624 writeByte(MODIF_KIND_SOURCE_NAME_FILTER, "modifier", modifierKindMap(), outData); writeString((String )fSourceNameFilters.get(i), "modifier", outData); } 627 } 628 } 629 } 630 631 636 private boolean supportsSourceNameFilters() { 637 return ((VirtualMachineImpl)virtualMachine()).isJdwpVersionGreaterOrEqual(1, 6); 638 } 639 640 643 public static void getConstantMaps() { 644 if (fStepSizeMap != null) 645 return; 646 647 java.lang.reflect.Field [] fields = EventRequestImpl.class.getDeclaredFields(); 648 fStepSizeMap = new HashMap (); 649 fStepDepthMap = new HashMap (); 650 fSuspendPolicyMap = new HashMap (); 651 fModifierKindMap = new HashMap (); 652 for (int i = 0; i < fields.length; i++) { 653 java.lang.reflect.Field field = fields[i]; 654 if ((field.getModifiers() & java.lang.reflect.Modifier.PUBLIC) == 0 || (field.getModifiers() & java.lang.reflect.Modifier.STATIC) == 0 || (field.getModifiers() & java.lang.reflect.Modifier.FINAL) == 0) 655 continue; 656 657 try { 658 String name = field.getName(); 659 Integer intValue = new Integer (field.getInt(null)); 660 if (name.startsWith("STEP_SIZE_")) { name = name.substring(10); 662 fStepSizeMap.put(intValue, name); 663 } else if (name.startsWith("STEP_DEPTH_")) { name = name.substring(11); 665 fStepDepthMap.put(intValue, name); 666 } else if (name.startsWith("SUSPENDPOL_")) { name = name.substring(11); 668 fSuspendPolicyMap.put(intValue, name); 669 } else if (name.startsWith("MODIF_KIND_")) { name = name.substring(11); 671 fModifierKindMap.put(intValue, name); 672 } 673 } catch (IllegalAccessException e) { 674 } catch (IllegalArgumentException e) { 676 } 680 } 681 } 682 683 686 public static Map stepSizeMap() { 687 getConstantMaps(); 688 return fStepSizeMap; 689 } 690 691 694 public static Map stepDepthMap() { 695 getConstantMaps(); 696 return fStepDepthMap; 697 } 698 699 702 public static Map suspendPolicyMap() { 703 getConstantMaps(); 704 return fSuspendPolicyMap; 705 } 706 707 710 public static Map modifierKindMap() { 711 getConstantMaps(); 712 return fModifierKindMap; 713 } 714 715 class ExceptionFilter { 716 717 ReferenceTypeImpl fException = null; 718 719 boolean fNotifyCaught = false; 720 721 boolean fNotifyUncaught = false; 722 } 723 724 class ThreadStepFilter { 725 726 protected ThreadReferenceImpl fThread = null; 727 728 protected int fThreadStepSize; 729 730 protected int fThreadStepDepth; 731 } 732 } 733 | Popular Tags |