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
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
*/
public class Attacher {

/**
* Indicates that any error during attachment should be dumped to a given file location.
*/
public static final String DUMP_PROPERTY = "net.bytebuddy.agent.attacher.dump";
/**
* Indicates that any error during attachment should be dumped to a given file location.
* When set for external attachment, diagnostic information is appended to this file.
*/
public static final String DUMP_PROPERTY = "net.bytebuddy.agent.attacher.dump";

/**
* The attacher provides only {@code static} utility methods and should not be instantiated.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -670,18 +670,22 @@ private static void installExternal(AttachmentProvider.Accessor.ExternalAttachme
for (File jar : externalAttachment.getClassPath()) {
classPath.append(File.pathSeparatorChar).append(jar.getCanonicalPath());
}
if (new ProcessBuilder(System.getProperty(JAVA_HOME)
+ File.separatorChar + "bin"
+ File.separatorChar + (System.getProperty(OS_NAME, "").toLowerCase(Locale.US).contains("windows") ? "java.exe" : "java"),
"-D" + Attacher.DUMP_PROPERTY + AGENT_ARGUMENT_SEPARATOR + System.getProperty(Attacher.DUMP_PROPERTY, ""),
CLASS_PATH_ARGUMENT,
classPath.toString(),
Attacher.class.getName(),
externalAttachment.getVirtualMachineType(),
processId,
agent.getAbsolutePath(),
Boolean.toString(isNative),
argument == null ? "" : (AGENT_ARGUMENT_SEPARATOR + argument)).start().waitFor() != 0) {
List<String> command = new ArrayList<String>();
command.add(System.getProperty(JAVA_HOME) + File.separatorChar + "bin" + File.separatorChar
+ (System.getProperty(OS_NAME, "").toLowerCase(Locale.US).contains("windows") ? "java.exe" : "java"));
command.add("-D" + Attacher.DUMP_PROPERTY + AGENT_ARGUMENT_SEPARATOR + System.getProperty(Attacher.DUMP_PROPERTY, ""));
command.add(CLASS_PATH_ARGUMENT);
command.add(classPath.toString());
command.add(Attacher.class.getName());
command.add(externalAttachment.getVirtualMachineType());
command.add(processId);
command.add(agent.getAbsolutePath());
command.add(Boolean.toString(isNative));
command.add(argument == null ? "" : (AGENT_ARGUMENT_SEPARATOR + argument));

dumpExternalAttachmentCommand(command);

if (new ProcessBuilder(command).start().waitFor() != 0) {
throw new IllegalStateException("Could not self-attach to current VM using external process - set a property "
+ Attacher.DUMP_PROPERTY
+ " to dump the process output to a file at the specified location");
Expand All @@ -694,6 +698,37 @@ private static void installExternal(AttachmentProvider.Accessor.ExternalAttachme
}
}
}
/**
* Dumps the external attachment command if a dump location is configured.
*
* @param arguments The external attachment process arguments.
*/
private static void dumpExternalAttachmentCommand(List<String> arguments) {
String dump = System.getProperty(Attacher.DUMP_PROPERTY);
if (dump == null || dump.length() == 0) {
return;
}
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(dump, true);
outputStream.write("Byte Buddy external attachment command line:".getBytes("UTF-8"));
outputStream.write(System.lineSeparator().getBytes("UTF-8"));
for (String argument : arguments) {
outputStream.write(argument.getBytes("UTF-8"));
outputStream.write(System.lineSeparator().getBytes("UTF-8"));
}
} catch (IOException ignored) {
/* do nothing */
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException ignored) {
/* do nothing */
}
}
}
}

/**
* Attempts to resolve the location of the {@link Attacher} class for a self-attachment. Doing so avoids the creation of a temporary jar file.
Expand Down