refer to:
https://medium.com/@kulman.david/how-to-use-remix-debugger-to-learn-solidity-assembly-5a745a22bb07
1. copy代码到remix
2. compile, deploy
3. 调用
调用后,可以看到debug信息
步骤,对应的代码,消耗的gas:
具体使用:
// SPDX-License-Identifier: MIT pragma solidity ^0.8.17; contract Yular { address public owner; bool public flag; modifier onlyOwner() { require(owner == msg.sender,"You are not owner"); _; } constructor() { owner = msg.sender; flag = false; } event ExtCodeSizeEvent(uint256 size); event CallerEvent(uint256 caller); event shlEvent(uint256 shl); event ifEvent(bool r); event MyLogEvent(uint256 value); // 1. from address wallet // 2. from contract function func() public { bytes32 extCodeSizeEventHash = bytes32(keccak256("ExtCodeSizeEvent(uint256)")); bytes32 callerEventHash = bytes32(keccak256("CallerEvent(uint256)")); bytes32 shlEventhash = bytes32(keccak256("shlEvent(uint256)")); bytes32 ifEventhash = bytes32(keccak256("ifEvent(bool)")); bytes32 myLogEventhash = bytes32(keccak256("MyLogEvent(uint256)")); assembly { let size := extcodesize(caller()) mstore(0x80, caller()) log1(0x80, 32, callerEventHash) mstore(0x80, size) log1(0x80, 32, extCodeSizeEventHash) mstore(0x80, shl(0x6,2)) log1(0x80, 32, shlEventhash) mstore(0x80, gt(size, shl(0x6,2))) log1(0x80, 32, ifEventhash) mstore(0x80, 1111) log1(0x80, 32, myLogEventhash) if gt(size, shl(0x6,2)) { mstore(0x80, 4001) log1(0x80, 32, myLogEventhash) // invalid() } mstore(0x80, 2222) log1(0x80, 32, myLogEventhash) /* for { let i := 0 } lt(i, 0x4) { i := add(i, 1) } { mstore(0x80, i) log1(0x80, 32, myLogEventhash) mstore(0, blockhash(mod(caller(),4))) let success := call(gas(), caller(), 0, shl(0x5, 1), 0, 0,0) if eq(success, 0) { mstore(0x80, 4002) log1(0x80, 32, myLogEventhash) // invalid() } returndatacopy(0, 0, shl(0x5, 1)) switch eq(i, mload(0)) case 0 { mstore(0x80, 4003) log1(0x80, 32, myLogEventhash) // invalid() } } */ } owner = msg.sender; } function targetCall (address target,bytes memory data) public onlyOwner(){ (bool success,bytes memory returnData)=target.delegatecall(data); } function isSolved() public view returns (bool){ return flag; } }
部署到remix上,
ctrl + s : 编译
每次修改代码之后,ctrl + s , 然后 deploy:
看到log了,就可以调试了。
如果程序报错,该tx 就没有任何event ( 因为该tx也没有,而event log 是依附于 tx的)