简体   繁体   English

多线程调试C程序

[英]Debugging C program with multiple threads

I want to debug a C program running multiple TCP server threads.我想调试一个运行多个 TCP 服务器线程的 C 程序。 I cannot set up a sophisticated Debugging tool as I have to debug in an embedded linux(busy box).我无法设置复杂的调试工具,因为我必须在嵌入式 linux(忙框)中进行调试。 I hoped it would natively support gdb.我希望它本身就支持 gdb。

SO I started with gdb.所以我从 gdb 开始。 Once I type run, the server seems to run in the background but gdb returns the prompt and "Program received signal SIG64 (Real-time event 64)" message(related to pthread I guess).一旦我输入运行,服务器似乎在后台运行,但 gdb 返回提示和“程序收到信号 SIG64(实时事件 64)”消息(我猜与 pthread 相关)。 I know it has to do be something with the main getting forked into several threads.我知道这与主线程分叉成多个线程有关。 But I have no clue how to debug this.但我不知道如何调试它。 Any starting points would be so helpful.任何起点都会很有帮助。

Also, is there someother "trace" like debuggers, small footprint that I can use?另外,是否还有其他“跟踪”,例如调试器,我可以使用的占用空间小?

Please help请帮忙

Most often, debugging a multi-threaded application is difficult with a debugger. 大多数情况下,调试器很难调试多线程应用程序。 The best way is to either try and isolate the bug to a single-threaded case, or use debug prints in suspect locations until the bug is discovered. 最好的方法是尝试将错误隔离到单线程的情况,或者在可疑位置使用调试打印,直到发现错误。

It's no help with your specific issue, but it's the best advice I've learned while working with multi-threaded applications, especially embedded. 这对您的具体问题没有帮助,但这是我在使用多线程应用程序时学到的最好建议,尤其是嵌入式应用程序。

See in such case I usually do this: 在这种情况下,我通常会这样做:

  1. Make a thread wise log file and have all stdout and stderr output redirect in that log file ... maybe this will help you for that: In multi thread application how can i redirect stderr & stdout in separate file as per thread? 创建一个线程明智的日志文件,并在该日志文件中重定向所有stdoutstderr输出...也许这将帮助您: 在多线程应用程序中如何根据线程将stderr&stdout重定向到单独的文件中?

  2. Keep track of global variables between all threads. 跟踪所有线程之间的全局变量。 Improper use of global variables tends to cause problems. 不正确地使用全局变量往往会导致问题。

  3. If you are using a mutex then check that it will not create deadlocks. 如果您使用的是互斥锁,请检查它是否会产生死锁。 In conditional & semaphore design always try to track of all those threads on paper. 在条件和信号量设计中,总是试图在纸上跟踪所有这些线程。

I recommend you to use memory access check program like valgrind. 我建议你使用像valgrind这样的内存访问检查程序。 In my case, many of bugs are caused by illegal memory handling. 就我而言,许​​多错误都是由非法内存处理引起的。 It's hard to find bug on multi-threaded program so using memory leak checking program is a better way to figure out bugs causes. 很难找到多线程程序的bug,所以使用内存泄漏检查程序是找出错误原因的更好方法。

GDB has a few functions for debugging with multiple threads. GDB 有一些用于多线程调试的函数。 For example, you can run the command i threads to view the currently existing threads.例如,您可以运行命令i threads查看当前存在的线程。 Here is a link that may be helpful: https://ftp.gnu.org/old-gnu/Manuals/gdb/html_node/gdb_24.html .这是一个可能有用的链接: https : //ftp.gnu.org/old-gnu/Manuals/gdb/html_node/gdb_24.html

some tricky idea .. 一些棘手的想法..

  1. add sleep(some times) into the thread to debug as starting point 将sleep(有时)添加到线程中以作为起始点进行调试
  2. after program is running, check pid of thread (using ps with -L option) 程序运行后,检查线程的pid(使用带-L选项的ps)
  3. run gdb program {pid} or call attach {pid} in gdb prompt 运行gdb程序{pid}或在gdb提示符中调用attach {pid}
  4. after sleep, u can trace next step for that thread 睡眠之后,你可以追踪该线程的下一步

Don't forget attaching must be done before sleep time is gone. 不要忘记在睡眠时间消失之前必须完成附着。

As remarked above, testing on single thread or using text logging facility is good choice. 如上所述,在单线程上测试或使用文本记录工具是不错的选择。

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

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