Path: blob/master/test/jdk/tools/jlink/plugins/StripNativeDebugSymbolsPlugin/FakeObjCopy.java
41155 views
/*1* Copyright (c) 2019, Red Hat, Inc.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425import java.io.File;26import java.io.PrintWriter;27import java.nio.file.Files;28import java.nio.file.Paths;29import java.nio.file.StandardOpenOption;30import java.util.Arrays;31import java.util.List;32import java.util.stream.Collectors;3334/**35* Fake objcopy used by StripNativeDebugSymbolsTest. It prints the36* passed in arguments to a log and creates a fake debug info file37* for --only-keep-debug invocation. Note that the first argument is38* the path to the log file. This argument will be omitted when39* logged.40*41* Callers need to ensure the log file is properly truncated.42*43*/44public class FakeObjCopy {4546private static final String OBJCOPY_ONLY_KEEP_DEBUG_OPT = "--only-keep-debug";4748public static void main(String[] args) throws Exception {49if (args.length < 1) {50throw new AssertionError("At least one argument expected");51}52String[] objCopyArgs = new String[args.length - 1];53System.arraycopy(args, 1, objCopyArgs, 0, objCopyArgs.length);54String logFile = args[0];55System.out.println("DEBUG: Fake objcopy called. Log file is: " + logFile);56// Log options57String line = Arrays.asList(objCopyArgs).stream().collect(Collectors.joining(" "));58Files.write(Paths.get(logFile),59List.<String>of(line),60StandardOpenOption.APPEND,61StandardOpenOption.CREATE);62// Handle --only-keep-debug option as plugin attempts to read63// debug info file after this utility being called.64if (objCopyArgs.length == 3 && OBJCOPY_ONLY_KEEP_DEBUG_OPT.equals(objCopyArgs[0])) {65handleOnlyKeepDebug(objCopyArgs[2]);66}67}6869private static void handleOnlyKeepDebug(String dbgFile) throws Exception {70try (PrintWriter pw = new PrintWriter(new File(dbgFile))) {71pw.println("Fake objcopy debug info file");72}73System.out.println("DEBUG: wrote fake debug file " + dbgFile);74}7576}777879