github.com/platonnetwork/platon-go@v0.7.6/eth/tracers/internal/tracers/evmdis_tracer.js (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // evmdisTracer returns sufficent information from a trace to perform evmdis-style
    18  // disassembly.
    19  {
    20  	stack: [{ops: []}],
    21  
    22  	npushes: {0: 0, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1, 10: 1, 11: 1, 16: 1, 17: 1, 18: 1, 19: 1, 20: 1, 21: 1, 22: 1, 23: 1, 24: 1, 25: 1, 26: 1, 32: 1, 48: 1, 49: 1, 50: 1, 51: 1, 52: 1, 53: 1, 54: 1, 55: 0, 56: 1, 57: 0, 58: 1, 59: 1, 60: 0, 64: 1, 65: 1, 66: 1, 67: 1, 68: 1, 69: 1, 80: 0, 81: 1, 82: 0, 83: 0, 84: 1, 85: 0, 86: 0, 87: 0, 88: 1, 89: 1, 90: 1, 91: 0, 96: 1, 97: 1, 98: 1, 99: 1, 100: 1, 101: 1, 102: 1, 103: 1, 104: 1, 105: 1, 106: 1, 107: 1, 108: 1, 109: 1, 110: 1, 111: 1, 112: 1, 113: 1, 114: 1, 115: 1, 116: 1, 117: 1, 118: 1, 119: 1, 120: 1, 121: 1, 122: 1, 123: 1, 124: 1, 125: 1, 126: 1, 127: 1, 128: 2, 129: 3, 130: 4, 131: 5, 132: 6, 133: 7, 134: 8, 135: 9, 136: 10, 137: 11, 138: 12, 139: 13, 140: 14, 141: 15, 142: 16, 143: 17, 144: 2, 145: 3, 146: 4, 147: 5, 148: 6, 149: 7, 150: 8, 151: 9, 152: 10, 153: 11, 154: 12, 155: 13, 156: 14, 157: 15, 158: 16, 159: 17, 160: 0, 161: 0, 162: 0, 163: 0, 164: 0, 240: 1, 241: 1, 242: 1, 243: 0, 244: 0, 255: 0},
    23  
    24  	// result is invoked when all the opcodes have been iterated over and returns
    25  	// the final result of the tracing.
    26  	result: function() { return this.stack[0].ops; },
    27  
    28  	// fault is invoked when the actual execution of an opcode fails.
    29  	fault: function(log, db) { },
    30  
    31  	// step is invoked for every opcode that the VM executes.
    32  	step: function(log, db) {
    33  		var frame = this.stack[this.stack.length - 1];
    34  
    35  		var error = log.getError();
    36  		if (error) {
    37  			frame["error"] = error;
    38  		} else if (log.getDepth() == this.stack.length) {
    39  			opinfo = {
    40  				op:     log.op.toNumber(),
    41  				depth : log.getDepth(),
    42  				result: [],
    43  			};
    44  			if (frame.ops.length > 0) {
    45  				var prevop = frame.ops[frame.ops.length - 1];
    46  				for(var i = 0; i < this.npushes[prevop.op]; i++)
    47  					prevop.result.push(log.stack.peek(i).toString(16));
    48  			}
    49  			switch(log.op.toString()) {
    50  			case "CALL": case "CALLCODE":
    51  				var instart = log.stack.peek(3).valueOf();
    52  				var insize = log.stack.peek(4).valueOf();
    53  				opinfo["gas"] = log.stack.peek(0).valueOf();
    54  				opinfo["to"] = log.stack.peek(1).toString(16);
    55  				opinfo["value"] = log.stack.peek(2).toString();
    56  				opinfo["input"] = log.memory.slice(instart, instart + insize);
    57  				opinfo["error"] = null;
    58  				opinfo["return"] = null;
    59  				opinfo["ops"] = [];
    60  				this.stack.push(opinfo);
    61  				break;
    62  			case "DELEGATECALL": case "STATICCALL":
    63  				var instart = log.stack.peek(2).valueOf();
    64  				var insize = log.stack.peek(3).valueOf();
    65  				opinfo["op"] =  log.op.toString();
    66  				opinfo["gas"] =  log.stack.peek(0).valueOf();
    67  				opinfo["to"] =  log.stack.peek(1).toString(16);
    68  				opinfo["input"] =  log.memory.slice(instart, instart + insize);
    69  				opinfo["error"] =  null;
    70  				opinfo["return"] =  null;
    71  				opinfo["ops"] = [];
    72  				this.stack.push(opinfo);
    73  				break;
    74  			case "RETURN":
    75  				var out = log.stack.peek(0).valueOf();
    76  				var outsize = log.stack.peek(1).valueOf();
    77  				frame.return = log.memory.slice(out, out + outsize);
    78  				break;
    79  			case "STOP": case "SUICIDE":
    80  				frame.return = log.memory.slice(0, 0);
    81  				break;
    82  			case "JUMPDEST":
    83  				opinfo["pc"] = log.getPC();
    84  			}
    85  			if(log.op.isPush()) {
    86  				opinfo["len"] = log.op.toNumber() - 0x5e;
    87  			}
    88  			frame.ops.push(opinfo);
    89  		} else {
    90  			this.stack = this.stack.slice(0, log.getDepth());
    91  		}
    92  	}
    93  }