makefile 中的 gdb 和 valgrind
我有一个非常基本的问题。我确实在这里环顾四周 http://www.cs.cmu.edu/~gilpin /tutorial/ 但仍然存疑..
考虑以下 makefile(在上一个问题中也给出了它)
all: clients.so simulator backup
LD_PRELOAD=/home/Juggler/client/clients.so ./simulator
backup: backup.c libclient.a
gcc backup.c -o backup -L /home/Juggler/client -L. -lclient -ldl
simulator: simulator.c libclient.a
gcc -g simulator.c -o simulator -L /home/Juggler/client -L. -lclient -ldl -pthread
libclient.a: libclient.o client.o
ar rcs libclient.a libclient.o client.o
libclient.o:libclient.c
gcc -c libclient.c -o libclient.o -pthread
clients.so: client.o client_invoke.o
ld -shared -o clients.so client_invoke.o client.o -ldl
client_invoke.o: client_invoke.c
gcc -Wall -fPIC -DPIC -c -g client_invoke.c
client.o: client.c
gcc -Wall -fPIC -DPIC -c -g client.c -ldl -pthread
我该怎么做/更改以使用 gdb 或 valgrind 进行调试。实际上,我在执行 make 时遇到分段错误,想进行调试。但我从未在 makefile 中使用过 gdb 或 valgrind
谢谢
I have a very basic question. I did look around like over here http://www.cs.cmu.edu/~gilpin/tutorial/ but still doubtfull..
Consider the following makefile(had also given it in a previous question)
all: clients.so simulator backup
LD_PRELOAD=/home/Juggler/client/clients.so ./simulator
backup: backup.c libclient.a
gcc backup.c -o backup -L /home/Juggler/client -L. -lclient -ldl
simulator: simulator.c libclient.a
gcc -g simulator.c -o simulator -L /home/Juggler/client -L. -lclient -ldl -pthread
libclient.a: libclient.o client.o
ar rcs libclient.a libclient.o client.o
libclient.o:libclient.c
gcc -c libclient.c -o libclient.o -pthread
clients.so: client.o client_invoke.o
ld -shared -o clients.so client_invoke.o client.o -ldl
client_invoke.o: client_invoke.c
gcc -Wall -fPIC -DPIC -c -g client_invoke.c
client.o: client.c
gcc -Wall -fPIC -DPIC -c -g client.c -ldl -pthread
What do I do/change to debug using either gdb or valgrind. Actually I am getting a segmentation fault while doing make and would like to debug. But I have never used gdb or valgrind from within a makefile
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我对你的演示文稿做了一个小修改。你最初写的是
你能把它改成新的形式并看看是否会出现段错误吗?
I made a small edit to your presentation. You originally wrote
Can you change it to the new form and see if make segfaults?
通常我会做一些事情来达到以下效果:
then
(从技术上讲,命令不需要 --args ,但如果您将来向模拟器添加参数。)
但是使用 LD_PRELOAD 会使这变得复杂,因为您可能不这样做不希望 gdb 加载 libclient
另外,通过 gcc -shared -o client.so 通过 gcc 链接共享库是正常的
normally I would do something to the effect of:
then
(technically the --args isn't needed for the command like it is, but if you add arguments to simulator in the future.)
but using LD_PRELOAD complicates this, because you probably don't want gdb loading libclient
Additionally its normal to link shared libraries through gcc via gcc -shared -o client.so
一种不优雅的方法是通过 makefile 生成进程,然后在另一个终端中使用 GDB 附加到它。使用linux:启动时挂起进程中的技巧,您可以启动该进程并让它立即挂起,然后与gdb连接。
有一个名为 launch.sh 的脚本:
#!/bin/bash
有一个像这样的 makefile 配方:
如果由于某种原因你看不到输出(比如你正在重定向),你仍然可以通过 ps -ef 或其他方式获取 PID。然后使用gdb:
One inelegant way would be to spawn the process via the makefile, and then attach to it with GDB in another terminal. Using the trick from linux: suspend process at startup, you can launch the process and have it suspend immediately, and then connect with gdb.
have a script called launch.sh:
#!/bin/bash
have a makefile recipe like this:
if for some reason you can't see the output (like if you are redirecting), you can still get the PID via ps -ef or something. then with gdb: