[英]Using GDB in linux if running application from a script
Normally we run gdb with gdb command and our application name.通常我们使用 gdb 命令和我们的应用程序名称运行 gdb。 But if we want to run our application from a script eg my_script.sh as./my_script.sh argument and this starts our application.
但是,如果我们想从脚本运行我们的应用程序,例如 my_script.sh as./my_script.sh 参数,这将启动我们的应用程序。 Then how to use gdb without including any command in script.
那么如何在脚本中不包含任何命令的情况下使用 gdb。
I want something like this: gdb./my_script.sh argument.我想要这样的东西:gdb./my_script.sh 参数。 So that whatever application will be started from script I can debug it.
这样无论从脚本启动什么应用程序,我都可以对其进行调试。
Normally we run gdb with
gdb command
and our application name.通常我们使用
gdb command
和我们的应用程序名称运行 gdb。
This is wrong .这是错误的。 You use
gdb
on some binary executable file (in ELF format) or (with -p
) on a running process .您在某些二进制可执行文件(ELF 格式)或(带有
-p
)正在运行的进程上使用gdb
。 For example, you cannot run
but gdb cd
cd
is a shell builtin command (see also this ).例如,您不能运行
但gdb cd
cd
是 shell 内置命令(另请参阅此)。 BTW an executable file can have several names (eg /bin/rbash
is a symbolic link to /bin/bash
on my Debian computer), and programs can be started without being "applications" on your desktop menu.顺便说一句,可执行文件可以有多个名称(例如,
/bin/rbash
是我的 Debian 计算机上的/bin/bash
的符号链接),并且可以在桌面菜单上的“应用程序”中启动程序。 For example, my g++
executable would run /usr/lib/gcc/x86_64-linux-gnu/9/cc1plus
.例如,我的
g++
可执行文件将运行/usr/lib/gcc/x86_64-linux-gnu/9/cc1plus
。 Check with strace(1) or by diving into the source code of GCC , or simply adding -v
inside your g++
command.使用strace(1)或深入研究 GCC 的源代码,或在
g++
命令中简单地添加-v
进行检查。
and the gdb(1) and gdbserver(1) man page.以及gdb(1)和gdbserver(1)手册页。 Run also in a terminal
gdb --help
to understand how you could start it.也在终端
gdb --help
中运行以了解如何启动它。
You might start (or improve your my_script.sh
shell script to run) your ELF executable yourprog
(obtained by compiling your C++ code) using您可以开始(或改进您的
my_script.sh
shell 脚本以运行)您的ELF可执行文件yourprog
(通过编译您的 C++ 代码获得)使用
gdb --args yourprog arg1toit arg2toit
and you could even use some pipeline (see pipe(7) or even unix(7) sockets) to pour commands into it.你甚至可以使用一些管道(参见pipe(7)甚至unix(7)套接字)将命令注入其中。 Of course
yourprog
should be compiled with g++ -Wall -g
and perhaps also -O
(assuming a recent enough GCC ).当然
yourprog
应该用g++ -Wall -g
或者-O
编译(假设足够近的GCC )。
In addition, recent GDB can be scripted or extended in Python or in Guile .此外,最近的 GDB 可以在Python或Guile中编写或扩展。 You might need to compile and configure GDB from its source code , which you are allowed to do since it is free software .
您可能需要从其源代码编译和配置 GDB,因为它是免费软件,您可以这样做。
If you need to debug a bash
shell script, consider using bashdb
, a shell script debugger .如果您需要调试
bash
shell 脚本,请考虑使用bashdb
, shell 脚本调试器。 Of course spend an hour in reading its documentation.当然要花一个小时阅读它的文档。 But if you want to debug a script coded in
bash
, why is your question tagged C++?但是,如果您想调试在
bash
中编码的脚本,为什么您的问题被标记为 C++?
Read alsoHow to debug small programs另请阅读如何调试小程序
However, a process won't be able to debug directly itself without precautions.但是,如果没有预防措施,进程将无法直接调试自身。 See ptrace(2) for details.
有关详细信息,请参阅ptrace(2) 。
I want something like this:
gdb./my_script.sh
argument.我想要这样的东西:
gdb./my_script.sh
参数。
GDB can (on recent Linux distributions) only debug an ELF executable (but not a shell script), preferably with DWARF debug information. GDB 可以(在最近的 Linux 发行版上)只能调试 ELF可执行文件(但不能调试 shell 脚本),最好使用DWARF调试信息。 Design your
my_script.sh
to sometimes run gdb
.设计您的
my_script.sh
以有时运行gdb
。 That debugged executable would be often compiled with GCC or Clang compilers (and -g
options) from some C++ or C source code. That debugged executable would be often compiled with GCC or Clang compilers (and
-g
options) from some C++ or C source code. You could find other compilers ( Ocaml , Rust , Go , ...) capable (with care) to produce ELF executables with DWARF debug information.您可以找到其他能够(小心)生成带有 DWARF 调试信息的 ELF 可执行文件的编译器( Ocaml 、 Rust 、 Go ,...)。
If your my_script.sh
is written for GNU bash (so starts with #!/bin/bash
, see execve(2) for details), read the documentation of GNU bash .如果您的
my_script.sh
是为 GNU bash 编写的(因此以#!/bin/bash
开头,请参阅execve(2)了解详细信息),请阅读GNU bash的文档。 You might consider rewriting your script in Python or in Guile or in Lua , since all these scripting languages are more expressive than POSIX shells.您可能会考虑在Python或Guile或Lua中重写您的脚本,因为所有这些脚本语言都比 POSIX shell 更具表现力。 In all cases, read the documentation of the scripting language you have chosen to use .
在所有情况下,请阅读您选择使用的脚本语言的文档。 Most of them have debugging facilities.
他们中的大多数都有调试设施。 (you could replace
#!/bin/bash
with a line like #!/bin/bash -vx
as the first line of your shell script). (您可以用
#!/bin/bash
#!/bin/bash -vx
作为 shell 脚本的第一行)。
NB.注意。 Notice that
/usr/bin/firefox
is often a script starting some /usr/bin/firefox.real
ELF executable.请注意,
/usr/bin/firefox
通常是启动某些/usr/bin/firefox.real
ELF 可执行文件的脚本。 You could glance inside it for inspiration.你可以看看里面的灵感。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.