简体   繁体   English

我如何gdb附加到在docker容器中运行的进程?

[英]How can I gdb attach to a process running in a docker container?

I have a long-running process in a docker container to which I would like to attach gdb to look at what threads are running and get stacktraces. 我在docker容器中有一个长时间运行的进程,我想附加gdb来查看正在运行的线程并获取stacktraces。 I can attach to the process from the host, but I cannot resolve any symbols because the executable file is in a different location in the filesystem (it's in a docker-mounted volume) and the shared system libraries are all stuck in a docker filesystem image somewhere in /var/lib/docker. 我可以从主机附加到进程,但是我无法解析任何符号,因为可执行文件位于文件系统中的不同位置(它位于装载器的卷中)并且共享系统库全部停留在docker文件系统映像中在/ var / lib / docker中的某个地方。

I am able to generate a core file and use gdb to look at it by specifying the host's path to the executable file, but because the system libraries are all in the wrong places and are loaded in the wrong locations in the corefile, I get no information from that. 我能够生成一个核心文件并使用gdb通过指定主机的可执行文件路径来查看它,但由于系统库都在错误的位置并且被加载到corefile中的错误位置,我得不到来自那里的信息。

Do I have any options that I've overlooked? 我有任何我忽略的选择吗?

You can attach to the process with gdb instance running in your container by attaching to the running container via lxc-attach . 您可以通过lxc-attach附加到正在运行的容器来附加到容器中运行的gdb实例的进程。

Note: gdb has to be already installed in that container or you have to install it. 注意:必须已在该容器中安装gdb,否则必须安装它。

# find your container ID
sudo docker ps
# list of your containers - container ID is 1234567890
# find your full container ID
sudo docker ps --no-trunc -q| grep <short ID>
sudo lxc-attach -n <container long ID>

root@1234567890:/#
# optionally, you can install gdb now if it is not installed
# yum install gdb

root@1234567890:/# gdb
...
(gdb) attach 1

UPDATE 2017-04: 更新2017-04:

There is an easier workflow using docker exec now available (thanks to @42n4). 现在可以使用docker exec更简单的工作流程(感谢@ 42n4)。

# find your container ID
sudo docker ps
# list of your containers - container ID is 1234567890
docker exec -i -t 1234567890 /bin/bash

root@1234567890:/#
# optionally, you can install gdb now if it is not installed
# yum install gdb

root@1234567890:/# gdb
...
(gdb) attach 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM