• 2022-06-28 用迂回的方法在 lldb 显示 thread_local 变量内容


    用迂回的方法在 lldb 显示 thread_local 变量内容


    前言

    目前,貌似 lldb 还不支持 thread_local 变量的显示,有时需要debug,不容易搞定。

    那我们就帮这种变量搞个镜子,用同类型非 thread_local 变量,在任何赋值的时候把值赋给它,通过一个镜子看到它。


    举个例子

    以下是 thread_local 的典型用例:分级锁,通过将锁分级,解决在不得不多次互斥的程序中避免死锁问题。

    先加高级锁,再加低级锁,只要顺序统一,基本很难死锁,并且用错级别会直接报错,不会出现运行时的 幽灵bug

    为了实现级别比较,我们为每个线程设计一个线程存储期静态变量:

        static thread_local uint32_t thisThreadLevelVal;
    
    • 1

    为了监视此线程存储期静态变量,我们添加一个镜子:

        uint32_t thisValPtr = ULONG_MAX;
    
    • 1

    并初始化为与线程存储期静态变量相同值,并使得两变量始终一致。间接进行捕获。

    #include <iostream>
    #include <mutex>
    #include <thread>
    
    struct levelMutex
    {
        explicit levelMutex(uint32_t value)
            : levelVal(value)
            , preLevelVal(0)
        {}
    
        void lock()
        {
            checkForLevelErr();
            inMutex.lock();
            updateLevelVal();
        }
    
        void unlock()
        {
            if (thisThreadLevelVal != levelVal)
            {
                throw std::logic_error("mutex level err");
            }
            thisThreadLevelVal = preLevelVal;
            thisValPtr = thisThreadLevelVal;
            inMutex.unlock();
        }
    
        auto tryLock() -> bool
        {
            checkForLevelErr();
            if (!inMutex.try_lock())
            {
                return false;
            }
            updateLevelVal();
            return true;
        }
    
      private:
        void checkForLevelErr() const
        {
            if (thisThreadLevelVal <= levelVal)
            {
                throw std::logic_error("mutex level err");
            }
        }
    
        void updateLevelVal()
        {
            preLevelVal = thisThreadLevelVal;
            thisThreadLevelVal = levelVal;
            thisValPtr = thisThreadLevelVal;
            fprintf(stdout, "%d\n", thisThreadLevelVal);
        }
    
        //内部互斥量
        std::mutex inMutex;
        //等级值
        uint32_t const levelVal;
        //前一个等级值
        uint32_t preLevelVal;
        //本线程等级值
        static thread_local uint32_t thisThreadLevelVal;
    
        uint32_t thisValPtr = ULONG_MAX;
    };
    
    thread_local uint32_t levelMutex::thisThreadLevelVal(ULONG_MAX);
    
    levelMutex highLevelMutex(10000);
    levelMutex lowLevelMutex(5000);
    levelMutex otherMutex(6000);
    
    auto doLowLevelStuff() -> int
    {
        return 1;
    }
    
    auto lowLevelFunc() -> int
    {
        std::lock_guard<levelMutex> lk(lowLevelMutex);
        return doLowLevelStuff();
    }
    
    void highLevelStuff(int someParam)
    {
        std::cout << someParam << std::endl;
    }
    
    void highLevelFunc()
    {
        std::lock_guard<levelMutex> lk(highLevelMutex);
        highLevelStuff(lowLevelFunc());
    }
    
    void thread_a()
    {
        highLevelFunc();
    }
    
    void doOtherStuff()
    {
        std::cout << "other" << std::endl;
    }
    
    void otherStuff()
    {
        highLevelFunc();
        doOtherStuff();
    }
    
    void thread_b()
    {
        std::lock_guard<levelMutex> lk(otherMutex);
        otherStuff();
    }
    
    auto main() -> int
    {
        std::thread ta(thread_a);
        std::thread tb(thread_b);
        ta.join();
        tb.join();
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127

    基本解决。


    总结

    最近学并发编程,发现 lldb 和 gdb 对多线程调试并不完美,本文内容的写作原因是,涉及线程存储期变量调试的内容很少,整个互联网,只有 StackOverflow 有两个相关回答,但是答案是 lldb 不支持,所以,只能见招拆招了。

  • 相关阅读:
    mysql 存储过程 带游标
    认识伦敦银的真相,并没有那么容易
    IDEA 设置 Git 在左侧展示
    ElasticSearch 学习(docker,传统方式安装、安装遇到的问题解决,)
    计组实践实验9——使用CMStudio设计基于分段模型机微程序指令(2)
    2022!影响百万用户金融信用评分,Equifax被告上法庭,罪魁祸首——『数据漂移』!⛵
    ElasticSearch之安装
    JavaDS —— 单链表 与 LinkedList
    一个iOS tableView 滚动标题联动效果的实现
    Java锁lock的应用
  • 原文地址:https://blog.csdn.net/m0_54206076/article/details/125509430