Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/src/main/java/com/taobao/arthas/core/Arthas.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ private void attachAgent(Configure configure) throws Exception {
boolean crossMountNamespace = inDifferentMountNamespace(targetPid);
Path socketSymlink = null;
if (crossMountNamespace) {
configure.setCrossMountNamespace(true);
socketSymlink = exposeTargetAttachSocket(targetPid);
}
Comment on lines 108 to 111

Expand Down
13 changes: 13 additions & 0 deletions core/src/main/java/com/taobao/arthas/core/config/Configure.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public class Configure {
private String arthasCore;
private String arthasAgent;

/**
* attach 进程和目标 JVM 是否处于不同的 mount namespace,仅用于传递内部 attach 元数据。
*/
private Boolean crossMountNamespace;

private String tunnelServer;
private String agentId;

Expand Down Expand Up @@ -137,6 +142,14 @@ public void setArthasCore(String arthasCore) {
this.arthasCore = arthasCore;
}

public boolean isCrossMountNamespace() {
return crossMountNamespace != null && crossMountNamespace;
}

public void setCrossMountNamespace(boolean crossMountNamespace) {
this.crossMountNamespace = crossMountNamespace;
}

public Long getSessionTimeout() {
return sessionTimeout;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,13 +510,24 @@ private void bind(Configure configure) throws Throwable {
}

logger().info("as-server started in {} ms", System.currentTimeMillis() - start);
logCrossMountNamespaceAttachSuccess(configure, arthasHome(), logger());
} catch (Throwable e) {
logger().error("Error during start as-server", e);
destroy();
throw e;
}
}

static void logCrossMountNamespaceAttachSuccess(Configure configure, String arthasHome, Logger logger) {
if (!configure.isCrossMountNamespace()) {
return;
}
logger.info("event=arthas_attach status=success mode=cross-mount-namespace "
+ "targetPid={} arthasHome={} network={} telnet={} http={}",
configure.getJavaPid(), arthasHome, configure.getIp(), configure.getTelnetPort(),
configure.getHttpPort());
}

private CommandResolver loadExternalCommandResolver(ShellServer shellServer, BuiltinCommandPack builtinCommands)
throws Throwable {
String arthasHome = reslove(arthasEnvironment, ARTHAS_HOME_PROPERTY, arthasHome());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.taobao.arthas.core.config;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

import com.taobao.arthas.core.env.ArthasEnvironment;
import com.taobao.arthas.core.env.MapPropertySource;

public class ConfigureTest {

@Test
public void shouldTransmitCrossMountNamespaceMetadataThroughAgentArgs() {
Configure source = new Configure();
source.setJavaPid(123L);
source.setCrossMountNamespace(true);

Map<String, String> attachArgs = FeatureCodec.DEFAULT_COMMANDLINE_CODEC.toMap(source.toString());
Map<String, Object> targetArgs = new HashMap<String, Object>();
for (Map.Entry<String, String> entry : attachArgs.entrySet()) {
targetArgs.put("arthas." + entry.getKey(), entry.getValue());
}
ArthasEnvironment environment = new ArthasEnvironment();
environment.addFirst(new MapPropertySource("agentArgs", targetArgs));
Configure target = new Configure();
BinderUtils.inject(environment, target);

assertThat(target.getJavaPid()).isEqualTo(123L);
assertThat(target.isCrossMountNamespace()).isTrue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.junit.Test;
import org.mockito.Mockito;

import com.alibaba.arthas.deps.org.slf4j.Logger;
import com.alibaba.bytekit.utils.ReflectionUtils;
import com.taobao.arthas.common.JavaVersionUtils;
import com.taobao.arthas.core.bytecode.TestHelper;
Expand Down Expand Up @@ -105,4 +106,31 @@ public void testConfigName() throws Exception {
System.clearProperty(ArthasBootstrap.CONFIG_NAME_PROPERTY);
assertThat(configName).isEqualTo("testName");
}

@Test
public void shouldLogSuccessfulCrossMountNamespaceAttach() {
Configure configure = new Configure();
configure.setJavaPid(123L);
configure.setCrossMountNamespace(true);
configure.setIp("127.0.0.1");
configure.setTelnetPort(3658);
configure.setHttpPort(8563);
Logger logger = Mockito.mock(Logger.class);

ArthasBootstrap.logCrossMountNamespaceAttachSuccess(configure, "/tmp/arthas-123", logger);

Mockito.verify(logger).info("event=arthas_attach status=success mode=cross-mount-namespace "
+ "targetPid={} arthasHome={} network={} telnet={} http={}",
123L, "/tmp/arthas-123", "127.0.0.1", 3658, 8563);
}

@Test
public void shouldNotLogSuccessfulSameMountNamespaceAttach() {
Configure configure = new Configure();
Logger logger = Mockito.mock(Logger.class);

ArthasBootstrap.logCrossMountNamespaceAttachSuccess(configure, "/opt/arthas", logger);

Mockito.verifyNoInteractions(logger);
}
}
Loading