makefile 中的 gdb 和 valgrind

发布于 2024-12-04 21:48:50 字数 1058 浏览 0 评论 0原文

我有一个非常基本的问题。我确实在这里环顾四周 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无人接听 2024-12-11 21:48:50

我对你的演示文稿做了一个小修改。你最初写的是

client.o: client.c     gcc -Wall -fPIC -DPIC -c -g client.c -ldl -pthread 

你能把它改成新的形式并看看是否会出现段错误吗?

I made a small edit to your presentation. You originally wrote

client.o: client.c     gcc -Wall -fPIC -DPIC -c -g client.c -ldl -pthread 

Can you change it to the new form and see if make segfaults?

薆情海 2024-12-11 21:48:50

通常我会做一些事情来达到以下效果:

   $(DEBUGGER) ./simulator

then

$ make DEBUGGER=gdb --args
$ make DEBUGGER=valgrind
$ make # should still work without the debugger.

(从技术上讲,命令不需要 --args ,但如果您将来向模拟器添加参数。)

但是使用 LD_PRELOAD 会使这变得复杂,因为您可能不这样做不希望 gdb 加载 libclient

   gdb -ex 'set env LD_PRELOAD=/home/Juggler/client/clients.so' -ex 'run' ./simulator

另外,通过 gcc -shared -o client.so 通过 gcc 链接共享库是正常的

normally I would do something to the effect of:

   $(DEBUGGER) ./simulator

then

$ make DEBUGGER=gdb --args
$ make DEBUGGER=valgrind
$ make # should still work without the debugger.

(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

   gdb -ex 'set env LD_PRELOAD=/home/Juggler/client/clients.so' -ex 'run' ./simulator

Additionally its normal to link shared libraries through gcc via gcc -shared -o client.so

悸初 2024-12-11 21:48:50

一种不优雅的方法是通过 makefile 生成进程,然后在另一个终端中使用 GDB 附加到它。使用linux:启动时挂起进程中的技巧,您可以启动该进程并让它立即挂起,然后与gdb连接。

有一个名为 launch.sh 的脚本:
#!/bin/bash

echo "Pid is $"
echo -n "Press Enter.." 
read 
exec $@

有一个像这样的 makefile 配方:

whatever_target: whatever_deps
        ./launch.sh PROGRAM [ARGS]

如果由于某种原因你看不到输出(比如你正在重定向),你仍然可以通过 ps -ef 或其他方式获取 PID。然后使用gdb:

gdb PROGRAM_NAME PID

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

echo "Pid is $"
echo -n "Press Enter.." 
read 
exec $@

have a makefile recipe like this:

whatever_target: whatever_deps
        ./launch.sh PROGRAM [ARGS]

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:

gdb PROGRAM_NAME PID
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文