|
1 | 1 | /* |
2 | | - * Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
@@ -96,22 +96,30 @@ private void loadAgentLibrary(String agentLibrary, boolean isAbsolute, String op |
96 | 96 | } |
97 | 97 |
|
98 | 98 | String msgPrefix = "return code: "; |
99 | | - InputStream in = execute("load", |
100 | | - agentLibrary, |
101 | | - isAbsolute ? "true" : "false", |
102 | | - options); |
103 | | - try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) { |
104 | | - String result = reader.readLine(); |
105 | | - if (result == null) { |
| 99 | + String errorMsg = "Failed to load agent library"; |
| 100 | + try { |
| 101 | + InputStream in = execute("load", |
| 102 | + agentLibrary, |
| 103 | + isAbsolute ? "true" : "false", |
| 104 | + options); |
| 105 | + String result = readErrorMessage(in); |
| 106 | + if (result.isEmpty()) { |
106 | 107 | throw new AgentLoadException("Target VM did not respond"); |
107 | 108 | } else if (result.startsWith(msgPrefix)) { |
108 | 109 | int retCode = Integer.parseInt(result.substring(msgPrefix.length())); |
109 | 110 | if (retCode != 0) { |
110 | 111 | throw new AgentInitializationException("Agent_OnAttach failed", retCode); |
111 | 112 | } |
112 | 113 | } else { |
113 | | - throw new AgentLoadException(result); |
| 114 | + if (!result.isEmpty()) { |
| 115 | + errorMsg += ": " + result; |
| 116 | + } |
| 117 | + throw new AgentLoadException(errorMsg); |
114 | 118 | } |
| 119 | + } catch (AttachOperationFailedException ex) { |
| 120 | + // execute() throws AttachOperationFailedException if attach agent reported error. |
| 121 | + // Convert it to AgentLoadException. |
| 122 | + throw new AgentLoadException(errorMsg + ": " + ex.getMessage()); |
115 | 123 | } |
116 | 124 | } |
117 | 125 |
|
@@ -364,6 +372,9 @@ String readErrorMessage(InputStream in) throws IOException { |
364 | 372 | StringBuilder message = new StringBuilder(); |
365 | 373 | BufferedReader br = new BufferedReader(new InputStreamReader(in)); |
366 | 374 | while ((s = br.readLine()) != null) { |
| 375 | + if (message.length() > 0) { |
| 376 | + message.append(' '); |
| 377 | + } |
367 | 378 | message.append(s); |
368 | 379 | } |
369 | 380 | return message.toString(); |
@@ -399,20 +410,10 @@ void processCompletionStatus(IOException ioe, String cmd, InputStream sis) throw |
399 | 410 | throw new IOException("Protocol mismatch with target VM"); |
400 | 411 | } |
401 | 412 |
|
402 | | - // Special-case the "load" command so that the right exception is |
403 | | - // thrown. |
404 | | - if (cmd.equals("load")) { |
405 | | - String msg = "Failed to load agent library"; |
406 | | - if (!message.isEmpty()) { |
407 | | - msg += ": " + message; |
408 | | - } |
409 | | - throw new AgentLoadException(msg); |
410 | | - } else { |
411 | | - if (message.isEmpty()) { |
412 | | - message = "Command failed in target VM"; |
413 | | - } |
414 | | - throw new AttachOperationFailedException(message); |
| 413 | + if (message.isEmpty()) { |
| 414 | + message = "Command failed in target VM"; |
415 | 415 | } |
| 416 | + throw new AttachOperationFailedException(message); |
416 | 417 | } |
417 | 418 | } |
418 | 419 |
|
|
0 commit comments