LLDB自定义模板类打印

发布于 2025-01-19 15:31:37 字数 2039 浏览 1 评论 0原文

我将LLDB用作调试器,并希望它以自定义格式打印我的模板类myArray< n>

我阅读了LLDB文档,并提出了Python脚本,这些脚本可以吸引myArray< n>的公共和私人数据成员。但是,我不知道如何获得n(模板参数),我也不知道如何通过myArray< n> :: size()获得结果返回。

这是代码

#include <stdio.h>
#include <iostream>

template<int N>
class MyArray
{
public:
    MyArray(){data = new int[N];}
    ~MyArray(){if (data) delete[] data;}

    int size() const{ return N;}
    int& operator[](size_t i) { return data[i];}
    int const& operator[](size_t i) const { return data[i];}

private:
    int* data = nullptr;
};

template<int N>
std::ostream& operator <<(std::ostream& os, const MyArray<N>& arr)
{
    os << "N = " << arr.size() << std::endl;
    os << "elements in array:" << std::endl;
    for (int i = 0; i < arr.size(); i++) {
        if (i > 0) os << ", ";
        os << arr[i];
    }
    return os << std::endl;
}

int main()
{
    MyArray<10> arr;
    for (int i = 0; i < arr.size(); i++)
        arr[i] = 10 + i;
    std::cout << arr << std::endl;  // Yeah, I can use this for print. but I want this during LLDB debug

    return 0;
}

////更新:添加相应的LLDB配置 〜/.lldbinit

command script import ~/.lldbcfg/print_my_array.py

〜/.lldbcfg/print_my_array.py

def print_my_array(valobj, internal_dict):
    #N = valobj.GetChildMemberWithName("size") # failed
    N = 10
    data = valobj.GetChildMemberWithName("data")
    info = ''
    for i in range(N):
        if(i>0): info += ', '
        info += str(data.GetChildAtIndex(i).GetValueAsSigned(0))
    info += ')'
    return info

def __lldb_init_module(debugger, internal_dict):
    debugger.HandleCommand('type summary add -P MyArray<10> -F ' + __name__ + '.print_my_array')

I use LLDB as my debugger, and want it to print my template class MyArray<N> in a customized format.

I read the LLDB document, and come up with python script that can get public and private data members of MyArray<N>. However, I don't know how to get N (the template parameter), neither do I know how to get result return by MyArray<N>::size().

Here is the code

#include <stdio.h>
#include <iostream>

template<int N>
class MyArray
{
public:
    MyArray(){data = new int[N];}
    ~MyArray(){if (data) delete[] data;}

    int size() const{ return N;}
    int& operator[](size_t i) { return data[i];}
    int const& operator[](size_t i) const { return data[i];}

private:
    int* data = nullptr;
};

template<int N>
std::ostream& operator <<(std::ostream& os, const MyArray<N>& arr)
{
    os << "N = " << arr.size() << std::endl;
    os << "elements in array:" << std::endl;
    for (int i = 0; i < arr.size(); i++) {
        if (i > 0) os << ", ";
        os << arr[i];
    }
    return os << std::endl;
}

int main()
{
    MyArray<10> arr;
    for (int i = 0; i < arr.size(); i++)
        arr[i] = 10 + i;
    std::cout << arr << std::endl;  // Yeah, I can use this for print. but I want this during LLDB debug

    return 0;
}

//// Update: Add corresponding lldb config
~/.lldbinit:

command script import ~/.lldbcfg/print_my_array.py

~/.lldbcfg/print_my_array.py:

def print_my_array(valobj, internal_dict):
    #N = valobj.GetChildMemberWithName("size") # failed
    N = 10
    data = valobj.GetChildMemberWithName("data")
    info = ''
    for i in range(N):
        if(i>0): info += ', '
        info += str(data.GetChildAtIndex(i).GetValueAsSigned(0))
    info += ')'
    return info

def __lldb_init_module(debugger, internal_dict):
    debugger.HandleCommand('type summary add -P MyArray<10> -F ' + __name__ + '.print_my_array')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稀香 2025-01-26 15:31:37

简单的方法是将 N 的值存储为静态成员:

template<int N>
class MyArray
{
public:
    static constexpr const int n = N;
};

假设 MyArray 不是您的类型,您可以通过特征推断模板参数:

template <typename T>
struct get_value;

template <int N>
struct get_value<MyArray<N>> {
     static constexpr const n = N;
};

The simple way would be to store the value of N as static member:

template<int N>
class MyArray
{
public:
    static constexpr const int n = N;
};

Supposed MyArray is not your type you can infer the template argument via a trait:

template <typename T>
struct get_value;

template <int N>
struct get_value<MyArray<N>> {
     static constexpr const n = N;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文