zoukankan      html  css  js  c++  java
  • LUA debug 工具函数

    local print = print
    local debug = debug
    local string = string
    local io = io
    local type = type
    local pairs = pairs
    
    module "dt"
    
    function bt()
        local dinfo = debug.getinfo(2)
        local func_line = dinfo.linedefined
        local filename = dinfo.short_src
        local func_name = dinfo.name or"main"
        print ("func_line:", func_line, filename, func_name)
    
        local a =1
        local var_tb ={}
        while true
        do
            local name, value = debug.getlocal(2, a)
            if not name then break end
    
            var_tb[name]= value
            print(string.format("%s=", name), value)
            a = a +1
        end
    
        while true
        do
            io.write(string.format(">>"))
            local cmd = io.read()
            if cmd =="c"or cmd =="n"or cmd =="go"or cmd =="g"
            then
                break
            end
    
            local b = string.find(cmd," ")
            if b ==nil
            then
                print ("usage p + var")
            else
                local sub_cmd = string.sub(cmd,1, b-1)
                if sub_cmd =="p"or sub_cmd =="print"
                    or sub_cmd =="dump"or sub_cmd =="d"
                then
                    local sub_var = string.sub(cmd, b +1)
                    local value = var_tb[sub_var]
                    print(string.format("%s=", sub_var), value)
    
                    if type(value)=="table"
                    then
                        for k, v in pairs(value)
                        do
                            print("", k , v)
                        end
                    end
                else
                    print ("usage p + var", sub_cmd)
                end
            end
        end
    end
    
    --[[ 示例
    
    require "dt"
    dt.bt()
    
    --]]
  • 相关阅读:
    nyoj 202红黑树 (搜索)
    POJ 3281 Dining(最大流)
    nyoj-488 素数环 +nyoj -32 组合数 (搜索)
    LeetCode100:Same Tree
    LeetCode283:Move Zeros
    Leetcode226:Invert Binary Tree
    LeetCode258:Add Digits
    Leetcode237:Delete Node in a Linked List
    LeetCode7:Reverse Integer
    LeetCode292:Nim Game
  • 原文地址:https://www.cnblogs.com/zhiranok/p/LUA_debug.html
Copyright © 2011-2022 走看看