0

I am trying to debug a contract on Remix IDE:

pragma solidity ^0.5.1;
contract DebuggerSampleContract{
   int counter = 10;
   function LoopCounter(int _input) public view returns (int) {
      int returnValue;
      for(; _input <counter; _input++)
      {
         returnValue += _input;
       }
       returnValue;
   }
}

I selected following steps: 1. Solidity Compiler, 2. Compile DebuggerSampleContract.sol, 3. Deployand Run Transactions, 4. Deploy, 5. Clicked arrow to see the function, 6. Debug, 7. Step over (whole contract got selected), 8. step Over, only the value 10 selected, 9. Step Over, It did not enter the function, instead it went to pragma statement.

@goodvibration Based upon the comments, I changed the contract to:

contract DebuggerSampleContract{
   int counter = 10;
   constructor(int _input)  public{
      int returnValue;
      for(; _input <counter; _input++)
      {
         returnValue += _input;
       }
       //returnValue;
   }
}

I started debugging and put the constructor argument to 6. When I pressed debug, it showed me machine language instructions i.e. EVM bytecode. I pressed "Step forward" but I don't know how to keep track of the local variables and state variables through bytecode.

Somebody please guide me.

Zulfi.

Debugging the constructor, can't understand how to locate var through bytecode

  • 2
    For the sake of debugging, maybe change the function to a constructor. – goodvibration Jan 2 at 6:26
  • 1
    sounds like you are debugging the deployment transaction; since you didn't run your function, the code you are debugging would be the deploy, which initializes your contract instance using the constructor. – Chan-Ho Suh Jan 13 at 1:54
  • I don't think your revised code would compile, let alone be available for debug, as you have made the constructor return an int and declared it as view (non-state changing). – Chan-Ho Suh Jan 13 at 1:57
  • Sorry, I forgot to remove view. I think it compiled on Remix IDE and asked me for the argument of constructor which I gave as 6. I don't know how to trace the variables through bytecode. – zak100 Jan 13 at 2:02
1

As @Chan-Ho Suh suggested in the comment you are looking through the debug of the transaction of the contract being deployed. As in your initial question, there is no constructor, there is nothing to step through.

The problem is that you are not calling the function LoopCounter after deploying the contract in your step 5, you need to enter a value and click the function name for it to run.

In the Deploy and Run Transactions screen, once you deploy your contract, you will see a gray box representing the contract that you just deployed (1). If you click the arrow in this box, it will show you the functions available in this contract. Add the number you'd like to test and click the blue button with the function name to call the function (2). Now you'll see another log in the console which you can use to debug this function (3). Using this debug, the debugger steps through the function as desired.

enter image description here

(the problem is that you need return returnValue instead of just returnValue)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.