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()
    
    --]]
  • 相关阅读:
    回答自己的提问
    <构建之法>13——17章的读后感
    <构建之法>10,11,12章的读后感
    <构建之法>8,9,10章的读后感
    作业5.2
    【作业报告】作业5 四则运算 测试与封装 5.1 改进版
    作业 5.1
    阅读
    做汉堡
    阅读《构建之法》
  • 原文地址:https://www.cnblogs.com/zhiranok/p/LUA_debug.html
Copyright © 2011-2022 走看看