github.com/aergoio/aergo@v1.3.1/contract/debug.c (about)

     1  // +build Debug
     2  
     3  #include <stdlib.h>
     4  #include "lua.h"
     5  
     6  #include "lualib.h"
     7  #include "lauxlib.h"
     8  
     9  // --- lua functions ---
    10  
    11  static int get_contract_info_lua(lua_State *L) {
    12      const char* contract_id_hex = luaL_checkstring (L, 1);
    13      
    14      char* contract_id_base58 = (char *)CGetContractID(contract_id_hex);
    15      char* src_path = (char *)CGetSrc(contract_id_hex);
    16      
    17      lua_pushstring(L, contract_id_base58);
    18      lua_pushstring(L, src_path);
    19      
    20      free(contract_id_base58);
    21      free(src_path);
    22  
    23      return 2; //base58 encoded address, srcpath
    24  }
    25  
    26  static int set_breakpoint_lua(lua_State *L) {
    27      const char* contract_name = luaL_checkstring (L, 1);
    28      double line = luaL_checknumber (L, 2);
    29  
    30      CSetBreakPoint(contract_name, line);
    31  
    32      return 0;
    33  }
    34  
    35  static int delete_breakpoint_lua(lua_State *L) {
    36      const char* contract_name = luaL_checkstring (L, 1);
    37      double line = luaL_checknumber (L, 2);
    38  
    39      CDelBreakPoint(contract_name, line);
    40  
    41      return 0;
    42  }
    43  
    44  
    45  static int has_breakpoint_lua(lua_State *L) {
    46      const char* contract_id_hex = luaL_checkstring (L, 1);
    47      double line = luaL_checknumber (L, 2);
    48  
    49      int exist = CHasBreakPoint(contract_id_hex, line);
    50  
    51      lua_pushboolean(L, exist);
    52      
    53      return 1;
    54  }
    55  
    56  static int print_breakpoints_lua(lua_State *L) {
    57      PrintBreakPoints();
    58  
    59      return 0;
    60  }
    61  
    62  static int reset_breakpoints_lua(lua_State *L) {
    63      ResetBreakPoints();
    64  
    65      return 0;
    66  }
    67  
    68  static int set_watchpoint_lua(lua_State *L) {
    69      const char* code = luaL_checkstring (L, 1);
    70      CSetWatchPoint(code);
    71  
    72      return 0;
    73  }
    74  
    75  static int delete_watchpoint_lua(lua_State *L) {
    76      double idx = luaL_checknumber (L, 1);
    77      CDelWatchPoint(idx);
    78  
    79      return 0;
    80  }
    81  
    82  static int reset_watchpoints_lua(lua_State *L) {
    83      ResetWatchPoints();
    84  
    85      return 0;
    86  }
    87  
    88  static int len_watchpoints_lua(lua_State *L) {
    89      int len = CLenWatchPoints();
    90  
    91      lua_pushnumber(L, len);
    92  
    93      return 1;
    94  }
    95  
    96  static int list_watchpoints_lua(lua_State *L) {
    97      int len = CLenWatchPoints();
    98      int i = 1;
    99  
   100      lua_newtable(L);
   101  
   102      for(i = 1; i <= len; i++) {
   103          char* watch_exp = CGetWatchPoint(i);
   104          lua_pushstring(L, watch_exp);
   105          lua_rawseti(L, -2, i);
   106          free(watch_exp);
   107      }
   108      
   109      return 1;
   110  }
   111  
   112  static int get_watchpoint_lua(lua_State *L) {
   113      double idx = luaL_checknumber (L, 1);
   114      char* watch_exp = CGetWatchPoint(idx);
   115      
   116      lua_pushstring(L, watch_exp);
   117      free(watch_exp);
   118  
   119      return 1;
   120  }
   121  
   122  const char* vm_set_debug_hook(lua_State *L)
   123  {
   124      lua_pushcfunction(L, get_contract_info_lua);
   125      lua_setglobal(L, "__get_contract_info");
   126      lua_pushcfunction(L, set_breakpoint_lua);
   127      lua_setglobal(L, "__set_breakpoint");
   128      lua_pushcfunction(L, delete_breakpoint_lua);
   129      lua_setglobal(L, "__delete_breakpoint");
   130      lua_pushcfunction(L, has_breakpoint_lua);
   131      lua_setglobal(L, "__has_breakpoint");
   132      lua_pushcfunction(L, print_breakpoints_lua);
   133      lua_setglobal(L, "__print_breakpoints");
   134      lua_pushcfunction(L, reset_breakpoints_lua);
   135      lua_setglobal(L, "__reset_breakpoints");
   136  
   137      lua_pushcfunction(L, set_watchpoint_lua);
   138      lua_setglobal(L, "__set_watchpoint");
   139      lua_pushcfunction(L, get_watchpoint_lua);
   140      lua_setglobal(L, "__get_watchpoint");
   141      lua_pushcfunction(L, delete_watchpoint_lua);
   142      lua_setglobal(L, "__delete_watchpoint");
   143      lua_pushcfunction(L, list_watchpoints_lua);
   144      lua_setglobal(L, "__list_watchpoints");
   145      lua_pushcfunction(L, reset_watchpoints_lua);
   146      lua_setglobal(L, "__reset_watchpoints");
   147      lua_pushcfunction(L, len_watchpoints_lua);
   148      lua_setglobal(L, "__len_watchpoints");
   149      
   150      char* code = (char *)GetDebuggerCode();
   151      luaL_loadstring(L, code);
   152  	int err = lua_pcall(L, 0, LUA_MULTRET, 0);
   153      free(code);
   154  	if (err != 0) {
   155  		return lua_tostring(L, -1);
   156  	}
   157  	return NULL;
   158  }