test/hotspot/jtreg/serviceability/sa/sadebugd/DebugdUtils.java
changeset 55650 56e8c0a3fe9a
equal deleted inserted replaced
55649:ad8e3b295615 55650:56e8c0a3fe9a
       
     1 /*
       
     2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 import java.io.BufferedReader;
       
    25 import java.io.InputStreamReader;
       
    26 import java.io.IOException;
       
    27 
       
    28 import jdk.test.lib.JDKToolLauncher;
       
    29 
       
    30 
       
    31 public class DebugdUtils {
       
    32 
       
    33     private static final String GOLDEN = "Debugger attached";
       
    34 
       
    35     private final String id;
       
    36 
       
    37     private Process debugdProcess;
       
    38 
       
    39     public DebugdUtils(String id) {
       
    40         this.id = id;
       
    41         debugdProcess = null;
       
    42     }
       
    43 
       
    44     public void attach(long pid) throws IOException {
       
    45         JDKToolLauncher jhsdbLauncher = JDKToolLauncher.createUsingTestJDK("jhsdb");
       
    46         jhsdbLauncher.addToolArg("debugd");
       
    47         jhsdbLauncher.addToolArg("--pid");
       
    48         jhsdbLauncher.addToolArg(Long.toString(pid));
       
    49         if (id != null) {
       
    50             jhsdbLauncher.addToolArg("--serverid");
       
    51             jhsdbLauncher.addToolArg(id);
       
    52         }
       
    53         debugdProcess = (new ProcessBuilder(jhsdbLauncher.getCommand())).start();
       
    54 
       
    55         // Wait until debug server attached
       
    56         try (BufferedReader reader = new BufferedReader(new InputStreamReader(debugdProcess.getErrorStream()))) {
       
    57             String line;
       
    58             while ((line = reader.readLine()) != null) {
       
    59                 if (line.contains(GOLDEN)) {
       
    60                     break;
       
    61                 }
       
    62             }
       
    63         }
       
    64     }
       
    65 
       
    66     public void detach() throws InterruptedException {
       
    67         if (debugdProcess != null) {
       
    68             debugdProcess.destroy();
       
    69             debugdProcess.waitFor();
       
    70         }
       
    71     }
       
    72 }