汇编代码转机器码 java 调用gcc 和lldb 等shell程序的方法 调objdump objcopy

java 调用gcc 和lldb 等shell程序的方法,我这里是调用的是gcc 的objdump工具 用于生成汇编代码

直接调用控制台程序会出现死锁的问题,java特色,所以封装一下

封装

package com.far.vms.opencar.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.function.Function;

/*
 * @description:
 *
 * 调用操作系统的Shell 需要兼容mac linux win
 *
 * @author mike/Fang.J
 * @data 2022/11/29
 */
public class ShellUtil {


    public static class RunShellThread implements Runnable {

        private InputStream inputStream;

        private Function<String, Integer> caller;

        public Function<String, Integer> getCaller() {
            return caller;
        }

        public RunShellThread setCaller(Function<String, Integer> caller) {
            this.caller = caller;
            return this;
        }

        public InputStream getInputStream() {
            return inputStream;
        }

        public RunShellThread setInputStream(InputStream inputStream) {
            this.inputStream = inputStream;
            return this;
        }

        @Override
        public void run() {

            InputStreamReader inputStreamReader = null;
            BufferedReader br = null;
            try {
                inputStreamReader = new InputStreamReader(inputStream);
                br = new BufferedReader(inputStreamReader);
                // 打印信息
                String line = null;
                while ((line = br.readLine()) != null) {
                    caller.apply(line);
                    //System.out.println(line);
                }
            } catch (IOException ioe) {
                ioe.printStackTrace();
            } finally {
                try {
                    br.close();
                    inputStreamReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    /**
     * @param shellProgPath
     * @param args
     * @param caller
     * @description:
     * @return: void
     * @author mike/Fang.J
     * @data 2022/11/29
     */
    public static void runShell(String shellProgPath, String[] args, Function<String, Integer> caller) {

        try {
            Process p = Runtime.getRuntime().exec(args);
            //caller没有考虑线程安全
            new Thread(new RunShellThread().setCaller(caller).setInputStream(p.getInputStream())).start();
            new Thread(new RunShellThread().setCaller(caller).setInputStream(p.getErrorStream())).start();
            p.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

调用

   String kr = "D:\\AAAA_WORK\\RISC-V-Tools\\os\\riscv-operating-system-mooc\\code\\os\\01-helloRVOS\\build\\kernel-img.img";
        String buildTool = "D:\\AAAA_WORK\\RISC-V-Tools\\riscv64-unknown-elf-toolchain-10.2.0-2020.12.8-x86_64-w64-mingw32\\bin\\riscv64-unknown-elf-objdump.exe";

        ShellUtil.runShell(buildTool, new String[]{buildTool,kr ,"-d"}, cmdInf -> {
            System.out.println(cmdInf);
            return 0;
        });


 String buildTool = settingDatas.getBuild().getGccPath() + "\\riscv64-unknown-elf-objcopy.exe";
        String elfFile = settingDatas.getBuild().getProgFilePath() + "/" + settingDatas.getBuild().getProgName() + "." + settingDatas.getBuild().getProgSufix();
        String outFile = settingDatas.getBuild().getProgFilePath() + "/" + settingDatas.getBuild().getProgName() + ".bin";
        ShellUtil.runShell(buildTool, new String[]{buildTool,
                "-I",
                "elf64-littleriscv",
                elfFile,
                "-g",
                "-S",
                "-O",
                "binary",
                outFile

        }, cmdInf -> {
            System.out.println(cmdInf);
            return 0;
        });

posted @ 2022-11-29 19:44  方东信  阅读(100)  评论(0编辑  收藏  举报