获取c+的调用堆栈+;包括共享库的应用程序 我编写了一个小函数,用C++来创建一个调用堆栈,用BFD来解析地址。它工作得很好,我得到了当前应用程序中所有函数的详细信息(源文件和行),但我没有得到关于我的应用程序中包含的共享库的任何信息

获取c+的调用堆栈+;包括共享库的应用程序 我编写了一个小函数,用C++来创建一个调用堆栈,用BFD来解析地址。它工作得很好,我得到了当前应用程序中所有函数的详细信息(源文件和行),但我没有得到关于我的应用程序中包含的共享库的任何信息,c++,linux,binutils,bfd,C++,Linux,Binutils,Bfd,例如: callstack: [0x00002b54229ba6d3 .none] <not sectioned address> [0x00002b5422927907 .none] <not sectioned address> [0x00002b54229286d0 .none] <not sectioned address> [0x00000000004f8608 .text] tensorNetwork.hxx:63 (inside operator

例如:

callstack:
[0x00002b54229ba6d3 .none] <not sectioned address>
[0x00002b5422927907 .none] <not sectioned address>
[0x00002b54229286d0 .none] <not sectioned address>
[0x00000000004f8608 .text] tensorNetwork.hxx:63 (inside operator())
[0x00000000005528da .text] /usr/include/c++/4.8/functional:2058 (inside _M_invoke)
[0x000000000058231c .text] /usr/include/c++/4.8/functional:2469 (inside std::function<bool ()>::operator()() const)
[0x00000000005806c0 .text] test.cpp:26 (inside ___test(std::pair<std::string, std::function<bool ()> > const&))
[0x0000000000581693 .text] test.cpp:119 (inside main)
[0x00002b5423fdebe5 .none] <not sectioned address>
[0x000000000042c129 .text] /home/abuild/rpmbuild/BUILD/glibc-2.18/csu/../sysdeps/x86_64/start.S:125 (inside _start)
调用堆栈: [0x00002b54229ba6d3.无] [0x00002b5422927907.无] [0x00002b54229286d0.无] [0x00000000004f8608.text]tensorNetwork.hxx:63(内部运算符()) [0x00000000005528da.text]/usr/include/c++/4.8/functional:2058(内部调用) [0x000000000058231c.text]/usr/include/c++/4.8/functional:2469(在std::function::operator()常量中) [0x00000000005806c0.text]test.cpp:26(内部测试(std::pair const&)) [0x0000000000581693.text]test.cpp:119(主管道内) [0x00002b5423fdebe5.无] [0x000000000042c129.text]/home/abuild/rpmbuild/BUILD/glibc-2.18/csu/。/sysdeps/x86_64/start.S:125(内部启动) 如您所见,与应用程序链接的可执行文件和静态对象的符号已正确解析,但高范围(例如0x00002b54229ba6d3)中的地址未正确解析。这些地址不是我的应用程序或共享库文件的一部分。因此,像
addr2line
这样的其他工具也无法重建该指令的位置


我不能用bfd工具解析这些地址,这在某种程度上是意料之中的,只要我只打开光盘上的文件来获取符号(我目前所做的是
abfd=bfd_openr(“/proc/self/exe”,0);
),那么有没有办法获取当前运行进程的bfd(从而包括共享库的部分)?如果没有:如何获取加载的共享对象及其偏移量的列表,以及如何将这些偏移量与光盘上的共享对象文件关联(以便我可以单独加载.so文件的bfd)?

glibc向名为的
dl
库添加一个函数。使用它可以找到共享对象的文件名及其加载的内存偏移量。使用
bfd\u openr
加载这些对象文件,使我可以转储源文件和行,类似于addr2line的方式(但如果这些信息不可用,仍会转储一些信息)。例如(请注意,
libc6.so
在我的系统上不包含调试符号,因此只显示最近的导出符号):

调用堆栈: [0x00002b3d744b4340.text]/homes/numerik/huber/store/code/tensorDev/algorithm/als.hpp:11(内部xerus::ALSVariant::lapack_解算器(xerus::Tensor网络常数&,xerus::Tensor&,xerus::Tensor常数&) [0x00000000000571f0a.text]/usr/include/c++/4.8/functional:2073(内部标准::_函数(处理程序)::_M(标准:)调用(标准:(u任意)数据常量&,xerus::Tensor网络常量&,xerus::Tensor&,xerus::Tensor常量&) [0x00002b3d744ddcba.text]/usr/include/c++/4.8/functional:2469(内部std::function::operator()(xerus::TensorNetwork const&,xerus::Tensor&,xerus::Tensor const&)const) [0x00002b3d744b98bc.text]/homes/numerik/huber/store/code/tensorDev/algorithm/als.hpp:117(xerus::ALSVariant::operator()内部(xerus::TTNetwork const&,xerus::TTNetwork&,xerus::TTNetwork const&,double,std::vector*)const) [0x0000000000547ac3.text]/homes/numerik/huber/store/code/tensorDev/unitTests/als.hxx:4(内部运算符()) [0x0000000000554744.text]/usr/include/c++/4.8/functional:2058(内部调用) [0x00000000005827c8.text]/usr/include/c++/4.8/functional:2469(在std::function::operator()常量中) [0x0000000000580b6c.text]/homes/numerik/huber/store/code/tensorDev/misc/test.cpp:26(内部测试(std::pair const&) [0x0000000000581b3f.text]/homes/numerik/huber/store/code/tensorDev/misc/test.cpp:119(主管道内) [0x00002b3d75b5dbe5.text]?:?(位于_ulibc_start_main+0x245内) [0x000000000042c269.text]/home/abuild/rpmbuild/BUILD/glibc-2.18/csu/。/sysdeps/x86_64/start.S:125(内部启动) 如果有人需要相同的功能(我发现很难创建一个好的调用堆栈),下面是源代码(作为我们的库的一部分,在AGPLv3下获得许可):

//Xerus-一个通用的张量库
//版权所有(C)2014-2015本杰明·胡伯和塞巴斯蒂安·沃尔夫。
// 
//Xerus是免费软件:您可以重新发布和/或修改它
//它是根据GNU Affero通用公共许可证的条款发布的
//通过自由软件基金会,许可证的版本3,
//或(由您选择)任何更高版本。
// 
//Xerus的发行是希望它会有用,
//但无任何保证;甚至没有任何关于
//适销性或适合某一特定目的。见
//GNU Affero通用公共许可证了解更多详细信息。
// 
//您应该已经收到GNU Affero通用公共许可证的副本
//还有施乐斯。如果没有,请参阅。
//
//欲了解更多有关施乐的信息,请访问https://libXerus.org 
//或联系我们contact@libXerus.org.
std::string demangle\u cxa(const std::string&\u cxa){
智力状态;
std::唯一的\u ptr实名;
realname.reset(abi::cxa_demangle(cxa.data()、0、0和状态));
如果(状态!=0){
返回cxa;
}
如果(实名){
返回std::string(realname.get());
}否则{
返回“”;
}
}
结构求解器{
struct storedBfd{
bfd*abfd;
asymbol**符号;
输入偏移量;
};
静态std::map-bfds;
静态布尔函数bfd_初始化;
静态标准::字符串解析(void*地址){
如果(!bfd_已初始化){
bfd_init();
bfd_initialized=true;
}
std::stringstream res;
res abfd,bfd_对象);
所需存储大小=bfd\u get\u symtab\u上限(newBfd->abfd);
newBfd->symbols=重新解释转换(新字符[需要存储]);
/*大小\u t numSymbols=*/bfd\u规范化\u符号选项卡(newBfd->abfd,newBfd->sym
callstack:
[0x00002b3d744b4340 .text] /homes/numerik/huber/store/code/tensorDev/algorithm/als.hpp:11 (inside xerus::ALSVariant::lapack_solver(xerus::TensorNetwork const&, xerus::Tensor&, xerus::Tensor const&))
[0x0000000000571f0a .text] /usr/include/c++/4.8/functional:2073 (inside std::_Function_handler<void (xerus::TensorNetwork const&, xerus::Tensor&, xerus::Tensor const&), void (*)(xerus::TensorNetwork const&, xerus::Tensor&, xerus::Tensor const&)>::_M_invoke(std::_Any_data const&, xerus::TensorNetwork const&, xerus::Tensor&, xerus::Tensor const&))
[0x00002b3d744ddcba .text] /usr/include/c++/4.8/functional:2469 (inside std::function<void (xerus::TensorNetwork const&, xerus::Tensor&, xerus::Tensor const&)>::operator()(xerus::TensorNetwork const&, xerus::Tensor&, xerus::Tensor const&) const)
[0x00002b3d744b98bc .text] /homes/numerik/huber/store/code/tensorDev/algorithm/als.hpp:117 (inside xerus::ALSVariant::operator()(xerus::TTNetwork<true> const&, xerus::TTNetwork<false>&, xerus::TTNetwork<false> const&, double, std::vector<double, std::allocator<double> >*) const)
[0x0000000000547ac3 .text] /homes/numerik/huber/store/code/tensorDev/unitTests/als.hxx:4 (inside operator())
[0x0000000000554744 .text] /usr/include/c++/4.8/functional:2058 (inside _M_invoke)
[0x00000000005827c8 .text] /usr/include/c++/4.8/functional:2469 (inside std::function<bool ()>::operator()() const)
[0x0000000000580b6c .text] /homes/numerik/huber/store/code/tensorDev/misc/test.cpp:26 (inside ___test(std::pair<std::string, std::function<bool ()> > const&))
[0x0000000000581b3f .text] /homes/numerik/huber/store/code/tensorDev/misc/test.cpp:119 (inside main)
[0x00002b3d75b5dbe5 .text] ??:? (inside __libc_start_main +0x245)
[0x000000000042c269 .text] /home/abuild/rpmbuild/BUILD/glibc-2.18/csu/../sysdeps/x86_64/start.S:125 (inside _start)
// Xerus - A General Purpose Tensor Library
// Copyright (C) 2014-2015 Benjamin Huber and Sebastian Wolf. 
// 
// Xerus is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
// 
// Xerus is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// 
// You should have received a copy of the GNU Affero General Public License
// along with Xerus. If not, see <http://www.gnu.org/licenses/>.
//
// For further information on Xerus visit https://libXerus.org 
// or contact us at contact@libXerus.org.

std::string demangle_cxa(const std::string &_cxa) {
    int status;
    std::unique_ptr<char[]> realname;
    realname.reset(abi::__cxa_demangle(_cxa.data(), 0, 0, &status));
    if (status != 0) {
        return _cxa;
    }
    if (realname) {
        return std::string(realname.get());
    } else {
        return "";
    }
}

struct bfdResolver {
    struct storedBfd {
        bfd* abfd;
        asymbol** symbols;
        intptr_t offset;
    };
    static std::map<void *, storedBfd> bfds;
    static bool bfd_initialized;


    static std::string resolve(void *address) {
        if (!bfd_initialized) {
            bfd_init();
            bfd_initialized = true;
        }

        std::stringstream res;
        res << "[0x" << std::setw((int)sizeof(void*)*2) << std::setfill('0') << std::hex << (uintptr_t)address;

        // get path and offset of shared object that contains this address
        Dl_info info;
        dladdr(address, &info);
        if (info.dli_fbase == nullptr) {
            return res.str()+" .?] <object to address not found>";
        }

        // load the corresponding bfd file (from file or map)
        if (bfds.count(info.dli_fbase) == 0) {
            std::unique_ptr<storedBfd> newBfd(new storedBfd);
            newBfd->abfd = bfd_openr(info.dli_fname, 0);
            if (!newBfd->abfd) {
                return res.str()+" .?] <could not open object file>";
            }
            bfd_check_format(newBfd->abfd,bfd_object);
            size_t storage_needed = bfd_get_symtab_upper_bound(newBfd->abfd);
            newBfd->symbols =reinterpret_cast<asymbol**>(new char[storage_needed]);
            /*size_t numSymbols = */bfd_canonicalize_symtab(newBfd->abfd, newBfd->symbols );

            newBfd->offset = (intptr_t)info.dli_fbase;

            bfds.insert(std::pair<void *, storedBfd>(info.dli_fbase, *newBfd.release()));
        } 

        storedBfd &currBfd = bfds.at(info.dli_fbase);

        asection *section = currBfd.abfd->sections;
        bool relative = section->vma < (uintptr_t)currBfd.offset;
//      std::cout << '\n' << "sections:\n";
        while (section != nullptr) {
            intptr_t offset = ((intptr_t)address) - (relative?currBfd.offset:0) - section->vma;
//          std::cout << section->name << " " << section->id << " file: " << section->filepos << " flags: " << section->flags 
//                      << " vma: " << std::hex << section->vma << " - " << std::hex << (section->vma+section->size) << std::endl;

            if (offset < 0 || (size_t)offset > section->size) {
                section = section->next;
                continue;
            }
            res << ' ' << section->name;
            if (!(section->flags | SEC_CODE)) {
                return res.str()+"] <non executable address>";
            }
            // get more info on legal addresses
            const char *file;
            const char *func;
            unsigned line;
            if (bfd_find_nearest_line(currBfd.abfd, section, currBfd.symbols, offset, &file, &func, &line)) {
                if (file) {
                    return res.str()+"] "+std::string(file)+":"+to_string(line)+" (inside "+demangle_cxa(func)+")";
                } else {
                    if (info.dli_saddr) {
                        return res.str()+"] ??:? (inside "+demangle_cxa(func)+" +0x"+std::to_string((intptr_t)address-(intptr_t)info.dli_saddr)+")";
                    } else {
                        return res.str()+"] ??:? (inside "+demangle_cxa(func)+")";
                    }
                }
            } else {
                return res.str()+"] <bfd_error> (inside "+demangle_cxa((info.dli_sname?info.dli_sname:""))+")";
            }
        }
//      std::cout << " ---- sections end ------ " << std::endl;
        return res.str()+" .none] <not sectioned address>";
    }
};

std::map<void *, bfdResolver::storedBfd> bfdResolver::bfds;
bool bfdResolver::bfd_initialized = false;






std::string get_call_stack() {
    const size_t MAX_FRAMES = 100;
    std::vector<void *> stack(MAX_FRAMES);
    int num = backtrace(&stack[0], MAX_FRAMES);
    if (num <= 0) {
        return "Callstack could not be built.";
    }
    stack.resize((size_t) num);
    std::string res;
    //NOTE i=0 corresponds to get_call_stack and is omitted
    for (size_t i=1; i<(size_t)num; ++i) {
        res += bfdResolver::resolve(stack[i]) + '\n';
    }
    return res;
}