|
1 | 1 | package net.nandgr.debugger; |
2 | 2 |
|
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
3 | 4 | import com.fasterxml.jackson.databind.ObjectMapper; |
4 | | -import net.nandgr.debugger.asm.json.Asm; |
5 | | -import net.nandgr.debugger.asm.json.Code; |
6 | | -import net.nandgr.debugger.asm.json.Contract; |
7 | 5 | import net.nandgr.debugger.cfg.CFGCreatorDefault; |
8 | 6 | import net.nandgr.debugger.cfg.beans.BytecodeChunk; |
9 | 7 | import net.nandgr.debugger.cfg.beans.ContractBytecode; |
10 | 8 | import net.nandgr.debugger.cfg.beans.OpcodeSource; |
11 | 9 | import net.nandgr.debugger.cfg.graphviz.GraphVizCreator; |
| 10 | +import net.nandgr.debugger.disassembler.DisassemblerException; |
| 11 | +import net.nandgr.debugger.disassembler.LinkedDisassembler; |
| 12 | +import net.nandgr.debugger.report.Report; |
| 13 | +import net.nandgr.debugger.report.ReportException; |
| 14 | +import net.nandgr.debugger.solc.Solc; |
| 15 | +import net.nandgr.debugger.solc.solcjson.Code; |
| 16 | +import net.nandgr.debugger.solc.solcjson.Contract; |
| 17 | +import net.nandgr.debugger.solc.solcjson.SolcOutput; |
12 | 18 | import net.nandgr.debugger.trace.TraceService; |
13 | 19 | import net.nandgr.debugger.trace.response.json.DebugTraceTransactionLog; |
14 | | -import net.nandgr.debugger.trace.response.json.DebugTraceTransactionResponse; |
15 | | -import net.nandgr.eth.Disassembler; |
16 | | -import net.nandgr.eth.Opcode; |
17 | 20 |
|
18 | 21 | import java.io.File; |
19 | 22 | import java.io.IOException; |
20 | 23 | import java.nio.file.Files; |
21 | | -import java.rmi.server.ExportException; |
22 | | -import java.util.ArrayList; |
| 24 | +import java.nio.file.Path; |
| 25 | +import java.nio.file.Paths; |
23 | 26 | import java.util.List; |
24 | 27 | import java.util.Map; |
25 | 28 |
|
26 | 29 | public class Main { |
27 | 30 |
|
28 | | - public static void main(String[] args) throws Exception { |
29 | | - |
30 | | - String name = "Impl.sol"; |
31 | | - String code = "608060405260043610610057576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680630c045f2d1461005c57806367e0badb14610093578063cd16ecbf146100be575b600080fd5b34801561006857600080fd5b5061009160048036038101908080359060200190929190803590602001909291905050506100eb565b005b34801561009f57600080fd5b506100a86100fd565b6040518082815260200191505060405180910390f35b3480156100ca57600080fd5b506100e960048036038101908080359060200190929190505050610106565b005b81600081905550806001819055505050565b60008054905090565b80600081905550505600a165627a7a7230582039dcdb7685335c75b3515b47d970516fe695dfb1f28195e1909aaf4f869118550029"; |
32 | | - File asmFile = new File("asm.asm"); |
33 | | - |
34 | | - byte[] bytes = Files.readAllBytes(asmFile.toPath()); |
35 | | - String asm = new String(bytes); |
| 31 | + public static void main(String[] args){ |
| 32 | + // Will be parameters |
| 33 | + String nodeUrl = "http://127.0.0.1:8545"; |
| 34 | + String txHash = "0xc296ffe00b37d7740f22f2227f39b210a4928756f9472a37cab61951cf9fbffa"; |
| 35 | + String solidityFile = "/home/nando/ethereum/temp/Impl.sol"; |
36 | 36 |
|
37 | 37 | ObjectMapper objectMapper = new ObjectMapper(); |
38 | | - Asm asmObject = objectMapper.readValue(asm, Asm.class); |
39 | | - List<Code> asmCode = asmObject.getContracts().get("Impl.sol:Impl").getAsm().getData().get("0").getCode(); |
40 | | - |
41 | | - asmCode.removeIf(elem -> elem.getName().equals("tag")); |
42 | 38 |
|
43 | | - Disassembler disassembler = new Disassembler(code); |
44 | | - |
45 | | - List<Opcode> opcodes = disassembler.getOpcodes(); |
| 39 | + if (!Solc.checkSolcInClasspath()) { |
| 40 | + System.out.println("solc was not found in classpath"); |
| 41 | + System.exit(0); |
| 42 | + } |
46 | 43 |
|
47 | | - if (opcodes.size()-1 != asmCode.size()) { |
48 | | - // TODO move to a new exception somewhere |
49 | | - throw new Exception("Asm does not match with bytecode. Opcodes size: " + opcodes.size() + ", asm size: " + asmCode.size()); |
| 44 | + TraceService traceService = new TraceService(nodeUrl); |
| 45 | + Map<Integer, DebugTraceTransactionLog> traceData = null; |
| 46 | + try { |
| 47 | + traceData = traceService.getTraceData(txHash); |
| 48 | + } catch (IOException e) { |
| 49 | + System.out.println("Failed when getting transaction trace"); |
| 50 | + e.printStackTrace(); |
| 51 | + System.exit(0); |
| 52 | + } |
| 53 | + Path path = Paths.get(solidityFile); |
| 54 | + String sourceCode = null; |
| 55 | + try { |
| 56 | + sourceCode = new String(Files.readAllBytes(path)); |
| 57 | + } catch (IOException e) { |
| 58 | + System.out.println("Failed when reading source code from source file"); |
| 59 | + e.printStackTrace(); |
| 60 | + System.exit(0); |
| 61 | + } |
| 62 | + String fileName = path.getFileName().toString(); |
| 63 | + String contractName = fileName.substring(0, fileName.lastIndexOf(".")); |
| 64 | + |
| 65 | + Solc solc = new Solc(solidityFile); |
| 66 | + SolcOutput solcOutput = null; |
| 67 | + try { |
| 68 | + solcOutput = solc.compile(); |
| 69 | + } catch (IOException | InterruptedException e) { |
| 70 | + System.out.println("Failed when compiling source"); |
| 71 | + e.printStackTrace(); |
| 72 | + System.exit(0); |
50 | 73 | } |
51 | 74 |
|
52 | | - List<OpcodeSource> opcodeSources = new ArrayList<>(); |
53 | | - for (int i = 0; i < asmCode.size(); i++) { |
54 | | - OpcodeSource opcodeSource = new OpcodeSource(opcodes.get(i)); |
55 | | - Code currentAsmOpcode = asmCode.get(i); |
56 | | - opcodeSource.setBegin(currentAsmOpcode.getBegin()); |
57 | | - opcodeSource.setEnd(currentAsmOpcode.getEnd()); |
58 | | - opcodeSources.add(opcodeSource); |
| 75 | + Contract contract = solcOutput.getContracts().get(solidityFile + ":" + contractName); |
| 76 | + List<Code> asmCode = contract.getAsm().getData().get("0").getCode(); |
| 77 | + String code = contract.getBinRuntime(); |
| 78 | + |
| 79 | + LinkedDisassembler disassembler = new LinkedDisassembler(code); |
| 80 | + List<OpcodeSource> opcodeSources = null; |
| 81 | + try { |
| 82 | + opcodeSources = disassembler.getOpcodeSources(asmCode); |
| 83 | + } catch (DisassemblerException e) { |
| 84 | + System.out.println("Failed when disassembling"); |
| 85 | + e.printStackTrace(); |
| 86 | + System.exit(0); |
59 | 87 | } |
60 | 88 |
|
61 | 89 | CFGCreatorDefault cfgCreatorDefault = new CFGCreatorDefault(); |
62 | 90 |
|
63 | 91 | ContractBytecode contractBytecode = cfgCreatorDefault.createContractBytecode(opcodeSources); |
64 | 92 | Map<Integer, BytecodeChunk> runtimeChunks = contractBytecode.getRuntime().getChunks(); |
65 | 93 |
|
66 | | - TraceService traceService = new TraceService("http://127.0.0.1:8545"); |
67 | | - Map<Integer, DebugTraceTransactionLog> traceData = traceService.getTraceData("0xc296ffe00b37d7740f22f2227f39b210a4928756f9472a37cab61951cf9fbffa"); |
| 94 | + String traceMapJson = null; |
| 95 | + try { |
| 96 | + traceMapJson = objectMapper.writeValueAsString(traceData); |
| 97 | + } catch (JsonProcessingException e) { |
| 98 | + System.out.println("Failed when mapping trace data"); |
| 99 | + e.printStackTrace(); |
| 100 | + System.exit(0); |
| 101 | + } |
68 | 102 |
|
69 | 103 | GraphVizCreator graphVizCreator = new GraphVizCreator(runtimeChunks, traceData); |
70 | 104 | String graph = graphVizCreator.buildStringGraph(); |
71 | | - System.out.println(graph); |
72 | 105 |
|
73 | | -// for (Map.Entry<Integer, BytecodeChunk> integerBytecodeChunkEntry : contractBytecode.getRuntime().getChunks().entrySet()) { |
74 | | -// for (Opcode opcode : integerBytecodeChunkEntry.getValue().getOpcodes()) { |
75 | | -// Integer key = integerBytecodeChunkEntry.getKey(); |
76 | | -// System.out.println(Integer.toHexString(key) + "-> " + opcode.toString()); |
77 | | -// } |
78 | | -// } |
79 | | - |
80 | | -// for (OpcodeSource opcode : opcodeSources) { |
81 | | -// System.out.println(opcode.toString()); |
82 | | -// } |
| 106 | + Report report = new Report(sourceCode, traceMapJson, graph); |
| 107 | + try { |
| 108 | + report.createReport(); |
| 109 | + } catch (ReportException e) { |
| 110 | + System.out.println("Failed when creating report"); |
| 111 | + e.printStackTrace(); |
| 112 | + System.exit(0); |
| 113 | + } |
83 | 114 | } |
84 | 115 | } |
0 commit comments