美文网首页
golang错误追踪与调试

golang错误追踪与调试

作者: 李孝伟 | 来源:发表于2018-08-21 17:07 被阅读139次

    程序开发过程中总会遇到bug,所以bug的定位与分析就非常关键。
    golang里面定义了很多error,但有些时候只有error是很难定位到问题,所以还是需要堆栈信息。
    我们通常会先定义一个错误打印的函数,这样可以对错误进行统一的处理和分析:

    func ErrorPbResponse(errCode string, errMsg string) pb.Response {
        LogMessage("errcode[" + errCode + "] Errmsg:" + errMsg)
        
        if errCode == RESP_CODE_SYSTEM_ERROR {
            PrintStack() //堆栈函数
        }
           //TODO
    }
    
    

    那堆栈函数如下:

    func PrintStack() {
        var buf [4096]byte
        n := runtime.Stack(buf[:], false)
        fmt.Printf("==> %s\n", string(buf[:n]))
    }
    

    具体效果如下:

    errcode[5000] Errmsg:GetDataByCompositeKey 解密数据错误签名已失效
    ==> goroutine 374 [running]:
    main.PrintStack()
        /chaincode/input/src/github.com/hyperledger/fabric/lychee/chaincode/go/cc_bscf/tool.go:54 +0x5b
    main.ErrorPbResponse(0xb043e0, 0x4, 0xc420019c80, 0x37, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
        /chaincode/input/src/github.com/hyperledger/fabric/lychee/chaincode/go/cc_bscf/tool.go:47 +0x2b4
    main.agentCountQuery(0x1075c20, 0xc42007f680, 0xc42000c230, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
        /chaincode/input/src/github.com/hyperledger/fabric/lychee/chaincode/go/cc_bscf/invoke_common.go:1263 +0x512
    main.(*SmartContract).Invoke(0x10c8d00, 0x1075c20, 0xc42007f680, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
        /chaincode/input/src/github.com/hyperledger/fabric/lychee/chaincode/go/cc_bscf/invoke_main.go:122 +0x193
    github.com/hyperledger/fabric/core/chaincode/shim.(*Handler).handleTransaction.func1(0xc4201e7dc0, 0xc4203910e0)
        /opt/gopath/src/github.com/hyperledger/fabric/core/chaincode/shim/handler.go:329 +0x4f3
    created by github.com/hyperledger/fabric/core/chaincode/shim.(*Handler).handleTransaction
        /opt/gopath/src/github.com/hyperledger/fabric/core/chaincode/shim/handler.go:295 +0x49
    

    问题就能快速定位了!

    相关文章

      网友评论

          本文标题:golang错误追踪与调试

          本文链接:https://www.haomeiwen.com/subject/whbdiftx.html