汇编代码转机器码 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;
});
Rust编程语言群 1036955113
java新手自学群 626070845
java/springboot/hadoop/JVM 群 4915800
Hadoop/mongodb(搭建/开发/运维)Q群481975850
GOLang Q1群:6848027
GOLang Q2群:450509103
GOLang Q3群:436173132
GOLang Q4群:141984758
GOLang Q5群:215535604
C/C++/QT群 1414577
单片机嵌入式/电子电路入门群群 306312845
MUD/LIB/交流群 391486684
Electron/koa/Nodejs/express 214737701
大前端群vue/js/ts 165150391
操作系统研发群:15375777
汇编/辅助/破解新手群:755783453
大数据 elasticsearch 群 481975850
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
java新手自学群 626070845
java/springboot/hadoop/JVM 群 4915800
Hadoop/mongodb(搭建/开发/运维)Q群481975850
GOLang Q1群:6848027
GOLang Q2群:450509103
GOLang Q3群:436173132
GOLang Q4群:141984758
GOLang Q5群:215535604
C/C++/QT群 1414577
单片机嵌入式/电子电路入门群群 306312845
MUD/LIB/交流群 391486684
Electron/koa/Nodejs/express 214737701
大前端群vue/js/ts 165150391
操作系统研发群:15375777
汇编/辅助/破解新手群:755783453
大数据 elasticsearch 群 481975850
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。