github.com/aergoio/aergo@v1.3.1/cmd/brick/exec/debug.go (about)

     1  // +build Debug
     2  
     3  package exec
     4  
     5  import (
     6  	"fmt"
     7  	"strconv"
     8  
     9  	"github.com/aergoio/aergo/cmd/brick/context"
    10  	"github.com/aergoio/aergo/contract"
    11  )
    12  
    13  func init() {
    14  	registerExec(&setb{})
    15  	registerExec(&delb{})
    16  	registerExec(&listb{})
    17  	registerExec(&resetb{})
    18  	registerExec(&setw{})
    19  	registerExec(&delw{})
    20  	registerExec(&listw{})
    21  	registerExec(&resetw{})
    22  }
    23  
    24  // =====================================
    25  //             Breakpoint
    26  // =====================================
    27  
    28  // =========== setb ==============
    29  type setb struct{}
    30  
    31  func (c *setb) Command() string {
    32  	return "setb"
    33  }
    34  
    35  func (c *setb) Syntax() string {
    36  	return fmt.Sprintf("%s %s", "<line>", context.ContractSymbol)
    37  }
    38  
    39  func (c *setb) Usage() string {
    40  	return "setb <line> <contract_name>"
    41  }
    42  
    43  func (c *setb) Describe() string {
    44  	return "set breakpoint"
    45  }
    46  
    47  func (c *setb) Validate(args string) error {
    48  
    49  	_, _, err := c.parse(args)
    50  
    51  	return err
    52  }
    53  
    54  func (c *setb) parse(args string) (uint64, string, error) {
    55  	splitArgs := context.SplitSpaceAndAccent(args, false)
    56  	if len(splitArgs) < 2 {
    57  		return 0, "", fmt.Errorf("need 2 arguments. usage: %s", c.Usage())
    58  	}
    59  
    60  	line, err := strconv.ParseUint(splitArgs[0].Text, 10, 64)
    61  	if err != nil {
    62  		return 0, "", fmt.Errorf("fail to parse number %s: %s", splitArgs[1].Text, err.Error())
    63  	}
    64  
    65  	contractIDHex := contract.PlainStrToHexAddr(splitArgs[1].Text)
    66  
    67  	return line, contractIDHex, nil
    68  }
    69  
    70  func (c *setb) Run(args string) (string, error) {
    71  	line, contractIDHex, _ := c.parse(args)
    72  
    73  	err := contract.SetBreakPoint(contractIDHex, line)
    74  	if err != nil {
    75  		return "", err
    76  	}
    77  	addr, err := contract.HexAddrToBase58Addr(contractIDHex)
    78  	if err != nil {
    79  		return "", err
    80  	}
    81  
    82  	return "set breakpoint: " + fmt.Sprintf("%s:%d", addr, line), nil
    83  }
    84  
    85  // =========== delb ==============
    86  
    87  type delb struct{}
    88  
    89  func (c *delb) Command() string {
    90  	return "delb"
    91  }
    92  
    93  func (c *delb) Syntax() string {
    94  	return fmt.Sprintf("%s %s", "<line>", context.ContractSymbol)
    95  }
    96  
    97  func (c *delb) Usage() string {
    98  	return "delb <line> <contract_name>"
    99  }
   100  
   101  func (c *delb) Describe() string {
   102  	return "delete breakpoint"
   103  }
   104  
   105  func (c *delb) Validate(args string) error {
   106  
   107  	_, _, err := c.parse(args)
   108  
   109  	return err
   110  }
   111  
   112  func (c *delb) parse(args string) (uint64, string, error) {
   113  	splitArgs := context.SplitSpaceAndAccent(args, false)
   114  	if len(splitArgs) < 2 {
   115  		return 0, "", fmt.Errorf("need 2 arguments. usage: %s", c.Usage())
   116  	}
   117  
   118  	line, err := strconv.ParseUint(splitArgs[0].Text, 10, 64)
   119  	if err != nil {
   120  		return 0, "", fmt.Errorf("fail to parse number %s: %s", splitArgs[1].Text, err.Error())
   121  	}
   122  
   123  	contractIDHex := contract.PlainStrToHexAddr(splitArgs[1].Text)
   124  
   125  	return line, contractIDHex, nil
   126  }
   127  
   128  func (c *delb) Run(args string) (string, error) {
   129  	line, contractIDHex, _ := c.parse(args)
   130  
   131  	err := contract.DelBreakPoint(contractIDHex, line)
   132  	if err != nil {
   133  		return "", err
   134  	}
   135  	addr, err := contract.HexAddrToBase58Addr(contractIDHex)
   136  	if err != nil {
   137  		return "", err
   138  	}
   139  
   140  	return "del breakpoint: " + fmt.Sprintf("%s:%d", addr, line), nil
   141  }
   142  
   143  // =========== listb ==============
   144  
   145  type listb struct{}
   146  
   147  func (c *listb) Command() string {
   148  	return "listb"
   149  }
   150  
   151  func (c *listb) Syntax() string {
   152  	return ""
   153  }
   154  
   155  func (c *listb) Usage() string {
   156  	return "listb"
   157  }
   158  
   159  func (c *listb) Describe() string {
   160  	return "list all breakpoints"
   161  }
   162  
   163  func (c *listb) Validate(args string) error {
   164  	return nil
   165  }
   166  
   167  func (c *listb) Run(args string) (string, error) {
   168  	contract.PrintBreakPoints()
   169  
   170  	return "list breakpoints", nil
   171  }
   172  
   173  // =========== resetb ==============
   174  
   175  type resetb struct{}
   176  
   177  func (c *resetb) Command() string {
   178  	return "resetb"
   179  }
   180  
   181  func (c *resetb) Syntax() string {
   182  	return ""
   183  }
   184  
   185  func (c *resetb) Usage() string {
   186  	return "resetb"
   187  }
   188  
   189  func (c *resetb) Describe() string {
   190  	return "reset all breakpoints"
   191  }
   192  
   193  func (c *resetb) Validate(args string) error {
   194  	return nil
   195  }
   196  
   197  func (c *resetb) Run(args string) (string, error) {
   198  	contract.ResetBreakPoints()
   199  
   200  	return "reset breakpoints", nil
   201  }
   202  
   203  // =====================================
   204  //             Watchpoint
   205  // =====================================
   206  
   207  // =========== setw ==============
   208  type setw struct{}
   209  
   210  func (c *setw) Command() string {
   211  	return "setw"
   212  }
   213  
   214  func (c *setw) Syntax() string {
   215  	return fmt.Sprintf("%s", "<watch_expr>")
   216  }
   217  
   218  func (c *setw) Usage() string {
   219  	return "setw `<watch_expr>`"
   220  }
   221  
   222  func (c *setw) Describe() string {
   223  	return "set watchpoint"
   224  }
   225  
   226  func (c *setw) Validate(args string) error {
   227  
   228  	_, err := c.parse(args)
   229  
   230  	return err
   231  }
   232  
   233  func (c *setw) parse(args string) (string, error) {
   234  	splitArgs := context.SplitSpaceAndAccent(args, false)
   235  	if len(splitArgs) < 1 {
   236  		return "", fmt.Errorf("need an arguments. usage: %s", c.Usage())
   237  	}
   238  
   239  	return splitArgs[0].Text, nil
   240  }
   241  
   242  func (c *setw) Run(args string) (string, error) {
   243  	watch_expr, _ := c.parse(args)
   244  
   245  	err := contract.SetWatchPoint(watch_expr)
   246  	if err != nil {
   247  		return "", err
   248  	}
   249  
   250  	return "set watchpoint: " + watch_expr, nil
   251  }
   252  
   253  // =========== delw ==============
   254  
   255  type delw struct{}
   256  
   257  func (c *delw) Command() string {
   258  	return "delw"
   259  }
   260  
   261  func (c *delw) Syntax() string {
   262  	return fmt.Sprintf("%s", "<index>")
   263  }
   264  
   265  func (c *delw) Usage() string {
   266  	return "delw <index>"
   267  }
   268  
   269  func (c *delw) Describe() string {
   270  	return "delete watchpoint"
   271  }
   272  
   273  func (c *delw) Validate(args string) error {
   274  
   275  	_, err := c.parse(args)
   276  
   277  	return err
   278  }
   279  
   280  func (c *delw) parse(args string) (uint64, error) {
   281  	splitArgs := context.SplitSpaceAndAccent(args, false)
   282  	if len(splitArgs) < 1 {
   283  		return 0, fmt.Errorf("need an arguments. usage: %s", c.Usage())
   284  	}
   285  
   286  	idx, err := strconv.ParseUint(splitArgs[0].Text, 10, 64)
   287  	if err != nil {
   288  		return 0, fmt.Errorf("fail to parse number %s: %s", splitArgs[0].Text, err.Error())
   289  	}
   290  
   291  	return idx, nil
   292  }
   293  
   294  func (c *delw) Run(args string) (string, error) {
   295  	idx, _ := c.parse(args)
   296  
   297  	err := contract.DelWatchPoint(idx)
   298  	if err != nil {
   299  		return "", err
   300  	}
   301  
   302  	return "del watchpoint: " + fmt.Sprintf("%d", idx), nil
   303  }
   304  
   305  // =========== listw ==============
   306  
   307  type listw struct{}
   308  
   309  func (c *listw) Command() string {
   310  	return "listw"
   311  }
   312  
   313  func (c *listw) Syntax() string {
   314  	return ""
   315  }
   316  
   317  func (c *listw) Usage() string {
   318  	return "listw"
   319  }
   320  
   321  func (c *listw) Describe() string {
   322  	return "list all watchpoints"
   323  }
   324  
   325  func (c *listw) Validate(args string) error {
   326  	return nil
   327  }
   328  
   329  func (c *listw) Run(args string) (string, error) {
   330  	watchpoints := contract.ListWatchPoints()
   331  	i := 0
   332  	for e := watchpoints.Front(); e != nil; e = e.Next() {
   333  		i++
   334  		fmt.Printf("%d: %s\n", i, e.Value)
   335  	}
   336  
   337  	return "list watchpoints", nil
   338  }
   339  
   340  // =========== resetb ==============
   341  
   342  type resetw struct{}
   343  
   344  func (c *resetw) Command() string {
   345  	return "resetw"
   346  }
   347  
   348  func (c *resetw) Syntax() string {
   349  	return ""
   350  }
   351  
   352  func (c *resetw) Usage() string {
   353  	return "resetw"
   354  }
   355  
   356  func (c *resetw) Describe() string {
   357  	return "reset all watchpoints"
   358  }
   359  
   360  func (c *resetw) Validate(args string) error {
   361  	return nil
   362  }
   363  
   364  func (c *resetw) Run(args string) (string, error) {
   365  	contract.ResetWatchPoints()
   366  
   367  	return "reset watchpoints", nil
   368  }
   369  
   370  // =====================================
   371  //             interfaces
   372  // =====================================
   373  
   374  func resetContractInfoInterface() {
   375  	contract.ResetContractInfo()
   376  }
   377  
   378  func updateContractInfoInterface(contractName string, defPath string) {
   379  	contract.UpdateContractInfo(
   380  		contract.PlainStrToHexAddr(contractName), defPath)
   381  }