2 * Copyright 2000-2015 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.intellij.internal;
18 import com.intellij.notification.Notification;
19 import com.intellij.notification.NotificationType;
20 import com.intellij.notification.Notifications;
21 import com.intellij.openapi.application.ex.ApplicationManagerEx;
22 import com.intellij.openapi.diagnostic.Logger;
23 import com.intellij.util.Alarm;
24 import com.intellij.util.SingleAlarm;
25 import com.intellij.util.net.NetUtils;
27 import java.lang.management.ManagementFactory;
32 public class DebugAttachDetector {
33 private static final Logger LOG = Logger.getInstance(DebugAttachDetector.class);
35 private String myHost = null;
36 private int myPort = -1;
37 private SingleAlarm myAlarm;
38 private boolean myAttached;
39 private boolean myReady = false;
41 public DebugAttachDetector() {
42 if (!ApplicationManagerEx.getApplicationEx().isInternal() || "true".equals(System.getProperty("idea.debug.mode"))) return;
44 for (String argument : ManagementFactory.getRuntimeMXBean().getInputArguments()) {
45 if (argument.startsWith("-agentlib:jdwp")) {
46 String[] params = argument.split(",");
47 for (String param : params) {
48 if (param.startsWith("address")) {
50 String[] address = param.split("=")[1].split(":");
51 if (address.length == 1) {
52 myPort = Integer.parseInt(address[0]);
56 myPort = Integer.parseInt(address[1]);
70 if (myPort < 0) return;
72 myAlarm = new SingleAlarm(new Runnable() {
75 boolean attached = !NetUtils.canConnectToRemoteSocket(myHost, myPort);
77 myAttached = attached;
80 else if (attached != myAttached) {
81 myAttached = attached;
82 Notifications.Bus.notify(new Notification(Notifications.SYSTEM_MESSAGES_GROUP_ID,
84 myAttached ? "attached" : "detached",
85 NotificationType.WARNING));
89 }, 5000, Alarm.ThreadToUse.POOLED_THREAD, null);