1 /*
   2  * Copyright (c) 2019, Red Hat, Inc.
   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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 import java.io.File;
  27 import java.io.PrintWriter;
  28 import java.nio.file.Files;
  29 import java.nio.file.Paths;
  30 import java.nio.file.StandardOpenOption;
  31 import java.util.Arrays;
  32 import java.util.List;
  33 import java.util.stream.Collectors;
  34 
  35 /**
  36  * Fake objcopy used by StripNativeDebugSymbolsTest. It prints the
  37  * passed in arguments to a log and creates a fake debug info file
  38  * for --only-keep-debug invocation. Note that the first argument is
  39  * the path to the log file. This argument will be omitted when
  40  * logged.
  41  *
  42  * Callers need to ensure the log file is properly truncated.
  43  *
  44  * @author Severin Gehwolf
  45  */
  46 public class FakeObjCopy {
  47 
  48     private static final String OBJCOPY_ONLY_KEEP_DEBUG_OPT = "--only-keep-debug";
  49 
  50     public static void main(String[] args) throws Exception {
  51         if (args.length < 1) {
  52             throw new AssertionError("At least one argument expected");
  53         }
  54         String[] objCopyArgs = new String[args.length - 1];
  55         System.arraycopy(args, 1, objCopyArgs, 0, objCopyArgs.length);
  56         String logFile = args[0];
  57         System.out.println("DEBUG: Fake objcopy called. Log file is: " + logFile);
  58         // Log options
  59         String line = Arrays.asList(objCopyArgs).stream().collect(Collectors.joining(" "));
  60         Files.write(Paths.get(logFile),
  61                     List.<String>of(line),
  62                     StandardOpenOption.APPEND,
  63                     StandardOpenOption.CREATE);
  64         // Handle --only-keep-debug option as plugin attempts to read
  65         // debug info file after this utility being called.
  66         if (objCopyArgs.length == 3 && OBJCOPY_ONLY_KEEP_DEBUG_OPT.equals(objCopyArgs[0])) {
  67             handleOnlyKeepDebug(objCopyArgs[2]);
  68         }
  69     }
  70 
  71     private static void handleOnlyKeepDebug(String dbgFile) throws Exception {
  72         try (PrintWriter pw = new PrintWriter(new File(dbgFile))) {
  73             pw.println("Fake objcopy debug info file");
  74         }
  75         System.out.println("DEBUG: wrote fake debug file " + dbgFile);
  76     }
  77 
  78 }