Linux 如何在 LD_PRELOAD 中使用 gdb
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10448254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use gdb with LD_PRELOAD
提问by MetallicPriest
I run a program with LD_PRELOADing a specific library. Like this.
我使用 LD_PRELOAD 运行一个特定库的程序。像这样。
LD_PRELOAD=./my.so ./my_program
How do I run this program with gdb
?
我如何运行这个程序gdb
?
回答by MetallicPriest
Do the following.
请执行下列操作。
gdb your_program
(gdb) set environment LD_PRELOAD ./yourso.so
(gdb) start
回答by Pavel ?imerda
You can basically do it the same way, just add gdb
before the program name:
你基本上可以用同样的方式来做,只需gdb
在程序名称前添加:
LD_PRELOAD=./my.so gdb ./my.program
You can check the environment variables using:
您可以使用以下方法检查环境变量:
(gdb) show environment LD_PRELOAD
In the rare case you actually need to change it inside gdb, e.g. when debugging a dlopen()
, you ca do that:
在极少数情况下,您实际上需要在 gdb 中更改它,例如在调试 a 时dlopen()
,您可以这样做:
(gdb) set environment LD_PRELOAD ./my.so
Oh, wait, it doesn't work for me with gdb 7.6.2! The library doesn't get loaded, that means none of the answer here are entirely correct, at least with current tools.
哦,等等,gdb 7.6.2 对我不起作用!该库没有被加载,这意味着这里没有一个答案是完全正确的,至少在当前的工具中是这样。
回答by Alexey Romanov
Posting because we ran into a case where set environment
didn't work:
发布是因为我们遇到了一个set environment
不起作用的案例:
From GDB documentation:
从GDB 文档:
set exec-wrapper wrapper show exec-wrapper unset exec-wrapper
When ‘exec-wrapper' is set, the specified wrapper is used to launch programs for debugging. gdb starts your program with a shell command of the form exec wrapper program. Quoting is added to program and its arguments, but not to wrapper, so you should add quotes if appropriate for your shell. The wrapper runs until it executes your program, and then gdb takes control.
You can use any program that eventually calls execve with its arguments as a wrapper. Several standard Unix utilities do this, e.g. env and nohup. Any Unix shell script ending with exec "$@" will also work.
For example, you can use env to pass an environment variable to the debugged program, without setting the variable in your shell's environment:
(gdb) set exec-wrapper env 'LD_PRELOAD=libtest.so' (gdb) run
set exec-wrapper wrapper show exec-wrapper unset exec-wrapper
当 'exec-wrapper' 被设置时,指定的包装器用于启动调试程序。gdb 使用 exec 包装程序形式的 shell 命令启动您的程序。引号被添加到程序及其参数中,但不会添加到包装器中,因此如果适合您的 shell,您应该添加引号。包装器运行直到它执行您的程序,然后 gdb 取得控制权。
您可以使用任何最终调用 execve 并将其参数作为包装器的程序。一些标准的 Unix 实用程序可以做到这一点,例如 env 和 nohup。任何以 exec "$@" 结尾的 Unix shell 脚本也可以使用。
例如,您可以使用 env 将环境变量传递给调试程序,而无需在 shell 环境中设置该变量:
(gdb) set exec-wrapper env 'LD_PRELOAD=libtest.so' (gdb) run
回答by user2394284
Here is a way to run everything (with arguments and environment) as one command:
这是一种将所有内容(带参数和环境)作为一个命令运行的方法:
Example:
例子:
gdb --args env LD_PRELOAD=/usr/local/lib/libstderred.so ls -l
The keen observer will notice that env
serves here as an exec wrapper (like Alexey Romanov's answer).
敏锐的观察者会注意到env
这里作为一个 exec 包装器(如 Alexey Romanov 的回答)。
回答by ecatmur
You can supply env
as an exec-wrapper on the command line using the -iex
flag:
您可以env
使用以下-iex
标志在命令行上作为 exec-wrapper 提供:
gdb -iex "set exec-wrapper env LD_PRELOAD=./my.so" ./my_program