- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我用 c++ 编写了一个小函数来创建调用堆栈,使用 bfd 解析地址。它运行良好,我获得了当前应用程序中所有函数的详细信息(源文件和行),但我没有获得关于我的应用程序中包含的共享库的任何信息。
例如:
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)未正确解析。这些地址既不是我的应用程序的一部分,也不是我的共享库文件的一部分。因此,addr2line
等其他工具也无法重建该指令的位置。
只要我只打开光盘上的文件来获取符号,我就无法使用 bfd 工具解析这些地址(我目前所做的是 abfd = bfd_openr("/proc/self/exe ", 0);
), 那么有没有办法获取当前正在运行的进程的 bfd(因此包括共享库的部分)?如果不是:我怎样才能获得加载的共享对象列表及其偏移量,以及如何将这些偏移量与磁盘上的共享对象文件相关联(这样我就可以单独加载 .so 文件的 bfd)?
最佳答案
glibc 向dl
库添加了一个名为dladdr
的函数。 .使用它可以找到共享对象的文件名及其加载的内存偏移量。使用 bfd_openr
加载这些目标文件允许我转储源文件和行,类似于 addr2line 的处理方式(但仍然转储一些信息以防这些不可用)。例如(注意 libc6.so
在我的系统上不包含调试符号,因此只显示最接近的导出符号):
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)
如果有人需要相同的功能(我发现创建一个漂亮的调用堆栈非常困难),这里是源代码(作为我们在 AGPLv3 下许可的库的一部分):
// 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;
}
关于c++ - 获取包括共享库的 C++ 应用程序的调用堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29606627/
Java 库和 android 库有什么区别,各自有什么优点/缺点? 最佳答案 您可以在 Android 应用程序中包含标准 Java .jar 文件库。它们在 .apk 构建时被翻译成 Dalvik
所以,我现在的代码就像从 Java 层加载库(比如 liba.so),并在内部 liba.so 加载 libb.so。因此,如果我必须将所有库打包到 APK 中并将其安装在没有 root 访问权限的设
我想在我的系统中设置 LEDA 库。 我已经从以下链接下载了 LEDA 库 http://www.algorithmic-solutions.info/free/d5.php Instruct
我想用 autoconf 创建一个共享库。但是,我希望共享库具有“.so”扩展名,而不是以“lib”开头。基本上,我想制作一个加载 dlopen 的插件。 .是否有捷径可寻? 当我尝试使用 autoc
我需要在 Apps 脚本应用程序上修改 PDF。为此,我想使用 JS 库:PDF-LIB 我的代码: eval(UrlFetchApp.fetch("https://unpkg.com/pdf-lib
我正在构建一个使用以下 Boost header 的程序(我使用的是 Microsoft Visual C++ 10), #include #include #include #include
当我通过 cygwin 在 hadoop 上运行此命令时: $bin/hadoop jar hadoop-examples-*.jar grep input output 'dfs[a-z.]+' 我
我已经通过 vcpgk 成功安装了一个 C++ 库,名为:lmdb:x64-windows 我还安装了lmdb通过 Cabal 安装的 Haskell 绑定(bind)包 在尝试测试 lmdb 包时:
我该如何解决这个问题? 我刚刚将 javacv jar 文件复制到我的项目 Lib 文件夹下,但出现了这个错误! 我可以找到这个thread来自谷歌,但不幸的是,由于我国的谷歌限制政策,该页面无法打开
我有一个 Android 库项目 FooLib。 FooLib 引用 Android Context 之类的东西,但不需要任何资源文件(res/ 中的东西)所以我目前将其打包为供我的应用使用的 JAR
我正在开发一个 Android 应用程序(使用 Android Studio),它能够通过手势识别算法了解您正在进行的 Activity 。对于我使用 nickgillian ithub 帐户上可用的
关于从 .NET Framework 项目中引用 .NET Standard 类库的问题有很多类似的问题,其中 netstandard 库中的 NuGet 包依赖项不会流向 netframework
我已经从互联网上下载了 jna-4.2.2.jar,现在想将这个 jar 导入到我的项目中。但是当我试图将这个 jar 导入我的项目时,出现以下错误。 [2016-06-20 09:35:01 - F
我正在尝试通过编译在 Mac 上安装 rsync 3.2.3。但是,我想安装所有功能。为此,它需要一些库,此处 ( https://download.samba.org/pub/rsync/INSTA
进入 Web 开发有点困难。过去 5 年我一直致力于 winforms 工作。所以我正在努力从一种切换到另一种。前段时间,我使用过 JavaScript,但现在还没有大量的 JavaScript 库
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我正在寻找一个用Python编写的与logstash(ruby + java)类似的工具/库。 我的目标是: 从 syslog 中解析所有系统日志 解析应用程序特定日志(apache、django、m
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
我花了几天时间试图寻找用于 JavaPOS 实现的 .jar 库,但我找不到任何可以工作的东西。我找到了很多像这样的文档:http://jpos.1045706.n5.nabble.com/file/
这个问题在这里已经有了答案: Merge multiple .so shared libraries (2 个答案) 关闭 9 年前。 我有我在代码中使用的第三方库的源代码和对象。该库附带有关如何使
我是一名优秀的程序员,十分优秀!